Merge tag 'arm64-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64...
[linux-2.6-microblaze.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          * This does not apply to an SPI device named "spi-gpio", because
123          * these have traditionally obtained their own GPIOs by parsing
124          * the device tree directly and did not respect any "spi-cs-high"
125          * property on the SPI bus children.
126          */
127         if (IS_ENABLED(CONFIG_SPI_MASTER) &&
128             !strcmp(propname, "cs-gpios") &&
129             !of_device_is_compatible(np, "spi-gpio") &&
130             of_property_read_bool(np, "cs-gpios")) {
131                 struct device_node *child;
132                 u32 cs;
133                 int ret;
134
135                 for_each_child_of_node(np, child) {
136                         ret = of_property_read_u32(child, "reg", &cs);
137                         if (ret)
138                                 continue;
139                         if (cs == index) {
140                                 /*
141                                  * SPI children have active low chip selects
142                                  * by default. This can be specified negatively
143                                  * by just omitting "spi-cs-high" in the
144                                  * device node, or actively by tagging on
145                                  * GPIO_ACTIVE_LOW as flag in the device
146                                  * tree. If the line is simultaneously
147                                  * tagged as active low in the device tree
148                                  * and has the "spi-cs-high" set, we get a
149                                  * conflict and the "spi-cs-high" flag will
150                                  * take precedence.
151                                  */
152                                 if (of_property_read_bool(child, "spi-cs-high")) {
153                                         if (*flags & OF_GPIO_ACTIVE_LOW) {
154                                                 pr_warn("%s GPIO handle specifies active low - ignored\n",
155                                                         of_node_full_name(child));
156                                                 *flags &= ~OF_GPIO_ACTIVE_LOW;
157                                         }
158                                 } else {
159                                         if (!(*flags & OF_GPIO_ACTIVE_LOW))
160                                                 pr_info("%s enforce active low on chipselect handle\n",
161                                                         of_node_full_name(child));
162                                         *flags |= OF_GPIO_ACTIVE_LOW;
163                                 }
164                                 break;
165                         }
166                 }
167         }
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  * Some regulator bindings happened before we managed to establish that GPIO
266  * properties should be named "foo-gpios" so we have this special kludge for
267  * them.
268  */
269 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
270                                                 enum of_gpio_flags *of_flags)
271 {
272         /* These are the connection IDs we accept as legacy GPIO phandles */
273         const char *whitelist[] = {
274                 "wlf,ldoena", /* Arizona */
275                 "wlf,ldo1ena", /* WM8994 */
276                 "wlf,ldo2ena", /* WM8994 */
277         };
278         struct device_node *np = dev->of_node;
279         struct gpio_desc *desc;
280         int i;
281
282         if (!IS_ENABLED(CONFIG_REGULATOR))
283                 return ERR_PTR(-ENOENT);
284
285         if (!con_id)
286                 return ERR_PTR(-ENOENT);
287
288         i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
289         if (i < 0)
290                 return ERR_PTR(-ENOENT);
291
292         desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
293         return desc;
294 }
295
296 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
297                                unsigned int idx, unsigned long *flags)
298 {
299         char prop_name[32]; /* 32 is max size of property name */
300         enum of_gpio_flags of_flags;
301         struct gpio_desc *desc;
302         unsigned int i;
303
304         /* Try GPIO property "foo-gpios" and "foo-gpio" */
305         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
306                 if (con_id)
307                         snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
308                                  gpio_suffixes[i]);
309                 else
310                         snprintf(prop_name, sizeof(prop_name), "%s",
311                                  gpio_suffixes[i]);
312
313                 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
314                                                 &of_flags);
315                 /*
316                  * -EPROBE_DEFER in our case means that we found a
317                  * valid GPIO property, but no controller has been
318                  * registered so far.
319                  *
320                  * This means we don't need to look any further for
321                  * alternate name conventions, and we should really
322                  * preserve the return code for our user to be able to
323                  * retry probing later.
324                  */
325                 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
326                         return desc;
327
328                 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
329                         break;
330         }
331
332         /* Special handling for SPI GPIOs if used */
333         if (IS_ERR(desc))
334                 desc = of_find_spi_gpio(dev, con_id, &of_flags);
335
336         /* Special handling for regulator GPIOs if used */
337         if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
338                 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
339
340         if (IS_ERR(desc))
341                 return desc;
342
343         if (of_flags & OF_GPIO_ACTIVE_LOW)
344                 *flags |= GPIO_ACTIVE_LOW;
345
346         if (of_flags & OF_GPIO_SINGLE_ENDED) {
347                 if (of_flags & OF_GPIO_OPEN_DRAIN)
348                         *flags |= GPIO_OPEN_DRAIN;
349                 else
350                         *flags |= GPIO_OPEN_SOURCE;
351         }
352
353         if (of_flags & OF_GPIO_TRANSITORY)
354                 *flags |= GPIO_TRANSITORY;
355
356         if (of_flags & OF_GPIO_PULL_UP)
357                 *flags |= GPIO_PULL_UP;
358         if (of_flags & OF_GPIO_PULL_DOWN)
359                 *flags |= GPIO_PULL_DOWN;
360
361         return desc;
362 }
363
364 /**
365  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
366  * @np:         device node to get GPIO from
367  * @chip:       GPIO chip whose hog is parsed
368  * @idx:        Index of the GPIO to parse
369  * @name:       GPIO line name
370  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
371  *              of_find_gpio() or of_parse_own_gpio()
372  * @dflags:     gpiod_flags - optional GPIO initialization flags
373  *
374  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
375  * value on the error condition.
376  */
377 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
378                                            struct gpio_chip *chip,
379                                            unsigned int idx, const char **name,
380                                            unsigned long *lflags,
381                                            enum gpiod_flags *dflags)
382 {
383         struct device_node *chip_np;
384         enum of_gpio_flags xlate_flags;
385         struct of_phandle_args gpiospec;
386         struct gpio_desc *desc;
387         unsigned int i;
388         u32 tmp;
389         int ret;
390
391         chip_np = chip->of_node;
392         if (!chip_np)
393                 return ERR_PTR(-EINVAL);
394
395         xlate_flags = 0;
396         *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
397         *dflags = 0;
398
399         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
400         if (ret)
401                 return ERR_PTR(ret);
402
403         gpiospec.np = chip_np;
404         gpiospec.args_count = tmp;
405
406         for (i = 0; i < tmp; i++) {
407                 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
408                                                  &gpiospec.args[i]);
409                 if (ret)
410                         return ERR_PTR(ret);
411         }
412
413         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
414         if (IS_ERR(desc))
415                 return desc;
416
417         if (xlate_flags & OF_GPIO_ACTIVE_LOW)
418                 *lflags |= GPIO_ACTIVE_LOW;
419         if (xlate_flags & OF_GPIO_TRANSITORY)
420                 *lflags |= GPIO_TRANSITORY;
421
422         if (of_property_read_bool(np, "input"))
423                 *dflags |= GPIOD_IN;
424         else if (of_property_read_bool(np, "output-low"))
425                 *dflags |= GPIOD_OUT_LOW;
426         else if (of_property_read_bool(np, "output-high"))
427                 *dflags |= GPIOD_OUT_HIGH;
428         else {
429                 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
430                         desc_to_gpio(desc), np);
431                 return ERR_PTR(-EINVAL);
432         }
433
434         if (name && of_property_read_string(np, "line-name", name))
435                 *name = np->name;
436
437         return desc;
438 }
439
440 /**
441  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
442  * @chip:       gpio chip to act on
443  *
444  * This is only used by of_gpiochip_add to request/set GPIO initial
445  * configuration.
446  * It returns error if it fails otherwise 0 on success.
447  */
448 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
449 {
450         struct gpio_desc *desc = NULL;
451         struct device_node *np;
452         const char *name;
453         unsigned long lflags;
454         enum gpiod_flags dflags;
455         unsigned int i;
456         int ret;
457
458         for_each_available_child_of_node(chip->of_node, np) {
459                 if (!of_property_read_bool(np, "gpio-hog"))
460                         continue;
461
462                 for (i = 0;; i++) {
463                         desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
464                                                  &dflags);
465                         if (IS_ERR(desc))
466                                 break;
467
468                         ret = gpiod_hog(desc, name, lflags, dflags);
469                         if (ret < 0) {
470                                 of_node_put(np);
471                                 return ret;
472                         }
473                 }
474         }
475
476         return 0;
477 }
478
479 /**
480  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
481  * @gc:         pointer to the gpio_chip structure
482  * @gpiospec:   GPIO specifier as found in the device tree
483  * @flags:      a flags pointer to fill in
484  *
485  * This is simple translation function, suitable for the most 1:1 mapped
486  * GPIO chips. This function performs only one sanity check: whether GPIO
487  * is less than ngpios (that is specified in the gpio_chip).
488  */
489 int of_gpio_simple_xlate(struct gpio_chip *gc,
490                          const struct of_phandle_args *gpiospec, u32 *flags)
491 {
492         /*
493          * We're discouraging gpio_cells < 2, since that way you'll have to
494          * write your own xlate function (that will have to retrieve the GPIO
495          * number and the flags from a single gpio cell -- this is possible,
496          * but not recommended).
497          */
498         if (gc->of_gpio_n_cells < 2) {
499                 WARN_ON(1);
500                 return -EINVAL;
501         }
502
503         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
504                 return -EINVAL;
505
506         if (gpiospec->args[0] >= gc->ngpio)
507                 return -EINVAL;
508
509         if (flags)
510                 *flags = gpiospec->args[1];
511
512         return gpiospec->args[0];
513 }
514 EXPORT_SYMBOL(of_gpio_simple_xlate);
515
516 /**
517  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
518  * @np:         device node of the GPIO chip
519  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
520  * @data:       driver data to store in the struct gpio_chip
521  *
522  * To use this function you should allocate and fill mm_gc with:
523  *
524  * 1) In the gpio_chip structure:
525  *    - all the callbacks
526  *    - of_gpio_n_cells
527  *    - of_xlate callback (optional)
528  *
529  * 3) In the of_mm_gpio_chip structure:
530  *    - save_regs callback (optional)
531  *
532  * If succeeded, this function will map bank's memory and will
533  * do all necessary work for you. Then you'll able to use .regs
534  * to manage GPIOs from the callbacks.
535  */
536 int of_mm_gpiochip_add_data(struct device_node *np,
537                             struct of_mm_gpio_chip *mm_gc,
538                             void *data)
539 {
540         int ret = -ENOMEM;
541         struct gpio_chip *gc = &mm_gc->gc;
542
543         gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
544         if (!gc->label)
545                 goto err0;
546
547         mm_gc->regs = of_iomap(np, 0);
548         if (!mm_gc->regs)
549                 goto err1;
550
551         gc->base = -1;
552
553         if (mm_gc->save_regs)
554                 mm_gc->save_regs(mm_gc);
555
556         mm_gc->gc.of_node = np;
557
558         ret = gpiochip_add_data(gc, data);
559         if (ret)
560                 goto err2;
561
562         return 0;
563 err2:
564         iounmap(mm_gc->regs);
565 err1:
566         kfree(gc->label);
567 err0:
568         pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
569         return ret;
570 }
571 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
572
573 /**
574  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
575  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
576  */
577 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
578 {
579         struct gpio_chip *gc = &mm_gc->gc;
580
581         if (!mm_gc)
582                 return;
583
584         gpiochip_remove(gc);
585         iounmap(mm_gc->regs);
586         kfree(gc->label);
587 }
588 EXPORT_SYMBOL(of_mm_gpiochip_remove);
589
590 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
591 {
592         int len, i;
593         u32 start, count;
594         struct device_node *np = chip->of_node;
595
596         len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
597         if (len < 0 || len % 2 != 0)
598                 return;
599
600         for (i = 0; i < len; i += 2) {
601                 of_property_read_u32_index(np, "gpio-reserved-ranges",
602                                            i, &start);
603                 of_property_read_u32_index(np, "gpio-reserved-ranges",
604                                            i + 1, &count);
605                 if (start >= chip->ngpio || start + count >= chip->ngpio)
606                         continue;
607
608                 bitmap_clear(chip->valid_mask, start, count);
609         }
610 };
611
612 #ifdef CONFIG_PINCTRL
613 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
614 {
615         struct device_node *np = chip->of_node;
616         struct of_phandle_args pinspec;
617         struct pinctrl_dev *pctldev;
618         int index = 0, ret;
619         const char *name;
620         static const char group_names_propname[] = "gpio-ranges-group-names";
621         struct property *group_names;
622
623         if (!np)
624                 return 0;
625
626         group_names = of_find_property(np, group_names_propname, NULL);
627
628         for (;; index++) {
629                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
630                                 index, &pinspec);
631                 if (ret)
632                         break;
633
634                 pctldev = of_pinctrl_get(pinspec.np);
635                 of_node_put(pinspec.np);
636                 if (!pctldev)
637                         return -EPROBE_DEFER;
638
639                 if (pinspec.args[2]) {
640                         if (group_names) {
641                                 of_property_read_string_index(np,
642                                                 group_names_propname,
643                                                 index, &name);
644                                 if (strlen(name)) {
645                                         pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
646                                                 np);
647                                         break;
648                                 }
649                         }
650                         /* npins != 0: linear range */
651                         ret = gpiochip_add_pin_range(chip,
652                                         pinctrl_dev_get_devname(pctldev),
653                                         pinspec.args[0],
654                                         pinspec.args[1],
655                                         pinspec.args[2]);
656                         if (ret)
657                                 return ret;
658                 } else {
659                         /* npins == 0: special range */
660                         if (pinspec.args[1]) {
661                                 pr_err("%pOF: Illegal gpio-range format.\n",
662                                         np);
663                                 break;
664                         }
665
666                         if (!group_names) {
667                                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
668                                         np, group_names_propname);
669                                 break;
670                         }
671
672                         ret = of_property_read_string_index(np,
673                                                 group_names_propname,
674                                                 index, &name);
675                         if (ret)
676                                 break;
677
678                         if (!strlen(name)) {
679                                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
680                                 np);
681                                 break;
682                         }
683
684                         ret = gpiochip_add_pingroup_range(chip, pctldev,
685                                                 pinspec.args[0], name);
686                         if (ret)
687                                 return ret;
688                 }
689         }
690
691         return 0;
692 }
693
694 #else
695 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
696 #endif
697
698 int of_gpiochip_add(struct gpio_chip *chip)
699 {
700         int status;
701
702         if (!chip->of_node)
703                 return 0;
704
705         if (!chip->of_xlate) {
706                 chip->of_gpio_n_cells = 2;
707                 chip->of_xlate = of_gpio_simple_xlate;
708         }
709
710         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
711                 return -EINVAL;
712
713         of_gpiochip_init_valid_mask(chip);
714
715         status = of_gpiochip_add_pin_range(chip);
716         if (status)
717                 return status;
718
719         /* If the chip defines names itself, these take precedence */
720         if (!chip->names)
721                 devprop_gpiochip_set_names(chip,
722                                            of_fwnode_handle(chip->of_node));
723
724         of_node_get(chip->of_node);
725
726         status = of_gpiochip_scan_gpios(chip);
727         if (status) {
728                 of_node_put(chip->of_node);
729                 gpiochip_remove_pin_ranges(chip);
730         }
731
732         return status;
733 }
734
735 void of_gpiochip_remove(struct gpio_chip *chip)
736 {
737         gpiochip_remove_pin_ranges(chip);
738         of_node_put(chip->of_node);
739 }