xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 86853c83)
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_simple_xlate - translate gpio_spec to the GPIO number and flags
93f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
94f141ed65SGrant Likely  * @np:		device node of the GPIO chip
95f141ed65SGrant Likely  * @gpio_spec:	gpio specifier as found in the device tree
96f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
97f141ed65SGrant Likely  *
98f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
99f141ed65SGrant Likely  * gpio chips. This function performs only one sanity check: whether gpio
100f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
101f141ed65SGrant Likely  */
102f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
103f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
104f141ed65SGrant Likely {
105f141ed65SGrant Likely 	/*
106f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
107f141ed65SGrant Likely 	 * write your own xlate function (that will have to retrive the GPIO
108f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
109f141ed65SGrant Likely 	 * but not recommended).
110f141ed65SGrant Likely 	 */
111f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
112f141ed65SGrant Likely 		WARN_ON(1);
113f141ed65SGrant Likely 		return -EINVAL;
114f141ed65SGrant Likely 	}
115f141ed65SGrant Likely 
116f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
117f141ed65SGrant Likely 		return -EINVAL;
118f141ed65SGrant Likely 
1197b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
120f141ed65SGrant Likely 		return -EINVAL;
121f141ed65SGrant Likely 
122f141ed65SGrant Likely 	if (flags)
123f141ed65SGrant Likely 		*flags = gpiospec->args[1];
124f141ed65SGrant Likely 
125f141ed65SGrant Likely 	return gpiospec->args[0];
126f141ed65SGrant Likely }
127f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
128f141ed65SGrant Likely 
129f141ed65SGrant Likely /**
130f141ed65SGrant Likely  * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
131f141ed65SGrant Likely  * @np:		device node of the GPIO chip
132f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
133f141ed65SGrant Likely  *
134f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
135f141ed65SGrant Likely  *
136f141ed65SGrant Likely  * 1) In the gpio_chip structure:
137f141ed65SGrant Likely  *    - all the callbacks
138f141ed65SGrant Likely  *    - of_gpio_n_cells
139f141ed65SGrant Likely  *    - of_xlate callback (optional)
140f141ed65SGrant Likely  *
141f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
142f141ed65SGrant Likely  *    - save_regs callback (optional)
143f141ed65SGrant Likely  *
144f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
145f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
146f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
147f141ed65SGrant Likely  */
148f141ed65SGrant Likely int of_mm_gpiochip_add(struct device_node *np,
149f141ed65SGrant Likely 		       struct of_mm_gpio_chip *mm_gc)
150f141ed65SGrant Likely {
151f141ed65SGrant Likely 	int ret = -ENOMEM;
152f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
153f141ed65SGrant Likely 
154f141ed65SGrant Likely 	gc->label = kstrdup(np->full_name, GFP_KERNEL);
155f141ed65SGrant Likely 	if (!gc->label)
156f141ed65SGrant Likely 		goto err0;
157f141ed65SGrant Likely 
158f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
159f141ed65SGrant Likely 	if (!mm_gc->regs)
160f141ed65SGrant Likely 		goto err1;
161f141ed65SGrant Likely 
162f141ed65SGrant Likely 	gc->base = -1;
163f141ed65SGrant Likely 
164f141ed65SGrant Likely 	if (mm_gc->save_regs)
165f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
166f141ed65SGrant Likely 
167f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
168f141ed65SGrant Likely 
169f141ed65SGrant Likely 	ret = gpiochip_add(gc);
170f141ed65SGrant Likely 	if (ret)
171f141ed65SGrant Likely 		goto err2;
172f141ed65SGrant Likely 
173f141ed65SGrant Likely 	return 0;
174f141ed65SGrant Likely err2:
175f141ed65SGrant Likely 	iounmap(mm_gc->regs);
176f141ed65SGrant Likely err1:
177f141ed65SGrant Likely 	kfree(gc->label);
178f141ed65SGrant Likely err0:
179f141ed65SGrant Likely 	pr_err("%s: GPIO chip registration failed with status %d\n",
180f141ed65SGrant Likely 	       np->full_name, ret);
181f141ed65SGrant Likely 	return ret;
182f141ed65SGrant Likely }
183f141ed65SGrant Likely EXPORT_SYMBOL(of_mm_gpiochip_add);
184f141ed65SGrant Likely 
185f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
186167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
187f23f1516SShiraz Hashim {
188f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
189f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
1901e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
191f23f1516SShiraz Hashim 	int index = 0, ret;
192f23f1516SShiraz Hashim 
193f23f1516SShiraz Hashim 	if (!np)
194f23f1516SShiraz Hashim 		return;
195f23f1516SShiraz Hashim 
196f23f1516SShiraz Hashim 	do {
197f23f1516SShiraz Hashim 		ret = of_parse_phandle_with_args(np, "gpio-ranges",
198f23f1516SShiraz Hashim 				"#gpio-range-cells", index, &pinspec);
199f23f1516SShiraz Hashim 		if (ret)
200f23f1516SShiraz Hashim 			break;
201f23f1516SShiraz Hashim 
2021e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
2031e63d7b9SLinus Walleij 		if (!pctldev)
204f23f1516SShiraz Hashim 			break;
205f23f1516SShiraz Hashim 
2061e63d7b9SLinus Walleij 		ret = gpiochip_add_pin_range(chip,
207ef5e3eefSHaojian Zhuang 					     pinctrl_dev_get_devname(pctldev),
2081e63d7b9SLinus Walleij 					     pinspec.args[0],
20986853c83SHaojian Zhuang 					     pinspec.args[1],
21086853c83SHaojian Zhuang 					     pinspec.args[2]);
211f23f1516SShiraz Hashim 
2121e63d7b9SLinus Walleij 		if (ret)
2131e63d7b9SLinus Walleij 			break;
214f23f1516SShiraz Hashim 
215f23f1516SShiraz Hashim 	} while (index++);
216f23f1516SShiraz Hashim }
217f23f1516SShiraz Hashim 
218f23f1516SShiraz Hashim #else
219167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
220f23f1516SShiraz Hashim #endif
221f23f1516SShiraz Hashim 
222f141ed65SGrant Likely void of_gpiochip_add(struct gpio_chip *chip)
223f141ed65SGrant Likely {
224f141ed65SGrant Likely 	if ((!chip->of_node) && (chip->dev))
225f141ed65SGrant Likely 		chip->of_node = chip->dev->of_node;
226f141ed65SGrant Likely 
227f141ed65SGrant Likely 	if (!chip->of_node)
228f141ed65SGrant Likely 		return;
229f141ed65SGrant Likely 
230f141ed65SGrant Likely 	if (!chip->of_xlate) {
231f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
232f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
233f141ed65SGrant Likely 	}
234f141ed65SGrant Likely 
235f23f1516SShiraz Hashim 	of_gpiochip_add_pin_range(chip);
236f141ed65SGrant Likely 	of_node_get(chip->of_node);
237f141ed65SGrant Likely }
238f141ed65SGrant Likely 
239f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
240f141ed65SGrant Likely {
241e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
242f23f1516SShiraz Hashim 
243f141ed65SGrant Likely 	if (chip->of_node)
244f141ed65SGrant Likely 		of_node_put(chip->of_node);
245f141ed65SGrant Likely }
246