xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 89fea04c)
127038c3eSVladimir Zapolskiy // SPDX-License-Identifier: GPL-2.0+
2f141ed65SGrant Likely /*
3f141ed65SGrant Likely  * OF helpers for the GPIO API
4f141ed65SGrant Likely  *
5f141ed65SGrant Likely  * Copyright (c) 2007-2008  MontaVista Software, Inc.
6f141ed65SGrant Likely  *
7f141ed65SGrant Likely  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8f141ed65SGrant Likely  */
9f141ed65SGrant Likely 
10f141ed65SGrant Likely #include <linux/device.h>
11bea4dbeeSSachin Kamat #include <linux/err.h>
12f141ed65SGrant Likely #include <linux/errno.h>
13f141ed65SGrant Likely #include <linux/module.h>
14f141ed65SGrant Likely #include <linux/io.h>
15af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h>
16f141ed65SGrant Likely #include <linux/of.h>
17f141ed65SGrant Likely #include <linux/of_address.h>
18f141ed65SGrant Likely #include <linux/of_gpio.h>
19f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h>
20f141ed65SGrant Likely #include <linux/slab.h>
21f625d460SBenoit Parrot #include <linux/gpio/machine.h>
22f141ed65SGrant Likely 
231bd6b601SAlexandre Courbot #include "gpiolib.h"
24af8b6375SAlexandre Courbot 
25c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
263d0f7cf0SGrant Likely {
27c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
28c7e9d398SMasahiro Yamada 
29c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
30d49b48f0SVincent Whitchurch 				chip->of_xlate &&
31c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
327b8792bbSHans Holmberg }
333d0f7cf0SGrant Likely 
34c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
35c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
36762c2e46SMasahiro Yamada {
37c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
383d0f7cf0SGrant Likely }
393d0f7cf0SGrant Likely 
4099468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
4199468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
4299468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
4399468c1aSMasahiro Yamada {
4499468c1aSMasahiro Yamada 	int ret;
4599468c1aSMasahiro Yamada 
4699468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
4799468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
4899468c1aSMasahiro Yamada 
4999468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
5099468c1aSMasahiro Yamada 	if (ret < 0)
5199468c1aSMasahiro Yamada 		return ERR_PTR(ret);
5299468c1aSMasahiro Yamada 
5399468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
54f141ed65SGrant Likely }
55f141ed65SGrant Likely 
56a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np,
5789a5e15bSLinus Walleij 				 const char *propname,
586953c57aSLinus Walleij 				 enum of_gpio_flags *flags,
596953c57aSLinus Walleij 				 int index)
60a603a2b8SLinus Walleij {
61a603a2b8SLinus Walleij 	/*
6281c85ec1SLinus Walleij 	 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
6381c85ec1SLinus Walleij 	 */
6481c85ec1SLinus Walleij 	if (IS_ENABLED(CONFIG_MMC)) {
6581c85ec1SLinus Walleij 		/*
6681c85ec1SLinus Walleij 		 * Active low is the default according to the
6789a5e15bSLinus Walleij 		 * SDHCI specification and the device tree
6889a5e15bSLinus Walleij 		 * bindings. However the code in the current
6989a5e15bSLinus Walleij 		 * kernel was written such that the phandle
7089a5e15bSLinus Walleij 		 * flags were always respected, and "cd-inverted"
7189a5e15bSLinus Walleij 		 * would invert the flag from the device phandle.
7281c85ec1SLinus Walleij 		 */
7389a5e15bSLinus Walleij 		if (!strcmp(propname, "cd-gpios")) {
7489a5e15bSLinus Walleij 			if (of_property_read_bool(np, "cd-inverted"))
7589a5e15bSLinus Walleij 				*flags ^= OF_GPIO_ACTIVE_LOW;
7681c85ec1SLinus Walleij 		}
7789a5e15bSLinus Walleij 		if (!strcmp(propname, "wp-gpios")) {
7889a5e15bSLinus Walleij 			if (of_property_read_bool(np, "wp-inverted"))
7989a5e15bSLinus Walleij 				*flags ^= OF_GPIO_ACTIVE_LOW;
8081c85ec1SLinus Walleij 		}
8181c85ec1SLinus Walleij 	}
8281c85ec1SLinus Walleij 	/*
83a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
84a603a2b8SLinus Walleij 	 * Note that active low is the default.
85a603a2b8SLinus Walleij 	 */
86a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
87906402a4SLinus Walleij 	    (of_device_is_compatible(np, "regulator-fixed") ||
88906402a4SLinus Walleij 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
89a71a81e7SGeert Uytterhoeven 	     (!(strcmp(propname, "enable-gpio") &&
90a71a81e7SGeert Uytterhoeven 		strcmp(propname, "enable-gpios")) &&
91a71a81e7SGeert Uytterhoeven 	      of_device_is_compatible(np, "regulator-gpio")))) {
92a603a2b8SLinus Walleij 		/*
93a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
94a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
95a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
96a603a2b8SLinus Walleij 		 * be actively ignored.
97a603a2b8SLinus Walleij 		 */
98a603a2b8SLinus Walleij 		if (*flags & OF_GPIO_ACTIVE_LOW) {
99a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
100a603a2b8SLinus Walleij 				of_node_full_name(np));
101a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
102a603a2b8SLinus Walleij 		}
103a603a2b8SLinus Walleij 		if (!of_property_read_bool(np, "enable-active-high"))
104a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
105a603a2b8SLinus Walleij 	}
106a603a2b8SLinus Walleij 	/*
107a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
108a603a2b8SLinus Walleij 	 */
109a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
110a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
111a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
112a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
113a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
114a603a2b8SLinus Walleij 			of_node_full_name(np));
115a603a2b8SLinus Walleij 	}
1166953c57aSLinus Walleij 
1176953c57aSLinus Walleij 	/*
1186953c57aSLinus Walleij 	 * Legacy handling of SPI active high chip select. If we have a
1196953c57aSLinus Walleij 	 * property named "cs-gpios" we need to inspect the child node
1206953c57aSLinus Walleij 	 * to determine if the flags should have inverted semantics.
121fbbf145aSLinus Walleij 	 *
122fbbf145aSLinus Walleij 	 * This does not apply to an SPI device named "spi-gpio", because
123fbbf145aSLinus Walleij 	 * these have traditionally obtained their own GPIOs by parsing
124fbbf145aSLinus Walleij 	 * the device tree directly and did not respect any "spi-cs-high"
125fbbf145aSLinus Walleij 	 * property on the SPI bus children.
1266953c57aSLinus Walleij 	 */
127fbbf145aSLinus Walleij 	if (IS_ENABLED(CONFIG_SPI_MASTER) &&
128fbbf145aSLinus Walleij 	    !strcmp(propname, "cs-gpios") &&
129fbbf145aSLinus Walleij 	    !of_device_is_compatible(np, "spi-gpio") &&
130a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
1316953c57aSLinus Walleij 		struct device_node *child;
1326953c57aSLinus Walleij 		u32 cs;
1336953c57aSLinus Walleij 		int ret;
1346953c57aSLinus Walleij 
1356953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
1366953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
137c1c04ceaSLinus Walleij 			if (ret)
1386953c57aSLinus Walleij 				continue;
1396953c57aSLinus Walleij 			if (cs == index) {
1406953c57aSLinus Walleij 				/*
1416953c57aSLinus Walleij 				 * SPI children have active low chip selects
1426953c57aSLinus Walleij 				 * by default. This can be specified negatively
1436953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
1446953c57aSLinus Walleij 				 * device node, or actively by tagging on
1456953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
1466953c57aSLinus Walleij 				 * tree. If the line is simultaneously
1476953c57aSLinus Walleij 				 * tagged as active low in the device tree
1486953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
1496953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
1506953c57aSLinus Walleij 				 * take precedence.
1516953c57aSLinus Walleij 				 */
1527ce40277SAndrey Smirnov 				if (of_property_read_bool(child, "spi-cs-high")) {
1536953c57aSLinus Walleij 					if (*flags & OF_GPIO_ACTIVE_LOW) {
1546953c57aSLinus Walleij 						pr_warn("%s GPIO handle specifies active low - ignored\n",
1557ce40277SAndrey Smirnov 							of_node_full_name(child));
1566953c57aSLinus Walleij 						*flags &= ~OF_GPIO_ACTIVE_LOW;
1576953c57aSLinus Walleij 					}
1586953c57aSLinus Walleij 				} else {
1596953c57aSLinus Walleij 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
1606953c57aSLinus Walleij 						pr_info("%s enforce active low on chipselect handle\n",
1617ce40277SAndrey Smirnov 							of_node_full_name(child));
1626953c57aSLinus Walleij 					*flags |= OF_GPIO_ACTIVE_LOW;
1636953c57aSLinus Walleij 				}
16489fea04cSNishka Dasgupta 				of_node_put(child);
1656953c57aSLinus Walleij 				break;
1666953c57aSLinus Walleij 			}
1676953c57aSLinus Walleij 		}
1686953c57aSLinus Walleij 	}
169edc1ef3fSMartin Blumenstingl 
170edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
171edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
172edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
173edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
174edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
175a603a2b8SLinus Walleij }
176a603a2b8SLinus Walleij 
177f141ed65SGrant Likely /**
178af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
179f141ed65SGrant Likely  * @np:		device node to get GPIO from
180f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
181f141ed65SGrant Likely  * @index:	index of the GPIO
182f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
183f141ed65SGrant Likely  *
184af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
185f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
186f141ed65SGrant Likely  * in flags for the GPIO.
187f141ed65SGrant Likely  */
188af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
189af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
190f141ed65SGrant Likely {
191762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
192762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
193762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
194f141ed65SGrant Likely 	int ret;
195f141ed65SGrant Likely 
196c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
197762c2e46SMasahiro Yamada 					     &gpiospec);
1983d0f7cf0SGrant Likely 	if (ret) {
1997eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
2007eb6ce2fSRob Herring 			__func__, propname, np, index);
201af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
2023d0f7cf0SGrant Likely 	}
203f141ed65SGrant Likely 
204c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
205762c2e46SMasahiro Yamada 	if (!chip) {
206762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
207762c2e46SMasahiro Yamada 		goto out;
208762c2e46SMasahiro Yamada 	}
2093d0f7cf0SGrant Likely 
21099468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
211762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
212762c2e46SMasahiro Yamada 		goto out;
213762c2e46SMasahiro Yamada 
214605f2d34SLinus Walleij 	if (flags)
21589a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
216a603a2b8SLinus Walleij 
2177eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
2187eb6ce2fSRob Herring 		 __func__, propname, np, index,
219762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
220762c2e46SMasahiro Yamada 
221762c2e46SMasahiro Yamada out:
222762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
223762c2e46SMasahiro Yamada 
224762c2e46SMasahiro Yamada 	return desc;
225f141ed65SGrant Likely }
226f141ed65SGrant Likely 
227f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
228f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
229f01d9075SAlexandre Courbot {
230f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
231f01d9075SAlexandre Courbot 
232f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
233f01d9075SAlexandre Courbot 
234f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
235f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
236f01d9075SAlexandre Courbot 	else
237f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
238f01d9075SAlexandre Courbot }
239f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
240f01d9075SAlexandre Courbot 
241c8582339SLinus Walleij /*
242c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
243c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
244c8582339SLinus Walleij  * them.
245c8582339SLinus Walleij  */
246c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
247c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
248c8582339SLinus Walleij {
249c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
250c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
251c8582339SLinus Walleij 	struct gpio_desc *desc;
252c8582339SLinus Walleij 
253c8582339SLinus Walleij 	/*
254c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
255c8582339SLinus Walleij 	 * is false.
256c8582339SLinus Walleij 	 */
257c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
258c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
259c8582339SLinus Walleij 
260c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
261c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
262c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
263c8582339SLinus Walleij 
264c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
265c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
266c8582339SLinus Walleij 
267c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
268c8582339SLinus Walleij 	return desc;
269c8582339SLinus Walleij }
270c8582339SLinus Walleij 
2716a537d48SLinus Walleij /*
272e3023bf8SLinus Walleij  * The old Freescale bindings use simply "gpios" as name for the chip select
273e3023bf8SLinus Walleij  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
274e3023bf8SLinus Walleij  * with a special quirk.
275e3023bf8SLinus Walleij  */
276e3023bf8SLinus Walleij static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
277e3023bf8SLinus Walleij 					     const char *con_id,
278e3023bf8SLinus Walleij 					     unsigned int idx,
279e3023bf8SLinus Walleij 					     unsigned long *flags)
280e3023bf8SLinus Walleij {
281e3023bf8SLinus Walleij 	struct device_node *np = dev->of_node;
282e3023bf8SLinus Walleij 
283e3023bf8SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
284e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
285e3023bf8SLinus Walleij 
286e3023bf8SLinus Walleij 	/* Allow this specifically for Freescale devices */
287e3023bf8SLinus Walleij 	if (!of_device_is_compatible(np, "fsl,spi") &&
288e3023bf8SLinus Walleij 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
289e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
290e3023bf8SLinus Walleij 	/* Allow only if asking for "cs-gpios" */
291e3023bf8SLinus Walleij 	if (!con_id || strcmp(con_id, "cs"))
292e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
293e3023bf8SLinus Walleij 
294e3023bf8SLinus Walleij 	/*
295e3023bf8SLinus Walleij 	 * While all other SPI controllers use "cs-gpios" the Freescale
296e3023bf8SLinus Walleij 	 * uses just "gpios" so translate to that when "cs-gpios" is
297e3023bf8SLinus Walleij 	 * requested.
298e3023bf8SLinus Walleij 	 */
299e3023bf8SLinus Walleij 	return of_find_gpio(dev, NULL, idx, flags);
300e3023bf8SLinus Walleij }
301e3023bf8SLinus Walleij 
302e3023bf8SLinus Walleij /*
3036a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
3046a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
3056a537d48SLinus Walleij  * them.
3066a537d48SLinus Walleij  */
3076a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
3086a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
3096a537d48SLinus Walleij {
3106a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
3116a537d48SLinus Walleij 	const char *whitelist[] = {
3126a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
3136a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
3146a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
3156a537d48SLinus Walleij 	};
3166a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
3176a537d48SLinus Walleij 	struct gpio_desc *desc;
3186a537d48SLinus Walleij 	int i;
3196a537d48SLinus Walleij 
3206a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
3216a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
3226a537d48SLinus Walleij 
3236a537d48SLinus Walleij 	if (!con_id)
3246a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
3256a537d48SLinus Walleij 
3264b21f94aSAndy Shevchenko 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
3274b21f94aSAndy Shevchenko 	if (i < 0)
3286a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
3296a537d48SLinus Walleij 
3306a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
3316a537d48SLinus Walleij 	return desc;
3326a537d48SLinus Walleij }
3336a537d48SLinus Walleij 
334ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
335fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
336ea713bc4SLinus Walleij {
337ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
338ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
339ea713bc4SLinus Walleij 	struct gpio_desc *desc;
340ea713bc4SLinus Walleij 	unsigned int i;
341ea713bc4SLinus Walleij 
342c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
343ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
344ea713bc4SLinus Walleij 		if (con_id)
345ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
346ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
347ea713bc4SLinus Walleij 		else
348ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
349ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
350ea713bc4SLinus Walleij 
351ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
352ea713bc4SLinus Walleij 						&of_flags);
3536662ae6aSMaxime Ripard 		/*
3546662ae6aSMaxime Ripard 		 * -EPROBE_DEFER in our case means that we found a
3556662ae6aSMaxime Ripard 		 * valid GPIO property, but no controller has been
3566662ae6aSMaxime Ripard 		 * registered so far.
3576662ae6aSMaxime Ripard 		 *
3586662ae6aSMaxime Ripard 		 * This means we don't need to look any further for
3596662ae6aSMaxime Ripard 		 * alternate name conventions, and we should really
3606662ae6aSMaxime Ripard 		 * preserve the return code for our user to be able to
3616662ae6aSMaxime Ripard 		 * retry probing later.
3626662ae6aSMaxime Ripard 		 */
3636662ae6aSMaxime Ripard 		if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
3646662ae6aSMaxime Ripard 			return desc;
3656662ae6aSMaxime Ripard 
366ea713bc4SLinus Walleij 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
367ea713bc4SLinus Walleij 			break;
368ea713bc4SLinus Walleij 	}
369ea713bc4SLinus Walleij 
370c8582339SLinus Walleij 	/* Special handling for SPI GPIOs if used */
371c8582339SLinus Walleij 	if (IS_ERR(desc))
372c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
373e3023bf8SLinus Walleij 	if (IS_ERR(desc)) {
374e3023bf8SLinus Walleij 		/* This quirk looks up flags and all */
375e3023bf8SLinus Walleij 		desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
376e3023bf8SLinus Walleij 		if (!IS_ERR(desc))
377e3023bf8SLinus Walleij 			return desc;
378e3023bf8SLinus Walleij 	}
379c8582339SLinus Walleij 
3806a537d48SLinus Walleij 	/* Special handling for regulator GPIOs if used */
381ce27fb2cSChen-Yu Tsai 	if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
3826a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
3836a537d48SLinus Walleij 
384ea713bc4SLinus Walleij 	if (IS_ERR(desc))
385ea713bc4SLinus Walleij 		return desc;
386ea713bc4SLinus Walleij 
387ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
388ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
389ea713bc4SLinus Walleij 
390ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
3914c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
392ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
393ea713bc4SLinus Walleij 		else
394ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
395ea713bc4SLinus Walleij 	}
396ea713bc4SLinus Walleij 
397e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
398e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
39905f479bfSCharles Keepax 
400d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_UP)
401d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_UP;
402d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_DOWN)
403d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_DOWN;
404d449991cSThomas Petazzoni 
405ea713bc4SLinus Walleij 	return desc;
406ea713bc4SLinus Walleij }
407ea713bc4SLinus Walleij 
408f141ed65SGrant Likely /**
409fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
410f625d460SBenoit Parrot  * @np:		device node to get GPIO from
411be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
412a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
413f625d460SBenoit Parrot  * @name:	GPIO line name
414fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
415fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
416f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
417f625d460SBenoit Parrot  *
418f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
419f625d460SBenoit Parrot  * value on the error condition.
420f625d460SBenoit Parrot  */
421fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
422be715343SMasahiro Yamada 					   struct gpio_chip *chip,
423a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
424fed7026aSAndy Shevchenko 					   unsigned long *lflags,
425f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
426f625d460SBenoit Parrot {
427f625d460SBenoit Parrot 	struct device_node *chip_np;
428f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
429be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
430be715343SMasahiro Yamada 	struct gpio_desc *desc;
431a79fead5SGeert Uytterhoeven 	unsigned int i;
432f625d460SBenoit Parrot 	u32 tmp;
4333f9547e1SMasahiro Yamada 	int ret;
434f625d460SBenoit Parrot 
435be715343SMasahiro Yamada 	chip_np = chip->of_node;
436f625d460SBenoit Parrot 	if (!chip_np)
437f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
438f625d460SBenoit Parrot 
439f625d460SBenoit Parrot 	xlate_flags = 0;
4402d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
441f625d460SBenoit Parrot 	*dflags = 0;
442f625d460SBenoit Parrot 
443f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
444f625d460SBenoit Parrot 	if (ret)
445f625d460SBenoit Parrot 		return ERR_PTR(ret);
446f625d460SBenoit Parrot 
447be715343SMasahiro Yamada 	gpiospec.np = chip_np;
448be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
449f625d460SBenoit Parrot 
450a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
451a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
452a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
453f625d460SBenoit Parrot 		if (ret)
454f625d460SBenoit Parrot 			return ERR_PTR(ret);
455a79fead5SGeert Uytterhoeven 	}
456f625d460SBenoit Parrot 
45799468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
458be715343SMasahiro Yamada 	if (IS_ERR(desc))
459be715343SMasahiro Yamada 		return desc;
460f625d460SBenoit Parrot 
461f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
462f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
463e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
464e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
465f625d460SBenoit Parrot 
466f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
467f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
468f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
469f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
470f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
471f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
472f625d460SBenoit Parrot 	else {
47362cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
47462cdcb6cSRob Herring 			desc_to_gpio(desc), np);
475f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
476f625d460SBenoit Parrot 	}
477f625d460SBenoit Parrot 
478f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
479f625d460SBenoit Parrot 		*name = np->name;
480f625d460SBenoit Parrot 
481be715343SMasahiro Yamada 	return desc;
482f625d460SBenoit Parrot }
483f625d460SBenoit Parrot 
484f625d460SBenoit Parrot /**
485fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
486f625d460SBenoit Parrot  * @chip:	gpio chip to act on
487f625d460SBenoit Parrot  *
488f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
489f625d460SBenoit Parrot  * configuration.
490ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
491f625d460SBenoit Parrot  */
492dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
493f625d460SBenoit Parrot {
494f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
495f625d460SBenoit Parrot 	struct device_node *np;
496f625d460SBenoit Parrot 	const char *name;
497fed7026aSAndy Shevchenko 	unsigned long lflags;
498f625d460SBenoit Parrot 	enum gpiod_flags dflags;
499a79fead5SGeert Uytterhoeven 	unsigned int i;
500dfbd379bSLaxman Dewangan 	int ret;
501f625d460SBenoit Parrot 
502d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
503f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
504f625d460SBenoit Parrot 			continue;
505f625d460SBenoit Parrot 
506a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
507a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
508a79fead5SGeert Uytterhoeven 						 &dflags);
509f625d460SBenoit Parrot 			if (IS_ERR(desc))
510a79fead5SGeert Uytterhoeven 				break;
511f625d460SBenoit Parrot 
512dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
51309e258afSWei Yongjun 			if (ret < 0) {
51409e258afSWei Yongjun 				of_node_put(np);
515dfbd379bSLaxman Dewangan 				return ret;
516f625d460SBenoit Parrot 			}
51709e258afSWei Yongjun 		}
518a79fead5SGeert Uytterhoeven 	}
519dfbd379bSLaxman Dewangan 
520dfbd379bSLaxman Dewangan 	return 0;
521f625d460SBenoit Parrot }
522f625d460SBenoit Parrot 
523f625d460SBenoit Parrot /**
52467049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
525f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
52667049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
527f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
528f141ed65SGrant Likely  *
529f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
53067049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
531f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
532f141ed65SGrant Likely  */
533f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
534f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
535f141ed65SGrant Likely {
536f141ed65SGrant Likely 	/*
537f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
53820a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
539f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
540f141ed65SGrant Likely 	 * but not recommended).
541f141ed65SGrant Likely 	 */
542f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
543f141ed65SGrant Likely 		WARN_ON(1);
544f141ed65SGrant Likely 		return -EINVAL;
545f141ed65SGrant Likely 	}
546f141ed65SGrant Likely 
547f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
548f141ed65SGrant Likely 		return -EINVAL;
549f141ed65SGrant Likely 
5507b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
551f141ed65SGrant Likely 		return -EINVAL;
552f141ed65SGrant Likely 
553f141ed65SGrant Likely 	if (flags)
554f141ed65SGrant Likely 		*flags = gpiospec->args[1];
555f141ed65SGrant Likely 
556f141ed65SGrant Likely 	return gpiospec->args[0];
557f141ed65SGrant Likely }
558f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
559f141ed65SGrant Likely 
560f141ed65SGrant Likely /**
5613208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
562f141ed65SGrant Likely  * @np:		device node of the GPIO chip
563f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
5643208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
565f141ed65SGrant Likely  *
566f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
567f141ed65SGrant Likely  *
568f141ed65SGrant Likely  * 1) In the gpio_chip structure:
569f141ed65SGrant Likely  *    - all the callbacks
570f141ed65SGrant Likely  *    - of_gpio_n_cells
571f141ed65SGrant Likely  *    - of_xlate callback (optional)
572f141ed65SGrant Likely  *
573f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
574f141ed65SGrant Likely  *    - save_regs callback (optional)
575f141ed65SGrant Likely  *
576f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
577f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
578f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
579f141ed65SGrant Likely  */
5803208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
5813208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
5823208b0f0SLinus Walleij 			    void *data)
583f141ed65SGrant Likely {
584f141ed65SGrant Likely 	int ret = -ENOMEM;
585f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
586f141ed65SGrant Likely 
5877eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
588f141ed65SGrant Likely 	if (!gc->label)
589f141ed65SGrant Likely 		goto err0;
590f141ed65SGrant Likely 
591f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
592f141ed65SGrant Likely 	if (!mm_gc->regs)
593f141ed65SGrant Likely 		goto err1;
594f141ed65SGrant Likely 
595f141ed65SGrant Likely 	gc->base = -1;
596f141ed65SGrant Likely 
597f141ed65SGrant Likely 	if (mm_gc->save_regs)
598f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
599f141ed65SGrant Likely 
600f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
601f141ed65SGrant Likely 
6023208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
603f141ed65SGrant Likely 	if (ret)
604f141ed65SGrant Likely 		goto err2;
605f141ed65SGrant Likely 
606f141ed65SGrant Likely 	return 0;
607f141ed65SGrant Likely err2:
608f141ed65SGrant Likely 	iounmap(mm_gc->regs);
609f141ed65SGrant Likely err1:
610f141ed65SGrant Likely 	kfree(gc->label);
611f141ed65SGrant Likely err0:
6127eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
613f141ed65SGrant Likely 	return ret;
614f141ed65SGrant Likely }
6153208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
616f141ed65SGrant Likely 
617d621e8baSRicardo Ribalda Delgado /**
618d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
619d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
620d621e8baSRicardo Ribalda Delgado  */
621d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
622d621e8baSRicardo Ribalda Delgado {
623d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
624d621e8baSRicardo Ribalda Delgado 
625d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
626d621e8baSRicardo Ribalda Delgado 		return;
627d621e8baSRicardo Ribalda Delgado 
628d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
629d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
630d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
631d621e8baSRicardo Ribalda Delgado }
632d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
633d621e8baSRicardo Ribalda Delgado 
634726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
635726cb3baSStephen Boyd {
636726cb3baSStephen Boyd 	int len, i;
637726cb3baSStephen Boyd 	u32 start, count;
638726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
639726cb3baSStephen Boyd 
640726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
641726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
642726cb3baSStephen Boyd 		return;
643726cb3baSStephen Boyd 
644726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
645726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
646726cb3baSStephen Boyd 					   i, &start);
647726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
648726cb3baSStephen Boyd 					   i + 1, &count);
649726cb3baSStephen Boyd 		if (start >= chip->ngpio || start + count >= chip->ngpio)
650726cb3baSStephen Boyd 			continue;
651726cb3baSStephen Boyd 
652726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
653726cb3baSStephen Boyd 	}
654726cb3baSStephen Boyd };
655726cb3baSStephen Boyd 
656f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
65728355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
658f23f1516SShiraz Hashim {
659f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
660f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
6611e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
662f23f1516SShiraz Hashim 	int index = 0, ret;
663586a87e6SChristian Ruppert 	const char *name;
664586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
665586a87e6SChristian Ruppert 	struct property *group_names;
666f23f1516SShiraz Hashim 
667f23f1516SShiraz Hashim 	if (!np)
66828355f81STomeu Vizoso 		return 0;
669f23f1516SShiraz Hashim 
670586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
671586a87e6SChristian Ruppert 
672ad4e1a7cSHaojian Zhuang 	for (;; index++) {
673d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
674d9fe0039SStephen Warren 				index, &pinspec);
675f23f1516SShiraz Hashim 		if (ret)
676f23f1516SShiraz Hashim 			break;
677f23f1516SShiraz Hashim 
6781e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
679602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
6801e63d7b9SLinus Walleij 		if (!pctldev)
68128355f81STomeu Vizoso 			return -EPROBE_DEFER;
682f23f1516SShiraz Hashim 
683586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
684586a87e6SChristian Ruppert 			if (group_names) {
68572858602SLaurent Navet 				of_property_read_string_index(np,
686586a87e6SChristian Ruppert 						group_names_propname,
687586a87e6SChristian Ruppert 						index, &name);
688586a87e6SChristian Ruppert 				if (strlen(name)) {
6897eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
6907eb6ce2fSRob Herring 						np);
691586a87e6SChristian Ruppert 					break;
692586a87e6SChristian Ruppert 				}
693586a87e6SChristian Ruppert 			}
694586a87e6SChristian Ruppert 			/* npins != 0: linear range */
6951e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
696ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
6971e63d7b9SLinus Walleij 					pinspec.args[0],
69886853c83SHaojian Zhuang 					pinspec.args[1],
69986853c83SHaojian Zhuang 					pinspec.args[2]);
7001e63d7b9SLinus Walleij 			if (ret)
70128355f81STomeu Vizoso 				return ret;
702586a87e6SChristian Ruppert 		} else {
703586a87e6SChristian Ruppert 			/* npins == 0: special range */
704586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
7057eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
7067eb6ce2fSRob Herring 					np);
707586a87e6SChristian Ruppert 				break;
708586a87e6SChristian Ruppert 			}
709586a87e6SChristian Ruppert 
710586a87e6SChristian Ruppert 			if (!group_names) {
7117eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
7127eb6ce2fSRob Herring 					np, group_names_propname);
713586a87e6SChristian Ruppert 				break;
714586a87e6SChristian Ruppert 			}
715586a87e6SChristian Ruppert 
716586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
717586a87e6SChristian Ruppert 						group_names_propname,
718586a87e6SChristian Ruppert 						index, &name);
719586a87e6SChristian Ruppert 			if (ret)
720586a87e6SChristian Ruppert 				break;
721586a87e6SChristian Ruppert 
722586a87e6SChristian Ruppert 			if (!strlen(name)) {
7237eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
7247eb6ce2fSRob Herring 				np);
725586a87e6SChristian Ruppert 				break;
726586a87e6SChristian Ruppert 			}
727586a87e6SChristian Ruppert 
728586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
729586a87e6SChristian Ruppert 						pinspec.args[0], name);
730586a87e6SChristian Ruppert 			if (ret)
73128355f81STomeu Vizoso 				return ret;
732586a87e6SChristian Ruppert 		}
733ad4e1a7cSHaojian Zhuang 	}
73428355f81STomeu Vizoso 
73528355f81STomeu Vizoso 	return 0;
736f23f1516SShiraz Hashim }
737f23f1516SShiraz Hashim 
738f23f1516SShiraz Hashim #else
73928355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
740f23f1516SShiraz Hashim #endif
741f23f1516SShiraz Hashim 
74228355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
743f141ed65SGrant Likely {
74428355f81STomeu Vizoso 	int status;
74528355f81STomeu Vizoso 
746f141ed65SGrant Likely 	if (!chip->of_node)
74728355f81STomeu Vizoso 		return 0;
748f141ed65SGrant Likely 
749f141ed65SGrant Likely 	if (!chip->of_xlate) {
750f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
751f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
752f141ed65SGrant Likely 	}
753f141ed65SGrant Likely 
7541020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
7551020dfd1SMasahiro Yamada 		return -EINVAL;
7561020dfd1SMasahiro Yamada 
757726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
758726cb3baSStephen Boyd 
75928355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
76028355f81STomeu Vizoso 	if (status)
76128355f81STomeu Vizoso 		return status;
76228355f81STomeu Vizoso 
763fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
764fd9c5531SLinus Walleij 	if (!chip->names)
76582270335SChristophe Leroy 		devprop_gpiochip_set_names(chip,
76682270335SChristophe Leroy 					   of_fwnode_handle(chip->of_node));
767fd9c5531SLinus Walleij 
768f141ed65SGrant Likely 	of_node_get(chip->of_node);
769f625d460SBenoit Parrot 
770f7299d44SGeert Uytterhoeven 	status = of_gpiochip_scan_gpios(chip);
771f7299d44SGeert Uytterhoeven 	if (status) {
772f7299d44SGeert Uytterhoeven 		of_node_put(chip->of_node);
773f7299d44SGeert Uytterhoeven 		gpiochip_remove_pin_ranges(chip);
774f7299d44SGeert Uytterhoeven 	}
775f7299d44SGeert Uytterhoeven 
776f7299d44SGeert Uytterhoeven 	return status;
777f141ed65SGrant Likely }
778f141ed65SGrant Likely 
779f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
780f141ed65SGrant Likely {
781e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
782f141ed65SGrant Likely 	of_node_put(chip->of_node);
783f141ed65SGrant Likely }
784