xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 7b8792bb)
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>
15bea4dbeeSSachin Kamat #include <linux/err.h>
16f141ed65SGrant Likely #include <linux/errno.h>
17f141ed65SGrant Likely #include <linux/module.h>
18f141ed65SGrant Likely #include <linux/io.h>
19af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h>
20f141ed65SGrant Likely #include <linux/of.h>
21f141ed65SGrant Likely #include <linux/of_address.h>
22f141ed65SGrant Likely #include <linux/of_gpio.h>
23f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h>
24f141ed65SGrant Likely #include <linux/slab.h>
25f141ed65SGrant Likely 
261bd6b601SAlexandre Courbot #include "gpiolib.h"
27af8b6375SAlexandre Courbot 
280df2c999SDong Aisheng /* Private data structure for of_gpiochip_find_and_xlate */
293d0f7cf0SGrant Likely struct gg_data {
303d0f7cf0SGrant Likely 	enum of_gpio_flags *flags;
313d0f7cf0SGrant Likely 	struct of_phandle_args gpiospec;
323d0f7cf0SGrant Likely 
33af8b6375SAlexandre Courbot 	struct gpio_desc *out_gpio;
343d0f7cf0SGrant Likely };
353d0f7cf0SGrant Likely 
363d0f7cf0SGrant Likely /* Private function for resolving node pointer to gpio_chip */
373d0f7cf0SGrant Likely static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
383d0f7cf0SGrant Likely {
393d0f7cf0SGrant Likely 	struct gg_data *gg_data = data;
403d0f7cf0SGrant Likely 	int ret;
413d0f7cf0SGrant Likely 
423d0f7cf0SGrant Likely 	if ((gc->of_node != gg_data->gpiospec.np) ||
433d0f7cf0SGrant Likely 	    (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
443d0f7cf0SGrant Likely 	    (!gc->of_xlate))
453d0f7cf0SGrant Likely 		return false;
463d0f7cf0SGrant Likely 
473d0f7cf0SGrant Likely 	ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
487b8792bbSHans Holmberg 	if (ret < 0) {
497b8792bbSHans Holmberg 		/* We've found the gpio chip, but the translation failed.
507b8792bbSHans Holmberg 		 * Return true to stop looking and return the translation
517b8792bbSHans Holmberg 		 * error via out_gpio
527b8792bbSHans Holmberg 		 */
537b8792bbSHans Holmberg 		gg_data->out_gpio = ERR_PTR(ret);
547b8792bbSHans Holmberg 		return true;
557b8792bbSHans Holmberg 	 }
563d0f7cf0SGrant Likely 
57ccd9726eSLinus Walleij 	gg_data->out_gpio = gpiochip_get_desc(gc, ret);
583d0f7cf0SGrant Likely 	return true;
593d0f7cf0SGrant Likely }
603d0f7cf0SGrant Likely 
61f141ed65SGrant Likely /**
62af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
63f141ed65SGrant Likely  * @np:		device node to get GPIO from
64f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
65f141ed65SGrant Likely  * @index:	index of the GPIO
66f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
67f141ed65SGrant Likely  *
68af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
69f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
70f141ed65SGrant Likely  * in flags for the GPIO.
71f141ed65SGrant Likely  */
72af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
73af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
74f141ed65SGrant Likely {
754fbb0022SRoland Stigge 	/* Return -EPROBE_DEFER to support probe() functions to be called
764fbb0022SRoland Stigge 	 * later when the GPIO actually becomes available
774fbb0022SRoland Stigge 	 */
78af8b6375SAlexandre Courbot 	struct gg_data gg_data = {
79af8b6375SAlexandre Courbot 		.flags = flags,
80af8b6375SAlexandre Courbot 		.out_gpio = ERR_PTR(-EPROBE_DEFER)
81af8b6375SAlexandre Courbot 	};
82f141ed65SGrant Likely 	int ret;
83f141ed65SGrant Likely 
843d0f7cf0SGrant Likely 	/* .of_xlate might decide to not fill in the flags, so clear it. */
85f141ed65SGrant Likely 	if (flags)
86f141ed65SGrant Likely 		*flags = 0;
87f141ed65SGrant Likely 
883d0f7cf0SGrant Likely 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
893d0f7cf0SGrant Likely 					 &gg_data.gpiospec);
903d0f7cf0SGrant Likely 	if (ret) {
9185ea29acSTushar Behera 		pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
9285ea29acSTushar Behera 			__func__, propname, np->full_name, index);
93af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
943d0f7cf0SGrant Likely 	}
95f141ed65SGrant Likely 
963d0f7cf0SGrant Likely 	gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
973d0f7cf0SGrant Likely 
983d0f7cf0SGrant Likely 	of_node_put(gg_data.gpiospec.np);
9985ea29acSTushar Behera 	pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
10085ea29acSTushar Behera 		 __func__, propname, np->full_name, index,
101bea4dbeeSSachin Kamat 		 PTR_ERR_OR_ZERO(gg_data.out_gpio));
1023d0f7cf0SGrant Likely 	return gg_data.out_gpio;
103f141ed65SGrant Likely }
104f141ed65SGrant Likely 
105f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
106f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
107f01d9075SAlexandre Courbot {
108f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
109f01d9075SAlexandre Courbot 
110f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
111f01d9075SAlexandre Courbot 
112f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
113f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
114f01d9075SAlexandre Courbot 	else
115f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
116f01d9075SAlexandre Courbot }
117f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
118f01d9075SAlexandre Courbot 
119f141ed65SGrant Likely /**
120f141ed65SGrant Likely  * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
121f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
122f141ed65SGrant Likely  * @np:		device node of the GPIO chip
123f141ed65SGrant Likely  * @gpio_spec:	gpio specifier as found in the device tree
124f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
125f141ed65SGrant Likely  *
126f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
127f141ed65SGrant Likely  * gpio chips. This function performs only one sanity check: whether gpio
128f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
129f141ed65SGrant Likely  */
130f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
131f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
132f141ed65SGrant Likely {
133f141ed65SGrant Likely 	/*
134f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
135f141ed65SGrant Likely 	 * write your own xlate function (that will have to retrive the GPIO
136f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
137f141ed65SGrant Likely 	 * but not recommended).
138f141ed65SGrant Likely 	 */
139f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
140f141ed65SGrant Likely 		WARN_ON(1);
141f141ed65SGrant Likely 		return -EINVAL;
142f141ed65SGrant Likely 	}
143f141ed65SGrant Likely 
144f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
145f141ed65SGrant Likely 		return -EINVAL;
146f141ed65SGrant Likely 
1477b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
148f141ed65SGrant Likely 		return -EINVAL;
149f141ed65SGrant Likely 
150f141ed65SGrant Likely 	if (flags)
151f141ed65SGrant Likely 		*flags = gpiospec->args[1];
152f141ed65SGrant Likely 
153f141ed65SGrant Likely 	return gpiospec->args[0];
154f141ed65SGrant Likely }
155f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
156f141ed65SGrant Likely 
157f141ed65SGrant Likely /**
158f141ed65SGrant Likely  * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
159f141ed65SGrant Likely  * @np:		device node of the GPIO chip
160f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
161f141ed65SGrant Likely  *
162f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
163f141ed65SGrant Likely  *
164f141ed65SGrant Likely  * 1) In the gpio_chip structure:
165f141ed65SGrant Likely  *    - all the callbacks
166f141ed65SGrant Likely  *    - of_gpio_n_cells
167f141ed65SGrant Likely  *    - of_xlate callback (optional)
168f141ed65SGrant Likely  *
169f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
170f141ed65SGrant Likely  *    - save_regs callback (optional)
171f141ed65SGrant Likely  *
172f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
173f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
174f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
175f141ed65SGrant Likely  */
176f141ed65SGrant Likely int of_mm_gpiochip_add(struct device_node *np,
177f141ed65SGrant Likely 		       struct of_mm_gpio_chip *mm_gc)
178f141ed65SGrant Likely {
179f141ed65SGrant Likely 	int ret = -ENOMEM;
180f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
181f141ed65SGrant Likely 
182f141ed65SGrant Likely 	gc->label = kstrdup(np->full_name, GFP_KERNEL);
183f141ed65SGrant Likely 	if (!gc->label)
184f141ed65SGrant Likely 		goto err0;
185f141ed65SGrant Likely 
186f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
187f141ed65SGrant Likely 	if (!mm_gc->regs)
188f141ed65SGrant Likely 		goto err1;
189f141ed65SGrant Likely 
190f141ed65SGrant Likely 	gc->base = -1;
191f141ed65SGrant Likely 
192f141ed65SGrant Likely 	if (mm_gc->save_regs)
193f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
194f141ed65SGrant Likely 
195f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
196f141ed65SGrant Likely 
197f141ed65SGrant Likely 	ret = gpiochip_add(gc);
198f141ed65SGrant Likely 	if (ret)
199f141ed65SGrant Likely 		goto err2;
200f141ed65SGrant Likely 
201f141ed65SGrant Likely 	return 0;
202f141ed65SGrant Likely err2:
203f141ed65SGrant Likely 	iounmap(mm_gc->regs);
204f141ed65SGrant Likely err1:
205f141ed65SGrant Likely 	kfree(gc->label);
206f141ed65SGrant Likely err0:
207f141ed65SGrant Likely 	pr_err("%s: GPIO chip registration failed with status %d\n",
208f141ed65SGrant Likely 	       np->full_name, ret);
209f141ed65SGrant Likely 	return ret;
210f141ed65SGrant Likely }
211f141ed65SGrant Likely EXPORT_SYMBOL(of_mm_gpiochip_add);
212f141ed65SGrant Likely 
213f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
214167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
215f23f1516SShiraz Hashim {
216f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
217f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
2181e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
219f23f1516SShiraz Hashim 	int index = 0, ret;
220586a87e6SChristian Ruppert 	const char *name;
221586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
222586a87e6SChristian Ruppert 	struct property *group_names;
223f23f1516SShiraz Hashim 
224f23f1516SShiraz Hashim 	if (!np)
225f23f1516SShiraz Hashim 		return;
226f23f1516SShiraz Hashim 
227586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
228586a87e6SChristian Ruppert 
229ad4e1a7cSHaojian Zhuang 	for (;; index++) {
230d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
231d9fe0039SStephen Warren 				index, &pinspec);
232f23f1516SShiraz Hashim 		if (ret)
233f23f1516SShiraz Hashim 			break;
234f23f1516SShiraz Hashim 
2351e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
2361e63d7b9SLinus Walleij 		if (!pctldev)
237f23f1516SShiraz Hashim 			break;
238f23f1516SShiraz Hashim 
239586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
240586a87e6SChristian Ruppert 			if (group_names) {
241586a87e6SChristian Ruppert 				ret = of_property_read_string_index(np,
242586a87e6SChristian Ruppert 						group_names_propname,
243586a87e6SChristian Ruppert 						index, &name);
244586a87e6SChristian Ruppert 				if (strlen(name)) {
245586a87e6SChristian Ruppert 					pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
246586a87e6SChristian Ruppert 						np->full_name);
247586a87e6SChristian Ruppert 					break;
248586a87e6SChristian Ruppert 				}
249586a87e6SChristian Ruppert 			}
250586a87e6SChristian Ruppert 			/* npins != 0: linear range */
2511e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
252ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
2531e63d7b9SLinus Walleij 					pinspec.args[0],
25486853c83SHaojian Zhuang 					pinspec.args[1],
25586853c83SHaojian Zhuang 					pinspec.args[2]);
2561e63d7b9SLinus Walleij 			if (ret)
2571e63d7b9SLinus Walleij 				break;
258586a87e6SChristian Ruppert 		} else {
259586a87e6SChristian Ruppert 			/* npins == 0: special range */
260586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
261586a87e6SChristian Ruppert 				pr_err("%s: Illegal gpio-range format.\n",
262586a87e6SChristian Ruppert 					np->full_name);
263586a87e6SChristian Ruppert 				break;
264586a87e6SChristian Ruppert 			}
265586a87e6SChristian Ruppert 
266586a87e6SChristian Ruppert 			if (!group_names) {
267586a87e6SChristian Ruppert 				pr_err("%s: GPIO group range requested but no %s property.\n",
268586a87e6SChristian Ruppert 					np->full_name, group_names_propname);
269586a87e6SChristian Ruppert 				break;
270586a87e6SChristian Ruppert 			}
271586a87e6SChristian Ruppert 
272586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
273586a87e6SChristian Ruppert 						group_names_propname,
274586a87e6SChristian Ruppert 						index, &name);
275586a87e6SChristian Ruppert 			if (ret)
276586a87e6SChristian Ruppert 				break;
277586a87e6SChristian Ruppert 
278586a87e6SChristian Ruppert 			if (!strlen(name)) {
279586a87e6SChristian Ruppert 				pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
280586a87e6SChristian Ruppert 				np->full_name);
281586a87e6SChristian Ruppert 				break;
282586a87e6SChristian Ruppert 			}
283586a87e6SChristian Ruppert 
284586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
285586a87e6SChristian Ruppert 						pinspec.args[0], name);
286586a87e6SChristian Ruppert 			if (ret)
287586a87e6SChristian Ruppert 				break;
288586a87e6SChristian Ruppert 		}
289ad4e1a7cSHaojian Zhuang 	}
290f23f1516SShiraz Hashim }
291f23f1516SShiraz Hashim 
292f23f1516SShiraz Hashim #else
293167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
294f23f1516SShiraz Hashim #endif
295f23f1516SShiraz Hashim 
296f141ed65SGrant Likely void of_gpiochip_add(struct gpio_chip *chip)
297f141ed65SGrant Likely {
298f141ed65SGrant Likely 	if ((!chip->of_node) && (chip->dev))
299f141ed65SGrant Likely 		chip->of_node = chip->dev->of_node;
300f141ed65SGrant Likely 
301f141ed65SGrant Likely 	if (!chip->of_node)
302f141ed65SGrant Likely 		return;
303f141ed65SGrant Likely 
304f141ed65SGrant Likely 	if (!chip->of_xlate) {
305f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
306f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
307f141ed65SGrant Likely 	}
308f141ed65SGrant Likely 
309f23f1516SShiraz Hashim 	of_gpiochip_add_pin_range(chip);
310f141ed65SGrant Likely 	of_node_get(chip->of_node);
311f141ed65SGrant Likely }
312f141ed65SGrant Likely 
313f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
314f141ed65SGrant Likely {
315e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
316f141ed65SGrant Likely 	of_node_put(chip->of_node);
317f141ed65SGrant Likely }
318