xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision a603a2b8)
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>
25f625d460SBenoit Parrot #include <linux/gpio/machine.h>
26f141ed65SGrant Likely 
271bd6b601SAlexandre Courbot #include "gpiolib.h"
28af8b6375SAlexandre Courbot 
29c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
303d0f7cf0SGrant Likely {
31c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
32c7e9d398SMasahiro Yamada 
33c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
34c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
357b8792bbSHans Holmberg }
363d0f7cf0SGrant Likely 
37c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
38c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
39762c2e46SMasahiro Yamada {
40c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
413d0f7cf0SGrant Likely }
423d0f7cf0SGrant Likely 
4399468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
4499468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
4599468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
4699468c1aSMasahiro Yamada {
4799468c1aSMasahiro Yamada 	int ret;
4899468c1aSMasahiro Yamada 
4999468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
5099468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
5199468c1aSMasahiro Yamada 
5299468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
5399468c1aSMasahiro Yamada 	if (ret < 0)
5499468c1aSMasahiro Yamada 		return ERR_PTR(ret);
5599468c1aSMasahiro Yamada 
5699468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
57f141ed65SGrant Likely }
58f141ed65SGrant Likely 
59a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np,
60a603a2b8SLinus Walleij 				 enum of_gpio_flags *flags)
61a603a2b8SLinus Walleij {
62a603a2b8SLinus Walleij 	/*
63a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
64a603a2b8SLinus Walleij 	 * Note that active low is the default.
65a603a2b8SLinus Walleij 	 */
66a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
67a603a2b8SLinus Walleij 	    (of_device_is_compatible(np, "reg-fixed-voltage") ||
68a603a2b8SLinus Walleij 	     of_device_is_compatible(np, "regulator-gpio"))) {
69a603a2b8SLinus Walleij 		/*
70a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
71a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
72a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
73a603a2b8SLinus Walleij 		 * be actively ignored.
74a603a2b8SLinus Walleij 		 */
75a603a2b8SLinus Walleij 		if (*flags & OF_GPIO_ACTIVE_LOW) {
76a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
77a603a2b8SLinus Walleij 				of_node_full_name(np));
78a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
79a603a2b8SLinus Walleij 		}
80a603a2b8SLinus Walleij 		if (!of_property_read_bool(np, "enable-active-high"))
81a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
82a603a2b8SLinus Walleij 	}
83a603a2b8SLinus Walleij 	/*
84a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
85a603a2b8SLinus Walleij 	 */
86a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
87a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
88a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
89a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
90a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
91a603a2b8SLinus Walleij 			of_node_full_name(np));
92a603a2b8SLinus Walleij 	}
93a603a2b8SLinus Walleij }
94a603a2b8SLinus Walleij 
95f141ed65SGrant Likely /**
96af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
97f141ed65SGrant Likely  * @np:		device node to get GPIO from
98f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
99f141ed65SGrant Likely  * @index:	index of the GPIO
100f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
101f141ed65SGrant Likely  *
102af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
103f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
104f141ed65SGrant Likely  * in flags for the GPIO.
105f141ed65SGrant Likely  */
106af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
107af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
108f141ed65SGrant Likely {
109762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
110762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
111762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
112f141ed65SGrant Likely 	int ret;
113f141ed65SGrant Likely 
1143d0f7cf0SGrant Likely 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
115762c2e46SMasahiro Yamada 					 &gpiospec);
1163d0f7cf0SGrant Likely 	if (ret) {
1177eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
1187eb6ce2fSRob Herring 			__func__, propname, np, index);
119af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
1203d0f7cf0SGrant Likely 	}
121f141ed65SGrant Likely 
122c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
123762c2e46SMasahiro Yamada 	if (!chip) {
124762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
125762c2e46SMasahiro Yamada 		goto out;
126762c2e46SMasahiro Yamada 	}
1273d0f7cf0SGrant Likely 
12899468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
129762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
130762c2e46SMasahiro Yamada 		goto out;
131762c2e46SMasahiro Yamada 
132a603a2b8SLinus Walleij 	of_gpio_flags_quirks(np, flags);
133a603a2b8SLinus Walleij 
1347eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
1357eb6ce2fSRob Herring 		 __func__, propname, np, index,
136762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
137762c2e46SMasahiro Yamada 
138762c2e46SMasahiro Yamada out:
139762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
140762c2e46SMasahiro Yamada 
141762c2e46SMasahiro Yamada 	return desc;
142f141ed65SGrant Likely }
143f141ed65SGrant Likely 
144f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
145f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
146f01d9075SAlexandre Courbot {
147f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
148f01d9075SAlexandre Courbot 
149f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
150f01d9075SAlexandre Courbot 
151f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
152f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
153f01d9075SAlexandre Courbot 	else
154f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
155f01d9075SAlexandre Courbot }
156f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
157f01d9075SAlexandre Courbot 
158c8582339SLinus Walleij /*
159c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
160c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
161c8582339SLinus Walleij  * them.
162c8582339SLinus Walleij  */
163c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
164c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
165c8582339SLinus Walleij {
166c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
167c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
168c8582339SLinus Walleij 	struct gpio_desc *desc;
169c8582339SLinus Walleij 
170c8582339SLinus Walleij 	/*
171c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
172c8582339SLinus Walleij 	 * is false.
173c8582339SLinus Walleij 	 */
174c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
175c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
176c8582339SLinus Walleij 
177c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
178c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
179c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
180c8582339SLinus Walleij 
181c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
182c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
183c8582339SLinus Walleij 
184c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
185c8582339SLinus Walleij 	return desc;
186c8582339SLinus Walleij }
187c8582339SLinus Walleij 
1886a537d48SLinus Walleij /*
1896a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
1906a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
1916a537d48SLinus Walleij  * them.
1926a537d48SLinus Walleij  */
1936a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
1946a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
1956a537d48SLinus Walleij {
1966a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
1976a537d48SLinus Walleij 	const char *whitelist[] = {
1986a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
1996a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
2006a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
2016a537d48SLinus Walleij 	};
2026a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
2036a537d48SLinus Walleij 	struct gpio_desc *desc;
2046a537d48SLinus Walleij 	int i;
2056a537d48SLinus Walleij 
2066a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
2076a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2086a537d48SLinus Walleij 
2096a537d48SLinus Walleij 	if (!con_id)
2106a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2116a537d48SLinus Walleij 
2126a537d48SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(whitelist); i++)
2136a537d48SLinus Walleij 		if (!strcmp(con_id, whitelist[i]))
2146a537d48SLinus Walleij 			break;
2156a537d48SLinus Walleij 
2166a537d48SLinus Walleij 	if (i == ARRAY_SIZE(whitelist))
2176a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2186a537d48SLinus Walleij 
2196a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
2206a537d48SLinus Walleij 	return desc;
2216a537d48SLinus Walleij }
2226a537d48SLinus Walleij 
223ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
224ea713bc4SLinus Walleij 			       unsigned int idx,
225ea713bc4SLinus Walleij 			       enum gpio_lookup_flags *flags)
226ea713bc4SLinus Walleij {
227ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
228ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
229ea713bc4SLinus Walleij 	struct gpio_desc *desc;
230ea713bc4SLinus Walleij 	unsigned int i;
231ea713bc4SLinus Walleij 
232c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
233ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
234ea713bc4SLinus Walleij 		if (con_id)
235ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
236ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
237ea713bc4SLinus Walleij 		else
238ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
239ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
240ea713bc4SLinus Walleij 
241ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
242ea713bc4SLinus Walleij 						&of_flags);
243ea713bc4SLinus Walleij 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
244ea713bc4SLinus Walleij 			break;
245ea713bc4SLinus Walleij 	}
246ea713bc4SLinus Walleij 
247c8582339SLinus Walleij 	/* Special handling for SPI GPIOs if used */
248c8582339SLinus Walleij 	if (IS_ERR(desc))
249c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
250c8582339SLinus Walleij 
2516a537d48SLinus Walleij 	/* Special handling for regulator GPIOs if used */
2526a537d48SLinus Walleij 	if (IS_ERR(desc))
2536a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
2546a537d48SLinus Walleij 
255ea713bc4SLinus Walleij 	if (IS_ERR(desc))
256ea713bc4SLinus Walleij 		return desc;
257ea713bc4SLinus Walleij 
258ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
259ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
260ea713bc4SLinus Walleij 
261ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
2624c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
263ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
264ea713bc4SLinus Walleij 		else
265ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
266ea713bc4SLinus Walleij 	}
267ea713bc4SLinus Walleij 
268e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
269e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
27005f479bfSCharles Keepax 
271ea713bc4SLinus Walleij 	return desc;
272ea713bc4SLinus Walleij }
273ea713bc4SLinus Walleij 
274f141ed65SGrant Likely /**
275fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
276f625d460SBenoit Parrot  * @np:		device node to get GPIO from
277be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
278a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
279f625d460SBenoit Parrot  * @name:	GPIO line name
280f625d460SBenoit Parrot  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
281fd7337fdSMarkus Pargmann  *		of_parse_own_gpio()
282f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
283f625d460SBenoit Parrot  *
284f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
285f625d460SBenoit Parrot  * value on the error condition.
286f625d460SBenoit Parrot  */
287fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
288be715343SMasahiro Yamada 					   struct gpio_chip *chip,
289a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
290f625d460SBenoit Parrot 					   enum gpio_lookup_flags *lflags,
291f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
292f625d460SBenoit Parrot {
293f625d460SBenoit Parrot 	struct device_node *chip_np;
294f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
295be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
296be715343SMasahiro Yamada 	struct gpio_desc *desc;
297a79fead5SGeert Uytterhoeven 	unsigned int i;
298f625d460SBenoit Parrot 	u32 tmp;
2993f9547e1SMasahiro Yamada 	int ret;
300f625d460SBenoit Parrot 
301be715343SMasahiro Yamada 	chip_np = chip->of_node;
302f625d460SBenoit Parrot 	if (!chip_np)
303f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
304f625d460SBenoit Parrot 
305f625d460SBenoit Parrot 	xlate_flags = 0;
306f625d460SBenoit Parrot 	*lflags = 0;
307f625d460SBenoit Parrot 	*dflags = 0;
308f625d460SBenoit Parrot 
309f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
310f625d460SBenoit Parrot 	if (ret)
311f625d460SBenoit Parrot 		return ERR_PTR(ret);
312f625d460SBenoit Parrot 
313be715343SMasahiro Yamada 	gpiospec.np = chip_np;
314be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
315f625d460SBenoit Parrot 
316a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
317a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
318a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
319f625d460SBenoit Parrot 		if (ret)
320f625d460SBenoit Parrot 			return ERR_PTR(ret);
321a79fead5SGeert Uytterhoeven 	}
322f625d460SBenoit Parrot 
32399468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
324be715343SMasahiro Yamada 	if (IS_ERR(desc))
325be715343SMasahiro Yamada 		return desc;
326f625d460SBenoit Parrot 
327f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
328f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
329e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
330e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
331f625d460SBenoit Parrot 
332f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
333f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
334f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
335f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
336f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
337f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
338f625d460SBenoit Parrot 	else {
339f625d460SBenoit Parrot 		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
340be715343SMasahiro Yamada 			desc_to_gpio(desc), np->name);
341f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
342f625d460SBenoit Parrot 	}
343f625d460SBenoit Parrot 
344f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
345f625d460SBenoit Parrot 		*name = np->name;
346f625d460SBenoit Parrot 
347be715343SMasahiro Yamada 	return desc;
348f625d460SBenoit Parrot }
349f625d460SBenoit Parrot 
350f625d460SBenoit Parrot /**
351fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
352f625d460SBenoit Parrot  * @chip:	gpio chip to act on
353f625d460SBenoit Parrot  *
354f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
355f625d460SBenoit Parrot  * configuration.
356ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
357f625d460SBenoit Parrot  */
358dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
359f625d460SBenoit Parrot {
360f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
361f625d460SBenoit Parrot 	struct device_node *np;
362f625d460SBenoit Parrot 	const char *name;
363f625d460SBenoit Parrot 	enum gpio_lookup_flags lflags;
364f625d460SBenoit Parrot 	enum gpiod_flags dflags;
365a79fead5SGeert Uytterhoeven 	unsigned int i;
366dfbd379bSLaxman Dewangan 	int ret;
367f625d460SBenoit Parrot 
368d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
369f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
370f625d460SBenoit Parrot 			continue;
371f625d460SBenoit Parrot 
372a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
373a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
374a79fead5SGeert Uytterhoeven 						 &dflags);
375f625d460SBenoit Parrot 			if (IS_ERR(desc))
376a79fead5SGeert Uytterhoeven 				break;
377f625d460SBenoit Parrot 
378dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
37909e258afSWei Yongjun 			if (ret < 0) {
38009e258afSWei Yongjun 				of_node_put(np);
381dfbd379bSLaxman Dewangan 				return ret;
382f625d460SBenoit Parrot 			}
38309e258afSWei Yongjun 		}
384a79fead5SGeert Uytterhoeven 	}
385dfbd379bSLaxman Dewangan 
386dfbd379bSLaxman Dewangan 	return 0;
387f625d460SBenoit Parrot }
388f625d460SBenoit Parrot 
389f625d460SBenoit Parrot /**
39067049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
391f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
39267049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
393f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
394f141ed65SGrant Likely  *
395f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
39667049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
397f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
398f141ed65SGrant Likely  */
399f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
400f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
401f141ed65SGrant Likely {
402f141ed65SGrant Likely 	/*
403f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
40420a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
405f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
406f141ed65SGrant Likely 	 * but not recommended).
407f141ed65SGrant Likely 	 */
408f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
409f141ed65SGrant Likely 		WARN_ON(1);
410f141ed65SGrant Likely 		return -EINVAL;
411f141ed65SGrant Likely 	}
412f141ed65SGrant Likely 
413f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
414f141ed65SGrant Likely 		return -EINVAL;
415f141ed65SGrant Likely 
4167b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
417f141ed65SGrant Likely 		return -EINVAL;
418f141ed65SGrant Likely 
419f141ed65SGrant Likely 	if (flags)
420f141ed65SGrant Likely 		*flags = gpiospec->args[1];
421f141ed65SGrant Likely 
422f141ed65SGrant Likely 	return gpiospec->args[0];
423f141ed65SGrant Likely }
424f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
425f141ed65SGrant Likely 
426f141ed65SGrant Likely /**
4273208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
428f141ed65SGrant Likely  * @np:		device node of the GPIO chip
429f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
4303208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
431f141ed65SGrant Likely  *
432f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
433f141ed65SGrant Likely  *
434f141ed65SGrant Likely  * 1) In the gpio_chip structure:
435f141ed65SGrant Likely  *    - all the callbacks
436f141ed65SGrant Likely  *    - of_gpio_n_cells
437f141ed65SGrant Likely  *    - of_xlate callback (optional)
438f141ed65SGrant Likely  *
439f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
440f141ed65SGrant Likely  *    - save_regs callback (optional)
441f141ed65SGrant Likely  *
442f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
443f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
444f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
445f141ed65SGrant Likely  */
4463208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
4473208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
4483208b0f0SLinus Walleij 			    void *data)
449f141ed65SGrant Likely {
450f141ed65SGrant Likely 	int ret = -ENOMEM;
451f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
452f141ed65SGrant Likely 
4537eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
454f141ed65SGrant Likely 	if (!gc->label)
455f141ed65SGrant Likely 		goto err0;
456f141ed65SGrant Likely 
457f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
458f141ed65SGrant Likely 	if (!mm_gc->regs)
459f141ed65SGrant Likely 		goto err1;
460f141ed65SGrant Likely 
461f141ed65SGrant Likely 	gc->base = -1;
462f141ed65SGrant Likely 
463f141ed65SGrant Likely 	if (mm_gc->save_regs)
464f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
465f141ed65SGrant Likely 
466f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
467f141ed65SGrant Likely 
4683208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
469f141ed65SGrant Likely 	if (ret)
470f141ed65SGrant Likely 		goto err2;
471f141ed65SGrant Likely 
472f141ed65SGrant Likely 	return 0;
473f141ed65SGrant Likely err2:
474f141ed65SGrant Likely 	iounmap(mm_gc->regs);
475f141ed65SGrant Likely err1:
476f141ed65SGrant Likely 	kfree(gc->label);
477f141ed65SGrant Likely err0:
4787eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
479f141ed65SGrant Likely 	return ret;
480f141ed65SGrant Likely }
4813208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
482f141ed65SGrant Likely 
483d621e8baSRicardo Ribalda Delgado /**
484d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
485d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
486d621e8baSRicardo Ribalda Delgado  */
487d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
488d621e8baSRicardo Ribalda Delgado {
489d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
490d621e8baSRicardo Ribalda Delgado 
491d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
492d621e8baSRicardo Ribalda Delgado 		return;
493d621e8baSRicardo Ribalda Delgado 
494d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
495d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
496d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
497d621e8baSRicardo Ribalda Delgado }
498d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
499d621e8baSRicardo Ribalda Delgado 
500f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
50128355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
502f23f1516SShiraz Hashim {
503f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
504f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
5051e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
506f23f1516SShiraz Hashim 	int index = 0, ret;
507586a87e6SChristian Ruppert 	const char *name;
508586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
509586a87e6SChristian Ruppert 	struct property *group_names;
510f23f1516SShiraz Hashim 
511f23f1516SShiraz Hashim 	if (!np)
51228355f81STomeu Vizoso 		return 0;
513f23f1516SShiraz Hashim 
514586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
515586a87e6SChristian Ruppert 
516ad4e1a7cSHaojian Zhuang 	for (;; index++) {
517d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
518d9fe0039SStephen Warren 				index, &pinspec);
519f23f1516SShiraz Hashim 		if (ret)
520f23f1516SShiraz Hashim 			break;
521f23f1516SShiraz Hashim 
5221e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
523602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
5241e63d7b9SLinus Walleij 		if (!pctldev)
52528355f81STomeu Vizoso 			return -EPROBE_DEFER;
526f23f1516SShiraz Hashim 
527586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
528586a87e6SChristian Ruppert 			if (group_names) {
52972858602SLaurent Navet 				of_property_read_string_index(np,
530586a87e6SChristian Ruppert 						group_names_propname,
531586a87e6SChristian Ruppert 						index, &name);
532586a87e6SChristian Ruppert 				if (strlen(name)) {
5337eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
5347eb6ce2fSRob Herring 						np);
535586a87e6SChristian Ruppert 					break;
536586a87e6SChristian Ruppert 				}
537586a87e6SChristian Ruppert 			}
538586a87e6SChristian Ruppert 			/* npins != 0: linear range */
5391e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
540ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
5411e63d7b9SLinus Walleij 					pinspec.args[0],
54286853c83SHaojian Zhuang 					pinspec.args[1],
54386853c83SHaojian Zhuang 					pinspec.args[2]);
5441e63d7b9SLinus Walleij 			if (ret)
54528355f81STomeu Vizoso 				return ret;
546586a87e6SChristian Ruppert 		} else {
547586a87e6SChristian Ruppert 			/* npins == 0: special range */
548586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
5497eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
5507eb6ce2fSRob Herring 					np);
551586a87e6SChristian Ruppert 				break;
552586a87e6SChristian Ruppert 			}
553586a87e6SChristian Ruppert 
554586a87e6SChristian Ruppert 			if (!group_names) {
5557eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
5567eb6ce2fSRob Herring 					np, group_names_propname);
557586a87e6SChristian Ruppert 				break;
558586a87e6SChristian Ruppert 			}
559586a87e6SChristian Ruppert 
560586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
561586a87e6SChristian Ruppert 						group_names_propname,
562586a87e6SChristian Ruppert 						index, &name);
563586a87e6SChristian Ruppert 			if (ret)
564586a87e6SChristian Ruppert 				break;
565586a87e6SChristian Ruppert 
566586a87e6SChristian Ruppert 			if (!strlen(name)) {
5677eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
5687eb6ce2fSRob Herring 				np);
569586a87e6SChristian Ruppert 				break;
570586a87e6SChristian Ruppert 			}
571586a87e6SChristian Ruppert 
572586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
573586a87e6SChristian Ruppert 						pinspec.args[0], name);
574586a87e6SChristian Ruppert 			if (ret)
57528355f81STomeu Vizoso 				return ret;
576586a87e6SChristian Ruppert 		}
577ad4e1a7cSHaojian Zhuang 	}
57828355f81STomeu Vizoso 
57928355f81STomeu Vizoso 	return 0;
580f23f1516SShiraz Hashim }
581f23f1516SShiraz Hashim 
582f23f1516SShiraz Hashim #else
58328355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
584f23f1516SShiraz Hashim #endif
585f23f1516SShiraz Hashim 
58628355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
587f141ed65SGrant Likely {
58828355f81STomeu Vizoso 	int status;
58928355f81STomeu Vizoso 
59058383c78SLinus Walleij 	if ((!chip->of_node) && (chip->parent))
59158383c78SLinus Walleij 		chip->of_node = chip->parent->of_node;
592f141ed65SGrant Likely 
593f141ed65SGrant Likely 	if (!chip->of_node)
59428355f81STomeu Vizoso 		return 0;
595f141ed65SGrant Likely 
596f141ed65SGrant Likely 	if (!chip->of_xlate) {
597f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
598f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
599f141ed65SGrant Likely 	}
600f141ed65SGrant Likely 
6011020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
6021020dfd1SMasahiro Yamada 		return -EINVAL;
6031020dfd1SMasahiro Yamada 
60428355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
60528355f81STomeu Vizoso 	if (status)
60628355f81STomeu Vizoso 		return status;
60728355f81STomeu Vizoso 
608fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
609fd9c5531SLinus Walleij 	if (!chip->names)
6109427ecbeSMika Westerberg 		devprop_gpiochip_set_names(chip);
611fd9c5531SLinus Walleij 
612f141ed65SGrant Likely 	of_node_get(chip->of_node);
613f625d460SBenoit Parrot 
614dfbd379bSLaxman Dewangan 	return of_gpiochip_scan_gpios(chip);
615f141ed65SGrant Likely }
616f141ed65SGrant Likely 
617f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
618f141ed65SGrant Likely {
619e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
620f141ed65SGrant Likely 	of_node_put(chip->of_node);
621f141ed65SGrant Likely }
622