blob: b370d28650413a6f1ac7c92e4240bb4cb0d393e1 [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;
Laurent Pincharte51d5342013-02-17 00:26:33 +010027
28 struct sh_pfc_window *mem;
Paul Mundtb3c185a2012-06-20 17:29:04 +090029};
30
31static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
32{
33 return container_of(gc, struct sh_pfc_chip, gpio_chip);
34}
35
36static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
37{
38 return gpio_to_pfc_chip(gc)->pfc;
39}
40
Laurent Pinchart41f12192013-02-15 02:04:55 +010041static void gpio_get_data_reg(struct sh_pfc *pfc, unsigned int gpio,
42 struct pinmux_data_reg **dr, unsigned int *bit)
43{
44 struct sh_pfc_pin *gpiop = sh_pfc_get_pin(pfc, gpio);
45
46 *dr = pfc->info->data_regs
47 + ((gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT);
48 *bit = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
49}
50
Laurent Pincharte51d5342013-02-17 00:26:33 +010051static unsigned long gpio_read_data_reg(struct sh_pfc_chip *chip,
52 const struct pinmux_data_reg *dreg)
53{
54 void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
55
56 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
57}
58
59static void gpio_write_data_reg(struct sh_pfc_chip *chip,
60 const struct pinmux_data_reg *dreg,
61 unsigned long value)
62{
63 void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
64
65 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
66}
67
Laurent Pinchart41f12192013-02-15 02:04:55 +010068static void gpio_setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
69{
70 struct sh_pfc_pin *gpiop = &pfc->info->pins[gpio];
Laurent Pincharte51d5342013-02-17 00:26:33 +010071 const struct pinmux_data_reg *dreg;
72 unsigned int bit;
73 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +010074
Laurent Pincharte51d5342013-02-17 00:26:33 +010075 for (i = 0, dreg = pfc->info->data_regs; dreg->reg; ++i, ++dreg) {
76 for (bit = 0; bit < dreg->reg_width; bit++) {
77 if (dreg->enum_ids[bit] == gpiop->enum_id) {
Laurent Pinchart41f12192013-02-15 02:04:55 +010078 gpiop->flags &= ~PINMUX_FLAG_DREG;
Laurent Pincharte51d5342013-02-17 00:26:33 +010079 gpiop->flags |= i << PINMUX_FLAG_DREG_SHIFT;
Laurent Pinchart41f12192013-02-15 02:04:55 +010080 gpiop->flags &= ~PINMUX_FLAG_DBIT;
Laurent Pincharte51d5342013-02-17 00:26:33 +010081 gpiop->flags |= bit << PINMUX_FLAG_DBIT_SHIFT;
Laurent Pinchart41f12192013-02-15 02:04:55 +010082 return;
83 }
84 }
Laurent Pinchart41f12192013-02-15 02:04:55 +010085 }
86
87 BUG();
88}
89
Laurent Pincharte51d5342013-02-17 00:26:33 +010090static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
Laurent Pinchart41f12192013-02-15 02:04:55 +010091{
Laurent Pincharte51d5342013-02-17 00:26:33 +010092 struct sh_pfc *pfc = chip->pfc;
93 unsigned long addr = pfc->info->data_regs[0].reg;
94 struct pinmux_data_reg *dreg;
95 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +010096
Laurent Pincharte51d5342013-02-17 00:26:33 +010097 /* Find the window that contain the GPIO registers. */
98 for (i = 0; i < pfc->num_windows; ++i) {
99 struct sh_pfc_window *window = &pfc->window[i];
100
101 if (addr >= window->phys && addr < window->phys + window->size)
102 break;
103 }
104
105 if (i == pfc->num_windows)
106 return -EINVAL;
107
108 /* GPIO data registers must be in the first memory resource. */
109 chip->mem = &pfc->window[i];
110
111 for (dreg = pfc->info->data_regs; dreg->reg; ++dreg)
112 dreg->reg_shadow = gpio_read_data_reg(chip, dreg);
113
114 for (i = 0; i < pfc->info->nr_pins; i++) {
115 if (pfc->info->pins[i].enum_id == 0)
Laurent Pinchart41f12192013-02-15 02:04:55 +0100116 continue;
117
Laurent Pincharte51d5342013-02-17 00:26:33 +0100118 gpio_setup_data_reg(pfc, i);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100119 }
120
Laurent Pincharte51d5342013-02-17 00:26:33 +0100121 return 0;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100122}
123
Laurent Pinchart16883812012-12-06 14:49:25 +0100124/* -----------------------------------------------------------------------------
125 * Pin GPIOs
126 */
127
128static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900129{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100130 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart934cb022013-02-14 22:35:09 +0100131 struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100132
Laurent Pinchart63d57382013-02-15 01:33:38 +0100133 if (pin == NULL || pin->enum_id == 0)
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100134 return -EINVAL;
135
Laurent Pinchart16883812012-12-06 14:49:25 +0100136 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900137}
138
Laurent Pinchart16883812012-12-06 14:49:25 +0100139static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900140{
Laurent Pinchart16883812012-12-06 14:49:25 +0100141 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900142}
143
Laurent Pincharte51d5342013-02-17 00:26:33 +0100144static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
145 int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900146{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100147 struct pinmux_data_reg *dreg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100148 unsigned long pos;
149 unsigned int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900150
Laurent Pincharte51d5342013-02-17 00:26:33 +0100151 gpio_get_data_reg(chip->pfc, offset, &dreg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100152
Laurent Pincharte51d5342013-02-17 00:26:33 +0100153 pos = dreg->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100154
155 if (value)
Laurent Pincharte51d5342013-02-17 00:26:33 +0100156 set_bit(pos, &dreg->reg_shadow);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100157 else
Laurent Pincharte51d5342013-02-17 00:26:33 +0100158 clear_bit(pos, &dreg->reg_shadow);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100159
Laurent Pincharte51d5342013-02-17 00:26:33 +0100160 gpio_write_data_reg(chip, dreg, dreg->reg_shadow);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900161}
162
Laurent Pinchart16883812012-12-06 14:49:25 +0100163static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900164{
Laurent Pinchart16883812012-12-06 14:49:25 +0100165 return pinctrl_gpio_direction_input(offset);
166}
167
168static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
169 int value)
170{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100171 gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
Laurent Pinchart16883812012-12-06 14:49:25 +0100172
173 return pinctrl_gpio_direction_output(offset);
174}
175
176static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
177{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100178 struct sh_pfc_chip *chip = gpio_to_pfc_chip(gc);
179 struct pinmux_data_reg *dreg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100180 unsigned long pos;
181 unsigned int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900182
Laurent Pincharte51d5342013-02-17 00:26:33 +0100183 gpio_get_data_reg(chip->pfc, offset, &dreg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100184
Laurent Pincharte51d5342013-02-17 00:26:33 +0100185 pos = dreg->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100186
Laurent Pincharte51d5342013-02-17 00:26:33 +0100187 return (gpio_read_data_reg(chip, dreg) >> pos) & 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900188}
189
Laurent Pinchart16883812012-12-06 14:49:25 +0100190static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +0900191{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100192 gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +0900193}
194
Laurent Pinchart16883812012-12-06 14:49:25 +0100195static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900196{
197 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100198 int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900199
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100200 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
201 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900202
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100203 for (k = 0; gpios[k]; k++) {
204 if (gpios[k] == offset)
205 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900206 }
207 }
208
209 return -ENOSYS;
210}
211
Laurent Pincharte51d5342013-02-17 00:26:33 +0100212static int gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900213{
214 struct sh_pfc *pfc = chip->pfc;
215 struct gpio_chip *gc = &chip->gpio_chip;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100216 int ret;
217
218 ret = gpio_setup_data_regs(chip);
219 if (ret < 0)
220 return ret;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900221
Laurent Pinchart16883812012-12-06 14:49:25 +0100222 gc->request = gpio_pin_request;
223 gc->free = gpio_pin_free;
224 gc->direction_input = gpio_pin_direction_input;
225 gc->get = gpio_pin_get;
226 gc->direction_output = gpio_pin_direction_output;
227 gc->set = gpio_pin_set;
228 gc->to_irq = gpio_pin_to_irq;
229
230 gc->label = pfc->info->name;
231 gc->dev = pfc->dev;
232 gc->owner = THIS_MODULE;
233 gc->base = 0;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100234 gc->ngpio = pfc->nr_pins;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100235
236 return 0;
Laurent Pinchart16883812012-12-06 14:49:25 +0100237}
238
239/* -----------------------------------------------------------------------------
240 * Function GPIOs
241 */
242
243static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
244{
245 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100246 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100247 unsigned long flags;
248 int ret = -EINVAL;
249
250 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
251
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100252 if (mark == 0)
Laurent Pinchart16883812012-12-06 14:49:25 +0100253 return ret;
254
255 spin_lock_irqsave(&pfc->lock, flags);
256
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100257 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
Laurent Pinchart16883812012-12-06 14:49:25 +0100258 goto done;
259
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100260 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
Laurent Pinchart16883812012-12-06 14:49:25 +0100261 goto done;
262
263 ret = 0;
264
265done:
266 spin_unlock_irqrestore(&pfc->lock, flags);
267 return ret;
268}
269
270static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
271{
272 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100273 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100274 unsigned long flags;
275
276 spin_lock_irqsave(&pfc->lock, flags);
277
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100278 sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
Laurent Pinchart16883812012-12-06 14:49:25 +0100279
280 spin_unlock_irqrestore(&pfc->lock, flags);
281}
282
Laurent Pincharte51d5342013-02-17 00:26:33 +0100283static int gpio_function_setup(struct sh_pfc_chip *chip)
Laurent Pinchart16883812012-12-06 14:49:25 +0100284{
285 struct sh_pfc *pfc = chip->pfc;
286 struct gpio_chip *gc = &chip->gpio_chip;
287
288 gc->request = gpio_function_request;
289 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900290
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100291 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900292 gc->owner = THIS_MODULE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100293 gc->base = pfc->nr_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100294 gc->ngpio = pfc->info->nr_func_gpios;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100295
296 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900297}
298
Laurent Pinchart16883812012-12-06 14:49:25 +0100299/* -----------------------------------------------------------------------------
300 * Register/unregister
301 */
302
303static struct sh_pfc_chip *
Laurent Pincharte51d5342013-02-17 00:26:33 +0100304sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900305{
306 struct sh_pfc_chip *chip;
307 int ret;
308
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100309 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900310 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100311 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900312
313 chip->pfc = pfc;
314
Laurent Pincharte51d5342013-02-17 00:26:33 +0100315 ret = setup(chip);
316 if (ret < 0)
317 return ERR_PTR(ret);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900318
319 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100320 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100321 return ERR_PTR(ret);
322
323 pr_info("%s handling gpio %u -> %u\n",
324 chip->gpio_chip.label, chip->gpio_chip.base,
325 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
326
327 return chip;
328}
329
330int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
331{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100332 const struct pinmux_range *ranges;
333 struct pinmux_range def_range;
Laurent Pinchart16883812012-12-06 14:49:25 +0100334 struct sh_pfc_chip *chip;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100335 unsigned int nr_ranges;
336 unsigned int i;
Laurent Pinchart247127f2013-03-08 00:45:12 +0100337 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100338
Laurent Pinchart63d57382013-02-15 01:33:38 +0100339 /* Register the real GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100340 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
341 if (IS_ERR(chip))
342 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100343
344 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900345
Laurent Pinchart63d57382013-02-15 01:33:38 +0100346 /* Register the GPIO to pin mappings. */
347 if (pfc->info->ranges == NULL) {
348 def_range.begin = 0;
349 def_range.end = pfc->info->nr_pins - 1;
350 ranges = &def_range;
351 nr_ranges = 1;
352 } else {
353 ranges = pfc->info->ranges;
354 nr_ranges = pfc->info->nr_ranges;
355 }
Laurent Pinchart247127f2013-03-08 00:45:12 +0100356
Laurent Pinchart63d57382013-02-15 01:33:38 +0100357 for (i = 0; i < nr_ranges; ++i) {
358 const struct pinmux_range *range = &ranges[i];
359
360 ret = gpiochip_add_pin_range(&chip->gpio_chip,
361 dev_name(pfc->dev),
362 range->begin, range->begin,
363 range->end - range->begin + 1);
364 if (ret < 0)
365 return ret;
366 }
367
368 /* Register the function GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100369 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
370 if (IS_ERR(chip))
371 return PTR_ERR(chip);
372
373 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900374
Paul Mundtb3c185a2012-06-20 17:29:04 +0900375 return 0;
376}
377
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100378int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900379{
Laurent Pinchart16883812012-12-06 14:49:25 +0100380 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900381 int ret;
382
Laurent Pinchart16883812012-12-06 14:49:25 +0100383 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
384 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900385
Laurent Pinchart16883812012-12-06 14:49:25 +0100386 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900387}