xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 1e63d7b9)
1f141ed65SGrant Likely /*
2f141ed65SGrant Likely  * OF helpers for the GPIO API
3f141ed65SGrant Likely  *
4f141ed65SGrant Likely  * Copyright (c) 2007-2008  MontaVista Software, Inc.
5f141ed65SGrant Likely  *
6f141ed65SGrant Likely  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7f141ed65SGrant Likely  *
8f141ed65SGrant Likely  * This program is free software; you can redistribute it and/or modify
9f141ed65SGrant Likely  * it under the terms of the GNU General Public License as published by
10f141ed65SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
11f141ed65SGrant Likely  * (at your option) any later version.
12f141ed65SGrant Likely  */
13f141ed65SGrant Likely 
14f141ed65SGrant Likely #include <linux/device.h>
15f141ed65SGrant Likely #include <linux/errno.h>
16f141ed65SGrant Likely #include <linux/module.h>
17f141ed65SGrant Likely #include <linux/io.h>
183d0f7cf0SGrant Likely #include <linux/gpio.h>
19f141ed65SGrant Likely #include <linux/of.h>
20f141ed65SGrant Likely #include <linux/of_address.h>
21f141ed65SGrant Likely #include <linux/of_gpio.h>
22f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h>
23f141ed65SGrant Likely #include <linux/slab.h>
24f141ed65SGrant Likely 
250df2c999SDong Aisheng /* Private data structure for of_gpiochip_find_and_xlate */
263d0f7cf0SGrant Likely struct gg_data {
273d0f7cf0SGrant Likely 	enum of_gpio_flags *flags;
283d0f7cf0SGrant Likely 	struct of_phandle_args gpiospec;
293d0f7cf0SGrant Likely 
303d0f7cf0SGrant Likely 	int out_gpio;
313d0f7cf0SGrant Likely };
323d0f7cf0SGrant Likely 
333d0f7cf0SGrant Likely /* Private function for resolving node pointer to gpio_chip */
343d0f7cf0SGrant Likely static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
353d0f7cf0SGrant Likely {
363d0f7cf0SGrant Likely 	struct gg_data *gg_data = data;
373d0f7cf0SGrant Likely 	int ret;
383d0f7cf0SGrant Likely 
393d0f7cf0SGrant Likely 	if ((gc->of_node != gg_data->gpiospec.np) ||
403d0f7cf0SGrant Likely 	    (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
413d0f7cf0SGrant Likely 	    (!gc->of_xlate))
423d0f7cf0SGrant Likely 		return false;
433d0f7cf0SGrant Likely 
443d0f7cf0SGrant Likely 	ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
453d0f7cf0SGrant Likely 	if (ret < 0)
463d0f7cf0SGrant Likely 		return false;
473d0f7cf0SGrant Likely 
483d0f7cf0SGrant Likely 	gg_data->out_gpio = ret + gc->base;
493d0f7cf0SGrant Likely 	return true;
503d0f7cf0SGrant Likely }
513d0f7cf0SGrant Likely 
52f141ed65SGrant Likely /**
53f141ed65SGrant Likely  * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
54f141ed65SGrant Likely  * @np:		device node to get GPIO from
55f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
56f141ed65SGrant Likely  * @index:	index of the GPIO
57f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
58f141ed65SGrant Likely  *
59f141ed65SGrant Likely  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
60f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
61f141ed65SGrant Likely  * in flags for the GPIO.
62f141ed65SGrant Likely  */
63f141ed65SGrant Likely int of_get_named_gpio_flags(struct device_node *np, const char *propname,
64f141ed65SGrant Likely                            int index, enum of_gpio_flags *flags)
65f141ed65SGrant Likely {
664fbb0022SRoland Stigge 	/* Return -EPROBE_DEFER to support probe() functions to be called
674fbb0022SRoland Stigge 	 * later when the GPIO actually becomes available
684fbb0022SRoland Stigge 	 */
694fbb0022SRoland Stigge 	struct gg_data gg_data = { .flags = flags, .out_gpio = -EPROBE_DEFER };
70f141ed65SGrant Likely 	int ret;
71f141ed65SGrant Likely 
723d0f7cf0SGrant Likely 	/* .of_xlate might decide to not fill in the flags, so clear it. */
73f141ed65SGrant Likely 	if (flags)
74f141ed65SGrant Likely 		*flags = 0;
75f141ed65SGrant Likely 
763d0f7cf0SGrant Likely 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
773d0f7cf0SGrant Likely 					 &gg_data.gpiospec);
783d0f7cf0SGrant Likely 	if (ret) {
793d0f7cf0SGrant Likely 		pr_debug("%s: can't parse gpios property\n", __func__);
8041920d16SAlexandre Courbot 		return ret;
813d0f7cf0SGrant Likely 	}
82f141ed65SGrant Likely 
833d0f7cf0SGrant Likely 	gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
843d0f7cf0SGrant Likely 
853d0f7cf0SGrant Likely 	of_node_put(gg_data.gpiospec.np);
86c61307a7SThierry Reding 	pr_debug("%s exited with status %d\n", __func__, gg_data.out_gpio);
873d0f7cf0SGrant Likely 	return gg_data.out_gpio;
88f141ed65SGrant Likely }
89f141ed65SGrant Likely EXPORT_SYMBOL(of_get_named_gpio_flags);
90f141ed65SGrant Likely 
91f141ed65SGrant Likely /**
92f141ed65SGrant Likely  * of_gpio_named_count - Count GPIOs for a device
93f141ed65SGrant Likely  * @np:		device node to count GPIOs for
94f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
95f141ed65SGrant Likely  *
96f141ed65SGrant Likely  * The function returns the count of GPIOs specified for a node.
97f141ed65SGrant Likely  *
98f141ed65SGrant Likely  * Note that the empty GPIO specifiers counts too. For example,
99f141ed65SGrant Likely  *
100f141ed65SGrant Likely  * gpios = <0
101f141ed65SGrant Likely  *          &pio1 1 2
102f141ed65SGrant Likely  *          0
103f141ed65SGrant Likely  *          &pio2 3 4>;
104f141ed65SGrant Likely  *
105f141ed65SGrant Likely  * defines four GPIOs (so this function will return 4), two of which
106f141ed65SGrant Likely  * are not specified.
107f141ed65SGrant Likely  */
108f141ed65SGrant Likely unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
109f141ed65SGrant Likely {
110f141ed65SGrant Likely 	unsigned int cnt = 0;
111f141ed65SGrant Likely 
112f141ed65SGrant Likely 	do {
113f141ed65SGrant Likely 		int ret;
114f141ed65SGrant Likely 
115f141ed65SGrant Likely 		ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
116f141ed65SGrant Likely 						 cnt, NULL);
117f141ed65SGrant Likely 		/* A hole in the gpios = <> counts anyway. */
118f141ed65SGrant Likely 		if (ret < 0 && ret != -EEXIST)
119f141ed65SGrant Likely 			break;
120f141ed65SGrant Likely 	} while (++cnt);
121f141ed65SGrant Likely 
122f141ed65SGrant Likely 	return cnt;
123f141ed65SGrant Likely }
124f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_named_count);
125f141ed65SGrant Likely 
126f141ed65SGrant Likely /**
127f141ed65SGrant Likely  * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
128f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
129f141ed65SGrant Likely  * @np:		device node of the GPIO chip
130f141ed65SGrant Likely  * @gpio_spec:	gpio specifier as found in the device tree
131f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
132f141ed65SGrant Likely  *
133f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
134f141ed65SGrant Likely  * gpio chips. This function performs only one sanity check: whether gpio
135f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
136f141ed65SGrant Likely  */
137f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
138f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
139f141ed65SGrant Likely {
140f141ed65SGrant Likely 	/*
141f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
142f141ed65SGrant Likely 	 * write your own xlate function (that will have to retrive the GPIO
143f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
144f141ed65SGrant Likely 	 * but not recommended).
145f141ed65SGrant Likely 	 */
146f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
147f141ed65SGrant Likely 		WARN_ON(1);
148f141ed65SGrant Likely 		return -EINVAL;
149f141ed65SGrant Likely 	}
150f141ed65SGrant Likely 
151f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
152f141ed65SGrant Likely 		return -EINVAL;
153f141ed65SGrant Likely 
1547b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
155f141ed65SGrant Likely 		return -EINVAL;
156f141ed65SGrant Likely 
157f141ed65SGrant Likely 	if (flags)
158f141ed65SGrant Likely 		*flags = gpiospec->args[1];
159f141ed65SGrant Likely 
160f141ed65SGrant Likely 	return gpiospec->args[0];
161f141ed65SGrant Likely }
162f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
163f141ed65SGrant Likely 
164f141ed65SGrant Likely /**
165f141ed65SGrant Likely  * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
166f141ed65SGrant Likely  * @np:		device node of the GPIO chip
167f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
168f141ed65SGrant Likely  *
169f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
170f141ed65SGrant Likely  *
171f141ed65SGrant Likely  * 1) In the gpio_chip structure:
172f141ed65SGrant Likely  *    - all the callbacks
173f141ed65SGrant Likely  *    - of_gpio_n_cells
174f141ed65SGrant Likely  *    - of_xlate callback (optional)
175f141ed65SGrant Likely  *
176f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
177f141ed65SGrant Likely  *    - save_regs callback (optional)
178f141ed65SGrant Likely  *
179f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
180f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
181f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
182f141ed65SGrant Likely  */
183f141ed65SGrant Likely int of_mm_gpiochip_add(struct device_node *np,
184f141ed65SGrant Likely 		       struct of_mm_gpio_chip *mm_gc)
185f141ed65SGrant Likely {
186f141ed65SGrant Likely 	int ret = -ENOMEM;
187f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
188f141ed65SGrant Likely 
189f141ed65SGrant Likely 	gc->label = kstrdup(np->full_name, GFP_KERNEL);
190f141ed65SGrant Likely 	if (!gc->label)
191f141ed65SGrant Likely 		goto err0;
192f141ed65SGrant Likely 
193f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
194f141ed65SGrant Likely 	if (!mm_gc->regs)
195f141ed65SGrant Likely 		goto err1;
196f141ed65SGrant Likely 
197f141ed65SGrant Likely 	gc->base = -1;
198f141ed65SGrant Likely 
199f141ed65SGrant Likely 	if (mm_gc->save_regs)
200f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
201f141ed65SGrant Likely 
202f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
203f141ed65SGrant Likely 
204f141ed65SGrant Likely 	ret = gpiochip_add(gc);
205f141ed65SGrant Likely 	if (ret)
206f141ed65SGrant Likely 		goto err2;
207f141ed65SGrant Likely 
208f141ed65SGrant Likely 	return 0;
209f141ed65SGrant Likely err2:
210f141ed65SGrant Likely 	iounmap(mm_gc->regs);
211f141ed65SGrant Likely err1:
212f141ed65SGrant Likely 	kfree(gc->label);
213f141ed65SGrant Likely err0:
214f141ed65SGrant Likely 	pr_err("%s: GPIO chip registration failed with status %d\n",
215f141ed65SGrant Likely 	       np->full_name, ret);
216f141ed65SGrant Likely 	return ret;
217f141ed65SGrant Likely }
218f141ed65SGrant Likely EXPORT_SYMBOL(of_mm_gpiochip_add);
219f141ed65SGrant Likely 
220f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
221167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
222f23f1516SShiraz Hashim {
223f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
224f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
2251e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
226f23f1516SShiraz Hashim 	int index = 0, ret;
227f23f1516SShiraz Hashim 
228f23f1516SShiraz Hashim 	if (!np)
229f23f1516SShiraz Hashim 		return;
230f23f1516SShiraz Hashim 
231f23f1516SShiraz Hashim 	do {
232f23f1516SShiraz Hashim 		ret = of_parse_phandle_with_args(np, "gpio-ranges",
233f23f1516SShiraz Hashim 				"#gpio-range-cells", index, &pinspec);
234f23f1516SShiraz Hashim 		if (ret)
235f23f1516SShiraz Hashim 			break;
236f23f1516SShiraz Hashim 
2371e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
2381e63d7b9SLinus Walleij 		if (!pctldev)
239f23f1516SShiraz Hashim 			break;
240f23f1516SShiraz Hashim 
2411e63d7b9SLinus Walleij 		ret = gpiochip_add_pin_range(chip,
2421e63d7b9SLinus Walleij 					     pinctrl_dev_get_name(pctldev),
2431e63d7b9SLinus Walleij 					     pinspec.args[0],
2441e63d7b9SLinus Walleij 					     pinspec.args[1]);
245f23f1516SShiraz Hashim 
2461e63d7b9SLinus Walleij 		if (ret)
2471e63d7b9SLinus Walleij 			break;
248f23f1516SShiraz Hashim 
249f23f1516SShiraz Hashim 	} while (index++);
250f23f1516SShiraz Hashim }
251f23f1516SShiraz Hashim 
252f23f1516SShiraz Hashim #else
253167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
254f23f1516SShiraz Hashim #endif
255f23f1516SShiraz Hashim 
256f141ed65SGrant Likely void of_gpiochip_add(struct gpio_chip *chip)
257f141ed65SGrant Likely {
258f141ed65SGrant Likely 	if ((!chip->of_node) && (chip->dev))
259f141ed65SGrant Likely 		chip->of_node = chip->dev->of_node;
260f141ed65SGrant Likely 
261f141ed65SGrant Likely 	if (!chip->of_node)
262f141ed65SGrant Likely 		return;
263f141ed65SGrant Likely 
264f141ed65SGrant Likely 	if (!chip->of_xlate) {
265f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
266f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
267f141ed65SGrant Likely 	}
268f141ed65SGrant Likely 
269f23f1516SShiraz Hashim 	of_gpiochip_add_pin_range(chip);
270f141ed65SGrant Likely 	of_node_get(chip->of_node);
271f141ed65SGrant Likely }
272f141ed65SGrant Likely 
273f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
274f141ed65SGrant Likely {
275e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
276f23f1516SShiraz Hashim 
277f141ed65SGrant Likely 	if (chip->of_node)
278f141ed65SGrant Likely 		of_node_put(chip->of_node);
279f141ed65SGrant Likely }
280