MAINTAINERS: Add MFD's DT bindings directory to MFD entry
[deliverable/linux.git] / drivers / gpio / gpiolib-of.c
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
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/io.h>
19 #include <linux/io-mapping.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/of.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>
27
28 #include "gpiolib.h"
29
30 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
31 {
32 return chip->gpiodev->dev.of_node == data;
33 }
34
35 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
36 {
37 return gpiochip_find(np, of_gpiochip_match_node);
38 }
39
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)
43 {
44 int ret;
45
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);
54 }
55
56 /**
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
62 *
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.
66 */
67 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
68 const char *propname, int index, enum of_gpio_flags *flags)
69 {
70 struct of_phandle_args gpiospec;
71 struct gpio_chip *chip;
72 struct gpio_desc *desc;
73 int ret;
74
75 ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
76 &gpiospec);
77 if (ret) {
78 pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
79 __func__, propname, np->full_name, index);
80 return ERR_PTR(ret);
81 }
82
83 chip = of_find_gpiochip_by_node(gpiospec.np);
84 if (!chip) {
85 desc = ERR_PTR(-EPROBE_DEFER);
86 goto out;
87 }
88
89 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
90 if (IS_ERR(desc))
91 goto out;
92
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));
96
97 out:
98 of_node_put(gpiospec.np);
99
100 return desc;
101 }
102
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
117 /**
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
125 *
126 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
127 * value on the error condition.
128 */
129 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
130 struct gpio_chip *chip,
131 const char **name,
132 enum gpio_lookup_flags *lflags,
133 enum gpiod_flags *dflags)
134 {
135 struct device_node *chip_np;
136 enum of_gpio_flags xlate_flags;
137 struct of_phandle_args gpiospec;
138 struct gpio_desc *desc;
139 u32 tmp;
140 int ret;
141
142 chip_np = chip->of_node;
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
154 gpiospec.np = chip_np;
155 gpiospec.args_count = tmp;
156
157 ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
158 if (ret)
159 return ERR_PTR(ret);
160
161 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
162 if (IS_ERR(desc))
163 return desc;
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",
176 desc_to_gpio(desc), np->name);
177 return ERR_PTR(-EINVAL);
178 }
179
180 if (name && of_property_read_string(np, "line-name", name))
181 *name = np->name;
182
183 return desc;
184 }
185
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
231 /**
232 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
233 * @chip: gpio chip to act on
234 *
235 * This is only used by of_gpiochip_add to request/set GPIO initial
236 * configuration.
237 * It retures error if it fails otherwise 0 on success.
238 */
239 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
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;
246 int ret;
247
248 for_each_available_child_of_node(chip->of_node, np) {
249 if (!of_property_read_bool(np, "gpio-hog"))
250 continue;
251
252 desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
253 if (IS_ERR(desc))
254 continue;
255
256 ret = gpiod_hog(desc, name, lflags, dflags);
257 if (ret < 0)
258 return ret;
259 }
260
261 return 0;
262 }
263
264 /**
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
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 */
275 int of_gpio_simple_xlate(struct gpio_chip *gc,
276 const struct of_phandle_args *gpiospec, u32 *flags)
277 {
278 /*
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).
283 */
284 if (gc->of_gpio_n_cells < 2) {
285 WARN_ON(1);
286 return -EINVAL;
287 }
288
289 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
290 return -EINVAL;
291
292 if (gpiospec->args[0] >= gc->ngpio)
293 return -EINVAL;
294
295 if (flags)
296 *flags = gpiospec->args[1];
297
298 return gpiospec->args[0];
299 }
300 EXPORT_SYMBOL(of_gpio_simple_xlate);
301
302 /**
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
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
312 * - of_gpio_n_cells
313 * - of_xlate callback (optional)
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 */
322 int of_mm_gpiochip_add_data(struct device_node *np,
323 struct of_mm_gpio_chip *mm_gc,
324 void *data)
325 {
326 int ret = -ENOMEM;
327 struct gpio_chip *gc = &mm_gc->gc;
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
337 gc->base = -1;
338
339 if (mm_gc->save_regs)
340 mm_gc->save_regs(mm_gc);
341
342 mm_gc->gc.of_node = np;
343
344 ret = gpiochip_add_data(gc, data);
345 if (ret)
346 goto err2;
347
348 return 0;
349 err2:
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 }
358 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
359
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
377 #ifdef CONFIG_PINCTRL
378 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
379 {
380 struct device_node *np = chip->of_node;
381 struct of_phandle_args pinspec;
382 struct pinctrl_dev *pctldev;
383 int index = 0, ret;
384 const char *name;
385 static const char group_names_propname[] = "gpio-ranges-group-names";
386 struct property *group_names;
387
388 if (!np)
389 return 0;
390
391 group_names = of_find_property(np, group_names_propname, NULL);
392
393 for (;; index++) {
394 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
395 index, &pinspec);
396 if (ret)
397 break;
398
399 pctldev = of_pinctrl_get(pinspec.np);
400 of_node_put(pinspec.np);
401 if (!pctldev)
402 return -EPROBE_DEFER;
403
404 if (pinspec.args[2]) {
405 if (group_names) {
406 of_property_read_string_index(np,
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)
422 return ret;
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)
452 return ret;
453 }
454 }
455
456 return 0;
457 }
458
459 #else
460 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
461 #endif
462
463 int of_gpiochip_add(struct gpio_chip *chip)
464 {
465 int status;
466
467 if ((!chip->of_node) && (chip->parent))
468 chip->of_node = chip->parent->of_node;
469
470 if (!chip->of_node)
471 return 0;
472
473 if (!chip->of_xlate) {
474 chip->of_gpio_n_cells = 2;
475 chip->of_xlate = of_gpio_simple_xlate;
476 }
477
478 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
479 return -EINVAL;
480
481 status = of_gpiochip_add_pin_range(chip);
482 if (status)
483 return status;
484
485 /* If the chip defines names itself, these take precedence */
486 if (!chip->names)
487 of_gpiochip_set_names(chip);
488
489 of_node_get(chip->of_node);
490
491 return of_gpiochip_scan_gpios(chip);
492 }
493
494 void of_gpiochip_remove(struct gpio_chip *chip)
495 {
496 gpiochip_remove_pin_ranges(chip);
497 of_node_put(chip->of_node);
498 }
This page took 0.043098 seconds and 5 git commands to generate.