treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[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 }
a603a2b8
LW
161}
162
863fbf49 163/**
af8b6375 164 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
863fbf49 165 * @np: device node to get GPIO from
a6b09191 166 * @propname: property name containing gpio specifier(s)
863fbf49 167 * @index: index of the GPIO
b908b53d 168 * @flags: a flags pointer to fill in
863fbf49 169 *
af8b6375 170 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
b908b53d
AV
171 * value on the error condition. If @flags is not NULL the function also fills
172 * in flags for the GPIO.
863fbf49 173 */
af8b6375
AC
174struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
175 const char *propname, int index, enum of_gpio_flags *flags)
863fbf49 176{
762c2e46
MY
177 struct of_phandle_args gpiospec;
178 struct gpio_chip *chip;
179 struct gpio_desc *desc;
64b60e09 180 int ret;
3d0f7cf0 181
c11e6f0f
SB
182 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
183 &gpiospec);
64b60e09 184 if (ret) {
7eb6ce2f
RH
185 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
186 __func__, propname, np, index);
af8b6375 187 return ERR_PTR(ret);
863fbf49
AV
188 }
189
c7e9d398 190 chip = of_find_gpiochip_by_xlate(&gpiospec);
762c2e46
MY
191 if (!chip) {
192 desc = ERR_PTR(-EPROBE_DEFER);
193 goto out;
194 }
762c2e46 195
99468c1a 196 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
762c2e46
MY
197 if (IS_ERR(desc))
198 goto out;
863fbf49 199
605f2d34 200 if (flags)
89a5e15b 201 of_gpio_flags_quirks(np, propname, flags, index);
a603a2b8 202
7eb6ce2f
RH
203 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
204 __func__, propname, np, index,
762c2e46
MY
205 PTR_ERR_OR_ZERO(desc));
206
207out:
208 of_node_put(gpiospec.np);
209
210 return desc;
863fbf49 211}
863fbf49 212
f01d9075
AC
213int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
214 int index, enum of_gpio_flags *flags)
215{
216 struct gpio_desc *desc;
217
218 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
219
220 if (IS_ERR(desc))
221 return PTR_ERR(desc);
222 else
223 return desc_to_gpio(desc);
224}
225EXPORT_SYMBOL(of_get_named_gpio_flags);
226
c8582339
LW
227/*
228 * The SPI GPIO bindings happened before we managed to establish that GPIO
229 * properties should be named "foo-gpios" so we have this special kludge for
230 * them.
231 */
232static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
233 enum of_gpio_flags *of_flags)
234{
235 char prop_name[32]; /* 32 is max size of property name */
236 struct device_node *np = dev->of_node;
237 struct gpio_desc *desc;
238
239 /*
240 * Hopefully the compiler stubs the rest of the function if this
241 * is false.
242 */
243 if (!IS_ENABLED(CONFIG_SPI_MASTER))
244 return ERR_PTR(-ENOENT);
245
246 /* Allow this specifically for "spi-gpio" devices */
247 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
248 return ERR_PTR(-ENOENT);
249
250 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
251 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
252
253 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
254 return desc;
255}
256
6a537d48
LW
257/*
258 * Some regulator bindings happened before we managed to establish that GPIO
259 * properties should be named "foo-gpios" so we have this special kludge for
260 * them.
261 */
262static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
263 enum of_gpio_flags *of_flags)
264{
265 /* These are the connection IDs we accept as legacy GPIO phandles */
266 const char *whitelist[] = {
267 "wlf,ldoena", /* Arizona */
268 "wlf,ldo1ena", /* WM8994 */
269 "wlf,ldo2ena", /* WM8994 */
270 };
271 struct device_node *np = dev->of_node;
272 struct gpio_desc *desc;
273 int i;
274
275 if (!IS_ENABLED(CONFIG_REGULATOR))
276 return ERR_PTR(-ENOENT);
277
278 if (!con_id)
279 return ERR_PTR(-ENOENT);
280
4b21f94a
AS
281 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
282 if (i < 0)
6a537d48
LW
283 return ERR_PTR(-ENOENT);
284
285 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
286 return desc;
287}
288
ea713bc4 289struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
fed7026a 290 unsigned int idx, unsigned long *flags)
ea713bc4
LW
291{
292 char prop_name[32]; /* 32 is max size of property name */
293 enum of_gpio_flags of_flags;
294 struct gpio_desc *desc;
295 unsigned int i;
296
c8582339 297 /* Try GPIO property "foo-gpios" and "foo-gpio" */
ea713bc4
LW
298 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
299 if (con_id)
300 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
301 gpio_suffixes[i]);
302 else
303 snprintf(prop_name, sizeof(prop_name), "%s",
304 gpio_suffixes[i]);
305
306 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
307 &of_flags);
6662ae6a
MR
308 /*
309 * -EPROBE_DEFER in our case means that we found a
310 * valid GPIO property, but no controller has been
311 * registered so far.
312 *
313 * This means we don't need to look any further for
314 * alternate name conventions, and we should really
315 * preserve the return code for our user to be able to
316 * retry probing later.
317 */
318 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
319 return desc;
320
ea713bc4
LW
321 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
322 break;
323 }
324
c8582339
LW
325 /* Special handling for SPI GPIOs if used */
326 if (IS_ERR(desc))
327 desc = of_find_spi_gpio(dev, con_id, &of_flags);
328
6a537d48 329 /* Special handling for regulator GPIOs if used */
ce27fb2c 330 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
6a537d48
LW
331 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
332
ea713bc4
LW
333 if (IS_ERR(desc))
334 return desc;
335
336 if (of_flags & OF_GPIO_ACTIVE_LOW)
337 *flags |= GPIO_ACTIVE_LOW;
338
339 if (of_flags & OF_GPIO_SINGLE_ENDED) {
4c0facdd 340 if (of_flags & OF_GPIO_OPEN_DRAIN)
ea713bc4
LW
341 *flags |= GPIO_OPEN_DRAIN;
342 else
343 *flags |= GPIO_OPEN_SOURCE;
344 }
345
e10f72bf
AJ
346 if (of_flags & OF_GPIO_TRANSITORY)
347 *flags |= GPIO_TRANSITORY;
05f479bf 348
d449991c
TP
349 if (of_flags & OF_GPIO_PULL_UP)
350 *flags |= GPIO_PULL_UP;
351 if (of_flags & OF_GPIO_PULL_DOWN)
352 *flags |= GPIO_PULL_DOWN;
353
ea713bc4
LW
354 return desc;
355}
356
f625d460 357/**
fd7337fd 358 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
f625d460 359 * @np: device node to get GPIO from
be715343 360 * @chip: GPIO chip whose hog is parsed
a79fead5 361 * @idx: Index of the GPIO to parse
f625d460 362 * @name: GPIO line name
fed7026a
AS
363 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
364 * of_find_gpio() or of_parse_own_gpio()
f625d460
BP
365 * @dflags: gpiod_flags - optional GPIO initialization flags
366 *
367 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
368 * value on the error condition.
369 */
fd7337fd 370static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
be715343 371 struct gpio_chip *chip,
a79fead5 372 unsigned int idx, const char **name,
fed7026a 373 unsigned long *lflags,
fd7337fd 374 enum gpiod_flags *dflags)
f625d460
BP
375{
376 struct device_node *chip_np;
377 enum of_gpio_flags xlate_flags;
be715343
MY
378 struct of_phandle_args gpiospec;
379 struct gpio_desc *desc;
a79fead5 380 unsigned int i;
f625d460 381 u32 tmp;
3f9547e1 382 int ret;
f625d460 383
be715343 384 chip_np = chip->of_node;
f625d460
BP
385 if (!chip_np)
386 return ERR_PTR(-EINVAL);
387
388 xlate_flags = 0;
2d6c06f5 389 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
f625d460
BP
390 *dflags = 0;
391
392 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
393 if (ret)
394 return ERR_PTR(ret);
395
be715343
MY
396 gpiospec.np = chip_np;
397 gpiospec.args_count = tmp;
f625d460 398
a79fead5
GU
399 for (i = 0; i < tmp; i++) {
400 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
401 &gpiospec.args[i]);
402 if (ret)
403 return ERR_PTR(ret);
404 }
f625d460 405
99468c1a 406 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
be715343
MY
407 if (IS_ERR(desc))
408 return desc;
f625d460
BP
409
410 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
411 *lflags |= GPIO_ACTIVE_LOW;
e10f72bf
AJ
412 if (xlate_flags & OF_GPIO_TRANSITORY)
413 *lflags |= GPIO_TRANSITORY;
f625d460
BP
414
415 if (of_property_read_bool(np, "input"))
416 *dflags |= GPIOD_IN;
417 else if (of_property_read_bool(np, "output-low"))
418 *dflags |= GPIOD_OUT_LOW;
419 else if (of_property_read_bool(np, "output-high"))
420 *dflags |= GPIOD_OUT_HIGH;
421 else {
62cdcb6c
RH
422 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
423 desc_to_gpio(desc), np);
f625d460
BP
424 return ERR_PTR(-EINVAL);
425 }
426
427 if (name && of_property_read_string(np, "line-name", name))
428 *name = np->name;
429
be715343 430 return desc;
f625d460
BP
431}
432
433/**
fd7337fd 434 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
f625d460
BP
435 * @chip: gpio chip to act on
436 *
437 * This is only used by of_gpiochip_add to request/set GPIO initial
438 * configuration.
ead066e6 439 * It returns error if it fails otherwise 0 on success.
f625d460 440 */
dfbd379b 441static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
f625d460
BP
442{
443 struct gpio_desc *desc = NULL;
444 struct device_node *np;
445 const char *name;
fed7026a 446 unsigned long lflags;
f625d460 447 enum gpiod_flags dflags;
a79fead5 448 unsigned int i;
dfbd379b 449 int ret;
f625d460 450
d1279d94 451 for_each_available_child_of_node(chip->of_node, np) {
f625d460
BP
452 if (!of_property_read_bool(np, "gpio-hog"))
453 continue;
454
a79fead5
GU
455 for (i = 0;; i++) {
456 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
457 &dflags);
458 if (IS_ERR(desc))
459 break;
460
461 ret = gpiod_hog(desc, name, lflags, dflags);
462 if (ret < 0) {
463 of_node_put(np);
464 return ret;
465 }
09e258af 466 }
f625d460 467 }
dfbd379b
LD
468
469 return 0;
f625d460
BP
470}
471
863fbf49 472/**
67049c50 473 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
a19e3da5 474 * @gc: pointer to the gpio_chip structure
67049c50 475 * @gpiospec: GPIO specifier as found in the device tree
b908b53d 476 * @flags: a flags pointer to fill in
863fbf49
AV
477 *
478 * This is simple translation function, suitable for the most 1:1 mapped
67049c50 479 * GPIO chips. This function performs only one sanity check: whether GPIO
863fbf49
AV
480 * is less than ngpios (that is specified in the gpio_chip).
481 */
15c9a0ac
GL
482int of_gpio_simple_xlate(struct gpio_chip *gc,
483 const struct of_phandle_args *gpiospec, u32 *flags)
863fbf49 484{
b908b53d
AV
485 /*
486 * We're discouraging gpio_cells < 2, since that way you'll have to
20a8a968 487 * write your own xlate function (that will have to retrieve the GPIO
b908b53d
AV
488 * number and the flags from a single gpio cell -- this is possible,
489 * but not recommended).
490 */
a19e3da5 491 if (gc->of_gpio_n_cells < 2) {
b908b53d
AV
492 WARN_ON(1);
493 return -EINVAL;
494 }
495
15c9a0ac
GL
496 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
497 return -EINVAL;
498
6270d830 499 if (gpiospec->args[0] >= gc->ngpio)
863fbf49
AV
500 return -EINVAL;
501
b908b53d 502 if (flags)
15c9a0ac 503 *flags = gpiospec->args[1];
b908b53d 504
15c9a0ac 505 return gpiospec->args[0];
863fbf49 506}
3038bbdf 507EXPORT_SYMBOL(of_gpio_simple_xlate);
863fbf49 508
863fbf49 509/**
3208b0f0 510 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
863fbf49
AV
511 * @np: device node of the GPIO chip
512 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
3208b0f0 513 * @data: driver data to store in the struct gpio_chip
863fbf49
AV
514 *
515 * To use this function you should allocate and fill mm_gc with:
516 *
517 * 1) In the gpio_chip structure:
518 * - all the callbacks
a19e3da5
AV
519 * - of_gpio_n_cells
520 * - of_xlate callback (optional)
863fbf49
AV
521 *
522 * 3) In the of_mm_gpio_chip structure:
523 * - save_regs callback (optional)
524 *
525 * If succeeded, this function will map bank's memory and will
526 * do all necessary work for you. Then you'll able to use .regs
527 * to manage GPIOs from the callbacks.
528 */
3208b0f0
LW
529int of_mm_gpiochip_add_data(struct device_node *np,
530 struct of_mm_gpio_chip *mm_gc,
531 void *data)
863fbf49
AV
532{
533 int ret = -ENOMEM;
a19e3da5 534 struct gpio_chip *gc = &mm_gc->gc;
863fbf49 535
7eb6ce2f 536 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
863fbf49
AV
537 if (!gc->label)
538 goto err0;
539
540 mm_gc->regs = of_iomap(np, 0);
541 if (!mm_gc->regs)
542 goto err1;
543
21451155 544 gc->base = -1;
863fbf49 545
863fbf49
AV
546 if (mm_gc->save_regs)
547 mm_gc->save_regs(mm_gc);
548
a19e3da5 549 mm_gc->gc.of_node = np;
863fbf49 550
3208b0f0 551 ret = gpiochip_add_data(gc, data);
863fbf49
AV
552 if (ret)
553 goto err2;
554
863fbf49
AV
555 return 0;
556err2:
863fbf49
AV
557 iounmap(mm_gc->regs);
558err1:
559 kfree(gc->label);
560err0:
7eb6ce2f 561 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
863fbf49
AV
562 return ret;
563}
3208b0f0 564EXPORT_SYMBOL(of_mm_gpiochip_add_data);
594fa265 565
d621e8ba
RRD
566/**
567 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
568 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
569 */
570void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
571{
572 struct gpio_chip *gc = &mm_gc->gc;
573
574 if (!mm_gc)
575 return;
576
577 gpiochip_remove(gc);
578 iounmap(mm_gc->regs);
579 kfree(gc->label);
580}
581EXPORT_SYMBOL(of_mm_gpiochip_remove);
582
726cb3ba
SB
583static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
584{
585 int len, i;
586 u32 start, count;
587 struct device_node *np = chip->of_node;
588
589 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
590 if (len < 0 || len % 2 != 0)
591 return;
592
593 for (i = 0; i < len; i += 2) {
594 of_property_read_u32_index(np, "gpio-reserved-ranges",
595 i, &start);
596 of_property_read_u32_index(np, "gpio-reserved-ranges",
597 i + 1, &count);
598 if (start >= chip->ngpio || start + count >= chip->ngpio)
599 continue;
600
601 bitmap_clear(chip->valid_mask, start, count);
602 }
603};
604
f23f1516 605#ifdef CONFIG_PINCTRL
28355f81 606static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
f23f1516
SH
607{
608 struct device_node *np = chip->of_node;
f23f1516 609 struct of_phandle_args pinspec;
1e63d7b9 610 struct pinctrl_dev *pctldev;
f23f1516 611 int index = 0, ret;
586a87e6
CR
612 const char *name;
613 static const char group_names_propname[] = "gpio-ranges-group-names";
614 struct property *group_names;
f23f1516
SH
615
616 if (!np)
28355f81 617 return 0;
f23f1516 618
586a87e6
CR
619 group_names = of_find_property(np, group_names_propname, NULL);
620
ad4e1a7c 621 for (;; index++) {
d9fe0039
SW
622 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
623 index, &pinspec);
f23f1516
SH
624 if (ret)
625 break;
626
1e63d7b9 627 pctldev = of_pinctrl_get(pinspec.np);
602cf638 628 of_node_put(pinspec.np);
1e63d7b9 629 if (!pctldev)
28355f81 630 return -EPROBE_DEFER;
f23f1516 631
586a87e6
CR
632 if (pinspec.args[2]) {
633 if (group_names) {
72858602 634 of_property_read_string_index(np,
586a87e6
CR
635 group_names_propname,
636 index, &name);
637 if (strlen(name)) {
7eb6ce2f
RH
638 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
639 np);
586a87e6
CR
640 break;
641 }
642 }
643 /* npins != 0: linear range */
644 ret = gpiochip_add_pin_range(chip,
645 pinctrl_dev_get_devname(pctldev),
646 pinspec.args[0],
647 pinspec.args[1],
648 pinspec.args[2]);
649 if (ret)
28355f81 650 return ret;
586a87e6
CR
651 } else {
652 /* npins == 0: special range */
653 if (pinspec.args[1]) {
7eb6ce2f
RH
654 pr_err("%pOF: Illegal gpio-range format.\n",
655 np);
586a87e6
CR
656 break;
657 }
658
659 if (!group_names) {
7eb6ce2f
RH
660 pr_err("%pOF: GPIO group range requested but no %s property.\n",
661 np, group_names_propname);
586a87e6
CR
662 break;
663 }
664
665 ret = of_property_read_string_index(np,
666 group_names_propname,
667 index, &name);
668 if (ret)
669 break;
670
671 if (!strlen(name)) {
7eb6ce2f
RH
672 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
673 np);
586a87e6
CR
674 break;
675 }
676
677 ret = gpiochip_add_pingroup_range(chip, pctldev,
678 pinspec.args[0], name);
679 if (ret)
28355f81 680 return ret;
586a87e6 681 }
ad4e1a7c 682 }
28355f81
TV
683
684 return 0;
f23f1516
SH
685}
686
f23f1516 687#else
28355f81 688static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
f23f1516
SH
689#endif
690
28355f81 691int of_gpiochip_add(struct gpio_chip *chip)
391c970c 692{
28355f81
TV
693 int status;
694
391c970c 695 if (!chip->of_node)
28355f81 696 return 0;
391c970c
AV
697
698 if (!chip->of_xlate) {
699 chip->of_gpio_n_cells = 2;
700 chip->of_xlate = of_gpio_simple_xlate;
701 }
702
1020dfd1
MY
703 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
704 return -EINVAL;
705
726cb3ba
SB
706 of_gpiochip_init_valid_mask(chip);
707
28355f81
TV
708 status = of_gpiochip_add_pin_range(chip);
709 if (status)
710 return status;
711
fd9c5531
LW
712 /* If the chip defines names itself, these take precedence */
713 if (!chip->names)
82270335
CL
714 devprop_gpiochip_set_names(chip,
715 of_fwnode_handle(chip->of_node));
fd9c5531 716
391c970c 717 of_node_get(chip->of_node);
f625d460 718
f7299d44
GU
719 status = of_gpiochip_scan_gpios(chip);
720 if (status) {
721 of_node_put(chip->of_node);
722 gpiochip_remove_pin_ranges(chip);
723 }
724
725 return status;
391c970c
AV
726}
727
728void of_gpiochip_remove(struct gpio_chip *chip)
729{
e93fa3f2 730 gpiochip_remove_pin_ranges(chip);
8a691550 731 of_node_put(chip->of_node);
391c970c 732}