1 // SPDX-License-Identifier: GPL-2.0+
3 * OF helpers for the GPIO API
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
15 #include <linux/gpio/consumer.h>
17 #include <linux/of_address.h>
18 #include <linux/of_gpio.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/slab.h>
21 #include <linux/gpio/machine.h>
24 #include "gpiolib-of.h"
27 * This is Linux-specific flags. By default controllers' and Linux' mapping
28 * match, but GPIO controllers are free to translate their own flags to
29 * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
32 OF_GPIO_ACTIVE_LOW = 0x1,
33 OF_GPIO_SINGLE_ENDED = 0x2,
34 OF_GPIO_OPEN_DRAIN = 0x4,
35 OF_GPIO_TRANSITORY = 0x8,
36 OF_GPIO_PULL_UP = 0x10,
37 OF_GPIO_PULL_DOWN = 0x20,
38 OF_GPIO_PULL_DISABLE = 0x40,
42 * of_gpio_named_count() - Count GPIOs for a device
43 * @np: device node to count GPIOs for
44 * @propname: property name containing gpio specifier(s)
46 * The function returns the count of GPIOs specified for a node.
47 * Note that the empty GPIO specifiers count too. Returns either
48 * Number of gpios defined in property,
49 * -EINVAL for an incorrectly formed gpios property, or
50 * -ENOENT for a missing gpios property
58 * The above example defines four GPIOs, two of which are not specified.
59 * This function will return '4'
61 static int of_gpio_named_count(const struct device_node *np,
64 return of_count_phandle_with_args(np, propname, "#gpio-cells");
68 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
69 * @dev: Consuming device
70 * @con_id: Function within the GPIO consumer
72 * Some elder GPIO controllers need special quirks. Currently we handle
73 * the Freescale and PPC GPIO controller with bindings that doesn't use the
74 * established "cs-gpios" for chip selects but instead rely on
75 * "gpios" for the chip select lines. If we detect this, we redirect
76 * the counting of "cs-gpios" to count "gpios" transparent to the
79 static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
81 struct device_node *np = dev->of_node;
83 if (!IS_ENABLED(CONFIG_SPI_MASTER))
85 if (!con_id || strcmp(con_id, "cs"))
87 if (!of_device_is_compatible(np, "fsl,spi") &&
88 !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
89 !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
91 return of_gpio_named_count(np, "gpios");
94 int of_gpio_get_count(struct device *dev, const char *con_id)
100 ret = of_gpio_spi_cs_get_count(dev, con_id);
104 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
106 snprintf(propname, sizeof(propname), "%s-%s",
107 con_id, gpio_suffixes[i]);
109 snprintf(propname, sizeof(propname), "%s",
112 ret = of_gpio_named_count(dev->of_node, propname);
116 return ret ? ret : -ENOENT;
119 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
121 struct of_phandle_args *gpiospec = data;
123 return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) &&
125 chip->of_xlate(chip, gpiospec, NULL) >= 0;
128 static struct gpio_chip *of_find_gpiochip_by_xlate(
129 struct of_phandle_args *gpiospec)
131 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
134 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
135 struct of_phandle_args *gpiospec,
136 enum of_gpio_flags *flags)
140 if (chip->of_gpio_n_cells != gpiospec->args_count)
141 return ERR_PTR(-EINVAL);
143 ret = chip->of_xlate(chip, gpiospec, flags);
147 return gpiochip_get_desc(chip, ret);
151 * Overrides stated polarity of a gpio line and warns when there is a
154 static void of_gpio_quirk_polarity(const struct device_node *np,
156 enum of_gpio_flags *flags)
159 if (*flags & OF_GPIO_ACTIVE_LOW) {
160 pr_warn("%s GPIO handle specifies active low - ignored\n",
161 of_node_full_name(np));
162 *flags &= ~OF_GPIO_ACTIVE_LOW;
165 if (!(*flags & OF_GPIO_ACTIVE_LOW))
166 pr_info("%s enforce active low on GPIO handle\n",
167 of_node_full_name(np));
168 *flags |= OF_GPIO_ACTIVE_LOW;
173 * This quirk does static polarity overrides in cases where existing
174 * DTS specified incorrect polarity.
176 static void of_gpio_try_fixup_polarity(const struct device_node *np,
177 const char *propname,
178 enum of_gpio_flags *flags)
180 static const struct {
181 const char *compatible;
182 const char *propname;
185 #if !IS_ENABLED(CONFIG_LCD_HX8357)
187 * Himax LCD controllers used incorrectly named
188 * "gpios-reset" property and also specified wrong
191 { "himax,hx8357", "gpios-reset", false },
192 { "himax,hx8369", "gpios-reset", false },
197 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
198 if (of_device_is_compatible(np, gpios[i].compatible) &&
199 !strcmp(propname, gpios[i].propname)) {
200 of_gpio_quirk_polarity(np, gpios[i].active_high, flags);
206 static void of_gpio_set_polarity_by_property(const struct device_node *np,
207 const char *propname,
208 enum of_gpio_flags *flags)
210 static const struct {
211 const char *compatible;
212 const char *gpio_propname;
213 const char *polarity_propname;
215 #if IS_ENABLED(CONFIG_FEC)
216 /* Freescale Fast Ethernet Controller */
217 { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" },
218 { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" },
219 { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" },
220 { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" },
221 { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" },
222 { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" },
223 { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" },
224 { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" },
225 { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" },
226 { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" },
228 #if IS_ENABLED(CONFIG_PCI_IMX6)
229 { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" },
230 { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" },
231 { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" },
232 { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" },
233 { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" },
234 { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
235 { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
239 * The regulator GPIO handles are specified such that the
240 * presence or absence of "enable-active-high" solely controls
241 * the polarity of the GPIO line. Any phandle flags must
242 * be actively ignored.
244 #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE)
245 { "regulator-fixed", "gpios", "enable-active-high" },
246 { "regulator-fixed", "gpio", "enable-active-high" },
247 { "reg-fixed-voltage", "gpios", "enable-active-high" },
248 { "reg-fixed-voltage", "gpio", "enable-active-high" },
250 #if IS_ENABLED(CONFIG_REGULATOR_GPIO)
251 { "regulator-gpio", "enable-gpio", "enable-active-high" },
252 { "regulator-gpio", "enable-gpios", "enable-active-high" },
258 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
259 if (of_device_is_compatible(np, gpios[i].compatible) &&
260 !strcmp(propname, gpios[i].gpio_propname)) {
261 active_high = of_property_read_bool(np,
262 gpios[i].polarity_propname);
263 of_gpio_quirk_polarity(np, active_high, flags);
269 static void of_gpio_flags_quirks(const struct device_node *np,
270 const char *propname,
271 enum of_gpio_flags *flags,
274 of_gpio_try_fixup_polarity(np, propname, flags);
275 of_gpio_set_polarity_by_property(np, propname, flags);
278 * Legacy open drain handling for fixed voltage regulators.
280 if (IS_ENABLED(CONFIG_REGULATOR) &&
281 of_device_is_compatible(np, "reg-fixed-voltage") &&
282 of_property_read_bool(np, "gpio-open-drain")) {
283 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
284 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
285 of_node_full_name(np));
289 * Legacy handling of SPI active high chip select. If we have a
290 * property named "cs-gpios" we need to inspect the child node
291 * to determine if the flags should have inverted semantics.
293 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
294 of_property_read_bool(np, "cs-gpios")) {
295 struct device_node *child;
299 for_each_child_of_node(np, child) {
300 ret = of_property_read_u32(child, "reg", &cs);
305 * SPI children have active low chip selects
306 * by default. This can be specified negatively
307 * by just omitting "spi-cs-high" in the
308 * device node, or actively by tagging on
309 * GPIO_ACTIVE_LOW as flag in the device
310 * tree. If the line is simultaneously
311 * tagged as active low in the device tree
312 * and has the "spi-cs-high" set, we get a
313 * conflict and the "spi-cs-high" flag will
316 bool active_high = of_property_read_bool(child,
318 of_gpio_quirk_polarity(child, active_high,
326 /* Legacy handling of stmmac's active-low PHY reset line */
327 if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
328 !strcmp(propname, "snps,reset-gpio") &&
329 of_property_read_bool(np, "snps,reset-active-low"))
330 *flags |= OF_GPIO_ACTIVE_LOW;
334 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
335 * @np: device node to get GPIO from
336 * @propname: property name containing gpio specifier(s)
337 * @index: index of the GPIO
338 * @flags: a flags pointer to fill in
340 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
341 * value on the error condition. If @flags is not NULL the function also fills
342 * in flags for the GPIO.
344 static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
345 const char *propname, int index, enum of_gpio_flags *flags)
347 struct of_phandle_args gpiospec;
348 struct gpio_chip *chip;
349 struct gpio_desc *desc;
352 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
355 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
356 __func__, propname, np, index);
360 chip = of_find_gpiochip_by_xlate(&gpiospec);
362 desc = ERR_PTR(-EPROBE_DEFER);
366 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
371 of_gpio_flags_quirks(np, propname, flags, index);
373 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
374 __func__, propname, np, index,
375 PTR_ERR_OR_ZERO(desc));
378 of_node_put(gpiospec.np);
384 * of_get_named_gpio() - Get a GPIO number to use with GPIO API
385 * @np: device node to get GPIO from
386 * @propname: Name of property containing gpio specifier(s)
387 * @index: index of the GPIO
389 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
390 * value on the error condition.
392 int of_get_named_gpio(const struct device_node *np, const char *propname,
395 struct gpio_desc *desc;
397 desc = of_get_named_gpiod_flags(np, propname, index, NULL);
400 return PTR_ERR(desc);
402 return desc_to_gpio(desc);
404 EXPORT_SYMBOL_GPL(of_get_named_gpio);
406 /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
407 static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
409 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
411 if (flags & OF_GPIO_ACTIVE_LOW)
412 lflags |= GPIO_ACTIVE_LOW;
414 if (flags & OF_GPIO_SINGLE_ENDED) {
415 if (flags & OF_GPIO_OPEN_DRAIN)
416 lflags |= GPIO_OPEN_DRAIN;
418 lflags |= GPIO_OPEN_SOURCE;
421 if (flags & OF_GPIO_TRANSITORY)
422 lflags |= GPIO_TRANSITORY;
424 if (flags & OF_GPIO_PULL_UP)
425 lflags |= GPIO_PULL_UP;
427 if (flags & OF_GPIO_PULL_DOWN)
428 lflags |= GPIO_PULL_DOWN;
430 if (flags & OF_GPIO_PULL_DISABLE)
431 lflags |= GPIO_PULL_DISABLE;
436 static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
439 enum of_gpio_flags *of_flags)
441 static const struct of_rename_gpio {
443 const char *legacy_id; /* NULL - same as con_id */
445 * Compatible string can be set to NULL in case where
446 * matching to a particular compatible is not practical,
447 * but it should only be done for gpio names that have
448 * vendor prefix to reduce risk of false positives.
449 * Addition of such entries is strongly discouraged.
451 const char *compatible;
453 #if !IS_ENABLED(CONFIG_LCD_HX8357)
454 /* Himax LCD controllers used "gpios-reset" */
455 { "reset", "gpios-reset", "himax,hx8357" },
456 { "reset", "gpios-reset", "himax,hx8369" },
458 #if IS_ENABLED(CONFIG_MFD_ARIZONA)
459 { "wlf,reset", NULL, NULL },
461 #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
462 { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" },
463 { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" },
464 { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" },
466 #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
467 { "reset", "reset-n-io", "marvell,nfc-i2c" },
469 #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
470 { "reset", "reset-n-io", "marvell,nfc-spi" },
472 #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
473 { "reset", "reset-n-io", "marvell,nfc-uart" },
474 { "reset", "reset-n-io", "mrvl,nfc-uart" },
476 #if !IS_ENABLED(CONFIG_PCI_LANTIQ)
477 /* MIPS Lantiq PCI */
478 { "reset", "gpios-reset", "lantiq,pci-xway" },
482 * Some regulator bindings happened before we managed to
483 * establish that GPIO properties should be named
484 * "foo-gpios" so we have this special kludge for them.
486 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
487 { "wlf,ldoena", NULL, NULL }, /* Arizona */
489 #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
490 { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */
491 { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */
494 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
495 { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" },
497 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
498 { "reset", "gpio-reset", "ti,tlv320aic3x" },
499 { "reset", "gpio-reset", "ti,tlv320aic33" },
500 { "reset", "gpio-reset", "ti,tlv320aic3007" },
501 { "reset", "gpio-reset", "ti,tlv320aic3104" },
502 { "reset", "gpio-reset", "ti,tlv320aic3106" },
504 #if IS_ENABLED(CONFIG_SPI_GPIO)
506 * The SPI GPIO bindings happened before we managed to
507 * establish that GPIO properties should be named
508 * "foo-gpios" so we have this special kludge for them.
510 { "miso", "gpio-miso", "spi-gpio" },
511 { "mosi", "gpio-mosi", "spi-gpio" },
512 { "sck", "gpio-sck", "spi-gpio" },
516 * The old Freescale bindings use simply "gpios" as name
517 * for the chip select lines rather than "cs-gpios" like
518 * all other SPI hardware. Allow this specifically for
519 * Freescale and PPC devices.
521 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
522 { "cs", "gpios", "fsl,spi" },
523 { "cs", "gpios", "aeroflexgaisler,spictrl" },
525 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
526 { "cs", "gpios", "ibm,ppc4xx-spi" },
529 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
531 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
532 * property without the compulsory "-gpios" suffix.
534 { "fcs,int_n", NULL, "fcs,fusb302" },
537 struct gpio_desc *desc;
538 const char *legacy_id;
542 return ERR_PTR(-ENOENT);
544 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
545 if (strcmp(con_id, gpios[i].con_id))
548 if (gpios[i].compatible &&
549 !of_device_is_compatible(np, gpios[i].compatible))
552 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
553 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
554 if (!gpiod_not_found(desc)) {
555 pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
556 of_node_full_name(np), legacy_id, con_id);
561 return ERR_PTR(-ENOENT);
564 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
567 enum of_gpio_flags *of_flags)
569 struct gpio_desc *desc;
570 const char *legacy_id;
572 if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
573 return ERR_PTR(-ENOENT);
575 if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
576 return ERR_PTR(-ENOENT);
578 if (!con_id || strcmp(con_id, "i2s1-in-sel"))
579 return ERR_PTR(-ENOENT);
582 legacy_id = "i2s1-in-sel-gpio1";
584 legacy_id = "i2s1-in-sel-gpio2";
586 return ERR_PTR(-ENOENT);
588 desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
589 if (!gpiod_not_found(desc))
590 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
591 of_node_full_name(np), legacy_id, con_id);
596 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
599 enum of_gpio_flags *of_flags);
600 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
606 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
607 unsigned int idx, unsigned long *flags)
609 char prop_name[32]; /* 32 is max size of property name */
610 enum of_gpio_flags of_flags;
611 const of_find_gpio_quirk *q;
612 struct gpio_desc *desc;
615 /* Try GPIO property "foo-gpios" and "foo-gpio" */
616 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
618 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
621 snprintf(prop_name, sizeof(prop_name), "%s",
624 desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
626 if (!gpiod_not_found(desc))
630 /* Properly named GPIO was not found, try workarounds */
631 for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
632 desc = (*q)(np, con_id, idx, &of_flags);
637 *flags = of_convert_gpio_flags(of_flags);
643 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
644 * @np: device node to get GPIO from
645 * @chip: GPIO chip whose hog is parsed
646 * @idx: Index of the GPIO to parse
647 * @name: GPIO line name
648 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
649 * of_find_gpio() or of_parse_own_gpio()
650 * @dflags: gpiod_flags - optional GPIO initialization flags
652 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
653 * value on the error condition.
655 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
656 struct gpio_chip *chip,
657 unsigned int idx, const char **name,
658 unsigned long *lflags,
659 enum gpiod_flags *dflags)
661 struct device_node *chip_np;
662 enum of_gpio_flags xlate_flags;
663 struct of_phandle_args gpiospec;
664 struct gpio_desc *desc;
669 chip_np = dev_of_node(&chip->gpiodev->dev);
671 return ERR_PTR(-EINVAL);
674 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
675 *dflags = GPIOD_ASIS;
677 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
681 gpiospec.np = chip_np;
682 gpiospec.args_count = tmp;
684 for (i = 0; i < tmp; i++) {
685 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
691 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
695 *lflags = of_convert_gpio_flags(xlate_flags);
697 if (of_property_read_bool(np, "input"))
699 else if (of_property_read_bool(np, "output-low"))
700 *dflags |= GPIOD_OUT_LOW;
701 else if (of_property_read_bool(np, "output-high"))
702 *dflags |= GPIOD_OUT_HIGH;
704 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
705 desc_to_gpio(desc), np);
706 return ERR_PTR(-EINVAL);
709 if (name && of_property_read_string(np, "line-name", name))
716 * of_gpiochip_add_hog - Add all hogs in a hog device node
717 * @chip: gpio chip to act on
718 * @hog: device node describing the hogs
720 * Returns error if it fails otherwise 0 on success.
722 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
724 enum gpiod_flags dflags;
725 struct gpio_desc *desc;
726 unsigned long lflags;
732 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
736 ret = gpiod_hog(desc, name, lflags, dflags);
740 #ifdef CONFIG_OF_DYNAMIC
749 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
750 * @chip: gpio chip to act on
752 * This is only used by of_gpiochip_add to request/set GPIO initial
754 * It returns error if it fails otherwise 0 on success.
756 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
758 struct device_node *np;
761 for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
762 if (!of_property_read_bool(np, "gpio-hog"))
765 ret = of_gpiochip_add_hog(chip, np);
771 of_node_set_flag(np, OF_POPULATED);
777 #ifdef CONFIG_OF_DYNAMIC
779 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
780 * @chip: gpio chip to act on
781 * @hog: device node describing the hogs
783 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
784 struct device_node *hog)
786 struct gpio_desc *desc;
788 for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
789 if (desc->hog == hog)
790 gpiochip_free_own_desc(desc);
793 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
795 return device_match_of_node(&chip->gpiodev->dev, data);
798 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
800 return gpiochip_find(np, of_gpiochip_match_node);
803 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
806 struct of_reconfig_data *rd = arg;
807 struct gpio_chip *chip;
811 * This only supports adding and removing complete gpio-hog nodes.
812 * Modifying an existing gpio-hog node is not supported (except for
813 * changing its "status" property, which is treated the same as
816 switch (of_reconfig_get_state_change(action, arg)) {
817 case OF_RECONFIG_CHANGE_ADD:
818 if (!of_property_read_bool(rd->dn, "gpio-hog"))
819 return NOTIFY_OK; /* not for us */
821 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
824 chip = of_find_gpiochip_by_node(rd->dn->parent);
826 return NOTIFY_OK; /* not for us */
828 ret = of_gpiochip_add_hog(chip, rd->dn);
830 pr_err("%s: failed to add hogs for %pOF\n", __func__,
832 of_node_clear_flag(rd->dn, OF_POPULATED);
833 return notifier_from_errno(ret);
837 case OF_RECONFIG_CHANGE_REMOVE:
838 if (!of_node_check_flag(rd->dn, OF_POPULATED))
839 return NOTIFY_OK; /* already depopulated */
841 chip = of_find_gpiochip_by_node(rd->dn->parent);
843 return NOTIFY_OK; /* not for us */
845 of_gpiochip_remove_hog(chip, rd->dn);
846 of_node_clear_flag(rd->dn, OF_POPULATED);
853 struct notifier_block gpio_of_notifier = {
854 .notifier_call = of_gpio_notify,
856 #endif /* CONFIG_OF_DYNAMIC */
859 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
860 * @gc: pointer to the gpio_chip structure
861 * @gpiospec: GPIO specifier as found in the device tree
862 * @flags: a flags pointer to fill in
864 * This is simple translation function, suitable for the most 1:1 mapped
865 * GPIO chips. This function performs only one sanity check: whether GPIO
866 * is less than ngpios (that is specified in the gpio_chip).
868 static int of_gpio_simple_xlate(struct gpio_chip *gc,
869 const struct of_phandle_args *gpiospec,
873 * We're discouraging gpio_cells < 2, since that way you'll have to
874 * write your own xlate function (that will have to retrieve the GPIO
875 * number and the flags from a single gpio cell -- this is possible,
876 * but not recommended).
878 if (gc->of_gpio_n_cells < 2) {
883 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
886 if (gpiospec->args[0] >= gc->ngpio)
890 *flags = gpiospec->args[1];
892 return gpiospec->args[0];
896 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
897 * @np: device node of the GPIO chip
898 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
899 * @data: driver data to store in the struct gpio_chip
901 * To use this function you should allocate and fill mm_gc with:
903 * 1) In the gpio_chip structure:
904 * - all the callbacks
906 * - of_xlate callback (optional)
908 * 3) In the of_mm_gpio_chip structure:
909 * - save_regs callback (optional)
911 * If succeeded, this function will map bank's memory and will
912 * do all necessary work for you. Then you'll able to use .regs
913 * to manage GPIOs from the callbacks.
915 int of_mm_gpiochip_add_data(struct device_node *np,
916 struct of_mm_gpio_chip *mm_gc,
920 struct gpio_chip *gc = &mm_gc->gc;
922 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
926 mm_gc->regs = of_iomap(np, 0);
932 if (mm_gc->save_regs)
933 mm_gc->save_regs(mm_gc);
935 fwnode_handle_put(mm_gc->gc.fwnode);
936 mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
938 ret = gpiochip_add_data(gc, data);
945 iounmap(mm_gc->regs);
949 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
952 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
955 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
956 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
958 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
960 struct gpio_chip *gc = &mm_gc->gc;
963 iounmap(mm_gc->regs);
966 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
968 #ifdef CONFIG_PINCTRL
969 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
971 struct of_phandle_args pinspec;
972 struct pinctrl_dev *pctldev;
973 struct device_node *np;
976 static const char group_names_propname[] = "gpio-ranges-group-names";
977 struct property *group_names;
979 np = dev_of_node(&chip->gpiodev->dev);
983 group_names = of_find_property(np, group_names_propname, NULL);
986 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
991 pctldev = of_pinctrl_get(pinspec.np);
992 of_node_put(pinspec.np);
994 return -EPROBE_DEFER;
996 if (pinspec.args[2]) {
998 of_property_read_string_index(np,
999 group_names_propname,
1002 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1007 /* npins != 0: linear range */
1008 ret = gpiochip_add_pin_range(chip,
1009 pinctrl_dev_get_devname(pctldev),
1016 /* npins == 0: special range */
1017 if (pinspec.args[1]) {
1018 pr_err("%pOF: Illegal gpio-range format.\n",
1024 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1025 np, group_names_propname);
1029 ret = of_property_read_string_index(np,
1030 group_names_propname,
1035 if (!strlen(name)) {
1036 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1041 ret = gpiochip_add_pingroup_range(chip, pctldev,
1042 pinspec.args[0], name);
1052 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1055 int of_gpiochip_add(struct gpio_chip *chip)
1057 struct device_node *np;
1060 np = dev_of_node(&chip->gpiodev->dev);
1064 if (!chip->of_xlate) {
1065 chip->of_gpio_n_cells = 2;
1066 chip->of_xlate = of_gpio_simple_xlate;
1069 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1072 ret = of_gpiochip_add_pin_range(chip);
1076 fwnode_handle_get(chip->fwnode);
1078 ret = of_gpiochip_scan_gpios(chip);
1080 fwnode_handle_put(chip->fwnode);
1085 void of_gpiochip_remove(struct gpio_chip *chip)
1087 fwnode_handle_put(chip->fwnode);