1 // SPDX-License-Identifier: GPL-2.0+
3 * OF helpers for the GPIO API
5 * Copyright (c) 2007-2008 MontaVista Software, Inc.
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>
25 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
27 struct of_phandle_args *gpiospec = data;
29 return chip->gpiodev->dev.of_node == gpiospec->np &&
31 chip->of_xlate(chip, gpiospec, NULL) >= 0;
34 static struct gpio_chip *of_find_gpiochip_by_xlate(
35 struct of_phandle_args *gpiospec)
37 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
40 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
41 struct of_phandle_args *gpiospec,
42 enum of_gpio_flags *flags)
46 if (chip->of_gpio_n_cells != gpiospec->args_count)
47 return ERR_PTR(-EINVAL);
49 ret = chip->of_xlate(chip, gpiospec, flags);
53 return gpiochip_get_desc(chip, ret);
56 static void of_gpio_flags_quirks(struct device_node *np,
58 enum of_gpio_flags *flags,
62 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
64 if (IS_ENABLED(CONFIG_MMC)) {
66 * Active low is the default according to the
67 * SDHCI specification and the device tree
68 * bindings. However the code in the current
69 * kernel was written such that the phandle
70 * flags were always respected, and "cd-inverted"
71 * would invert the flag from the device phandle.
73 if (!strcmp(propname, "cd-gpios")) {
74 if (of_property_read_bool(np, "cd-inverted"))
75 *flags ^= OF_GPIO_ACTIVE_LOW;
77 if (!strcmp(propname, "wp-gpios")) {
78 if (of_property_read_bool(np, "wp-inverted"))
79 *flags ^= OF_GPIO_ACTIVE_LOW;
83 * Some GPIO fixed regulator quirks.
84 * Note that active low is the default.
86 if (IS_ENABLED(CONFIG_REGULATOR) &&
87 (of_device_is_compatible(np, "regulator-fixed") ||
88 of_device_is_compatible(np, "reg-fixed-voltage") ||
89 (!(strcmp(propname, "enable-gpio") &&
90 strcmp(propname, "enable-gpios")) &&
91 of_device_is_compatible(np, "regulator-gpio")))) {
93 * The regulator GPIO handles are specified such that the
94 * presence or absence of "enable-active-high" solely controls
95 * the polarity of the GPIO line. Any phandle flags must
96 * be actively ignored.
98 if (*flags & OF_GPIO_ACTIVE_LOW) {
99 pr_warn("%s GPIO handle specifies active low - ignored\n",
100 of_node_full_name(np));
101 *flags &= ~OF_GPIO_ACTIVE_LOW;
103 if (!of_property_read_bool(np, "enable-active-high"))
104 *flags |= OF_GPIO_ACTIVE_LOW;
107 * Legacy open drain handling for fixed voltage regulators.
109 if (IS_ENABLED(CONFIG_REGULATOR) &&
110 of_device_is_compatible(np, "reg-fixed-voltage") &&
111 of_property_read_bool(np, "gpio-open-drain")) {
112 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
113 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
114 of_node_full_name(np));
118 * Legacy handling of SPI active high chip select. If we have a
119 * property named "cs-gpios" we need to inspect the child node
120 * to determine if the flags should have inverted semantics.
122 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
123 of_property_read_bool(np, "cs-gpios")) {
124 struct device_node *child;
128 for_each_child_of_node(np, child) {
129 ret = of_property_read_u32(child, "reg", &cs);
134 * SPI children have active low chip selects
135 * by default. This can be specified negatively
136 * by just omitting "spi-cs-high" in the
137 * device node, or actively by tagging on
138 * GPIO_ACTIVE_LOW as flag in the device
139 * tree. If the line is simultaneously
140 * tagged as active low in the device tree
141 * and has the "spi-cs-high" set, we get a
142 * conflict and the "spi-cs-high" flag will
145 if (of_property_read_bool(child, "spi-cs-high")) {
146 if (*flags & OF_GPIO_ACTIVE_LOW) {
147 pr_warn("%s GPIO handle specifies active low - ignored\n",
148 of_node_full_name(child));
149 *flags &= ~OF_GPIO_ACTIVE_LOW;
152 if (!(*flags & OF_GPIO_ACTIVE_LOW))
153 pr_info("%s enforce active low on chipselect handle\n",
154 of_node_full_name(child));
155 *flags |= OF_GPIO_ACTIVE_LOW;
163 /* Legacy handling of stmmac's active-low PHY reset line */
164 if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
165 !strcmp(propname, "snps,reset-gpio") &&
166 of_property_read_bool(np, "snps,reset-active-low"))
167 *flags |= OF_GPIO_ACTIVE_LOW;
171 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
172 * @np: device node to get GPIO from
173 * @propname: property name containing gpio specifier(s)
174 * @index: index of the GPIO
175 * @flags: a flags pointer to fill in
177 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
178 * value on the error condition. If @flags is not NULL the function also fills
179 * in flags for the GPIO.
181 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
182 const char *propname, int index, enum of_gpio_flags *flags)
184 struct of_phandle_args gpiospec;
185 struct gpio_chip *chip;
186 struct gpio_desc *desc;
189 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
192 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
193 __func__, propname, np, index);
197 chip = of_find_gpiochip_by_xlate(&gpiospec);
199 desc = ERR_PTR(-EPROBE_DEFER);
203 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
208 of_gpio_flags_quirks(np, propname, flags, index);
210 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
211 __func__, propname, np, index,
212 PTR_ERR_OR_ZERO(desc));
215 of_node_put(gpiospec.np);
220 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
221 int index, enum of_gpio_flags *flags)
223 struct gpio_desc *desc;
225 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
228 return PTR_ERR(desc);
230 return desc_to_gpio(desc);
232 EXPORT_SYMBOL(of_get_named_gpio_flags);
235 * The SPI GPIO bindings happened before we managed to establish that GPIO
236 * properties should be named "foo-gpios" so we have this special kludge for
239 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
240 enum of_gpio_flags *of_flags)
242 char prop_name[32]; /* 32 is max size of property name */
243 struct device_node *np = dev->of_node;
244 struct gpio_desc *desc;
247 * Hopefully the compiler stubs the rest of the function if this
250 if (!IS_ENABLED(CONFIG_SPI_MASTER))
251 return ERR_PTR(-ENOENT);
253 /* Allow this specifically for "spi-gpio" devices */
254 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
255 return ERR_PTR(-ENOENT);
257 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
258 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
260 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
265 * The old Freescale bindings use simply "gpios" as name for the chip select
266 * lines rather than "cs-gpios" like all other SPI hardware. Account for this
267 * with a special quirk.
269 static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
272 unsigned long *flags)
274 struct device_node *np = dev->of_node;
276 if (!IS_ENABLED(CONFIG_SPI_MASTER))
277 return ERR_PTR(-ENOENT);
279 /* Allow this specifically for Freescale devices */
280 if (!of_device_is_compatible(np, "fsl,spi") &&
281 !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
282 return ERR_PTR(-ENOENT);
283 /* Allow only if asking for "cs-gpios" */
284 if (!con_id || strcmp(con_id, "cs"))
285 return ERR_PTR(-ENOENT);
288 * While all other SPI controllers use "cs-gpios" the Freescale
289 * uses just "gpios" so translate to that when "cs-gpios" is
292 return of_find_gpio(dev, NULL, idx, flags);
296 * Some regulator bindings happened before we managed to establish that GPIO
297 * properties should be named "foo-gpios" so we have this special kludge for
300 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
301 enum of_gpio_flags *of_flags)
303 /* These are the connection IDs we accept as legacy GPIO phandles */
304 const char *whitelist[] = {
305 "wlf,ldoena", /* Arizona */
306 "wlf,ldo1ena", /* WM8994 */
307 "wlf,ldo2ena", /* WM8994 */
309 struct device_node *np = dev->of_node;
310 struct gpio_desc *desc;
313 if (!IS_ENABLED(CONFIG_REGULATOR))
314 return ERR_PTR(-ENOENT);
317 return ERR_PTR(-ENOENT);
319 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
321 return ERR_PTR(-ENOENT);
323 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
327 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
328 unsigned int idx, unsigned long *flags)
330 char prop_name[32]; /* 32 is max size of property name */
331 enum of_gpio_flags of_flags;
332 struct gpio_desc *desc;
335 /* Try GPIO property "foo-gpios" and "foo-gpio" */
336 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
338 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
341 snprintf(prop_name, sizeof(prop_name), "%s",
344 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
347 * -EPROBE_DEFER in our case means that we found a
348 * valid GPIO property, but no controller has been
351 * This means we don't need to look any further for
352 * alternate name conventions, and we should really
353 * preserve the return code for our user to be able to
354 * retry probing later.
356 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
359 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
363 /* Special handling for SPI GPIOs if used */
365 desc = of_find_spi_gpio(dev, con_id, &of_flags);
367 /* This quirk looks up flags and all */
368 desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
373 /* Special handling for regulator GPIOs if used */
374 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
375 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
380 if (of_flags & OF_GPIO_ACTIVE_LOW)
381 *flags |= GPIO_ACTIVE_LOW;
383 if (of_flags & OF_GPIO_SINGLE_ENDED) {
384 if (of_flags & OF_GPIO_OPEN_DRAIN)
385 *flags |= GPIO_OPEN_DRAIN;
387 *flags |= GPIO_OPEN_SOURCE;
390 if (of_flags & OF_GPIO_TRANSITORY)
391 *flags |= GPIO_TRANSITORY;
393 if (of_flags & OF_GPIO_PULL_UP)
394 *flags |= GPIO_PULL_UP;
395 if (of_flags & OF_GPIO_PULL_DOWN)
396 *flags |= GPIO_PULL_DOWN;
402 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
403 * @np: device node to get GPIO from
404 * @chip: GPIO chip whose hog is parsed
405 * @idx: Index of the GPIO to parse
406 * @name: GPIO line name
407 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
408 * of_find_gpio() or of_parse_own_gpio()
409 * @dflags: gpiod_flags - optional GPIO initialization flags
411 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
412 * value on the error condition.
414 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
415 struct gpio_chip *chip,
416 unsigned int idx, const char **name,
417 unsigned long *lflags,
418 enum gpiod_flags *dflags)
420 struct device_node *chip_np;
421 enum of_gpio_flags xlate_flags;
422 struct of_phandle_args gpiospec;
423 struct gpio_desc *desc;
428 chip_np = chip->of_node;
430 return ERR_PTR(-EINVAL);
433 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
436 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
440 gpiospec.np = chip_np;
441 gpiospec.args_count = tmp;
443 for (i = 0; i < tmp; i++) {
444 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
450 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
454 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
455 *lflags |= GPIO_ACTIVE_LOW;
456 if (xlate_flags & OF_GPIO_TRANSITORY)
457 *lflags |= GPIO_TRANSITORY;
459 if (of_property_read_bool(np, "input"))
461 else if (of_property_read_bool(np, "output-low"))
462 *dflags |= GPIOD_OUT_LOW;
463 else if (of_property_read_bool(np, "output-high"))
464 *dflags |= GPIOD_OUT_HIGH;
466 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
467 desc_to_gpio(desc), np);
468 return ERR_PTR(-EINVAL);
471 if (name && of_property_read_string(np, "line-name", name))
478 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
479 * @chip: gpio chip to act on
481 * This is only used by of_gpiochip_add to request/set GPIO initial
483 * It returns error if it fails otherwise 0 on success.
485 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
487 struct gpio_desc *desc = NULL;
488 struct device_node *np;
490 unsigned long lflags;
491 enum gpiod_flags dflags;
495 for_each_available_child_of_node(chip->of_node, np) {
496 if (!of_property_read_bool(np, "gpio-hog"))
500 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
505 ret = gpiod_hog(desc, name, lflags, dflags);
517 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
518 * @gc: pointer to the gpio_chip structure
519 * @gpiospec: GPIO specifier as found in the device tree
520 * @flags: a flags pointer to fill in
522 * This is simple translation function, suitable for the most 1:1 mapped
523 * GPIO chips. This function performs only one sanity check: whether GPIO
524 * is less than ngpios (that is specified in the gpio_chip).
526 int of_gpio_simple_xlate(struct gpio_chip *gc,
527 const struct of_phandle_args *gpiospec, u32 *flags)
530 * We're discouraging gpio_cells < 2, since that way you'll have to
531 * write your own xlate function (that will have to retrieve the GPIO
532 * number and the flags from a single gpio cell -- this is possible,
533 * but not recommended).
535 if (gc->of_gpio_n_cells < 2) {
540 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
543 if (gpiospec->args[0] >= gc->ngpio)
547 *flags = gpiospec->args[1];
549 return gpiospec->args[0];
551 EXPORT_SYMBOL(of_gpio_simple_xlate);
554 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
555 * @np: device node of the GPIO chip
556 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
557 * @data: driver data to store in the struct gpio_chip
559 * To use this function you should allocate and fill mm_gc with:
561 * 1) In the gpio_chip structure:
562 * - all the callbacks
564 * - of_xlate callback (optional)
566 * 3) In the of_mm_gpio_chip structure:
567 * - save_regs callback (optional)
569 * If succeeded, this function will map bank's memory and will
570 * do all necessary work for you. Then you'll able to use .regs
571 * to manage GPIOs from the callbacks.
573 int of_mm_gpiochip_add_data(struct device_node *np,
574 struct of_mm_gpio_chip *mm_gc,
578 struct gpio_chip *gc = &mm_gc->gc;
580 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
584 mm_gc->regs = of_iomap(np, 0);
590 if (mm_gc->save_regs)
591 mm_gc->save_regs(mm_gc);
593 mm_gc->gc.of_node = np;
595 ret = gpiochip_add_data(gc, data);
601 iounmap(mm_gc->regs);
605 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
608 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
611 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
612 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
614 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
616 struct gpio_chip *gc = &mm_gc->gc;
622 iounmap(mm_gc->regs);
625 EXPORT_SYMBOL(of_mm_gpiochip_remove);
627 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
631 struct device_node *np = chip->of_node;
633 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
634 if (len < 0 || len % 2 != 0)
637 for (i = 0; i < len; i += 2) {
638 of_property_read_u32_index(np, "gpio-reserved-ranges",
640 of_property_read_u32_index(np, "gpio-reserved-ranges",
642 if (start >= chip->ngpio || start + count >= chip->ngpio)
645 bitmap_clear(chip->valid_mask, start, count);
649 #ifdef CONFIG_PINCTRL
650 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
652 struct device_node *np = chip->of_node;
653 struct of_phandle_args pinspec;
654 struct pinctrl_dev *pctldev;
657 static const char group_names_propname[] = "gpio-ranges-group-names";
658 struct property *group_names;
663 group_names = of_find_property(np, group_names_propname, NULL);
666 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
671 pctldev = of_pinctrl_get(pinspec.np);
672 of_node_put(pinspec.np);
674 return -EPROBE_DEFER;
676 if (pinspec.args[2]) {
678 of_property_read_string_index(np,
679 group_names_propname,
682 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
687 /* npins != 0: linear range */
688 ret = gpiochip_add_pin_range(chip,
689 pinctrl_dev_get_devname(pctldev),
696 /* npins == 0: special range */
697 if (pinspec.args[1]) {
698 pr_err("%pOF: Illegal gpio-range format.\n",
704 pr_err("%pOF: GPIO group range requested but no %s property.\n",
705 np, group_names_propname);
709 ret = of_property_read_string_index(np,
710 group_names_propname,
716 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
721 ret = gpiochip_add_pingroup_range(chip, pctldev,
722 pinspec.args[0], name);
732 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
735 int of_gpiochip_add(struct gpio_chip *chip)
742 if (!chip->of_xlate) {
743 chip->of_gpio_n_cells = 2;
744 chip->of_xlate = of_gpio_simple_xlate;
747 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
750 of_gpiochip_init_valid_mask(chip);
752 status = of_gpiochip_add_pin_range(chip);
756 /* If the chip defines names itself, these take precedence */
758 devprop_gpiochip_set_names(chip,
759 of_fwnode_handle(chip->of_node));
761 of_node_get(chip->of_node);
763 status = of_gpiochip_scan_gpios(chip);
765 of_node_put(chip->of_node);
766 gpiochip_remove_pin_ranges(chip);
772 void of_gpiochip_remove(struct gpio_chip *chip)
774 gpiochip_remove_pin_ranges(chip);
775 of_node_put(chip->of_node);