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/fwnode.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_address.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 */
of_gpio_named_count(const struct device_node * np,const char * propname)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 */
of_gpio_spi_cs_get_count(const struct device_node * np,const char * con_id)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
of_gpio_count(const struct fwnode_handle * fwnode,const char * con_id)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
of_gpiochip_match_node_and_xlate(struct gpio_chip * chip,const void * data)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 *
of_find_gpio_device_by_xlate(const struct of_phandle_args * gpiospec)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
of_xlate_and_get_gpiod_flags(struct gpio_chip * chip,struct of_phandle_args * gpiospec,enum of_gpio_flags * flags)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 */
of_gpio_quirk_polarity(const struct device_node * np,bool active_high,enum of_gpio_flags * flags)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 */
of_gpio_try_fixup_polarity(const struct device_node * np,const char * propname,enum of_gpio_flags * flags)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
of_gpio_set_polarity_by_property(const struct device_node * np,const char * propname,enum of_gpio_flags * flags)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
of_gpio_flags_quirks(const struct device_node * np,const char * propname,enum of_gpio_flags * flags,int index)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 */
of_get_named_gpiod_flags(const struct device_node * np,const char * propname,int index,enum of_gpio_flags * flags)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 /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
of_convert_gpio_flags(enum of_gpio_flags flags)450 static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
451 {
452 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
453
454 if (flags & OF_GPIO_ACTIVE_LOW)
455 lflags |= GPIO_ACTIVE_LOW;
456
457 if (flags & OF_GPIO_SINGLE_ENDED) {
458 if (flags & OF_GPIO_OPEN_DRAIN)
459 lflags |= GPIO_OPEN_DRAIN;
460 else
461 lflags |= GPIO_OPEN_SOURCE;
462 }
463
464 if (flags & OF_GPIO_TRANSITORY)
465 lflags |= GPIO_TRANSITORY;
466
467 if (flags & OF_GPIO_PULL_UP)
468 lflags |= GPIO_PULL_UP;
469
470 if (flags & OF_GPIO_PULL_DOWN)
471 lflags |= GPIO_PULL_DOWN;
472
473 if (flags & OF_GPIO_PULL_DISABLE)
474 lflags |= GPIO_PULL_DISABLE;
475
476 return lflags;
477 }
478
of_find_gpio_rename(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)479 static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
480 const char *con_id,
481 unsigned int idx,
482 enum of_gpio_flags *of_flags)
483 {
484 static const struct of_rename_gpio {
485 const char *con_id;
486 const char *legacy_id; /* NULL - same as con_id */
487 /*
488 * Compatible string can be set to NULL in case where
489 * matching to a particular compatible is not practical,
490 * but it should only be done for gpio names that have
491 * vendor prefix to reduce risk of false positives.
492 * Addition of such entries is strongly discouraged.
493 */
494 const char *compatible;
495 } gpios[] = {
496 #if IS_ENABLED(CONFIG_LCD_HX8357)
497 /* Himax LCD controllers used "gpios-reset" */
498 { "reset", "gpios-reset", "himax,hx8357" },
499 { "reset", "gpios-reset", "himax,hx8369" },
500 #endif
501 #if IS_ENABLED(CONFIG_MFD_ARIZONA)
502 { "wlf,reset", NULL, NULL },
503 #endif
504 #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
505 { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" },
506 { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" },
507 { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" },
508 #endif
509 #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
510 { "reset", "reset-n-io", "marvell,nfc-i2c" },
511 #endif
512 #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
513 { "reset", "reset-n-io", "marvell,nfc-spi" },
514 #endif
515 #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
516 { "reset", "reset-n-io", "marvell,nfc-uart" },
517 { "reset", "reset-n-io", "mrvl,nfc-uart" },
518 #endif
519 #if IS_ENABLED(CONFIG_NFC_S3FWRN5_I2C)
520 { "en", "s3fwrn5,en-gpios", "samsung,s3fwrn5-i2c" },
521 { "wake", "s3fwrn5,fw-gpios", "samsung,s3fwrn5-i2c" },
522 #endif
523 #if IS_ENABLED(CONFIG_PCI_LANTIQ)
524 /* MIPS Lantiq PCI */
525 { "reset", "gpio-reset", "lantiq,pci-xway" },
526 #endif
527
528 /*
529 * Some regulator bindings happened before we managed to
530 * establish that GPIO properties should be named
531 * "foo-gpios" so we have this special kludge for them.
532 */
533 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
534 { "wlf,ldoena", NULL, NULL }, /* Arizona */
535 #endif
536 #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
537 { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */
538 { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */
539 #endif
540
541 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
542 { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" },
543 #endif
544 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
545 { "i2s1-in-sel-gpio1", NULL, "mediatek,mt2701-cs42448-machine" },
546 { "i2s1-in-sel-gpio2", NULL, "mediatek,mt2701-cs42448-machine" },
547 #endif
548 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
549 { "reset", "gpio-reset", "ti,tlv320aic3x" },
550 { "reset", "gpio-reset", "ti,tlv320aic33" },
551 { "reset", "gpio-reset", "ti,tlv320aic3007" },
552 { "reset", "gpio-reset", "ti,tlv320aic3104" },
553 { "reset", "gpio-reset", "ti,tlv320aic3106" },
554 #endif
555 #if IS_ENABLED(CONFIG_SPI_GPIO)
556 /*
557 * The SPI GPIO bindings happened before we managed to
558 * establish that GPIO properties should be named
559 * "foo-gpios" so we have this special kludge for them.
560 */
561 { "miso", "gpio-miso", "spi-gpio" },
562 { "mosi", "gpio-mosi", "spi-gpio" },
563 { "sck", "gpio-sck", "spi-gpio" },
564 #endif
565
566 /*
567 * The old Freescale bindings use simply "gpios" as name
568 * for the chip select lines rather than "cs-gpios" like
569 * all other SPI hardware. Allow this specifically for
570 * Freescale and PPC devices.
571 */
572 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
573 { "cs", "gpios", "fsl,spi" },
574 { "cs", "gpios", "aeroflexgaisler,spictrl" },
575 #endif
576 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
577 { "cs", "gpios", "ibm,ppc4xx-spi" },
578 #endif
579
580 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
581 /*
582 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
583 * property without the compulsory "-gpios" suffix.
584 */
585 { "fcs,int_n", NULL, "fcs,fusb302" },
586 #endif
587 };
588 struct gpio_desc *desc;
589 const char *legacy_id;
590 unsigned int i;
591
592 if (!con_id)
593 return ERR_PTR(-ENOENT);
594
595 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
596 if (strcmp(con_id, gpios[i].con_id))
597 continue;
598
599 if (gpios[i].compatible &&
600 !of_device_is_compatible(np, gpios[i].compatible))
601 continue;
602
603 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
604 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
605 if (!gpiod_not_found(desc)) {
606 pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
607 of_node_full_name(np), legacy_id, con_id);
608 return desc;
609 }
610 }
611
612 return ERR_PTR(-ENOENT);
613 }
614
615 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
of_find_mt2701_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)616 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
617 const char *con_id,
618 unsigned int idx,
619 enum of_gpio_flags *of_flags)
620 {
621 struct gpio_desc *desc;
622 const char *legacy_id;
623
624 if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
625 return ERR_PTR(-ENOENT);
626
627 if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
628 return ERR_PTR(-ENOENT);
629
630 if (!con_id || strcmp(con_id, "i2s1-in-sel"))
631 return ERR_PTR(-ENOENT);
632
633 if (idx == 0)
634 legacy_id = "i2s1-in-sel-gpio1";
635 else if (idx == 1)
636 legacy_id = "i2s1-in-sel-gpio2";
637 else
638 return ERR_PTR(-ENOENT);
639
640 desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
641 if (!gpiod_not_found(desc))
642 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
643 of_node_full_name(np), legacy_id, con_id);
644
645 return desc;
646 }
647 #endif
648
649 /*
650 * Trigger sources are special, they allow us to use any GPIO as a LED trigger
651 * and have the name "trigger-sources" no matter which kind of phandle it is
652 * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case
653 * we allow looking something up that is not named "foo-gpios".
654 */
of_find_trigger_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)655 static struct gpio_desc *of_find_trigger_gpio(struct device_node *np,
656 const char *con_id,
657 unsigned int idx,
658 enum of_gpio_flags *of_flags)
659 {
660 struct gpio_desc *desc;
661
662 if (!IS_ENABLED(CONFIG_LEDS_TRIGGER_GPIO))
663 return ERR_PTR(-ENOENT);
664
665 if (!con_id || strcmp(con_id, "trigger-sources"))
666 return ERR_PTR(-ENOENT);
667
668 desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags);
669 if (!gpiod_not_found(desc))
670 pr_debug("%s is used as a trigger\n", of_node_full_name(np));
671
672 return desc;
673 }
674
675
676 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
677 const char *con_id,
678 unsigned int idx,
679 enum of_gpio_flags *of_flags);
680 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
681 of_find_gpio_rename,
682 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
683 of_find_mt2701_gpio,
684 #endif
685 of_find_trigger_gpio,
686 NULL
687 };
688
of_find_gpio(struct device_node * np,const char * con_id,unsigned int idx,unsigned long * flags)689 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
690 unsigned int idx, unsigned long *flags)
691 {
692 char propname[32]; /* 32 is max size of property name */
693 enum of_gpio_flags of_flags = 0;
694 const of_find_gpio_quirk *q;
695 struct gpio_desc *desc;
696
697 /* Try GPIO property "foo-gpios" and "foo-gpio" */
698 for_each_gpio_property_name(propname, con_id) {
699 desc = of_get_named_gpiod_flags(np, propname, idx, &of_flags);
700 if (!gpiod_not_found(desc))
701 break;
702 }
703
704 /* Properly named GPIO was not found, try workarounds */
705 for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
706 desc = (*q)(np, con_id, idx, &of_flags);
707
708 if (IS_ERR(desc))
709 return desc;
710
711 *flags = of_convert_gpio_flags(of_flags);
712
713 return desc;
714 }
715
of_gpiochip_get_lflags(struct gpio_chip * chip,struct fwnode_reference_args * gpiospec,unsigned long * lflags)716 int of_gpiochip_get_lflags(struct gpio_chip *chip,
717 struct fwnode_reference_args *gpiospec,
718 unsigned long *lflags)
719 {
720 enum of_gpio_flags xlate_flags;
721 struct of_phandle_args args;
722 struct gpio_desc *desc;
723
724 args.np = to_of_node(gpiospec->fwnode);
725 args.args_count = gpiospec->nargs;
726
727 for (int i = 0; i < args.args_count; i++)
728 args.args[i] = gpiospec->args[i];
729
730 desc = of_xlate_and_get_gpiod_flags(chip, &args, &xlate_flags);
731 if (IS_ERR(desc))
732 return PTR_ERR(desc);
733
734 *lflags = of_convert_gpio_flags(xlate_flags);
735
736 return 0;
737 }
738
739 #ifdef CONFIG_OF_DYNAMIC
740 /**
741 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
742 * @chip: gpio chip to act on
743 * @hog: device node describing the hogs
744 */
of_gpiochip_remove_hog(struct gpio_chip * chip,struct device_node * hog)745 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
746 struct device_node *hog)
747 {
748 struct gpio_desc *desc;
749
750 for_each_gpio_desc_with_flag(chip, desc, GPIOD_FLAG_IS_HOGGED)
751 if (READ_ONCE(desc->hog) == hog)
752 gpiochip_free_own_desc(desc);
753 }
754
of_gpiochip_match_node(struct gpio_chip * chip,const void * data)755 static int of_gpiochip_match_node(struct gpio_chip *chip, const void *data)
756 {
757 return device_match_of_node(&chip->gpiodev->dev, data);
758 }
759
of_find_gpio_device_by_node(struct device_node * np)760 static struct gpio_device *of_find_gpio_device_by_node(struct device_node *np)
761 {
762 return gpio_device_find(np, of_gpiochip_match_node);
763 }
764
of_gpio_notify(struct notifier_block * nb,unsigned long action,void * arg)765 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
766 void *arg)
767 {
768 struct gpio_device *gdev __free(gpio_device_put) = NULL;
769 struct of_reconfig_data *rd = arg;
770 int ret;
771
772 /*
773 * This only supports adding and removing complete gpio-hog nodes.
774 * Modifying an existing gpio-hog node is not supported (except for
775 * changing its "status" property, which is treated the same as
776 * addition/removal).
777 */
778 switch (of_reconfig_get_state_change(action, arg)) {
779 case OF_RECONFIG_CHANGE_ADD:
780 if (!of_property_read_bool(rd->dn, "gpio-hog"))
781 return NOTIFY_DONE; /* not for us */
782
783 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
784 return NOTIFY_DONE;
785
786 gdev = of_find_gpio_device_by_node(rd->dn->parent);
787 if (!gdev)
788 return NOTIFY_DONE; /* not for us */
789
790 ret = gpiochip_add_hog(gpio_device_get_chip(gdev), of_fwnode_handle(rd->dn));
791 if (ret < 0) {
792 pr_err("%s: failed to add hogs for %pOF\n", __func__,
793 rd->dn);
794 of_node_clear_flag(rd->dn, OF_POPULATED);
795 return notifier_from_errno(ret);
796 }
797 return NOTIFY_OK;
798
799 case OF_RECONFIG_CHANGE_REMOVE:
800 if (!of_node_check_flag(rd->dn, OF_POPULATED))
801 return NOTIFY_DONE; /* already depopulated */
802
803 gdev = of_find_gpio_device_by_node(rd->dn->parent);
804 if (!gdev)
805 return NOTIFY_DONE; /* not for us */
806
807 of_gpiochip_remove_hog(gpio_device_get_chip(gdev), rd->dn);
808 of_node_clear_flag(rd->dn, OF_POPULATED);
809 return NOTIFY_OK;
810 }
811
812 return NOTIFY_DONE;
813 }
814
815 struct notifier_block gpio_of_notifier = {
816 .notifier_call = of_gpio_notify,
817 };
818 #endif /* CONFIG_OF_DYNAMIC */
819
820 /**
821 * of_gpio_twocell_xlate - translate twocell gpiospec to the GPIO number and flags
822 * @gc: pointer to the gpio_chip structure
823 * @gpiospec: GPIO specifier as found in the device tree
824 * @flags: a flags pointer to fill in
825 *
826 * This is simple translation function, suitable for the most 1:1 mapped
827 * GPIO chips. This function performs only one sanity check: whether GPIO
828 * is less than ngpios (that is specified in the gpio_chip).
829 *
830 * Returns:
831 * GPIO number (>= 0) on success, negative errno on failure.
832 */
of_gpio_twocell_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)833 static int of_gpio_twocell_xlate(struct gpio_chip *gc,
834 const struct of_phandle_args *gpiospec,
835 u32 *flags)
836 {
837 /*
838 * We're discouraging gpio_cells < 2, since that way you'll have to
839 * write your own xlate function (that will have to retrieve the GPIO
840 * number and the flags from a single gpio cell -- this is possible,
841 * but not recommended).
842 */
843 if (gc->of_gpio_n_cells != 2) {
844 WARN_ON(1);
845 return -EINVAL;
846 }
847
848 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
849 return -EINVAL;
850
851 if (gpiospec->args[0] >= gc->ngpio)
852 return -EINVAL;
853
854 if (flags)
855 *flags = gpiospec->args[1];
856
857 return gpiospec->args[0];
858 }
859
860 /**
861 * of_gpio_threecell_xlate - translate threecell gpiospec to the GPIO number and flags
862 * @gc: pointer to the gpio_chip structure
863 * @gpiospec: GPIO specifier as found in the device tree
864 * @flags: a flags pointer to fill in
865 *
866 * This is simple translation function, suitable for the most 1:n mapped
867 * GPIO chips, i.e. several GPIO chip instances from one device tree node.
868 * In this case the following binding is implied:
869 *
870 * foo-gpios = <&gpio instance offset flags>;
871 *
872 * Returns:
873 * GPIO number (>= 0) on success, negative errno on failure.
874 */
of_gpio_threecell_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)875 static int of_gpio_threecell_xlate(struct gpio_chip *gc,
876 const struct of_phandle_args *gpiospec,
877 u32 *flags)
878 {
879 if (gc->of_gpio_n_cells != 3) {
880 WARN_ON(1);
881 return -EINVAL;
882 }
883
884 if (WARN_ON(gpiospec->args_count != 3))
885 return -EINVAL;
886
887 /*
888 * Check chip instance number, the driver responds with true if
889 * this is the chip we are looking for.
890 */
891 if (!gc->of_node_instance_match(gc, gpiospec->args[0]))
892 return -EINVAL;
893
894 if (gpiospec->args[1] >= gc->ngpio)
895 return -EINVAL;
896
897 if (flags)
898 *flags = gpiospec->args[2];
899
900 return gpiospec->args[1];
901 }
902
903 #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)904 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
905 {
906 struct of_phandle_args pinspec;
907 struct pinctrl_dev *pctldev;
908 struct device_node *np;
909 int index = 0, ret, trim;
910 const char *name;
911 static const char group_names_propname[] = "gpio-ranges-group-names";
912 bool has_group_names;
913 int offset; /* Offset of the first GPIO line on the chip */
914 int pin; /* Pin base number in the range */
915 int count; /* Number of pins/GPIO lines to map */
916
917 np = dev_of_node(&chip->gpiodev->dev);
918 if (!np)
919 return 0;
920
921 has_group_names = of_property_present(np, group_names_propname);
922
923 for (;; index++) {
924 /*
925 * Ordinary phandles contain 2-3 cells:
926 * gpios = <&gpio [instance] offset flags>;
927 * Ranges always contain one more cell:
928 * gpio-ranges <&pinctrl [gpio_instance] gpio_offet pin_offet count>;
929 * This is why we parse chip->of_gpio_n_cells + 1 cells
930 */
931 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges",
932 chip->of_gpio_n_cells + 1,
933 index, &pinspec);
934 if (ret)
935 break;
936
937 pctldev = of_pinctrl_get(pinspec.np);
938 of_node_put(pinspec.np);
939 if (!pctldev)
940 return -EPROBE_DEFER;
941
942 if (chip->of_gpio_n_cells == 3) {
943 /* First cell is the gpiochip instance number */
944 offset = pinspec.args[1];
945 pin = pinspec.args[2];
946 count = pinspec.args[3];
947 } else {
948 offset = pinspec.args[0];
949 pin = pinspec.args[1];
950 count = pinspec.args[2];
951 }
952
953 /*
954 * With multiple GPIO chips per node, check that this chip is the
955 * right instance.
956 */
957 if (chip->of_node_instance_match &&
958 (chip->of_gpio_n_cells == 3) &&
959 !chip->of_node_instance_match(chip, pinspec.args[0]))
960 continue;
961
962 /* Ignore ranges outside of this GPIO chip */
963 if (offset >= (chip->offset + chip->ngpio))
964 continue;
965 if (offset + count <= chip->offset)
966 continue;
967
968 if (count) {
969 /* npins != 0: linear range */
970 if (has_group_names) {
971 of_property_read_string_index(np,
972 group_names_propname,
973 index, &name);
974 if (strlen(name)) {
975 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
976 np);
977 break;
978 }
979 }
980
981 /* Trim the range to fit this GPIO chip */
982 if (chip->offset > offset) {
983 trim = chip->offset - offset;
984 count -= trim;
985 pin += trim;
986 offset = 0;
987 } else {
988 offset -= chip->offset;
989 }
990 if ((offset + count) > chip->ngpio)
991 count = chip->ngpio - offset;
992
993 ret = gpiochip_add_pin_range(chip,
994 pinctrl_dev_get_devname(pctldev),
995 offset,
996 pin,
997 count);
998 if (ret)
999 return ret;
1000 } else {
1001 /* npins == 0: special range */
1002 if (pin) {
1003 pr_err("%pOF: Illegal gpio-range format.\n",
1004 np);
1005 break;
1006 }
1007
1008 if (!has_group_names) {
1009 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1010 np, group_names_propname);
1011 break;
1012 }
1013
1014 ret = of_property_read_string_index(np,
1015 group_names_propname,
1016 index, &name);
1017 if (ret)
1018 break;
1019
1020 if (!strlen(name)) {
1021 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1022 np);
1023 break;
1024 }
1025
1026 ret = gpiochip_add_pingroup_range(chip, pctldev,
1027 offset, name);
1028 if (ret)
1029 return ret;
1030 }
1031 }
1032
1033 return 0;
1034 }
1035
1036 #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)1037 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1038 #endif
1039
of_gpiochip_add(struct gpio_chip * chip)1040 int of_gpiochip_add(struct gpio_chip *chip)
1041 {
1042 struct device_node *np;
1043 int ret;
1044
1045 np = dev_of_node(&chip->gpiodev->dev);
1046 if (!np)
1047 return 0;
1048
1049 if (!chip->of_xlate) {
1050 if (chip->of_gpio_n_cells == 3) {
1051 if (!chip->of_node_instance_match)
1052 return -EINVAL;
1053 chip->of_xlate = of_gpio_threecell_xlate;
1054 } else {
1055 chip->of_gpio_n_cells = 2;
1056 chip->of_xlate = of_gpio_twocell_xlate;
1057 }
1058 }
1059
1060 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1061 return -EINVAL;
1062
1063 ret = of_gpiochip_add_pin_range(chip);
1064 if (ret)
1065 return ret;
1066
1067 of_node_get(np);
1068
1069 for_each_available_child_of_node_scoped(np, child) {
1070 if (of_property_read_bool(child, "gpio-hog"))
1071 of_node_set_flag(child, OF_POPULATED);
1072 }
1073
1074 return ret;
1075 }
1076
of_gpiochip_remove(struct gpio_chip * chip)1077 void of_gpiochip_remove(struct gpio_chip *chip)
1078 {
1079 struct device_node *np = dev_of_node(&chip->gpiodev->dev);
1080
1081 for_each_child_of_node_scoped(np, child) {
1082 if (of_property_present(child, "gpio-hog"))
1083 of_node_clear_flag(child, OF_POPULATED);
1084 }
1085
1086 of_node_put(np);
1087 }
1088
of_gpiochip_instance_match(struct gpio_chip * gc,unsigned int index)1089 bool of_gpiochip_instance_match(struct gpio_chip *gc, unsigned int index)
1090 {
1091 if (gc->of_node_instance_match)
1092 return gc->of_node_instance_match(gc, index);
1093
1094 return false;
1095 }
1096