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_TLV320AIC3X)
516 { "reset", "gpio-reset", "ti,tlv320aic3x" },
517 { "reset", "gpio-reset", "ti,tlv320aic33" },
518 { "reset", "gpio-reset", "ti,tlv320aic3007" },
519 { "reset", "gpio-reset", "ti,tlv320aic3104" },
520 { "reset", "gpio-reset", "ti,tlv320aic3106" },
522 #if IS_ENABLED(CONFIG_SPI_GPIO)
524 * The SPI GPIO bindings happened before we managed to
525 * establish that GPIO properties should be named
526 * "foo-gpios" so we have this special kludge for them.
528 { "miso", "gpio-miso", "spi-gpio" },
529 { "mosi", "gpio-mosi", "spi-gpio" },
530 { "sck", "gpio-sck", "spi-gpio" },
534 * The old Freescale bindings use simply "gpios" as name
535 * for the chip select lines rather than "cs-gpios" like
536 * all other SPI hardware. Allow this specifically for
537 * Freescale and PPC devices.
539 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
540 { "cs", "gpios", "fsl,spi" },
541 { "cs", "gpios", "aeroflexgaisler,spictrl" },
543 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
544 { "cs", "gpios", "ibm,ppc4xx-spi" },
547 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
549 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
550 * property without the compulsory "-gpios" suffix.
552 { "fcs,int_n", NULL, "fcs,fusb302" },
555 struct gpio_desc *desc;
556 const char *legacy_id;
560 return ERR_PTR(-ENOENT);
562 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
563 if (strcmp(con_id, gpios[i].con_id))
566 if (gpios[i].compatible &&
567 !of_device_is_compatible(np, gpios[i].compatible))
570 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
571 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
572 if (!gpiod_not_found(desc)) {
573 pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
574 of_node_full_name(np), legacy_id, con_id);
579 return ERR_PTR(-ENOENT);
582 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
585 enum of_gpio_flags *of_flags)
587 struct gpio_desc *desc;
588 const char *legacy_id;
590 if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
591 return ERR_PTR(-ENOENT);
593 if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
594 return ERR_PTR(-ENOENT);
596 if (!con_id || strcmp(con_id, "i2s1-in-sel"))
597 return ERR_PTR(-ENOENT);
600 legacy_id = "i2s1-in-sel-gpio1";
602 legacy_id = "i2s1-in-sel-gpio2";
604 return ERR_PTR(-ENOENT);
606 desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
607 if (!gpiod_not_found(desc))
608 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
609 of_node_full_name(np), legacy_id, con_id);
614 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
617 enum of_gpio_flags *of_flags);
618 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
624 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
625 unsigned int idx, unsigned long *flags)
627 char prop_name[32]; /* 32 is max size of property name */
628 enum of_gpio_flags of_flags;
629 const of_find_gpio_quirk *q;
630 struct gpio_desc *desc;
633 /* Try GPIO property "foo-gpios" and "foo-gpio" */
634 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
636 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
639 snprintf(prop_name, sizeof(prop_name), "%s",
642 desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
644 if (!gpiod_not_found(desc))
648 /* Properly named GPIO was not found, try workarounds */
649 for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
650 desc = (*q)(np, con_id, idx, &of_flags);
655 *flags = of_convert_gpio_flags(of_flags);
661 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
662 * @np: device node to get GPIO from
663 * @chip: GPIO chip whose hog is parsed
664 * @idx: Index of the GPIO to parse
665 * @name: GPIO line name
666 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
667 * of_find_gpio() or of_parse_own_gpio()
668 * @dflags: gpiod_flags - optional GPIO initialization flags
670 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
671 * value on the error condition.
673 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
674 struct gpio_chip *chip,
675 unsigned int idx, const char **name,
676 unsigned long *lflags,
677 enum gpiod_flags *dflags)
679 struct device_node *chip_np;
680 enum of_gpio_flags xlate_flags;
681 struct of_phandle_args gpiospec;
682 struct gpio_desc *desc;
687 chip_np = dev_of_node(&chip->gpiodev->dev);
689 return ERR_PTR(-EINVAL);
692 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
693 *dflags = GPIOD_ASIS;
695 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
699 gpiospec.np = chip_np;
700 gpiospec.args_count = tmp;
702 for (i = 0; i < tmp; i++) {
703 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
709 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
713 *lflags = of_convert_gpio_flags(xlate_flags);
715 if (of_property_read_bool(np, "input"))
717 else if (of_property_read_bool(np, "output-low"))
718 *dflags |= GPIOD_OUT_LOW;
719 else if (of_property_read_bool(np, "output-high"))
720 *dflags |= GPIOD_OUT_HIGH;
722 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
723 desc_to_gpio(desc), np);
724 return ERR_PTR(-EINVAL);
727 if (name && of_property_read_string(np, "line-name", name))
734 * of_gpiochip_add_hog - Add all hogs in a hog device node
735 * @chip: gpio chip to act on
736 * @hog: device node describing the hogs
738 * Returns error if it fails otherwise 0 on success.
740 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
742 enum gpiod_flags dflags;
743 struct gpio_desc *desc;
744 unsigned long lflags;
750 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
754 ret = gpiod_hog(desc, name, lflags, dflags);
758 #ifdef CONFIG_OF_DYNAMIC
767 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
768 * @chip: gpio chip to act on
770 * This is only used by of_gpiochip_add to request/set GPIO initial
772 * It returns error if it fails otherwise 0 on success.
774 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
776 struct device_node *np;
779 for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
780 if (!of_property_read_bool(np, "gpio-hog"))
783 ret = of_gpiochip_add_hog(chip, np);
789 of_node_set_flag(np, OF_POPULATED);
795 #ifdef CONFIG_OF_DYNAMIC
797 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
798 * @chip: gpio chip to act on
799 * @hog: device node describing the hogs
801 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
802 struct device_node *hog)
804 struct gpio_desc *desc;
806 for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
807 if (desc->hog == hog)
808 gpiochip_free_own_desc(desc);
811 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
813 return device_match_of_node(&chip->gpiodev->dev, data);
816 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
818 return gpiochip_find(np, of_gpiochip_match_node);
821 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
824 struct of_reconfig_data *rd = arg;
825 struct gpio_chip *chip;
829 * This only supports adding and removing complete gpio-hog nodes.
830 * Modifying an existing gpio-hog node is not supported (except for
831 * changing its "status" property, which is treated the same as
834 switch (of_reconfig_get_state_change(action, arg)) {
835 case OF_RECONFIG_CHANGE_ADD:
836 if (!of_property_read_bool(rd->dn, "gpio-hog"))
837 return NOTIFY_OK; /* not for us */
839 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
842 chip = of_find_gpiochip_by_node(rd->dn->parent);
844 return NOTIFY_OK; /* not for us */
846 ret = of_gpiochip_add_hog(chip, rd->dn);
848 pr_err("%s: failed to add hogs for %pOF\n", __func__,
850 of_node_clear_flag(rd->dn, OF_POPULATED);
851 return notifier_from_errno(ret);
855 case OF_RECONFIG_CHANGE_REMOVE:
856 if (!of_node_check_flag(rd->dn, OF_POPULATED))
857 return NOTIFY_OK; /* already depopulated */
859 chip = of_find_gpiochip_by_node(rd->dn->parent);
861 return NOTIFY_OK; /* not for us */
863 of_gpiochip_remove_hog(chip, rd->dn);
864 of_node_clear_flag(rd->dn, OF_POPULATED);
871 struct notifier_block gpio_of_notifier = {
872 .notifier_call = of_gpio_notify,
874 #endif /* CONFIG_OF_DYNAMIC */
877 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
878 * @gc: pointer to the gpio_chip structure
879 * @gpiospec: GPIO specifier as found in the device tree
880 * @flags: a flags pointer to fill in
882 * This is simple translation function, suitable for the most 1:1 mapped
883 * GPIO chips. This function performs only one sanity check: whether GPIO
884 * is less than ngpios (that is specified in the gpio_chip).
886 static int of_gpio_simple_xlate(struct gpio_chip *gc,
887 const struct of_phandle_args *gpiospec,
891 * We're discouraging gpio_cells < 2, since that way you'll have to
892 * write your own xlate function (that will have to retrieve the GPIO
893 * number and the flags from a single gpio cell -- this is possible,
894 * but not recommended).
896 if (gc->of_gpio_n_cells < 2) {
901 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
904 if (gpiospec->args[0] >= gc->ngpio)
908 *flags = gpiospec->args[1];
910 return gpiospec->args[0];
913 #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
914 #include <linux/gpio/legacy-of-mm-gpiochip.h>
916 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
917 * @np: device node of the GPIO chip
918 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
919 * @data: driver data to store in the struct gpio_chip
921 * To use this function you should allocate and fill mm_gc with:
923 * 1) In the gpio_chip structure:
924 * - all the callbacks
926 * - of_xlate callback (optional)
928 * 3) In the of_mm_gpio_chip structure:
929 * - save_regs callback (optional)
931 * If succeeded, this function will map bank's memory and will
932 * do all necessary work for you. Then you'll able to use .regs
933 * to manage GPIOs from the callbacks.
935 int of_mm_gpiochip_add_data(struct device_node *np,
936 struct of_mm_gpio_chip *mm_gc,
940 struct gpio_chip *gc = &mm_gc->gc;
942 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
946 mm_gc->regs = of_iomap(np, 0);
952 if (mm_gc->save_regs)
953 mm_gc->save_regs(mm_gc);
955 fwnode_handle_put(mm_gc->gc.fwnode);
956 mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
958 ret = gpiochip_add_data(gc, data);
965 iounmap(mm_gc->regs);
969 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
972 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
975 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
976 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
978 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
980 struct gpio_chip *gc = &mm_gc->gc;
983 iounmap(mm_gc->regs);
986 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
989 #ifdef CONFIG_PINCTRL
990 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
992 struct of_phandle_args pinspec;
993 struct pinctrl_dev *pctldev;
994 struct device_node *np;
997 static const char group_names_propname[] = "gpio-ranges-group-names";
998 struct property *group_names;
1000 np = dev_of_node(&chip->gpiodev->dev);
1004 group_names = of_find_property(np, group_names_propname, NULL);
1007 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1012 pctldev = of_pinctrl_get(pinspec.np);
1013 of_node_put(pinspec.np);
1015 return -EPROBE_DEFER;
1017 if (pinspec.args[2]) {
1019 of_property_read_string_index(np,
1020 group_names_propname,
1023 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1028 /* npins != 0: linear range */
1029 ret = gpiochip_add_pin_range(chip,
1030 pinctrl_dev_get_devname(pctldev),
1037 /* npins == 0: special range */
1038 if (pinspec.args[1]) {
1039 pr_err("%pOF: Illegal gpio-range format.\n",
1045 pr_err("%pOF: GPIO group range requested but no %s property.\n",
1046 np, group_names_propname);
1050 ret = of_property_read_string_index(np,
1051 group_names_propname,
1056 if (!strlen(name)) {
1057 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1062 ret = gpiochip_add_pingroup_range(chip, pctldev,
1063 pinspec.args[0], name);
1073 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1076 int of_gpiochip_add(struct gpio_chip *chip)
1078 struct device_node *np;
1081 np = dev_of_node(&chip->gpiodev->dev);
1085 if (!chip->of_xlate) {
1086 chip->of_gpio_n_cells = 2;
1087 chip->of_xlate = of_gpio_simple_xlate;
1090 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1093 ret = of_gpiochip_add_pin_range(chip);
1099 ret = of_gpiochip_scan_gpios(chip);
1106 void of_gpiochip_remove(struct gpio_chip *chip)
1108 of_node_put(dev_of_node(&chip->gpiodev->dev));