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