blob: 97c1332c1045739a3093213e769acdf4e1999633 [file] [log] [blame]
Kuninori Morimoto63b6d7e2018-09-07 02:13:29 +00001// SPDX-License-Identifier: GPL-2.0
Paul Mundtb3c185a2012-06-20 17:29:04 +09002/*
3 * SuperH Pin Function Controller GPIO driver.
4 *
5 * Copyright (C) 2008 Magnus Damm
6 * Copyright (C) 2009 - 2012 Paul Mundt
Paul Mundtb3c185a2012-06-20 17:29:04 +09007 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +01008
Laurent Pinchart1724acf2012-12-15 23:50:48 +01009#include <linux/device.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090010#include <linux/gpio.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010011#include <linux/init.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090012#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090013#include <linux/pinctrl/consumer.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010014#include <linux/slab.h>
15#include <linux/spinlock.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090016
Laurent Pinchartf9165132012-12-15 23:50:44 +010017#include "core.h"
18
Laurent Pinchart51cb2262013-02-16 18:34:32 +010019struct sh_pfc_gpio_data_reg {
20 const struct pinmux_data_reg *info;
Geert Uytterhoevenfc889362015-02-27 18:38:04 +010021 u32 shadow;
Laurent Pinchart51cb2262013-02-16 18:34:32 +010022};
23
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010024struct sh_pfc_gpio_pin {
25 u8 dbit;
26 u8 dreg;
27};
Laurent Pincharte51d5342013-02-17 00:26:33 +010028
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010029struct sh_pfc_chip {
30 struct sh_pfc *pfc;
31 struct gpio_chip gpio_chip;
32
33 struct sh_pfc_window *mem;
Laurent Pinchart51cb2262013-02-16 18:34:32 +010034 struct sh_pfc_gpio_data_reg *regs;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010035 struct sh_pfc_gpio_pin *pins;
Paul Mundtb3c185a2012-06-20 17:29:04 +090036};
37
Paul Mundtb3c185a2012-06-20 17:29:04 +090038static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
39{
Linus Walleij7cb093c2015-12-08 10:24:54 +010040 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
41 return chip->pfc;
Paul Mundtb3c185a2012-06-20 17:29:04 +090042}
43
Laurent Pinchart757b0552013-07-15 13:25:08 +020044static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int offset,
Laurent Pinchart51cb2262013-02-16 18:34:32 +010045 struct sh_pfc_gpio_data_reg **reg,
46 unsigned int *bit)
Laurent Pinchart41f12192013-02-15 02:04:55 +010047{
Laurent Pinchart757b0552013-07-15 13:25:08 +020048 int idx = sh_pfc_get_pin_index(chip->pfc, offset);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010049 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
Laurent Pinchart41f12192013-02-15 02:04:55 +010050
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010051 *reg = &chip->regs[gpio_pin->dreg];
52 *bit = gpio_pin->dbit;
Laurent Pinchart41f12192013-02-15 02:04:55 +010053}
54
Geert Uytterhoevenfc889362015-02-27 18:38:04 +010055static u32 gpio_read_data_reg(struct sh_pfc_chip *chip,
56 const struct pinmux_data_reg *dreg)
Laurent Pincharte51d5342013-02-17 00:26:33 +010057{
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010058 phys_addr_t address = dreg->reg;
59 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
Laurent Pincharte51d5342013-02-17 00:26:33 +010060
61 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
62}
63
64static void gpio_write_data_reg(struct sh_pfc_chip *chip,
Geert Uytterhoevenfc889362015-02-27 18:38:04 +010065 const struct pinmux_data_reg *dreg, u32 value)
Laurent Pincharte51d5342013-02-17 00:26:33 +010066{
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +010067 phys_addr_t address = dreg->reg;
68 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
Laurent Pincharte51d5342013-02-17 00:26:33 +010069
70 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
71}
72
Laurent Pinchart757b0552013-07-15 13:25:08 +020073static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx)
Laurent Pinchart41f12192013-02-15 02:04:55 +010074{
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010075 struct sh_pfc *pfc = chip->pfc;
Laurent Pinchart757b0552013-07-15 13:25:08 +020076 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
77 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pincharte51d5342013-02-17 00:26:33 +010078 const struct pinmux_data_reg *dreg;
79 unsigned int bit;
80 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +010081
Geert Uytterhoeven17c7cbb2015-03-12 11:09:15 +010082 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
Laurent Pincharte51d5342013-02-17 00:26:33 +010083 for (bit = 0; bit < dreg->reg_width; bit++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010084 if (dreg->enum_ids[bit] == pin->enum_id) {
85 gpio_pin->dreg = i;
86 gpio_pin->dbit = bit;
Laurent Pinchart41f12192013-02-15 02:04:55 +010087 return;
88 }
89 }
Laurent Pinchart41f12192013-02-15 02:04:55 +010090 }
91
92 BUG();
93}
94
Laurent Pincharte51d5342013-02-17 00:26:33 +010095static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
Laurent Pinchart41f12192013-02-15 02:04:55 +010096{
Laurent Pincharte51d5342013-02-17 00:26:33 +010097 struct sh_pfc *pfc = chip->pfc;
Laurent Pinchart51cb2262013-02-16 18:34:32 +010098 const struct pinmux_data_reg *dreg;
Laurent Pincharte51d5342013-02-17 00:26:33 +010099 unsigned int i;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100100
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100101 /* Count the number of data registers, allocate memory and initialize
102 * them.
103 */
104 for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
105 ;
106
Kees Cooka86854d2018-06-12 14:07:58 -0700107 chip->regs = devm_kcalloc(pfc->dev, i, sizeof(*chip->regs),
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100108 GFP_KERNEL);
109 if (chip->regs == NULL)
110 return -ENOMEM;
111
112 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
113 chip->regs[i].info = dreg;
114 chip->regs[i].shadow = gpio_read_data_reg(chip, dreg);
115 }
Laurent Pincharte51d5342013-02-17 00:26:33 +0100116
117 for (i = 0; i < pfc->info->nr_pins; i++) {
118 if (pfc->info->pins[i].enum_id == 0)
Laurent Pinchart41f12192013-02-15 02:04:55 +0100119 continue;
120
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100121 gpio_setup_data_reg(chip, i);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100122 }
123
Laurent Pincharte51d5342013-02-17 00:26:33 +0100124 return 0;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100125}
126
Laurent Pinchart16883812012-12-06 14:49:25 +0100127/* -----------------------------------------------------------------------------
128 * Pin GPIOs
129 */
130
131static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900132{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100133 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100134 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100135
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100136 if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
Laurent Pinchart0b73ee52013-02-14 22:12:11 +0100137 return -EINVAL;
138
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200139 return pinctrl_gpio_request(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900140}
141
Laurent Pinchart16883812012-12-06 14:49:25 +0100142static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900143{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200144 return pinctrl_gpio_free(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900145}
146
Laurent Pincharte51d5342013-02-17 00:26:33 +0100147static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
148 int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900149{
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100150 struct sh_pfc_gpio_data_reg *reg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100151 unsigned int bit;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100152 unsigned int pos;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900153
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100154 gpio_get_data_reg(chip, offset, &reg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100155
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100156 pos = reg->info->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100157
158 if (value)
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100159 reg->shadow |= BIT(pos);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100160 else
Geert Uytterhoevenfc889362015-02-27 18:38:04 +0100161 reg->shadow &= ~BIT(pos);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100162
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100163 gpio_write_data_reg(chip, reg->info, reg->shadow);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900164}
165
Laurent Pinchart16883812012-12-06 14:49:25 +0100166static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900167{
Laurent Pinchart16883812012-12-06 14:49:25 +0100168 return pinctrl_gpio_direction_input(offset);
169}
170
171static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
172 int value)
173{
Linus Walleij7cb093c2015-12-08 10:24:54 +0100174 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
Laurent Pinchart16883812012-12-06 14:49:25 +0100175
176 return pinctrl_gpio_direction_output(offset);
177}
178
179static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
180{
Linus Walleij7cb093c2015-12-08 10:24:54 +0100181 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100182 struct sh_pfc_gpio_data_reg *reg;
Laurent Pinchart41f12192013-02-15 02:04:55 +0100183 unsigned int bit;
Geert Uytterhoevencef28a22015-03-12 11:09:14 +0100184 unsigned int pos;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900185
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100186 gpio_get_data_reg(chip, offset, &reg, &bit);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100187
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100188 pos = reg->info->reg_width - (bit + 1);
Laurent Pinchart41f12192013-02-15 02:04:55 +0100189
Laurent Pinchart51cb2262013-02-16 18:34:32 +0100190 return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900191}
192
Laurent Pinchart16883812012-12-06 14:49:25 +0100193static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +0900194{
Linus Walleij7cb093c2015-12-08 10:24:54 +0100195 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +0900196}
197
Laurent Pinchart16883812012-12-06 14:49:25 +0100198static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900199{
200 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart8d72a7f2013-12-11 04:26:21 +0100201 unsigned int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900202
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100203 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
Laurent Pinchart6d5bddd2013-12-16 20:25:15 +0100204 const short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900205
Laurent Pinchart316b2552013-12-11 04:26:22 +0100206 for (k = 0; gpios[k] >= 0; k++) {
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100207 if (gpios[k] == offset)
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100208 goto found;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900209 }
210 }
211
Geert Uytterhoeven96976432016-05-04 10:21:53 +0200212 return 0;
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100213
214found:
Laurent Pinchart4adeabd2015-09-22 10:08:13 +0300215 return pfc->irqs[i];
Paul Mundtb3c185a2012-06-20 17:29:04 +0900216}
217
Laurent Pincharte51d5342013-02-17 00:26:33 +0100218static int gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900219{
220 struct sh_pfc *pfc = chip->pfc;
221 struct gpio_chip *gc = &chip->gpio_chip;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100222 int ret;
223
Kees Cooka86854d2018-06-12 14:07:58 -0700224 chip->pins = devm_kcalloc(pfc->dev,
225 pfc->info->nr_pins, sizeof(*chip->pins),
226 GFP_KERNEL);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100227 if (chip->pins == NULL)
228 return -ENOMEM;
229
Laurent Pincharte51d5342013-02-17 00:26:33 +0100230 ret = gpio_setup_data_regs(chip);
231 if (ret < 0)
232 return ret;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900233
Laurent Pinchart16883812012-12-06 14:49:25 +0100234 gc->request = gpio_pin_request;
235 gc->free = gpio_pin_free;
236 gc->direction_input = gpio_pin_direction_input;
237 gc->get = gpio_pin_get;
238 gc->direction_output = gpio_pin_direction_output;
239 gc->set = gpio_pin_set;
240 gc->to_irq = gpio_pin_to_irq;
241
242 gc->label = pfc->info->name;
Linus Walleij58383c782015-11-04 09:56:26 +0100243 gc->parent = pfc->dev;
Laurent Pinchart16883812012-12-06 14:49:25 +0100244 gc->owner = THIS_MODULE;
245 gc->base = 0;
Laurent Pinchart28818fa2013-07-15 13:48:56 +0200246 gc->ngpio = pfc->nr_gpio_pins;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100247
248 return 0;
Laurent Pinchart16883812012-12-06 14:49:25 +0100249}
250
251/* -----------------------------------------------------------------------------
252 * Function GPIOs
253 */
254
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100255#ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
Laurent Pinchart16883812012-12-06 14:49:25 +0100256static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
257{
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100258 static bool __print_once;
Laurent Pinchart16883812012-12-06 14:49:25 +0100259 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100260 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100261 unsigned long flags;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100262 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100263
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100264 if (!__print_once) {
265 dev_notice(pfc->dev,
266 "Use of GPIO API for function requests is deprecated."
267 " Convert to pinctrl\n");
268 __print_once = true;
269 }
Laurent Pinchart16883812012-12-06 14:49:25 +0100270
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100271 if (mark == 0)
Laurent Pinchartb705c052013-03-10 16:38:23 +0100272 return -EINVAL;
Laurent Pinchart16883812012-12-06 14:49:25 +0100273
274 spin_lock_irqsave(&pfc->lock, flags);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100275 ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
Laurent Pinchart16883812012-12-06 14:49:25 +0100276 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100277
Laurent Pinchart16883812012-12-06 14:49:25 +0100278 return ret;
279}
280
Laurent Pincharte51d5342013-02-17 00:26:33 +0100281static int gpio_function_setup(struct sh_pfc_chip *chip)
Laurent Pinchart16883812012-12-06 14:49:25 +0100282{
283 struct sh_pfc *pfc = chip->pfc;
284 struct gpio_chip *gc = &chip->gpio_chip;
285
286 gc->request = gpio_function_request;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900287
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100288 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900289 gc->owner = THIS_MODULE;
Laurent Pinchart28818fa2013-07-15 13:48:56 +0200290 gc->base = pfc->nr_gpio_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100291 gc->ngpio = pfc->info->nr_func_gpios;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100292
293 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900294}
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100295#endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900296
Laurent Pinchart16883812012-12-06 14:49:25 +0100297/* -----------------------------------------------------------------------------
298 * Register/unregister
299 */
300
301static struct sh_pfc_chip *
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100302sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
303 struct sh_pfc_window *mem)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900304{
305 struct sh_pfc_chip *chip;
306 int ret;
307
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100308 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900309 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100310 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900311
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100312 chip->mem = mem;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900313 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
Geert Uytterhoevenc29e2f22016-06-10 11:22:44 +0200319 ret = devm_gpiochip_add_data(pfc->dev, &chip->gpio_chip, 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
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100323 dev_info(pfc->dev, "%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);
Laurent Pinchart16883812012-12-06 14:49:25 +0100326
327 return chip;
328}
329
330int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
331{
332 struct sh_pfc_chip *chip;
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100333 phys_addr_t address;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100334 unsigned int i;
Laurent Pinchart16883812012-12-06 14:49:25 +0100335
Laurent Pinchart1a4fd582013-03-10 03:19:44 +0100336 if (pfc->info->data_regs == NULL)
337 return 0;
338
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100339 /* Find the memory window that contain the GPIO registers. Boards that
340 * register a separate GPIO device will not supply a memory resource
341 * that covers the data registers. In that case don't try to handle
342 * GPIOs.
343 */
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100344 address = pfc->info->data_regs[0].reg;
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100345 for (i = 0; i < pfc->num_windows; ++i) {
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100346 struct sh_pfc_window *window = &pfc->windows[i];
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100347
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100348 if (address >= window->phys &&
349 address < window->phys + window->size)
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100350 break;
351 }
352
353 if (i == pfc->num_windows)
354 return 0;
355
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100356 /* If we have IRQ resources make sure their number is correct. */
Laurent Pinchart4adeabd2015-09-22 10:08:13 +0300357 if (pfc->num_irqs != pfc->info->gpio_irq_size) {
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100358 dev_err(pfc->dev, "invalid number of IRQ resources\n");
359 return -EINVAL;
360 }
361
Laurent Pinchart63d57382013-02-15 01:33:38 +0100362 /* Register the real GPIOs chip. */
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100363 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]);
Laurent Pinchart16883812012-12-06 14:49:25 +0100364 if (IS_ERR(chip))
365 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100366
367 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900368
Geert Uytterhoeven18fab3992015-08-04 15:55:17 +0200369 if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node)
370 return 0;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100371
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100372#ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
Geert Uytterhoeven90d06612015-08-27 22:07:23 +0200373 /*
374 * Register the GPIO to pin mappings. As pins with GPIO ports
375 * must come first in the ranges, skip the pins without GPIO
376 * ports by stopping at the first range that contains such a
377 * pin.
378 */
379 for (i = 0; i < pfc->nr_ranges; ++i) {
380 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
381 int ret;
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200382
Geert Uytterhoeven90d06612015-08-27 22:07:23 +0200383 if (range->start >= pfc->nr_gpio_pins)
384 break;
Geert Uytterhoeven18fab3992015-08-04 15:55:17 +0200385
Geert Uytterhoeven90d06612015-08-27 22:07:23 +0200386 ret = gpiochip_add_pin_range(&chip->gpio_chip,
387 dev_name(pfc->dev), range->start, range->start,
388 range->end - range->start + 1);
389 if (ret < 0)
390 return ret;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100391 }
392
393 /* Register the function GPIOs chip. */
Laurent Pinchart542a5642013-03-07 14:31:57 +0100394 if (pfc->info->nr_func_gpios == 0)
395 return 0;
396
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100397 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
Laurent Pinchart16883812012-12-06 14:49:25 +0100398 if (IS_ERR(chip))
399 return PTR_ERR(chip);
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100400#endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900401
Paul Mundtb3c185a2012-06-20 17:29:04 +0900402 return 0;
403}