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>
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 of_device_is_compatible(np
, "regulator-gpio"))) {
91 * The regulator GPIO handles are specified such that the
92 * presence or absence of "enable-active-high" solely controls
93 * the polarity of the GPIO line. Any phandle flags must
94 * be actively ignored.
96 if (*flags
& OF_GPIO_ACTIVE_LOW
) {
97 pr_warn("%s GPIO handle specifies active low - ignored\n",
98 of_node_full_name(np
));
99 *flags
&= ~OF_GPIO_ACTIVE_LOW
;
101 if (!of_property_read_bool(np
, "enable-active-high"))
102 *flags
|= OF_GPIO_ACTIVE_LOW
;
105 * Legacy open drain handling for fixed voltage regulators.
107 if (IS_ENABLED(CONFIG_REGULATOR
) &&
108 of_device_is_compatible(np
, "reg-fixed-voltage") &&
109 of_property_read_bool(np
, "gpio-open-drain")) {
110 *flags
|= (OF_GPIO_SINGLE_ENDED
| OF_GPIO_OPEN_DRAIN
);
111 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
112 of_node_full_name(np
));
116 * Legacy handling of SPI active high chip select. If we have a
117 * property named "cs-gpios" we need to inspect the child node
118 * to determine if the flags should have inverted semantics.
120 if (IS_ENABLED(CONFIG_SPI_MASTER
) &&
121 of_property_read_bool(np
, "cs-gpios")) {
122 struct device_node
*child
;
126 for_each_child_of_node(np
, child
) {
127 ret
= of_property_read_u32(child
, "reg", &cs
);
132 * SPI children have active low chip selects
133 * by default. This can be specified negatively
134 * by just omitting "spi-cs-high" in the
135 * device node, or actively by tagging on
136 * GPIO_ACTIVE_LOW as flag in the device
137 * tree. If the line is simultaneously
138 * tagged as active low in the device tree
139 * and has the "spi-cs-high" set, we get a
140 * conflict and the "spi-cs-high" flag will
143 if (of_property_read_bool(np
, "spi-cs-high")) {
144 if (*flags
& OF_GPIO_ACTIVE_LOW
) {
145 pr_warn("%s GPIO handle specifies active low - ignored\n",
146 of_node_full_name(np
));
147 *flags
&= ~OF_GPIO_ACTIVE_LOW
;
150 if (!(*flags
& OF_GPIO_ACTIVE_LOW
))
151 pr_info("%s enforce active low on chipselect handle\n",
152 of_node_full_name(np
));
153 *flags
|= OF_GPIO_ACTIVE_LOW
;
162 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
163 * @np: device node to get GPIO from
164 * @propname: property name containing gpio specifier(s)
165 * @index: index of the GPIO
166 * @flags: a flags pointer to fill in
168 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
169 * value on the error condition. If @flags is not NULL the function also fills
170 * in flags for the GPIO.
172 struct gpio_desc
*of_get_named_gpiod_flags(struct device_node
*np
,
173 const char *propname
, int index
, enum of_gpio_flags
*flags
)
175 struct of_phandle_args gpiospec
;
176 struct gpio_chip
*chip
;
177 struct gpio_desc
*desc
;
180 ret
= of_parse_phandle_with_args_map(np
, propname
, "gpio", index
,
183 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
184 __func__
, propname
, np
, index
);
188 chip
= of_find_gpiochip_by_xlate(&gpiospec
);
190 desc
= ERR_PTR(-EPROBE_DEFER
);
194 desc
= of_xlate_and_get_gpiod_flags(chip
, &gpiospec
, flags
);
199 of_gpio_flags_quirks(np
, propname
, flags
, index
);
201 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
202 __func__
, propname
, np
, index
,
203 PTR_ERR_OR_ZERO(desc
));
206 of_node_put(gpiospec
.np
);
211 int of_get_named_gpio_flags(struct device_node
*np
, const char *list_name
,
212 int index
, enum of_gpio_flags
*flags
)
214 struct gpio_desc
*desc
;
216 desc
= of_get_named_gpiod_flags(np
, list_name
, index
, flags
);
219 return PTR_ERR(desc
);
221 return desc_to_gpio(desc
);
223 EXPORT_SYMBOL(of_get_named_gpio_flags
);
226 * The SPI GPIO bindings happened before we managed to establish that GPIO
227 * properties should be named "foo-gpios" so we have this special kludge for
230 static struct gpio_desc
*of_find_spi_gpio(struct device
*dev
, const char *con_id
,
231 enum of_gpio_flags
*of_flags
)
233 char prop_name
[32]; /* 32 is max size of property name */
234 struct device_node
*np
= dev
->of_node
;
235 struct gpio_desc
*desc
;
238 * Hopefully the compiler stubs the rest of the function if this
241 if (!IS_ENABLED(CONFIG_SPI_MASTER
))
242 return ERR_PTR(-ENOENT
);
244 /* Allow this specifically for "spi-gpio" devices */
245 if (!of_device_is_compatible(np
, "spi-gpio") || !con_id
)
246 return ERR_PTR(-ENOENT
);
248 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
249 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", "gpio", con_id
);
251 desc
= of_get_named_gpiod_flags(np
, prop_name
, 0, of_flags
);
256 * Some regulator bindings happened before we managed to establish that GPIO
257 * properties should be named "foo-gpios" so we have this special kludge for
260 static struct gpio_desc
*of_find_regulator_gpio(struct device
*dev
, const char *con_id
,
261 enum of_gpio_flags
*of_flags
)
263 /* These are the connection IDs we accept as legacy GPIO phandles */
264 const char *whitelist
[] = {
265 "wlf,ldoena", /* Arizona */
266 "wlf,ldo1ena", /* WM8994 */
267 "wlf,ldo2ena", /* WM8994 */
269 struct device_node
*np
= dev
->of_node
;
270 struct gpio_desc
*desc
;
273 if (!IS_ENABLED(CONFIG_REGULATOR
))
274 return ERR_PTR(-ENOENT
);
277 return ERR_PTR(-ENOENT
);
279 i
= match_string(whitelist
, ARRAY_SIZE(whitelist
), con_id
);
281 return ERR_PTR(-ENOENT
);
283 desc
= of_get_named_gpiod_flags(np
, con_id
, 0, of_flags
);
287 struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
289 enum gpio_lookup_flags
*flags
)
291 char prop_name
[32]; /* 32 is max size of property name */
292 enum of_gpio_flags of_flags
;
293 struct gpio_desc
*desc
;
296 /* Try GPIO property "foo-gpios" and "foo-gpio" */
297 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
299 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", con_id
,
302 snprintf(prop_name
, sizeof(prop_name
), "%s",
305 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
308 * -EPROBE_DEFER in our case means that we found a
309 * valid GPIO property, but no controller has been
312 * This means we don't need to look any further for
313 * alternate name conventions, and we should really
314 * preserve the return code for our user to be able to
315 * retry probing later.
317 if (IS_ERR(desc
) && PTR_ERR(desc
) == -EPROBE_DEFER
)
320 if (!IS_ERR(desc
) || (PTR_ERR(desc
) != -ENOENT
))
324 /* Special handling for SPI GPIOs if used */
326 desc
= of_find_spi_gpio(dev
, con_id
, &of_flags
);
328 /* Special handling for regulator GPIOs if used */
329 if (IS_ERR(desc
) && PTR_ERR(desc
) != -EPROBE_DEFER
)
330 desc
= of_find_regulator_gpio(dev
, con_id
, &of_flags
);
335 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
336 *flags
|= GPIO_ACTIVE_LOW
;
338 if (of_flags
& OF_GPIO_SINGLE_ENDED
) {
339 if (of_flags
& OF_GPIO_OPEN_DRAIN
)
340 *flags
|= GPIO_OPEN_DRAIN
;
342 *flags
|= GPIO_OPEN_SOURCE
;
345 if (of_flags
& OF_GPIO_TRANSITORY
)
346 *flags
|= GPIO_TRANSITORY
;
352 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
353 * @np: device node to get GPIO from
354 * @chip: GPIO chip whose hog is parsed
355 * @idx: Index of the GPIO to parse
356 * @name: GPIO line name
357 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
358 * of_parse_own_gpio()
359 * @dflags: gpiod_flags - optional GPIO initialization flags
361 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
362 * value on the error condition.
364 static struct gpio_desc
*of_parse_own_gpio(struct device_node
*np
,
365 struct gpio_chip
*chip
,
366 unsigned int idx
, const char **name
,
367 enum gpio_lookup_flags
*lflags
,
368 enum gpiod_flags
*dflags
)
370 struct device_node
*chip_np
;
371 enum of_gpio_flags xlate_flags
;
372 struct of_phandle_args gpiospec
;
373 struct gpio_desc
*desc
;
378 chip_np
= chip
->of_node
;
380 return ERR_PTR(-EINVAL
);
386 ret
= of_property_read_u32(chip_np
, "#gpio-cells", &tmp
);
390 gpiospec
.np
= chip_np
;
391 gpiospec
.args_count
= tmp
;
393 for (i
= 0; i
< tmp
; i
++) {
394 ret
= of_property_read_u32_index(np
, "gpios", idx
* tmp
+ i
,
400 desc
= of_xlate_and_get_gpiod_flags(chip
, &gpiospec
, &xlate_flags
);
404 if (xlate_flags
& OF_GPIO_ACTIVE_LOW
)
405 *lflags
|= GPIO_ACTIVE_LOW
;
406 if (xlate_flags
& OF_GPIO_TRANSITORY
)
407 *lflags
|= GPIO_TRANSITORY
;
409 if (of_property_read_bool(np
, "input"))
411 else if (of_property_read_bool(np
, "output-low"))
412 *dflags
|= GPIOD_OUT_LOW
;
413 else if (of_property_read_bool(np
, "output-high"))
414 *dflags
|= GPIOD_OUT_HIGH
;
416 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
417 desc_to_gpio(desc
), np
);
418 return ERR_PTR(-EINVAL
);
421 if (name
&& of_property_read_string(np
, "line-name", name
))
428 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
429 * @chip: gpio chip to act on
431 * This is only used by of_gpiochip_add to request/set GPIO initial
433 * It returns error if it fails otherwise 0 on success.
435 static int of_gpiochip_scan_gpios(struct gpio_chip
*chip
)
437 struct gpio_desc
*desc
= NULL
;
438 struct device_node
*np
;
440 enum gpio_lookup_flags lflags
;
441 enum gpiod_flags dflags
;
445 for_each_available_child_of_node(chip
->of_node
, np
) {
446 if (!of_property_read_bool(np
, "gpio-hog"))
450 desc
= of_parse_own_gpio(np
, chip
, i
, &name
, &lflags
,
455 ret
= gpiod_hog(desc
, name
, lflags
, dflags
);
467 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
468 * @gc: pointer to the gpio_chip structure
469 * @gpiospec: GPIO specifier as found in the device tree
470 * @flags: a flags pointer to fill in
472 * This is simple translation function, suitable for the most 1:1 mapped
473 * GPIO chips. This function performs only one sanity check: whether GPIO
474 * is less than ngpios (that is specified in the gpio_chip).
476 int of_gpio_simple_xlate(struct gpio_chip
*gc
,
477 const struct of_phandle_args
*gpiospec
, u32
*flags
)
480 * We're discouraging gpio_cells < 2, since that way you'll have to
481 * write your own xlate function (that will have to retrieve the GPIO
482 * number and the flags from a single gpio cell -- this is possible,
483 * but not recommended).
485 if (gc
->of_gpio_n_cells
< 2) {
490 if (WARN_ON(gpiospec
->args_count
< gc
->of_gpio_n_cells
))
493 if (gpiospec
->args
[0] >= gc
->ngpio
)
497 *flags
= gpiospec
->args
[1];
499 return gpiospec
->args
[0];
501 EXPORT_SYMBOL(of_gpio_simple_xlate
);
504 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
505 * @np: device node of the GPIO chip
506 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
507 * @data: driver data to store in the struct gpio_chip
509 * To use this function you should allocate and fill mm_gc with:
511 * 1) In the gpio_chip structure:
512 * - all the callbacks
514 * - of_xlate callback (optional)
516 * 3) In the of_mm_gpio_chip structure:
517 * - save_regs callback (optional)
519 * If succeeded, this function will map bank's memory and will
520 * do all necessary work for you. Then you'll able to use .regs
521 * to manage GPIOs from the callbacks.
523 int of_mm_gpiochip_add_data(struct device_node
*np
,
524 struct of_mm_gpio_chip
*mm_gc
,
528 struct gpio_chip
*gc
= &mm_gc
->gc
;
530 gc
->label
= kasprintf(GFP_KERNEL
, "%pOF", np
);
534 mm_gc
->regs
= of_iomap(np
, 0);
540 if (mm_gc
->save_regs
)
541 mm_gc
->save_regs(mm_gc
);
543 mm_gc
->gc
.of_node
= np
;
545 ret
= gpiochip_add_data(gc
, data
);
551 iounmap(mm_gc
->regs
);
555 pr_err("%pOF: GPIO chip registration failed with status %d\n", np
, ret
);
558 EXPORT_SYMBOL(of_mm_gpiochip_add_data
);
561 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
562 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
564 void of_mm_gpiochip_remove(struct of_mm_gpio_chip
*mm_gc
)
566 struct gpio_chip
*gc
= &mm_gc
->gc
;
572 iounmap(mm_gc
->regs
);
575 EXPORT_SYMBOL(of_mm_gpiochip_remove
);
577 static void of_gpiochip_init_valid_mask(struct gpio_chip
*chip
)
581 struct device_node
*np
= chip
->of_node
;
583 len
= of_property_count_u32_elems(np
, "gpio-reserved-ranges");
584 if (len
< 0 || len
% 2 != 0)
587 for (i
= 0; i
< len
; i
+= 2) {
588 of_property_read_u32_index(np
, "gpio-reserved-ranges",
590 of_property_read_u32_index(np
, "gpio-reserved-ranges",
592 if (start
>= chip
->ngpio
|| start
+ count
>= chip
->ngpio
)
595 bitmap_clear(chip
->valid_mask
, start
, count
);
599 #ifdef CONFIG_PINCTRL
600 static int of_gpiochip_add_pin_range(struct gpio_chip
*chip
)
602 struct device_node
*np
= chip
->of_node
;
603 struct of_phandle_args pinspec
;
604 struct pinctrl_dev
*pctldev
;
607 static const char group_names_propname
[] = "gpio-ranges-group-names";
608 struct property
*group_names
;
613 group_names
= of_find_property(np
, group_names_propname
, NULL
);
616 ret
= of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3,
621 pctldev
= of_pinctrl_get(pinspec
.np
);
622 of_node_put(pinspec
.np
);
624 return -EPROBE_DEFER
;
626 if (pinspec
.args
[2]) {
628 of_property_read_string_index(np
,
629 group_names_propname
,
632 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
637 /* npins != 0: linear range */
638 ret
= gpiochip_add_pin_range(chip
,
639 pinctrl_dev_get_devname(pctldev
),
646 /* npins == 0: special range */
647 if (pinspec
.args
[1]) {
648 pr_err("%pOF: Illegal gpio-range format.\n",
654 pr_err("%pOF: GPIO group range requested but no %s property.\n",
655 np
, group_names_propname
);
659 ret
= of_property_read_string_index(np
,
660 group_names_propname
,
666 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
671 ret
= gpiochip_add_pingroup_range(chip
, pctldev
,
672 pinspec
.args
[0], name
);
682 static int of_gpiochip_add_pin_range(struct gpio_chip
*chip
) { return 0; }
685 int of_gpiochip_add(struct gpio_chip
*chip
)
692 if (!chip
->of_xlate
) {
693 chip
->of_gpio_n_cells
= 2;
694 chip
->of_xlate
= of_gpio_simple_xlate
;
697 if (chip
->of_gpio_n_cells
> MAX_PHANDLE_ARGS
)
700 of_gpiochip_init_valid_mask(chip
);
702 status
= of_gpiochip_add_pin_range(chip
);
706 /* If the chip defines names itself, these take precedence */
708 devprop_gpiochip_set_names(chip
,
709 of_fwnode_handle(chip
->of_node
));
711 of_node_get(chip
->of_node
);
713 return of_gpiochip_scan_gpios(chip
);
716 void of_gpiochip_remove(struct gpio_chip
*chip
)
718 gpiochip_remove_pin_ranges(chip
);
719 of_node_put(chip
->of_node
);