gpio: of: parse stmmac PHY reset line specific active-low property
[linux-2.6-block.git] / drivers / gpio / gpiolib-of.c
CommitLineData
27038c3e 1// SPDX-License-Identifier: GPL-2.0+
863fbf49
AV
2/*
3 * OF helpers for the GPIO API
4 *
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
6 *
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
863fbf49
AV
8 */
9
2e13cba8 10#include <linux/device.h>
bea4dbee 11#include <linux/err.h>
863fbf49 12#include <linux/errno.h>
2e13cba8 13#include <linux/module.h>
863fbf49 14#include <linux/io.h>
af8b6375 15#include <linux/gpio/consumer.h>
863fbf49 16#include <linux/of.h>
2e13cba8 17#include <linux/of_address.h>
863fbf49 18#include <linux/of_gpio.h>
f23f1516 19#include <linux/pinctrl/pinctrl.h>
2e13cba8 20#include <linux/slab.h>
f625d460 21#include <linux/gpio/machine.h>
863fbf49 22
1bd6b601 23#include "gpiolib.h"
af8b6375 24
c7e9d398 25static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
3d0f7cf0 26{
c7e9d398
MY
27 struct of_phandle_args *gpiospec = data;
28
29 return chip->gpiodev->dev.of_node == gpiospec->np &&
d49b48f0 30 chip->of_xlate &&
c7e9d398 31 chip->of_xlate(chip, gpiospec, NULL) >= 0;
762c2e46 32}
3d0f7cf0 33
c7e9d398
MY
34static struct gpio_chip *of_find_gpiochip_by_xlate(
35 struct of_phandle_args *gpiospec)
762c2e46 36{
c7e9d398 37 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
3d0f7cf0 38}
3d0f7cf0 39
99468c1a
MY
40static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
41 struct of_phandle_args *gpiospec,
42 enum of_gpio_flags *flags)
3d0f7cf0 43{
3d0f7cf0
GL
44 int ret;
45
99468c1a
MY
46 if (chip->of_gpio_n_cells != gpiospec->args_count)
47 return ERR_PTR(-EINVAL);
48
49 ret = chip->of_xlate(chip, gpiospec, flags);
50 if (ret < 0)
51 return ERR_PTR(ret);
52
53 return gpiochip_get_desc(chip, ret);
3d0f7cf0
GL
54}
55
a603a2b8 56static void of_gpio_flags_quirks(struct device_node *np,
89a5e15b 57 const char *propname,
6953c57a
LW
58 enum of_gpio_flags *flags,
59 int index)
a603a2b8 60{
81c85ec1
LW
61 /*
62 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
63 */
64 if (IS_ENABLED(CONFIG_MMC)) {
89a5e15b
LW
65 /*
66 * Active low is the default according to the
67 * SDHCI specification and the device tree
68 * bindings. However the code in the current
69 * kernel was written such that the phandle
70 * flags were always respected, and "cd-inverted"
71 * would invert the flag from the device phandle.
72 */
73 if (!strcmp(propname, "cd-gpios")) {
74 if (of_property_read_bool(np, "cd-inverted"))
75 *flags ^= OF_GPIO_ACTIVE_LOW;
81c85ec1 76 }
89a5e15b
LW
77 if (!strcmp(propname, "wp-gpios")) {
78 if (of_property_read_bool(np, "wp-inverted"))
79 *flags ^= OF_GPIO_ACTIVE_LOW;
81c85ec1
LW
80 }
81 }
a603a2b8
LW
82 /*
83 * Some GPIO fixed regulator quirks.
84 * Note that active low is the default.
85 */
86 if (IS_ENABLED(CONFIG_REGULATOR) &&
906402a4
LW
87 (of_device_is_compatible(np, "regulator-fixed") ||
88 of_device_is_compatible(np, "reg-fixed-voltage") ||
a71a81e7
GU
89 (!(strcmp(propname, "enable-gpio") &&
90 strcmp(propname, "enable-gpios")) &&
91 of_device_is_compatible(np, "regulator-gpio")))) {
a603a2b8
LW
92 /*
93 * The regulator GPIO handles are specified such that the
94 * presence or absence of "enable-active-high" solely controls
95 * the polarity of the GPIO line. Any phandle flags must
96 * be actively ignored.
97 */
98 if (*flags & OF_GPIO_ACTIVE_LOW) {
99 pr_warn("%s GPIO handle specifies active low - ignored\n",
100 of_node_full_name(np));
101 *flags &= ~OF_GPIO_ACTIVE_LOW;
102 }
103 if (!of_property_read_bool(np, "enable-active-high"))
104 *flags |= OF_GPIO_ACTIVE_LOW;
105 }
106 /*
107 * Legacy open drain handling for fixed voltage regulators.
108 */
109 if (IS_ENABLED(CONFIG_REGULATOR) &&
110 of_device_is_compatible(np, "reg-fixed-voltage") &&
111 of_property_read_bool(np, "gpio-open-drain")) {
112 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
113 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
114 of_node_full_name(np));
115 }
6953c57a
LW
116
117 /*
118 * Legacy handling of SPI active high chip select. If we have a
119 * property named "cs-gpios" we need to inspect the child node
120 * to determine if the flags should have inverted semantics.
121 */
a71a81e7
GU
122 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
123 of_property_read_bool(np, "cs-gpios")) {
6953c57a
LW
124 struct device_node *child;
125 u32 cs;
126 int ret;
127
128 for_each_child_of_node(np, child) {
129 ret = of_property_read_u32(child, "reg", &cs);
c1c04cea 130 if (ret)
6953c57a
LW
131 continue;
132 if (cs == index) {
133 /*
134 * SPI children have active low chip selects
135 * by default. This can be specified negatively
136 * by just omitting "spi-cs-high" in the
137 * device node, or actively by tagging on
138 * GPIO_ACTIVE_LOW as flag in the device
139 * tree. If the line is simultaneously
140 * tagged as active low in the device tree
141 * and has the "spi-cs-high" set, we get a
142 * conflict and the "spi-cs-high" flag will
143 * take precedence.
144 */
7ce40277 145 if (of_property_read_bool(child, "spi-cs-high")) {
6953c57a
LW
146 if (*flags & OF_GPIO_ACTIVE_LOW) {
147 pr_warn("%s GPIO handle specifies active low - ignored\n",
7ce40277 148 of_node_full_name(child));
6953c57a
LW
149 *flags &= ~OF_GPIO_ACTIVE_LOW;
150 }
151 } else {
152 if (!(*flags & OF_GPIO_ACTIVE_LOW))
153 pr_info("%s enforce active low on chipselect handle\n",
7ce40277 154 of_node_full_name(child));
6953c57a
LW
155 *flags |= OF_GPIO_ACTIVE_LOW;
156 }
157 break;
158 }
159 }
160 }
edc1ef3f
MB
161
162 /* Legacy handling of stmmac's active-low PHY reset line */
163 if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
164 !strcmp(propname, "snps,reset-gpio") &&
165 of_property_read_bool(np, "snps,reset-active-low"))
166 *flags |= OF_GPIO_ACTIVE_LOW;
a603a2b8
LW
167}
168
863fbf49 169/**
af8b6375 170 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
863fbf49 171 * @np: device node to get GPIO from
a6b09191 172 * @propname: property name containing gpio specifier(s)
863fbf49 173 * @index: index of the GPIO
b908b53d 174 * @flags: a flags pointer to fill in
863fbf49 175 *
af8b6375 176 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
b908b53d
AV
177 * value on the error condition. If @flags is not NULL the function also fills
178 * in flags for the GPIO.
863fbf49 179 */
af8b6375
AC
180struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
181 const char *propname, int index, enum of_gpio_flags *flags)
863fbf49 182{
762c2e46
MY
183 struct of_phandle_args gpiospec;
184 struct gpio_chip *chip;
185 struct gpio_desc *desc;
64b60e09 186 int ret;
3d0f7cf0 187
c11e6f0f
SB
188 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
189 &gpiospec);
64b60e09 190 if (ret) {
7eb6ce2f
RH
191 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
192 __func__, propname, np, index);
af8b6375 193 return ERR_PTR(ret);
863fbf49
AV
194 }
195
c7e9d398 196 chip = of_find_gpiochip_by_xlate(&gpiospec);
762c2e46
MY
197 if (!chip) {
198 desc = ERR_PTR(-EPROBE_DEFER);
199 goto out;
200 }
762c2e46 201
99468c1a 202 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
762c2e46
MY
203 if (IS_ERR(desc))
204 goto out;
863fbf49 205
605f2d34 206 if (flags)
89a5e15b 207 of_gpio_flags_quirks(np, propname, flags, index);
a603a2b8 208
7eb6ce2f
RH
209 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
210 __func__, propname, np, index,
762c2e46
MY
211 PTR_ERR_OR_ZERO(desc));
212
213out:
214 of_node_put(gpiospec.np);
215
216 return desc;
863fbf49 217}
863fbf49 218
f01d9075
AC
219int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
220 int index, enum of_gpio_flags *flags)
221{
222 struct gpio_desc *desc;
223
224 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
225
226 if (IS_ERR(desc))
227 return PTR_ERR(desc);
228 else
229 return desc_to_gpio(desc);
230}
231EXPORT_SYMBOL(of_get_named_gpio_flags);
232
c8582339
LW
233/*
234 * The SPI GPIO bindings happened before we managed to establish that GPIO
235 * properties should be named "foo-gpios" so we have this special kludge for
236 * them.
237 */
238static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
239 enum of_gpio_flags *of_flags)
240{
241 char prop_name[32]; /* 32 is max size of property name */
242 struct device_node *np = dev->of_node;
243 struct gpio_desc *desc;
244
245 /*
246 * Hopefully the compiler stubs the rest of the function if this
247 * is false.
248 */
249 if (!IS_ENABLED(CONFIG_SPI_MASTER))
250 return ERR_PTR(-ENOENT);
251
252 /* Allow this specifically for "spi-gpio" devices */
253 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
254 return ERR_PTR(-ENOENT);
255
256 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
257 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
258
259 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
260 return desc;
261}
262
6a537d48
LW
263/*
264 * Some regulator bindings happened before we managed to establish that GPIO
265 * properties should be named "foo-gpios" so we have this special kludge for
266 * them.
267 */
268static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
269 enum of_gpio_flags *of_flags)
270{
271 /* These are the connection IDs we accept as legacy GPIO phandles */
272 const char *whitelist[] = {
273 "wlf,ldoena", /* Arizona */
274 "wlf,ldo1ena", /* WM8994 */
275 "wlf,ldo2ena", /* WM8994 */
276 };
277 struct device_node *np = dev->of_node;
278 struct gpio_desc *desc;
279 int i;
280
281 if (!IS_ENABLED(CONFIG_REGULATOR))
282 return ERR_PTR(-ENOENT);
283
284 if (!con_id)
285 return ERR_PTR(-ENOENT);
286
4b21f94a
AS
287 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
288 if (i < 0)
6a537d48
LW
289 return ERR_PTR(-ENOENT);
290
291 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
292 return desc;
293}
294
ea713bc4 295struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
fed7026a 296 unsigned int idx, unsigned long *flags)
ea713bc4
LW
297{
298 char prop_name[32]; /* 32 is max size of property name */
299 enum of_gpio_flags of_flags;
300 struct gpio_desc *desc;
301 unsigned int i;
302
c8582339 303 /* Try GPIO property "foo-gpios" and "foo-gpio" */
ea713bc4
LW
304 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
305 if (con_id)
306 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
307 gpio_suffixes[i]);
308 else
309 snprintf(prop_name, sizeof(prop_name), "%s",
310 gpio_suffixes[i]);
311
312 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
313 &of_flags);
6662ae6a
MR
314 /*
315 * -EPROBE_DEFER in our case means that we found a
316 * valid GPIO property, but no controller has been
317 * registered so far.
318 *
319 * This means we don't need to look any further for
320 * alternate name conventions, and we should really
321 * preserve the return code for our user to be able to
322 * retry probing later.
323 */
324 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
325 return desc;
326
ea713bc4
LW
327 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
328 break;
329 }
330
c8582339
LW
331 /* Special handling for SPI GPIOs if used */
332 if (IS_ERR(desc))
333 desc = of_find_spi_gpio(dev, con_id, &of_flags);
334
6a537d48 335 /* Special handling for regulator GPIOs if used */
ce27fb2c 336 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
6a537d48
LW
337 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
338
ea713bc4
LW
339 if (IS_ERR(desc))
340 return desc;
341
342 if (of_flags & OF_GPIO_ACTIVE_LOW)
343 *flags |= GPIO_ACTIVE_LOW;
344
345 if (of_flags & OF_GPIO_SINGLE_ENDED) {
4c0facdd 346 if (of_flags & OF_GPIO_OPEN_DRAIN)
ea713bc4
LW
347 *flags |= GPIO_OPEN_DRAIN;
348 else
349 *flags |= GPIO_OPEN_SOURCE;
350 }
351
e10f72bf
AJ
352 if (of_flags & OF_GPIO_TRANSITORY)
353 *flags |= GPIO_TRANSITORY;
05f479bf 354
d449991c
TP
355 if (of_flags & OF_GPIO_PULL_UP)
356 *flags |= GPIO_PULL_UP;
357 if (of_flags & OF_GPIO_PULL_DOWN)
358 *flags |= GPIO_PULL_DOWN;
359
ea713bc4
LW
360 return desc;
361}
362
f625d460 363/**
fd7337fd 364 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
f625d460 365 * @np: device node to get GPIO from
be715343 366 * @chip: GPIO chip whose hog is parsed
a79fead5 367 * @idx: Index of the GPIO to parse
f625d460 368 * @name: GPIO line name
fed7026a
AS
369 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
370 * of_find_gpio() or of_parse_own_gpio()
f625d460
BP
371 * @dflags: gpiod_flags - optional GPIO initialization flags
372 *
373 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
374 * value on the error condition.
375 */
fd7337fd 376static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
be715343 377 struct gpio_chip *chip,
a79fead5 378 unsigned int idx, const char **name,
fed7026a 379 unsigned long *lflags,
fd7337fd 380 enum gpiod_flags *dflags)
f625d460
BP
381{
382 struct device_node *chip_np;
383 enum of_gpio_flags xlate_flags;
be715343
MY
384 struct of_phandle_args gpiospec;
385 struct gpio_desc *desc;
a79fead5 386 unsigned int i;
f625d460 387 u32 tmp;
3f9547e1 388 int ret;
f625d460 389
be715343 390 chip_np = chip->of_node;
f625d460
BP
391 if (!chip_np)
392 return ERR_PTR(-EINVAL);
393
394 xlate_flags = 0;
2d6c06f5 395 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
f625d460
BP
396 *dflags = 0;
397
398 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
399 if (ret)
400 return ERR_PTR(ret);
401
be715343
MY
402 gpiospec.np = chip_np;
403 gpiospec.args_count = tmp;
f625d460 404
a79fead5
GU
405 for (i = 0; i < tmp; i++) {
406 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
407 &gpiospec.args[i]);
408 if (ret)
409 return ERR_PTR(ret);
410 }
f625d460 411
99468c1a 412 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
be715343
MY
413 if (IS_ERR(desc))
414 return desc;
f625d460
BP
415
416 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
417 *lflags |= GPIO_ACTIVE_LOW;
e10f72bf
AJ
418 if (xlate_flags & OF_GPIO_TRANSITORY)
419 *lflags |= GPIO_TRANSITORY;
f625d460
BP
420
421 if (of_property_read_bool(np, "input"))
422 *dflags |= GPIOD_IN;
423 else if (of_property_read_bool(np, "output-low"))
424 *dflags |= GPIOD_OUT_LOW;
425 else if (of_property_read_bool(np, "output-high"))
426 *dflags |= GPIOD_OUT_HIGH;
427 else {
62cdcb6c
RH
428 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
429 desc_to_gpio(desc), np);
f625d460
BP
430 return ERR_PTR(-EINVAL);
431 }
432
433 if (name && of_property_read_string(np, "line-name", name))
434 *name = np->name;
435
be715343 436 return desc;
f625d460
BP
437}
438
439/**
fd7337fd 440 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
f625d460
BP
441 * @chip: gpio chip to act on
442 *
443 * This is only used by of_gpiochip_add to request/set GPIO initial
444 * configuration.
ead066e6 445 * It returns error if it fails otherwise 0 on success.
f625d460 446 */
dfbd379b 447static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
f625d460
BP
448{
449 struct gpio_desc *desc = NULL;
450 struct device_node *np;
451 const char *name;
fed7026a 452 unsigned long lflags;
f625d460 453 enum gpiod_flags dflags;
a79fead5 454 unsigned int i;
dfbd379b 455 int ret;
f625d460 456
d1279d94 457 for_each_available_child_of_node(chip->of_node, np) {
f625d460
BP
458 if (!of_property_read_bool(np, "gpio-hog"))
459 continue;
460
a79fead5
GU
461 for (i = 0;; i++) {
462 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
463 &dflags);
464 if (IS_ERR(desc))
465 break;
466
467 ret = gpiod_hog(desc, name, lflags, dflags);
468 if (ret < 0) {
469 of_node_put(np);
470 return ret;
471 }
09e258af 472 }
f625d460 473 }
dfbd379b
LD
474
475 return 0;
f625d460
BP
476}
477
863fbf49 478/**
67049c50 479 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
a19e3da5 480 * @gc: pointer to the gpio_chip structure
67049c50 481 * @gpiospec: GPIO specifier as found in the device tree
b908b53d 482 * @flags: a flags pointer to fill in
863fbf49
AV
483 *
484 * This is simple translation function, suitable for the most 1:1 mapped
67049c50 485 * GPIO chips. This function performs only one sanity check: whether GPIO
863fbf49
AV
486 * is less than ngpios (that is specified in the gpio_chip).
487 */
15c9a0ac
GL
488int of_gpio_simple_xlate(struct gpio_chip *gc,
489 const struct of_phandle_args *gpiospec, u32 *flags)
863fbf49 490{
b908b53d
AV
491 /*
492 * We're discouraging gpio_cells < 2, since that way you'll have to
20a8a968 493 * write your own xlate function (that will have to retrieve the GPIO
b908b53d
AV
494 * number and the flags from a single gpio cell -- this is possible,
495 * but not recommended).
496 */
a19e3da5 497 if (gc->of_gpio_n_cells < 2) {
b908b53d
AV
498 WARN_ON(1);
499 return -EINVAL;
500 }
501
15c9a0ac
GL
502 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
503 return -EINVAL;
504
6270d830 505 if (gpiospec->args[0] >= gc->ngpio)
863fbf49
AV
506 return -EINVAL;
507
b908b53d 508 if (flags)
15c9a0ac 509 *flags = gpiospec->args[1];
b908b53d 510
15c9a0ac 511 return gpiospec->args[0];
863fbf49 512}
3038bbdf 513EXPORT_SYMBOL(of_gpio_simple_xlate);
863fbf49 514
863fbf49 515/**
3208b0f0 516 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
863fbf49
AV
517 * @np: device node of the GPIO chip
518 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
3208b0f0 519 * @data: driver data to store in the struct gpio_chip
863fbf49
AV
520 *
521 * To use this function you should allocate and fill mm_gc with:
522 *
523 * 1) In the gpio_chip structure:
524 * - all the callbacks
a19e3da5
AV
525 * - of_gpio_n_cells
526 * - of_xlate callback (optional)
863fbf49
AV
527 *
528 * 3) In the of_mm_gpio_chip structure:
529 * - save_regs callback (optional)
530 *
531 * If succeeded, this function will map bank's memory and will
532 * do all necessary work for you. Then you'll able to use .regs
533 * to manage GPIOs from the callbacks.
534 */
3208b0f0
LW
535int of_mm_gpiochip_add_data(struct device_node *np,
536 struct of_mm_gpio_chip *mm_gc,
537 void *data)
863fbf49
AV
538{
539 int ret = -ENOMEM;
a19e3da5 540 struct gpio_chip *gc = &mm_gc->gc;
863fbf49 541
7eb6ce2f 542 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
863fbf49
AV
543 if (!gc->label)
544 goto err0;
545
546 mm_gc->regs = of_iomap(np, 0);
547 if (!mm_gc->regs)
548 goto err1;
549
21451155 550 gc->base = -1;
863fbf49 551
863fbf49
AV
552 if (mm_gc->save_regs)
553 mm_gc->save_regs(mm_gc);
554
a19e3da5 555 mm_gc->gc.of_node = np;
863fbf49 556
3208b0f0 557 ret = gpiochip_add_data(gc, data);
863fbf49
AV
558 if (ret)
559 goto err2;
560
863fbf49
AV
561 return 0;
562err2:
863fbf49
AV
563 iounmap(mm_gc->regs);
564err1:
565 kfree(gc->label);
566err0:
7eb6ce2f 567 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
863fbf49
AV
568 return ret;
569}
3208b0f0 570EXPORT_SYMBOL(of_mm_gpiochip_add_data);
594fa265 571
d621e8ba
RRD
572/**
573 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
574 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
575 */
576void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
577{
578 struct gpio_chip *gc = &mm_gc->gc;
579
580 if (!mm_gc)
581 return;
582
583 gpiochip_remove(gc);
584 iounmap(mm_gc->regs);
585 kfree(gc->label);
586}
587EXPORT_SYMBOL(of_mm_gpiochip_remove);
588
726cb3ba
SB
589static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
590{
591 int len, i;
592 u32 start, count;
593 struct device_node *np = chip->of_node;
594
595 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
596 if (len < 0 || len % 2 != 0)
597 return;
598
599 for (i = 0; i < len; i += 2) {
600 of_property_read_u32_index(np, "gpio-reserved-ranges",
601 i, &start);
602 of_property_read_u32_index(np, "gpio-reserved-ranges",
603 i + 1, &count);
604 if (start >= chip->ngpio || start + count >= chip->ngpio)
605 continue;
606
607 bitmap_clear(chip->valid_mask, start, count);
608 }
609};
610
f23f1516 611#ifdef CONFIG_PINCTRL
28355f81 612static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
f23f1516
SH
613{
614 struct device_node *np = chip->of_node;
f23f1516 615 struct of_phandle_args pinspec;
1e63d7b9 616 struct pinctrl_dev *pctldev;
f23f1516 617 int index = 0, ret;
586a87e6
CR
618 const char *name;
619 static const char group_names_propname[] = "gpio-ranges-group-names";
620 struct property *group_names;
f23f1516
SH
621
622 if (!np)
28355f81 623 return 0;
f23f1516 624
586a87e6
CR
625 group_names = of_find_property(np, group_names_propname, NULL);
626
ad4e1a7c 627 for (;; index++) {
d9fe0039
SW
628 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
629 index, &pinspec);
f23f1516
SH
630 if (ret)
631 break;
632
1e63d7b9 633 pctldev = of_pinctrl_get(pinspec.np);
602cf638 634 of_node_put(pinspec.np);
1e63d7b9 635 if (!pctldev)
28355f81 636 return -EPROBE_DEFER;
f23f1516 637
586a87e6
CR
638 if (pinspec.args[2]) {
639 if (group_names) {
72858602 640 of_property_read_string_index(np,
586a87e6
CR
641 group_names_propname,
642 index, &name);
643 if (strlen(name)) {
7eb6ce2f
RH
644 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
645 np);
586a87e6
CR
646 break;
647 }
648 }
649 /* npins != 0: linear range */
650 ret = gpiochip_add_pin_range(chip,
651 pinctrl_dev_get_devname(pctldev),
652 pinspec.args[0],
653 pinspec.args[1],
654 pinspec.args[2]);
655 if (ret)
28355f81 656 return ret;
586a87e6
CR
657 } else {
658 /* npins == 0: special range */
659 if (pinspec.args[1]) {
7eb6ce2f
RH
660 pr_err("%pOF: Illegal gpio-range format.\n",
661 np);
586a87e6
CR
662 break;
663 }
664
665 if (!group_names) {
7eb6ce2f
RH
666 pr_err("%pOF: GPIO group range requested but no %s property.\n",
667 np, group_names_propname);
586a87e6
CR
668 break;
669 }
670
671 ret = of_property_read_string_index(np,
672 group_names_propname,
673 index, &name);
674 if (ret)
675 break;
676
677 if (!strlen(name)) {
7eb6ce2f
RH
678 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
679 np);
586a87e6
CR
680 break;
681 }
682
683 ret = gpiochip_add_pingroup_range(chip, pctldev,
684 pinspec.args[0], name);
685 if (ret)
28355f81 686 return ret;
586a87e6 687 }
ad4e1a7c 688 }
28355f81
TV
689
690 return 0;
f23f1516
SH
691}
692
f23f1516 693#else
28355f81 694static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
f23f1516
SH
695#endif
696
28355f81 697int of_gpiochip_add(struct gpio_chip *chip)
391c970c 698{
28355f81
TV
699 int status;
700
391c970c 701 if (!chip->of_node)
28355f81 702 return 0;
391c970c
AV
703
704 if (!chip->of_xlate) {
705 chip->of_gpio_n_cells = 2;
706 chip->of_xlate = of_gpio_simple_xlate;
707 }
708
1020dfd1
MY
709 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
710 return -EINVAL;
711
726cb3ba
SB
712 of_gpiochip_init_valid_mask(chip);
713
28355f81
TV
714 status = of_gpiochip_add_pin_range(chip);
715 if (status)
716 return status;
717
fd9c5531
LW
718 /* If the chip defines names itself, these take precedence */
719 if (!chip->names)
82270335
CL
720 devprop_gpiochip_set_names(chip,
721 of_fwnode_handle(chip->of_node));
fd9c5531 722
391c970c 723 of_node_get(chip->of_node);
f625d460 724
f7299d44
GU
725 status = of_gpiochip_scan_gpios(chip);
726 if (status) {
727 of_node_put(chip->of_node);
728 gpiochip_remove_pin_ranges(chip);
729 }
730
731 return status;
391c970c
AV
732}
733
734void of_gpiochip_remove(struct gpio_chip *chip)
735{
e93fa3f2 736 gpiochip_remove_pin_ranges(chip);
8a691550 737 of_node_put(chip->of_node);
391c970c 738}