xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision edc1ef3f)
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.
1216953c57aSLinus Walleij 	 */
122a71a81e7SGeert Uytterhoeven 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
123a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
1246953c57aSLinus Walleij 		struct device_node *child;
1256953c57aSLinus Walleij 		u32 cs;
1266953c57aSLinus Walleij 		int ret;
1276953c57aSLinus Walleij 
1286953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
1296953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
130c1c04ceaSLinus Walleij 			if (ret)
1316953c57aSLinus Walleij 				continue;
1326953c57aSLinus Walleij 			if (cs == index) {
1336953c57aSLinus Walleij 				/*
1346953c57aSLinus Walleij 				 * SPI children have active low chip selects
1356953c57aSLinus Walleij 				 * by default. This can be specified negatively
1366953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
1376953c57aSLinus Walleij 				 * device node, or actively by tagging on
1386953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
1396953c57aSLinus Walleij 				 * tree. If the line is simultaneously
1406953c57aSLinus Walleij 				 * tagged as active low in the device tree
1416953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
1426953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
1436953c57aSLinus Walleij 				 * take precedence.
1446953c57aSLinus Walleij 				 */
1457ce40277SAndrey Smirnov 				if (of_property_read_bool(child, "spi-cs-high")) {
1466953c57aSLinus Walleij 					if (*flags & OF_GPIO_ACTIVE_LOW) {
1476953c57aSLinus Walleij 						pr_warn("%s GPIO handle specifies active low - ignored\n",
1487ce40277SAndrey Smirnov 							of_node_full_name(child));
1496953c57aSLinus Walleij 						*flags &= ~OF_GPIO_ACTIVE_LOW;
1506953c57aSLinus Walleij 					}
1516953c57aSLinus Walleij 				} else {
1526953c57aSLinus Walleij 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
1536953c57aSLinus Walleij 						pr_info("%s enforce active low on chipselect handle\n",
1547ce40277SAndrey Smirnov 							of_node_full_name(child));
1556953c57aSLinus Walleij 					*flags |= OF_GPIO_ACTIVE_LOW;
1566953c57aSLinus Walleij 				}
1576953c57aSLinus Walleij 				break;
1586953c57aSLinus Walleij 			}
1596953c57aSLinus Walleij 		}
1606953c57aSLinus Walleij 	}
161edc1ef3fSMartin Blumenstingl 
162edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
163edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
164edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
165edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
166edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
167a603a2b8SLinus Walleij }
168a603a2b8SLinus Walleij 
169f141ed65SGrant Likely /**
170af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
171f141ed65SGrant Likely  * @np:		device node to get GPIO from
172f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
173f141ed65SGrant Likely  * @index:	index of the GPIO
174f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
175f141ed65SGrant Likely  *
176af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
177f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
178f141ed65SGrant Likely  * in flags for the GPIO.
179f141ed65SGrant Likely  */
180af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
181af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
182f141ed65SGrant Likely {
183762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
184762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
185762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
186f141ed65SGrant Likely 	int ret;
187f141ed65SGrant Likely 
188c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
189762c2e46SMasahiro Yamada 					     &gpiospec);
1903d0f7cf0SGrant Likely 	if (ret) {
1917eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
1927eb6ce2fSRob Herring 			__func__, propname, np, index);
193af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
1943d0f7cf0SGrant Likely 	}
195f141ed65SGrant Likely 
196c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
197762c2e46SMasahiro Yamada 	if (!chip) {
198762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
199762c2e46SMasahiro Yamada 		goto out;
200762c2e46SMasahiro Yamada 	}
2013d0f7cf0SGrant Likely 
20299468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
203762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
204762c2e46SMasahiro Yamada 		goto out;
205762c2e46SMasahiro Yamada 
206605f2d34SLinus Walleij 	if (flags)
20789a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
208a603a2b8SLinus Walleij 
2097eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
2107eb6ce2fSRob Herring 		 __func__, propname, np, index,
211762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
212762c2e46SMasahiro Yamada 
213762c2e46SMasahiro Yamada out:
214762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
215762c2e46SMasahiro Yamada 
216762c2e46SMasahiro Yamada 	return desc;
217f141ed65SGrant Likely }
218f141ed65SGrant Likely 
219f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
220f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
221f01d9075SAlexandre Courbot {
222f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
223f01d9075SAlexandre Courbot 
224f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
225f01d9075SAlexandre Courbot 
226f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
227f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
228f01d9075SAlexandre Courbot 	else
229f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
230f01d9075SAlexandre Courbot }
231f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
232f01d9075SAlexandre Courbot 
233c8582339SLinus Walleij /*
234c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
235c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
236c8582339SLinus Walleij  * them.
237c8582339SLinus Walleij  */
238c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
239c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
240c8582339SLinus Walleij {
241c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
242c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
243c8582339SLinus Walleij 	struct gpio_desc *desc;
244c8582339SLinus Walleij 
245c8582339SLinus Walleij 	/*
246c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
247c8582339SLinus Walleij 	 * is false.
248c8582339SLinus Walleij 	 */
249c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
250c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
251c8582339SLinus Walleij 
252c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
253c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
254c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
255c8582339SLinus Walleij 
256c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
257c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
258c8582339SLinus Walleij 
259c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
260c8582339SLinus Walleij 	return desc;
261c8582339SLinus Walleij }
262c8582339SLinus Walleij 
2636a537d48SLinus Walleij /*
2646a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
2656a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
2666a537d48SLinus Walleij  * them.
2676a537d48SLinus Walleij  */
2686a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
2696a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
2706a537d48SLinus Walleij {
2716a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
2726a537d48SLinus Walleij 	const char *whitelist[] = {
2736a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
2746a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
2756a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
2766a537d48SLinus Walleij 	};
2776a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
2786a537d48SLinus Walleij 	struct gpio_desc *desc;
2796a537d48SLinus Walleij 	int i;
2806a537d48SLinus Walleij 
2816a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
2826a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2836a537d48SLinus Walleij 
2846a537d48SLinus Walleij 	if (!con_id)
2856a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2866a537d48SLinus Walleij 
2874b21f94aSAndy Shevchenko 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
2884b21f94aSAndy Shevchenko 	if (i < 0)
2896a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2906a537d48SLinus Walleij 
2916a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
2926a537d48SLinus Walleij 	return desc;
2936a537d48SLinus Walleij }
2946a537d48SLinus Walleij 
295ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
296fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
297ea713bc4SLinus Walleij {
298ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
299ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
300ea713bc4SLinus Walleij 	struct gpio_desc *desc;
301ea713bc4SLinus Walleij 	unsigned int i;
302ea713bc4SLinus Walleij 
303c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
304ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
305ea713bc4SLinus Walleij 		if (con_id)
306ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
307ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
308ea713bc4SLinus Walleij 		else
309ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
310ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
311ea713bc4SLinus Walleij 
312ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
313ea713bc4SLinus Walleij 						&of_flags);
3146662ae6aSMaxime Ripard 		/*
3156662ae6aSMaxime Ripard 		 * -EPROBE_DEFER in our case means that we found a
3166662ae6aSMaxime Ripard 		 * valid GPIO property, but no controller has been
3176662ae6aSMaxime Ripard 		 * registered so far.
3186662ae6aSMaxime Ripard 		 *
3196662ae6aSMaxime Ripard 		 * This means we don't need to look any further for
3206662ae6aSMaxime Ripard 		 * alternate name conventions, and we should really
3216662ae6aSMaxime Ripard 		 * preserve the return code for our user to be able to
3226662ae6aSMaxime Ripard 		 * retry probing later.
3236662ae6aSMaxime Ripard 		 */
3246662ae6aSMaxime Ripard 		if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
3256662ae6aSMaxime Ripard 			return desc;
3266662ae6aSMaxime Ripard 
327ea713bc4SLinus Walleij 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
328ea713bc4SLinus Walleij 			break;
329ea713bc4SLinus Walleij 	}
330ea713bc4SLinus Walleij 
331c8582339SLinus Walleij 	/* Special handling for SPI GPIOs if used */
332c8582339SLinus Walleij 	if (IS_ERR(desc))
333c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
334c8582339SLinus Walleij 
3356a537d48SLinus Walleij 	/* Special handling for regulator GPIOs if used */
336ce27fb2cSChen-Yu Tsai 	if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
3376a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
3386a537d48SLinus Walleij 
339ea713bc4SLinus Walleij 	if (IS_ERR(desc))
340ea713bc4SLinus Walleij 		return desc;
341ea713bc4SLinus Walleij 
342ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
343ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
344ea713bc4SLinus Walleij 
345ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
3464c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
347ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
348ea713bc4SLinus Walleij 		else
349ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
350ea713bc4SLinus Walleij 	}
351ea713bc4SLinus Walleij 
352e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
353e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
35405f479bfSCharles Keepax 
355d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_UP)
356d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_UP;
357d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_DOWN)
358d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_DOWN;
359d449991cSThomas Petazzoni 
360ea713bc4SLinus Walleij 	return desc;
361ea713bc4SLinus Walleij }
362ea713bc4SLinus Walleij 
363f141ed65SGrant Likely /**
364fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
365f625d460SBenoit Parrot  * @np:		device node to get GPIO from
366be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
367a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
368f625d460SBenoit Parrot  * @name:	GPIO line name
369fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
370fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
371f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
372f625d460SBenoit Parrot  *
373f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
374f625d460SBenoit Parrot  * value on the error condition.
375f625d460SBenoit Parrot  */
376fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
377be715343SMasahiro Yamada 					   struct gpio_chip *chip,
378a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
379fed7026aSAndy Shevchenko 					   unsigned long *lflags,
380f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
381f625d460SBenoit Parrot {
382f625d460SBenoit Parrot 	struct device_node *chip_np;
383f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
384be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
385be715343SMasahiro Yamada 	struct gpio_desc *desc;
386a79fead5SGeert Uytterhoeven 	unsigned int i;
387f625d460SBenoit Parrot 	u32 tmp;
3883f9547e1SMasahiro Yamada 	int ret;
389f625d460SBenoit Parrot 
390be715343SMasahiro Yamada 	chip_np = chip->of_node;
391f625d460SBenoit Parrot 	if (!chip_np)
392f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
393f625d460SBenoit Parrot 
394f625d460SBenoit Parrot 	xlate_flags = 0;
3952d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
396f625d460SBenoit Parrot 	*dflags = 0;
397f625d460SBenoit Parrot 
398f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
399f625d460SBenoit Parrot 	if (ret)
400f625d460SBenoit Parrot 		return ERR_PTR(ret);
401f625d460SBenoit Parrot 
402be715343SMasahiro Yamada 	gpiospec.np = chip_np;
403be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
404f625d460SBenoit Parrot 
405a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
406a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
407a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
408f625d460SBenoit Parrot 		if (ret)
409f625d460SBenoit Parrot 			return ERR_PTR(ret);
410a79fead5SGeert Uytterhoeven 	}
411f625d460SBenoit Parrot 
41299468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
413be715343SMasahiro Yamada 	if (IS_ERR(desc))
414be715343SMasahiro Yamada 		return desc;
415f625d460SBenoit Parrot 
416f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
417f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
418e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
419e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
420f625d460SBenoit Parrot 
421f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
422f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
423f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
424f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
425f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
426f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
427f625d460SBenoit Parrot 	else {
42862cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
42962cdcb6cSRob Herring 			desc_to_gpio(desc), np);
430f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
431f625d460SBenoit Parrot 	}
432f625d460SBenoit Parrot 
433f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
434f625d460SBenoit Parrot 		*name = np->name;
435f625d460SBenoit Parrot 
436be715343SMasahiro Yamada 	return desc;
437f625d460SBenoit Parrot }
438f625d460SBenoit Parrot 
439f625d460SBenoit Parrot /**
440fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
441f625d460SBenoit Parrot  * @chip:	gpio chip to act on
442f625d460SBenoit Parrot  *
443f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
444f625d460SBenoit Parrot  * configuration.
445ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
446f625d460SBenoit Parrot  */
447dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
448f625d460SBenoit Parrot {
449f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
450f625d460SBenoit Parrot 	struct device_node *np;
451f625d460SBenoit Parrot 	const char *name;
452fed7026aSAndy Shevchenko 	unsigned long lflags;
453f625d460SBenoit Parrot 	enum gpiod_flags dflags;
454a79fead5SGeert Uytterhoeven 	unsigned int i;
455dfbd379bSLaxman Dewangan 	int ret;
456f625d460SBenoit Parrot 
457d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
458f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
459f625d460SBenoit Parrot 			continue;
460f625d460SBenoit Parrot 
461a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
462a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
463a79fead5SGeert Uytterhoeven 						 &dflags);
464f625d460SBenoit Parrot 			if (IS_ERR(desc))
465a79fead5SGeert Uytterhoeven 				break;
466f625d460SBenoit Parrot 
467dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
46809e258afSWei Yongjun 			if (ret < 0) {
46909e258afSWei Yongjun 				of_node_put(np);
470dfbd379bSLaxman Dewangan 				return ret;
471f625d460SBenoit Parrot 			}
47209e258afSWei Yongjun 		}
473a79fead5SGeert Uytterhoeven 	}
474dfbd379bSLaxman Dewangan 
475dfbd379bSLaxman Dewangan 	return 0;
476f625d460SBenoit Parrot }
477f625d460SBenoit Parrot 
478f625d460SBenoit Parrot /**
47967049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
480f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
48167049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
482f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
483f141ed65SGrant Likely  *
484f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
48567049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
486f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
487f141ed65SGrant Likely  */
488f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
489f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
490f141ed65SGrant Likely {
491f141ed65SGrant Likely 	/*
492f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
49320a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
494f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
495f141ed65SGrant Likely 	 * but not recommended).
496f141ed65SGrant Likely 	 */
497f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
498f141ed65SGrant Likely 		WARN_ON(1);
499f141ed65SGrant Likely 		return -EINVAL;
500f141ed65SGrant Likely 	}
501f141ed65SGrant Likely 
502f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
503f141ed65SGrant Likely 		return -EINVAL;
504f141ed65SGrant Likely 
5057b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
506f141ed65SGrant Likely 		return -EINVAL;
507f141ed65SGrant Likely 
508f141ed65SGrant Likely 	if (flags)
509f141ed65SGrant Likely 		*flags = gpiospec->args[1];
510f141ed65SGrant Likely 
511f141ed65SGrant Likely 	return gpiospec->args[0];
512f141ed65SGrant Likely }
513f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
514f141ed65SGrant Likely 
515f141ed65SGrant Likely /**
5163208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
517f141ed65SGrant Likely  * @np:		device node of the GPIO chip
518f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
5193208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
520f141ed65SGrant Likely  *
521f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
522f141ed65SGrant Likely  *
523f141ed65SGrant Likely  * 1) In the gpio_chip structure:
524f141ed65SGrant Likely  *    - all the callbacks
525f141ed65SGrant Likely  *    - of_gpio_n_cells
526f141ed65SGrant Likely  *    - of_xlate callback (optional)
527f141ed65SGrant Likely  *
528f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
529f141ed65SGrant Likely  *    - save_regs callback (optional)
530f141ed65SGrant Likely  *
531f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
532f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
533f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
534f141ed65SGrant Likely  */
5353208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
5363208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
5373208b0f0SLinus Walleij 			    void *data)
538f141ed65SGrant Likely {
539f141ed65SGrant Likely 	int ret = -ENOMEM;
540f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
541f141ed65SGrant Likely 
5427eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
543f141ed65SGrant Likely 	if (!gc->label)
544f141ed65SGrant Likely 		goto err0;
545f141ed65SGrant Likely 
546f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
547f141ed65SGrant Likely 	if (!mm_gc->regs)
548f141ed65SGrant Likely 		goto err1;
549f141ed65SGrant Likely 
550f141ed65SGrant Likely 	gc->base = -1;
551f141ed65SGrant Likely 
552f141ed65SGrant Likely 	if (mm_gc->save_regs)
553f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
554f141ed65SGrant Likely 
555f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
556f141ed65SGrant Likely 
5573208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
558f141ed65SGrant Likely 	if (ret)
559f141ed65SGrant Likely 		goto err2;
560f141ed65SGrant Likely 
561f141ed65SGrant Likely 	return 0;
562f141ed65SGrant Likely err2:
563f141ed65SGrant Likely 	iounmap(mm_gc->regs);
564f141ed65SGrant Likely err1:
565f141ed65SGrant Likely 	kfree(gc->label);
566f141ed65SGrant Likely err0:
5677eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
568f141ed65SGrant Likely 	return ret;
569f141ed65SGrant Likely }
5703208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
571f141ed65SGrant Likely 
572d621e8baSRicardo Ribalda Delgado /**
573d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
574d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
575d621e8baSRicardo Ribalda Delgado  */
576d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
577d621e8baSRicardo Ribalda Delgado {
578d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
579d621e8baSRicardo Ribalda Delgado 
580d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
581d621e8baSRicardo Ribalda Delgado 		return;
582d621e8baSRicardo Ribalda Delgado 
583d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
584d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
585d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
586d621e8baSRicardo Ribalda Delgado }
587d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
588d621e8baSRicardo Ribalda Delgado 
589726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
590726cb3baSStephen Boyd {
591726cb3baSStephen Boyd 	int len, i;
592726cb3baSStephen Boyd 	u32 start, count;
593726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
594726cb3baSStephen Boyd 
595726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
596726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
597726cb3baSStephen Boyd 		return;
598726cb3baSStephen Boyd 
599726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
600726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
601726cb3baSStephen Boyd 					   i, &start);
602726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
603726cb3baSStephen Boyd 					   i + 1, &count);
604726cb3baSStephen Boyd 		if (start >= chip->ngpio || start + count >= chip->ngpio)
605726cb3baSStephen Boyd 			continue;
606726cb3baSStephen Boyd 
607726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
608726cb3baSStephen Boyd 	}
609726cb3baSStephen Boyd };
610726cb3baSStephen Boyd 
611f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
61228355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
613f23f1516SShiraz Hashim {
614f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
615f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
6161e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
617f23f1516SShiraz Hashim 	int index = 0, ret;
618586a87e6SChristian Ruppert 	const char *name;
619586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
620586a87e6SChristian Ruppert 	struct property *group_names;
621f23f1516SShiraz Hashim 
622f23f1516SShiraz Hashim 	if (!np)
62328355f81STomeu Vizoso 		return 0;
624f23f1516SShiraz Hashim 
625586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
626586a87e6SChristian Ruppert 
627ad4e1a7cSHaojian Zhuang 	for (;; index++) {
628d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
629d9fe0039SStephen Warren 				index, &pinspec);
630f23f1516SShiraz Hashim 		if (ret)
631f23f1516SShiraz Hashim 			break;
632f23f1516SShiraz Hashim 
6331e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
634602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
6351e63d7b9SLinus Walleij 		if (!pctldev)
63628355f81STomeu Vizoso 			return -EPROBE_DEFER;
637f23f1516SShiraz Hashim 
638586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
639586a87e6SChristian Ruppert 			if (group_names) {
64072858602SLaurent Navet 				of_property_read_string_index(np,
641586a87e6SChristian Ruppert 						group_names_propname,
642586a87e6SChristian Ruppert 						index, &name);
643586a87e6SChristian Ruppert 				if (strlen(name)) {
6447eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
6457eb6ce2fSRob Herring 						np);
646586a87e6SChristian Ruppert 					break;
647586a87e6SChristian Ruppert 				}
648586a87e6SChristian Ruppert 			}
649586a87e6SChristian Ruppert 			/* npins != 0: linear range */
6501e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
651ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
6521e63d7b9SLinus Walleij 					pinspec.args[0],
65386853c83SHaojian Zhuang 					pinspec.args[1],
65486853c83SHaojian Zhuang 					pinspec.args[2]);
6551e63d7b9SLinus Walleij 			if (ret)
65628355f81STomeu Vizoso 				return ret;
657586a87e6SChristian Ruppert 		} else {
658586a87e6SChristian Ruppert 			/* npins == 0: special range */
659586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
6607eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
6617eb6ce2fSRob Herring 					np);
662586a87e6SChristian Ruppert 				break;
663586a87e6SChristian Ruppert 			}
664586a87e6SChristian Ruppert 
665586a87e6SChristian Ruppert 			if (!group_names) {
6667eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
6677eb6ce2fSRob Herring 					np, group_names_propname);
668586a87e6SChristian Ruppert 				break;
669586a87e6SChristian Ruppert 			}
670586a87e6SChristian Ruppert 
671586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
672586a87e6SChristian Ruppert 						group_names_propname,
673586a87e6SChristian Ruppert 						index, &name);
674586a87e6SChristian Ruppert 			if (ret)
675586a87e6SChristian Ruppert 				break;
676586a87e6SChristian Ruppert 
677586a87e6SChristian Ruppert 			if (!strlen(name)) {
6787eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
6797eb6ce2fSRob Herring 				np);
680586a87e6SChristian Ruppert 				break;
681586a87e6SChristian Ruppert 			}
682586a87e6SChristian Ruppert 
683586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
684586a87e6SChristian Ruppert 						pinspec.args[0], name);
685586a87e6SChristian Ruppert 			if (ret)
68628355f81STomeu Vizoso 				return ret;
687586a87e6SChristian Ruppert 		}
688ad4e1a7cSHaojian Zhuang 	}
68928355f81STomeu Vizoso 
69028355f81STomeu Vizoso 	return 0;
691f23f1516SShiraz Hashim }
692f23f1516SShiraz Hashim 
693f23f1516SShiraz Hashim #else
69428355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
695f23f1516SShiraz Hashim #endif
696f23f1516SShiraz Hashim 
69728355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
698f141ed65SGrant Likely {
69928355f81STomeu Vizoso 	int status;
70028355f81STomeu Vizoso 
701f141ed65SGrant Likely 	if (!chip->of_node)
70228355f81STomeu Vizoso 		return 0;
703f141ed65SGrant Likely 
704f141ed65SGrant Likely 	if (!chip->of_xlate) {
705f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
706f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
707f141ed65SGrant Likely 	}
708f141ed65SGrant Likely 
7091020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
7101020dfd1SMasahiro Yamada 		return -EINVAL;
7111020dfd1SMasahiro Yamada 
712726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
713726cb3baSStephen Boyd 
71428355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
71528355f81STomeu Vizoso 	if (status)
71628355f81STomeu Vizoso 		return status;
71728355f81STomeu Vizoso 
718fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
719fd9c5531SLinus Walleij 	if (!chip->names)
72082270335SChristophe Leroy 		devprop_gpiochip_set_names(chip,
72182270335SChristophe Leroy 					   of_fwnode_handle(chip->of_node));
722fd9c5531SLinus Walleij 
723f141ed65SGrant Likely 	of_node_get(chip->of_node);
724f625d460SBenoit Parrot 
725f7299d44SGeert Uytterhoeven 	status = of_gpiochip_scan_gpios(chip);
726f7299d44SGeert Uytterhoeven 	if (status) {
727f7299d44SGeert Uytterhoeven 		of_node_put(chip->of_node);
728f7299d44SGeert Uytterhoeven 		gpiochip_remove_pin_ranges(chip);
729f7299d44SGeert Uytterhoeven 	}
730f7299d44SGeert Uytterhoeven 
731f7299d44SGeert Uytterhoeven 	return status;
732f141ed65SGrant Likely }
733f141ed65SGrant Likely 
734f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
735f141ed65SGrant Likely {
736e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
737f141ed65SGrant Likely 	of_node_put(chip->of_node);
738f141ed65SGrant Likely }
739