Merge tag 'pinctrl-v5.3-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[sfrench/cifs-2.6.git] / drivers / gpio / gpiolib-of.c
1 // SPDX-License-Identifier: GPL-2.0+
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>
8  */
9
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/io.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/of_gpio.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/slab.h>
21 #include <linux/gpio/machine.h>
22
23 #include "gpiolib.h"
24
25 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
26 {
27         struct of_phandle_args *gpiospec = data;
28
29         return chip->gpiodev->dev.of_node == gpiospec->np &&
30                                 chip->of_xlate &&
31                                 chip->of_xlate(chip, gpiospec, NULL) >= 0;
32 }
33
34 static struct gpio_chip *of_find_gpiochip_by_xlate(
35                                         struct of_phandle_args *gpiospec)
36 {
37         return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
38 }
39
40 static 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)
43 {
44         int ret;
45
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);
54 }
55
56 static void of_gpio_flags_quirks(struct device_node *np,
57                                  const char *propname,
58                                  enum of_gpio_flags *flags,
59                                  int index)
60 {
61         /*
62          * Handle MMC "cd-inverted" and "wp-inverted" semantics.
63          */
64         if (IS_ENABLED(CONFIG_MMC)) {
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;
76                 }
77                 if (!strcmp(propname, "wp-gpios")) {
78                         if (of_property_read_bool(np, "wp-inverted"))
79                                 *flags ^= OF_GPIO_ACTIVE_LOW;
80                 }
81         }
82         /*
83          * Some GPIO fixed regulator quirks.
84          * Note that active low is the default.
85          */
86         if (IS_ENABLED(CONFIG_REGULATOR) &&
87             (of_device_is_compatible(np, "regulator-fixed") ||
88              of_device_is_compatible(np, "reg-fixed-voltage") ||
89              (!(strcmp(propname, "enable-gpio") &&
90                 strcmp(propname, "enable-gpios")) &&
91               of_device_is_compatible(np, "regulator-gpio")))) {
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         }
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) && !strcmp(propname, "cs-gpios") &&
123             of_property_read_bool(np, "cs-gpios")) {
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);
130                         if (ret)
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                                  */
145                                 if (of_property_read_bool(child, "spi-cs-high")) {
146                                         if (*flags & OF_GPIO_ACTIVE_LOW) {
147                                                 pr_warn("%s GPIO handle specifies active low - ignored\n",
148                                                         of_node_full_name(child));
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",
154                                                         of_node_full_name(child));
155                                         *flags |= OF_GPIO_ACTIVE_LOW;
156                                 }
157                                 of_node_put(child);
158                                 break;
159                         }
160                 }
161         }
162
163         /* Legacy handling of stmmac's active-low PHY reset line */
164         if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
165             !strcmp(propname, "snps,reset-gpio") &&
166             of_property_read_bool(np, "snps,reset-active-low"))
167                 *flags |= OF_GPIO_ACTIVE_LOW;
168 }
169
170 /**
171  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
172  * @np:         device node to get GPIO from
173  * @propname:   property name containing gpio specifier(s)
174  * @index:      index of the GPIO
175  * @flags:      a flags pointer to fill in
176  *
177  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
178  * value on the error condition. If @flags is not NULL the function also fills
179  * in flags for the GPIO.
180  */
181 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
182                      const char *propname, int index, enum of_gpio_flags *flags)
183 {
184         struct of_phandle_args gpiospec;
185         struct gpio_chip *chip;
186         struct gpio_desc *desc;
187         int ret;
188
189         ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
190                                              &gpiospec);
191         if (ret) {
192                 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
193                         __func__, propname, np, index);
194                 return ERR_PTR(ret);
195         }
196
197         chip = of_find_gpiochip_by_xlate(&gpiospec);
198         if (!chip) {
199                 desc = ERR_PTR(-EPROBE_DEFER);
200                 goto out;
201         }
202
203         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
204         if (IS_ERR(desc))
205                 goto out;
206
207         if (flags)
208                 of_gpio_flags_quirks(np, propname, flags, index);
209
210         pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
211                  __func__, propname, np, index,
212                  PTR_ERR_OR_ZERO(desc));
213
214 out:
215         of_node_put(gpiospec.np);
216
217         return desc;
218 }
219
220 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
221                             int index, enum of_gpio_flags *flags)
222 {
223         struct gpio_desc *desc;
224
225         desc = of_get_named_gpiod_flags(np, list_name, index, flags);
226
227         if (IS_ERR(desc))
228                 return PTR_ERR(desc);
229         else
230                 return desc_to_gpio(desc);
231 }
232 EXPORT_SYMBOL(of_get_named_gpio_flags);
233
234 /*
235  * The SPI GPIO bindings happened before we managed to establish that GPIO
236  * properties should be named "foo-gpios" so we have this special kludge for
237  * them.
238  */
239 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
240                                           enum of_gpio_flags *of_flags)
241 {
242         char prop_name[32]; /* 32 is max size of property name */
243         struct device_node *np = dev->of_node;
244         struct gpio_desc *desc;
245
246         /*
247          * Hopefully the compiler stubs the rest of the function if this
248          * is false.
249          */
250         if (!IS_ENABLED(CONFIG_SPI_MASTER))
251                 return ERR_PTR(-ENOENT);
252
253         /* Allow this specifically for "spi-gpio" devices */
254         if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
255                 return ERR_PTR(-ENOENT);
256
257         /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
258         snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
259
260         desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
261         return desc;
262 }
263
264 /*
265  * The old Freescale bindings use simply "gpios" as name for the chip select
266  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
267  * with a special quirk.
268  */
269 static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
270                                              const char *con_id,
271                                              unsigned int idx,
272                                              unsigned long *flags)
273 {
274         struct device_node *np = dev->of_node;
275
276         if (!IS_ENABLED(CONFIG_SPI_MASTER))
277                 return ERR_PTR(-ENOENT);
278
279         /* Allow this specifically for Freescale devices */
280         if (!of_device_is_compatible(np, "fsl,spi") &&
281             !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
282                 return ERR_PTR(-ENOENT);
283         /* Allow only if asking for "cs-gpios" */
284         if (!con_id || strcmp(con_id, "cs"))
285                 return ERR_PTR(-ENOENT);
286
287         /*
288          * While all other SPI controllers use "cs-gpios" the Freescale
289          * uses just "gpios" so translate to that when "cs-gpios" is
290          * requested.
291          */
292         return of_find_gpio(dev, NULL, idx, flags);
293 }
294
295 /*
296  * Some regulator bindings happened before we managed to establish that GPIO
297  * properties should be named "foo-gpios" so we have this special kludge for
298  * them.
299  */
300 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
301                                                 enum of_gpio_flags *of_flags)
302 {
303         /* These are the connection IDs we accept as legacy GPIO phandles */
304         const char *whitelist[] = {
305                 "wlf,ldoena", /* Arizona */
306                 "wlf,ldo1ena", /* WM8994 */
307                 "wlf,ldo2ena", /* WM8994 */
308         };
309         struct device_node *np = dev->of_node;
310         struct gpio_desc *desc;
311         int i;
312
313         if (!IS_ENABLED(CONFIG_REGULATOR))
314                 return ERR_PTR(-ENOENT);
315
316         if (!con_id)
317                 return ERR_PTR(-ENOENT);
318
319         i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
320         if (i < 0)
321                 return ERR_PTR(-ENOENT);
322
323         desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
324         return desc;
325 }
326
327 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
328                                unsigned int idx, unsigned long *flags)
329 {
330         char prop_name[32]; /* 32 is max size of property name */
331         enum of_gpio_flags of_flags;
332         struct gpio_desc *desc;
333         unsigned int i;
334
335         /* Try GPIO property "foo-gpios" and "foo-gpio" */
336         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
337                 if (con_id)
338                         snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
339                                  gpio_suffixes[i]);
340                 else
341                         snprintf(prop_name, sizeof(prop_name), "%s",
342                                  gpio_suffixes[i]);
343
344                 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
345                                                 &of_flags);
346
347                 if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
348                         break;
349         }
350
351         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
352                 /* Special handling for SPI GPIOs if used */
353                 desc = of_find_spi_gpio(dev, con_id, &of_flags);
354         }
355
356         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
357                 /* This quirk looks up flags and all */
358                 desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
359                 if (!IS_ERR(desc))
360                         return desc;
361         }
362
363         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
364                 /* Special handling for regulator GPIOs if used */
365                 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
366         }
367
368         if (IS_ERR(desc))
369                 return desc;
370
371         if (of_flags & OF_GPIO_ACTIVE_LOW)
372                 *flags |= GPIO_ACTIVE_LOW;
373
374         if (of_flags & OF_GPIO_SINGLE_ENDED) {
375                 if (of_flags & OF_GPIO_OPEN_DRAIN)
376                         *flags |= GPIO_OPEN_DRAIN;
377                 else
378                         *flags |= GPIO_OPEN_SOURCE;
379         }
380
381         if (of_flags & OF_GPIO_TRANSITORY)
382                 *flags |= GPIO_TRANSITORY;
383
384         if (of_flags & OF_GPIO_PULL_UP)
385                 *flags |= GPIO_PULL_UP;
386         if (of_flags & OF_GPIO_PULL_DOWN)
387                 *flags |= GPIO_PULL_DOWN;
388
389         return desc;
390 }
391
392 /**
393  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
394  * @np:         device node to get GPIO from
395  * @chip:       GPIO chip whose hog is parsed
396  * @idx:        Index of the GPIO to parse
397  * @name:       GPIO line name
398  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
399  *              of_find_gpio() or of_parse_own_gpio()
400  * @dflags:     gpiod_flags - optional GPIO initialization flags
401  *
402  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
403  * value on the error condition.
404  */
405 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
406                                            struct gpio_chip *chip,
407                                            unsigned int idx, const char **name,
408                                            unsigned long *lflags,
409                                            enum gpiod_flags *dflags)
410 {
411         struct device_node *chip_np;
412         enum of_gpio_flags xlate_flags;
413         struct of_phandle_args gpiospec;
414         struct gpio_desc *desc;
415         unsigned int i;
416         u32 tmp;
417         int ret;
418
419         chip_np = chip->of_node;
420         if (!chip_np)
421                 return ERR_PTR(-EINVAL);
422
423         xlate_flags = 0;
424         *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
425         *dflags = 0;
426
427         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
428         if (ret)
429                 return ERR_PTR(ret);
430
431         gpiospec.np = chip_np;
432         gpiospec.args_count = tmp;
433
434         for (i = 0; i < tmp; i++) {
435                 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
436                                                  &gpiospec.args[i]);
437                 if (ret)
438                         return ERR_PTR(ret);
439         }
440
441         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
442         if (IS_ERR(desc))
443                 return desc;
444
445         if (xlate_flags & OF_GPIO_ACTIVE_LOW)
446                 *lflags |= GPIO_ACTIVE_LOW;
447         if (xlate_flags & OF_GPIO_TRANSITORY)
448                 *lflags |= GPIO_TRANSITORY;
449
450         if (of_property_read_bool(np, "input"))
451                 *dflags |= GPIOD_IN;
452         else if (of_property_read_bool(np, "output-low"))
453                 *dflags |= GPIOD_OUT_LOW;
454         else if (of_property_read_bool(np, "output-high"))
455                 *dflags |= GPIOD_OUT_HIGH;
456         else {
457                 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
458                         desc_to_gpio(desc), np);
459                 return ERR_PTR(-EINVAL);
460         }
461
462         if (name && of_property_read_string(np, "line-name", name))
463                 *name = np->name;
464
465         return desc;
466 }
467
468 /**
469  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
470  * @chip:       gpio chip to act on
471  *
472  * This is only used by of_gpiochip_add to request/set GPIO initial
473  * configuration.
474  * It returns error if it fails otherwise 0 on success.
475  */
476 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
477 {
478         struct gpio_desc *desc = NULL;
479         struct device_node *np;
480         const char *name;
481         unsigned long lflags;
482         enum gpiod_flags dflags;
483         unsigned int i;
484         int ret;
485
486         for_each_available_child_of_node(chip->of_node, np) {
487                 if (!of_property_read_bool(np, "gpio-hog"))
488                         continue;
489
490                 for (i = 0;; i++) {
491                         desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
492                                                  &dflags);
493                         if (IS_ERR(desc))
494                                 break;
495
496                         ret = gpiod_hog(desc, name, lflags, dflags);
497                         if (ret < 0) {
498                                 of_node_put(np);
499                                 return ret;
500                         }
501                 }
502         }
503
504         return 0;
505 }
506
507 /**
508  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
509  * @gc:         pointer to the gpio_chip structure
510  * @gpiospec:   GPIO specifier as found in the device tree
511  * @flags:      a flags pointer to fill in
512  *
513  * This is simple translation function, suitable for the most 1:1 mapped
514  * GPIO chips. This function performs only one sanity check: whether GPIO
515  * is less than ngpios (that is specified in the gpio_chip).
516  */
517 int of_gpio_simple_xlate(struct gpio_chip *gc,
518                          const struct of_phandle_args *gpiospec, u32 *flags)
519 {
520         /*
521          * We're discouraging gpio_cells < 2, since that way you'll have to
522          * write your own xlate function (that will have to retrieve the GPIO
523          * number and the flags from a single gpio cell -- this is possible,
524          * but not recommended).
525          */
526         if (gc->of_gpio_n_cells < 2) {
527                 WARN_ON(1);
528                 return -EINVAL;
529         }
530
531         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
532                 return -EINVAL;
533
534         if (gpiospec->args[0] >= gc->ngpio)
535                 return -EINVAL;
536
537         if (flags)
538                 *flags = gpiospec->args[1];
539
540         return gpiospec->args[0];
541 }
542 EXPORT_SYMBOL(of_gpio_simple_xlate);
543
544 /**
545  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
546  * @np:         device node of the GPIO chip
547  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
548  * @data:       driver data to store in the struct gpio_chip
549  *
550  * To use this function you should allocate and fill mm_gc with:
551  *
552  * 1) In the gpio_chip structure:
553  *    - all the callbacks
554  *    - of_gpio_n_cells
555  *    - of_xlate callback (optional)
556  *
557  * 3) In the of_mm_gpio_chip structure:
558  *    - save_regs callback (optional)
559  *
560  * If succeeded, this function will map bank's memory and will
561  * do all necessary work for you. Then you'll able to use .regs
562  * to manage GPIOs from the callbacks.
563  */
564 int of_mm_gpiochip_add_data(struct device_node *np,
565                             struct of_mm_gpio_chip *mm_gc,
566                             void *data)
567 {
568         int ret = -ENOMEM;
569         struct gpio_chip *gc = &mm_gc->gc;
570
571         gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
572         if (!gc->label)
573                 goto err0;
574
575         mm_gc->regs = of_iomap(np, 0);
576         if (!mm_gc->regs)
577                 goto err1;
578
579         gc->base = -1;
580
581         if (mm_gc->save_regs)
582                 mm_gc->save_regs(mm_gc);
583
584         mm_gc->gc.of_node = np;
585
586         ret = gpiochip_add_data(gc, data);
587         if (ret)
588                 goto err2;
589
590         return 0;
591 err2:
592         iounmap(mm_gc->regs);
593 err1:
594         kfree(gc->label);
595 err0:
596         pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
597         return ret;
598 }
599 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
600
601 /**
602  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
603  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
604  */
605 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
606 {
607         struct gpio_chip *gc = &mm_gc->gc;
608
609         if (!mm_gc)
610                 return;
611
612         gpiochip_remove(gc);
613         iounmap(mm_gc->regs);
614         kfree(gc->label);
615 }
616 EXPORT_SYMBOL(of_mm_gpiochip_remove);
617
618 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
619 {
620         int len, i;
621         u32 start, count;
622         struct device_node *np = chip->of_node;
623
624         len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
625         if (len < 0 || len % 2 != 0)
626                 return;
627
628         for (i = 0; i < len; i += 2) {
629                 of_property_read_u32_index(np, "gpio-reserved-ranges",
630                                            i, &start);
631                 of_property_read_u32_index(np, "gpio-reserved-ranges",
632                                            i + 1, &count);
633                 if (start >= chip->ngpio || start + count >= chip->ngpio)
634                         continue;
635
636                 bitmap_clear(chip->valid_mask, start, count);
637         }
638 };
639
640 #ifdef CONFIG_PINCTRL
641 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
642 {
643         struct device_node *np = chip->of_node;
644         struct of_phandle_args pinspec;
645         struct pinctrl_dev *pctldev;
646         int index = 0, ret;
647         const char *name;
648         static const char group_names_propname[] = "gpio-ranges-group-names";
649         struct property *group_names;
650
651         if (!np)
652                 return 0;
653
654         group_names = of_find_property(np, group_names_propname, NULL);
655
656         for (;; index++) {
657                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
658                                 index, &pinspec);
659                 if (ret)
660                         break;
661
662                 pctldev = of_pinctrl_get(pinspec.np);
663                 of_node_put(pinspec.np);
664                 if (!pctldev)
665                         return -EPROBE_DEFER;
666
667                 if (pinspec.args[2]) {
668                         if (group_names) {
669                                 of_property_read_string_index(np,
670                                                 group_names_propname,
671                                                 index, &name);
672                                 if (strlen(name)) {
673                                         pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
674                                                 np);
675                                         break;
676                                 }
677                         }
678                         /* npins != 0: linear range */
679                         ret = gpiochip_add_pin_range(chip,
680                                         pinctrl_dev_get_devname(pctldev),
681                                         pinspec.args[0],
682                                         pinspec.args[1],
683                                         pinspec.args[2]);
684                         if (ret)
685                                 return ret;
686                 } else {
687                         /* npins == 0: special range */
688                         if (pinspec.args[1]) {
689                                 pr_err("%pOF: Illegal gpio-range format.\n",
690                                         np);
691                                 break;
692                         }
693
694                         if (!group_names) {
695                                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
696                                         np, group_names_propname);
697                                 break;
698                         }
699
700                         ret = of_property_read_string_index(np,
701                                                 group_names_propname,
702                                                 index, &name);
703                         if (ret)
704                                 break;
705
706                         if (!strlen(name)) {
707                                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
708                                 np);
709                                 break;
710                         }
711
712                         ret = gpiochip_add_pingroup_range(chip, pctldev,
713                                                 pinspec.args[0], name);
714                         if (ret)
715                                 return ret;
716                 }
717         }
718
719         return 0;
720 }
721
722 #else
723 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
724 #endif
725
726 int of_gpiochip_add(struct gpio_chip *chip)
727 {
728         int status;
729
730         if (!chip->of_node)
731                 return 0;
732
733         if (!chip->of_xlate) {
734                 chip->of_gpio_n_cells = 2;
735                 chip->of_xlate = of_gpio_simple_xlate;
736         }
737
738         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
739                 return -EINVAL;
740
741         of_gpiochip_init_valid_mask(chip);
742
743         status = of_gpiochip_add_pin_range(chip);
744         if (status)
745                 return status;
746
747         /* If the chip defines names itself, these take precedence */
748         if (!chip->names)
749                 devprop_gpiochip_set_names(chip,
750                                            of_fwnode_handle(chip->of_node));
751
752         of_node_get(chip->of_node);
753
754         status = of_gpiochip_scan_gpios(chip);
755         if (status) {
756                 of_node_put(chip->of_node);
757                 gpiochip_remove_pin_ranges(chip);
758         }
759
760         return status;
761 }
762
763 void of_gpiochip_remove(struct gpio_chip *chip)
764 {
765         gpiochip_remove_pin_ranges(chip);
766         of_node_put(chip->of_node);
767 }