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.c93
1 files changed, 58 insertions, 35 deletions
diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
index a1b31a4abae4..905960338fb2 100644
--- a/drivers/of/gpio.c
+++ b/drivers/of/gpio.c
@@ -11,13 +11,14 @@
11 * (at your option) any later version. 11 * (at your option) any later version.
12 */ 12 */
13 13
14#include <linux/kernel.h> 14#include <linux/device.h>
15#include <linux/errno.h> 15#include <linux/errno.h>
16#include <linux/module.h>
16#include <linux/io.h> 17#include <linux/io.h>
17#include <linux/of.h> 18#include <linux/of.h>
18#include <linux/slab.h> 19#include <linux/of_address.h>
19#include <linux/of_gpio.h> 20#include <linux/of_gpio.h>
20#include <asm/prom.h> 21#include <linux/slab.h>
21 22
22/** 23/**
23 * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API 24 * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API
@@ -33,32 +34,32 @@ int of_get_gpio_flags(struct device_node *np, int index,
33 enum of_gpio_flags *flags) 34 enum of_gpio_flags *flags)
34{ 35{
35 int ret; 36 int ret;
36 struct device_node *gc; 37 struct device_node *gpio_np;
37 struct of_gpio_chip *of_gc = NULL; 38 struct gpio_chip *gc;
38 int size; 39 int size;
39 const void *gpio_spec; 40 const void *gpio_spec;
40 const __be32 *gpio_cells; 41 const __be32 *gpio_cells;
41 42
42 ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index, 43 ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index,
43 &gc, &gpio_spec); 44 &gpio_np, &gpio_spec);
44 if (ret) { 45 if (ret) {
45 pr_debug("%s: can't parse gpios property\n", __func__); 46 pr_debug("%s: can't parse gpios property\n", __func__);
46 goto err0; 47 goto err0;
47 } 48 }
48 49
49 of_gc = gc->data; 50 gc = of_node_to_gpiochip(gpio_np);
50 if (!of_gc) { 51 if (!gc) {
51 pr_debug("%s: gpio controller %s isn't registered\n", 52 pr_debug("%s: gpio controller %s isn't registered\n",
52 np->full_name, gc->full_name); 53 np->full_name, gpio_np->full_name);
53 ret = -ENODEV; 54 ret = -ENODEV;
54 goto err1; 55 goto err1;
55 } 56 }
56 57
57 gpio_cells = of_get_property(gc, "#gpio-cells", &size); 58 gpio_cells = of_get_property(gpio_np, "#gpio-cells", &size);
58 if (!gpio_cells || size != sizeof(*gpio_cells) || 59 if (!gpio_cells || size != sizeof(*gpio_cells) ||
59 be32_to_cpup(gpio_cells) != of_gc->gpio_cells) { 60 be32_to_cpup(gpio_cells) != gc->of_gpio_n_cells) {
60 pr_debug("%s: wrong #gpio-cells for %s\n", 61 pr_debug("%s: wrong #gpio-cells for %s\n",
61 np->full_name, gc->full_name); 62 np->full_name, gpio_np->full_name);
62 ret = -EINVAL; 63 ret = -EINVAL;
63 goto err1; 64 goto err1;
64 } 65 }
@@ -67,13 +68,13 @@ int of_get_gpio_flags(struct device_node *np, int index,
67 if (flags) 68 if (flags)
68 *flags = 0; 69 *flags = 0;
69 70
70 ret = of_gc->xlate(of_gc, np, gpio_spec, flags); 71 ret = gc->of_xlate(gc, np, gpio_spec, flags);
71 if (ret < 0) 72 if (ret < 0)
72 goto err1; 73 goto err1;
73 74
74 ret += of_gc->gc.base; 75 ret += gc->base;
75err1: 76err1:
76 of_node_put(gc); 77 of_node_put(gpio_np);
77err0: 78err0:
78 pr_debug("%s exited with status %d\n", __func__, ret); 79 pr_debug("%s exited with status %d\n", __func__, ret);
79 return ret; 80 return ret;
@@ -116,7 +117,7 @@ EXPORT_SYMBOL(of_gpio_count);
116 117
117/** 118/**
118 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 119 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
119 * @of_gc: pointer to the of_gpio_chip structure 120 * @gc: pointer to the gpio_chip structure
120 * @np: device node of the GPIO chip 121 * @np: device node of the GPIO chip
121 * @gpio_spec: gpio specifier as found in the device tree 122 * @gpio_spec: gpio specifier as found in the device tree
122 * @flags: a flags pointer to fill in 123 * @flags: a flags pointer to fill in
@@ -125,8 +126,8 @@ EXPORT_SYMBOL(of_gpio_count);
125 * gpio chips. This function performs only one sanity check: whether gpio 126 * gpio chips. This function performs only one sanity check: whether gpio
126 * is less than ngpios (that is specified in the gpio_chip). 127 * is less than ngpios (that is specified in the gpio_chip).
127 */ 128 */
128int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np, 129static int of_gpio_simple_xlate(struct gpio_chip *gc, struct device_node *np,
129 const void *gpio_spec, enum of_gpio_flags *flags) 130 const void *gpio_spec, u32 *flags)
130{ 131{
131 const __be32 *gpio = gpio_spec; 132 const __be32 *gpio = gpio_spec;
132 const u32 n = be32_to_cpup(gpio); 133 const u32 n = be32_to_cpup(gpio);
@@ -137,12 +138,12 @@ int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
137 * number and the flags from a single gpio cell -- this is possible, 138 * number and the flags from a single gpio cell -- this is possible,
138 * but not recommended). 139 * but not recommended).
139 */ 140 */
140 if (of_gc->gpio_cells < 2) { 141 if (gc->of_gpio_n_cells < 2) {
141 WARN_ON(1); 142 WARN_ON(1);
142 return -EINVAL; 143 return -EINVAL;
143 } 144 }
144 145
145 if (n > of_gc->gc.ngpio) 146 if (n > gc->ngpio)
146 return -EINVAL; 147 return -EINVAL;
147 148
148 if (flags) 149 if (flags)
@@ -150,7 +151,6 @@ int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
150 151
151 return n; 152 return n;
152} 153}
153EXPORT_SYMBOL(of_gpio_simple_xlate);
154 154
155/** 155/**
156 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank) 156 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
@@ -161,10 +161,8 @@ EXPORT_SYMBOL(of_gpio_simple_xlate);
161 * 161 *
162 * 1) In the gpio_chip structure: 162 * 1) In the gpio_chip structure:
163 * - all the callbacks 163 * - all the callbacks
164 * 164 * - of_gpio_n_cells
165 * 2) In the of_gpio_chip structure: 165 * - of_xlate callback (optional)
166 * - gpio_cells
167 * - xlate callback (optional)
168 * 166 *
169 * 3) In the of_mm_gpio_chip structure: 167 * 3) In the of_mm_gpio_chip structure:
170 * - save_regs callback (optional) 168 * - save_regs callback (optional)
@@ -177,8 +175,7 @@ int of_mm_gpiochip_add(struct device_node *np,
177 struct of_mm_gpio_chip *mm_gc) 175 struct of_mm_gpio_chip *mm_gc)
178{ 176{
179 int ret = -ENOMEM; 177 int ret = -ENOMEM;
180 struct of_gpio_chip *of_gc = &mm_gc->of_gc; 178 struct gpio_chip *gc = &mm_gc->gc;
181 struct gpio_chip *gc = &of_gc->gc;
182 179
183 gc->label = kstrdup(np->full_name, GFP_KERNEL); 180 gc->label = kstrdup(np->full_name, GFP_KERNEL);
184 if (!gc->label) 181 if (!gc->label)
@@ -190,26 +187,19 @@ int of_mm_gpiochip_add(struct device_node *np,
190 187
191 gc->base = -1; 188 gc->base = -1;
192 189
193 if (!of_gc->xlate)
194 of_gc->xlate = of_gpio_simple_xlate;
195
196 if (mm_gc->save_regs) 190 if (mm_gc->save_regs)
197 mm_gc->save_regs(mm_gc); 191 mm_gc->save_regs(mm_gc);
198 192
199 np->data = of_gc; 193 mm_gc->gc.of_node = np;
200 194
201 ret = gpiochip_add(gc); 195 ret = gpiochip_add(gc);
202 if (ret) 196 if (ret)
203 goto err2; 197 goto err2;
204 198
205 /* We don't want to lose the node and its ->data */
206 of_node_get(np);
207
208 pr_debug("%s: registered as generic GPIO chip, base is %d\n", 199 pr_debug("%s: registered as generic GPIO chip, base is %d\n",
209 np->full_name, gc->base); 200 np->full_name, gc->base);
210 return 0; 201 return 0;
211err2: 202err2:
212 np->data = NULL;
213 iounmap(mm_gc->regs); 203 iounmap(mm_gc->regs);
214err1: 204err1:
215 kfree(gc->label); 205 kfree(gc->label);
@@ -219,3 +209,36 @@ err0:
219 return ret; 209 return ret;
220} 210}
221EXPORT_SYMBOL(of_mm_gpiochip_add); 211EXPORT_SYMBOL(of_mm_gpiochip_add);
212
213void of_gpiochip_add(struct gpio_chip *chip)
214{
215 if ((!chip->of_node) && (chip->dev))
216 chip->of_node = chip->dev->of_node;
217
218 if (!chip->of_node)
219 return;
220
221 if (!chip->of_xlate) {
222 chip->of_gpio_n_cells = 2;
223 chip->of_xlate = of_gpio_simple_xlate;
224 }
225
226 of_node_get(chip->of_node);
227}
228
229void of_gpiochip_remove(struct gpio_chip *chip)
230{
231 if (chip->of_node)
232 of_node_put(chip->of_node);
233}
234
235/* Private function for resolving node pointer to gpio_chip */
236static int of_gpiochip_is_match(struct gpio_chip *chip, void *data)
237{
238 return chip->of_node == data;
239}
240
241struct gpio_chip *of_node_to_gpiochip(struct device_node *np)
242{
243 return gpiochip_find(np, of_gpiochip_is_match);
244}