blob: 480beae2ee670321ad2e61f5414100cfa64bc754 [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
Laurent Pinchart51cb2262013-02-16 18:34:32 +010024struct sh_pfc_gpio_data_reg {
25 const struct pinmux_data_reg *info;
26 unsigned long shadow;
27};
28
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010029struct sh_pfc_gpio_pin {
30 u8 dbit;
31 u8 dreg;
32};
Laurent Pincharte51d5342013-02-17 00:26:33 +010033
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010034struct sh_pfc_chip {
35 struct sh_pfc *pfc;
36 struct gpio_chip gpio_chip;
37
38 struct sh_pfc_window *mem;
Laurent Pinchart51cb2262013-02-16 18:34:32 +010039 struct sh_pfc_gpio_data_reg *regs;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010040 struct sh_pfc_gpio_pin *pins;
Paul Mundtb3c185a2012-06-20 17:29:04 +090041};
42
43static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
44{
45 return container_of(gc, struct sh_pfc_chip, gpio_chip);
46}
47
48static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
49{
50 return gpio_to_pfc_chip(gc)->pfc;
51}
52
Laurent Pinchart51cb2262013-02-16 18:34:32 +010053static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int gpio,
54 struct sh_pfc_gpio_data_reg **reg,
55 unsigned int *bit)
Laurent Pinchart41f12192013-02-15 02:04:55 +010056{
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010057 int idx = sh_pfc_get_pin_index(chip->pfc, gpio);
58 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
Laurent Pinchart41f12192013-02-15 02:04:55 +010059
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010060 *reg = &chip->regs[gpio_pin->dreg];
61 *bit = gpio_pin->dbit;
Laurent Pinchart41f12192013-02-15 02:04:55 +010062}
63
Laurent Pincharte51d5342013-02-17 00:26:33 +010064static unsigned long gpio_read_data_reg(struct sh_pfc_chip *chip,
65 const struct pinmux_data_reg *dreg)
66{
67 void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
68
69 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
70}
71
72static void gpio_write_data_reg(struct sh_pfc_chip *chip,
73 const struct pinmux_data_reg *dreg,
74 unsigned long value)
75{
76 void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
77
78 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
79}
80
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010081static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned gpio)
Laurent Pinchart41f12192013-02-15 02:04:55 +010082{
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010083 struct sh_pfc *pfc = chip->pfc;
84 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[gpio];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +010085 const struct sh_pfc_pin *pin = &pfc->info->pins[gpio];
Laurent Pincharte51d5342013-02-17 00:26:33 +010086 const struct pinmux_data_reg *dreg;
87 unsigned int bit;
88 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +010089
Laurent Pincharte51d5342013-02-17 00:26:33 +010090 for (i = 0, dreg = pfc->info->data_regs; dreg->reg; ++i, ++dreg) {
91 for (bit = 0; bit < dreg->reg_width; bit++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010092 if (dreg->enum_ids[bit] == pin->enum_id) {
93 gpio_pin->dreg = i;
94 gpio_pin->dbit = bit;
Laurent Pinchart41f12192013-02-15 02:04:55 +010095 return;
96 }
97 }
Laurent Pinchart41f12192013-02-15 02:04:55 +010098 }
99
100 BUG();
101}
102
Laurent Pincharte51d5342013-02-17 00:26:33 +0100103static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
Laurent Pinchart41f12192013-02-15 02:04:55 +0100104{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100105 struct sh_pfc *pfc = chip->pfc;
106 unsigned long addr = pfc->info->data_regs[0].reg;
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100107 const struct pinmux_data_reg *dreg;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100108 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100109
Laurent Pincharte51d5342013-02-17 00:26:33 +0100110 /* Find the window that contain the GPIO registers. */
111 for (i = 0; i < pfc->num_windows; ++i) {
112 struct sh_pfc_window *window = &pfc->window[i];
113
114 if (addr >= window->phys && addr < window->phys + window->size)
115 break;
116 }
117
118 if (i == pfc->num_windows)
119 return -EINVAL;
120
121 /* GPIO data registers must be in the first memory resource. */
122 chip->mem = &pfc->window[i];
123
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100124 /* Count the number of data registers, allocate memory and initialize
125 * them.
126 */
127 for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
128 ;
129
130 chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs),
131 GFP_KERNEL);
132 if (chip->regs == NULL)
133 return -ENOMEM;
134
135 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
136 chip->regs[i].info = dreg;
137 chip->regs[i].shadow = gpio_read_data_reg(chip, dreg);
138 }
Laurent Pincharte51d5342013-02-17 00:26:33 +0100139
140 for (i = 0; i < pfc->info->nr_pins; i++) {
141 if (pfc->info->pins[i].enum_id == 0)
Laurent Pinchart41f12192013-02-15 02:04:55 +0100142 continue;
143
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100144 gpio_setup_data_reg(chip, i);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100145 }
146
Laurent Pincharte51d5342013-02-17 00:26:33 +0100147 return 0;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100148}
149
Laurent Pinchart16883812012-12-06 14:49:25 +0100150/* -----------------------------------------------------------------------------
151 * Pin GPIOs
152 */
153
154static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900155{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100156 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100157 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100158
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100159 if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100160 return -EINVAL;
161
Laurent Pinchart16883812012-12-06 14:49:25 +0100162 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900163}
164
Laurent Pinchart16883812012-12-06 14:49:25 +0100165static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900166{
Laurent Pinchart16883812012-12-06 14:49:25 +0100167 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900168}
169
Laurent Pincharte51d5342013-02-17 00:26:33 +0100170static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
171 int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900172{
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100173 struct sh_pfc_gpio_data_reg *reg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100174 unsigned long pos;
175 unsigned int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900176
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100177 gpio_get_data_reg(chip, offset, &reg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100178
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100179 pos = reg->info->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100180
181 if (value)
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100182 set_bit(pos, &reg->shadow);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100183 else
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100184 clear_bit(pos, &reg->shadow);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100185
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100186 gpio_write_data_reg(chip, reg->info, reg->shadow);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900187}
188
Laurent Pinchart16883812012-12-06 14:49:25 +0100189static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900190{
Laurent Pinchart16883812012-12-06 14:49:25 +0100191 return pinctrl_gpio_direction_input(offset);
192}
193
194static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
195 int value)
196{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100197 gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
Laurent Pinchart16883812012-12-06 14:49:25 +0100198
199 return pinctrl_gpio_direction_output(offset);
200}
201
202static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
203{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100204 struct sh_pfc_chip *chip = gpio_to_pfc_chip(gc);
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100205 struct sh_pfc_gpio_data_reg *reg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100206 unsigned long pos;
207 unsigned int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900208
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100209 gpio_get_data_reg(chip, offset, &reg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100210
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100211 pos = reg->info->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100212
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100213 return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900214}
215
Laurent Pinchart16883812012-12-06 14:49:25 +0100216static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +0900217{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100218 gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +0900219}
220
Laurent Pinchart16883812012-12-06 14:49:25 +0100221static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900222{
223 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100224 int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900225
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100226 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
227 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900228
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100229 for (k = 0; gpios[k]; k++) {
230 if (gpios[k] == offset)
231 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900232 }
233 }
234
235 return -ENOSYS;
236}
237
Laurent Pincharte51d5342013-02-17 00:26:33 +0100238static int gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900239{
240 struct sh_pfc *pfc = chip->pfc;
241 struct gpio_chip *gc = &chip->gpio_chip;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100242 int ret;
243
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100244 chip->pins = devm_kzalloc(pfc->dev, pfc->nr_pins * sizeof(*chip->pins),
245 GFP_KERNEL);
246 if (chip->pins == NULL)
247 return -ENOMEM;
248
Laurent Pincharte51d5342013-02-17 00:26:33 +0100249 ret = gpio_setup_data_regs(chip);
250 if (ret < 0)
251 return ret;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900252
Laurent Pinchart16883812012-12-06 14:49:25 +0100253 gc->request = gpio_pin_request;
254 gc->free = gpio_pin_free;
255 gc->direction_input = gpio_pin_direction_input;
256 gc->get = gpio_pin_get;
257 gc->direction_output = gpio_pin_direction_output;
258 gc->set = gpio_pin_set;
259 gc->to_irq = gpio_pin_to_irq;
260
261 gc->label = pfc->info->name;
262 gc->dev = pfc->dev;
263 gc->owner = THIS_MODULE;
264 gc->base = 0;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100265 gc->ngpio = pfc->nr_pins;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100266
267 return 0;
Laurent Pinchart16883812012-12-06 14:49:25 +0100268}
269
270/* -----------------------------------------------------------------------------
271 * Function GPIOs
272 */
273
274static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
275{
276 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100277 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100278 unsigned long flags;
279 int ret = -EINVAL;
280
281 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
282
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100283 if (mark == 0)
Laurent Pinchart16883812012-12-06 14:49:25 +0100284 return ret;
285
286 spin_lock_irqsave(&pfc->lock, flags);
287
Laurent Pinchart861601d2013-03-10 15:29:14 +0100288 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION))
Laurent Pinchart16883812012-12-06 14:49:25 +0100289 goto done;
290
291 ret = 0;
292
293done:
294 spin_unlock_irqrestore(&pfc->lock, flags);
295 return ret;
296}
297
298static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
299{
Laurent Pinchart16883812012-12-06 14:49:25 +0100300}
301
Laurent Pincharte51d5342013-02-17 00:26:33 +0100302static int gpio_function_setup(struct sh_pfc_chip *chip)
Laurent Pinchart16883812012-12-06 14:49:25 +0100303{
304 struct sh_pfc *pfc = chip->pfc;
305 struct gpio_chip *gc = &chip->gpio_chip;
306
307 gc->request = gpio_function_request;
308 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900309
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100310 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900311 gc->owner = THIS_MODULE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100312 gc->base = pfc->nr_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100313 gc->ngpio = pfc->info->nr_func_gpios;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100314
315 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900316}
317
Laurent Pinchart16883812012-12-06 14:49:25 +0100318/* -----------------------------------------------------------------------------
319 * Register/unregister
320 */
321
322static struct sh_pfc_chip *
Laurent Pincharte51d5342013-02-17 00:26:33 +0100323sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900324{
325 struct sh_pfc_chip *chip;
326 int ret;
327
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100328 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900329 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100330 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900331
332 chip->pfc = pfc;
333
Laurent Pincharte51d5342013-02-17 00:26:33 +0100334 ret = setup(chip);
335 if (ret < 0)
336 return ERR_PTR(ret);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900337
338 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100339 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100340 return ERR_PTR(ret);
341
342 pr_info("%s handling gpio %u -> %u\n",
343 chip->gpio_chip.label, chip->gpio_chip.base,
344 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
345
346 return chip;
347}
348
349int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
350{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100351 const struct pinmux_range *ranges;
352 struct pinmux_range def_range;
Laurent Pinchart16883812012-12-06 14:49:25 +0100353 struct sh_pfc_chip *chip;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100354 unsigned int nr_ranges;
355 unsigned int i;
Laurent Pinchart247127f2013-03-08 00:45:12 +0100356 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100357
Laurent Pinchart63d57382013-02-15 01:33:38 +0100358 /* Register the real GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100359 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
360 if (IS_ERR(chip))
361 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100362
363 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900364
Laurent Pinchart63d57382013-02-15 01:33:38 +0100365 /* Register the GPIO to pin mappings. */
366 if (pfc->info->ranges == NULL) {
367 def_range.begin = 0;
368 def_range.end = pfc->info->nr_pins - 1;
369 ranges = &def_range;
370 nr_ranges = 1;
371 } else {
372 ranges = pfc->info->ranges;
373 nr_ranges = pfc->info->nr_ranges;
374 }
Laurent Pinchart247127f2013-03-08 00:45:12 +0100375
Laurent Pinchart63d57382013-02-15 01:33:38 +0100376 for (i = 0; i < nr_ranges; ++i) {
377 const struct pinmux_range *range = &ranges[i];
378
379 ret = gpiochip_add_pin_range(&chip->gpio_chip,
380 dev_name(pfc->dev),
381 range->begin, range->begin,
382 range->end - range->begin + 1);
383 if (ret < 0)
384 return ret;
385 }
386
387 /* Register the function GPIOs chip. */
Laurent Pinchart16883812012-12-06 14:49:25 +0100388 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
389 if (IS_ERR(chip))
390 return PTR_ERR(chip);
391
392 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900393
Paul Mundtb3c185a2012-06-20 17:29:04 +0900394 return 0;
395}
396
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100397int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900398{
Laurent Pinchart16883812012-12-06 14:49:25 +0100399 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900400 int ret;
401
Laurent Pinchart16883812012-12-06 14:49:25 +0100402 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
403 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900404
Laurent Pinchart16883812012-12-06 14:49:25 +0100405 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900406}