xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision ea06a482)
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"
24f626d6dfSLinus Walleij #include "gpiolib-of.h"
25f626d6dfSLinus Walleij 
2671b8f600SLinus Walleij /**
2771b8f600SLinus Walleij  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
2871b8f600SLinus Walleij  * Some elder GPIO controllers need special quirks. Currently we handle
2971b8f600SLinus Walleij  * the Freescale GPIO controller with bindings that doesn't use the
3071b8f600SLinus Walleij  * established "cs-gpios" for chip selects but instead rely on
3171b8f600SLinus Walleij  * "gpios" for the chip select lines. If we detect this, we redirect
3271b8f600SLinus Walleij  * the counting of "cs-gpios" to count "gpios" transparent to the
3371b8f600SLinus Walleij  * driver.
3471b8f600SLinus Walleij  */
35a1f4c96bSYueHaibing static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
3671b8f600SLinus Walleij {
3771b8f600SLinus Walleij 	struct device_node *np = dev->of_node;
3871b8f600SLinus Walleij 
3971b8f600SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
4071b8f600SLinus Walleij 		return 0;
4171b8f600SLinus Walleij 	if (!con_id || strcmp(con_id, "cs"))
4271b8f600SLinus Walleij 		return 0;
4371b8f600SLinus Walleij 	if (!of_device_is_compatible(np, "fsl,spi") &&
4471b8f600SLinus Walleij 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
4571b8f600SLinus Walleij 		return 0;
4671b8f600SLinus Walleij 	return of_gpio_named_count(np, "gpios");
4771b8f600SLinus Walleij }
4871b8f600SLinus Walleij 
49f626d6dfSLinus Walleij /*
50f626d6dfSLinus Walleij  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
51f626d6dfSLinus Walleij  *
52f626d6dfSLinus Walleij  * FIXME: get rid of those external users by converting them to GPIO
53808b9931SGeert Uytterhoeven  * descriptors and let them all use gpiod_count()
54f626d6dfSLinus Walleij  */
55f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id)
56f626d6dfSLinus Walleij {
57f626d6dfSLinus Walleij 	int ret;
58f626d6dfSLinus Walleij 	char propname[32];
59f626d6dfSLinus Walleij 	unsigned int i;
60f626d6dfSLinus Walleij 
6171b8f600SLinus Walleij 	ret = of_gpio_spi_cs_get_count(dev, con_id);
6271b8f600SLinus Walleij 	if (ret > 0)
6371b8f600SLinus Walleij 		return ret;
6471b8f600SLinus Walleij 
65f626d6dfSLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
66f626d6dfSLinus Walleij 		if (con_id)
67f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s-%s",
68f626d6dfSLinus Walleij 				 con_id, gpio_suffixes[i]);
69f626d6dfSLinus Walleij 		else
70f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s",
71f626d6dfSLinus Walleij 				 gpio_suffixes[i]);
72f626d6dfSLinus Walleij 
73f626d6dfSLinus Walleij 		ret = of_gpio_named_count(dev->of_node, propname);
74f626d6dfSLinus Walleij 		if (ret > 0)
75f626d6dfSLinus Walleij 			break;
76f626d6dfSLinus Walleij 	}
77f626d6dfSLinus Walleij 	return ret ? ret : -ENOENT;
78f626d6dfSLinus Walleij }
79af8b6375SAlexandre Courbot 
80c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
813d0f7cf0SGrant Likely {
82c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
83c7e9d398SMasahiro Yamada 
84c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
85d49b48f0SVincent Whitchurch 				chip->of_xlate &&
86c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
877b8792bbSHans Holmberg }
883d0f7cf0SGrant Likely 
89c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
90c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
91762c2e46SMasahiro Yamada {
92c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
933d0f7cf0SGrant Likely }
943d0f7cf0SGrant Likely 
9599468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
9699468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
9799468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
9899468c1aSMasahiro Yamada {
9999468c1aSMasahiro Yamada 	int ret;
10099468c1aSMasahiro Yamada 
10199468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
10299468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
10399468c1aSMasahiro Yamada 
10499468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
10599468c1aSMasahiro Yamada 	if (ret < 0)
10699468c1aSMasahiro Yamada 		return ERR_PTR(ret);
10799468c1aSMasahiro Yamada 
10899468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
109f141ed65SGrant Likely }
110f141ed65SGrant Likely 
111f626d6dfSLinus Walleij /**
112f626d6dfSLinus Walleij  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
113f626d6dfSLinus Walleij  * to set the .valid_mask
11414e8c535SRandy Dunlap  * @gc: the target gpio_chip
11514e8c535SRandy Dunlap  *
11614e8c535SRandy Dunlap  * Return: true if the valid mask needs to be set
117f626d6dfSLinus Walleij  */
11849281a22SStephen Boyd bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
119f626d6dfSLinus Walleij {
120f626d6dfSLinus Walleij 	int size;
121f626d6dfSLinus Walleij 	struct device_node *np = gc->of_node;
122f626d6dfSLinus Walleij 
123f626d6dfSLinus Walleij 	size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
124f626d6dfSLinus Walleij 	if (size > 0 && size % 2 == 0)
125f626d6dfSLinus Walleij 		return true;
126f626d6dfSLinus Walleij 	return false;
127f626d6dfSLinus Walleij }
128f626d6dfSLinus Walleij 
129a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np,
13089a5e15bSLinus Walleij 				 const char *propname,
1316953c57aSLinus Walleij 				 enum of_gpio_flags *flags,
1326953c57aSLinus Walleij 				 int index)
133a603a2b8SLinus Walleij {
134a603a2b8SLinus Walleij 	/*
135a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
136a603a2b8SLinus Walleij 	 * Note that active low is the default.
137a603a2b8SLinus Walleij 	 */
138a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
139906402a4SLinus Walleij 	    (of_device_is_compatible(np, "regulator-fixed") ||
140906402a4SLinus Walleij 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
141a71a81e7SGeert Uytterhoeven 	     (!(strcmp(propname, "enable-gpio") &&
142a71a81e7SGeert Uytterhoeven 		strcmp(propname, "enable-gpios")) &&
143a71a81e7SGeert Uytterhoeven 	      of_device_is_compatible(np, "regulator-gpio")))) {
144228fc010SLucas Stach 		bool active_low = !of_property_read_bool(np,
145228fc010SLucas Stach 							 "enable-active-high");
146a603a2b8SLinus Walleij 		/*
147a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
148a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
149a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
150a603a2b8SLinus Walleij 		 * be actively ignored.
151a603a2b8SLinus Walleij 		 */
152228fc010SLucas Stach 		if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
153a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
154a603a2b8SLinus Walleij 				of_node_full_name(np));
155a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
156a603a2b8SLinus Walleij 		}
157228fc010SLucas Stach 		if (active_low)
158a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
159a603a2b8SLinus Walleij 	}
160a603a2b8SLinus Walleij 	/*
161a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
162a603a2b8SLinus Walleij 	 */
163a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
164a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
165a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
166a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
167a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
168a603a2b8SLinus Walleij 			of_node_full_name(np));
169a603a2b8SLinus Walleij 	}
1706953c57aSLinus Walleij 
1716953c57aSLinus Walleij 	/*
1726953c57aSLinus Walleij 	 * Legacy handling of SPI active high chip select. If we have a
1736953c57aSLinus Walleij 	 * property named "cs-gpios" we need to inspect the child node
1746953c57aSLinus Walleij 	 * to determine if the flags should have inverted semantics.
1756953c57aSLinus Walleij 	 */
176da7f1349SLinus Walleij 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
177a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
1786953c57aSLinus Walleij 		struct device_node *child;
1796953c57aSLinus Walleij 		u32 cs;
1806953c57aSLinus Walleij 		int ret;
1816953c57aSLinus Walleij 
1826953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
1836953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
184c1c04ceaSLinus Walleij 			if (ret)
1856953c57aSLinus Walleij 				continue;
1866953c57aSLinus Walleij 			if (cs == index) {
1876953c57aSLinus Walleij 				/*
1886953c57aSLinus Walleij 				 * SPI children have active low chip selects
1896953c57aSLinus Walleij 				 * by default. This can be specified negatively
1906953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
1916953c57aSLinus Walleij 				 * device node, or actively by tagging on
1926953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
1936953c57aSLinus Walleij 				 * tree. If the line is simultaneously
1946953c57aSLinus Walleij 				 * tagged as active low in the device tree
1956953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
1966953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
1976953c57aSLinus Walleij 				 * take precedence.
1986953c57aSLinus Walleij 				 */
1997ce40277SAndrey Smirnov 				if (of_property_read_bool(child, "spi-cs-high")) {
2006953c57aSLinus Walleij 					if (*flags & OF_GPIO_ACTIVE_LOW) {
2016953c57aSLinus Walleij 						pr_warn("%s GPIO handle specifies active low - ignored\n",
2027ce40277SAndrey Smirnov 							of_node_full_name(child));
2036953c57aSLinus Walleij 						*flags &= ~OF_GPIO_ACTIVE_LOW;
2046953c57aSLinus Walleij 					}
2056953c57aSLinus Walleij 				} else {
2066953c57aSLinus Walleij 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
2076953c57aSLinus Walleij 						pr_info("%s enforce active low on chipselect handle\n",
2087ce40277SAndrey Smirnov 							of_node_full_name(child));
2096953c57aSLinus Walleij 					*flags |= OF_GPIO_ACTIVE_LOW;
2106953c57aSLinus Walleij 				}
21189fea04cSNishka Dasgupta 				of_node_put(child);
2126953c57aSLinus Walleij 				break;
2136953c57aSLinus Walleij 			}
2146953c57aSLinus Walleij 		}
2156953c57aSLinus Walleij 	}
216edc1ef3fSMartin Blumenstingl 
217edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
218edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
219edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
220edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
221edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
222a603a2b8SLinus Walleij }
223a603a2b8SLinus Walleij 
224f141ed65SGrant Likely /**
225af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
226f141ed65SGrant Likely  * @np:		device node to get GPIO from
227f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
228f141ed65SGrant Likely  * @index:	index of the GPIO
229f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
230f141ed65SGrant Likely  *
231af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
232f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
233f141ed65SGrant Likely  * in flags for the GPIO.
234f141ed65SGrant Likely  */
235c83d3c77SGeert Uytterhoeven static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
236af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
237f141ed65SGrant Likely {
238762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
239762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
240762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
241f141ed65SGrant Likely 	int ret;
242f141ed65SGrant Likely 
243c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
244762c2e46SMasahiro Yamada 					     &gpiospec);
2453d0f7cf0SGrant Likely 	if (ret) {
2467eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
2477eb6ce2fSRob Herring 			__func__, propname, np, index);
248af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
2493d0f7cf0SGrant Likely 	}
250f141ed65SGrant Likely 
251c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
252762c2e46SMasahiro Yamada 	if (!chip) {
253762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
254762c2e46SMasahiro Yamada 		goto out;
255762c2e46SMasahiro Yamada 	}
2563d0f7cf0SGrant Likely 
25799468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
258762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
259762c2e46SMasahiro Yamada 		goto out;
260762c2e46SMasahiro Yamada 
261605f2d34SLinus Walleij 	if (flags)
26289a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
263a603a2b8SLinus Walleij 
2647eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
2657eb6ce2fSRob Herring 		 __func__, propname, np, index,
266762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
267762c2e46SMasahiro Yamada 
268762c2e46SMasahiro Yamada out:
269762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
270762c2e46SMasahiro Yamada 
271762c2e46SMasahiro Yamada 	return desc;
272f141ed65SGrant Likely }
273f141ed65SGrant Likely 
274f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
275f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
276f01d9075SAlexandre Courbot {
277f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
278f01d9075SAlexandre Courbot 
279f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
280f01d9075SAlexandre Courbot 
281f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
282f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
283f01d9075SAlexandre Courbot 	else
284f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
285f01d9075SAlexandre Courbot }
2866d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
287f01d9075SAlexandre Courbot 
288f626d6dfSLinus Walleij /**
289f626d6dfSLinus Walleij  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
290f626d6dfSLinus Walleij  * @node:	handle of the OF node
291f626d6dfSLinus Walleij  * @propname:	name of the DT property representing the GPIO
292f626d6dfSLinus Walleij  * @index:	index of the GPIO to obtain for the consumer
293f626d6dfSLinus Walleij  * @dflags:	GPIO initialization flags
294f626d6dfSLinus Walleij  * @label:	label to attach to the requested GPIO
295f626d6dfSLinus Walleij  *
296f626d6dfSLinus Walleij  * Returns:
297f626d6dfSLinus Walleij  * On successful request the GPIO pin is configured in accordance with
298f626d6dfSLinus Walleij  * provided @dflags.
299f626d6dfSLinus Walleij  *
300f626d6dfSLinus Walleij  * In case of error an ERR_PTR() is returned.
301f626d6dfSLinus Walleij  */
302f626d6dfSLinus Walleij struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
303f626d6dfSLinus Walleij 					 const char *propname, int index,
304f626d6dfSLinus Walleij 					 enum gpiod_flags dflags,
305f626d6dfSLinus Walleij 					 const char *label)
306f626d6dfSLinus Walleij {
307f626d6dfSLinus Walleij 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
308f626d6dfSLinus Walleij 	struct gpio_desc *desc;
309f626d6dfSLinus Walleij 	enum of_gpio_flags flags;
310f626d6dfSLinus Walleij 	bool active_low = false;
311f626d6dfSLinus Walleij 	bool single_ended = false;
312f626d6dfSLinus Walleij 	bool open_drain = false;
313f626d6dfSLinus Walleij 	bool transitory = false;
314f626d6dfSLinus Walleij 	int ret;
315f626d6dfSLinus Walleij 
316f626d6dfSLinus Walleij 	desc = of_get_named_gpiod_flags(node, propname,
317f626d6dfSLinus Walleij 					index, &flags);
318f626d6dfSLinus Walleij 
319f626d6dfSLinus Walleij 	if (!desc || IS_ERR(desc)) {
320f626d6dfSLinus Walleij 		return desc;
321f626d6dfSLinus Walleij 	}
322f626d6dfSLinus Walleij 
323f626d6dfSLinus Walleij 	active_low = flags & OF_GPIO_ACTIVE_LOW;
324f626d6dfSLinus Walleij 	single_ended = flags & OF_GPIO_SINGLE_ENDED;
325f626d6dfSLinus Walleij 	open_drain = flags & OF_GPIO_OPEN_DRAIN;
326f626d6dfSLinus Walleij 	transitory = flags & OF_GPIO_TRANSITORY;
327f626d6dfSLinus Walleij 
328f626d6dfSLinus Walleij 	ret = gpiod_request(desc, label);
329be7ae45cSMarco Felsch 	if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
330f626d6dfSLinus Walleij 		return desc;
331f626d6dfSLinus Walleij 	if (ret)
332f626d6dfSLinus Walleij 		return ERR_PTR(ret);
333f626d6dfSLinus Walleij 
334f626d6dfSLinus Walleij 	if (active_low)
335f626d6dfSLinus Walleij 		lflags |= GPIO_ACTIVE_LOW;
336f626d6dfSLinus Walleij 
337f626d6dfSLinus Walleij 	if (single_ended) {
338f626d6dfSLinus Walleij 		if (open_drain)
339f626d6dfSLinus Walleij 			lflags |= GPIO_OPEN_DRAIN;
340f626d6dfSLinus Walleij 		else
341f626d6dfSLinus Walleij 			lflags |= GPIO_OPEN_SOURCE;
342f626d6dfSLinus Walleij 	}
343f626d6dfSLinus Walleij 
344f626d6dfSLinus Walleij 	if (transitory)
345f626d6dfSLinus Walleij 		lflags |= GPIO_TRANSITORY;
346f626d6dfSLinus Walleij 
347*ea06a482SAdam Ford 	if (flags & OF_GPIO_PULL_UP)
348*ea06a482SAdam Ford 		lflags |= GPIO_PULL_UP;
349*ea06a482SAdam Ford 
350*ea06a482SAdam Ford 	if (flags & OF_GPIO_PULL_DOWN)
351*ea06a482SAdam Ford 		lflags |= GPIO_PULL_DOWN;
352*ea06a482SAdam Ford 
353f626d6dfSLinus Walleij 	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
354f626d6dfSLinus Walleij 	if (ret < 0) {
355f626d6dfSLinus Walleij 		gpiod_put(desc);
356f626d6dfSLinus Walleij 		return ERR_PTR(ret);
357f626d6dfSLinus Walleij 	}
358f626d6dfSLinus Walleij 
359f626d6dfSLinus Walleij 	return desc;
360f626d6dfSLinus Walleij }
3616d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
362f141ed65SGrant Likely 
363c8582339SLinus Walleij /*
364c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
365c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
366c8582339SLinus Walleij  * them.
367c8582339SLinus Walleij  */
368c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
369c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
370c8582339SLinus Walleij {
371c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
372c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
373c8582339SLinus Walleij 	struct gpio_desc *desc;
374c8582339SLinus Walleij 
375c8582339SLinus Walleij 	/*
376c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
377c8582339SLinus Walleij 	 * is false.
378c8582339SLinus Walleij 	 */
379c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
380c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
381c8582339SLinus Walleij 
382c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
383c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
384c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
385c8582339SLinus Walleij 
386c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
387c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
388c8582339SLinus Walleij 
389c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
390c8582339SLinus Walleij 	return desc;
391c8582339SLinus Walleij }
392c8582339SLinus Walleij 
3936a537d48SLinus Walleij /*
394e3023bf8SLinus Walleij  * The old Freescale bindings use simply "gpios" as name for the chip select
395e3023bf8SLinus Walleij  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
396e3023bf8SLinus Walleij  * with a special quirk.
397e3023bf8SLinus Walleij  */
398e3023bf8SLinus Walleij static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
399e3023bf8SLinus Walleij 					     const char *con_id,
400e3023bf8SLinus Walleij 					     unsigned int idx,
401e3023bf8SLinus Walleij 					     unsigned long *flags)
402e3023bf8SLinus Walleij {
403e3023bf8SLinus Walleij 	struct device_node *np = dev->of_node;
404e3023bf8SLinus Walleij 
405e3023bf8SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
406e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
407e3023bf8SLinus Walleij 
408e3023bf8SLinus Walleij 	/* Allow this specifically for Freescale devices */
409e3023bf8SLinus Walleij 	if (!of_device_is_compatible(np, "fsl,spi") &&
410e3023bf8SLinus Walleij 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
411e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
412e3023bf8SLinus Walleij 	/* Allow only if asking for "cs-gpios" */
413e3023bf8SLinus Walleij 	if (!con_id || strcmp(con_id, "cs"))
414e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
415e3023bf8SLinus Walleij 
416e3023bf8SLinus Walleij 	/*
417e3023bf8SLinus Walleij 	 * While all other SPI controllers use "cs-gpios" the Freescale
418e3023bf8SLinus Walleij 	 * uses just "gpios" so translate to that when "cs-gpios" is
419e3023bf8SLinus Walleij 	 * requested.
420e3023bf8SLinus Walleij 	 */
421e3023bf8SLinus Walleij 	return of_find_gpio(dev, NULL, idx, flags);
422e3023bf8SLinus Walleij }
423e3023bf8SLinus Walleij 
424e3023bf8SLinus Walleij /*
4256a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
4266a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
4276a537d48SLinus Walleij  * them.
4286a537d48SLinus Walleij  */
4296a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
4306a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
4316a537d48SLinus Walleij {
4326a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
4336a537d48SLinus Walleij 	const char *whitelist[] = {
4346a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
4356a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
4366a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
4376a537d48SLinus Walleij 	};
4386a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
4396a537d48SLinus Walleij 	struct gpio_desc *desc;
4406a537d48SLinus Walleij 	int i;
4416a537d48SLinus Walleij 
4426a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
4436a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4446a537d48SLinus Walleij 
4456a537d48SLinus Walleij 	if (!con_id)
4466a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4476a537d48SLinus Walleij 
4484b21f94aSAndy Shevchenko 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
4494b21f94aSAndy Shevchenko 	if (i < 0)
4506a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4516a537d48SLinus Walleij 
4526a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
4536a537d48SLinus Walleij 	return desc;
4546a537d48SLinus Walleij }
4556a537d48SLinus Walleij 
45611c43bb0SDmitry Torokhov static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
45711c43bb0SDmitry Torokhov 					      const char *con_id,
45811c43bb0SDmitry Torokhov 					      enum of_gpio_flags *of_flags)
45911c43bb0SDmitry Torokhov {
46011c43bb0SDmitry Torokhov 	if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
46111c43bb0SDmitry Torokhov 		return ERR_PTR(-ENOENT);
46211c43bb0SDmitry Torokhov 
46311c43bb0SDmitry Torokhov 	if (!con_id || strcmp(con_id, "wlf,reset"))
46411c43bb0SDmitry Torokhov 		return ERR_PTR(-ENOENT);
46511c43bb0SDmitry Torokhov 
46611c43bb0SDmitry Torokhov 	return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
46711c43bb0SDmitry Torokhov }
46811c43bb0SDmitry Torokhov 
469ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
470fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
471ea713bc4SLinus Walleij {
472ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
473ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
474ea713bc4SLinus Walleij 	struct gpio_desc *desc;
475ea713bc4SLinus Walleij 	unsigned int i;
476ea713bc4SLinus Walleij 
477c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
478ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
479ea713bc4SLinus Walleij 		if (con_id)
480ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
481ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
482ea713bc4SLinus Walleij 		else
483ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
484ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
485ea713bc4SLinus Walleij 
486ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
487ea713bc4SLinus Walleij 						&of_flags);
4886662ae6aSMaxime Ripard 
4891dea33e8SDmitry Torokhov 		if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
490ea713bc4SLinus Walleij 			break;
491ea713bc4SLinus Walleij 	}
492ea713bc4SLinus Walleij 
49345586c70SMasahiro Yamada 	if (PTR_ERR(desc) == -ENOENT) {
494c8582339SLinus Walleij 		/* Special handling for SPI GPIOs if used */
495c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
4961dea33e8SDmitry Torokhov 	}
4971dea33e8SDmitry Torokhov 
49845586c70SMasahiro Yamada 	if (PTR_ERR(desc) == -ENOENT) {
499e3023bf8SLinus Walleij 		/* This quirk looks up flags and all */
500e3023bf8SLinus Walleij 		desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
501e3023bf8SLinus Walleij 		if (!IS_ERR(desc))
502e3023bf8SLinus Walleij 			return desc;
503e3023bf8SLinus Walleij 	}
504c8582339SLinus Walleij 
50545586c70SMasahiro Yamada 	if (PTR_ERR(desc) == -ENOENT) {
5066a537d48SLinus Walleij 		/* Special handling for regulator GPIOs if used */
5076a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
5081dea33e8SDmitry Torokhov 	}
5096a537d48SLinus Walleij 
51045586c70SMasahiro Yamada 	if (PTR_ERR(desc) == -ENOENT)
51111c43bb0SDmitry Torokhov 		desc = of_find_arizona_gpio(dev, con_id, &of_flags);
51211c43bb0SDmitry Torokhov 
513ea713bc4SLinus Walleij 	if (IS_ERR(desc))
514ea713bc4SLinus Walleij 		return desc;
515ea713bc4SLinus Walleij 
516ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
517ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
518ea713bc4SLinus Walleij 
519ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
5204c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
521ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
522ea713bc4SLinus Walleij 		else
523ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
524ea713bc4SLinus Walleij 	}
525ea713bc4SLinus Walleij 
526e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
527e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
52805f479bfSCharles Keepax 
529d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_UP)
530d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_UP;
531d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_DOWN)
532d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_DOWN;
533d449991cSThomas Petazzoni 
534ea713bc4SLinus Walleij 	return desc;
535ea713bc4SLinus Walleij }
536ea713bc4SLinus Walleij 
537f141ed65SGrant Likely /**
538fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
539f625d460SBenoit Parrot  * @np:		device node to get GPIO from
540be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
541a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
542f625d460SBenoit Parrot  * @name:	GPIO line name
543fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
544fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
545f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
546f625d460SBenoit Parrot  *
547f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
548f625d460SBenoit Parrot  * value on the error condition.
549f625d460SBenoit Parrot  */
550fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
551be715343SMasahiro Yamada 					   struct gpio_chip *chip,
552a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
553fed7026aSAndy Shevchenko 					   unsigned long *lflags,
554f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
555f625d460SBenoit Parrot {
556f625d460SBenoit Parrot 	struct device_node *chip_np;
557f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
558be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
559be715343SMasahiro Yamada 	struct gpio_desc *desc;
560a79fead5SGeert Uytterhoeven 	unsigned int i;
561f625d460SBenoit Parrot 	u32 tmp;
5623f9547e1SMasahiro Yamada 	int ret;
563f625d460SBenoit Parrot 
564be715343SMasahiro Yamada 	chip_np = chip->of_node;
565f625d460SBenoit Parrot 	if (!chip_np)
566f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
567f625d460SBenoit Parrot 
568f625d460SBenoit Parrot 	xlate_flags = 0;
5692d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
570f625d460SBenoit Parrot 	*dflags = 0;
571f625d460SBenoit Parrot 
572f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
573f625d460SBenoit Parrot 	if (ret)
574f625d460SBenoit Parrot 		return ERR_PTR(ret);
575f625d460SBenoit Parrot 
576be715343SMasahiro Yamada 	gpiospec.np = chip_np;
577be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
578f625d460SBenoit Parrot 
579a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
580a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
581a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
582f625d460SBenoit Parrot 		if (ret)
583f625d460SBenoit Parrot 			return ERR_PTR(ret);
584a79fead5SGeert Uytterhoeven 	}
585f625d460SBenoit Parrot 
58699468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
587be715343SMasahiro Yamada 	if (IS_ERR(desc))
588be715343SMasahiro Yamada 		return desc;
589f625d460SBenoit Parrot 
590f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
591f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
592e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
593e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
594*ea06a482SAdam Ford 	if (xlate_flags & OF_GPIO_PULL_UP)
595*ea06a482SAdam Ford 		*lflags |= GPIO_PULL_UP;
596*ea06a482SAdam Ford 	if (xlate_flags & OF_GPIO_PULL_DOWN)
597*ea06a482SAdam Ford 		*lflags |= GPIO_PULL_DOWN;
598f625d460SBenoit Parrot 
599f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
600f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
601f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
602f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
603f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
604f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
605f625d460SBenoit Parrot 	else {
60662cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
60762cdcb6cSRob Herring 			desc_to_gpio(desc), np);
608f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
609f625d460SBenoit Parrot 	}
610f625d460SBenoit Parrot 
611f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
612f625d460SBenoit Parrot 		*name = np->name;
613f625d460SBenoit Parrot 
614be715343SMasahiro Yamada 	return desc;
615f625d460SBenoit Parrot }
616f625d460SBenoit Parrot 
617f625d460SBenoit Parrot /**
618bc21077eSGeert Uytterhoeven  * of_gpiochip_add_hog - Add all hogs in a hog device node
619bc21077eSGeert Uytterhoeven  * @chip:	gpio chip to act on
620bc21077eSGeert Uytterhoeven  * @hog:	device node describing the hogs
621bc21077eSGeert Uytterhoeven  *
622bc21077eSGeert Uytterhoeven  * Returns error if it fails otherwise 0 on success.
623bc21077eSGeert Uytterhoeven  */
624bc21077eSGeert Uytterhoeven static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
625bc21077eSGeert Uytterhoeven {
626bc21077eSGeert Uytterhoeven 	enum gpiod_flags dflags;
627bc21077eSGeert Uytterhoeven 	struct gpio_desc *desc;
628bc21077eSGeert Uytterhoeven 	unsigned long lflags;
629bc21077eSGeert Uytterhoeven 	const char *name;
630bc21077eSGeert Uytterhoeven 	unsigned int i;
631bc21077eSGeert Uytterhoeven 	int ret;
632bc21077eSGeert Uytterhoeven 
633bc21077eSGeert Uytterhoeven 	for (i = 0;; i++) {
634bc21077eSGeert Uytterhoeven 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
635bc21077eSGeert Uytterhoeven 		if (IS_ERR(desc))
636bc21077eSGeert Uytterhoeven 			break;
637bc21077eSGeert Uytterhoeven 
638bc21077eSGeert Uytterhoeven 		ret = gpiod_hog(desc, name, lflags, dflags);
639bc21077eSGeert Uytterhoeven 		if (ret < 0)
640bc21077eSGeert Uytterhoeven 			return ret;
64163636d95SGeert Uytterhoeven 
64263636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
64363636d95SGeert Uytterhoeven 		desc->hog = hog;
64463636d95SGeert Uytterhoeven #endif
645bc21077eSGeert Uytterhoeven 	}
646bc21077eSGeert Uytterhoeven 
647bc21077eSGeert Uytterhoeven 	return 0;
648bc21077eSGeert Uytterhoeven }
649bc21077eSGeert Uytterhoeven 
650bc21077eSGeert Uytterhoeven /**
651fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
652f625d460SBenoit Parrot  * @chip:	gpio chip to act on
653f625d460SBenoit Parrot  *
654f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
655f625d460SBenoit Parrot  * configuration.
656ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
657f625d460SBenoit Parrot  */
658dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
659f625d460SBenoit Parrot {
660f625d460SBenoit Parrot 	struct device_node *np;
661dfbd379bSLaxman Dewangan 	int ret;
662f625d460SBenoit Parrot 
663d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
664f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
665f625d460SBenoit Parrot 			continue;
666f625d460SBenoit Parrot 
667bc21077eSGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, np);
66809e258afSWei Yongjun 		if (ret < 0) {
66909e258afSWei Yongjun 			of_node_put(np);
670dfbd379bSLaxman Dewangan 			return ret;
671f625d460SBenoit Parrot 		}
67263636d95SGeert Uytterhoeven 
67363636d95SGeert Uytterhoeven 		of_node_set_flag(np, OF_POPULATED);
67409e258afSWei Yongjun 	}
675dfbd379bSLaxman Dewangan 
676dfbd379bSLaxman Dewangan 	return 0;
677f625d460SBenoit Parrot }
678f625d460SBenoit Parrot 
67963636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
68063636d95SGeert Uytterhoeven /**
68163636d95SGeert Uytterhoeven  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
68263636d95SGeert Uytterhoeven  * @chip:	gpio chip to act on
68363636d95SGeert Uytterhoeven  * @hog:	device node describing the hogs
68463636d95SGeert Uytterhoeven  */
68563636d95SGeert Uytterhoeven static void of_gpiochip_remove_hog(struct gpio_chip *chip,
68663636d95SGeert Uytterhoeven 				   struct device_node *hog)
68763636d95SGeert Uytterhoeven {
68863636d95SGeert Uytterhoeven 	struct gpio_desc *descs = chip->gpiodev->descs;
68963636d95SGeert Uytterhoeven 	unsigned int i;
69063636d95SGeert Uytterhoeven 
69163636d95SGeert Uytterhoeven 	for (i = 0; i < chip->ngpio; i++) {
69263636d95SGeert Uytterhoeven 		if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
69363636d95SGeert Uytterhoeven 		    descs[i].hog == hog)
69463636d95SGeert Uytterhoeven 			gpiochip_free_own_desc(&descs[i]);
69563636d95SGeert Uytterhoeven 	}
69663636d95SGeert Uytterhoeven }
69763636d95SGeert Uytterhoeven 
69863636d95SGeert Uytterhoeven static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
69963636d95SGeert Uytterhoeven {
70063636d95SGeert Uytterhoeven 	return chip->gpiodev->dev.of_node == data;
70163636d95SGeert Uytterhoeven }
70263636d95SGeert Uytterhoeven 
70363636d95SGeert Uytterhoeven static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
70463636d95SGeert Uytterhoeven {
70563636d95SGeert Uytterhoeven 	return gpiochip_find(np, of_gpiochip_match_node);
70663636d95SGeert Uytterhoeven }
70763636d95SGeert Uytterhoeven 
70863636d95SGeert Uytterhoeven static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
70963636d95SGeert Uytterhoeven 			  void *arg)
71063636d95SGeert Uytterhoeven {
71163636d95SGeert Uytterhoeven 	struct of_reconfig_data *rd = arg;
71263636d95SGeert Uytterhoeven 	struct gpio_chip *chip;
71363636d95SGeert Uytterhoeven 	int ret;
71463636d95SGeert Uytterhoeven 
71563636d95SGeert Uytterhoeven 	/*
71663636d95SGeert Uytterhoeven 	 * This only supports adding and removing complete gpio-hog nodes.
71763636d95SGeert Uytterhoeven 	 * Modifying an existing gpio-hog node is not supported (except for
71863636d95SGeert Uytterhoeven 	 * changing its "status" property, which is treated the same as
71963636d95SGeert Uytterhoeven 	 * addition/removal).
72063636d95SGeert Uytterhoeven 	 */
72163636d95SGeert Uytterhoeven 	switch (of_reconfig_get_state_change(action, arg)) {
72263636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_ADD:
72363636d95SGeert Uytterhoeven 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
72463636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
72563636d95SGeert Uytterhoeven 
72663636d95SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
72763636d95SGeert Uytterhoeven 			return NOTIFY_OK;
72863636d95SGeert Uytterhoeven 
72963636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
73063636d95SGeert Uytterhoeven 		if (chip == NULL)
73163636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
73263636d95SGeert Uytterhoeven 
73363636d95SGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, rd->dn);
73463636d95SGeert Uytterhoeven 		if (ret < 0) {
73563636d95SGeert Uytterhoeven 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
73663636d95SGeert Uytterhoeven 			       rd->dn);
73763636d95SGeert Uytterhoeven 			of_node_clear_flag(rd->dn, OF_POPULATED);
73863636d95SGeert Uytterhoeven 			return notifier_from_errno(ret);
73963636d95SGeert Uytterhoeven 		}
74063636d95SGeert Uytterhoeven 		break;
74163636d95SGeert Uytterhoeven 
74263636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_REMOVE:
74363636d95SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
74463636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* already depopulated */
74563636d95SGeert Uytterhoeven 
74663636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
74763636d95SGeert Uytterhoeven 		if (chip == NULL)
74863636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
74963636d95SGeert Uytterhoeven 
75063636d95SGeert Uytterhoeven 		of_gpiochip_remove_hog(chip, rd->dn);
75163636d95SGeert Uytterhoeven 		of_node_clear_flag(rd->dn, OF_POPULATED);
75263636d95SGeert Uytterhoeven 		break;
75363636d95SGeert Uytterhoeven 	}
75463636d95SGeert Uytterhoeven 
75563636d95SGeert Uytterhoeven 	return NOTIFY_OK;
75663636d95SGeert Uytterhoeven }
75763636d95SGeert Uytterhoeven 
75863636d95SGeert Uytterhoeven struct notifier_block gpio_of_notifier = {
75963636d95SGeert Uytterhoeven 	.notifier_call = of_gpio_notify,
76063636d95SGeert Uytterhoeven };
76163636d95SGeert Uytterhoeven #endif /* CONFIG_OF_DYNAMIC */
76263636d95SGeert Uytterhoeven 
763f625d460SBenoit Parrot /**
76467049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
765f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
76667049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
767f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
768f141ed65SGrant Likely  *
769f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
77067049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
771f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
772f141ed65SGrant Likely  */
773b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc,
774b0c7e73bSGeert Uytterhoeven 				const struct of_phandle_args *gpiospec,
775b0c7e73bSGeert Uytterhoeven 				u32 *flags)
776f141ed65SGrant Likely {
777f141ed65SGrant Likely 	/*
778f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
77920a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
780f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
781f141ed65SGrant Likely 	 * but not recommended).
782f141ed65SGrant Likely 	 */
783f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
784f141ed65SGrant Likely 		WARN_ON(1);
785f141ed65SGrant Likely 		return -EINVAL;
786f141ed65SGrant Likely 	}
787f141ed65SGrant Likely 
788f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
789f141ed65SGrant Likely 		return -EINVAL;
790f141ed65SGrant Likely 
7917b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
792f141ed65SGrant Likely 		return -EINVAL;
793f141ed65SGrant Likely 
794f141ed65SGrant Likely 	if (flags)
795f141ed65SGrant Likely 		*flags = gpiospec->args[1];
796f141ed65SGrant Likely 
797f141ed65SGrant Likely 	return gpiospec->args[0];
798f141ed65SGrant Likely }
799f141ed65SGrant Likely 
800f141ed65SGrant Likely /**
8013208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
802f141ed65SGrant Likely  * @np:		device node of the GPIO chip
803f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
8043208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
805f141ed65SGrant Likely  *
806f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
807f141ed65SGrant Likely  *
808f141ed65SGrant Likely  * 1) In the gpio_chip structure:
809f141ed65SGrant Likely  *    - all the callbacks
810f141ed65SGrant Likely  *    - of_gpio_n_cells
811f141ed65SGrant Likely  *    - of_xlate callback (optional)
812f141ed65SGrant Likely  *
813f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
814f141ed65SGrant Likely  *    - save_regs callback (optional)
815f141ed65SGrant Likely  *
816f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
817f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
818f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
819f141ed65SGrant Likely  */
8203208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
8213208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
8223208b0f0SLinus Walleij 			    void *data)
823f141ed65SGrant Likely {
824f141ed65SGrant Likely 	int ret = -ENOMEM;
825f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
826f141ed65SGrant Likely 
8277eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
828f141ed65SGrant Likely 	if (!gc->label)
829f141ed65SGrant Likely 		goto err0;
830f141ed65SGrant Likely 
831f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
832f141ed65SGrant Likely 	if (!mm_gc->regs)
833f141ed65SGrant Likely 		goto err1;
834f141ed65SGrant Likely 
835f141ed65SGrant Likely 	gc->base = -1;
836f141ed65SGrant Likely 
837f141ed65SGrant Likely 	if (mm_gc->save_regs)
838f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
839f141ed65SGrant Likely 
840f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
841f141ed65SGrant Likely 
8423208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
843f141ed65SGrant Likely 	if (ret)
844f141ed65SGrant Likely 		goto err2;
845f141ed65SGrant Likely 
846f141ed65SGrant Likely 	return 0;
847f141ed65SGrant Likely err2:
848f141ed65SGrant Likely 	iounmap(mm_gc->regs);
849f141ed65SGrant Likely err1:
850f141ed65SGrant Likely 	kfree(gc->label);
851f141ed65SGrant Likely err0:
8527eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
853f141ed65SGrant Likely 	return ret;
854f141ed65SGrant Likely }
8556d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
856f141ed65SGrant Likely 
857d621e8baSRicardo Ribalda Delgado /**
858d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
859d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
860d621e8baSRicardo Ribalda Delgado  */
861d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
862d621e8baSRicardo Ribalda Delgado {
863d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
864d621e8baSRicardo Ribalda Delgado 
865d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
866d621e8baSRicardo Ribalda Delgado 		return;
867d621e8baSRicardo Ribalda Delgado 
868d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
869d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
870d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
871d621e8baSRicardo Ribalda Delgado }
8726d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
873d621e8baSRicardo Ribalda Delgado 
874726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
875726cb3baSStephen Boyd {
876726cb3baSStephen Boyd 	int len, i;
877726cb3baSStephen Boyd 	u32 start, count;
878726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
879726cb3baSStephen Boyd 
880726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
881726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
882726cb3baSStephen Boyd 		return;
883726cb3baSStephen Boyd 
884726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
885726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
886726cb3baSStephen Boyd 					   i, &start);
887726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
888726cb3baSStephen Boyd 					   i + 1, &count);
889726cb3baSStephen Boyd 		if (start >= chip->ngpio || start + count >= chip->ngpio)
890726cb3baSStephen Boyd 			continue;
891726cb3baSStephen Boyd 
892726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
893726cb3baSStephen Boyd 	}
894726cb3baSStephen Boyd };
895726cb3baSStephen Boyd 
896f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
89728355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
898f23f1516SShiraz Hashim {
899f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
900f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
9011e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
902f23f1516SShiraz Hashim 	int index = 0, ret;
903586a87e6SChristian Ruppert 	const char *name;
904586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
905586a87e6SChristian Ruppert 	struct property *group_names;
906f23f1516SShiraz Hashim 
907f23f1516SShiraz Hashim 	if (!np)
90828355f81STomeu Vizoso 		return 0;
909f23f1516SShiraz Hashim 
910586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
911586a87e6SChristian Ruppert 
912ad4e1a7cSHaojian Zhuang 	for (;; index++) {
913d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
914d9fe0039SStephen Warren 				index, &pinspec);
915f23f1516SShiraz Hashim 		if (ret)
916f23f1516SShiraz Hashim 			break;
917f23f1516SShiraz Hashim 
9181e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
919602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
9201e63d7b9SLinus Walleij 		if (!pctldev)
92128355f81STomeu Vizoso 			return -EPROBE_DEFER;
922f23f1516SShiraz Hashim 
923586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
924586a87e6SChristian Ruppert 			if (group_names) {
92572858602SLaurent Navet 				of_property_read_string_index(np,
926586a87e6SChristian Ruppert 						group_names_propname,
927586a87e6SChristian Ruppert 						index, &name);
928586a87e6SChristian Ruppert 				if (strlen(name)) {
9297eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
9307eb6ce2fSRob Herring 						np);
931586a87e6SChristian Ruppert 					break;
932586a87e6SChristian Ruppert 				}
933586a87e6SChristian Ruppert 			}
934586a87e6SChristian Ruppert 			/* npins != 0: linear range */
9351e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
936ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
9371e63d7b9SLinus Walleij 					pinspec.args[0],
93886853c83SHaojian Zhuang 					pinspec.args[1],
93986853c83SHaojian Zhuang 					pinspec.args[2]);
9401e63d7b9SLinus Walleij 			if (ret)
94128355f81STomeu Vizoso 				return ret;
942586a87e6SChristian Ruppert 		} else {
943586a87e6SChristian Ruppert 			/* npins == 0: special range */
944586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
9457eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
9467eb6ce2fSRob Herring 					np);
947586a87e6SChristian Ruppert 				break;
948586a87e6SChristian Ruppert 			}
949586a87e6SChristian Ruppert 
950586a87e6SChristian Ruppert 			if (!group_names) {
9517eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
9527eb6ce2fSRob Herring 					np, group_names_propname);
953586a87e6SChristian Ruppert 				break;
954586a87e6SChristian Ruppert 			}
955586a87e6SChristian Ruppert 
956586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
957586a87e6SChristian Ruppert 						group_names_propname,
958586a87e6SChristian Ruppert 						index, &name);
959586a87e6SChristian Ruppert 			if (ret)
960586a87e6SChristian Ruppert 				break;
961586a87e6SChristian Ruppert 
962586a87e6SChristian Ruppert 			if (!strlen(name)) {
9637eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
9647eb6ce2fSRob Herring 				np);
965586a87e6SChristian Ruppert 				break;
966586a87e6SChristian Ruppert 			}
967586a87e6SChristian Ruppert 
968586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
969586a87e6SChristian Ruppert 						pinspec.args[0], name);
970586a87e6SChristian Ruppert 			if (ret)
97128355f81STomeu Vizoso 				return ret;
972586a87e6SChristian Ruppert 		}
973ad4e1a7cSHaojian Zhuang 	}
97428355f81STomeu Vizoso 
97528355f81STomeu Vizoso 	return 0;
976f23f1516SShiraz Hashim }
977f23f1516SShiraz Hashim 
978f23f1516SShiraz Hashim #else
97928355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
980f23f1516SShiraz Hashim #endif
981f23f1516SShiraz Hashim 
98228355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
983f141ed65SGrant Likely {
984f0d1ab05SLinus Walleij 	int ret;
98528355f81STomeu Vizoso 
986f141ed65SGrant Likely 	if (!chip->of_node)
98728355f81STomeu Vizoso 		return 0;
988f141ed65SGrant Likely 
989f141ed65SGrant Likely 	if (!chip->of_xlate) {
990f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
991f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
992f141ed65SGrant Likely 	}
993f141ed65SGrant Likely 
9941020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
9951020dfd1SMasahiro Yamada 		return -EINVAL;
9961020dfd1SMasahiro Yamada 
997726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
998726cb3baSStephen Boyd 
999f0d1ab05SLinus Walleij 	ret = of_gpiochip_add_pin_range(chip);
1000f0d1ab05SLinus Walleij 	if (ret)
1001f0d1ab05SLinus Walleij 		return ret;
100228355f81STomeu Vizoso 
1003fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
1004fd9c5531SLinus Walleij 	if (!chip->names)
100582270335SChristophe Leroy 		devprop_gpiochip_set_names(chip,
100682270335SChristophe Leroy 					   of_fwnode_handle(chip->of_node));
1007fd9c5531SLinus Walleij 
1008f141ed65SGrant Likely 	of_node_get(chip->of_node);
1009f625d460SBenoit Parrot 
1010f0d1ab05SLinus Walleij 	ret = of_gpiochip_scan_gpios(chip);
10112f4133bbSAndy Shevchenko 	if (ret)
1012f7299d44SGeert Uytterhoeven 		of_node_put(chip->of_node);
1013f7299d44SGeert Uytterhoeven 
1014f0d1ab05SLinus Walleij 	return ret;
1015f141ed65SGrant Likely }
1016f141ed65SGrant Likely 
1017f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
1018f141ed65SGrant Likely {
1019f141ed65SGrant Likely 	of_node_put(chip->of_node);
1020f141ed65SGrant Likely }
1021