Merge tag 'dlm-6.6' of git://git.kernel.org/pub/scm/linux/kernel/git/teigland/linux-dlm
[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/io.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_gpio.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio/machine.h>
24
25 #include "gpiolib.h"
26 #include "gpiolib-of.h"
27
28 /*
29  * This is Linux-specific flags. By default controllers' and Linux' mapping
30  * match, but GPIO controllers are free to translate their own flags to
31  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
32  */
33 enum of_gpio_flags {
34         OF_GPIO_ACTIVE_LOW = 0x1,
35         OF_GPIO_SINGLE_ENDED = 0x2,
36         OF_GPIO_OPEN_DRAIN = 0x4,
37         OF_GPIO_TRANSITORY = 0x8,
38         OF_GPIO_PULL_UP = 0x10,
39         OF_GPIO_PULL_DOWN = 0x20,
40         OF_GPIO_PULL_DISABLE = 0x40,
41 };
42
43 /**
44  * of_gpio_named_count() - Count GPIOs for a device
45  * @np:         device node to count GPIOs for
46  * @propname:   property name containing gpio specifier(s)
47  *
48  * The function returns the count of GPIOs specified for a node.
49  * Note that the empty GPIO specifiers count too. Returns either
50  *   Number of gpios defined in property,
51  *   -EINVAL for an incorrectly formed gpios property, or
52  *   -ENOENT for a missing gpios property
53  *
54  * Example:
55  * gpios = <0
56  *          &gpio1 1 2
57  *          0
58  *          &gpio2 3 4>;
59  *
60  * The above example defines four GPIOs, two of which are not specified.
61  * This function will return '4'
62  */
63 static int of_gpio_named_count(const struct device_node *np,
64                                const char *propname)
65 {
66         return of_count_phandle_with_args(np, propname, "#gpio-cells");
67 }
68
69 /**
70  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
71  * @dev:    Consuming device
72  * @con_id: Function within the GPIO consumer
73  *
74  * Some elder GPIO controllers need special quirks. Currently we handle
75  * the Freescale and PPC GPIO controller with bindings that doesn't use the
76  * established "cs-gpios" for chip selects but instead rely on
77  * "gpios" for the chip select lines. If we detect this, we redirect
78  * the counting of "cs-gpios" to count "gpios" transparent to the
79  * driver.
80  */
81 static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
82 {
83         struct device_node *np = dev->of_node;
84
85         if (!IS_ENABLED(CONFIG_SPI_MASTER))
86                 return 0;
87         if (!con_id || strcmp(con_id, "cs"))
88                 return 0;
89         if (!of_device_is_compatible(np, "fsl,spi") &&
90             !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
91             !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
92                 return 0;
93         return of_gpio_named_count(np, "gpios");
94 }
95
96 int of_gpio_get_count(struct device *dev, const char *con_id)
97 {
98         int ret;
99         char propname[32];
100         unsigned int i;
101
102         ret = of_gpio_spi_cs_get_count(dev, con_id);
103         if (ret > 0)
104                 return ret;
105
106         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
107                 if (con_id)
108                         snprintf(propname, sizeof(propname), "%s-%s",
109                                  con_id, gpio_suffixes[i]);
110                 else
111                         snprintf(propname, sizeof(propname), "%s",
112                                  gpio_suffixes[i]);
113
114                 ret = of_gpio_named_count(dev->of_node, propname);
115                 if (ret > 0)
116                         break;
117         }
118         return ret ? ret : -ENOENT;
119 }
120
121 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
122 {
123         struct of_phandle_args *gpiospec = data;
124
125         return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) &&
126                                 chip->of_xlate &&
127                                 chip->of_xlate(chip, gpiospec, NULL) >= 0;
128 }
129
130 static struct gpio_chip *of_find_gpiochip_by_xlate(
131                                         struct of_phandle_args *gpiospec)
132 {
133         return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
134 }
135
136 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
137                                         struct of_phandle_args *gpiospec,
138                                         enum of_gpio_flags *flags)
139 {
140         int ret;
141
142         if (chip->of_gpio_n_cells != gpiospec->args_count)
143                 return ERR_PTR(-EINVAL);
144
145         ret = chip->of_xlate(chip, gpiospec, flags);
146         if (ret < 0)
147                 return ERR_PTR(ret);
148
149         return gpiochip_get_desc(chip, ret);
150 }
151
152 /*
153  * Overrides stated polarity of a gpio line and warns when there is a
154  * discrepancy.
155  */
156 static void of_gpio_quirk_polarity(const struct device_node *np,
157                                    bool active_high,
158                                    enum of_gpio_flags *flags)
159 {
160         if (active_high) {
161                 if (*flags & OF_GPIO_ACTIVE_LOW) {
162                         pr_warn("%s GPIO handle specifies active low - ignored\n",
163                                 of_node_full_name(np));
164                         *flags &= ~OF_GPIO_ACTIVE_LOW;
165                 }
166         } else {
167                 if (!(*flags & OF_GPIO_ACTIVE_LOW))
168                         pr_info("%s enforce active low on GPIO handle\n",
169                                 of_node_full_name(np));
170                 *flags |= OF_GPIO_ACTIVE_LOW;
171         }
172 }
173
174 /*
175  * This quirk does static polarity overrides in cases where existing
176  * DTS specified incorrect polarity.
177  */
178 static void of_gpio_try_fixup_polarity(const struct device_node *np,
179                                        const char *propname,
180                                        enum of_gpio_flags *flags)
181 {
182         static const struct {
183                 const char *compatible;
184                 const char *propname;
185                 bool active_high;
186         } gpios[] = {
187 #if !IS_ENABLED(CONFIG_LCD_HX8357)
188                 /*
189                  * Himax LCD controllers used incorrectly named
190                  * "gpios-reset" property and also specified wrong
191                  * polarity.
192                  */
193                 { "himax,hx8357",       "gpios-reset",  false },
194                 { "himax,hx8369",       "gpios-reset",  false },
195 #endif
196         };
197         unsigned int i;
198
199         for (i = 0; i < ARRAY_SIZE(gpios); i++) {
200                 if (of_device_is_compatible(np, gpios[i].compatible) &&
201                     !strcmp(propname, gpios[i].propname)) {
202                         of_gpio_quirk_polarity(np, gpios[i].active_high, flags);
203                         break;
204                 }
205         }
206 }
207
208 static void of_gpio_set_polarity_by_property(const struct device_node *np,
209                                              const char *propname,
210                                              enum of_gpio_flags *flags)
211 {
212         const struct device_node *np_compat = np;
213         const struct device_node *np_propname = np;
214         static const struct {
215                 const char *compatible;
216                 const char *gpio_propname;
217                 const char *polarity_propname;
218         } gpios[] = {
219 #if IS_ENABLED(CONFIG_FEC)
220                 /* Freescale Fast Ethernet Controller */
221                 { "fsl,imx25-fec",   "phy-reset-gpios", "phy-reset-active-high" },
222                 { "fsl,imx27-fec",   "phy-reset-gpios", "phy-reset-active-high" },
223                 { "fsl,imx28-fec",   "phy-reset-gpios", "phy-reset-active-high" },
224                 { "fsl,imx6q-fec",   "phy-reset-gpios", "phy-reset-active-high" },
225                 { "fsl,mvf600-fec",  "phy-reset-gpios", "phy-reset-active-high" },
226                 { "fsl,imx6sx-fec",  "phy-reset-gpios", "phy-reset-active-high" },
227                 { "fsl,imx6ul-fec",  "phy-reset-gpios", "phy-reset-active-high" },
228                 { "fsl,imx8mq-fec",  "phy-reset-gpios", "phy-reset-active-high" },
229                 { "fsl,imx8qm-fec",  "phy-reset-gpios", "phy-reset-active-high" },
230                 { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" },
231 #endif
232 #if IS_ENABLED(CONFIG_PCI_IMX6)
233                 { "fsl,imx6q-pcie",  "reset-gpio", "reset-gpio-active-high" },
234                 { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" },
235                 { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" },
236                 { "fsl,imx7d-pcie",  "reset-gpio", "reset-gpio-active-high" },
237                 { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" },
238                 { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
239                 { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
240 #endif
241
242                 /*
243                  * The regulator GPIO handles are specified such that the
244                  * presence or absence of "enable-active-high" solely controls
245                  * the polarity of the GPIO line. Any phandle flags must
246                  * be actively ignored.
247                  */
248 #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE)
249                 { "regulator-fixed",   "gpios",        "enable-active-high" },
250                 { "regulator-fixed",   "gpio",         "enable-active-high" },
251                 { "reg-fixed-voltage", "gpios",        "enable-active-high" },
252                 { "reg-fixed-voltage", "gpio",         "enable-active-high" },
253 #endif
254 #if IS_ENABLED(CONFIG_REGULATOR_GPIO)
255                 { "regulator-gpio",    "enable-gpio",  "enable-active-high" },
256                 { "regulator-gpio",    "enable-gpios", "enable-active-high" },
257 #endif
258 #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
259                 { "atmel,hsmci",       "cd-gpios",     "cd-inverted" },
260 #endif
261         };
262         unsigned int i;
263         bool active_high;
264
265 #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
266         /*
267          * The Atmel HSMCI has compatible property in the parent node and
268          * gpio property in a child node
269          */
270         if (of_device_is_compatible(np->parent, "atmel,hsmci")) {
271                 np_compat = np->parent;
272                 np_propname = np;
273         }
274 #endif
275
276         for (i = 0; i < ARRAY_SIZE(gpios); i++) {
277                 if (of_device_is_compatible(np_compat, gpios[i].compatible) &&
278                     !strcmp(propname, gpios[i].gpio_propname)) {
279                         active_high = of_property_read_bool(np_propname,
280                                                 gpios[i].polarity_propname);
281                         of_gpio_quirk_polarity(np, active_high, flags);
282                         break;
283                 }
284         }
285 }
286
287 static void of_gpio_flags_quirks(const struct device_node *np,
288                                  const char *propname,
289                                  enum of_gpio_flags *flags,
290                                  int index)
291 {
292         of_gpio_try_fixup_polarity(np, propname, flags);
293         of_gpio_set_polarity_by_property(np, propname, flags);
294
295         /*
296          * Legacy open drain handling for fixed voltage regulators.
297          */
298         if (IS_ENABLED(CONFIG_REGULATOR) &&
299             of_device_is_compatible(np, "reg-fixed-voltage") &&
300             of_property_read_bool(np, "gpio-open-drain")) {
301                 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
302                 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
303                         of_node_full_name(np));
304         }
305
306         /*
307          * Legacy handling of SPI active high chip select. If we have a
308          * property named "cs-gpios" we need to inspect the child node
309          * to determine if the flags should have inverted semantics.
310          */
311         if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
312             of_property_read_bool(np, "cs-gpios")) {
313                 struct device_node *child;
314                 u32 cs;
315                 int ret;
316
317                 for_each_child_of_node(np, child) {
318                         ret = of_property_read_u32(child, "reg", &cs);
319                         if (ret)
320                                 continue;
321                         if (cs == index) {
322                                 /*
323                                  * SPI children have active low chip selects
324                                  * by default. This can be specified negatively
325                                  * by just omitting "spi-cs-high" in the
326                                  * device node, or actively by tagging on
327                                  * GPIO_ACTIVE_LOW as flag in the device
328                                  * tree. If the line is simultaneously
329                                  * tagged as active low in the device tree
330                                  * and has the "spi-cs-high" set, we get a
331                                  * conflict and the "spi-cs-high" flag will
332                                  * take precedence.
333                                  */
334                                 bool active_high = of_property_read_bool(child,
335                                                                 "spi-cs-high");
336                                 of_gpio_quirk_polarity(child, active_high,
337                                                        flags);
338                                 of_node_put(child);
339                                 break;
340                         }
341                 }
342         }
343
344         /* Legacy handling of stmmac's active-low PHY reset line */
345         if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
346             !strcmp(propname, "snps,reset-gpio") &&
347             of_property_read_bool(np, "snps,reset-active-low"))
348                 *flags |= OF_GPIO_ACTIVE_LOW;
349 }
350
351 /**
352  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
353  * @np:         device node to get GPIO from
354  * @propname:   property name containing gpio specifier(s)
355  * @index:      index of the GPIO
356  * @flags:      a flags pointer to fill in
357  *
358  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
359  * value on the error condition. If @flags is not NULL the function also fills
360  * in flags for the GPIO.
361  */
362 static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
363                      const char *propname, int index, enum of_gpio_flags *flags)
364 {
365         struct of_phandle_args gpiospec;
366         struct gpio_chip *chip;
367         struct gpio_desc *desc;
368         int ret;
369
370         ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
371                                              &gpiospec);
372         if (ret) {
373                 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
374                         __func__, propname, np, index);
375                 return ERR_PTR(ret);
376         }
377
378         chip = of_find_gpiochip_by_xlate(&gpiospec);
379         if (!chip) {
380                 desc = ERR_PTR(-EPROBE_DEFER);
381                 goto out;
382         }
383
384         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
385         if (IS_ERR(desc))
386                 goto out;
387
388         if (flags)
389                 of_gpio_flags_quirks(np, propname, flags, index);
390
391         pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
392                  __func__, propname, np, index,
393                  PTR_ERR_OR_ZERO(desc));
394
395 out:
396         of_node_put(gpiospec.np);
397
398         return desc;
399 }
400
401 /**
402  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
403  * @np:         device node to get GPIO from
404  * @propname:   Name of property containing gpio specifier(s)
405  * @index:      index of the GPIO
406  *
407  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
408  * value on the error condition.
409  */
410 int of_get_named_gpio(const struct device_node *np, const char *propname,
411                       int index)
412 {
413         struct gpio_desc *desc;
414
415         desc = of_get_named_gpiod_flags(np, propname, index, NULL);
416
417         if (IS_ERR(desc))
418                 return PTR_ERR(desc);
419         else
420                 return desc_to_gpio(desc);
421 }
422 EXPORT_SYMBOL_GPL(of_get_named_gpio);
423
424 /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
425 static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
426 {
427         unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
428
429         if (flags & OF_GPIO_ACTIVE_LOW)
430                 lflags |= GPIO_ACTIVE_LOW;
431
432         if (flags & OF_GPIO_SINGLE_ENDED) {
433                 if (flags & OF_GPIO_OPEN_DRAIN)
434                         lflags |= GPIO_OPEN_DRAIN;
435                 else
436                         lflags |= GPIO_OPEN_SOURCE;
437         }
438
439         if (flags & OF_GPIO_TRANSITORY)
440                 lflags |= GPIO_TRANSITORY;
441
442         if (flags & OF_GPIO_PULL_UP)
443                 lflags |= GPIO_PULL_UP;
444
445         if (flags & OF_GPIO_PULL_DOWN)
446                 lflags |= GPIO_PULL_DOWN;
447
448         if (flags & OF_GPIO_PULL_DISABLE)
449                 lflags |= GPIO_PULL_DISABLE;
450
451         return lflags;
452 }
453
454 static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
455                                              const char *con_id,
456                                              unsigned int idx,
457                                              enum of_gpio_flags *of_flags)
458 {
459         static const struct of_rename_gpio {
460                 const char *con_id;
461                 const char *legacy_id;  /* NULL - same as con_id */
462                 /*
463                  * Compatible string can be set to NULL in case where
464                  * matching to a particular compatible is not practical,
465                  * but it should only be done for gpio names that have
466                  * vendor prefix to reduce risk of false positives.
467                  * Addition of such entries is strongly discouraged.
468                  */
469                 const char *compatible;
470         } gpios[] = {
471 #if !IS_ENABLED(CONFIG_LCD_HX8357)
472                 /* Himax LCD controllers used "gpios-reset" */
473                 { "reset",      "gpios-reset",  "himax,hx8357" },
474                 { "reset",      "gpios-reset",  "himax,hx8369" },
475 #endif
476 #if IS_ENABLED(CONFIG_MFD_ARIZONA)
477                 { "wlf,reset",  NULL,           NULL },
478 #endif
479 #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
480                 { "rtc-data",   "gpio-rtc-data",        "moxa,moxart-rtc" },
481                 { "rtc-sclk",   "gpio-rtc-sclk",        "moxa,moxart-rtc" },
482                 { "rtc-reset",  "gpio-rtc-reset",       "moxa,moxart-rtc" },
483 #endif
484 #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
485                 { "reset",      "reset-n-io",   "marvell,nfc-i2c" },
486 #endif
487 #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
488                 { "reset",      "reset-n-io",   "marvell,nfc-spi" },
489 #endif
490 #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
491                 { "reset",      "reset-n-io",   "marvell,nfc-uart" },
492                 { "reset",      "reset-n-io",   "mrvl,nfc-uart" },
493 #endif
494 #if !IS_ENABLED(CONFIG_PCI_LANTIQ)
495                 /* MIPS Lantiq PCI */
496                 { "reset",      "gpios-reset",  "lantiq,pci-xway" },
497 #endif
498
499                 /*
500                  * Some regulator bindings happened before we managed to
501                  * establish that GPIO properties should be named
502                  * "foo-gpios" so we have this special kludge for them.
503                  */
504 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
505                 { "wlf,ldoena",  NULL,          NULL }, /* Arizona */
506 #endif
507 #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
508                 { "wlf,ldo1ena", NULL,          NULL }, /* WM8994 */
509                 { "wlf,ldo2ena", NULL,          NULL }, /* WM8994 */
510 #endif
511
512 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
513                 { "reset",      "cirrus,gpio-nreset",   "cirrus,cs42l56" },
514 #endif
515 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
516                 { "reset",      "gpio-reset",   "ti,tlv320aic3x" },
517                 { "reset",      "gpio-reset",   "ti,tlv320aic33" },
518                 { "reset",      "gpio-reset",   "ti,tlv320aic3007" },
519                 { "reset",      "gpio-reset",   "ti,tlv320aic3104" },
520                 { "reset",      "gpio-reset",   "ti,tlv320aic3106" },
521 #endif
522 #if IS_ENABLED(CONFIG_SPI_GPIO)
523                 /*
524                  * The SPI GPIO bindings happened before we managed to
525                  * establish that GPIO properties should be named
526                  * "foo-gpios" so we have this special kludge for them.
527                  */
528                 { "miso",       "gpio-miso",    "spi-gpio" },
529                 { "mosi",       "gpio-mosi",    "spi-gpio" },
530                 { "sck",        "gpio-sck",     "spi-gpio" },
531 #endif
532
533                 /*
534                  * The old Freescale bindings use simply "gpios" as name
535                  * for the chip select lines rather than "cs-gpios" like
536                  * all other SPI hardware. Allow this specifically for
537                  * Freescale and PPC devices.
538                  */
539 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
540                 { "cs",         "gpios",        "fsl,spi" },
541                 { "cs",         "gpios",        "aeroflexgaisler,spictrl" },
542 #endif
543 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
544                 { "cs",         "gpios",        "ibm,ppc4xx-spi" },
545 #endif
546
547 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
548                 /*
549                  * Fairchild FUSB302 host is using undocumented "fcs,int_n"
550                  * property without the compulsory "-gpios" suffix.
551                  */
552                 { "fcs,int_n",  NULL,           "fcs,fusb302" },
553 #endif
554         };
555         struct gpio_desc *desc;
556         const char *legacy_id;
557         unsigned int i;
558
559         if (!con_id)
560                 return ERR_PTR(-ENOENT);
561
562         for (i = 0; i < ARRAY_SIZE(gpios); i++) {
563                 if (strcmp(con_id, gpios[i].con_id))
564                         continue;
565
566                 if (gpios[i].compatible &&
567                     !of_device_is_compatible(np, gpios[i].compatible))
568                         continue;
569
570                 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
571                 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
572                 if (!gpiod_not_found(desc)) {
573                         pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
574                                 of_node_full_name(np), legacy_id, con_id);
575                         return desc;
576                 }
577         }
578
579         return ERR_PTR(-ENOENT);
580 }
581
582 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
583                                              const char *con_id,
584                                              unsigned int idx,
585                                              enum of_gpio_flags *of_flags)
586 {
587         struct gpio_desc *desc;
588         const char *legacy_id;
589
590         if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
591                 return ERR_PTR(-ENOENT);
592
593         if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
594                 return ERR_PTR(-ENOENT);
595
596         if (!con_id || strcmp(con_id, "i2s1-in-sel"))
597                 return ERR_PTR(-ENOENT);
598
599         if (idx == 0)
600                 legacy_id = "i2s1-in-sel-gpio1";
601         else if (idx == 1)
602                 legacy_id = "i2s1-in-sel-gpio2";
603         else
604                 return ERR_PTR(-ENOENT);
605
606         desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
607         if (!gpiod_not_found(desc))
608                 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
609                         of_node_full_name(np), legacy_id, con_id);
610
611         return desc;
612 }
613
614 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
615                                                 const char *con_id,
616                                                 unsigned int idx,
617                                                 enum of_gpio_flags *of_flags);
618 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
619         of_find_gpio_rename,
620         of_find_mt2701_gpio,
621         NULL
622 };
623
624 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
625                                unsigned int idx, unsigned long *flags)
626 {
627         char prop_name[32]; /* 32 is max size of property name */
628         enum of_gpio_flags of_flags;
629         const of_find_gpio_quirk *q;
630         struct gpio_desc *desc;
631         unsigned int i;
632
633         /* Try GPIO property "foo-gpios" and "foo-gpio" */
634         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
635                 if (con_id)
636                         snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
637                                  gpio_suffixes[i]);
638                 else
639                         snprintf(prop_name, sizeof(prop_name), "%s",
640                                  gpio_suffixes[i]);
641
642                 desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
643
644                 if (!gpiod_not_found(desc))
645                         break;
646         }
647
648         /* Properly named GPIO was not found, try workarounds */
649         for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
650                 desc = (*q)(np, con_id, idx, &of_flags);
651
652         if (IS_ERR(desc))
653                 return desc;
654
655         *flags = of_convert_gpio_flags(of_flags);
656
657         return desc;
658 }
659
660 /**
661  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
662  * @np:         device node to get GPIO from
663  * @chip:       GPIO chip whose hog is parsed
664  * @idx:        Index of the GPIO to parse
665  * @name:       GPIO line name
666  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
667  *              of_find_gpio() or of_parse_own_gpio()
668  * @dflags:     gpiod_flags - optional GPIO initialization flags
669  *
670  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
671  * value on the error condition.
672  */
673 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
674                                            struct gpio_chip *chip,
675                                            unsigned int idx, const char **name,
676                                            unsigned long *lflags,
677                                            enum gpiod_flags *dflags)
678 {
679         struct device_node *chip_np;
680         enum of_gpio_flags xlate_flags;
681         struct of_phandle_args gpiospec;
682         struct gpio_desc *desc;
683         unsigned int i;
684         u32 tmp;
685         int ret;
686
687         chip_np = dev_of_node(&chip->gpiodev->dev);
688         if (!chip_np)
689                 return ERR_PTR(-EINVAL);
690
691         xlate_flags = 0;
692         *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
693         *dflags = GPIOD_ASIS;
694
695         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
696         if (ret)
697                 return ERR_PTR(ret);
698
699         gpiospec.np = chip_np;
700         gpiospec.args_count = tmp;
701
702         for (i = 0; i < tmp; i++) {
703                 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
704                                                  &gpiospec.args[i]);
705                 if (ret)
706                         return ERR_PTR(ret);
707         }
708
709         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
710         if (IS_ERR(desc))
711                 return desc;
712
713         *lflags = of_convert_gpio_flags(xlate_flags);
714
715         if (of_property_read_bool(np, "input"))
716                 *dflags |= GPIOD_IN;
717         else if (of_property_read_bool(np, "output-low"))
718                 *dflags |= GPIOD_OUT_LOW;
719         else if (of_property_read_bool(np, "output-high"))
720                 *dflags |= GPIOD_OUT_HIGH;
721         else {
722                 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
723                         desc_to_gpio(desc), np);
724                 return ERR_PTR(-EINVAL);
725         }
726
727         if (name && of_property_read_string(np, "line-name", name))
728                 *name = np->name;
729
730         return desc;
731 }
732
733 /**
734  * of_gpiochip_add_hog - Add all hogs in a hog device node
735  * @chip:       gpio chip to act on
736  * @hog:        device node describing the hogs
737  *
738  * Returns error if it fails otherwise 0 on success.
739  */
740 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
741 {
742         enum gpiod_flags dflags;
743         struct gpio_desc *desc;
744         unsigned long lflags;
745         const char *name;
746         unsigned int i;
747         int ret;
748
749         for (i = 0;; i++) {
750                 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
751                 if (IS_ERR(desc))
752                         break;
753
754                 ret = gpiod_hog(desc, name, lflags, dflags);
755                 if (ret < 0)
756                         return ret;
757
758 #ifdef CONFIG_OF_DYNAMIC
759                 desc->hog = hog;
760 #endif
761         }
762
763         return 0;
764 }
765
766 /**
767  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
768  * @chip:       gpio chip to act on
769  *
770  * This is only used by of_gpiochip_add to request/set GPIO initial
771  * configuration.
772  * It returns error if it fails otherwise 0 on success.
773  */
774 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
775 {
776         struct device_node *np;
777         int ret;
778
779         for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
780                 if (!of_property_read_bool(np, "gpio-hog"))
781                         continue;
782
783                 ret = of_gpiochip_add_hog(chip, np);
784                 if (ret < 0) {
785                         of_node_put(np);
786                         return ret;
787                 }
788
789                 of_node_set_flag(np, OF_POPULATED);
790         }
791
792         return 0;
793 }
794
795 #ifdef CONFIG_OF_DYNAMIC
796 /**
797  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
798  * @chip:       gpio chip to act on
799  * @hog:        device node describing the hogs
800  */
801 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
802                                    struct device_node *hog)
803 {
804         struct gpio_desc *desc;
805
806         for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
807                 if (desc->hog == hog)
808                         gpiochip_free_own_desc(desc);
809 }
810
811 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
812 {
813         return device_match_of_node(&chip->gpiodev->dev, data);
814 }
815
816 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
817 {
818         return gpiochip_find(np, of_gpiochip_match_node);
819 }
820
821 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
822                           void *arg)
823 {
824         struct of_reconfig_data *rd = arg;
825         struct gpio_chip *chip;
826         int ret;
827
828         /*
829          * This only supports adding and removing complete gpio-hog nodes.
830          * Modifying an existing gpio-hog node is not supported (except for
831          * changing its "status" property, which is treated the same as
832          * addition/removal).
833          */
834         switch (of_reconfig_get_state_change(action, arg)) {
835         case OF_RECONFIG_CHANGE_ADD:
836                 if (!of_property_read_bool(rd->dn, "gpio-hog"))
837                         return NOTIFY_OK;       /* not for us */
838
839                 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
840                         return NOTIFY_OK;
841
842                 chip = of_find_gpiochip_by_node(rd->dn->parent);
843                 if (chip == NULL)
844                         return NOTIFY_OK;       /* not for us */
845
846                 ret = of_gpiochip_add_hog(chip, rd->dn);
847                 if (ret < 0) {
848                         pr_err("%s: failed to add hogs for %pOF\n", __func__,
849                                rd->dn);
850                         of_node_clear_flag(rd->dn, OF_POPULATED);
851                         return notifier_from_errno(ret);
852                 }
853                 break;
854
855         case OF_RECONFIG_CHANGE_REMOVE:
856                 if (!of_node_check_flag(rd->dn, OF_POPULATED))
857                         return NOTIFY_OK;       /* already depopulated */
858
859                 chip = of_find_gpiochip_by_node(rd->dn->parent);
860                 if (chip == NULL)
861                         return NOTIFY_OK;       /* not for us */
862
863                 of_gpiochip_remove_hog(chip, rd->dn);
864                 of_node_clear_flag(rd->dn, OF_POPULATED);
865                 break;
866         }
867
868         return NOTIFY_OK;
869 }
870
871 struct notifier_block gpio_of_notifier = {
872         .notifier_call = of_gpio_notify,
873 };
874 #endif /* CONFIG_OF_DYNAMIC */
875
876 /**
877  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
878  * @gc:         pointer to the gpio_chip structure
879  * @gpiospec:   GPIO specifier as found in the device tree
880  * @flags:      a flags pointer to fill in
881  *
882  * This is simple translation function, suitable for the most 1:1 mapped
883  * GPIO chips. This function performs only one sanity check: whether GPIO
884  * is less than ngpios (that is specified in the gpio_chip).
885  */
886 static int of_gpio_simple_xlate(struct gpio_chip *gc,
887                                 const struct of_phandle_args *gpiospec,
888                                 u32 *flags)
889 {
890         /*
891          * We're discouraging gpio_cells < 2, since that way you'll have to
892          * write your own xlate function (that will have to retrieve the GPIO
893          * number and the flags from a single gpio cell -- this is possible,
894          * but not recommended).
895          */
896         if (gc->of_gpio_n_cells < 2) {
897                 WARN_ON(1);
898                 return -EINVAL;
899         }
900
901         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
902                 return -EINVAL;
903
904         if (gpiospec->args[0] >= gc->ngpio)
905                 return -EINVAL;
906
907         if (flags)
908                 *flags = gpiospec->args[1];
909
910         return gpiospec->args[0];
911 }
912
913 #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
914 #include <linux/gpio/legacy-of-mm-gpiochip.h>
915 /**
916  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
917  * @np:         device node of the GPIO chip
918  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
919  * @data:       driver data to store in the struct gpio_chip
920  *
921  * To use this function you should allocate and fill mm_gc with:
922  *
923  * 1) In the gpio_chip structure:
924  *    - all the callbacks
925  *    - of_gpio_n_cells
926  *    - of_xlate callback (optional)
927  *
928  * 3) In the of_mm_gpio_chip structure:
929  *    - save_regs callback (optional)
930  *
931  * If succeeded, this function will map bank's memory and will
932  * do all necessary work for you. Then you'll able to use .regs
933  * to manage GPIOs from the callbacks.
934  */
935 int of_mm_gpiochip_add_data(struct device_node *np,
936                             struct of_mm_gpio_chip *mm_gc,
937                             void *data)
938 {
939         int ret = -ENOMEM;
940         struct gpio_chip *gc = &mm_gc->gc;
941
942         gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
943         if (!gc->label)
944                 goto err0;
945
946         mm_gc->regs = of_iomap(np, 0);
947         if (!mm_gc->regs)
948                 goto err1;
949
950         gc->base = -1;
951
952         if (mm_gc->save_regs)
953                 mm_gc->save_regs(mm_gc);
954
955         fwnode_handle_put(mm_gc->gc.fwnode);
956         mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
957
958         ret = gpiochip_add_data(gc, data);
959         if (ret)
960                 goto err2;
961
962         return 0;
963 err2:
964         of_node_put(np);
965         iounmap(mm_gc->regs);
966 err1:
967         kfree(gc->label);
968 err0:
969         pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
970         return ret;
971 }
972 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
973
974 /**
975  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
976  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
977  */
978 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
979 {
980         struct gpio_chip *gc = &mm_gc->gc;
981
982         gpiochip_remove(gc);
983         iounmap(mm_gc->regs);
984         kfree(gc->label);
985 }
986 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
987 #endif
988
989 #ifdef CONFIG_PINCTRL
990 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
991 {
992         struct of_phandle_args pinspec;
993         struct pinctrl_dev *pctldev;
994         struct device_node *np;
995         int index = 0, ret;
996         const char *name;
997         static const char group_names_propname[] = "gpio-ranges-group-names";
998         struct property *group_names;
999
1000         np = dev_of_node(&chip->gpiodev->dev);
1001         if (!np)
1002                 return 0;
1003
1004         group_names = of_find_property(np, group_names_propname, NULL);
1005
1006         for (;; index++) {
1007                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1008                                 index, &pinspec);
1009                 if (ret)
1010                         break;
1011
1012                 pctldev = of_pinctrl_get(pinspec.np);
1013                 of_node_put(pinspec.np);
1014                 if (!pctldev)
1015                         return -EPROBE_DEFER;
1016
1017                 if (pinspec.args[2]) {
1018                         if (group_names) {
1019                                 of_property_read_string_index(np,
1020                                                 group_names_propname,
1021                                                 index, &name);
1022                                 if (strlen(name)) {
1023                                         pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1024                                                 np);
1025                                         break;
1026                                 }
1027                         }
1028                         /* npins != 0: linear range */
1029                         ret = gpiochip_add_pin_range(chip,
1030                                         pinctrl_dev_get_devname(pctldev),
1031                                         pinspec.args[0],
1032                                         pinspec.args[1],
1033                                         pinspec.args[2]);
1034                         if (ret)
1035                                 return ret;
1036                 } else {
1037                         /* npins == 0: special range */
1038                         if (pinspec.args[1]) {
1039                                 pr_err("%pOF: Illegal gpio-range format.\n",
1040                                         np);
1041                                 break;
1042                         }
1043
1044                         if (!group_names) {
1045                                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1046                                         np, group_names_propname);
1047                                 break;
1048                         }
1049
1050                         ret = of_property_read_string_index(np,
1051                                                 group_names_propname,
1052                                                 index, &name);
1053                         if (ret)
1054                                 break;
1055
1056                         if (!strlen(name)) {
1057                                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1058                                 np);
1059                                 break;
1060                         }
1061
1062                         ret = gpiochip_add_pingroup_range(chip, pctldev,
1063                                                 pinspec.args[0], name);
1064                         if (ret)
1065                                 return ret;
1066                 }
1067         }
1068
1069         return 0;
1070 }
1071
1072 #else
1073 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1074 #endif
1075
1076 int of_gpiochip_add(struct gpio_chip *chip)
1077 {
1078         struct device_node *np;
1079         int ret;
1080
1081         np = dev_of_node(&chip->gpiodev->dev);
1082         if (!np)
1083                 return 0;
1084
1085         if (!chip->of_xlate) {
1086                 chip->of_gpio_n_cells = 2;
1087                 chip->of_xlate = of_gpio_simple_xlate;
1088         }
1089
1090         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1091                 return -EINVAL;
1092
1093         ret = of_gpiochip_add_pin_range(chip);
1094         if (ret)
1095                 return ret;
1096
1097         of_node_get(np);
1098
1099         ret = of_gpiochip_scan_gpios(chip);
1100         if (ret)
1101                 of_node_put(np);
1102
1103         return ret;
1104 }
1105
1106 void of_gpiochip_remove(struct gpio_chip *chip)
1107 {
1108         of_node_put(dev_of_node(&chip->gpiodev->dev));
1109 }