blob: 7b41fd8be575cf2efcfb1fb2428af0586c219bf3 [file] [log] [blame]
Anton Vorontsov863fbf42008-04-11 23:06:45 +10001/*
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
Grant Likely2e13cba2010-07-05 16:11:55 -060014#include <linux/device.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100015#include <linux/errno.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060016#include <linux/module.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100017#include <linux/io.h>
18#include <linux/of.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060019#include <linux/of_address.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100020#include <linux/of_gpio.h>
Shiraz Hashime77d22c2012-10-27 15:21:36 +053021#include <linux/pinctrl/pinctrl.h>
Grant Likely2e13cba2010-07-05 16:11:55 -060022#include <linux/slab.h>
Anton Vorontsov863fbf42008-04-11 23:06:45 +100023
24/**
John Bonesioa6b09192011-06-27 16:49:57 -070025 * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
Anton Vorontsov863fbf42008-04-11 23:06:45 +100026 * @np: device node to get GPIO from
John Bonesioa6b09192011-06-27 16:49:57 -070027 * @propname: property name containing gpio specifier(s)
Anton Vorontsov863fbf42008-04-11 23:06:45 +100028 * @index: index of the GPIO
Anton Vorontsovb908b532008-12-01 06:30:04 +000029 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +100030 *
31 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
Anton Vorontsovb908b532008-12-01 06:30:04 +000032 * value on the error condition. If @flags is not NULL the function also fills
33 * in flags for the GPIO.
Anton Vorontsov863fbf42008-04-11 23:06:45 +100034 */
John Bonesioa6b09192011-06-27 16:49:57 -070035int of_get_named_gpio_flags(struct device_node *np, const char *propname,
36 int index, enum of_gpio_flags *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +100037{
Anton Vorontsov64b60e02008-10-10 04:43:17 +000038 int ret;
Anton Vorontsova19e3da2010-06-08 07:48:16 -060039 struct gpio_chip *gc;
Grant Likely15c9a0a2011-12-12 09:25:57 -070040 struct of_phandle_args gpiospec;
Anton Vorontsov863fbf42008-04-11 23:06:45 +100041
Grant Likely15c9a0a2011-12-12 09:25:57 -070042 ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
43 &gpiospec);
Anton Vorontsov64b60e02008-10-10 04:43:17 +000044 if (ret) {
45 pr_debug("%s: can't parse gpios property\n", __func__);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100046 goto err0;
47 }
Anton Vorontsov863fbf42008-04-11 23:06:45 +100048
Grant Likely15c9a0a2011-12-12 09:25:57 -070049 gc = of_node_to_gpiochip(gpiospec.np);
Anton Vorontsova19e3da2010-06-08 07:48:16 -060050 if (!gc) {
Anton Vorontsov64b60e02008-10-10 04:43:17 +000051 pr_debug("%s: gpio controller %s isn't registered\n",
Grant Likely15c9a0a2011-12-12 09:25:57 -070052 np->full_name, gpiospec.np->full_name);
Roland Stiggebc841ce2012-06-28 00:32:14 +020053 ret = -EPROBE_DEFER;
Anton Vorontsov64b60e02008-10-10 04:43:17 +000054 goto err1;
55 }
56
Grant Likely15c9a0a2011-12-12 09:25:57 -070057 if (gpiospec.args_count != gc->of_gpio_n_cells) {
Anton Vorontsov64b60e02008-10-10 04:43:17 +000058 pr_debug("%s: wrong #gpio-cells for %s\n",
Grant Likely15c9a0a2011-12-12 09:25:57 -070059 np->full_name, gpiospec.np->full_name);
Anton Vorontsov64b60e02008-10-10 04:43:17 +000060 ret = -EINVAL;
61 goto err1;
Anton Vorontsov863fbf42008-04-11 23:06:45 +100062 }
63
Anton Vorontsovb908b532008-12-01 06:30:04 +000064 /* .xlate might decide to not fill in the flags, so clear it. */
65 if (flags)
66 *flags = 0;
67
Grant Likely15c9a0a2011-12-12 09:25:57 -070068 ret = gc->of_xlate(gc, &gpiospec, flags);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100069 if (ret < 0)
70 goto err1;
71
Anton Vorontsova19e3da2010-06-08 07:48:16 -060072 ret += gc->base;
Anton Vorontsov863fbf42008-04-11 23:06:45 +100073err1:
Grant Likely15c9a0a2011-12-12 09:25:57 -070074 of_node_put(gpiospec.np);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100075err0:
76 pr_debug("%s exited with status %d\n", __func__, ret);
77 return ret;
78}
John Bonesioa6b09192011-06-27 16:49:57 -070079EXPORT_SYMBOL(of_get_named_gpio_flags);
Anton Vorontsov863fbf42008-04-11 23:06:45 +100080
81/**
Jean-Christophe PLAGNIOL-VILLARDff64abe2012-02-02 16:20:01 +010082 * of_gpio_named_count - Count GPIOs for a device
Anton Vorontsov74982092008-12-05 08:15:54 +000083 * @np: device node to count GPIOs for
Jean-Christophe PLAGNIOL-VILLARDff64abe2012-02-02 16:20:01 +010084 * @propname: property name containing gpio specifier(s)
Anton Vorontsov74982092008-12-05 08:15:54 +000085 *
86 * The function returns the count of GPIOs specified for a node.
87 *
88 * Note that the empty GPIO specifiers counts too. For example,
89 *
90 * gpios = <0
91 * &pio1 1 2
92 * 0
93 * &pio2 3 4>;
94 *
95 * defines four GPIOs (so this function will return 4), two of which
96 * are not specified.
97 */
Jean-Christophe PLAGNIOL-VILLARDff64abe2012-02-02 16:20:01 +010098unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
Anton Vorontsov74982092008-12-05 08:15:54 +000099{
100 unsigned int cnt = 0;
101
102 do {
103 int ret;
104
Jean-Christophe PLAGNIOL-VILLARDff64abe2012-02-02 16:20:01 +0100105 ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
Grant Likely15c9a0a2011-12-12 09:25:57 -0700106 cnt, NULL);
Anton Vorontsov74982092008-12-05 08:15:54 +0000107 /* A hole in the gpios = <> counts anyway. */
108 if (ret < 0 && ret != -EEXIST)
109 break;
110 } while (++cnt);
111
112 return cnt;
113}
Jean-Christophe PLAGNIOL-VILLARDff64abe2012-02-02 16:20:01 +0100114EXPORT_SYMBOL(of_gpio_named_count);
Anton Vorontsov74982092008-12-05 08:15:54 +0000115
116/**
Anton Vorontsovb908b532008-12-01 06:30:04 +0000117 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600118 * @gc: pointer to the gpio_chip structure
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000119 * @np: device node of the GPIO chip
120 * @gpio_spec: gpio specifier as found in the device tree
Anton Vorontsovb908b532008-12-01 06:30:04 +0000121 * @flags: a flags pointer to fill in
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000122 *
123 * This is simple translation function, suitable for the most 1:1 mapped
124 * gpio chips. This function performs only one sanity check: whether gpio
125 * is less than ngpios (that is specified in the gpio_chip).
126 */
Grant Likely15c9a0a2011-12-12 09:25:57 -0700127int of_gpio_simple_xlate(struct gpio_chip *gc,
128 const struct of_phandle_args *gpiospec, u32 *flags)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000129{
Anton Vorontsovb908b532008-12-01 06:30:04 +0000130 /*
131 * We're discouraging gpio_cells < 2, since that way you'll have to
132 * write your own xlate function (that will have to retrive the GPIO
133 * number and the flags from a single gpio cell -- this is possible,
134 * but not recommended).
135 */
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600136 if (gc->of_gpio_n_cells < 2) {
Anton Vorontsovb908b532008-12-01 06:30:04 +0000137 WARN_ON(1);
138 return -EINVAL;
139 }
140
Grant Likely15c9a0a2011-12-12 09:25:57 -0700141 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
142 return -EINVAL;
143
Roland Stigge6270d832012-04-04 02:02:58 +0200144 if (gpiospec->args[0] >= gc->ngpio)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000145 return -EINVAL;
146
Anton Vorontsovb908b532008-12-01 06:30:04 +0000147 if (flags)
Grant Likely15c9a0a2011-12-12 09:25:57 -0700148 *flags = gpiospec->args[1];
Anton Vorontsovb908b532008-12-01 06:30:04 +0000149
Grant Likely15c9a0a2011-12-12 09:25:57 -0700150 return gpiospec->args[0];
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000151}
Jamie Iles3038bbd2011-07-28 16:25:41 +0100152EXPORT_SYMBOL(of_gpio_simple_xlate);
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000153
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000154/**
155 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
156 * @np: device node of the GPIO chip
157 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
158 *
159 * To use this function you should allocate and fill mm_gc with:
160 *
161 * 1) In the gpio_chip structure:
162 * - all the callbacks
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600163 * - of_gpio_n_cells
164 * - of_xlate callback (optional)
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000165 *
166 * 3) In the of_mm_gpio_chip structure:
167 * - save_regs callback (optional)
168 *
169 * If succeeded, this function will map bank's memory and will
170 * do all necessary work for you. Then you'll able to use .regs
171 * to manage GPIOs from the callbacks.
172 */
173int of_mm_gpiochip_add(struct device_node *np,
174 struct of_mm_gpio_chip *mm_gc)
175{
176 int ret = -ENOMEM;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600177 struct gpio_chip *gc = &mm_gc->gc;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000178
179 gc->label = kstrdup(np->full_name, GFP_KERNEL);
180 if (!gc->label)
181 goto err0;
182
183 mm_gc->regs = of_iomap(np, 0);
184 if (!mm_gc->regs)
185 goto err1;
186
Anton Vorontsov21451152008-04-30 00:05:24 +1000187 gc->base = -1;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000188
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000189 if (mm_gc->save_regs)
190 mm_gc->save_regs(mm_gc);
191
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600192 mm_gc->gc.of_node = np;
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000193
194 ret = gpiochip_add(gc);
195 if (ret)
196 goto err2;
197
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000198 return 0;
199err2:
Anton Vorontsov863fbf42008-04-11 23:06:45 +1000200 iounmap(mm_gc->regs);
201err1:
202 kfree(gc->label);
203err0:
204 pr_err("%s: GPIO chip registration failed with status %d\n",
205 np->full_name, ret);
206 return ret;
207}
208EXPORT_SYMBOL(of_mm_gpiochip_add);
Grant Likely594fa262010-06-08 07:48:16 -0600209
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530210#ifdef CONFIG_PINCTRL
211void of_gpiochip_add_pin_range(struct gpio_chip *chip)
212{
213 struct device_node *np = chip->of_node;
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530214 struct of_phandle_args pinspec;
Linus Walleij4aca0362012-11-06 16:03:35 +0100215 struct pinctrl_dev *pctldev;
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530216 int index = 0, ret;
217
218 if (!np)
219 return;
220
221 do {
222 ret = of_parse_phandle_with_args(np, "gpio-ranges",
223 "#gpio-range-cells", index, &pinspec);
224 if (ret)
225 break;
226
Linus Walleij4aca0362012-11-06 16:03:35 +0100227 pctldev = of_pinctrl_get(pinspec.np);
228 if (!pctldev)
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530229 break;
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530230
Linus Walleij4aca0362012-11-06 16:03:35 +0100231 ret = gpiochip_add_pin_range(chip,
232 pinctrl_dev_get_name(pctldev),
233 pinspec.args[0],
234 pinspec.args[1]);
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530235
Linus Walleij4aca0362012-11-06 16:03:35 +0100236 if (ret)
237 break;
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530238
239 } while (index++);
240}
241
242void of_gpiochip_remove_pin_range(struct gpio_chip *chip)
243{
244 struct gpio_pin_range *pin_range, *tmp;
245
246 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
247 list_del(&pin_range->node);
248 pinctrl_remove_gpio_range(pin_range->pctldev,
249 &pin_range->range);
250 }
251}
252#else
253void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
254void of_gpiochip_remove_pin_range(struct gpio_chip *chip) {}
255#endif
256
Anton Vorontsov391c9702010-06-08 07:48:17 -0600257void of_gpiochip_add(struct gpio_chip *chip)
258{
259 if ((!chip->of_node) && (chip->dev))
260 chip->of_node = chip->dev->of_node;
261
262 if (!chip->of_node)
263 return;
264
265 if (!chip->of_xlate) {
266 chip->of_gpio_n_cells = 2;
267 chip->of_xlate = of_gpio_simple_xlate;
268 }
269
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530270 of_gpiochip_add_pin_range(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600271 of_node_get(chip->of_node);
272}
273
274void of_gpiochip_remove(struct gpio_chip *chip)
275{
Shiraz Hashime77d22c2012-10-27 15:21:36 +0530276 of_gpiochip_remove_pin_range(chip);
277
Anton Vorontsov391c9702010-06-08 07:48:17 -0600278 if (chip->of_node)
279 of_node_put(chip->of_node);
280}
281
Grant Likely594fa262010-06-08 07:48:16 -0600282/* Private function for resolving node pointer to gpio_chip */
Grant Likely6e2cf652012-03-02 15:56:03 -0700283static int of_gpiochip_is_match(struct gpio_chip *chip, const void *data)
Grant Likely594fa262010-06-08 07:48:16 -0600284{
285 return chip->of_node == data;
286}
287
288struct gpio_chip *of_node_to_gpiochip(struct device_node *np)
289{
290 return gpiochip_find(np, of_gpiochip_is_match);
291}