blob: 027c77762d8fb295fdce7e7aa48e73fa46cbeb68 [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 Pinchart41f12192013-02-15 02:04:55 +010039static void gpio_get_data_reg(struct sh_pfc *pfc, unsigned int gpio,
40 struct pinmux_data_reg **dr, unsigned int *bit)
41{
42 struct sh_pfc_pin *gpiop = sh_pfc_get_pin(pfc, gpio);
43
44 *dr = pfc->info->data_regs
45 + ((gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT);
46 *bit = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
47}
48
49static void gpio_setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
50{
51 struct sh_pfc_pin *gpiop = &pfc->info->pins[gpio];
52 struct pinmux_data_reg *data_reg;
53 int k, n;
54
55 k = 0;
56 while (1) {
57 data_reg = pfc->info->data_regs + k;
58
59 if (!data_reg->reg_width)
60 break;
61
62 data_reg->mapped_reg = sh_pfc_phys_to_virt(pfc, data_reg->reg);
63
64 for (n = 0; n < data_reg->reg_width; n++) {
65 if (data_reg->enum_ids[n] == gpiop->enum_id) {
66 gpiop->flags &= ~PINMUX_FLAG_DREG;
67 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
68 gpiop->flags &= ~PINMUX_FLAG_DBIT;
69 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
70 return;
71 }
72 }
73 k++;
74 }
75
76 BUG();
77}
78
79static void gpio_setup_data_regs(struct sh_pfc *pfc)
80{
81 struct pinmux_data_reg *drp;
82 int k;
83
84 for (k = 0; k < pfc->info->nr_pins; k++) {
85 if (pfc->info->pins[k].enum_id == 0)
86 continue;
87
88 gpio_setup_data_reg(pfc, k);
89 }
90
91 k = 0;
92 while (1) {
93 drp = pfc->info->data_regs + k;
94
95 if (!drp->reg_width)
96 break;
97
98 drp->reg_shadow = sh_pfc_read_raw_reg(drp->mapped_reg,
99 drp->reg_width);
100 k++;
101 }
102}
103
Laurent Pinchart16883812012-12-06 14:49:25 +0100104/* -----------------------------------------------------------------------------
105 * Pin GPIOs
106 */
107
108static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900109{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100110 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart934cb022013-02-14 22:35:09 +0100111 struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100112
Laurent Pinchart63d57382013-02-15 01:33:38 +0100113 if (pin == NULL || pin->enum_id == 0)
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100114 return -EINVAL;
115
Laurent Pinchart16883812012-12-06 14:49:25 +0100116 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900117}
118
Laurent Pinchart16883812012-12-06 14:49:25 +0100119static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900120{
Laurent Pinchart16883812012-12-06 14:49:25 +0100121 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900122}
123
Laurent Pinchart16883812012-12-06 14:49:25 +0100124static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900125{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100126 struct pinmux_data_reg *dr;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100127 unsigned long pos;
128 unsigned int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900129
Laurent Pinchart41f12192013-02-15 02:04:55 +0100130 gpio_get_data_reg(pfc, offset, &dr, &bit);
131
132 pos = dr->reg_width - (bit + 1);
133
134 if (value)
135 set_bit(pos, &dr->reg_shadow);
136 else
137 clear_bit(pos, &dr->reg_shadow);
138
139 sh_pfc_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900140}
141
Laurent Pinchart16883812012-12-06 14:49:25 +0100142static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900143{
Laurent Pinchart16883812012-12-06 14:49:25 +0100144 return pinctrl_gpio_direction_input(offset);
145}
146
147static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
148 int value)
149{
150 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
151
152 return pinctrl_gpio_direction_output(offset);
153}
154
155static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
156{
157 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100158 struct pinmux_data_reg *dr;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100159 unsigned long pos;
160 unsigned int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900161
Laurent Pinchart41f12192013-02-15 02:04:55 +0100162 gpio_get_data_reg(pfc, offset, &dr, &bit);
163
164 pos = dr->reg_width - (bit + 1);
165
166 return (sh_pfc_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900167}
168
Laurent Pinchart16883812012-12-06 14:49:25 +0100169static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +0900170{
Laurent Pinchart16883812012-12-06 14:49:25 +0100171 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +0900172}
173
Laurent Pinchart16883812012-12-06 14:49:25 +0100174static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900175{
176 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100177 int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900178
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100179 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
180 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900181
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100182 for (k = 0; gpios[k]; k++) {
183 if (gpios[k] == offset)
184 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900185 }
186 }
187
188 return -ENOSYS;
189}
190
Laurent Pinchart16883812012-12-06 14:49:25 +0100191static void gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900192{
193 struct sh_pfc *pfc = chip->pfc;
194 struct gpio_chip *gc = &chip->gpio_chip;
195
Laurent Pinchart16883812012-12-06 14:49:25 +0100196 gc->request = gpio_pin_request;
197 gc->free = gpio_pin_free;
198 gc->direction_input = gpio_pin_direction_input;
199 gc->get = gpio_pin_get;
200 gc->direction_output = gpio_pin_direction_output;
201 gc->set = gpio_pin_set;
202 gc->to_irq = gpio_pin_to_irq;
203
204 gc->label = pfc->info->name;
205 gc->dev = pfc->dev;
206 gc->owner = THIS_MODULE;
207 gc->base = 0;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100208 gc->ngpio = pfc->nr_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100209}
210
211/* -----------------------------------------------------------------------------
212 * Function GPIOs
213 */
214
215static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
216{
217 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100218 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100219 unsigned long flags;
220 int ret = -EINVAL;
221
222 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
223
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100224 if (mark == 0)
Laurent Pinchart16883812012-12-06 14:49:25 +0100225 return ret;
226
227 spin_lock_irqsave(&pfc->lock, flags);
228
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100229 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
Laurent Pinchart16883812012-12-06 14:49:25 +0100230 goto done;
231
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100232 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
Laurent Pinchart16883812012-12-06 14:49:25 +0100233 goto done;
234
235 ret = 0;
236
237done:
238 spin_unlock_irqrestore(&pfc->lock, flags);
239 return ret;
240}
241
242static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
243{
244 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100245 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100246 unsigned long flags;
247
248 spin_lock_irqsave(&pfc->lock, flags);
249
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100250 sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
Laurent Pinchart16883812012-12-06 14:49:25 +0100251
252 spin_unlock_irqrestore(&pfc->lock, flags);
253}
254
255static void gpio_function_setup(struct sh_pfc_chip *chip)
256{
257 struct sh_pfc *pfc = chip->pfc;
258 struct gpio_chip *gc = &chip->gpio_chip;
259
260 gc->request = gpio_function_request;
261 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900262
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100263 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900264 gc->owner = THIS_MODULE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100265 gc->base = pfc->nr_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100266 gc->ngpio = pfc->info->nr_func_gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900267}
268
Laurent Pinchart16883812012-12-06 14:49:25 +0100269/* -----------------------------------------------------------------------------
270 * Register/unregister
271 */
272
273static struct sh_pfc_chip *
274sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900275{
276 struct sh_pfc_chip *chip;
277 int ret;
278
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100279 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900280 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100281 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900282
283 chip->pfc = pfc;
284
Laurent Pinchart16883812012-12-06 14:49:25 +0100285 setup(chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900286
287 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100288 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100289 return ERR_PTR(ret);
290
291 pr_info("%s handling gpio %u -> %u\n",
292 chip->gpio_chip.label, chip->gpio_chip.base,
293 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
294
295 return chip;
296}
297
298int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
299{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100300 const struct pinmux_range *ranges;
301 struct pinmux_range def_range;
Laurent Pinchart16883812012-12-06 14:49:25 +0100302 struct sh_pfc_chip *chip;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100303 unsigned int nr_ranges;
304 unsigned int i;
Laurent Pinchart247127f2013-03-08 00:45:12 +0100305 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100306
Laurent Pinchart41f12192013-02-15 02:04:55 +0100307 gpio_setup_data_regs(pfc);
308
Laurent Pinchart63d57382013-02-15 01:33:38 +0100309 /* Register the real GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100310 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
311 if (IS_ERR(chip))
312 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100313
314 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900315
Laurent Pinchart63d57382013-02-15 01:33:38 +0100316 /* Register the GPIO to pin mappings. */
317 if (pfc->info->ranges == NULL) {
318 def_range.begin = 0;
319 def_range.end = pfc->info->nr_pins - 1;
320 ranges = &def_range;
321 nr_ranges = 1;
322 } else {
323 ranges = pfc->info->ranges;
324 nr_ranges = pfc->info->nr_ranges;
325 }
Laurent Pinchart247127f2013-03-08 00:45:12 +0100326
Laurent Pinchart63d57382013-02-15 01:33:38 +0100327 for (i = 0; i < nr_ranges; ++i) {
328 const struct pinmux_range *range = &ranges[i];
329
330 ret = gpiochip_add_pin_range(&chip->gpio_chip,
331 dev_name(pfc->dev),
332 range->begin, range->begin,
333 range->end - range->begin + 1);
334 if (ret < 0)
335 return ret;
336 }
337
338 /* Register the function GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100339 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
340 if (IS_ERR(chip))
341 return PTR_ERR(chip);
342
343 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900344
Paul Mundtb3c185a2012-06-20 17:29:04 +0900345 return 0;
346}
347
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100348int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900349{
Laurent Pinchart16883812012-12-06 14:49:25 +0100350 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900351 int ret;
352
Laurent Pinchart16883812012-12-06 14:49:25 +0100353 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
354 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900355
Laurent Pinchart16883812012-12-06 14:49:25 +0100356 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900357}