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;
164 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
165 * @np: device node to get GPIO from
166 * @propname: property name containing gpio specifier(s)
167 * @index: index of the GPIO
168 * @flags: a flags pointer to fill in
170 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
171 * value on the error condition. If @flags is not NULL the function also fills
172 * in flags for the GPIO.
174 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
175 const char *propname, int index, enum of_gpio_flags *flags)
177 struct of_phandle_args gpiospec;
178 struct gpio_chip *chip;
179 struct gpio_desc *desc;
182 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
185 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
186 __func__, propname, np, index);
190 chip = of_find_gpiochip_by_xlate(&gpiospec);
192 desc = ERR_PTR(-EPROBE_DEFER);
196 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
201 of_gpio_flags_quirks(np, propname, flags, index);
203 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
204 __func__, propname, np, index,
205 PTR_ERR_OR_ZERO(desc));
208 of_node_put(gpiospec.np);
213 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
214 int index, enum of_gpio_flags *flags)
216 struct gpio_desc *desc;
218 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
221 return PTR_ERR(desc);
223 return desc_to_gpio(desc);
225 EXPORT_SYMBOL(of_get_named_gpio_flags);
228 * The SPI GPIO bindings happened before we managed to establish that GPIO
229 * properties should be named "foo-gpios" so we have this special kludge for
232 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
233 enum of_gpio_flags *of_flags)
235 char prop_name[32]; /* 32 is max size of property name */
236 struct device_node *np = dev->of_node;
237 struct gpio_desc *desc;
240 * Hopefully the compiler stubs the rest of the function if this
243 if (!IS_ENABLED(CONFIG_SPI_MASTER))
244 return ERR_PTR(-ENOENT);
246 /* Allow this specifically for "spi-gpio" devices */
247 if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
248 return ERR_PTR(-ENOENT);
250 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
251 snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
253 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
258 * Some regulator bindings happened before we managed to establish that GPIO
259 * properties should be named "foo-gpios" so we have this special kludge for
262 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
263 enum of_gpio_flags *of_flags)
265 /* These are the connection IDs we accept as legacy GPIO phandles */
266 const char *whitelist[] = {
267 "wlf,ldoena", /* Arizona */
268 "wlf,ldo1ena", /* WM8994 */
269 "wlf,ldo2ena", /* WM8994 */
271 struct device_node *np = dev->of_node;
272 struct gpio_desc *desc;
275 if (!IS_ENABLED(CONFIG_REGULATOR))
276 return ERR_PTR(-ENOENT);
279 return ERR_PTR(-ENOENT);
281 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
283 return ERR_PTR(-ENOENT);
285 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
289 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
290 unsigned int idx, unsigned long *flags)
292 char prop_name[32]; /* 32 is max size of property name */
293 enum of_gpio_flags of_flags;
294 struct gpio_desc *desc;
297 /* Try GPIO property "foo-gpios" and "foo-gpio" */
298 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
300 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
303 snprintf(prop_name, sizeof(prop_name), "%s",
306 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
309 * -EPROBE_DEFER in our case means that we found a
310 * valid GPIO property, but no controller has been
313 * This means we don't need to look any further for
314 * alternate name conventions, and we should really
315 * preserve the return code for our user to be able to
316 * retry probing later.
318 if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
321 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
325 /* Special handling for SPI GPIOs if used */
327 desc = of_find_spi_gpio(dev, con_id, &of_flags);
329 /* Special handling for regulator GPIOs if used */
330 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
331 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
336 if (of_flags & OF_GPIO_ACTIVE_LOW)
337 *flags |= GPIO_ACTIVE_LOW;
339 if (of_flags & OF_GPIO_SINGLE_ENDED) {
340 if (of_flags & OF_GPIO_OPEN_DRAIN)
341 *flags |= GPIO_OPEN_DRAIN;
343 *flags |= GPIO_OPEN_SOURCE;
346 if (of_flags & OF_GPIO_TRANSITORY)
347 *flags |= GPIO_TRANSITORY;
349 if (of_flags & OF_GPIO_PULL_UP)
350 *flags |= GPIO_PULL_UP;
351 if (of_flags & OF_GPIO_PULL_DOWN)
352 *flags |= GPIO_PULL_DOWN;
358 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
359 * @np: device node to get GPIO from
360 * @chip: GPIO chip whose hog is parsed
361 * @idx: Index of the GPIO to parse
362 * @name: GPIO line name
363 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
364 * of_find_gpio() or of_parse_own_gpio()
365 * @dflags: gpiod_flags - optional GPIO initialization flags
367 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
368 * value on the error condition.
370 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
371 struct gpio_chip *chip,
372 unsigned int idx, const char **name,
373 unsigned long *lflags,
374 enum gpiod_flags *dflags)
376 struct device_node *chip_np;
377 enum of_gpio_flags xlate_flags;
378 struct of_phandle_args gpiospec;
379 struct gpio_desc *desc;
384 chip_np = chip->of_node;
386 return ERR_PTR(-EINVAL);
389 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
392 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
396 gpiospec.np = chip_np;
397 gpiospec.args_count = tmp;
399 for (i = 0; i < tmp; i++) {
400 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
406 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
410 if (xlate_flags & OF_GPIO_ACTIVE_LOW)
411 *lflags |= GPIO_ACTIVE_LOW;
412 if (xlate_flags & OF_GPIO_TRANSITORY)
413 *lflags |= GPIO_TRANSITORY;
415 if (of_property_read_bool(np, "input"))
417 else if (of_property_read_bool(np, "output-low"))
418 *dflags |= GPIOD_OUT_LOW;
419 else if (of_property_read_bool(np, "output-high"))
420 *dflags |= GPIOD_OUT_HIGH;
422 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
423 desc_to_gpio(desc), np);
424 return ERR_PTR(-EINVAL);
427 if (name && of_property_read_string(np, "line-name", name))
434 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
435 * @chip: gpio chip to act on
437 * This is only used by of_gpiochip_add to request/set GPIO initial
439 * It returns error if it fails otherwise 0 on success.
441 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
443 struct gpio_desc *desc = NULL;
444 struct device_node *np;
446 unsigned long lflags;
447 enum gpiod_flags dflags;
451 for_each_available_child_of_node(chip->of_node, np) {
452 if (!of_property_read_bool(np, "gpio-hog"))
456 desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
461 ret = gpiod_hog(desc, name, lflags, dflags);
473 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
474 * @gc: pointer to the gpio_chip structure
475 * @gpiospec: GPIO specifier as found in the device tree
476 * @flags: a flags pointer to fill in
478 * This is simple translation function, suitable for the most 1:1 mapped
479 * GPIO chips. This function performs only one sanity check: whether GPIO
480 * is less than ngpios (that is specified in the gpio_chip).
482 int of_gpio_simple_xlate(struct gpio_chip *gc,
483 const struct of_phandle_args *gpiospec, u32 *flags)
486 * We're discouraging gpio_cells < 2, since that way you'll have to
487 * write your own xlate function (that will have to retrieve the GPIO
488 * number and the flags from a single gpio cell -- this is possible,
489 * but not recommended).
491 if (gc->of_gpio_n_cells < 2) {
496 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
499 if (gpiospec->args[0] >= gc->ngpio)
503 *flags = gpiospec->args[1];
505 return gpiospec->args[0];
507 EXPORT_SYMBOL(of_gpio_simple_xlate);
510 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
511 * @np: device node of the GPIO chip
512 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
513 * @data: driver data to store in the struct gpio_chip
515 * To use this function you should allocate and fill mm_gc with:
517 * 1) In the gpio_chip structure:
518 * - all the callbacks
520 * - of_xlate callback (optional)
522 * 3) In the of_mm_gpio_chip structure:
523 * - save_regs callback (optional)
525 * If succeeded, this function will map bank's memory and will
526 * do all necessary work for you. Then you'll able to use .regs
527 * to manage GPIOs from the callbacks.
529 int of_mm_gpiochip_add_data(struct device_node *np,
530 struct of_mm_gpio_chip *mm_gc,
534 struct gpio_chip *gc = &mm_gc->gc;
536 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
540 mm_gc->regs = of_iomap(np, 0);
546 if (mm_gc->save_regs)
547 mm_gc->save_regs(mm_gc);
549 mm_gc->gc.of_node = np;
551 ret = gpiochip_add_data(gc, data);
557 iounmap(mm_gc->regs);
561 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
564 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
567 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
568 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
570 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
572 struct gpio_chip *gc = &mm_gc->gc;
578 iounmap(mm_gc->regs);
581 EXPORT_SYMBOL(of_mm_gpiochip_remove);
583 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
587 struct device_node *np = chip->of_node;
589 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
590 if (len < 0 || len % 2 != 0)
593 for (i = 0; i < len; i += 2) {
594 of_property_read_u32_index(np, "gpio-reserved-ranges",
596 of_property_read_u32_index(np, "gpio-reserved-ranges",
598 if (start >= chip->ngpio || start + count >= chip->ngpio)
601 bitmap_clear(chip->valid_mask, start, count);
605 #ifdef CONFIG_PINCTRL
606 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
608 struct device_node *np = chip->of_node;
609 struct of_phandle_args pinspec;
610 struct pinctrl_dev *pctldev;
613 static const char group_names_propname[] = "gpio-ranges-group-names";
614 struct property *group_names;
619 group_names = of_find_property(np, group_names_propname, NULL);
622 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
627 pctldev = of_pinctrl_get(pinspec.np);
628 of_node_put(pinspec.np);
630 return -EPROBE_DEFER;
632 if (pinspec.args[2]) {
634 of_property_read_string_index(np,
635 group_names_propname,
638 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
643 /* npins != 0: linear range */
644 ret = gpiochip_add_pin_range(chip,
645 pinctrl_dev_get_devname(pctldev),
652 /* npins == 0: special range */
653 if (pinspec.args[1]) {
654 pr_err("%pOF: Illegal gpio-range format.\n",
660 pr_err("%pOF: GPIO group range requested but no %s property.\n",
661 np, group_names_propname);
665 ret = of_property_read_string_index(np,
666 group_names_propname,
672 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
677 ret = gpiochip_add_pingroup_range(chip, pctldev,
678 pinspec.args[0], name);
688 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
691 int of_gpiochip_add(struct gpio_chip *chip)
698 if (!chip->of_xlate) {
699 chip->of_gpio_n_cells = 2;
700 chip->of_xlate = of_gpio_simple_xlate;
703 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
706 of_gpiochip_init_valid_mask(chip);
708 status = of_gpiochip_add_pin_range(chip);
712 /* If the chip defines names itself, these take precedence */
714 devprop_gpiochip_set_names(chip,
715 of_fwnode_handle(chip->of_node));
717 of_node_get(chip->of_node);
719 status = of_gpiochip_scan_gpios(chip);
721 of_node_put(chip->of_node);
722 gpiochip_remove_pin_ranges(chip);
728 void of_gpiochip_remove(struct gpio_chip *chip)
730 gpiochip_remove_pin_ranges(chip);
731 of_node_put(chip->of_node);