Commit | Line | Data |
---|---|---|
863fbf49 AV |
1 | /* |
2 | * OF helpers for the GPIO API | |
3 | * | |
4 | * Copyright (c) 2007-2008 MontaVista Software, Inc. | |
5 | * | |
6 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> | |
7 | * | |
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. | |
12 | */ | |
13 | ||
2e13cba8 | 14 | #include <linux/device.h> |
bea4dbee | 15 | #include <linux/err.h> |
863fbf49 | 16 | #include <linux/errno.h> |
2e13cba8 | 17 | #include <linux/module.h> |
863fbf49 | 18 | #include <linux/io.h> |
7d4defe2 | 19 | #include <linux/io-mapping.h> |
af8b6375 | 20 | #include <linux/gpio/consumer.h> |
863fbf49 | 21 | #include <linux/of.h> |
2e13cba8 | 22 | #include <linux/of_address.h> |
863fbf49 | 23 | #include <linux/of_gpio.h> |
f23f1516 | 24 | #include <linux/pinctrl/pinctrl.h> |
2e13cba8 | 25 | #include <linux/slab.h> |
f625d460 | 26 | #include <linux/gpio/machine.h> |
863fbf49 | 27 | |
1bd6b601 | 28 | #include "gpiolib.h" |
af8b6375 | 29 | |
762c2e46 | 30 | static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) |
3d0f7cf0 | 31 | { |
762c2e46 MY |
32 | return chip->gpiodev->dev.of_node == data; |
33 | } | |
3d0f7cf0 | 34 | |
762c2e46 MY |
35 | static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) |
36 | { | |
37 | return gpiochip_find(np, of_gpiochip_match_node); | |
3d0f7cf0 | 38 | } |
3d0f7cf0 | 39 | |
99468c1a MY |
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) | |
3d0f7cf0 | 43 | { |
3d0f7cf0 GL |
44 | int ret; |
45 | ||
99468c1a MY |
46 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
47 | return ERR_PTR(-EINVAL); | |
48 | ||
49 | ret = chip->of_xlate(chip, gpiospec, flags); | |
50 | if (ret < 0) | |
51 | return ERR_PTR(ret); | |
52 | ||
53 | return gpiochip_get_desc(chip, ret); | |
3d0f7cf0 GL |
54 | } |
55 | ||
863fbf49 | 56 | /** |
af8b6375 | 57 | * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API |
863fbf49 | 58 | * @np: device node to get GPIO from |
a6b09191 | 59 | * @propname: property name containing gpio specifier(s) |
863fbf49 | 60 | * @index: index of the GPIO |
b908b53d | 61 | * @flags: a flags pointer to fill in |
863fbf49 | 62 | * |
af8b6375 | 63 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
b908b53d AV |
64 | * value on the error condition. If @flags is not NULL the function also fills |
65 | * in flags for the GPIO. | |
863fbf49 | 66 | */ |
af8b6375 AC |
67 | struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
68 | const char *propname, int index, enum of_gpio_flags *flags) | |
863fbf49 | 69 | { |
762c2e46 MY |
70 | struct of_phandle_args gpiospec; |
71 | struct gpio_chip *chip; | |
72 | struct gpio_desc *desc; | |
64b60e09 | 73 | int ret; |
3d0f7cf0 | 74 | |
15c9a0ac | 75 | ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, |
762c2e46 | 76 | &gpiospec); |
64b60e09 | 77 | if (ret) { |
85ea29ac TB |
78 | pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n", |
79 | __func__, propname, np->full_name, index); | |
af8b6375 | 80 | return ERR_PTR(ret); |
863fbf49 AV |
81 | } |
82 | ||
762c2e46 MY |
83 | chip = of_find_gpiochip_by_node(gpiospec.np); |
84 | if (!chip) { | |
85 | desc = ERR_PTR(-EPROBE_DEFER); | |
86 | goto out; | |
87 | } | |
762c2e46 | 88 | |
99468c1a | 89 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
762c2e46 MY |
90 | if (IS_ERR(desc)) |
91 | goto out; | |
863fbf49 | 92 | |
85ea29ac TB |
93 | pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n", |
94 | __func__, propname, np->full_name, index, | |
762c2e46 MY |
95 | PTR_ERR_OR_ZERO(desc)); |
96 | ||
97 | out: | |
98 | of_node_put(gpiospec.np); | |
99 | ||
100 | return desc; | |
863fbf49 | 101 | } |
863fbf49 | 102 | |
f01d9075 AC |
103 | int of_get_named_gpio_flags(struct device_node *np, const char *list_name, |
104 | int index, enum of_gpio_flags *flags) | |
105 | { | |
106 | struct gpio_desc *desc; | |
107 | ||
108 | desc = of_get_named_gpiod_flags(np, list_name, index, flags); | |
109 | ||
110 | if (IS_ERR(desc)) | |
111 | return PTR_ERR(desc); | |
112 | else | |
113 | return desc_to_gpio(desc); | |
114 | } | |
115 | EXPORT_SYMBOL(of_get_named_gpio_flags); | |
116 | ||
f625d460 | 117 | /** |
fd7337fd | 118 | * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API |
f625d460 | 119 | * @np: device node to get GPIO from |
be715343 | 120 | * @chip: GPIO chip whose hog is parsed |
f625d460 BP |
121 | * @name: GPIO line name |
122 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
fd7337fd | 123 | * of_parse_own_gpio() |
f625d460 BP |
124 | * @dflags: gpiod_flags - optional GPIO initialization flags |
125 | * | |
126 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno | |
127 | * value on the error condition. | |
128 | */ | |
fd7337fd | 129 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
be715343 | 130 | struct gpio_chip *chip, |
fd7337fd MP |
131 | const char **name, |
132 | enum gpio_lookup_flags *lflags, | |
133 | enum gpiod_flags *dflags) | |
f625d460 BP |
134 | { |
135 | struct device_node *chip_np; | |
136 | enum of_gpio_flags xlate_flags; | |
be715343 MY |
137 | struct of_phandle_args gpiospec; |
138 | struct gpio_desc *desc; | |
f625d460 | 139 | u32 tmp; |
3f9547e1 | 140 | int ret; |
f625d460 | 141 | |
be715343 | 142 | chip_np = chip->of_node; |
f625d460 BP |
143 | if (!chip_np) |
144 | return ERR_PTR(-EINVAL); | |
145 | ||
146 | xlate_flags = 0; | |
147 | *lflags = 0; | |
148 | *dflags = 0; | |
149 | ||
150 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); | |
151 | if (ret) | |
152 | return ERR_PTR(ret); | |
153 | ||
be715343 MY |
154 | gpiospec.np = chip_np; |
155 | gpiospec.args_count = tmp; | |
f625d460 | 156 | |
be715343 | 157 | ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp); |
3f9547e1 MY |
158 | if (ret) |
159 | return ERR_PTR(ret); | |
f625d460 | 160 | |
99468c1a | 161 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); |
be715343 MY |
162 | if (IS_ERR(desc)) |
163 | return desc; | |
f625d460 BP |
164 | |
165 | if (xlate_flags & OF_GPIO_ACTIVE_LOW) | |
166 | *lflags |= GPIO_ACTIVE_LOW; | |
167 | ||
168 | if (of_property_read_bool(np, "input")) | |
169 | *dflags |= GPIOD_IN; | |
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; | |
174 | else { | |
175 | pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n", | |
be715343 | 176 | desc_to_gpio(desc), np->name); |
f625d460 BP |
177 | return ERR_PTR(-EINVAL); |
178 | } | |
179 | ||
180 | if (name && of_property_read_string(np, "line-name", name)) | |
181 | *name = np->name; | |
182 | ||
be715343 | 183 | return desc; |
f625d460 BP |
184 | } |
185 | ||
fd9c5531 LW |
186 | /** |
187 | * of_gpiochip_set_names() - set up the names of the lines | |
188 | * @chip: GPIO chip whose lines should be named, if possible | |
189 | */ | |
190 | static void of_gpiochip_set_names(struct gpio_chip *gc) | |
191 | { | |
192 | struct gpio_device *gdev = gc->gpiodev; | |
193 | struct device_node *np = gc->of_node; | |
194 | int i; | |
195 | int nstrings; | |
196 | ||
197 | nstrings = of_property_count_strings(np, "gpio-line-names"); | |
198 | if (nstrings <= 0) | |
199 | /* Lines names not present */ | |
200 | return; | |
201 | ||
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); | |
207 | ||
208 | /* | |
209 | * Make sure to not index beyond the end of the number of descriptors | |
210 | * of the GPIO device. | |
211 | */ | |
212 | for (i = 0; i < gdev->ngpio; i++) { | |
213 | const char *name; | |
214 | int ret; | |
215 | ||
216 | ret = of_property_read_string_index(np, | |
217 | "gpio-line-names", | |
218 | i, | |
219 | &name); | |
220 | if (ret) { | |
221 | if (ret != -ENODATA) | |
222 | dev_err(&gdev->dev, | |
223 | "unable to name line %d: %d\n", | |
224 | i, ret); | |
225 | break; | |
226 | } | |
227 | gdev->descs[i].name = name; | |
228 | } | |
229 | } | |
230 | ||
f625d460 | 231 | /** |
fd7337fd | 232 | * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions |
f625d460 BP |
233 | * @chip: gpio chip to act on |
234 | * | |
235 | * This is only used by of_gpiochip_add to request/set GPIO initial | |
236 | * configuration. | |
dfbd379b | 237 | * It retures error if it fails otherwise 0 on success. |
f625d460 | 238 | */ |
dfbd379b | 239 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
f625d460 BP |
240 | { |
241 | struct gpio_desc *desc = NULL; | |
242 | struct device_node *np; | |
243 | const char *name; | |
244 | enum gpio_lookup_flags lflags; | |
245 | enum gpiod_flags dflags; | |
dfbd379b | 246 | int ret; |
f625d460 | 247 | |
d1279d94 | 248 | for_each_available_child_of_node(chip->of_node, np) { |
f625d460 BP |
249 | if (!of_property_read_bool(np, "gpio-hog")) |
250 | continue; | |
251 | ||
be715343 | 252 | desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags); |
f625d460 BP |
253 | if (IS_ERR(desc)) |
254 | continue; | |
255 | ||
dfbd379b LD |
256 | ret = gpiod_hog(desc, name, lflags, dflags); |
257 | if (ret < 0) | |
258 | return ret; | |
f625d460 | 259 | } |
dfbd379b LD |
260 | |
261 | return 0; | |
f625d460 BP |
262 | } |
263 | ||
863fbf49 | 264 | /** |
b908b53d | 265 | * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags |
a19e3da5 | 266 | * @gc: pointer to the gpio_chip structure |
863fbf49 AV |
267 | * @np: device node of the GPIO chip |
268 | * @gpio_spec: gpio specifier as found in the device tree | |
b908b53d | 269 | * @flags: a flags pointer to fill in |
863fbf49 AV |
270 | * |
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). | |
274 | */ | |
15c9a0ac GL |
275 | int of_gpio_simple_xlate(struct gpio_chip *gc, |
276 | const struct of_phandle_args *gpiospec, u32 *flags) | |
863fbf49 | 277 | { |
b908b53d AV |
278 | /* |
279 | * We're discouraging gpio_cells < 2, since that way you'll have to | |
20a8a968 | 280 | * write your own xlate function (that will have to retrieve the GPIO |
b908b53d AV |
281 | * number and the flags from a single gpio cell -- this is possible, |
282 | * but not recommended). | |
283 | */ | |
a19e3da5 | 284 | if (gc->of_gpio_n_cells < 2) { |
b908b53d AV |
285 | WARN_ON(1); |
286 | return -EINVAL; | |
287 | } | |
288 | ||
15c9a0ac GL |
289 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
290 | return -EINVAL; | |
291 | ||
6270d830 | 292 | if (gpiospec->args[0] >= gc->ngpio) |
863fbf49 AV |
293 | return -EINVAL; |
294 | ||
b908b53d | 295 | if (flags) |
15c9a0ac | 296 | *flags = gpiospec->args[1]; |
b908b53d | 297 | |
15c9a0ac | 298 | return gpiospec->args[0]; |
863fbf49 | 299 | } |
3038bbdf | 300 | EXPORT_SYMBOL(of_gpio_simple_xlate); |
863fbf49 | 301 | |
863fbf49 | 302 | /** |
3208b0f0 | 303 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
863fbf49 AV |
304 | * @np: device node of the GPIO chip |
305 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
3208b0f0 | 306 | * @data: driver data to store in the struct gpio_chip |
863fbf49 AV |
307 | * |
308 | * To use this function you should allocate and fill mm_gc with: | |
309 | * | |
310 | * 1) In the gpio_chip structure: | |
311 | * - all the callbacks | |
a19e3da5 AV |
312 | * - of_gpio_n_cells |
313 | * - of_xlate callback (optional) | |
863fbf49 AV |
314 | * |
315 | * 3) In the of_mm_gpio_chip structure: | |
316 | * - save_regs callback (optional) | |
317 | * | |
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. | |
321 | */ | |
3208b0f0 LW |
322 | int of_mm_gpiochip_add_data(struct device_node *np, |
323 | struct of_mm_gpio_chip *mm_gc, | |
324 | void *data) | |
863fbf49 AV |
325 | { |
326 | int ret = -ENOMEM; | |
a19e3da5 | 327 | struct gpio_chip *gc = &mm_gc->gc; |
863fbf49 AV |
328 | |
329 | gc->label = kstrdup(np->full_name, GFP_KERNEL); | |
330 | if (!gc->label) | |
331 | goto err0; | |
332 | ||
333 | mm_gc->regs = of_iomap(np, 0); | |
334 | if (!mm_gc->regs) | |
335 | goto err1; | |
336 | ||
21451155 | 337 | gc->base = -1; |
863fbf49 | 338 | |
863fbf49 AV |
339 | if (mm_gc->save_regs) |
340 | mm_gc->save_regs(mm_gc); | |
341 | ||
a19e3da5 | 342 | mm_gc->gc.of_node = np; |
863fbf49 | 343 | |
3208b0f0 | 344 | ret = gpiochip_add_data(gc, data); |
863fbf49 AV |
345 | if (ret) |
346 | goto err2; | |
347 | ||
863fbf49 AV |
348 | return 0; |
349 | err2: | |
863fbf49 AV |
350 | iounmap(mm_gc->regs); |
351 | err1: | |
352 | kfree(gc->label); | |
353 | err0: | |
354 | pr_err("%s: GPIO chip registration failed with status %d\n", | |
355 | np->full_name, ret); | |
356 | return ret; | |
357 | } | |
3208b0f0 | 358 | EXPORT_SYMBOL(of_mm_gpiochip_add_data); |
594fa265 | 359 | |
d621e8ba RRD |
360 | /** |
361 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) | |
362 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
363 | */ | |
364 | void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) | |
365 | { | |
366 | struct gpio_chip *gc = &mm_gc->gc; | |
367 | ||
368 | if (!mm_gc) | |
369 | return; | |
370 | ||
371 | gpiochip_remove(gc); | |
372 | iounmap(mm_gc->regs); | |
373 | kfree(gc->label); | |
374 | } | |
375 | EXPORT_SYMBOL(of_mm_gpiochip_remove); | |
376 | ||
f23f1516 | 377 | #ifdef CONFIG_PINCTRL |
28355f81 | 378 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) |
f23f1516 SH |
379 | { |
380 | struct device_node *np = chip->of_node; | |
f23f1516 | 381 | struct of_phandle_args pinspec; |
1e63d7b9 | 382 | struct pinctrl_dev *pctldev; |
f23f1516 | 383 | int index = 0, ret; |
586a87e6 CR |
384 | const char *name; |
385 | static const char group_names_propname[] = "gpio-ranges-group-names"; | |
386 | struct property *group_names; | |
f23f1516 SH |
387 | |
388 | if (!np) | |
28355f81 | 389 | return 0; |
f23f1516 | 390 | |
586a87e6 CR |
391 | group_names = of_find_property(np, group_names_propname, NULL); |
392 | ||
ad4e1a7c | 393 | for (;; index++) { |
d9fe0039 SW |
394 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, |
395 | index, &pinspec); | |
f23f1516 SH |
396 | if (ret) |
397 | break; | |
398 | ||
1e63d7b9 | 399 | pctldev = of_pinctrl_get(pinspec.np); |
602cf638 | 400 | of_node_put(pinspec.np); |
1e63d7b9 | 401 | if (!pctldev) |
28355f81 | 402 | return -EPROBE_DEFER; |
f23f1516 | 403 | |
586a87e6 CR |
404 | if (pinspec.args[2]) { |
405 | if (group_names) { | |
72858602 | 406 | of_property_read_string_index(np, |
586a87e6 CR |
407 | group_names_propname, |
408 | index, &name); | |
409 | if (strlen(name)) { | |
410 | pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n", | |
411 | np->full_name); | |
412 | break; | |
413 | } | |
414 | } | |
415 | /* npins != 0: linear range */ | |
416 | ret = gpiochip_add_pin_range(chip, | |
417 | pinctrl_dev_get_devname(pctldev), | |
418 | pinspec.args[0], | |
419 | pinspec.args[1], | |
420 | pinspec.args[2]); | |
421 | if (ret) | |
28355f81 | 422 | return ret; |
586a87e6 CR |
423 | } else { |
424 | /* npins == 0: special range */ | |
425 | if (pinspec.args[1]) { | |
426 | pr_err("%s: Illegal gpio-range format.\n", | |
427 | np->full_name); | |
428 | break; | |
429 | } | |
430 | ||
431 | if (!group_names) { | |
432 | pr_err("%s: GPIO group range requested but no %s property.\n", | |
433 | np->full_name, group_names_propname); | |
434 | break; | |
435 | } | |
436 | ||
437 | ret = of_property_read_string_index(np, | |
438 | group_names_propname, | |
439 | index, &name); | |
440 | if (ret) | |
441 | break; | |
442 | ||
443 | if (!strlen(name)) { | |
444 | pr_err("%s: Group name of GPIO group range cannot be the empty string.\n", | |
445 | np->full_name); | |
446 | break; | |
447 | } | |
448 | ||
449 | ret = gpiochip_add_pingroup_range(chip, pctldev, | |
450 | pinspec.args[0], name); | |
451 | if (ret) | |
28355f81 | 452 | return ret; |
586a87e6 | 453 | } |
ad4e1a7c | 454 | } |
28355f81 TV |
455 | |
456 | return 0; | |
f23f1516 SH |
457 | } |
458 | ||
f23f1516 | 459 | #else |
28355f81 | 460 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } |
f23f1516 SH |
461 | #endif |
462 | ||
28355f81 | 463 | int of_gpiochip_add(struct gpio_chip *chip) |
391c970c | 464 | { |
28355f81 TV |
465 | int status; |
466 | ||
58383c78 LW |
467 | if ((!chip->of_node) && (chip->parent)) |
468 | chip->of_node = chip->parent->of_node; | |
391c970c AV |
469 | |
470 | if (!chip->of_node) | |
28355f81 | 471 | return 0; |
391c970c AV |
472 | |
473 | if (!chip->of_xlate) { | |
474 | chip->of_gpio_n_cells = 2; | |
475 | chip->of_xlate = of_gpio_simple_xlate; | |
476 | } | |
477 | ||
1020dfd1 MY |
478 | if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) |
479 | return -EINVAL; | |
480 | ||
28355f81 TV |
481 | status = of_gpiochip_add_pin_range(chip); |
482 | if (status) | |
483 | return status; | |
484 | ||
fd9c5531 LW |
485 | /* If the chip defines names itself, these take precedence */ |
486 | if (!chip->names) | |
487 | of_gpiochip_set_names(chip); | |
488 | ||
391c970c | 489 | of_node_get(chip->of_node); |
f625d460 | 490 | |
dfbd379b | 491 | return of_gpiochip_scan_gpios(chip); |
391c970c AV |
492 | } |
493 | ||
494 | void of_gpiochip_remove(struct gpio_chip *chip) | |
495 | { | |
e93fa3f2 | 496 | gpiochip_remove_pin_ranges(chip); |
8a691550 | 497 | of_node_put(chip->of_node); |
391c970c | 498 | } |