blob: ad06f5355d1ed1c59d99a6826ad5b6a796cfed2c [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>
Linus Walleijdf622672019-08-14 09:20:32 +020010#include <linux/gpio/driver.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)
Geert Uytterhoeven6793baa2020-02-12 10:02:00 +0100208 return pfc->irqs[i];
Paul Mundtb3c185a2012-06-20 17:29:04 +0900209 }
210 }
211
Geert Uytterhoeven96976432016-05-04 10:21:53 +0200212 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900213}
214
Laurent Pincharte51d5342013-02-17 00:26:33 +0100215static int gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900216{
217 struct sh_pfc *pfc = chip->pfc;
218 struct gpio_chip *gc = &chip->gpio_chip;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100219 int ret;
220
Kees Cooka86854d2018-06-12 14:07:58 -0700221 chip->pins = devm_kcalloc(pfc->dev,
222 pfc->info->nr_pins, sizeof(*chip->pins),
223 GFP_KERNEL);
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100224 if (chip->pins == NULL)
225 return -ENOMEM;
226
Laurent Pincharte51d5342013-02-17 00:26:33 +0100227 ret = gpio_setup_data_regs(chip);
228 if (ret < 0)
229 return ret;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900230
Laurent Pinchart16883812012-12-06 14:49:25 +0100231 gc->request = gpio_pin_request;
232 gc->free = gpio_pin_free;
233 gc->direction_input = gpio_pin_direction_input;
234 gc->get = gpio_pin_get;
235 gc->direction_output = gpio_pin_direction_output;
236 gc->set = gpio_pin_set;
237 gc->to_irq = gpio_pin_to_irq;
238
239 gc->label = pfc->info->name;
Linus Walleij58383c782015-11-04 09:56:26 +0100240 gc->parent = pfc->dev;
Laurent Pinchart16883812012-12-06 14:49:25 +0100241 gc->owner = THIS_MODULE;
242 gc->base = 0;
Laurent Pinchart28818fa2013-07-15 13:48:56 +0200243 gc->ngpio = pfc->nr_gpio_pins;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100244
245 return 0;
Laurent Pinchart16883812012-12-06 14:49:25 +0100246}
247
248/* -----------------------------------------------------------------------------
249 * Function GPIOs
250 */
251
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100252#ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
Laurent Pinchart16883812012-12-06 14:49:25 +0100253static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
254{
255 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100256 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100257 unsigned long flags;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100258 int ret;
Laurent Pinchart16883812012-12-06 14:49:25 +0100259
Geert Uytterhoeven6da53cf2019-07-31 11:41:16 +0200260 dev_notice_once(pfc->dev,
261 "Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
Laurent Pinchart16883812012-12-06 14:49:25 +0100262
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100263 if (mark == 0)
Laurent Pinchartb705c052013-03-10 16:38:23 +0100264 return -EINVAL;
Laurent Pinchart16883812012-12-06 14:49:25 +0100265
266 spin_lock_irqsave(&pfc->lock, flags);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100267 ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
Laurent Pinchart16883812012-12-06 14:49:25 +0100268 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchartb705c052013-03-10 16:38:23 +0100269
Laurent Pinchart16883812012-12-06 14:49:25 +0100270 return ret;
271}
272
Laurent Pincharte51d5342013-02-17 00:26:33 +0100273static int gpio_function_setup(struct sh_pfc_chip *chip)
Laurent Pinchart16883812012-12-06 14:49:25 +0100274{
275 struct sh_pfc *pfc = chip->pfc;
276 struct gpio_chip *gc = &chip->gpio_chip;
277
278 gc->request = gpio_function_request;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900279
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100280 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900281 gc->owner = THIS_MODULE;
Laurent Pinchart28818fa2013-07-15 13:48:56 +0200282 gc->base = pfc->nr_gpio_pins;
Laurent Pinchart16883812012-12-06 14:49:25 +0100283 gc->ngpio = pfc->info->nr_func_gpios;
Laurent Pincharte51d5342013-02-17 00:26:33 +0100284
285 return 0;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900286}
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100287#endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900288
Laurent Pinchart16883812012-12-06 14:49:25 +0100289/* -----------------------------------------------------------------------------
290 * Register/unregister
291 */
292
293static struct sh_pfc_chip *
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100294sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
295 struct sh_pfc_window *mem)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900296{
297 struct sh_pfc_chip *chip;
298 int ret;
299
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100300 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900301 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100302 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900303
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100304 chip->mem = mem;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900305 chip->pfc = pfc;
306
Laurent Pincharte51d5342013-02-17 00:26:33 +0100307 ret = setup(chip);
308 if (ret < 0)
309 return ERR_PTR(ret);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900310
Geert Uytterhoevenc29e2f22016-06-10 11:22:44 +0200311 ret = devm_gpiochip_add_data(pfc->dev, &chip->gpio_chip, chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100312 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100313 return ERR_PTR(ret);
314
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100315 dev_info(pfc->dev, "%s handling gpio %u -> %u\n",
316 chip->gpio_chip.label, chip->gpio_chip.base,
317 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
Laurent Pinchart16883812012-12-06 14:49:25 +0100318
319 return chip;
320}
321
322int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
323{
324 struct sh_pfc_chip *chip;
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100325 phys_addr_t address;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100326 unsigned int i;
Laurent Pinchart16883812012-12-06 14:49:25 +0100327
Laurent Pinchart1a4fd582013-03-10 03:19:44 +0100328 if (pfc->info->data_regs == NULL)
329 return 0;
330
Geert Uytterhoevenb589f242020-10-28 16:16:31 +0100331 /* Find the memory window that contains the GPIO registers. Boards that
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100332 * register a separate GPIO device will not supply a memory resource
333 * that covers the data registers. In that case don't try to handle
334 * GPIOs.
335 */
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100336 address = pfc->info->data_regs[0].reg;
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100337 for (i = 0; i < pfc->num_windows; ++i) {
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100338 struct sh_pfc_window *window = &pfc->windows[i];
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100339
Geert Uytterhoeven1f34de02015-03-12 11:09:16 +0100340 if (address >= window->phys &&
341 address < window->phys + window->size)
Laurent Pinchartceef91d2013-03-10 03:19:44 +0100342 break;
343 }
344
345 if (i == pfc->num_windows)
346 return 0;
347
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100348 /* If we have IRQ resources make sure their number is correct. */
Laurent Pinchart4adeabd2015-09-22 10:08:13 +0300349 if (pfc->num_irqs != pfc->info->gpio_irq_size) {
Laurent Pinchart70c8f012013-12-11 04:26:26 +0100350 dev_err(pfc->dev, "invalid number of IRQ resources\n");
351 return -EINVAL;
352 }
353
Laurent Pinchart63d57382013-02-15 01:33:38 +0100354 /* Register the real GPIOs chip. */
Laurent Pinchart5b46ac32013-12-11 04:26:25 +0100355 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]);
Laurent Pinchart16883812012-12-06 14:49:25 +0100356 if (IS_ERR(chip))
357 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100358
359 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900360
Geert Uytterhoeven18fab3992015-08-04 15:55:17 +0200361 if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node)
362 return 0;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100363
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100364#ifdef CONFIG_PINCTRL_SH_FUNC_GPIO
Geert Uytterhoeven90d06612015-08-27 22:07:23 +0200365 /*
366 * Register the GPIO to pin mappings. As pins with GPIO ports
367 * must come first in the ranges, skip the pins without GPIO
368 * ports by stopping at the first range that contains such a
369 * pin.
370 */
371 for (i = 0; i < pfc->nr_ranges; ++i) {
372 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
373 int ret;
Laurent Pinchart4f82e3e2013-07-15 21:10:54 +0200374
Geert Uytterhoeven90d06612015-08-27 22:07:23 +0200375 if (range->start >= pfc->nr_gpio_pins)
376 break;
Geert Uytterhoeven18fab3992015-08-04 15:55:17 +0200377
Geert Uytterhoeven90d06612015-08-27 22:07:23 +0200378 ret = gpiochip_add_pin_range(&chip->gpio_chip,
379 dev_name(pfc->dev), range->start, range->start,
380 range->end - range->start + 1);
381 if (ret < 0)
382 return ret;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100383 }
384
385 /* Register the function GPIOs chip. */
Geert Uytterhoeven1d0f9e12019-11-13 11:18:09 +0100386 if (pfc->info->nr_func_gpios) {
387 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
388 if (IS_ERR(chip))
389 return PTR_ERR(chip);
390 }
Geert Uytterhoeven0ace9592019-01-21 17:05:45 +0100391#endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */
Paul Mundtb3c185a2012-06-20 17:29:04 +0900392
Paul Mundtb3c185a2012-06-20 17:29:04 +0900393 return 0;
394}