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