2 * OF helpers for the GPIO API
4 * Copyright (c) 2007-2008 MontaVista Software, Inc.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
19 #include <linux/io-mapping.h>
20 #include <linux/gpio/consumer.h>
22 #include <linux/of_address.h>
23 #include <linux/of_gpio.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/slab.h>
26 #include <linux/gpio/machine.h>
30 static int of_gpiochip_match_node(struct gpio_chip
*chip
, void *data
)
32 return chip
->gpiodev
->dev
.of_node
== data
;
35 static struct gpio_chip
*of_find_gpiochip_by_node(struct device_node
*np
)
37 return gpiochip_find(np
, of_gpiochip_match_node
);
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
);
57 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
58 * @np: device node to get GPIO from
59 * @propname: property name containing gpio specifier(s)
60 * @index: index of the GPIO
61 * @flags: a flags pointer to fill in
63 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
64 * value on the error condition. If @flags is not NULL the function also fills
65 * in flags for the GPIO.
67 struct gpio_desc
*of_get_named_gpiod_flags(struct device_node
*np
,
68 const char *propname
, int index
, enum of_gpio_flags
*flags
)
70 struct of_phandle_args gpiospec
;
71 struct gpio_chip
*chip
;
72 struct gpio_desc
*desc
;
75 ret
= of_parse_phandle_with_args(np
, propname
, "#gpio-cells", index
,
78 pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
79 __func__
, propname
, np
->full_name
, index
);
83 chip
= of_find_gpiochip_by_node(gpiospec
.np
);
85 desc
= ERR_PTR(-EPROBE_DEFER
);
89 desc
= of_xlate_and_get_gpiod_flags(chip
, &gpiospec
, flags
);
93 pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
94 __func__
, propname
, np
->full_name
, index
,
95 PTR_ERR_OR_ZERO(desc
));
98 of_node_put(gpiospec
.np
);
103 int of_get_named_gpio_flags(struct device_node
*np
, const char *list_name
,
104 int index
, enum of_gpio_flags
*flags
)
106 struct gpio_desc
*desc
;
108 desc
= of_get_named_gpiod_flags(np
, list_name
, index
, flags
);
111 return PTR_ERR(desc
);
113 return desc_to_gpio(desc
);
115 EXPORT_SYMBOL(of_get_named_gpio_flags
);
118 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
119 * @np: device node to get GPIO from
120 * @chip: GPIO chip whose hog is parsed
121 * @name: GPIO line name
122 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
123 * of_parse_own_gpio()
124 * @dflags: gpiod_flags - optional GPIO initialization flags
126 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
127 * value on the error condition.
129 static struct gpio_desc
*of_parse_own_gpio(struct device_node
*np
,
130 struct gpio_chip
*chip
,
132 enum gpio_lookup_flags
*lflags
,
133 enum gpiod_flags
*dflags
)
135 struct device_node
*chip_np
;
136 enum of_gpio_flags xlate_flags
;
137 struct of_phandle_args gpiospec
;
138 struct gpio_desc
*desc
;
142 chip_np
= chip
->of_node
;
144 return ERR_PTR(-EINVAL
);
150 ret
= of_property_read_u32(chip_np
, "#gpio-cells", &tmp
);
154 gpiospec
.np
= chip_np
;
155 gpiospec
.args_count
= tmp
;
157 ret
= of_property_read_u32_array(np
, "gpios", gpiospec
.args
, tmp
);
161 desc
= of_xlate_and_get_gpiod_flags(chip
, &gpiospec
, &xlate_flags
);
165 if (xlate_flags
& OF_GPIO_ACTIVE_LOW
)
166 *lflags
|= GPIO_ACTIVE_LOW
;
168 if (of_property_read_bool(np
, "input"))
170 else if (of_property_read_bool(np
, "output-low"))
171 *dflags
|= GPIOD_OUT_LOW
;
172 else if (of_property_read_bool(np
, "output-high"))
173 *dflags
|= GPIOD_OUT_HIGH
;
175 pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
176 desc_to_gpio(desc
), np
->name
);
177 return ERR_PTR(-EINVAL
);
180 if (name
&& of_property_read_string(np
, "line-name", name
))
187 * of_gpiochip_set_names() - set up the names of the lines
188 * @chip: GPIO chip whose lines should be named, if possible
190 static void of_gpiochip_set_names(struct gpio_chip
*gc
)
192 struct gpio_device
*gdev
= gc
->gpiodev
;
193 struct device_node
*np
= gc
->of_node
;
197 nstrings
= of_property_count_strings(np
, "gpio-line-names");
199 /* Lines names not present */
202 /* This is normally not what you want */
203 if (gdev
->ngpio
!= nstrings
)
204 dev_info(&gdev
->dev
, "gpio-line-names specifies %d line "
205 "names but there are %d lines on the chip\n",
206 nstrings
, gdev
->ngpio
);
209 * Make sure to not index beyond the end of the number of descriptors
210 * of the GPIO device.
212 for (i
= 0; i
< gdev
->ngpio
; i
++) {
216 ret
= of_property_read_string_index(np
,
223 "unable to name line %d: %d\n",
227 gdev
->descs
[i
].name
= name
;
232 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
233 * @chip: gpio chip to act on
235 * This is only used by of_gpiochip_add to request/set GPIO initial
237 * It retures error if it fails otherwise 0 on success.
239 static int of_gpiochip_scan_gpios(struct gpio_chip
*chip
)
241 struct gpio_desc
*desc
= NULL
;
242 struct device_node
*np
;
244 enum gpio_lookup_flags lflags
;
245 enum gpiod_flags dflags
;
248 for_each_available_child_of_node(chip
->of_node
, np
) {
249 if (!of_property_read_bool(np
, "gpio-hog"))
252 desc
= of_parse_own_gpio(np
, chip
, &name
, &lflags
, &dflags
);
256 ret
= gpiod_hog(desc
, name
, lflags
, dflags
);
265 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
266 * @gc: pointer to the gpio_chip structure
267 * @np: device node of the GPIO chip
268 * @gpio_spec: gpio specifier as found in the device tree
269 * @flags: a flags pointer to fill in
271 * This is simple translation function, suitable for the most 1:1 mapped
272 * gpio chips. This function performs only one sanity check: whether gpio
273 * is less than ngpios (that is specified in the gpio_chip).
275 int of_gpio_simple_xlate(struct gpio_chip
*gc
,
276 const struct of_phandle_args
*gpiospec
, u32
*flags
)
279 * We're discouraging gpio_cells < 2, since that way you'll have to
280 * write your own xlate function (that will have to retrieve the GPIO
281 * number and the flags from a single gpio cell -- this is possible,
282 * but not recommended).
284 if (gc
->of_gpio_n_cells
< 2) {
289 if (WARN_ON(gpiospec
->args_count
< gc
->of_gpio_n_cells
))
292 if (gpiospec
->args
[0] >= gc
->ngpio
)
296 *flags
= gpiospec
->args
[1];
298 return gpiospec
->args
[0];
300 EXPORT_SYMBOL(of_gpio_simple_xlate
);
303 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
304 * @np: device node of the GPIO chip
305 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
306 * @data: driver data to store in the struct gpio_chip
308 * To use this function you should allocate and fill mm_gc with:
310 * 1) In the gpio_chip structure:
311 * - all the callbacks
313 * - of_xlate callback (optional)
315 * 3) In the of_mm_gpio_chip structure:
316 * - save_regs callback (optional)
318 * If succeeded, this function will map bank's memory and will
319 * do all necessary work for you. Then you'll able to use .regs
320 * to manage GPIOs from the callbacks.
322 int of_mm_gpiochip_add_data(struct device_node
*np
,
323 struct of_mm_gpio_chip
*mm_gc
,
327 struct gpio_chip
*gc
= &mm_gc
->gc
;
329 gc
->label
= kstrdup(np
->full_name
, GFP_KERNEL
);
333 mm_gc
->regs
= of_iomap(np
, 0);
339 if (mm_gc
->save_regs
)
340 mm_gc
->save_regs(mm_gc
);
342 mm_gc
->gc
.of_node
= np
;
344 ret
= gpiochip_add_data(gc
, data
);
350 iounmap(mm_gc
->regs
);
354 pr_err("%s: GPIO chip registration failed with status %d\n",
358 EXPORT_SYMBOL(of_mm_gpiochip_add_data
);
361 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
362 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
364 void of_mm_gpiochip_remove(struct of_mm_gpio_chip
*mm_gc
)
366 struct gpio_chip
*gc
= &mm_gc
->gc
;
372 iounmap(mm_gc
->regs
);
375 EXPORT_SYMBOL(of_mm_gpiochip_remove
);
377 #ifdef CONFIG_PINCTRL
378 static int of_gpiochip_add_pin_range(struct gpio_chip
*chip
)
380 struct device_node
*np
= chip
->of_node
;
381 struct of_phandle_args pinspec
;
382 struct pinctrl_dev
*pctldev
;
385 static const char group_names_propname
[] = "gpio-ranges-group-names";
386 struct property
*group_names
;
391 group_names
= of_find_property(np
, group_names_propname
, NULL
);
394 ret
= of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3,
399 pctldev
= of_pinctrl_get(pinspec
.np
);
400 of_node_put(pinspec
.np
);
402 return -EPROBE_DEFER
;
404 if (pinspec
.args
[2]) {
406 of_property_read_string_index(np
,
407 group_names_propname
,
410 pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
415 /* npins != 0: linear range */
416 ret
= gpiochip_add_pin_range(chip
,
417 pinctrl_dev_get_devname(pctldev
),
424 /* npins == 0: special range */
425 if (pinspec
.args
[1]) {
426 pr_err("%s: Illegal gpio-range format.\n",
432 pr_err("%s: GPIO group range requested but no %s property.\n",
433 np
->full_name
, group_names_propname
);
437 ret
= of_property_read_string_index(np
,
438 group_names_propname
,
444 pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
449 ret
= gpiochip_add_pingroup_range(chip
, pctldev
,
450 pinspec
.args
[0], name
);
460 static int of_gpiochip_add_pin_range(struct gpio_chip
*chip
) { return 0; }
463 int of_gpiochip_add(struct gpio_chip
*chip
)
467 if ((!chip
->of_node
) && (chip
->parent
))
468 chip
->of_node
= chip
->parent
->of_node
;
473 if (!chip
->of_xlate
) {
474 chip
->of_gpio_n_cells
= 2;
475 chip
->of_xlate
= of_gpio_simple_xlate
;
478 if (chip
->of_gpio_n_cells
> MAX_PHANDLE_ARGS
)
481 status
= of_gpiochip_add_pin_range(chip
);
485 /* If the chip defines names itself, these take precedence */
487 of_gpiochip_set_names(chip
);
489 of_node_get(chip
->of_node
);
491 return of_gpiochip_scan_gpios(chip
);
494 void of_gpiochip_remove(struct gpio_chip
*chip
)
496 gpiochip_remove_pin_ranges(chip
);
497 of_node_put(chip
->of_node
);