Merge tag 'armsoc-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[linux-2.6-block.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         return desc;
349 }
350
351 /**
352  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
353  * @np:         device node to get GPIO from
354  * @chip:       GPIO chip whose hog is parsed
355  * @idx:        Index of the GPIO to parse
356  * @name:       GPIO line name
357  * @lflags:     gpio_lookup_flags - returned from of_find_gpio() or
358  *              of_parse_own_gpio()
359  * @dflags:     gpiod_flags - optional GPIO initialization flags
360  *
361  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
362  * value on the error condition.
363  */
364 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
365                                            struct gpio_chip *chip,
366                                            unsigned int idx, const char **name,
367                                            enum gpio_lookup_flags *lflags,
368                                            enum gpiod_flags *dflags)
369 {
370         struct device_node *chip_np;
371         enum of_gpio_flags xlate_flags;
372         struct of_phandle_args gpiospec;
373         struct gpio_desc *desc;
374         unsigned int i;
375         u32 tmp;
376         int ret;
377
378         chip_np = chip->of_node;
379         if (!chip_np)
380                 return ERR_PTR(-EINVAL);
381
382         xlate_flags = 0;
383         *lflags = 0;
384         *dflags = 0;
385
386         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
387         if (ret)
388                 return ERR_PTR(ret);
389
390         gpiospec.np = chip_np;
391         gpiospec.args_count = tmp;
392
393         for (i = 0; i < tmp; i++) {
394                 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
395                                                  &gpiospec.args[i]);
396                 if (ret)
397                         return ERR_PTR(ret);
398         }
399
400         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
401         if (IS_ERR(desc))
402                 return desc;
403
404         if (xlate_flags & OF_GPIO_ACTIVE_LOW)
405                 *lflags |= GPIO_ACTIVE_LOW;
406         if (xlate_flags & OF_GPIO_TRANSITORY)
407                 *lflags |= GPIO_TRANSITORY;
408
409         if (of_property_read_bool(np, "input"))
410                 *dflags |= GPIOD_IN;
411         else if (of_property_read_bool(np, "output-low"))
412                 *dflags |= GPIOD_OUT_LOW;
413         else if (of_property_read_bool(np, "output-high"))
414                 *dflags |= GPIOD_OUT_HIGH;
415         else {
416                 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
417                         desc_to_gpio(desc), np);
418                 return ERR_PTR(-EINVAL);
419         }
420
421         if (name && of_property_read_string(np, "line-name", name))
422                 *name = np->name;
423
424         return desc;
425 }
426
427 /**
428  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
429  * @chip:       gpio chip to act on
430  *
431  * This is only used by of_gpiochip_add to request/set GPIO initial
432  * configuration.
433  * It returns error if it fails otherwise 0 on success.
434  */
435 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
436 {
437         struct gpio_desc *desc = NULL;
438         struct device_node *np;
439         const char *name;
440         enum gpio_lookup_flags lflags;
441         enum gpiod_flags dflags;
442         unsigned int i;
443         int ret;
444
445         for_each_available_child_of_node(chip->of_node, np) {
446                 if (!of_property_read_bool(np, "gpio-hog"))
447                         continue;
448
449                 for (i = 0;; i++) {
450                         desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
451                                                  &dflags);
452                         if (IS_ERR(desc))
453                                 break;
454
455                         ret = gpiod_hog(desc, name, lflags, dflags);
456                         if (ret < 0) {
457                                 of_node_put(np);
458                                 return ret;
459                         }
460                 }
461         }
462
463         return 0;
464 }
465
466 /**
467  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
468  * @gc:         pointer to the gpio_chip structure
469  * @gpiospec:   GPIO specifier as found in the device tree
470  * @flags:      a flags pointer to fill in
471  *
472  * This is simple translation function, suitable for the most 1:1 mapped
473  * GPIO chips. This function performs only one sanity check: whether GPIO
474  * is less than ngpios (that is specified in the gpio_chip).
475  */
476 int of_gpio_simple_xlate(struct gpio_chip *gc,
477                          const struct of_phandle_args *gpiospec, u32 *flags)
478 {
479         /*
480          * We're discouraging gpio_cells < 2, since that way you'll have to
481          * write your own xlate function (that will have to retrieve the GPIO
482          * number and the flags from a single gpio cell -- this is possible,
483          * but not recommended).
484          */
485         if (gc->of_gpio_n_cells < 2) {
486                 WARN_ON(1);
487                 return -EINVAL;
488         }
489
490         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
491                 return -EINVAL;
492
493         if (gpiospec->args[0] >= gc->ngpio)
494                 return -EINVAL;
495
496         if (flags)
497                 *flags = gpiospec->args[1];
498
499         return gpiospec->args[0];
500 }
501 EXPORT_SYMBOL(of_gpio_simple_xlate);
502
503 /**
504  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
505  * @np:         device node of the GPIO chip
506  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
507  * @data:       driver data to store in the struct gpio_chip
508  *
509  * To use this function you should allocate and fill mm_gc with:
510  *
511  * 1) In the gpio_chip structure:
512  *    - all the callbacks
513  *    - of_gpio_n_cells
514  *    - of_xlate callback (optional)
515  *
516  * 3) In the of_mm_gpio_chip structure:
517  *    - save_regs callback (optional)
518  *
519  * If succeeded, this function will map bank's memory and will
520  * do all necessary work for you. Then you'll able to use .regs
521  * to manage GPIOs from the callbacks.
522  */
523 int of_mm_gpiochip_add_data(struct device_node *np,
524                             struct of_mm_gpio_chip *mm_gc,
525                             void *data)
526 {
527         int ret = -ENOMEM;
528         struct gpio_chip *gc = &mm_gc->gc;
529
530         gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
531         if (!gc->label)
532                 goto err0;
533
534         mm_gc->regs = of_iomap(np, 0);
535         if (!mm_gc->regs)
536                 goto err1;
537
538         gc->base = -1;
539
540         if (mm_gc->save_regs)
541                 mm_gc->save_regs(mm_gc);
542
543         mm_gc->gc.of_node = np;
544
545         ret = gpiochip_add_data(gc, data);
546         if (ret)
547                 goto err2;
548
549         return 0;
550 err2:
551         iounmap(mm_gc->regs);
552 err1:
553         kfree(gc->label);
554 err0:
555         pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
556         return ret;
557 }
558 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
559
560 /**
561  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
562  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
563  */
564 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
565 {
566         struct gpio_chip *gc = &mm_gc->gc;
567
568         if (!mm_gc)
569                 return;
570
571         gpiochip_remove(gc);
572         iounmap(mm_gc->regs);
573         kfree(gc->label);
574 }
575 EXPORT_SYMBOL(of_mm_gpiochip_remove);
576
577 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
578 {
579         int len, i;
580         u32 start, count;
581         struct device_node *np = chip->of_node;
582
583         len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
584         if (len < 0 || len % 2 != 0)
585                 return;
586
587         for (i = 0; i < len; i += 2) {
588                 of_property_read_u32_index(np, "gpio-reserved-ranges",
589                                            i, &start);
590                 of_property_read_u32_index(np, "gpio-reserved-ranges",
591                                            i + 1, &count);
592                 if (start >= chip->ngpio || start + count >= chip->ngpio)
593                         continue;
594
595                 bitmap_clear(chip->valid_mask, start, count);
596         }
597 };
598
599 #ifdef CONFIG_PINCTRL
600 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
601 {
602         struct device_node *np = chip->of_node;
603         struct of_phandle_args pinspec;
604         struct pinctrl_dev *pctldev;
605         int index = 0, ret;
606         const char *name;
607         static const char group_names_propname[] = "gpio-ranges-group-names";
608         struct property *group_names;
609
610         if (!np)
611                 return 0;
612
613         group_names = of_find_property(np, group_names_propname, NULL);
614
615         for (;; index++) {
616                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
617                                 index, &pinspec);
618                 if (ret)
619                         break;
620
621                 pctldev = of_pinctrl_get(pinspec.np);
622                 of_node_put(pinspec.np);
623                 if (!pctldev)
624                         return -EPROBE_DEFER;
625
626                 if (pinspec.args[2]) {
627                         if (group_names) {
628                                 of_property_read_string_index(np,
629                                                 group_names_propname,
630                                                 index, &name);
631                                 if (strlen(name)) {
632                                         pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
633                                                 np);
634                                         break;
635                                 }
636                         }
637                         /* npins != 0: linear range */
638                         ret = gpiochip_add_pin_range(chip,
639                                         pinctrl_dev_get_devname(pctldev),
640                                         pinspec.args[0],
641                                         pinspec.args[1],
642                                         pinspec.args[2]);
643                         if (ret)
644                                 return ret;
645                 } else {
646                         /* npins == 0: special range */
647                         if (pinspec.args[1]) {
648                                 pr_err("%pOF: Illegal gpio-range format.\n",
649                                         np);
650                                 break;
651                         }
652
653                         if (!group_names) {
654                                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
655                                         np, group_names_propname);
656                                 break;
657                         }
658
659                         ret = of_property_read_string_index(np,
660                                                 group_names_propname,
661                                                 index, &name);
662                         if (ret)
663                                 break;
664
665                         if (!strlen(name)) {
666                                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
667                                 np);
668                                 break;
669                         }
670
671                         ret = gpiochip_add_pingroup_range(chip, pctldev,
672                                                 pinspec.args[0], name);
673                         if (ret)
674                                 return ret;
675                 }
676         }
677
678         return 0;
679 }
680
681 #else
682 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
683 #endif
684
685 int of_gpiochip_add(struct gpio_chip *chip)
686 {
687         int status;
688
689         if (!chip->of_node)
690                 return 0;
691
692         if (!chip->of_xlate) {
693                 chip->of_gpio_n_cells = 2;
694                 chip->of_xlate = of_gpio_simple_xlate;
695         }
696
697         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
698                 return -EINVAL;
699
700         of_gpiochip_init_valid_mask(chip);
701
702         status = of_gpiochip_add_pin_range(chip);
703         if (status)
704                 return status;
705
706         /* If the chip defines names itself, these take precedence */
707         if (!chip->names)
708                 devprop_gpiochip_set_names(chip,
709                                            of_fwnode_handle(chip->of_node));
710
711         of_node_get(chip->of_node);
712
713         return of_gpiochip_scan_gpios(chip);
714 }
715
716 void of_gpiochip_remove(struct gpio_chip *chip)
717 {
718         gpiochip_remove_pin_ranges(chip);
719         of_node_put(chip->of_node);
720 }