xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision b311c5cb)
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
28c5a66b97SLee Jones  * @dev:    Consuming device
29c5a66b97SLee Jones  * @con_id: Function within the GPIO consumer
30c5a66b97SLee Jones  *
3171b8f600SLinus Walleij  * Some elder GPIO controllers need special quirks. Currently we handle
3247267732SLinus Walleij  * the Freescale and PPC GPIO controller with bindings that doesn't use the
3371b8f600SLinus Walleij  * established "cs-gpios" for chip selects but instead rely on
3471b8f600SLinus Walleij  * "gpios" for the chip select lines. If we detect this, we redirect
3571b8f600SLinus Walleij  * the counting of "cs-gpios" to count "gpios" transparent to the
3671b8f600SLinus Walleij  * driver.
3771b8f600SLinus Walleij  */
38a1f4c96bSYueHaibing static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
3971b8f600SLinus Walleij {
4071b8f600SLinus Walleij 	struct device_node *np = dev->of_node;
4171b8f600SLinus Walleij 
4271b8f600SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
4371b8f600SLinus Walleij 		return 0;
4471b8f600SLinus Walleij 	if (!con_id || strcmp(con_id, "cs"))
4571b8f600SLinus Walleij 		return 0;
4671b8f600SLinus Walleij 	if (!of_device_is_compatible(np, "fsl,spi") &&
4747267732SLinus Walleij 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
4847267732SLinus Walleij 	    !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
4971b8f600SLinus Walleij 		return 0;
5071b8f600SLinus Walleij 	return of_gpio_named_count(np, "gpios");
5171b8f600SLinus Walleij }
5271b8f600SLinus Walleij 
53f626d6dfSLinus Walleij /*
54f626d6dfSLinus Walleij  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
55f626d6dfSLinus Walleij  *
56f626d6dfSLinus Walleij  * FIXME: get rid of those external users by converting them to GPIO
57808b9931SGeert Uytterhoeven  * descriptors and let them all use gpiod_count()
58f626d6dfSLinus Walleij  */
59f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id)
60f626d6dfSLinus Walleij {
61f626d6dfSLinus Walleij 	int ret;
62f626d6dfSLinus Walleij 	char propname[32];
63f626d6dfSLinus Walleij 	unsigned int i;
64f626d6dfSLinus Walleij 
6571b8f600SLinus Walleij 	ret = of_gpio_spi_cs_get_count(dev, con_id);
6671b8f600SLinus Walleij 	if (ret > 0)
6771b8f600SLinus Walleij 		return ret;
6871b8f600SLinus Walleij 
69f626d6dfSLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
70f626d6dfSLinus Walleij 		if (con_id)
71f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s-%s",
72f626d6dfSLinus Walleij 				 con_id, gpio_suffixes[i]);
73f626d6dfSLinus Walleij 		else
74f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s",
75f626d6dfSLinus Walleij 				 gpio_suffixes[i]);
76f626d6dfSLinus Walleij 
77f626d6dfSLinus Walleij 		ret = of_gpio_named_count(dev->of_node, propname);
78f626d6dfSLinus Walleij 		if (ret > 0)
79f626d6dfSLinus Walleij 			break;
80f626d6dfSLinus Walleij 	}
81f626d6dfSLinus Walleij 	return ret ? ret : -ENOENT;
82f626d6dfSLinus Walleij }
83af8b6375SAlexandre Courbot 
84c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
853d0f7cf0SGrant Likely {
86c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
87c7e9d398SMasahiro Yamada 
88c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
89d49b48f0SVincent Whitchurch 				chip->of_xlate &&
90c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
917b8792bbSHans Holmberg }
923d0f7cf0SGrant Likely 
93c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
94c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
95762c2e46SMasahiro Yamada {
96c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
973d0f7cf0SGrant Likely }
983d0f7cf0SGrant Likely 
9999468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
10099468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
10199468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
10299468c1aSMasahiro Yamada {
10399468c1aSMasahiro Yamada 	int ret;
10499468c1aSMasahiro Yamada 
10599468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
10699468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
10799468c1aSMasahiro Yamada 
10899468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
10999468c1aSMasahiro Yamada 	if (ret < 0)
11099468c1aSMasahiro Yamada 		return ERR_PTR(ret);
11199468c1aSMasahiro Yamada 
11299468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
113f141ed65SGrant Likely }
114f141ed65SGrant Likely 
115f626d6dfSLinus Walleij /**
116f626d6dfSLinus Walleij  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
117f626d6dfSLinus Walleij  * to set the .valid_mask
11814e8c535SRandy Dunlap  * @gc: the target gpio_chip
11914e8c535SRandy Dunlap  *
12014e8c535SRandy Dunlap  * Return: true if the valid mask needs to be set
121f626d6dfSLinus Walleij  */
12249281a22SStephen Boyd bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
123f626d6dfSLinus Walleij {
124f626d6dfSLinus Walleij 	int size;
1258990899dSKrzysztof Kozlowski 	const struct device_node *np = gc->of_node;
126f626d6dfSLinus Walleij 
127f626d6dfSLinus Walleij 	size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
128f626d6dfSLinus Walleij 	if (size > 0 && size % 2 == 0)
129f626d6dfSLinus Walleij 		return true;
130f626d6dfSLinus Walleij 	return false;
131f626d6dfSLinus Walleij }
132f626d6dfSLinus Walleij 
133e6ae9a83SKrzysztof Kozlowski static void of_gpio_flags_quirks(const struct device_node *np,
13489a5e15bSLinus Walleij 				 const char *propname,
1356953c57aSLinus Walleij 				 enum of_gpio_flags *flags,
1366953c57aSLinus Walleij 				 int index)
137a603a2b8SLinus Walleij {
138a603a2b8SLinus Walleij 	/*
139a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
140a603a2b8SLinus Walleij 	 * Note that active low is the default.
141a603a2b8SLinus Walleij 	 */
142a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
143906402a4SLinus Walleij 	    (of_device_is_compatible(np, "regulator-fixed") ||
144906402a4SLinus Walleij 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
145a71a81e7SGeert Uytterhoeven 	     (!(strcmp(propname, "enable-gpio") &&
146a71a81e7SGeert Uytterhoeven 		strcmp(propname, "enable-gpios")) &&
147a71a81e7SGeert Uytterhoeven 	      of_device_is_compatible(np, "regulator-gpio")))) {
148228fc010SLucas Stach 		bool active_low = !of_property_read_bool(np,
149228fc010SLucas Stach 							 "enable-active-high");
150a603a2b8SLinus Walleij 		/*
151a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
152a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
153a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
154a603a2b8SLinus Walleij 		 * be actively ignored.
155a603a2b8SLinus Walleij 		 */
156228fc010SLucas Stach 		if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
157a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
158a603a2b8SLinus Walleij 				of_node_full_name(np));
159a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
160a603a2b8SLinus Walleij 		}
161228fc010SLucas Stach 		if (active_low)
162a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
163a603a2b8SLinus Walleij 	}
164a603a2b8SLinus Walleij 	/*
165a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
166a603a2b8SLinus Walleij 	 */
167a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
168a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
169a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
170a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
171a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
172a603a2b8SLinus Walleij 			of_node_full_name(np));
173a603a2b8SLinus Walleij 	}
1746953c57aSLinus Walleij 
1756953c57aSLinus Walleij 	/*
1766953c57aSLinus Walleij 	 * Legacy handling of SPI active high chip select. If we have a
1776953c57aSLinus Walleij 	 * property named "cs-gpios" we need to inspect the child node
1786953c57aSLinus Walleij 	 * to determine if the flags should have inverted semantics.
1796953c57aSLinus Walleij 	 */
180da7f1349SLinus Walleij 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
181a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
1826953c57aSLinus Walleij 		struct device_node *child;
1836953c57aSLinus Walleij 		u32 cs;
1846953c57aSLinus Walleij 		int ret;
1856953c57aSLinus Walleij 
1866953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
1876953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
188c1c04ceaSLinus Walleij 			if (ret)
1896953c57aSLinus Walleij 				continue;
1906953c57aSLinus Walleij 			if (cs == index) {
1916953c57aSLinus Walleij 				/*
1926953c57aSLinus Walleij 				 * SPI children have active low chip selects
1936953c57aSLinus Walleij 				 * by default. This can be specified negatively
1946953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
1956953c57aSLinus Walleij 				 * device node, or actively by tagging on
1966953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
1976953c57aSLinus Walleij 				 * tree. If the line is simultaneously
1986953c57aSLinus Walleij 				 * tagged as active low in the device tree
1996953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
2006953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
2016953c57aSLinus Walleij 				 * take precedence.
2026953c57aSLinus Walleij 				 */
2037ce40277SAndrey Smirnov 				if (of_property_read_bool(child, "spi-cs-high")) {
2046953c57aSLinus Walleij 					if (*flags & OF_GPIO_ACTIVE_LOW) {
2056953c57aSLinus Walleij 						pr_warn("%s GPIO handle specifies active low - ignored\n",
2067ce40277SAndrey Smirnov 							of_node_full_name(child));
2076953c57aSLinus Walleij 						*flags &= ~OF_GPIO_ACTIVE_LOW;
2086953c57aSLinus Walleij 					}
2096953c57aSLinus Walleij 				} else {
2106953c57aSLinus Walleij 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
2116953c57aSLinus Walleij 						pr_info("%s enforce active low on chipselect handle\n",
2127ce40277SAndrey Smirnov 							of_node_full_name(child));
2136953c57aSLinus Walleij 					*flags |= OF_GPIO_ACTIVE_LOW;
2146953c57aSLinus Walleij 				}
21589fea04cSNishka Dasgupta 				of_node_put(child);
2166953c57aSLinus Walleij 				break;
2176953c57aSLinus Walleij 			}
2186953c57aSLinus Walleij 		}
2196953c57aSLinus Walleij 	}
220edc1ef3fSMartin Blumenstingl 
221edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
222edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
223edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
224edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
225edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
226a603a2b8SLinus Walleij }
227a603a2b8SLinus Walleij 
228f141ed65SGrant Likely /**
229af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
230f141ed65SGrant Likely  * @np:		device node to get GPIO from
231f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
232f141ed65SGrant Likely  * @index:	index of the GPIO
233f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
234f141ed65SGrant Likely  *
235af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
236f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
237f141ed65SGrant Likely  * in flags for the GPIO.
238f141ed65SGrant Likely  */
239e6ae9a83SKrzysztof Kozlowski static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
240af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
241f141ed65SGrant Likely {
242762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
243762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
244762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
245f141ed65SGrant Likely 	int ret;
246f141ed65SGrant Likely 
247c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
248762c2e46SMasahiro Yamada 					     &gpiospec);
2493d0f7cf0SGrant Likely 	if (ret) {
2507eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
2517eb6ce2fSRob Herring 			__func__, propname, np, index);
252af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
2533d0f7cf0SGrant Likely 	}
254f141ed65SGrant Likely 
255c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
256762c2e46SMasahiro Yamada 	if (!chip) {
257762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
258762c2e46SMasahiro Yamada 		goto out;
259762c2e46SMasahiro Yamada 	}
2603d0f7cf0SGrant Likely 
26199468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
262762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
263762c2e46SMasahiro Yamada 		goto out;
264762c2e46SMasahiro Yamada 
265605f2d34SLinus Walleij 	if (flags)
26689a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
267a603a2b8SLinus Walleij 
2687eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
2697eb6ce2fSRob Herring 		 __func__, propname, np, index,
270762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
271762c2e46SMasahiro Yamada 
272762c2e46SMasahiro Yamada out:
273762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
274762c2e46SMasahiro Yamada 
275762c2e46SMasahiro Yamada 	return desc;
276f141ed65SGrant Likely }
277f141ed65SGrant Likely 
278e6ae9a83SKrzysztof Kozlowski int of_get_named_gpio_flags(const struct device_node *np, const char *list_name,
279f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
280f01d9075SAlexandre Courbot {
281f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
282f01d9075SAlexandre Courbot 
283f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
284f01d9075SAlexandre Courbot 
285f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
286f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
287f01d9075SAlexandre Courbot 	else
288f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
289f01d9075SAlexandre Courbot }
2906d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
291f01d9075SAlexandre Courbot 
292d9e7f0e3SDmitry Torokhov /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
293d9e7f0e3SDmitry Torokhov static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
294d9e7f0e3SDmitry Torokhov {
295d9e7f0e3SDmitry Torokhov 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
296d9e7f0e3SDmitry Torokhov 
297d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_ACTIVE_LOW)
298d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_ACTIVE_LOW;
299d9e7f0e3SDmitry Torokhov 
300d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_SINGLE_ENDED) {
301d9e7f0e3SDmitry Torokhov 		if (flags & OF_GPIO_OPEN_DRAIN)
302d9e7f0e3SDmitry Torokhov 			lflags |= GPIO_OPEN_DRAIN;
303d9e7f0e3SDmitry Torokhov 		else
304d9e7f0e3SDmitry Torokhov 			lflags |= GPIO_OPEN_SOURCE;
305d9e7f0e3SDmitry Torokhov 	}
306d9e7f0e3SDmitry Torokhov 
307d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_TRANSITORY)
308d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_TRANSITORY;
309d9e7f0e3SDmitry Torokhov 
310d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_UP)
311d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_UP;
312d9e7f0e3SDmitry Torokhov 
313d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_DOWN)
314d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_DOWN;
315d9e7f0e3SDmitry Torokhov 
316d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_DISABLE)
317d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_DISABLE;
318d9e7f0e3SDmitry Torokhov 
319d9e7f0e3SDmitry Torokhov 	return lflags;
320d9e7f0e3SDmitry Torokhov }
321d9e7f0e3SDmitry Torokhov 
322f626d6dfSLinus Walleij /**
323f626d6dfSLinus Walleij  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
324f626d6dfSLinus Walleij  * @node:	handle of the OF node
325f626d6dfSLinus Walleij  * @propname:	name of the DT property representing the GPIO
326f626d6dfSLinus Walleij  * @index:	index of the GPIO to obtain for the consumer
327f626d6dfSLinus Walleij  * @dflags:	GPIO initialization flags
328f626d6dfSLinus Walleij  * @label:	label to attach to the requested GPIO
329f626d6dfSLinus Walleij  *
330f626d6dfSLinus Walleij  * Returns:
331f626d6dfSLinus Walleij  * On successful request the GPIO pin is configured in accordance with
332f626d6dfSLinus Walleij  * provided @dflags.
333f626d6dfSLinus Walleij  *
334f626d6dfSLinus Walleij  * In case of error an ERR_PTR() is returned.
335f626d6dfSLinus Walleij  */
336e6ae9a83SKrzysztof Kozlowski struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
337f626d6dfSLinus Walleij 					 const char *propname, int index,
338f626d6dfSLinus Walleij 					 enum gpiod_flags dflags,
339f626d6dfSLinus Walleij 					 const char *label)
340f626d6dfSLinus Walleij {
341d9e7f0e3SDmitry Torokhov 	unsigned long lflags;
342f626d6dfSLinus Walleij 	struct gpio_desc *desc;
343d9e7f0e3SDmitry Torokhov 	enum of_gpio_flags of_flags;
344f626d6dfSLinus Walleij 	int ret;
345f626d6dfSLinus Walleij 
346d9e7f0e3SDmitry Torokhov 	desc = of_get_named_gpiod_flags(node, propname, index, &of_flags);
347d9e7f0e3SDmitry Torokhov 	if (!desc || IS_ERR(desc))
348f626d6dfSLinus Walleij 		return desc;
349f626d6dfSLinus Walleij 
350f626d6dfSLinus Walleij 	ret = gpiod_request(desc, label);
351be7ae45cSMarco Felsch 	if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
352f626d6dfSLinus Walleij 		return desc;
353f626d6dfSLinus Walleij 	if (ret)
354f626d6dfSLinus Walleij 		return ERR_PTR(ret);
355f626d6dfSLinus Walleij 
356d9e7f0e3SDmitry Torokhov 	lflags = of_convert_gpio_flags(of_flags);
35731bea231SNuno Sá 
358f626d6dfSLinus Walleij 	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
359f626d6dfSLinus Walleij 	if (ret < 0) {
360f626d6dfSLinus Walleij 		gpiod_put(desc);
361f626d6dfSLinus Walleij 		return ERR_PTR(ret);
362f626d6dfSLinus Walleij 	}
363f626d6dfSLinus Walleij 
364f626d6dfSLinus Walleij 	return desc;
365f626d6dfSLinus Walleij }
3666d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
367f141ed65SGrant Likely 
368*b311c5cbSDmitry Torokhov static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
36998c3c940SDmitry Torokhov 					     const char *con_id,
37098c3c940SDmitry Torokhov 					     unsigned int idx,
371c8582339SLinus Walleij 					     enum of_gpio_flags *of_flags)
372c8582339SLinus Walleij {
373*b311c5cbSDmitry Torokhov 	static const struct of_rename_gpio {
374*b311c5cbSDmitry Torokhov 		const char *con_id;
375*b311c5cbSDmitry Torokhov 		const char *legacy_id;	/* NULL - same as con_id */
376*b311c5cbSDmitry Torokhov 		/*
377*b311c5cbSDmitry Torokhov 		 * Compatible string can be set to NULL in case where
378*b311c5cbSDmitry Torokhov 		 * matching to a particular compatible is not practical,
379*b311c5cbSDmitry Torokhov 		 * but it should only be done for gpio names that have
380*b311c5cbSDmitry Torokhov 		 * vendor prefix to reduce risk of false positives.
381*b311c5cbSDmitry Torokhov 		 * Addition of such entries is strongly discouraged.
382*b311c5cbSDmitry Torokhov 		 */
383*b311c5cbSDmitry Torokhov 		const char *compatible;
384*b311c5cbSDmitry Torokhov 	} gpios[] = {
385*b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_MFD_ARIZONA)
386*b311c5cbSDmitry Torokhov 		{ "wlf,reset",	NULL,		NULL },
387*b311c5cbSDmitry Torokhov #endif
388*b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR)
389*b311c5cbSDmitry Torokhov 		/*
390*b311c5cbSDmitry Torokhov 		 * Some regulator bindings happened before we managed to
391*b311c5cbSDmitry Torokhov 		 * establish that GPIO properties should be named
392*b311c5cbSDmitry Torokhov 		 * "foo-gpios" so we have this special kludge for them.
393*b311c5cbSDmitry Torokhov 		 */
394*b311c5cbSDmitry Torokhov 		{ "wlf,ldoena",  NULL,		NULL }, /* Arizona */
395*b311c5cbSDmitry Torokhov 		{ "wlf,ldo1ena", NULL,		NULL }, /* WM8994 */
396*b311c5cbSDmitry Torokhov 		{ "wlf,ldo2ena", NULL,		NULL }, /* WM8994 */
397*b311c5cbSDmitry Torokhov #endif
398*b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_MASTER)
399c8582339SLinus Walleij 
400c8582339SLinus Walleij 		/*
401*b311c5cbSDmitry Torokhov 		 * The SPI GPIO bindings happened before we managed to
402*b311c5cbSDmitry Torokhov 		 * establish that GPIO properties should be named
403*b311c5cbSDmitry Torokhov 		 * "foo-gpios" so we have this special kludge for them.
404c8582339SLinus Walleij 		 */
405*b311c5cbSDmitry Torokhov 		{ "miso",	"gpio-miso",	"spi-gpio" },
406*b311c5cbSDmitry Torokhov 		{ "mosi",	"gpio-mosi",	"spi-gpio" },
407*b311c5cbSDmitry Torokhov 		{ "sck",	"gpio-sck",	"spi-gpio" },
408c8582339SLinus Walleij 
4096a537d48SLinus Walleij 		/*
410*b311c5cbSDmitry Torokhov 		 * The old Freescale bindings use simply "gpios" as name
411*b311c5cbSDmitry Torokhov 		 * for the chip select lines rather than "cs-gpios" like
412*b311c5cbSDmitry Torokhov 		 * all other SPI hardware. Allow this specifically for
413*b311c5cbSDmitry Torokhov 		 * Freescale and PPC devices.
414e3023bf8SLinus Walleij 		 */
415*b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"fsl,spi" },
416*b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"aeroflexgaisler,spictrl" },
417*b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"ibm,ppc4xx-spi" },
418*b311c5cbSDmitry Torokhov #endif
419*b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
420e3023bf8SLinus Walleij 		/*
421*b311c5cbSDmitry Torokhov 		 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
422*b311c5cbSDmitry Torokhov 		 * property without the compulsory "-gpios" suffix.
423e3023bf8SLinus Walleij 		 */
424*b311c5cbSDmitry Torokhov 		{ "fcs,int_n",	NULL,		"fcs,fusb302" },
425*b311c5cbSDmitry Torokhov #endif
4266a537d48SLinus Walleij 	};
427*b311c5cbSDmitry Torokhov 	struct gpio_desc *desc;
428*b311c5cbSDmitry Torokhov 	const char *legacy_id;
429*b311c5cbSDmitry Torokhov 	unsigned int i;
4306a537d48SLinus Walleij 
4316a537d48SLinus Walleij 	if (!con_id)
4326a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4336a537d48SLinus Walleij 
434*b311c5cbSDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
435*b311c5cbSDmitry Torokhov 		if (strcmp(con_id, gpios[i].con_id))
436*b311c5cbSDmitry Torokhov 			continue;
4376a537d48SLinus Walleij 
438*b311c5cbSDmitry Torokhov 		if (gpios[i].compatible &&
439*b311c5cbSDmitry Torokhov 		    !of_device_is_compatible(np, gpios[i].compatible))
440*b311c5cbSDmitry Torokhov 			continue;
441*b311c5cbSDmitry Torokhov 
442*b311c5cbSDmitry Torokhov 		legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
443*b311c5cbSDmitry Torokhov 		desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
444*b311c5cbSDmitry Torokhov 		if (!gpiod_not_found(desc)) {
445*b311c5cbSDmitry Torokhov 			pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
446*b311c5cbSDmitry Torokhov 				of_node_full_name(np), legacy_id, con_id);
447*b311c5cbSDmitry Torokhov 			return desc;
448*b311c5cbSDmitry Torokhov 		}
4496a537d48SLinus Walleij 	}
4506a537d48SLinus Walleij 
45111c43bb0SDmitry Torokhov 	return ERR_PTR(-ENOENT);
4526e24826dSLinus Walleij }
4536e24826dSLinus Walleij 
454326c3753SDmitry Torokhov static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
455326c3753SDmitry Torokhov 					     const char *con_id,
456326c3753SDmitry Torokhov 					     unsigned int idx,
457326c3753SDmitry Torokhov 					     enum of_gpio_flags *of_flags)
458326c3753SDmitry Torokhov {
459326c3753SDmitry Torokhov 	struct gpio_desc *desc;
460326c3753SDmitry Torokhov 	const char *legacy_id;
461326c3753SDmitry Torokhov 
462326c3753SDmitry Torokhov 	if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
463326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
464326c3753SDmitry Torokhov 
465326c3753SDmitry Torokhov 	if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
466326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
467326c3753SDmitry Torokhov 
468326c3753SDmitry Torokhov 	if (!con_id || strcmp(con_id, "i2s1-in-sel"))
469326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
470326c3753SDmitry Torokhov 
471326c3753SDmitry Torokhov 	if (idx == 0)
472326c3753SDmitry Torokhov 		legacy_id = "i2s1-in-sel-gpio1";
473326c3753SDmitry Torokhov 	else if (idx == 1)
474326c3753SDmitry Torokhov 		legacy_id = "i2s1-in-sel-gpio2";
475326c3753SDmitry Torokhov 	else
476326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
477326c3753SDmitry Torokhov 
478326c3753SDmitry Torokhov 	desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
479326c3753SDmitry Torokhov 	if (!gpiod_not_found(desc))
480326c3753SDmitry Torokhov 		pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
481326c3753SDmitry Torokhov 			of_node_full_name(np), legacy_id, con_id);
482326c3753SDmitry Torokhov 
483326c3753SDmitry Torokhov 	return desc;
484326c3753SDmitry Torokhov }
485326c3753SDmitry Torokhov 
486a2b5e207SDmitry Torokhov typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
487a2b5e207SDmitry Torokhov 						const char *con_id,
488a2b5e207SDmitry Torokhov 						unsigned int idx,
489a2b5e207SDmitry Torokhov 						enum of_gpio_flags *of_flags);
490a2b5e207SDmitry Torokhov static const of_find_gpio_quirk of_find_gpio_quirks[] = {
491*b311c5cbSDmitry Torokhov 	of_find_gpio_rename,
492326c3753SDmitry Torokhov 	of_find_mt2701_gpio,
4938b10ca2fSMichael Walle 	NULL
494a2b5e207SDmitry Torokhov };
495a2b5e207SDmitry Torokhov 
496ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
497fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
498ea713bc4SLinus Walleij {
499ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
500ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
501a2b5e207SDmitry Torokhov 	const of_find_gpio_quirk *q;
502ea713bc4SLinus Walleij 	struct gpio_desc *desc;
503ea713bc4SLinus Walleij 	unsigned int i;
504ea713bc4SLinus Walleij 
505c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
506ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
507ea713bc4SLinus Walleij 		if (con_id)
508ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
509ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
510ea713bc4SLinus Walleij 		else
511ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
512ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
513ea713bc4SLinus Walleij 
514ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
515ea713bc4SLinus Walleij 						&of_flags);
5166662ae6aSMaxime Ripard 
5177b58696dSAndy Shevchenko 		if (!gpiod_not_found(desc))
518ea713bc4SLinus Walleij 			break;
519ea713bc4SLinus Walleij 	}
520ea713bc4SLinus Walleij 
521a2b5e207SDmitry Torokhov 	/* Properly named GPIO was not found, try workarounds */
522a2b5e207SDmitry Torokhov 	for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
523a2b5e207SDmitry Torokhov 		desc = (*q)(dev->of_node, con_id, idx, &of_flags);
5246e24826dSLinus Walleij 
525ea713bc4SLinus Walleij 	if (IS_ERR(desc))
526ea713bc4SLinus Walleij 		return desc;
527ea713bc4SLinus Walleij 
528d9e7f0e3SDmitry Torokhov 	*flags = of_convert_gpio_flags(of_flags);
529d449991cSThomas Petazzoni 
530ea713bc4SLinus Walleij 	return desc;
531ea713bc4SLinus Walleij }
532ea713bc4SLinus Walleij 
533f141ed65SGrant Likely /**
534fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
535f625d460SBenoit Parrot  * @np:		device node to get GPIO from
536be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
537a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
538f625d460SBenoit Parrot  * @name:	GPIO line name
539fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
540fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
541f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
542f625d460SBenoit Parrot  *
543f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
544f625d460SBenoit Parrot  * value on the error condition.
545f625d460SBenoit Parrot  */
546fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
547be715343SMasahiro Yamada 					   struct gpio_chip *chip,
548a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
549fed7026aSAndy Shevchenko 					   unsigned long *lflags,
550f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
551f625d460SBenoit Parrot {
552f625d460SBenoit Parrot 	struct device_node *chip_np;
553f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
554be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
555be715343SMasahiro Yamada 	struct gpio_desc *desc;
556a79fead5SGeert Uytterhoeven 	unsigned int i;
557f625d460SBenoit Parrot 	u32 tmp;
5583f9547e1SMasahiro Yamada 	int ret;
559f625d460SBenoit Parrot 
560be715343SMasahiro Yamada 	chip_np = chip->of_node;
561f625d460SBenoit Parrot 	if (!chip_np)
562f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
563f625d460SBenoit Parrot 
564f625d460SBenoit Parrot 	xlate_flags = 0;
5652d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
56640941954SAndy Shevchenko 	*dflags = GPIOD_ASIS;
567f625d460SBenoit Parrot 
568f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
569f625d460SBenoit Parrot 	if (ret)
570f625d460SBenoit Parrot 		return ERR_PTR(ret);
571f625d460SBenoit Parrot 
572be715343SMasahiro Yamada 	gpiospec.np = chip_np;
573be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
574f625d460SBenoit Parrot 
575a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
576a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
577a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
578f625d460SBenoit Parrot 		if (ret)
579f625d460SBenoit Parrot 			return ERR_PTR(ret);
580a79fead5SGeert Uytterhoeven 	}
581f625d460SBenoit Parrot 
58299468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
583be715343SMasahiro Yamada 	if (IS_ERR(desc))
584be715343SMasahiro Yamada 		return desc;
585f625d460SBenoit Parrot 
586d9e7f0e3SDmitry Torokhov 	*lflags = of_convert_gpio_flags(xlate_flags);
587f625d460SBenoit Parrot 
588f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
589f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
590f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
591f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
592f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
593f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
594f625d460SBenoit Parrot 	else {
59562cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
59662cdcb6cSRob Herring 			desc_to_gpio(desc), np);
597f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
598f625d460SBenoit Parrot 	}
599f625d460SBenoit Parrot 
600f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
601f625d460SBenoit Parrot 		*name = np->name;
602f625d460SBenoit Parrot 
603be715343SMasahiro Yamada 	return desc;
604f625d460SBenoit Parrot }
605f625d460SBenoit Parrot 
606f625d460SBenoit Parrot /**
607bc21077eSGeert Uytterhoeven  * of_gpiochip_add_hog - Add all hogs in a hog device node
608bc21077eSGeert Uytterhoeven  * @chip:	gpio chip to act on
609bc21077eSGeert Uytterhoeven  * @hog:	device node describing the hogs
610bc21077eSGeert Uytterhoeven  *
611bc21077eSGeert Uytterhoeven  * Returns error if it fails otherwise 0 on success.
612bc21077eSGeert Uytterhoeven  */
613bc21077eSGeert Uytterhoeven static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
614bc21077eSGeert Uytterhoeven {
615bc21077eSGeert Uytterhoeven 	enum gpiod_flags dflags;
616bc21077eSGeert Uytterhoeven 	struct gpio_desc *desc;
617bc21077eSGeert Uytterhoeven 	unsigned long lflags;
618bc21077eSGeert Uytterhoeven 	const char *name;
619bc21077eSGeert Uytterhoeven 	unsigned int i;
620bc21077eSGeert Uytterhoeven 	int ret;
621bc21077eSGeert Uytterhoeven 
622bc21077eSGeert Uytterhoeven 	for (i = 0;; i++) {
623bc21077eSGeert Uytterhoeven 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
624bc21077eSGeert Uytterhoeven 		if (IS_ERR(desc))
625bc21077eSGeert Uytterhoeven 			break;
626bc21077eSGeert Uytterhoeven 
627bc21077eSGeert Uytterhoeven 		ret = gpiod_hog(desc, name, lflags, dflags);
628bc21077eSGeert Uytterhoeven 		if (ret < 0)
629bc21077eSGeert Uytterhoeven 			return ret;
63063636d95SGeert Uytterhoeven 
63163636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
63263636d95SGeert Uytterhoeven 		desc->hog = hog;
63363636d95SGeert Uytterhoeven #endif
634bc21077eSGeert Uytterhoeven 	}
635bc21077eSGeert Uytterhoeven 
636bc21077eSGeert Uytterhoeven 	return 0;
637bc21077eSGeert Uytterhoeven }
638bc21077eSGeert Uytterhoeven 
639bc21077eSGeert Uytterhoeven /**
640fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
641f625d460SBenoit Parrot  * @chip:	gpio chip to act on
642f625d460SBenoit Parrot  *
643f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
644f625d460SBenoit Parrot  * configuration.
645ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
646f625d460SBenoit Parrot  */
647dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
648f625d460SBenoit Parrot {
649f625d460SBenoit Parrot 	struct device_node *np;
650dfbd379bSLaxman Dewangan 	int ret;
651f625d460SBenoit Parrot 
652d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
653f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
654f625d460SBenoit Parrot 			continue;
655f625d460SBenoit Parrot 
656bc21077eSGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, np);
65709e258afSWei Yongjun 		if (ret < 0) {
65809e258afSWei Yongjun 			of_node_put(np);
659dfbd379bSLaxman Dewangan 			return ret;
660f625d460SBenoit Parrot 		}
66163636d95SGeert Uytterhoeven 
66263636d95SGeert Uytterhoeven 		of_node_set_flag(np, OF_POPULATED);
66309e258afSWei Yongjun 	}
664dfbd379bSLaxman Dewangan 
665dfbd379bSLaxman Dewangan 	return 0;
666f625d460SBenoit Parrot }
667f625d460SBenoit Parrot 
66863636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
66963636d95SGeert Uytterhoeven /**
67063636d95SGeert Uytterhoeven  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
67163636d95SGeert Uytterhoeven  * @chip:	gpio chip to act on
67263636d95SGeert Uytterhoeven  * @hog:	device node describing the hogs
67363636d95SGeert Uytterhoeven  */
67463636d95SGeert Uytterhoeven static void of_gpiochip_remove_hog(struct gpio_chip *chip,
67563636d95SGeert Uytterhoeven 				   struct device_node *hog)
67663636d95SGeert Uytterhoeven {
67780c78fbeSAndy Shevchenko 	struct gpio_desc *desc;
67863636d95SGeert Uytterhoeven 
67957017eddSAndy Shevchenko 	for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
68080c78fbeSAndy Shevchenko 		if (desc->hog == hog)
68180c78fbeSAndy Shevchenko 			gpiochip_free_own_desc(desc);
68263636d95SGeert Uytterhoeven }
68363636d95SGeert Uytterhoeven 
68463636d95SGeert Uytterhoeven static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
68563636d95SGeert Uytterhoeven {
686597a8a88SAndy Shevchenko 	return device_match_of_node(&chip->gpiodev->dev, data);
68763636d95SGeert Uytterhoeven }
68863636d95SGeert Uytterhoeven 
68963636d95SGeert Uytterhoeven static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
69063636d95SGeert Uytterhoeven {
69163636d95SGeert Uytterhoeven 	return gpiochip_find(np, of_gpiochip_match_node);
69263636d95SGeert Uytterhoeven }
69363636d95SGeert Uytterhoeven 
69463636d95SGeert Uytterhoeven static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
69563636d95SGeert Uytterhoeven 			  void *arg)
69663636d95SGeert Uytterhoeven {
69763636d95SGeert Uytterhoeven 	struct of_reconfig_data *rd = arg;
69863636d95SGeert Uytterhoeven 	struct gpio_chip *chip;
69963636d95SGeert Uytterhoeven 	int ret;
70063636d95SGeert Uytterhoeven 
70163636d95SGeert Uytterhoeven 	/*
70263636d95SGeert Uytterhoeven 	 * This only supports adding and removing complete gpio-hog nodes.
70363636d95SGeert Uytterhoeven 	 * Modifying an existing gpio-hog node is not supported (except for
70463636d95SGeert Uytterhoeven 	 * changing its "status" property, which is treated the same as
70563636d95SGeert Uytterhoeven 	 * addition/removal).
70663636d95SGeert Uytterhoeven 	 */
70763636d95SGeert Uytterhoeven 	switch (of_reconfig_get_state_change(action, arg)) {
70863636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_ADD:
70963636d95SGeert Uytterhoeven 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
71063636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
71163636d95SGeert Uytterhoeven 
71263636d95SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
71363636d95SGeert Uytterhoeven 			return NOTIFY_OK;
71463636d95SGeert Uytterhoeven 
71563636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
71663636d95SGeert Uytterhoeven 		if (chip == NULL)
71763636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
71863636d95SGeert Uytterhoeven 
71963636d95SGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, rd->dn);
72063636d95SGeert Uytterhoeven 		if (ret < 0) {
72163636d95SGeert Uytterhoeven 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
72263636d95SGeert Uytterhoeven 			       rd->dn);
72363636d95SGeert Uytterhoeven 			of_node_clear_flag(rd->dn, OF_POPULATED);
72463636d95SGeert Uytterhoeven 			return notifier_from_errno(ret);
72563636d95SGeert Uytterhoeven 		}
72663636d95SGeert Uytterhoeven 		break;
72763636d95SGeert Uytterhoeven 
72863636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_REMOVE:
72963636d95SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
73063636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* already depopulated */
73163636d95SGeert Uytterhoeven 
73263636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
73363636d95SGeert Uytterhoeven 		if (chip == NULL)
73463636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
73563636d95SGeert Uytterhoeven 
73663636d95SGeert Uytterhoeven 		of_gpiochip_remove_hog(chip, rd->dn);
73763636d95SGeert Uytterhoeven 		of_node_clear_flag(rd->dn, OF_POPULATED);
73863636d95SGeert Uytterhoeven 		break;
73963636d95SGeert Uytterhoeven 	}
74063636d95SGeert Uytterhoeven 
74163636d95SGeert Uytterhoeven 	return NOTIFY_OK;
74263636d95SGeert Uytterhoeven }
74363636d95SGeert Uytterhoeven 
74463636d95SGeert Uytterhoeven struct notifier_block gpio_of_notifier = {
74563636d95SGeert Uytterhoeven 	.notifier_call = of_gpio_notify,
74663636d95SGeert Uytterhoeven };
74763636d95SGeert Uytterhoeven #endif /* CONFIG_OF_DYNAMIC */
74863636d95SGeert Uytterhoeven 
749f625d460SBenoit Parrot /**
75067049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
751f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
75267049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
753f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
754f141ed65SGrant Likely  *
755f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
75667049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
757f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
758f141ed65SGrant Likely  */
759b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc,
760b0c7e73bSGeert Uytterhoeven 				const struct of_phandle_args *gpiospec,
761b0c7e73bSGeert Uytterhoeven 				u32 *flags)
762f141ed65SGrant Likely {
763f141ed65SGrant Likely 	/*
764f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
76520a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
766f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
767f141ed65SGrant Likely 	 * but not recommended).
768f141ed65SGrant Likely 	 */
769f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
770f141ed65SGrant Likely 		WARN_ON(1);
771f141ed65SGrant Likely 		return -EINVAL;
772f141ed65SGrant Likely 	}
773f141ed65SGrant Likely 
774f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
775f141ed65SGrant Likely 		return -EINVAL;
776f141ed65SGrant Likely 
7777b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
778f141ed65SGrant Likely 		return -EINVAL;
779f141ed65SGrant Likely 
780f141ed65SGrant Likely 	if (flags)
781f141ed65SGrant Likely 		*flags = gpiospec->args[1];
782f141ed65SGrant Likely 
783f141ed65SGrant Likely 	return gpiospec->args[0];
784f141ed65SGrant Likely }
785f141ed65SGrant Likely 
786f141ed65SGrant Likely /**
7873208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
788f141ed65SGrant Likely  * @np:		device node of the GPIO chip
789f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
7903208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
791f141ed65SGrant Likely  *
792f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
793f141ed65SGrant Likely  *
794f141ed65SGrant Likely  * 1) In the gpio_chip structure:
795f141ed65SGrant Likely  *    - all the callbacks
796f141ed65SGrant Likely  *    - of_gpio_n_cells
797f141ed65SGrant Likely  *    - of_xlate callback (optional)
798f141ed65SGrant Likely  *
799f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
800f141ed65SGrant Likely  *    - save_regs callback (optional)
801f141ed65SGrant Likely  *
802f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
803f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
804f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
805f141ed65SGrant Likely  */
8063208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
8073208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
8083208b0f0SLinus Walleij 			    void *data)
809f141ed65SGrant Likely {
810f141ed65SGrant Likely 	int ret = -ENOMEM;
811f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
812f141ed65SGrant Likely 
8137eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
814f141ed65SGrant Likely 	if (!gc->label)
815f141ed65SGrant Likely 		goto err0;
816f141ed65SGrant Likely 
817f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
818f141ed65SGrant Likely 	if (!mm_gc->regs)
819f141ed65SGrant Likely 		goto err1;
820f141ed65SGrant Likely 
821f141ed65SGrant Likely 	gc->base = -1;
822f141ed65SGrant Likely 
823f141ed65SGrant Likely 	if (mm_gc->save_regs)
824f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
825f141ed65SGrant Likely 
8265d07a692SLiang He 	of_node_put(mm_gc->gc.of_node);
8275d07a692SLiang He 	mm_gc->gc.of_node = of_node_get(np);
828f141ed65SGrant Likely 
8293208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
830f141ed65SGrant Likely 	if (ret)
831f141ed65SGrant Likely 		goto err2;
832f141ed65SGrant Likely 
833f141ed65SGrant Likely 	return 0;
834f141ed65SGrant Likely err2:
8355d07a692SLiang He 	of_node_put(np);
836f141ed65SGrant Likely 	iounmap(mm_gc->regs);
837f141ed65SGrant Likely err1:
838f141ed65SGrant Likely 	kfree(gc->label);
839f141ed65SGrant Likely err0:
8407eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
841f141ed65SGrant Likely 	return ret;
842f141ed65SGrant Likely }
8436d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
844f141ed65SGrant Likely 
845d621e8baSRicardo Ribalda Delgado /**
846d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
847d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
848d621e8baSRicardo Ribalda Delgado  */
849d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
850d621e8baSRicardo Ribalda Delgado {
851d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
852d621e8baSRicardo Ribalda Delgado 
853d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
854d621e8baSRicardo Ribalda Delgado 		return;
855d621e8baSRicardo Ribalda Delgado 
856d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
857d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
858d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
859d621e8baSRicardo Ribalda Delgado }
8606d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
861d621e8baSRicardo Ribalda Delgado 
862726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
863726cb3baSStephen Boyd {
864726cb3baSStephen Boyd 	int len, i;
865726cb3baSStephen Boyd 	u32 start, count;
866726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
867726cb3baSStephen Boyd 
868726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
869726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
870726cb3baSStephen Boyd 		return;
871726cb3baSStephen Boyd 
872726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
873726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
874726cb3baSStephen Boyd 					   i, &start);
875726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
876726cb3baSStephen Boyd 					   i + 1, &count);
877e75f88efSAndrei Lalaev 		if (start >= chip->ngpio || start + count > chip->ngpio)
878726cb3baSStephen Boyd 			continue;
879726cb3baSStephen Boyd 
880726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
881726cb3baSStephen Boyd 	}
882726cb3baSStephen Boyd };
883726cb3baSStephen Boyd 
884f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
88528355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
886f23f1516SShiraz Hashim {
887f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
888f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
8891e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
890f23f1516SShiraz Hashim 	int index = 0, ret;
891586a87e6SChristian Ruppert 	const char *name;
892586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
893586a87e6SChristian Ruppert 	struct property *group_names;
894f23f1516SShiraz Hashim 
895f23f1516SShiraz Hashim 	if (!np)
89628355f81STomeu Vizoso 		return 0;
897f23f1516SShiraz Hashim 
8983550bba2SStefan Wahren 	if (!of_property_read_bool(np, "gpio-ranges") &&
8993550bba2SStefan Wahren 	    chip->of_gpio_ranges_fallback) {
9003550bba2SStefan Wahren 		return chip->of_gpio_ranges_fallback(chip, np);
9013550bba2SStefan Wahren 	}
9023550bba2SStefan Wahren 
903586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
904586a87e6SChristian Ruppert 
905ad4e1a7cSHaojian Zhuang 	for (;; index++) {
906d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
907d9fe0039SStephen Warren 				index, &pinspec);
908f23f1516SShiraz Hashim 		if (ret)
909f23f1516SShiraz Hashim 			break;
910f23f1516SShiraz Hashim 
9111e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
912602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
9131e63d7b9SLinus Walleij 		if (!pctldev)
91428355f81STomeu Vizoso 			return -EPROBE_DEFER;
915f23f1516SShiraz Hashim 
916586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
917586a87e6SChristian Ruppert 			if (group_names) {
91872858602SLaurent Navet 				of_property_read_string_index(np,
919586a87e6SChristian Ruppert 						group_names_propname,
920586a87e6SChristian Ruppert 						index, &name);
921586a87e6SChristian Ruppert 				if (strlen(name)) {
9227eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
9237eb6ce2fSRob Herring 						np);
924586a87e6SChristian Ruppert 					break;
925586a87e6SChristian Ruppert 				}
926586a87e6SChristian Ruppert 			}
927586a87e6SChristian Ruppert 			/* npins != 0: linear range */
9281e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
929ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
9301e63d7b9SLinus Walleij 					pinspec.args[0],
93186853c83SHaojian Zhuang 					pinspec.args[1],
93286853c83SHaojian Zhuang 					pinspec.args[2]);
9331e63d7b9SLinus Walleij 			if (ret)
93428355f81STomeu Vizoso 				return ret;
935586a87e6SChristian Ruppert 		} else {
936586a87e6SChristian Ruppert 			/* npins == 0: special range */
937586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
9387eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
9397eb6ce2fSRob Herring 					np);
940586a87e6SChristian Ruppert 				break;
941586a87e6SChristian Ruppert 			}
942586a87e6SChristian Ruppert 
943586a87e6SChristian Ruppert 			if (!group_names) {
9447eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
9457eb6ce2fSRob Herring 					np, group_names_propname);
946586a87e6SChristian Ruppert 				break;
947586a87e6SChristian Ruppert 			}
948586a87e6SChristian Ruppert 
949586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
950586a87e6SChristian Ruppert 						group_names_propname,
951586a87e6SChristian Ruppert 						index, &name);
952586a87e6SChristian Ruppert 			if (ret)
953586a87e6SChristian Ruppert 				break;
954586a87e6SChristian Ruppert 
955586a87e6SChristian Ruppert 			if (!strlen(name)) {
9567eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
9577eb6ce2fSRob Herring 				np);
958586a87e6SChristian Ruppert 				break;
959586a87e6SChristian Ruppert 			}
960586a87e6SChristian Ruppert 
961586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
962586a87e6SChristian Ruppert 						pinspec.args[0], name);
963586a87e6SChristian Ruppert 			if (ret)
96428355f81STomeu Vizoso 				return ret;
965586a87e6SChristian Ruppert 		}
966ad4e1a7cSHaojian Zhuang 	}
96728355f81STomeu Vizoso 
96828355f81STomeu Vizoso 	return 0;
969f23f1516SShiraz Hashim }
970f23f1516SShiraz Hashim 
971f23f1516SShiraz Hashim #else
97228355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
973f23f1516SShiraz Hashim #endif
974f23f1516SShiraz Hashim 
97528355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
976f141ed65SGrant Likely {
977f0d1ab05SLinus Walleij 	int ret;
97828355f81STomeu Vizoso 
979f141ed65SGrant Likely 	if (!chip->of_node)
98028355f81STomeu Vizoso 		return 0;
981f141ed65SGrant Likely 
982f141ed65SGrant Likely 	if (!chip->of_xlate) {
983f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
984f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
985f141ed65SGrant Likely 	}
986f141ed65SGrant Likely 
9871020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
9881020dfd1SMasahiro Yamada 		return -EINVAL;
9891020dfd1SMasahiro Yamada 
990726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
991726cb3baSStephen Boyd 
992f0d1ab05SLinus Walleij 	ret = of_gpiochip_add_pin_range(chip);
993f0d1ab05SLinus Walleij 	if (ret)
994f0d1ab05SLinus Walleij 		return ret;
99528355f81STomeu Vizoso 
996f141ed65SGrant Likely 	of_node_get(chip->of_node);
997f625d460SBenoit Parrot 
998f0d1ab05SLinus Walleij 	ret = of_gpiochip_scan_gpios(chip);
9992f4133bbSAndy Shevchenko 	if (ret)
1000f7299d44SGeert Uytterhoeven 		of_node_put(chip->of_node);
1001f7299d44SGeert Uytterhoeven 
1002f0d1ab05SLinus Walleij 	return ret;
1003f141ed65SGrant Likely }
1004f141ed65SGrant Likely 
1005f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
1006f141ed65SGrant Likely {
1007f141ed65SGrant Likely 	of_node_put(chip->of_node);
1008f141ed65SGrant Likely }
10094731210cSSaravana Kannan 
10104731210cSSaravana Kannan void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
10114731210cSSaravana Kannan {
10121df62542SAndy Shevchenko 	/* Set default OF node to parent's one if present */
10131df62542SAndy Shevchenko 	if (gc->parent)
10141df62542SAndy Shevchenko 		gdev->dev.of_node = gc->parent->of_node;
10151df62542SAndy Shevchenko 
1016ac627260SBartosz Golaszewski 	if (gc->fwnode)
1017ac627260SBartosz Golaszewski 		gc->of_node = to_of_node(gc->fwnode);
1018ac627260SBartosz Golaszewski 
10194731210cSSaravana Kannan 	/* If the gpiochip has an assigned OF node this takes precedence */
10204731210cSSaravana Kannan 	if (gc->of_node)
10214731210cSSaravana Kannan 		gdev->dev.of_node = gc->of_node;
10224731210cSSaravana Kannan 	else
10234731210cSSaravana Kannan 		gc->of_node = gdev->dev.of_node;
10244731210cSSaravana Kannan }
1025