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>
14 #include <linux/module.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>
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio/machine.h>
26 #include "gpiolib-of.h"
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.
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,
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)
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
60 * The above example defines four GPIOs, two of which are not specified.
61 * This function will return '4'
63 static int of_gpio_named_count(const struct device_node *np,
66 return of_count_phandle_with_args(np, propname, "#gpio-cells");
70 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
71 * @dev: Consuming device
72 * @con_id: Function within the GPIO consumer
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
81 static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
83 struct device_node *np = dev->of_node;
85 if (!IS_ENABLED(CONFIG_SPI_MASTER))
87 if (!con_id || strcmp(con_id, "cs"))
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"))
93 return of_gpio_named_count(np, "gpios");
96 int of_gpio_get_count(struct device *dev, const char *con_id)
102 ret = of_gpio_spi_cs_get_count(dev, con_id);
106 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
108 snprintf(propname, sizeof(propname), "%s-%s",
109 con_id, gpio_suffixes[i]);
111 snprintf(propname, sizeof(propname), "%s",
114 ret = of_gpio_named_count(dev->of_node, propname);
118 return ret ? ret : -ENOENT;
121 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
123 struct of_phandle_args *gpiospec = data;
125 return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) &&
127 chip->of_xlate(chip, gpiospec, NULL) >= 0;
130 static struct gpio_chip *of_find_gpiochip_by_xlate(
131 struct of_phandle_args *gpiospec)
133 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
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)
142 if (chip->of_gpio_n_cells != gpiospec->args_count)
143 return ERR_PTR(-EINVAL);
145 ret = chip->of_xlate(chip, gpiospec, flags);
149 return gpiochip_get_desc(chip, ret);
153 * Overrides stated polarity of a gpio line and warns when there is a
156 static void of_gpio_quirk_polarity(const struct device_node *np,
158 enum of_gpio_flags *flags)
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;
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;
175 * This quirk does static polarity overrides in cases where existing
176 * DTS specified incorrect polarity.
178 static void of_gpio_try_fixup_polarity(const struct device_node *np,
179 const char *propname,
180 enum of_gpio_flags *flags)
182 static const struct {
183 const char *compatible;
184 const char *propname;
187 #if !IS_ENABLED(CONFIG_LCD_HX8357)
189 * Himax LCD controllers used incorrectly named
190 * "gpios-reset" property and also specified wrong
193 { "himax,hx8357", "gpios-reset", false },
194 { "himax,hx8369", "gpios-reset", false },
199 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
200 if (of_device_is_compatible(np, gpios[i].compatible) &&
201 !strcmp(propname, gpios[i].propname)) {
202 of_gpio_quirk_polarity(np, gpios[i].active_high, flags);
208 static void of_gpio_set_polarity_by_property(const struct device_node *np,
209 const char *propname,
210 enum of_gpio_flags *flags)
212 const struct device_node *np_compat = np;
213 const struct device_node *np_propname = np;
214 static const struct {
215 const char *compatible;
216 const char *gpio_propname;
217 const char *polarity_propname;
219 #if IS_ENABLED(CONFIG_FEC)
220 /* Freescale Fast Ethernet Controller */
221 { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" },
222 { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" },
223 { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" },
224 { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" },
225 { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" },
226 { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" },
227 { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" },
228 { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" },
229 { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" },
230 { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" },
232 #if IS_ENABLED(CONFIG_PCI_IMX6)
233 { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" },
234 { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" },
235 { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" },
236 { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" },
237 { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" },
238 { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
239 { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
243 * The regulator GPIO handles are specified such that the
244 * presence or absence of "enable-active-high" solely controls
245 * the polarity of the GPIO line. Any phandle flags must
246 * be actively ignored.
248 #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE)
249 { "regulator-fixed", "gpios", "enable-active-high" },
250 { "regulator-fixed", "gpio", "enable-active-high" },
251 { "reg-fixed-voltage", "gpios", "enable-active-high" },
252 { "reg-fixed-voltage", "gpio", "enable-active-high" },
254 #if IS_ENABLED(CONFIG_REGULATOR_GPIO)
255 { "regulator-gpio", "enable-gpio", "enable-active-high" },
256 { "regulator-gpio", "enable-gpios", "enable-active-high" },
258 #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
259 { "atmel,hsmci", "cd-gpios", "cd-inverted" },
265 #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
267 * The Atmel HSMCI has compatible property in the parent node and
268 * gpio property in a child node
270 if (of_device_is_compatible(np->parent, "atmel,hsmci")) {
271 np_compat = np->parent;
276 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
277 if (of_device_is_compatible(np_compat, gpios[i].compatible) &&
278 !strcmp(propname, gpios[i].gpio_propname)) {
279 active_high = of_property_read_bool(np_propname,
280 gpios[i].polarity_propname);
281 of_gpio_quirk_polarity(np, active_high, flags);
287 static void of_gpio_flags_quirks(const struct device_node *np,
288 const char *propname,
289 enum of_gpio_flags *flags,
292 of_gpio_try_fixup_polarity(np, propname, flags);
293 of_gpio_set_polarity_by_property(np, propname, flags);
296 * Legacy open drain handling for fixed voltage regulators.
298 if (IS_ENABLED(CONFIG_REGULATOR) &&
299 of_device_is_compatible(np, "reg-fixed-voltage") &&
300 of_property_read_bool(np, "gpio-open-drain")) {
301 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
302 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
303 of_node_full_name(np));
307 * Legacy handling of SPI active high chip select. If we have a
308 * property named "cs-gpios" we need to inspect the child node
309 * to determine if the flags should have inverted semantics.
311 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
312 of_property_read_bool(np, "cs-gpios")) {
313 struct device_node *child;
317 for_each_child_of_node(np, child) {
318 ret = of_property_read_u32(child, "reg", &cs);
323 * SPI children have active low chip selects
324 * by default. This can be specified negatively
325 * by just omitting "spi-cs-high" in the
326 * device node, or actively by tagging on
327 * GPIO_ACTIVE_LOW as flag in the device
328 * tree. If the line is simultaneously
329 * tagged as active low in the device tree
330 * and has the "spi-cs-high" set, we get a
331 * conflict and the "spi-cs-high" flag will
334 bool active_high = of_property_read_bool(child,
336 of_gpio_quirk_polarity(child, active_high,
344 /* Legacy handling of stmmac's active-low PHY reset line */
345 if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
346 !strcmp(propname, "snps,reset-gpio") &&
347 of_property_read_bool(np, "snps,reset-active-low"))
348 *flags |= OF_GPIO_ACTIVE_LOW;
352 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
353 * @np: device node to get GPIO from
354 * @propname: property name containing gpio specifier(s)
355 * @index: index of the GPIO
356 * @flags: a flags pointer to fill in
358 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
359 * value on the error condition. If @flags is not NULL the function also fills
360 * in flags for the GPIO.
362 static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
363 const char *propname, int index, enum of_gpio_flags *flags)
365 struct of_phandle_args gpiospec;
366 struct gpio_chip *chip;
367 struct gpio_desc *desc;
370 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
373 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
374 __func__, propname, np, index);
378 chip = of_find_gpiochip_by_xlate(&gpiospec);
380 desc = ERR_PTR(-EPROBE_DEFER);
384 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
389 of_gpio_flags_quirks(np, propname, flags, index);
391 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
392 __func__, propname, np, index,
393 PTR_ERR_OR_ZERO(desc));
396 of_node_put(gpiospec.np);
402 * of_get_named_gpio() - Get a GPIO number to use with GPIO API
403 * @np: device node to get GPIO from
404 * @propname: Name of property containing gpio specifier(s)
405 * @index: index of the GPIO
407 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
408 * value on the error condition.
410 int of_get_named_gpio(const struct device_node *np, const char *propname,
413 struct gpio_desc *desc;
415 desc = of_get_named_gpiod_flags(np, propname, index, NULL);
418 return PTR_ERR(desc);
420 return desc_to_gpio(desc);
422 EXPORT_SYMBOL_GPL(of_get_named_gpio);
424 /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
425 static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
427 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
429 if (flags & OF_GPIO_ACTIVE_LOW)
430 lflags |= GPIO_ACTIVE_LOW;
432 if (flags & OF_GPIO_SINGLE_ENDED) {
433 if (flags & OF_GPIO_OPEN_DRAIN)
434 lflags |= GPIO_OPEN_DRAIN;
436 lflags |= GPIO_OPEN_SOURCE;
439 if (flags & OF_GPIO_TRANSITORY)
440 lflags |= GPIO_TRANSITORY;
442 if (flags & OF_GPIO_PULL_UP)
443 lflags |= GPIO_PULL_UP;
445 if (flags & OF_GPIO_PULL_DOWN)
446 lflags |= GPIO_PULL_DOWN;
448 if (flags & OF_GPIO_PULL_DISABLE)
449 lflags |= GPIO_PULL_DISABLE;
454 static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
457 enum of_gpio_flags *of_flags)
459 static const struct of_rename_gpio {
461 const char *legacy_id; /* NULL - same as con_id */
463 * Compatible string can be set to NULL in case where
464 * matching to a particular compatible is not practical,
465 * but it should only be done for gpio names that have
466 * vendor prefix to reduce risk of false positives.
467 * Addition of such entries is strongly discouraged.
469 const char *compatible;
471 #if !IS_ENABLED(CONFIG_LCD_HX8357)
472 /* Himax LCD controllers used "gpios-reset" */
473 { "reset", "gpios-reset", "himax,hx8357" },
474 { "reset", "gpios-reset", "himax,hx8369" },
476 #if IS_ENABLED(CONFIG_MFD_ARIZONA)
477 { "wlf,reset", NULL, NULL },
479 #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
480 { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" },
481 { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" },
482 { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" },
484 #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
485 { "reset", "reset-n-io", "marvell,nfc-i2c" },
487 #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
488 { "reset", "reset-n-io", "marvell,nfc-spi" },
490 #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
491 { "reset", "reset-n-io", "marvell,nfc-uart" },
492 { "reset", "reset-n-io", "mrvl,nfc-uart" },
494 #if !IS_ENABLED(CONFIG_PCI_LANTIQ)
495 /* MIPS Lantiq PCI */
496 { "reset", "gpios-reset", "lantiq,pci-xway" },
500 * Some regulator bindings happened before we managed to
501 * establish that GPIO properties should be named
502 * "foo-gpios" so we have this special kludge for them.
504 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
505 { "wlf,ldoena", NULL, NULL }, /* Arizona */
507 #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
508 { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */
509 { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */
512 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
513 { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" },
515 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
516 { "i2s1-in-sel-gpio1", NULL, "mediatek,mt2701-cs42448-machine" },
517 { "i2s1-in-sel-gpio2", NULL, "mediatek,mt2701-cs42448-machine" },
519 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
520 { "reset", "gpio-reset", "ti,tlv320aic3x" },
521 { "reset", "gpio-reset", "ti,tlv320aic33" },
522 { "reset", "gpio-reset", "ti,tlv320aic3007" },
523 { "reset", "gpio-reset", "ti,tlv320aic3104" },
524 { "reset", "gpio-reset", "ti,tlv320aic3106" },
526 #if IS_ENABLED(CONFIG_SPI_GPIO)
528 * The SPI GPIO bindings happened before we managed to
529 * establish that GPIO properties should be named
530 * "foo-gpios" so we have this special kludge for them.
532 { "miso", "gpio-miso", "spi-gpio" },
533 { "mosi", "gpio-mosi", "spi-gpio" },
534 { "sck", "gpio-sck", "spi-gpio" },
538 * The old Freescale bindings use simply "gpios" as name
539 * for the chip select lines rather than "cs-gpios" like
540 * all other SPI hardware. Allow this specifically for
541 * Freescale and PPC devices.
543 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
544 { "cs", "gpios", "fsl,spi" },
545 { "cs", "gpios", "aeroflexgaisler,spictrl" },
547 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
548 { "cs", "gpios", "ibm,ppc4xx-spi" },
551 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
553 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
554 * property without the compulsory "-gpios" suffix.
556 { "fcs,int_n", NULL, "fcs,fusb302" },
559 struct gpio_desc *desc;
560 const char *legacy_id;
564 return ERR_PTR(-ENOENT);
566 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
567 if (strcmp(con_id, gpios[i].con_id))
570 if (gpios[i].compatible &&
571 !of_device_is_compatible(np, gpios[i].compatible))
574 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
575 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
576 if (!gpiod_not_found(desc)) {
577 pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
578 of_node_full_name(np), legacy_id, con_id);
583 return ERR_PTR(-ENOENT);
586 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
589 enum of_gpio_flags *of_flags)
591 struct gpio_desc *desc;
592 const char *legacy_id;
594 if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
595 return ERR_PTR(-ENOENT);
597 if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
598 return ERR_PTR(-ENOENT);
600 if (!con_id || strcmp(con_id, "i2s1-in-sel"))
601 return ERR_PTR(-ENOENT);
604 legacy_id = "i2s1-in-sel-gpio1";
606 legacy_id = "i2s1-in-sel-gpio2";
608 return ERR_PTR(-ENOENT);
610 desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
611 if (!gpiod_not_found(desc))
612 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
613 of_node_full_name(np), legacy_id, con_id);
618 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
621 enum of_gpio_flags *of_flags);
622 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
628 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
629 unsigned int idx, unsigned long *flags)
631 char prop_name[32]; /* 32 is max size of property name */
632 enum of_gpio_flags of_flags;
633 const of_find_gpio_quirk *q;
634 struct gpio_desc *desc;
637 /* Try GPIO property "foo-gpios" and "foo-gpio" */
638 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
640 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
643 snprintf(prop_name, sizeof(prop_name), "%s",
646 desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
648 if (!gpiod_not_found(desc))
652 /* Properly named GPIO was not found, try workarounds */
653 for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
654 desc = (*q)(np, con_id, idx, &of_flags);
659 *flags = of_convert_gpio_flags(of_flags);
665 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
666 * @np: device node to get GPIO from
667 * @chip: GPIO chip whose hog is parsed
668 * @idx: Index of the GPIO to parse
669 * @name: GPIO line name
670 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
671 * of_find_gpio() or of_parse_own_gpio()
672 * @dflags: gpiod_flags - optional GPIO initialization flags
674 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
675 * value on the error condition.
677 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
678 struct gpio_chip *chip,
679 unsigned int idx, const char **name,
680 unsigned long *lflags,
681 enum gpiod_flags *dflags)
683 struct device_node *chip_np;
684 enum of_gpio_flags xlate_flags;
685 struct of_phandle_args gpiospec;
686 struct gpio_desc *desc;
691 chip_np = dev_of_node(&chip->gpiodev->dev);
693 return ERR_PTR(-EINVAL);
696 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
697 *dflags = GPIOD_ASIS;
699 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
703 gpiospec.np = chip_np;
704 gpiospec.args_count = tmp;
706 for (i = 0; i < tmp; i++) {
707 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
713 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
717 *lflags = of_convert_gpio_flags(xlate_flags);
719 if (of_property_read_bool(np, "input"))
721 else if (of_property_read_bool(np, "output-low"))
722 *dflags |= GPIOD_OUT_LOW;
723 else if (of_property_read_bool(np, "output-high"))
724 *dflags |= GPIOD_OUT_HIGH;
726 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
727 desc_to_gpio(desc), np);
728 return ERR_PTR(-EINVAL);
731 if (name && of_property_read_string(np, "line-name", name))
738 * of_gpiochip_add_hog - Add all hogs in a hog device node
739 * @chip: gpio chip to act on
740 * @hog: device node describing the hogs
742 * Returns error if it fails otherwise 0 on success.
744 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
746 enum gpiod_flags dflags;
747 struct gpio_desc *desc;
748 unsigned long lflags;
754 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
758 ret = gpiod_hog(desc, name, lflags, dflags);
762 #ifdef CONFIG_OF_DYNAMIC
771 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
772 * @chip: gpio chip to act on
774 * This is only used by of_gpiochip_add to request/set GPIO initial
776 * It returns error if it fails otherwise 0 on success.
778 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
780 struct device_node *np;
783 for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
784 if (!of_property_read_bool(np, "gpio-hog"))
787 ret = of_gpiochip_add_hog(chip, np);
793 of_node_set_flag(np, OF_POPULATED);
799 #ifdef CONFIG_OF_DYNAMIC
801 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
802 * @chip: gpio chip to act on
803 * @hog: device node describing the hogs
805 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
806 struct device_node *hog)
808 struct gpio_desc *desc;
810 for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
811 if (desc->hog == hog)
812 gpiochip_free_own_desc(desc);
815 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
817 return device_match_of_node(&chip->gpiodev->dev, data);
820 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
822 return gpiochip_find(np, of_gpiochip_match_node);
825 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
828 struct of_reconfig_data *rd = arg;
829 struct gpio_chip *chip;
833 * This only supports adding and removing complete gpio-hog nodes.
834 * Modifying an existing gpio-hog node is not supported (except for
835 * changing its "status" property, which is treated the same as
838 switch (of_reconfig_get_state_change(action, arg)) {
839 case OF_RECONFIG_CHANGE_ADD:
840 if (!of_property_read_bool(rd->dn, "gpio-hog"))
841 return NOTIFY_OK; /* not for us */
843 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
846 chip = of_find_gpiochip_by_node(rd->dn->parent);
848 return NOTIFY_OK; /* not for us */
850 ret = of_gpiochip_add_hog(chip, rd->dn);
852 pr_err("%s: failed to add hogs for %pOF\n", __func__,
854 of_node_clear_flag(rd->dn, OF_POPULATED);
855 return notifier_from_errno(ret);
859 case OF_RECONFIG_CHANGE_REMOVE:
860 if (!of_node_check_flag(rd->dn, OF_POPULATED))
861 return NOTIFY_OK; /* already depopulated */
863 chip = of_find_gpiochip_by_node(rd->dn->parent);
865 return NOTIFY_OK; /* not for us */
867 of_gpiochip_remove_hog(chip, rd->dn);
868 of_node_clear_flag(rd->dn, OF_POPULATED);
875 struct notifier_block gpio_of_notifier = {
876 .notifier_call = of_gpio_notify,
878 #endif /* CONFIG_OF_DYNAMIC */
881 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
882 * @gc: pointer to the gpio_chip structure
883 * @gpiospec: GPIO specifier as found in the device tree
884 * @flags: a flags pointer to fill in
886 * This is simple translation function, suitable for the most 1:1 mapped
887 * GPIO chips. This function performs only one sanity check: whether GPIO
888 * is less than ngpios (that is specified in the gpio_chip).
890 static int of_gpio_simple_xlate(struct gpio_chip *gc,
891 const struct of_phandle_args *gpiospec,
895 * We're discouraging gpio_cells < 2, since that way you'll have to
896 * write your own xlate function (that will have to retrieve the GPIO
897 * number and the flags from a single gpio cell -- this is possible,
898 * but not recommended).
900 if (gc->of_gpio_n_cells < 2) {
905 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
908 if (gpiospec->args[0] >= gc->ngpio)
912 *flags = gpiospec->args[1];
914 return gpiospec->args[0];
917 #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
918 #include <linux/gpio/legacy-of-mm-gpiochip.h>
920 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
921 * @np: device node of the GPIO chip
922 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
923 * @data: driver data to store in the struct gpio_chip
925 * To use this function you should allocate and fill mm_gc with:
927 * 1) In the gpio_chip structure:
928 * - all the callbacks
930 * - of_xlate callback (optional)
932 * 3) In the of_mm_gpio_chip structure:
933 * - save_regs callback (optional)
935 * If succeeded, this function will map bank's memory and will
936 * do all necessary work for you. Then you'll able to use .regs
937 * to manage GPIOs from the callbacks.
939 int of_mm_gpiochip_add_data(struct device_node *np,
940 struct of_mm_gpio_chip *mm_gc,
944 struct gpio_chip *gc = &mm_gc->gc;
946 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
950 mm_gc->regs = of_iomap(np, 0);
956 if (mm_gc->save_regs)
957 mm_gc->save_regs(mm_gc);
959 fwnode_handle_put(mm_gc->gc.fwnode);
960 mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
962 ret = gpiochip_add_data(gc, data);
969 iounmap(mm_gc->regs);
973 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
976 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
979 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
980 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
982 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
984 struct gpio_chip *gc = &mm_gc->gc;
987 iounmap(mm_gc->regs);
990 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
993 #ifdef CONFIG_PINCTRL
994 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
996 struct of_phandle_args pinspec;
997 struct pinctrl_dev *pctldev;
998 struct device_node *np;
1001 static const char group_names_propname[] = "gpio-ranges-group-names";
1002 struct property *group_names;
1004 np = dev_of_node(&chip->gpiodev->dev);
1008 group_names = of_find_property(np, group_names_propname, NULL);
1011 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1016 pctldev = of_pinctrl_get(pinspec.np);
1017 of_node_put(pinspec.np);
1019 return -EPROBE_DEFER;
1021 if (pinspec.args[2]) {
1023 of_property_read_string_index(np,
1024 group_names_propname,
1027 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1032 /* npins != 0: linear range */
1033 ret = gpiochip_add_pin_range(chip,
1034 pinctrl_dev_get_devname(pctldev),
1041 /* npins == 0: special range */
1042 if (pinspec.args[1]) {
1043 pr_err("%pOF: Illegal gpio-range format.\n",
1049 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1050 np, group_names_propname);
1054 ret = of_property_read_string_index(np,
1055 group_names_propname,
1060 if (!strlen(name)) {
1061 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1066 ret = gpiochip_add_pingroup_range(chip, pctldev,
1067 pinspec.args[0], name);
1077 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1080 int of_gpiochip_add(struct gpio_chip *chip)
1082 struct device_node *np;
1085 np = dev_of_node(&chip->gpiodev->dev);
1089 if (!chip->of_xlate) {
1090 chip->of_gpio_n_cells = 2;
1091 chip->of_xlate = of_gpio_simple_xlate;
1094 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1097 ret = of_gpiochip_add_pin_range(chip);
1103 ret = of_gpiochip_scan_gpios(chip);
1110 void of_gpiochip_remove(struct gpio_chip *chip)
1112 of_node_put(dev_of_node(&chip->gpiodev->dev));