GNU Linux-libre 4.9.328-gnu1
[releases.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/gpio/consumer.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/of_gpio.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/slab.h>
25 #include <linux/gpio/machine.h>
26
27 #include "gpiolib.h"
28
29 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
30 {
31         struct of_phandle_args *gpiospec = data;
32
33         return chip->gpiodev->dev.of_node == gpiospec->np &&
34                                 chip->of_xlate &&
35                                 chip->of_xlate(chip, gpiospec, NULL) >= 0;
36 }
37
38 static struct gpio_chip *of_find_gpiochip_by_xlate(
39                                         struct of_phandle_args *gpiospec)
40 {
41         return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
42 }
43
44 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
45                                         struct of_phandle_args *gpiospec,
46                                         enum of_gpio_flags *flags)
47 {
48         int ret;
49
50         if (chip->of_gpio_n_cells != gpiospec->args_count)
51                 return ERR_PTR(-EINVAL);
52
53         ret = chip->of_xlate(chip, gpiospec, flags);
54         if (ret < 0)
55                 return ERR_PTR(ret);
56
57         return gpiochip_get_desc(chip, ret);
58 }
59
60 /**
61  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
62  * @np:         device node to get GPIO from
63  * @propname:   property name containing gpio specifier(s)
64  * @index:      index of the GPIO
65  * @flags:      a flags pointer to fill in
66  *
67  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
68  * value on the error condition. If @flags is not NULL the function also fills
69  * in flags for the GPIO.
70  */
71 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
72                      const char *propname, int index, enum of_gpio_flags *flags)
73 {
74         struct of_phandle_args gpiospec;
75         struct gpio_chip *chip;
76         struct gpio_desc *desc;
77         int ret;
78
79         ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
80                                          &gpiospec);
81         if (ret) {
82                 pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
83                         __func__, propname, np ? np->full_name : NULL, index);
84                 return ERR_PTR(ret);
85         }
86
87         chip = of_find_gpiochip_by_xlate(&gpiospec);
88         if (!chip) {
89                 desc = ERR_PTR(-EPROBE_DEFER);
90                 goto out;
91         }
92
93         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
94         if (IS_ERR(desc))
95                 goto out;
96
97         pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
98                  __func__, propname, np->full_name, index,
99                  PTR_ERR_OR_ZERO(desc));
100
101 out:
102         of_node_put(gpiospec.np);
103
104         return desc;
105 }
106
107 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
108                             int index, enum of_gpio_flags *flags)
109 {
110         struct gpio_desc *desc;
111
112         desc = of_get_named_gpiod_flags(np, list_name, index, flags);
113
114         if (IS_ERR(desc))
115                 return PTR_ERR(desc);
116         else
117                 return desc_to_gpio(desc);
118 }
119 EXPORT_SYMBOL(of_get_named_gpio_flags);
120
121 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
122                                unsigned int idx,
123                                enum gpio_lookup_flags *flags)
124 {
125         char prop_name[32]; /* 32 is max size of property name */
126         enum of_gpio_flags of_flags;
127         struct gpio_desc *desc;
128         unsigned int i;
129
130         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
131                 if (con_id)
132                         snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
133                                  gpio_suffixes[i]);
134                 else
135                         snprintf(prop_name, sizeof(prop_name), "%s",
136                                  gpio_suffixes[i]);
137
138                 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
139                                                 &of_flags);
140                 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
141                         break;
142         }
143
144         if (IS_ERR(desc))
145                 return desc;
146
147         if (of_flags & OF_GPIO_ACTIVE_LOW)
148                 *flags |= GPIO_ACTIVE_LOW;
149
150         if (of_flags & OF_GPIO_SINGLE_ENDED) {
151                 if (of_flags & OF_GPIO_ACTIVE_LOW)
152                         *flags |= GPIO_OPEN_DRAIN;
153                 else
154                         *flags |= GPIO_OPEN_SOURCE;
155         }
156
157         return desc;
158 }
159
160 /**
161  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
162  * @np:         device node to get GPIO from
163  * @chip:       GPIO chip whose hog is parsed
164  * @name:       GPIO line name
165  * @lflags:     gpio_lookup_flags - returned from of_find_gpio() or
166  *              of_parse_own_gpio()
167  * @dflags:     gpiod_flags - optional GPIO initialization flags
168  *
169  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
170  * value on the error condition.
171  */
172 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
173                                            struct gpio_chip *chip,
174                                            const char **name,
175                                            enum gpio_lookup_flags *lflags,
176                                            enum gpiod_flags *dflags)
177 {
178         struct device_node *chip_np;
179         enum of_gpio_flags xlate_flags;
180         struct of_phandle_args gpiospec;
181         struct gpio_desc *desc;
182         u32 tmp;
183         int ret;
184
185         chip_np = chip->of_node;
186         if (!chip_np)
187                 return ERR_PTR(-EINVAL);
188
189         xlate_flags = 0;
190         *lflags = 0;
191         *dflags = 0;
192
193         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
194         if (ret)
195                 return ERR_PTR(ret);
196
197         gpiospec.np = chip_np;
198         gpiospec.args_count = tmp;
199
200         ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
201         if (ret)
202                 return ERR_PTR(ret);
203
204         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
205         if (IS_ERR(desc))
206                 return desc;
207
208         if (xlate_flags & OF_GPIO_ACTIVE_LOW)
209                 *lflags |= GPIO_ACTIVE_LOW;
210
211         if (of_property_read_bool(np, "input"))
212                 *dflags |= GPIOD_IN;
213         else if (of_property_read_bool(np, "output-low"))
214                 *dflags |= GPIOD_OUT_LOW;
215         else if (of_property_read_bool(np, "output-high"))
216                 *dflags |= GPIOD_OUT_HIGH;
217         else {
218                 pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
219                         desc_to_gpio(desc), np->name);
220                 return ERR_PTR(-EINVAL);
221         }
222
223         if (name && of_property_read_string(np, "line-name", name))
224                 *name = np->name;
225
226         return desc;
227 }
228
229 /**
230  * of_gpiochip_set_names() - set up the names of the lines
231  * @chip: GPIO chip whose lines should be named, if possible
232  */
233 static void of_gpiochip_set_names(struct gpio_chip *gc)
234 {
235         struct gpio_device *gdev = gc->gpiodev;
236         struct device_node *np = gc->of_node;
237         int i;
238         int nstrings;
239
240         nstrings = of_property_count_strings(np, "gpio-line-names");
241         if (nstrings <= 0)
242                 /* Lines names not present */
243                 return;
244
245         /* This is normally not what you want */
246         if (gdev->ngpio != nstrings)
247                 dev_info(&gdev->dev, "gpio-line-names specifies %d line "
248                          "names but there are %d lines on the chip\n",
249                          nstrings, gdev->ngpio);
250
251         /*
252          * Make sure to not index beyond the end of the number of descriptors
253          * of the GPIO device.
254          */
255         for (i = 0; i < gdev->ngpio; i++) {
256                 const char *name;
257                 int ret;
258
259                 ret = of_property_read_string_index(np,
260                                                     "gpio-line-names",
261                                                     i,
262                                                     &name);
263                 if (ret) {
264                         if (ret != -ENODATA)
265                                 dev_err(&gdev->dev,
266                                         "unable to name line %d: %d\n",
267                                         i, ret);
268                         break;
269                 }
270                 gdev->descs[i].name = name;
271         }
272 }
273
274 /**
275  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
276  * @chip:       gpio chip to act on
277  *
278  * This is only used by of_gpiochip_add to request/set GPIO initial
279  * configuration.
280  * It retures error if it fails otherwise 0 on success.
281  */
282 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
283 {
284         struct gpio_desc *desc = NULL;
285         struct device_node *np;
286         const char *name;
287         enum gpio_lookup_flags lflags;
288         enum gpiod_flags dflags;
289         int ret;
290
291         for_each_available_child_of_node(chip->of_node, np) {
292                 if (!of_property_read_bool(np, "gpio-hog"))
293                         continue;
294
295                 desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
296                 if (IS_ERR(desc))
297                         continue;
298
299                 ret = gpiod_hog(desc, name, lflags, dflags);
300                 if (ret < 0)
301                         return ret;
302         }
303
304         return 0;
305 }
306
307 /**
308  * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
309  * @gc:         pointer to the gpio_chip structure
310  * @np:         device node of the GPIO chip
311  * @gpio_spec:  gpio specifier as found in the device tree
312  * @flags:      a flags pointer to fill in
313  *
314  * This is simple translation function, suitable for the most 1:1 mapped
315  * gpio chips. This function performs only one sanity check: whether gpio
316  * is less than ngpios (that is specified in the gpio_chip).
317  */
318 int of_gpio_simple_xlate(struct gpio_chip *gc,
319                          const struct of_phandle_args *gpiospec, u32 *flags)
320 {
321         /*
322          * We're discouraging gpio_cells < 2, since that way you'll have to
323          * write your own xlate function (that will have to retrieve the GPIO
324          * number and the flags from a single gpio cell -- this is possible,
325          * but not recommended).
326          */
327         if (gc->of_gpio_n_cells < 2) {
328                 WARN_ON(1);
329                 return -EINVAL;
330         }
331
332         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
333                 return -EINVAL;
334
335         if (gpiospec->args[0] >= gc->ngpio)
336                 return -EINVAL;
337
338         if (flags)
339                 *flags = gpiospec->args[1];
340
341         return gpiospec->args[0];
342 }
343 EXPORT_SYMBOL(of_gpio_simple_xlate);
344
345 /**
346  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
347  * @np:         device node of the GPIO chip
348  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
349  * @data:       driver data to store in the struct gpio_chip
350  *
351  * To use this function you should allocate and fill mm_gc with:
352  *
353  * 1) In the gpio_chip structure:
354  *    - all the callbacks
355  *    - of_gpio_n_cells
356  *    - of_xlate callback (optional)
357  *
358  * 3) In the of_mm_gpio_chip structure:
359  *    - save_regs callback (optional)
360  *
361  * If succeeded, this function will map bank's memory and will
362  * do all necessary work for you. Then you'll able to use .regs
363  * to manage GPIOs from the callbacks.
364  */
365 int of_mm_gpiochip_add_data(struct device_node *np,
366                             struct of_mm_gpio_chip *mm_gc,
367                             void *data)
368 {
369         int ret = -ENOMEM;
370         struct gpio_chip *gc = &mm_gc->gc;
371
372         gc->label = kstrdup(np->full_name, GFP_KERNEL);
373         if (!gc->label)
374                 goto err0;
375
376         mm_gc->regs = of_iomap(np, 0);
377         if (!mm_gc->regs)
378                 goto err1;
379
380         gc->base = -1;
381
382         if (mm_gc->save_regs)
383                 mm_gc->save_regs(mm_gc);
384
385         mm_gc->gc.of_node = np;
386
387         ret = gpiochip_add_data(gc, data);
388         if (ret)
389                 goto err2;
390
391         return 0;
392 err2:
393         iounmap(mm_gc->regs);
394 err1:
395         kfree(gc->label);
396 err0:
397         pr_err("%s: GPIO chip registration failed with status %d\n",
398                np->full_name, ret);
399         return ret;
400 }
401 EXPORT_SYMBOL(of_mm_gpiochip_add_data);
402
403 /**
404  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
405  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
406  */
407 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
408 {
409         struct gpio_chip *gc = &mm_gc->gc;
410
411         if (!mm_gc)
412                 return;
413
414         gpiochip_remove(gc);
415         iounmap(mm_gc->regs);
416         kfree(gc->label);
417 }
418 EXPORT_SYMBOL(of_mm_gpiochip_remove);
419
420 #ifdef CONFIG_PINCTRL
421 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
422 {
423         struct device_node *np = chip->of_node;
424         struct of_phandle_args pinspec;
425         struct pinctrl_dev *pctldev;
426         int index = 0, ret;
427         const char *name;
428         static const char group_names_propname[] = "gpio-ranges-group-names";
429         struct property *group_names;
430
431         if (!np)
432                 return 0;
433
434         group_names = of_find_property(np, group_names_propname, NULL);
435
436         for (;; index++) {
437                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
438                                 index, &pinspec);
439                 if (ret)
440                         break;
441
442                 pctldev = of_pinctrl_get(pinspec.np);
443                 of_node_put(pinspec.np);
444                 if (!pctldev)
445                         return -EPROBE_DEFER;
446
447                 if (pinspec.args[2]) {
448                         if (group_names) {
449                                 of_property_read_string_index(np,
450                                                 group_names_propname,
451                                                 index, &name);
452                                 if (strlen(name)) {
453                                         pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
454                                                 np->full_name);
455                                         break;
456                                 }
457                         }
458                         /* npins != 0: linear range */
459                         ret = gpiochip_add_pin_range(chip,
460                                         pinctrl_dev_get_devname(pctldev),
461                                         pinspec.args[0],
462                                         pinspec.args[1],
463                                         pinspec.args[2]);
464                         if (ret)
465                                 return ret;
466                 } else {
467                         /* npins == 0: special range */
468                         if (pinspec.args[1]) {
469                                 pr_err("%s: Illegal gpio-range format.\n",
470                                         np->full_name);
471                                 break;
472                         }
473
474                         if (!group_names) {
475                                 pr_err("%s: GPIO group range requested but no %s property.\n",
476                                         np->full_name, group_names_propname);
477                                 break;
478                         }
479
480                         ret = of_property_read_string_index(np,
481                                                 group_names_propname,
482                                                 index, &name);
483                         if (ret)
484                                 break;
485
486                         if (!strlen(name)) {
487                                 pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
488                                 np->full_name);
489                                 break;
490                         }
491
492                         ret = gpiochip_add_pingroup_range(chip, pctldev,
493                                                 pinspec.args[0], name);
494                         if (ret)
495                                 return ret;
496                 }
497         }
498
499         return 0;
500 }
501
502 #else
503 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
504 #endif
505
506 int of_gpiochip_add(struct gpio_chip *chip)
507 {
508         int status;
509
510         if ((!chip->of_node) && (chip->parent))
511                 chip->of_node = chip->parent->of_node;
512
513         if (!chip->of_node)
514                 return 0;
515
516         if (!chip->of_xlate) {
517                 chip->of_gpio_n_cells = 2;
518                 chip->of_xlate = of_gpio_simple_xlate;
519         }
520
521         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
522                 return -EINVAL;
523
524         status = of_gpiochip_add_pin_range(chip);
525         if (status)
526                 return status;
527
528         /* If the chip defines names itself, these take precedence */
529         if (!chip->names)
530                 of_gpiochip_set_names(chip);
531
532         of_node_get(chip->of_node);
533
534         status = of_gpiochip_scan_gpios(chip);
535         if (status) {
536                 of_node_put(chip->of_node);
537                 gpiochip_remove_pin_ranges(chip);
538         }
539
540         return status;
541 }
542
543 void of_gpiochip_remove(struct gpio_chip *chip)
544 {
545         gpiochip_remove_pin_ranges(chip);
546         of_node_put(chip->of_node);
547 }