blob: ba353735ecf2be9a1ca39c29e516c850beaa1ce1 [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
Laurent Pinchart1724acf2012-12-15 23:50:48 +010012#include <linux/device.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090013#include <linux/gpio.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010014#include <linux/init.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090015#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090016#include <linux/pinctrl/consumer.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010017#include <linux/slab.h>
18#include <linux/spinlock.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090019
Laurent Pinchartf9165132012-12-15 23:50:44 +010020#include "core.h"
21
Laurent Pinchart51cb2262013-02-16 18:34:32 +010022struct sh_pfc_gpio_data_reg {
23 const struct pinmux_data_reg *info;
Geert Uytterhoevenfc889362015-02-27 18:38:04 +010024 u32 shadow;
Laurent Pinchart51cb2262013-02-16 18:34:32 +010025};
26
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010027struct sh_pfc_gpio_pin {
28 u8 dbit;
29 u8 dreg;
30};
Laurent Pincharte51d5342013-02-17 00:26:33 +010031
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010032struct sh_pfc_chip {
33 struct sh_pfc *pfc;
34 struct gpio_chip gpio_chip;
35
36 struct sh_pfc_window *mem;
Laurent Pinchart51cb2262013-02-16 18:34:32 +010037 struct sh_pfc_gpio_data_reg *regs;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010038 struct sh_pfc_gpio_pin *pins;
Paul Mundtb3c185a2012-06-20 17:29:04 +090039};
40
41static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
42{
43 return container_of(gc, struct sh_pfc_chip, gpio_chip);
44}
45
46static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
47{
48 return gpio_to_pfc_chip(gc)->pfc;
49}
50
Laurent Pinchart757b0552013-07-15 13:25:08 +020051static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int offset,
Laurent Pinchart51cb2262013-02-16 18:34:32 +010052 struct sh_pfc_gpio_data_reg **reg,
53 unsigned int *bit)
Laurent Pinchart41f12192013-02-15 02:04:55 +010054{
Laurent Pinchart757b0552013-07-15 13:25:08 +020055 int idx = sh_pfc_get_pin_index(chip->pfc, offset);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010056 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
Laurent Pinchart41f12192013-02-15 02:04:55 +010057
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010058 *reg = &chip->regs[gpio_pin->dreg];
59 *bit = gpio_pin->dbit;
Laurent Pinchart41f12192013-02-15 02:04:55 +010060}
61
Geert Uytterhoevenfc889362015-02-27 18:38:04 +010062static u32 gpio_read_data_reg(struct sh_pfc_chip *chip,
63 const struct pinmux_data_reg *dreg)
Laurent Pincharte51d5342013-02-17 00:26:33 +010064{
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010065 phys_addr_t address = dreg->reg;
66 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
Laurent Pincharte51d5342013-02-17 00:26:33 +010067
68 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
69}
70
71static void gpio_write_data_reg(struct sh_pfc_chip *chip,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +010072 const struct pinmux_data_reg *dreg, u32 value)
Laurent Pincharte51d5342013-02-17 00:26:33 +010073{
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010074 phys_addr_t address = dreg->reg;
75 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
Laurent Pincharte51d5342013-02-17 00:26:33 +010076
77 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
78}
79
Laurent Pinchart757b0552013-07-15 13:25:08 +020080static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx)
Laurent Pinchart41f12192013-02-15 02:04:55 +010081{
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010082 struct sh_pfc *pfc = chip->pfc;
Laurent Pinchart757b0552013-07-15 13:25:08 +020083 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
84 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pincharte51d5342013-02-17 00:26:33 +010085 const struct pinmux_data_reg *dreg;
86 unsigned int bit;
87 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +010088
Geert Uytterhoeven17c7cbb2015-03-12 11:09:15 +010089 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
Laurent Pincharte51d5342013-02-17 00:26:33 +010090 for (bit = 0; bit < dreg->reg_width; bit++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010091 if (dreg->enum_ids[bit] == pin->enum_id) {
92 gpio_pin->dreg = i;
93 gpio_pin->dbit = bit;
Laurent Pinchart41f12192013-02-15 02:04:55 +010094 return;
95 }
96 }
Laurent Pinchart41f12192013-02-15 02:04:55 +010097 }
98
99 BUG();
100}
101
Laurent Pincharte51d5342013-02-17 00:26:33 +0100102static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
Laurent Pinchart41f12192013-02-15 02:04:55 +0100103{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100104 struct sh_pfc *pfc = chip->pfc;
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100105 const struct pinmux_data_reg *dreg;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100106 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100107
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100108 /* Count the number of data registers, allocate memory and initialize
109 * them.
110 */
111 for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
112 ;
113
114 chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs),
115 GFP_KERNEL);
116 if (chip->regs == NULL)
117 return -ENOMEM;
118
119 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
120 chip->regs[i].info = dreg;
121 chip->regs[i].shadow = gpio_read_data_reg(chip, dreg);
122 }
Laurent Pincharte51d5342013-02-17 00:26:33 +0100123
124 for (i = 0; i < pfc->info->nr_pins; i++) {
125 if (pfc->info->pins[i].enum_id == 0)
Laurent Pinchart41f12192013-02-15 02:04:55 +0100126 continue;
127
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100128 gpio_setup_data_reg(chip, i);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100129 }
130
Laurent Pincharte51d5342013-02-17 00:26:33 +0100131 return 0;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100132}
133
Laurent Pinchart16883812012-12-06 14:49:25 +0100134/* -----------------------------------------------------------------------------
135 * Pin GPIOs
136 */
137
138static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900139{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100140 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100141 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100142
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100143 if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100144 return -EINVAL;
145
Laurent Pinchart16883812012-12-06 14:49:25 +0100146 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900147}
148
Laurent Pinchart16883812012-12-06 14:49:25 +0100149static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900150{
Laurent Pinchart16883812012-12-06 14:49:25 +0100151 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900152}
153
Laurent Pincharte51d5342013-02-17 00:26:33 +0100154static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
155 int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900156{
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100157 struct sh_pfc_gpio_data_reg *reg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100158 unsigned int bit;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100159 unsigned int pos;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900160
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100161 gpio_get_data_reg(chip, offset, &reg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100162
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100163 pos = reg->info->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100164
165 if (value)
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100166 reg->shadow |= BIT(pos);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100167 else
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100168 reg->shadow &= ~BIT(pos);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100169
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100170 gpio_write_data_reg(chip, reg->info, reg->shadow);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900171}
172
Laurent Pinchart16883812012-12-06 14:49:25 +0100173static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900174{
Laurent Pinchart16883812012-12-06 14:49:25 +0100175 return pinctrl_gpio_direction_input(offset);
176}
177
178static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
179 int value)
180{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100181 gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
Laurent Pinchart16883812012-12-06 14:49:25 +0100182
183 return pinctrl_gpio_direction_output(offset);
184}
185
186static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
187{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100188 struct sh_pfc_chip *chip = gpio_to_pfc_chip(gc);
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100189 struct sh_pfc_gpio_data_reg *reg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100190 unsigned int bit;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100191 unsigned int pos;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900192
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100193 gpio_get_data_reg(chip, offset, &reg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100194
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100195 pos = reg->info->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100196
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100197 return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900198}
199
Laurent Pinchart16883812012-12-06 14:49:25 +0100200static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +0900201{
Laurent Pincharte51d5342013-02-17 00:26:33 +0100202 gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +0900203}
204
Laurent Pinchart16883812012-12-06 14:49:25 +0100205static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900206{
207 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100208 unsigned int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900209
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100210 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
Laurent Pinchart6d5bddd2013-12-16 20:25:15 +0100211 const short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900212
Laurent Pinchart316b2552013-12-11 04:26:22 +0100213 for (k = 0; gpios[k] >= 0; k++) {
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100214 if (gpios[k] == offset)
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100215 goto found;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900216 }
217 }
218
219 return -ENOSYS;
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100220
221found:
222 if (pfc->num_irqs)
223 return pfc->irqs[i];
224 else
225 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900226}
227
Laurent Pincharte51d5342013-02-17 00:26:33 +0100228static int gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900229{
230 struct sh_pfc *pfc = chip->pfc;
231 struct gpio_chip *gc = &chip->gpio_chip;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100232 int ret;
233
Laurent Pincharta1a35802013-07-15 13:36:39 +0200234 chip->pins = devm_kzalloc(pfc->dev, pfc->info->nr_pins *
235 sizeof(*chip->pins), GFP_KERNEL);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100236 if (chip->pins == NULL)
237 return -ENOMEM;
238
Laurent Pincharte51d5342013-02-17 00:26:33 +0100239 ret = gpio_setup_data_regs(chip);
240 if (ret < 0)
241 return ret;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900242
Laurent Pinchart16883812012-12-06 14:49:25 +0100243 gc->request = gpio_pin_request;
244 gc->free = gpio_pin_free;
245 gc->direction_input = gpio_pin_direction_input;
246 gc->get = gpio_pin_get;
247 gc->direction_output = gpio_pin_direction_output;
248 gc->set = gpio_pin_set;
249 gc->to_irq = gpio_pin_to_irq;
250
251 gc->label = pfc->info->name;
252 gc->dev = pfc->dev;
253 gc->owner = THIS_MODULE;
254 gc->base = 0;
Laurent Pinchart28818fa2013-07-15 13:48:56 +0200255 gc->ngpio = pfc->nr_gpio_pins;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100256
257 return 0;
Laurent Pinchart16883812012-12-06 14:49:25 +0100258}
259
260/* -----------------------------------------------------------------------------
261 * Function GPIOs
262 */
263
264static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
265{
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100266 static bool __print_once;
Laurent Pinchart16883812012-12-06 14:49:25 +0100267 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100268 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100269 unsigned long flags;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100270 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100271
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100272 if (!__print_once) {
273 dev_notice(pfc->dev,
274 "Use of GPIO API for function requests is deprecated."
275 " Convert to pinctrl\n");
276 __print_once = true;
277 }
Laurent Pinchart16883812012-12-06 14:49:25 +0100278
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100279 if (mark == 0)
Laurent Pinchartb705c052013-03-10 16:38:23 +0100280 return -EINVAL;
Laurent Pinchart16883812012-12-06 14:49:25 +0100281
282 spin_lock_irqsave(&pfc->lock, flags);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100283 ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
Laurent Pinchart16883812012-12-06 14:49:25 +0100284 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100285
Laurent Pinchart16883812012-12-06 14:49:25 +0100286 return ret;
287}
288
289static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
290{
Laurent Pinchart16883812012-12-06 14:49:25 +0100291}
292
Laurent Pincharte51d5342013-02-17 00:26:33 +0100293static int gpio_function_setup(struct sh_pfc_chip *chip)
Laurent Pinchart16883812012-12-06 14:49:25 +0100294{
295 struct sh_pfc *pfc = chip->pfc;
296 struct gpio_chip *gc = &chip->gpio_chip;
297
298 gc->request = gpio_function_request;
299 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900300
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100301 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900302 gc->owner = THIS_MODULE;
Laurent Pinchart28818fa2013-07-15 13:48:56 +0200303 gc->base = pfc->nr_gpio_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100304 gc->ngpio = pfc->info->nr_func_gpios;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100305
306 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900307}
308
Laurent Pinchart16883812012-12-06 14:49:25 +0100309/* -----------------------------------------------------------------------------
310 * Register/unregister
311 */
312
313static struct sh_pfc_chip *
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100314sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
315 struct sh_pfc_window *mem)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900316{
317 struct sh_pfc_chip *chip;
318 int ret;
319
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100320 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900321 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100322 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900323
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100324 chip->mem = mem;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900325 chip->pfc = pfc;
326
Laurent Pincharte51d5342013-02-17 00:26:33 +0100327 ret = setup(chip);
328 if (ret < 0)
329 return ERR_PTR(ret);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900330
331 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100332 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100333 return ERR_PTR(ret);
334
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100335 dev_info(pfc->dev, "%s handling gpio %u -> %u\n",
336 chip->gpio_chip.label, chip->gpio_chip.base,
337 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
Laurent Pinchart16883812012-12-06 14:49:25 +0100338
339 return chip;
340}
341
342int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
343{
344 struct sh_pfc_chip *chip;
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100345 phys_addr_t address;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100346 unsigned int i;
Laurent Pinchart247127f2013-03-08 00:45:12 +0100347 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100348
Laurent Pinchart1a4fd582013-03-10 03:19:44 +0100349 if (pfc->info->data_regs == NULL)
350 return 0;
351
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100352 /* Find the memory window that contain the GPIO registers. Boards that
353 * register a separate GPIO device will not supply a memory resource
354 * that covers the data registers. In that case don't try to handle
355 * GPIOs.
356 */
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100357 address = pfc->info->data_regs[0].reg;
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100358 for (i = 0; i < pfc->num_windows; ++i) {
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100359 struct sh_pfc_window *window = &pfc->windows[i];
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100360
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100361 if (address >= window->phys &&
362 address < window->phys + window->size)
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100363 break;
364 }
365
366 if (i == pfc->num_windows)
367 return 0;
368
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100369 /* If we have IRQ resources make sure their number is correct. */
370 if (pfc->num_irqs && pfc->num_irqs != pfc->info->gpio_irq_size) {
371 dev_err(pfc->dev, "invalid number of IRQ resources\n");
372 return -EINVAL;
373 }
374
Laurent Pinchart63d57382013-02-15 01:33:38 +0100375 /* Register the real GPIOs chip. */
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100376 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]);
Laurent Pinchart16883812012-12-06 14:49:25 +0100377 if (IS_ERR(chip))
378 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100379
380 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900381
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200382 /* Register the GPIO to pin mappings. As pins with GPIO ports must come
383 * first in the ranges, skip the pins without GPIO ports by stopping at
384 * the first range that contains such a pin.
385 */
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200386 for (i = 0; i < pfc->nr_ranges; ++i) {
387 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100388
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200389 if (range->start >= pfc->nr_gpio_pins)
390 break;
391
Laurent Pinchart63d57382013-02-15 01:33:38 +0100392 ret = gpiochip_add_pin_range(&chip->gpio_chip,
393 dev_name(pfc->dev),
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200394 range->start, range->start,
395 range->end - range->start + 1);
Laurent Pinchart63d57382013-02-15 01:33:38 +0100396 if (ret < 0)
397 return ret;
398 }
399
400 /* Register the function GPIOs chip. */
Laurent Pinchart542a5642013-03-07 14:31:57 +0100401 if (pfc->info->nr_func_gpios == 0)
402 return 0;
403
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100404 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
Laurent Pinchart16883812012-12-06 14:49:25 +0100405 if (IS_ERR(chip))
406 return PTR_ERR(chip);
407
408 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900409
Paul Mundtb3c185a2012-06-20 17:29:04 +0900410 return 0;
411}
412
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100413int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900414{
abdoulaye bertheb4e7c552014-07-12 22:30:13 +0200415 gpiochip_remove(&pfc->gpio->gpio_chip);
416 gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900417
abdoulaye bertheb4e7c552014-07-12 22:30:13 +0200418 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900419}