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
,
57 enum of_gpio_flags
*flags
,
61 * Some GPIO fixed regulator quirks.
62 * Note that active low is the default.
64 if (IS_ENABLED(CONFIG_REGULATOR
) &&
65 (of_device_is_compatible(np
, "regulator-fixed") ||
66 of_device_is_compatible(np
, "reg-fixed-voltage") ||
67 of_device_is_compatible(np
, "regulator-gpio"))) {
69 * The regulator GPIO handles are specified such that the
70 * presence or absence of "enable-active-high" solely controls
71 * the polarity of the GPIO line. Any phandle flags must
72 * be actively ignored.
74 if (*flags
& OF_GPIO_ACTIVE_LOW
) {
75 pr_warn("%s GPIO handle specifies active low - ignored\n",
76 of_node_full_name(np
));
77 *flags
&= ~OF_GPIO_ACTIVE_LOW
;
79 if (!of_property_read_bool(np
, "enable-active-high"))
80 *flags
|= OF_GPIO_ACTIVE_LOW
;
83 * Legacy open drain handling for fixed voltage regulators.
85 if (IS_ENABLED(CONFIG_REGULATOR
) &&
86 of_device_is_compatible(np
, "reg-fixed-voltage") &&
87 of_property_read_bool(np
, "gpio-open-drain")) {
88 *flags
|= (OF_GPIO_SINGLE_ENDED
| OF_GPIO_OPEN_DRAIN
);
89 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
90 of_node_full_name(np
));
94 * Legacy handling of SPI active high chip select. If we have a
95 * property named "cs-gpios" we need to inspect the child node
96 * to determine if the flags should have inverted semantics.
98 if (IS_ENABLED(CONFIG_SPI_MASTER
) &&
99 of_property_read_bool(np
, "cs-gpios")) {
100 struct device_node
*child
;
104 for_each_child_of_node(np
, child
) {
105 ret
= of_property_read_u32(child
, "reg", &cs
);
110 * SPI children have active low chip selects
111 * by default. This can be specified negatively
112 * by just omitting "spi-cs-high" in the
113 * device node, or actively by tagging on
114 * GPIO_ACTIVE_LOW as flag in the device
115 * tree. If the line is simultaneously
116 * tagged as active low in the device tree
117 * and has the "spi-cs-high" set, we get a
118 * conflict and the "spi-cs-high" flag will
121 if (of_property_read_bool(np
, "spi-cs-high")) {
122 if (*flags
& OF_GPIO_ACTIVE_LOW
) {
123 pr_warn("%s GPIO handle specifies active low - ignored\n",
124 of_node_full_name(np
));
125 *flags
&= ~OF_GPIO_ACTIVE_LOW
;
128 if (!(*flags
& OF_GPIO_ACTIVE_LOW
))
129 pr_info("%s enforce active low on chipselect handle\n",
130 of_node_full_name(np
));
131 *flags
|= OF_GPIO_ACTIVE_LOW
;
140 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
141 * @np: device node to get GPIO from
142 * @propname: property name containing gpio specifier(s)
143 * @index: index of the GPIO
144 * @flags: a flags pointer to fill in
146 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
147 * value on the error condition. If @flags is not NULL the function also fills
148 * in flags for the GPIO.
150 struct gpio_desc
*of_get_named_gpiod_flags(struct device_node
*np
,
151 const char *propname
, int index
, enum of_gpio_flags
*flags
)
153 struct of_phandle_args gpiospec
;
154 struct gpio_chip
*chip
;
155 struct gpio_desc
*desc
;
158 ret
= of_parse_phandle_with_args_map(np
, propname
, "gpio", index
,
161 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
162 __func__
, propname
, np
, index
);
166 chip
= of_find_gpiochip_by_xlate(&gpiospec
);
168 desc
= ERR_PTR(-EPROBE_DEFER
);
172 desc
= of_xlate_and_get_gpiod_flags(chip
, &gpiospec
, flags
);
177 of_gpio_flags_quirks(np
, flags
, index
);
179 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
180 __func__
, propname
, np
, index
,
181 PTR_ERR_OR_ZERO(desc
));
184 of_node_put(gpiospec
.np
);
189 int of_get_named_gpio_flags(struct device_node
*np
, const char *list_name
,
190 int index
, enum of_gpio_flags
*flags
)
192 struct gpio_desc
*desc
;
194 desc
= of_get_named_gpiod_flags(np
, list_name
, index
, flags
);
197 return PTR_ERR(desc
);
199 return desc_to_gpio(desc
);
201 EXPORT_SYMBOL(of_get_named_gpio_flags
);
204 * The SPI GPIO bindings happened before we managed to establish that GPIO
205 * properties should be named "foo-gpios" so we have this special kludge for
208 static struct gpio_desc
*of_find_spi_gpio(struct device
*dev
, const char *con_id
,
209 enum of_gpio_flags
*of_flags
)
211 char prop_name
[32]; /* 32 is max size of property name */
212 struct device_node
*np
= dev
->of_node
;
213 struct gpio_desc
*desc
;
216 * Hopefully the compiler stubs the rest of the function if this
219 if (!IS_ENABLED(CONFIG_SPI_MASTER
))
220 return ERR_PTR(-ENOENT
);
222 /* Allow this specifically for "spi-gpio" devices */
223 if (!of_device_is_compatible(np
, "spi-gpio") || !con_id
)
224 return ERR_PTR(-ENOENT
);
226 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
227 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", "gpio", con_id
);
229 desc
= of_get_named_gpiod_flags(np
, prop_name
, 0, of_flags
);
234 * Some regulator bindings happened before we managed to establish that GPIO
235 * properties should be named "foo-gpios" so we have this special kludge for
238 static struct gpio_desc
*of_find_regulator_gpio(struct device
*dev
, const char *con_id
,
239 enum of_gpio_flags
*of_flags
)
241 /* These are the connection IDs we accept as legacy GPIO phandles */
242 const char *whitelist
[] = {
243 "wlf,ldoena", /* Arizona */
244 "wlf,ldo1ena", /* WM8994 */
245 "wlf,ldo2ena", /* WM8994 */
247 struct device_node
*np
= dev
->of_node
;
248 struct gpio_desc
*desc
;
251 if (!IS_ENABLED(CONFIG_REGULATOR
))
252 return ERR_PTR(-ENOENT
);
255 return ERR_PTR(-ENOENT
);
257 i
= match_string(whitelist
, ARRAY_SIZE(whitelist
), con_id
);
259 return ERR_PTR(-ENOENT
);
261 desc
= of_get_named_gpiod_flags(np
, con_id
, 0, of_flags
);
265 struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
267 enum gpio_lookup_flags
*flags
)
269 char prop_name
[32]; /* 32 is max size of property name */
270 enum of_gpio_flags of_flags
;
271 struct gpio_desc
*desc
;
274 /* Try GPIO property "foo-gpios" and "foo-gpio" */
275 for (i
= 0; i
< ARRAY_SIZE(gpio_suffixes
); i
++) {
277 snprintf(prop_name
, sizeof(prop_name
), "%s-%s", con_id
,
280 snprintf(prop_name
, sizeof(prop_name
), "%s",
283 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
286 * -EPROBE_DEFER in our case means that we found a
287 * valid GPIO property, but no controller has been
290 * This means we don't need to look any further for
291 * alternate name conventions, and we should really
292 * preserve the return code for our user to be able to
293 * retry probing later.
295 if (IS_ERR(desc
) && PTR_ERR(desc
) == -EPROBE_DEFER
)
298 if (!IS_ERR(desc
) || (PTR_ERR(desc
) != -ENOENT
))
302 /* Special handling for SPI GPIOs if used */
304 desc
= of_find_spi_gpio(dev
, con_id
, &of_flags
);
306 /* Special handling for regulator GPIOs if used */
307 if (IS_ERR(desc
) && PTR_ERR(desc
) != -EPROBE_DEFER
)
308 desc
= of_find_regulator_gpio(dev
, con_id
, &of_flags
);
313 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
314 *flags
|= GPIO_ACTIVE_LOW
;
316 if (of_flags
& OF_GPIO_SINGLE_ENDED
) {
317 if (of_flags
& OF_GPIO_OPEN_DRAIN
)
318 *flags
|= GPIO_OPEN_DRAIN
;
320 *flags
|= GPIO_OPEN_SOURCE
;
323 if (of_flags
& OF_GPIO_TRANSITORY
)
324 *flags
|= GPIO_TRANSITORY
;
330 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
331 * @np: device node to get GPIO from
332 * @chip: GPIO chip whose hog is parsed
333 * @idx: Index of the GPIO to parse
334 * @name: GPIO line name
335 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
336 * of_parse_own_gpio()
337 * @dflags: gpiod_flags - optional GPIO initialization flags
339 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
340 * value on the error condition.
342 static struct gpio_desc
*of_parse_own_gpio(struct device_node
*np
,
343 struct gpio_chip
*chip
,
344 unsigned int idx
, const char **name
,
345 enum gpio_lookup_flags
*lflags
,
346 enum gpiod_flags
*dflags
)
348 struct device_node
*chip_np
;
349 enum of_gpio_flags xlate_flags
;
350 struct of_phandle_args gpiospec
;
351 struct gpio_desc
*desc
;
356 chip_np
= chip
->of_node
;
358 return ERR_PTR(-EINVAL
);
364 ret
= of_property_read_u32(chip_np
, "#gpio-cells", &tmp
);
368 gpiospec
.np
= chip_np
;
369 gpiospec
.args_count
= tmp
;
371 for (i
= 0; i
< tmp
; i
++) {
372 ret
= of_property_read_u32_index(np
, "gpios", idx
* tmp
+ i
,
378 desc
= of_xlate_and_get_gpiod_flags(chip
, &gpiospec
, &xlate_flags
);
382 if (xlate_flags
& OF_GPIO_ACTIVE_LOW
)
383 *lflags
|= GPIO_ACTIVE_LOW
;
384 if (xlate_flags
& OF_GPIO_TRANSITORY
)
385 *lflags
|= GPIO_TRANSITORY
;
387 if (of_property_read_bool(np
, "input"))
389 else if (of_property_read_bool(np
, "output-low"))
390 *dflags
|= GPIOD_OUT_LOW
;
391 else if (of_property_read_bool(np
, "output-high"))
392 *dflags
|= GPIOD_OUT_HIGH
;
394 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
395 desc_to_gpio(desc
), np
);
396 return ERR_PTR(-EINVAL
);
399 if (name
&& of_property_read_string(np
, "line-name", name
))
406 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
407 * @chip: gpio chip to act on
409 * This is only used by of_gpiochip_add to request/set GPIO initial
411 * It returns error if it fails otherwise 0 on success.
413 static int of_gpiochip_scan_gpios(struct gpio_chip
*chip
)
415 struct gpio_desc
*desc
= NULL
;
416 struct device_node
*np
;
418 enum gpio_lookup_flags lflags
;
419 enum gpiod_flags dflags
;
423 for_each_available_child_of_node(chip
->of_node
, np
) {
424 if (!of_property_read_bool(np
, "gpio-hog"))
428 desc
= of_parse_own_gpio(np
, chip
, i
, &name
, &lflags
,
433 ret
= gpiod_hog(desc
, name
, lflags
, dflags
);
445 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
446 * @gc: pointer to the gpio_chip structure
447 * @gpiospec: GPIO specifier as found in the device tree
448 * @flags: a flags pointer to fill in
450 * This is simple translation function, suitable for the most 1:1 mapped
451 * GPIO chips. This function performs only one sanity check: whether GPIO
452 * is less than ngpios (that is specified in the gpio_chip).
454 int of_gpio_simple_xlate(struct gpio_chip
*gc
,
455 const struct of_phandle_args
*gpiospec
, u32
*flags
)
458 * We're discouraging gpio_cells < 2, since that way you'll have to
459 * write your own xlate function (that will have to retrieve the GPIO
460 * number and the flags from a single gpio cell -- this is possible,
461 * but not recommended).
463 if (gc
->of_gpio_n_cells
< 2) {
468 if (WARN_ON(gpiospec
->args_count
< gc
->of_gpio_n_cells
))
471 if (gpiospec
->args
[0] >= gc
->ngpio
)
475 *flags
= gpiospec
->args
[1];
477 return gpiospec
->args
[0];
479 EXPORT_SYMBOL(of_gpio_simple_xlate
);
482 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
483 * @np: device node of the GPIO chip
484 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
485 * @data: driver data to store in the struct gpio_chip
487 * To use this function you should allocate and fill mm_gc with:
489 * 1) In the gpio_chip structure:
490 * - all the callbacks
492 * - of_xlate callback (optional)
494 * 3) In the of_mm_gpio_chip structure:
495 * - save_regs callback (optional)
497 * If succeeded, this function will map bank's memory and will
498 * do all necessary work for you. Then you'll able to use .regs
499 * to manage GPIOs from the callbacks.
501 int of_mm_gpiochip_add_data(struct device_node
*np
,
502 struct of_mm_gpio_chip
*mm_gc
,
506 struct gpio_chip
*gc
= &mm_gc
->gc
;
508 gc
->label
= kasprintf(GFP_KERNEL
, "%pOF", np
);
512 mm_gc
->regs
= of_iomap(np
, 0);
518 if (mm_gc
->save_regs
)
519 mm_gc
->save_regs(mm_gc
);
521 mm_gc
->gc
.of_node
= np
;
523 ret
= gpiochip_add_data(gc
, data
);
529 iounmap(mm_gc
->regs
);
533 pr_err("%pOF: GPIO chip registration failed with status %d\n", np
, ret
);
536 EXPORT_SYMBOL(of_mm_gpiochip_add_data
);
539 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
540 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
542 void of_mm_gpiochip_remove(struct of_mm_gpio_chip
*mm_gc
)
544 struct gpio_chip
*gc
= &mm_gc
->gc
;
550 iounmap(mm_gc
->regs
);
553 EXPORT_SYMBOL(of_mm_gpiochip_remove
);
555 static void of_gpiochip_init_valid_mask(struct gpio_chip
*chip
)
559 struct device_node
*np
= chip
->of_node
;
561 len
= of_property_count_u32_elems(np
, "gpio-reserved-ranges");
562 if (len
< 0 || len
% 2 != 0)
565 for (i
= 0; i
< len
; i
+= 2) {
566 of_property_read_u32_index(np
, "gpio-reserved-ranges",
568 of_property_read_u32_index(np
, "gpio-reserved-ranges",
570 if (start
>= chip
->ngpio
|| start
+ count
>= chip
->ngpio
)
573 bitmap_clear(chip
->valid_mask
, start
, count
);
577 #ifdef CONFIG_PINCTRL
578 static int of_gpiochip_add_pin_range(struct gpio_chip
*chip
)
580 struct device_node
*np
= chip
->of_node
;
581 struct of_phandle_args pinspec
;
582 struct pinctrl_dev
*pctldev
;
585 static const char group_names_propname
[] = "gpio-ranges-group-names";
586 struct property
*group_names
;
591 group_names
= of_find_property(np
, group_names_propname
, NULL
);
594 ret
= of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3,
599 pctldev
= of_pinctrl_get(pinspec
.np
);
600 of_node_put(pinspec
.np
);
602 return -EPROBE_DEFER
;
604 if (pinspec
.args
[2]) {
606 of_property_read_string_index(np
,
607 group_names_propname
,
610 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
615 /* npins != 0: linear range */
616 ret
= gpiochip_add_pin_range(chip
,
617 pinctrl_dev_get_devname(pctldev
),
624 /* npins == 0: special range */
625 if (pinspec
.args
[1]) {
626 pr_err("%pOF: Illegal gpio-range format.\n",
632 pr_err("%pOF: GPIO group range requested but no %s property.\n",
633 np
, group_names_propname
);
637 ret
= of_property_read_string_index(np
,
638 group_names_propname
,
644 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
649 ret
= gpiochip_add_pingroup_range(chip
, pctldev
,
650 pinspec
.args
[0], name
);
660 static int of_gpiochip_add_pin_range(struct gpio_chip
*chip
) { return 0; }
663 int of_gpiochip_add(struct gpio_chip
*chip
)
670 if (!chip
->of_xlate
) {
671 chip
->of_gpio_n_cells
= 2;
672 chip
->of_xlate
= of_gpio_simple_xlate
;
675 if (chip
->of_gpio_n_cells
> MAX_PHANDLE_ARGS
)
678 of_gpiochip_init_valid_mask(chip
);
680 status
= of_gpiochip_add_pin_range(chip
);
684 /* If the chip defines names itself, these take precedence */
686 devprop_gpiochip_set_names(chip
,
687 of_fwnode_handle(chip
->of_node
));
689 of_node_get(chip
->of_node
);
691 return of_gpiochip_scan_gpios(chip
);
694 void of_gpiochip_remove(struct gpio_chip
*chip
)
696 gpiochip_remove_pin_ranges(chip
);
697 of_node_put(chip
->of_node
);