Debian Codegraph @

drivers/gpio/gpiolib-of.c

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