gpio: of: remove of_gpiochip_and_xlate() and struct gg_data
[cascardo/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/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(struct gpio_chip *chip, void *data)
30 {
31         return chip->gpiodev->dev.of_node == data;
32 }
33
34 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
35 {
36         return gpiochip_find(np, of_gpiochip_match_node);
37 }
38
39 /**
40  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
41  * @np:         device node to get GPIO from
42  * @propname:   property name containing gpio specifier(s)
43  * @index:      index of the GPIO
44  * @flags:      a flags pointer to fill in
45  *
46  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
47  * value on the error condition. If @flags is not NULL the function also fills
48  * in flags for the GPIO.
49  */
50 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
51                      const char *propname, int index, enum of_gpio_flags *flags)
52 {
53         struct of_phandle_args gpiospec;
54         struct gpio_chip *chip;
55         struct gpio_desc *desc;
56         int ret;
57
58         ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
59                                          &gpiospec);
60         if (ret) {
61                 pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
62                         __func__, propname, np->full_name, index);
63                 return ERR_PTR(ret);
64         }
65
66         chip = of_find_gpiochip_by_node(gpiospec.np);
67         if (!chip) {
68                 desc = ERR_PTR(-EPROBE_DEFER);
69                 goto out;
70         }
71         if (chip->of_gpio_n_cells != gpiospec.args_count) {
72                 desc = ERR_PTR(-EINVAL);
73                 goto out;
74         }
75
76         ret = chip->of_xlate(chip, &gpiospec, flags);
77         if (ret < 0) {
78                 desc = ERR_PTR(ret);
79                 goto out;
80         }
81
82         desc = gpiochip_get_desc(chip, ret);
83         if (IS_ERR(desc))
84                 goto out;
85
86         pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
87                  __func__, propname, np->full_name, index,
88                  PTR_ERR_OR_ZERO(desc));
89
90 out:
91         of_node_put(gpiospec.np);
92
93         return desc;
94 }
95
96 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
97                             int index, enum of_gpio_flags *flags)
98 {
99         struct gpio_desc *desc;
100
101         desc = of_get_named_gpiod_flags(np, list_name, index, flags);
102
103         if (IS_ERR(desc))
104                 return PTR_ERR(desc);
105         else
106                 return desc_to_gpio(desc);
107 }
108 EXPORT_SYMBOL(of_get_named_gpio_flags);
109
110 /**
111  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
112  * @np:         device node to get GPIO from
113  * @chip:       GPIO chip whose hog is parsed
114  * @name:       GPIO line name
115  * @lflags:     gpio_lookup_flags - returned from of_find_gpio() or
116  *              of_parse_own_gpio()
117  * @dflags:     gpiod_flags - optional GPIO initialization flags
118  *
119  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
120  * value on the error condition.
121  */
122 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
123                                            struct gpio_chip *chip,
124                                            const char **name,
125                                            enum gpio_lookup_flags *lflags,
126                                            enum gpiod_flags *dflags)
127 {
128         struct device_node *chip_np;
129         enum of_gpio_flags xlate_flags;
130         struct of_phandle_args gpiospec;
131         struct gpio_desc *desc;
132         u32 tmp;
133         int ret;
134
135         chip_np = chip->of_node;
136         if (!chip_np)
137                 return ERR_PTR(-EINVAL);
138
139         xlate_flags = 0;
140         *lflags = 0;
141         *dflags = 0;
142
143         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
144         if (ret)
145                 return ERR_PTR(ret);
146
147         if (tmp != chip->of_gpio_n_cells)
148                 return ERR_PTR(-EINVAL);
149
150         gpiospec.np = chip_np;
151         gpiospec.args_count = tmp;
152
153         ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
154         if (ret)
155                 return ERR_PTR(ret);
156
157         ret = chip->of_xlate(chip, &gpiospec, &xlate_flags);
158         if (ret < 0)
159                 return ERR_PTR(ret);
160
161         desc = gpiochip_get_desc(chip, ret);
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 }