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