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