aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of/gpio.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/of/gpio.c')
-rw-r--r--drivers/of/gpio.c70
1 files changed, 63 insertions, 7 deletions
diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
index 7cd7301b5839..6eea601a9204 100644
--- a/drivers/of/gpio.c
+++ b/drivers/of/gpio.c
@@ -19,14 +19,17 @@
19#include <asm/prom.h> 19#include <asm/prom.h>
20 20
21/** 21/**
22 * of_get_gpio - Get a GPIO number from the device tree to use with GPIO API 22 * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API
23 * @np: device node to get GPIO from 23 * @np: device node to get GPIO from
24 * @index: index of the GPIO 24 * @index: index of the GPIO
25 * @flags: a flags pointer to fill in
25 * 26 *
26 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 27 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
27 * value on the error condition. 28 * value on the error condition. If @flags is not NULL the function also fills
29 * in flags for the GPIO.
28 */ 30 */
29int of_get_gpio(struct device_node *np, int index) 31int of_get_gpio_flags(struct device_node *np, int index,
32 enum of_gpio_flags *flags)
30{ 33{
31 int ret; 34 int ret;
32 struct device_node *gc; 35 struct device_node *gc;
@@ -59,7 +62,11 @@ int of_get_gpio(struct device_node *np, int index)
59 goto err1; 62 goto err1;
60 } 63 }
61 64
62 ret = of_gc->xlate(of_gc, np, gpio_spec); 65 /* .xlate might decide to not fill in the flags, so clear it. */
66 if (flags)
67 *flags = 0;
68
69 ret = of_gc->xlate(of_gc, np, gpio_spec, flags);
63 if (ret < 0) 70 if (ret < 0)
64 goto err1; 71 goto err1;
65 72
@@ -70,26 +77,75 @@ err0:
70 pr_debug("%s exited with status %d\n", __func__, ret); 77 pr_debug("%s exited with status %d\n", __func__, ret);
71 return ret; 78 return ret;
72} 79}
73EXPORT_SYMBOL(of_get_gpio); 80EXPORT_SYMBOL(of_get_gpio_flags);
74 81
75/** 82/**
76 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number 83 * of_gpio_count - Count GPIOs for a device
84 * @np: device node to count GPIOs for
85 *
86 * The function returns the count of GPIOs specified for a node.
87 *
88 * Note that the empty GPIO specifiers counts too. For example,
89 *
90 * gpios = <0
91 * &pio1 1 2
92 * 0
93 * &pio2 3 4>;
94 *
95 * defines four GPIOs (so this function will return 4), two of which
96 * are not specified.
97 */
98unsigned int of_gpio_count(struct device_node *np)
99{
100 unsigned int cnt = 0;
101
102 do {
103 int ret;
104
105 ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells",
106 cnt, NULL, NULL);
107 /* A hole in the gpios = <> counts anyway. */
108 if (ret < 0 && ret != -EEXIST)
109 break;
110 } while (++cnt);
111
112 return cnt;
113}
114EXPORT_SYMBOL(of_gpio_count);
115
116/**
117 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
77 * @of_gc: pointer to the of_gpio_chip structure 118 * @of_gc: pointer to the of_gpio_chip structure
78 * @np: device node of the GPIO chip 119 * @np: device node of the GPIO chip
79 * @gpio_spec: gpio specifier as found in the device tree 120 * @gpio_spec: gpio specifier as found in the device tree
121 * @flags: a flags pointer to fill in
80 * 122 *
81 * This is simple translation function, suitable for the most 1:1 mapped 123 * This is simple translation function, suitable for the most 1:1 mapped
82 * gpio chips. This function performs only one sanity check: whether gpio 124 * gpio chips. This function performs only one sanity check: whether gpio
83 * is less than ngpios (that is specified in the gpio_chip). 125 * is less than ngpios (that is specified in the gpio_chip).
84 */ 126 */
85int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np, 127int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
86 const void *gpio_spec) 128 const void *gpio_spec, enum of_gpio_flags *flags)
87{ 129{
88 const u32 *gpio = gpio_spec; 130 const u32 *gpio = gpio_spec;
89 131
132 /*
133 * We're discouraging gpio_cells < 2, since that way you'll have to
134 * write your own xlate function (that will have to retrive the GPIO
135 * number and the flags from a single gpio cell -- this is possible,
136 * but not recommended).
137 */
138 if (of_gc->gpio_cells < 2) {
139 WARN_ON(1);
140 return -EINVAL;
141 }
142
90 if (*gpio > of_gc->gc.ngpio) 143 if (*gpio > of_gc->gc.ngpio)
91 return -EINVAL; 144 return -EINVAL;
92 145
146 if (flags)
147 *flags = gpio[1];
148
93 return *gpio; 149 return *gpio;
94} 150}
95EXPORT_SYMBOL(of_gpio_simple_xlate); 151EXPORT_SYMBOL(of_gpio_simple_xlate);