OSDN Git Service

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