blob: 82fcb5f9faf0e937b03b2855f7a56cec93799dc4 [file] [log] [blame]
Paul Mundtb3c185a2012-06-20 17:29:04 +09001/*
2 * SuperH Pin Function Controller GPIO driver.
3 *
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010011
12#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
Paul Mundtb3c185a2012-06-20 17:29:04 +090013
Laurent Pinchart1724acf2012-12-15 23:50:48 +010014#include <linux/device.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090015#include <linux/gpio.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010016#include <linux/init.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090017#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090018#include <linux/pinctrl/consumer.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010019#include <linux/slab.h>
20#include <linux/spinlock.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090021
Laurent Pinchartf9165132012-12-15 23:50:44 +010022#include "core.h"
23
Paul Mundtb3c185a2012-06-20 17:29:04 +090024struct sh_pfc_chip {
25 struct sh_pfc *pfc;
26 struct gpio_chip gpio_chip;
27};
28
29static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
30{
31 return container_of(gc, struct sh_pfc_chip, gpio_chip);
32}
33
34static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
35{
36 return gpio_to_pfc_chip(gc)->pfc;
37}
38
Laurent Pinchart16883812012-12-06 14:49:25 +010039/* -----------------------------------------------------------------------------
40 * Pin GPIOs
41 */
42
43static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090044{
Laurent Pinchart16883812012-12-06 14:49:25 +010045 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090046}
47
Laurent Pinchart16883812012-12-06 14:49:25 +010048static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090049{
Laurent Pinchart16883812012-12-06 14:49:25 +010050 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090051}
52
Laurent Pinchart16883812012-12-06 14:49:25 +010053static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +090054{
55 struct pinmux_data_reg *dr = NULL;
56 int bit = 0;
57
Laurent Pinchart16883812012-12-06 14:49:25 +010058 if (sh_pfc_get_data_reg(pfc, offset, &dr, &bit) != 0)
Paul Mundtb3c185a2012-06-20 17:29:04 +090059 BUG();
Laurent Pinchart16883812012-12-06 14:49:25 +010060
61 sh_pfc_write_bit(dr, bit, value);
Paul Mundtb3c185a2012-06-20 17:29:04 +090062}
63
Laurent Pinchart16883812012-12-06 14:49:25 +010064static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090065{
Laurent Pinchart16883812012-12-06 14:49:25 +010066 return pinctrl_gpio_direction_input(offset);
67}
68
69static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
70 int value)
71{
72 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
73
74 return pinctrl_gpio_direction_output(offset);
75}
76
77static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
78{
79 struct sh_pfc *pfc = gpio_to_pfc(gc);
Paul Mundtb3c185a2012-06-20 17:29:04 +090080 struct pinmux_data_reg *dr = NULL;
81 int bit = 0;
82
Laurent Pinchart16883812012-12-06 14:49:25 +010083 if (sh_pfc_get_data_reg(pfc, offset, &dr, &bit) != 0)
Paul Mundtb3c185a2012-06-20 17:29:04 +090084 return -EINVAL;
85
86 return sh_pfc_read_bit(dr, bit);
87}
88
Laurent Pinchart16883812012-12-06 14:49:25 +010089static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +090090{
Laurent Pinchart16883812012-12-06 14:49:25 +010091 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +090092}
93
Laurent Pinchart16883812012-12-06 14:49:25 +010094static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090095{
96 struct sh_pfc *pfc = gpio_to_pfc(gc);
97 pinmux_enum_t enum_id;
98 pinmux_enum_t *enum_ids;
99 int i, k, pos;
100
101 pos = 0;
102 enum_id = 0;
103 while (1) {
104 pos = sh_pfc_gpio_to_enum(pfc, offset, pos, &enum_id);
105 if (pos <= 0 || !enum_id)
106 break;
107
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100108 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
109 enum_ids = pfc->info->gpio_irq[i].enum_ids;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900110 for (k = 0; enum_ids[k]; k++) {
111 if (enum_ids[k] == enum_id)
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100112 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900113 }
114 }
115 }
116
117 return -ENOSYS;
118}
119
Laurent Pinchart16883812012-12-06 14:49:25 +0100120static void gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900121{
122 struct sh_pfc *pfc = chip->pfc;
123 struct gpio_chip *gc = &chip->gpio_chip;
124
Laurent Pinchart16883812012-12-06 14:49:25 +0100125 gc->request = gpio_pin_request;
126 gc->free = gpio_pin_free;
127 gc->direction_input = gpio_pin_direction_input;
128 gc->get = gpio_pin_get;
129 gc->direction_output = gpio_pin_direction_output;
130 gc->set = gpio_pin_set;
131 gc->to_irq = gpio_pin_to_irq;
132
133 gc->label = pfc->info->name;
134 gc->dev = pfc->dev;
135 gc->owner = THIS_MODULE;
136 gc->base = 0;
137 gc->ngpio = pfc->info->nr_pins;
138}
139
140/* -----------------------------------------------------------------------------
141 * Function GPIOs
142 */
143
144static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
145{
146 struct sh_pfc *pfc = gpio_to_pfc(gc);
147 unsigned int gpio = gc->base + offset;
148 unsigned long flags;
149 int ret = -EINVAL;
150
151 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
152
153 if (pfc->info->func_gpios[offset].enum_id == 0)
154 return ret;
155
156 spin_lock_irqsave(&pfc->lock, flags);
157
158 if (sh_pfc_config_gpio(pfc, gpio, PINMUX_TYPE_FUNCTION,
159 GPIO_CFG_DRYRUN))
160 goto done;
161
162 if (sh_pfc_config_gpio(pfc, gpio, PINMUX_TYPE_FUNCTION,
163 GPIO_CFG_REQ))
164 goto done;
165
166 ret = 0;
167
168done:
169 spin_unlock_irqrestore(&pfc->lock, flags);
170 return ret;
171}
172
173static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
174{
175 struct sh_pfc *pfc = gpio_to_pfc(gc);
176 unsigned int gpio = gc->base + offset;
177 unsigned long flags;
178
179 spin_lock_irqsave(&pfc->lock, flags);
180
181 sh_pfc_config_gpio(pfc, gpio, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
182
183 spin_unlock_irqrestore(&pfc->lock, flags);
184}
185
186static void gpio_function_setup(struct sh_pfc_chip *chip)
187{
188 struct sh_pfc *pfc = chip->pfc;
189 struct gpio_chip *gc = &chip->gpio_chip;
190
191 gc->request = gpio_function_request;
192 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900193
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100194 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900195 gc->owner = THIS_MODULE;
Laurent Pinchart16883812012-12-06 14:49:25 +0100196 gc->base = pfc->info->nr_pins;
197 gc->ngpio = pfc->info->nr_func_gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900198}
199
Laurent Pinchart16883812012-12-06 14:49:25 +0100200/* -----------------------------------------------------------------------------
201 * Register/unregister
202 */
203
204static struct sh_pfc_chip *
205sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900206{
207 struct sh_pfc_chip *chip;
208 int ret;
209
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100210 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900211 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100212 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900213
214 chip->pfc = pfc;
215
Laurent Pinchart16883812012-12-06 14:49:25 +0100216 setup(chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900217
218 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100219 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100220 return ERR_PTR(ret);
221
222 pr_info("%s handling gpio %u -> %u\n",
223 chip->gpio_chip.label, chip->gpio_chip.base,
224 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
225
226 return chip;
227}
228
229int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
230{
231 struct sh_pfc_chip *chip;
232
233 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
234 if (IS_ERR(chip))
235 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100236
237 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900238
Laurent Pinchart16883812012-12-06 14:49:25 +0100239 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
240 if (IS_ERR(chip))
241 return PTR_ERR(chip);
242
243 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900244
Paul Mundtb3c185a2012-06-20 17:29:04 +0900245 return 0;
246}
247
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100248int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900249{
Laurent Pinchart16883812012-12-06 14:49:25 +0100250 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900251 int ret;
252
Laurent Pinchart16883812012-12-06 14:49:25 +0100253 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
254 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900255
Laurent Pinchart16883812012-12-06 14:49:25 +0100256 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900257}