nvme: catch errors from nvme_configure_metadata()
[platform/kernel/linux-starfive.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_MT2701_CS42448)
516                 { "i2s1-in-sel-gpio1",  NULL,   "mediatek,mt2701-cs42448-machine" },
517                 { "i2s1-in-sel-gpio2",  NULL,   "mediatek,mt2701-cs42448-machine" },
518 #endif
519 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
520                 { "reset",      "gpio-reset",   "ti,tlv320aic3x" },
521                 { "reset",      "gpio-reset",   "ti,tlv320aic33" },
522                 { "reset",      "gpio-reset",   "ti,tlv320aic3007" },
523                 { "reset",      "gpio-reset",   "ti,tlv320aic3104" },
524                 { "reset",      "gpio-reset",   "ti,tlv320aic3106" },
525 #endif
526 #if IS_ENABLED(CONFIG_SPI_GPIO)
527                 /*
528                  * The SPI GPIO bindings happened before we managed to
529                  * establish that GPIO properties should be named
530                  * "foo-gpios" so we have this special kludge for them.
531                  */
532                 { "miso",       "gpio-miso",    "spi-gpio" },
533                 { "mosi",       "gpio-mosi",    "spi-gpio" },
534                 { "sck",        "gpio-sck",     "spi-gpio" },
535 #endif
536
537                 /*
538                  * The old Freescale bindings use simply "gpios" as name
539                  * for the chip select lines rather than "cs-gpios" like
540                  * all other SPI hardware. Allow this specifically for
541                  * Freescale and PPC devices.
542                  */
543 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
544                 { "cs",         "gpios",        "fsl,spi" },
545                 { "cs",         "gpios",        "aeroflexgaisler,spictrl" },
546 #endif
547 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
548                 { "cs",         "gpios",        "ibm,ppc4xx-spi" },
549 #endif
550
551 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
552                 /*
553                  * Fairchild FUSB302 host is using undocumented "fcs,int_n"
554                  * property without the compulsory "-gpios" suffix.
555                  */
556                 { "fcs,int_n",  NULL,           "fcs,fusb302" },
557 #endif
558         };
559         struct gpio_desc *desc;
560         const char *legacy_id;
561         unsigned int i;
562
563         if (!con_id)
564                 return ERR_PTR(-ENOENT);
565
566         for (i = 0; i < ARRAY_SIZE(gpios); i++) {
567                 if (strcmp(con_id, gpios[i].con_id))
568                         continue;
569
570                 if (gpios[i].compatible &&
571                     !of_device_is_compatible(np, gpios[i].compatible))
572                         continue;
573
574                 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
575                 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
576                 if (!gpiod_not_found(desc)) {
577                         pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
578                                 of_node_full_name(np), legacy_id, con_id);
579                         return desc;
580                 }
581         }
582
583         return ERR_PTR(-ENOENT);
584 }
585
586 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
587                                              const char *con_id,
588                                              unsigned int idx,
589                                              enum of_gpio_flags *of_flags)
590 {
591         struct gpio_desc *desc;
592         const char *legacy_id;
593
594         if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
595                 return ERR_PTR(-ENOENT);
596
597         if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
598                 return ERR_PTR(-ENOENT);
599
600         if (!con_id || strcmp(con_id, "i2s1-in-sel"))
601                 return ERR_PTR(-ENOENT);
602
603         if (idx == 0)
604                 legacy_id = "i2s1-in-sel-gpio1";
605         else if (idx == 1)
606                 legacy_id = "i2s1-in-sel-gpio2";
607         else
608                 return ERR_PTR(-ENOENT);
609
610         desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
611         if (!gpiod_not_found(desc))
612                 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
613                         of_node_full_name(np), legacy_id, con_id);
614
615         return desc;
616 }
617
618 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
619                                                 const char *con_id,
620                                                 unsigned int idx,
621                                                 enum of_gpio_flags *of_flags);
622 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
623         of_find_gpio_rename,
624         of_find_mt2701_gpio,
625         NULL
626 };
627
628 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
629                                unsigned int idx, unsigned long *flags)
630 {
631         char prop_name[32]; /* 32 is max size of property name */
632         enum of_gpio_flags of_flags;
633         const of_find_gpio_quirk *q;
634         struct gpio_desc *desc;
635         unsigned int i;
636
637         /* Try GPIO property "foo-gpios" and "foo-gpio" */
638         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
639                 if (con_id)
640                         snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
641                                  gpio_suffixes[i]);
642                 else
643                         snprintf(prop_name, sizeof(prop_name), "%s",
644                                  gpio_suffixes[i]);
645
646                 desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
647
648                 if (!gpiod_not_found(desc))
649                         break;
650         }
651
652         /* Properly named GPIO was not found, try workarounds */
653         for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
654                 desc = (*q)(np, con_id, idx, &of_flags);
655
656         if (IS_ERR(desc))
657                 return desc;
658
659         *flags = of_convert_gpio_flags(of_flags);
660
661         return desc;
662 }
663
664 /**
665  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
666  * @np:         device node to get GPIO from
667  * @chip:       GPIO chip whose hog is parsed
668  * @idx:        Index of the GPIO to parse
669  * @name:       GPIO line name
670  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
671  *              of_find_gpio() or of_parse_own_gpio()
672  * @dflags:     gpiod_flags - optional GPIO initialization flags
673  *
674  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
675  * value on the error condition.
676  */
677 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
678                                            struct gpio_chip *chip,
679                                            unsigned int idx, const char **name,
680                                            unsigned long *lflags,
681                                            enum gpiod_flags *dflags)
682 {
683         struct device_node *chip_np;
684         enum of_gpio_flags xlate_flags;
685         struct of_phandle_args gpiospec;
686         struct gpio_desc *desc;
687         unsigned int i;
688         u32 tmp;
689         int ret;
690
691         chip_np = dev_of_node(&chip->gpiodev->dev);
692         if (!chip_np)
693                 return ERR_PTR(-EINVAL);
694
695         xlate_flags = 0;
696         *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
697         *dflags = GPIOD_ASIS;
698
699         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
700         if (ret)
701                 return ERR_PTR(ret);
702
703         gpiospec.np = chip_np;
704         gpiospec.args_count = tmp;
705
706         for (i = 0; i < tmp; i++) {
707                 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
708                                                  &gpiospec.args[i]);
709                 if (ret)
710                         return ERR_PTR(ret);
711         }
712
713         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
714         if (IS_ERR(desc))
715                 return desc;
716
717         *lflags = of_convert_gpio_flags(xlate_flags);
718
719         if (of_property_read_bool(np, "input"))
720                 *dflags |= GPIOD_IN;
721         else if (of_property_read_bool(np, "output-low"))
722                 *dflags |= GPIOD_OUT_LOW;
723         else if (of_property_read_bool(np, "output-high"))
724                 *dflags |= GPIOD_OUT_HIGH;
725         else {
726                 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
727                         desc_to_gpio(desc), np);
728                 return ERR_PTR(-EINVAL);
729         }
730
731         if (name && of_property_read_string(np, "line-name", name))
732                 *name = np->name;
733
734         return desc;
735 }
736
737 /**
738  * of_gpiochip_add_hog - Add all hogs in a hog device node
739  * @chip:       gpio chip to act on
740  * @hog:        device node describing the hogs
741  *
742  * Returns error if it fails otherwise 0 on success.
743  */
744 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
745 {
746         enum gpiod_flags dflags;
747         struct gpio_desc *desc;
748         unsigned long lflags;
749         const char *name;
750         unsigned int i;
751         int ret;
752
753         for (i = 0;; i++) {
754                 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
755                 if (IS_ERR(desc))
756                         break;
757
758                 ret = gpiod_hog(desc, name, lflags, dflags);
759                 if (ret < 0)
760                         return ret;
761
762 #ifdef CONFIG_OF_DYNAMIC
763                 desc->hog = hog;
764 #endif
765         }
766
767         return 0;
768 }
769
770 /**
771  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
772  * @chip:       gpio chip to act on
773  *
774  * This is only used by of_gpiochip_add to request/set GPIO initial
775  * configuration.
776  * It returns error if it fails otherwise 0 on success.
777  */
778 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
779 {
780         struct device_node *np;
781         int ret;
782
783         for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
784                 if (!of_property_read_bool(np, "gpio-hog"))
785                         continue;
786
787                 ret = of_gpiochip_add_hog(chip, np);
788                 if (ret < 0) {
789                         of_node_put(np);
790                         return ret;
791                 }
792
793                 of_node_set_flag(np, OF_POPULATED);
794         }
795
796         return 0;
797 }
798
799 #ifdef CONFIG_OF_DYNAMIC
800 /**
801  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
802  * @chip:       gpio chip to act on
803  * @hog:        device node describing the hogs
804  */
805 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
806                                    struct device_node *hog)
807 {
808         struct gpio_desc *desc;
809
810         for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
811                 if (desc->hog == hog)
812                         gpiochip_free_own_desc(desc);
813 }
814
815 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
816 {
817         return device_match_of_node(&chip->gpiodev->dev, data);
818 }
819
820 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
821 {
822         return gpiochip_find(np, of_gpiochip_match_node);
823 }
824
825 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
826                           void *arg)
827 {
828         struct of_reconfig_data *rd = arg;
829         struct gpio_chip *chip;
830         int ret;
831
832         /*
833          * This only supports adding and removing complete gpio-hog nodes.
834          * Modifying an existing gpio-hog node is not supported (except for
835          * changing its "status" property, which is treated the same as
836          * addition/removal).
837          */
838         switch (of_reconfig_get_state_change(action, arg)) {
839         case OF_RECONFIG_CHANGE_ADD:
840                 if (!of_property_read_bool(rd->dn, "gpio-hog"))
841                         return NOTIFY_OK;       /* not for us */
842
843                 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
844                         return NOTIFY_OK;
845
846                 chip = of_find_gpiochip_by_node(rd->dn->parent);
847                 if (chip == NULL)
848                         return NOTIFY_OK;       /* not for us */
849
850                 ret = of_gpiochip_add_hog(chip, rd->dn);
851                 if (ret < 0) {
852                         pr_err("%s: failed to add hogs for %pOF\n", __func__,
853                                rd->dn);
854                         of_node_clear_flag(rd->dn, OF_POPULATED);
855                         return notifier_from_errno(ret);
856                 }
857                 break;
858
859         case OF_RECONFIG_CHANGE_REMOVE:
860                 if (!of_node_check_flag(rd->dn, OF_POPULATED))
861                         return NOTIFY_OK;       /* already depopulated */
862
863                 chip = of_find_gpiochip_by_node(rd->dn->parent);
864                 if (chip == NULL)
865                         return NOTIFY_OK;       /* not for us */
866
867                 of_gpiochip_remove_hog(chip, rd->dn);
868                 of_node_clear_flag(rd->dn, OF_POPULATED);
869                 break;
870         }
871
872         return NOTIFY_OK;
873 }
874
875 struct notifier_block gpio_of_notifier = {
876         .notifier_call = of_gpio_notify,
877 };
878 #endif /* CONFIG_OF_DYNAMIC */
879
880 /**
881  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
882  * @gc:         pointer to the gpio_chip structure
883  * @gpiospec:   GPIO specifier as found in the device tree
884  * @flags:      a flags pointer to fill in
885  *
886  * This is simple translation function, suitable for the most 1:1 mapped
887  * GPIO chips. This function performs only one sanity check: whether GPIO
888  * is less than ngpios (that is specified in the gpio_chip).
889  */
890 static int of_gpio_simple_xlate(struct gpio_chip *gc,
891                                 const struct of_phandle_args *gpiospec,
892                                 u32 *flags)
893 {
894         /*
895          * We're discouraging gpio_cells < 2, since that way you'll have to
896          * write your own xlate function (that will have to retrieve the GPIO
897          * number and the flags from a single gpio cell -- this is possible,
898          * but not recommended).
899          */
900         if (gc->of_gpio_n_cells < 2) {
901                 WARN_ON(1);
902                 return -EINVAL;
903         }
904
905         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
906                 return -EINVAL;
907
908         if (gpiospec->args[0] >= gc->ngpio)
909                 return -EINVAL;
910
911         if (flags)
912                 *flags = gpiospec->args[1];
913
914         return gpiospec->args[0];
915 }
916
917 #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
918 #include <linux/gpio/legacy-of-mm-gpiochip.h>
919 /**
920  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
921  * @np:         device node of the GPIO chip
922  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
923  * @data:       driver data to store in the struct gpio_chip
924  *
925  * To use this function you should allocate and fill mm_gc with:
926  *
927  * 1) In the gpio_chip structure:
928  *    - all the callbacks
929  *    - of_gpio_n_cells
930  *    - of_xlate callback (optional)
931  *
932  * 3) In the of_mm_gpio_chip structure:
933  *    - save_regs callback (optional)
934  *
935  * If succeeded, this function will map bank's memory and will
936  * do all necessary work for you. Then you'll able to use .regs
937  * to manage GPIOs from the callbacks.
938  */
939 int of_mm_gpiochip_add_data(struct device_node *np,
940                             struct of_mm_gpio_chip *mm_gc,
941                             void *data)
942 {
943         int ret = -ENOMEM;
944         struct gpio_chip *gc = &mm_gc->gc;
945
946         gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
947         if (!gc->label)
948                 goto err0;
949
950         mm_gc->regs = of_iomap(np, 0);
951         if (!mm_gc->regs)
952                 goto err1;
953
954         gc->base = -1;
955
956         if (mm_gc->save_regs)
957                 mm_gc->save_regs(mm_gc);
958
959         fwnode_handle_put(mm_gc->gc.fwnode);
960         mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
961
962         ret = gpiochip_add_data(gc, data);
963         if (ret)
964                 goto err2;
965
966         return 0;
967 err2:
968         of_node_put(np);
969         iounmap(mm_gc->regs);
970 err1:
971         kfree(gc->label);
972 err0:
973         pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
974         return ret;
975 }
976 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
977
978 /**
979  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
980  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
981  */
982 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
983 {
984         struct gpio_chip *gc = &mm_gc->gc;
985
986         gpiochip_remove(gc);
987         iounmap(mm_gc->regs);
988         kfree(gc->label);
989 }
990 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
991 #endif
992
993 #ifdef CONFIG_PINCTRL
994 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
995 {
996         struct of_phandle_args pinspec;
997         struct pinctrl_dev *pctldev;
998         struct device_node *np;
999         int index = 0, ret;
1000         const char *name;
1001         static const char group_names_propname[] = "gpio-ranges-group-names";
1002         struct property *group_names;
1003
1004         np = dev_of_node(&chip->gpiodev->dev);
1005         if (!np)
1006                 return 0;
1007
1008         group_names = of_find_property(np, group_names_propname, NULL);
1009
1010         for (;; index++) {
1011                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1012                                 index, &pinspec);
1013                 if (ret)
1014                         break;
1015
1016                 pctldev = of_pinctrl_get(pinspec.np);
1017                 of_node_put(pinspec.np);
1018                 if (!pctldev)
1019                         return -EPROBE_DEFER;
1020
1021                 if (pinspec.args[2]) {
1022                         if (group_names) {
1023                                 of_property_read_string_index(np,
1024                                                 group_names_propname,
1025                                                 index, &name);
1026                                 if (strlen(name)) {
1027                                         pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1028                                                 np);
1029                                         break;
1030                                 }
1031                         }
1032                         /* npins != 0: linear range */
1033                         ret = gpiochip_add_pin_range(chip,
1034                                         pinctrl_dev_get_devname(pctldev),
1035                                         pinspec.args[0],
1036                                         pinspec.args[1],
1037                                         pinspec.args[2]);
1038                         if (ret)
1039                                 return ret;
1040                 } else {
1041                         /* npins == 0: special range */
1042                         if (pinspec.args[1]) {
1043                                 pr_err("%pOF: Illegal gpio-range format.\n",
1044                                         np);
1045                                 break;
1046                         }
1047
1048                         if (!group_names) {
1049                                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1050                                         np, group_names_propname);
1051                                 break;
1052                         }
1053
1054                         ret = of_property_read_string_index(np,
1055                                                 group_names_propname,
1056                                                 index, &name);
1057                         if (ret)
1058                                 break;
1059
1060                         if (!strlen(name)) {
1061                                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1062                                 np);
1063                                 break;
1064                         }
1065
1066                         ret = gpiochip_add_pingroup_range(chip, pctldev,
1067                                                 pinspec.args[0], name);
1068                         if (ret)
1069                                 return ret;
1070                 }
1071         }
1072
1073         return 0;
1074 }
1075
1076 #else
1077 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1078 #endif
1079
1080 int of_gpiochip_add(struct gpio_chip *chip)
1081 {
1082         struct device_node *np;
1083         int ret;
1084
1085         np = dev_of_node(&chip->gpiodev->dev);
1086         if (!np)
1087                 return 0;
1088
1089         if (!chip->of_xlate) {
1090                 chip->of_gpio_n_cells = 2;
1091                 chip->of_xlate = of_gpio_simple_xlate;
1092         }
1093
1094         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1095                 return -EINVAL;
1096
1097         ret = of_gpiochip_add_pin_range(chip);
1098         if (ret)
1099                 return ret;
1100
1101         of_node_get(np);
1102
1103         ret = of_gpiochip_scan_gpios(chip);
1104         if (ret)
1105                 of_node_put(np);
1106
1107         return ret;
1108 }
1109
1110 void of_gpiochip_remove(struct gpio_chip *chip)
1111 {
1112         of_node_put(dev_of_node(&chip->gpiodev->dev));
1113 }