xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 34cb9352)
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 
88d59fdbc7SAndy Shevchenko 	return device_match_of_node(&chip->gpiodev->dev, 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 
133e3186e36SDmitry Torokhov /*
134e3186e36SDmitry Torokhov  * Overrides stated polarity of a gpio line and warns when there is a
135e3186e36SDmitry Torokhov  * discrepancy.
136e3186e36SDmitry Torokhov  */
137e3186e36SDmitry Torokhov static void of_gpio_quirk_polarity(const struct device_node *np,
138e3186e36SDmitry Torokhov 				   bool active_high,
139e3186e36SDmitry Torokhov 				   enum of_gpio_flags *flags)
140e3186e36SDmitry Torokhov {
141e3186e36SDmitry Torokhov 	if (active_high) {
142e3186e36SDmitry Torokhov 		if (*flags & OF_GPIO_ACTIVE_LOW) {
143e3186e36SDmitry Torokhov 			pr_warn("%s GPIO handle specifies active low - ignored\n",
144e3186e36SDmitry Torokhov 				of_node_full_name(np));
145e3186e36SDmitry Torokhov 			*flags &= ~OF_GPIO_ACTIVE_LOW;
146e3186e36SDmitry Torokhov 		}
147e3186e36SDmitry Torokhov 	} else {
148e3186e36SDmitry Torokhov 		if (!(*flags & OF_GPIO_ACTIVE_LOW))
149e3186e36SDmitry Torokhov 			pr_info("%s enforce active low on GPIO handle\n",
150e3186e36SDmitry Torokhov 				of_node_full_name(np));
151e3186e36SDmitry Torokhov 		*flags |= OF_GPIO_ACTIVE_LOW;
152e3186e36SDmitry Torokhov 	}
153e3186e36SDmitry Torokhov }
154e3186e36SDmitry Torokhov 
15599d18d42SDmitry Torokhov /*
15699d18d42SDmitry Torokhov  * This quirk does static polarity overrides in cases where existing
15799d18d42SDmitry Torokhov  * DTS specified incorrect polarity.
15899d18d42SDmitry Torokhov  */
15999d18d42SDmitry Torokhov static void of_gpio_try_fixup_polarity(const struct device_node *np,
16099d18d42SDmitry Torokhov 				       const char *propname,
16199d18d42SDmitry Torokhov 				       enum of_gpio_flags *flags)
16299d18d42SDmitry Torokhov {
16399d18d42SDmitry Torokhov 	static const struct {
16499d18d42SDmitry Torokhov 		const char *compatible;
16599d18d42SDmitry Torokhov 		const char *propname;
16699d18d42SDmitry Torokhov 		bool active_high;
16799d18d42SDmitry Torokhov 	} gpios[] = {
16899d18d42SDmitry Torokhov #if !IS_ENABLED(CONFIG_LCD_HX8357)
16999d18d42SDmitry Torokhov 		/*
17099d18d42SDmitry Torokhov 		 * Himax LCD controllers used incorrectly named
17199d18d42SDmitry Torokhov 		 * "gpios-reset" property and also specified wrong
17299d18d42SDmitry Torokhov 		 * polarity.
17399d18d42SDmitry Torokhov 		 */
17499d18d42SDmitry Torokhov 		{ "himax,hx8357",	"gpios-reset",	false },
17599d18d42SDmitry Torokhov 		{ "himax,hx8369",	"gpios-reset",	false },
17699d18d42SDmitry Torokhov #endif
17799d18d42SDmitry Torokhov 	};
17899d18d42SDmitry Torokhov 	unsigned int i;
17999d18d42SDmitry Torokhov 
18099d18d42SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
18199d18d42SDmitry Torokhov 		if (of_device_is_compatible(np, gpios[i].compatible) &&
18299d18d42SDmitry Torokhov 		    !strcmp(propname, gpios[i].propname)) {
18399d18d42SDmitry Torokhov 			of_gpio_quirk_polarity(np, gpios[i].active_high, flags);
18499d18d42SDmitry Torokhov 			break;
18599d18d42SDmitry Torokhov 		}
18699d18d42SDmitry Torokhov 	}
18799d18d42SDmitry Torokhov }
18899d18d42SDmitry Torokhov 
189*34cb9352SDmitry Torokhov static void of_gpio_set_polarity_by_property(const struct device_node *np,
19089a5e15bSLinus Walleij 					     const char *propname,
191*34cb9352SDmitry Torokhov 					     enum of_gpio_flags *flags)
192a603a2b8SLinus Walleij {
193*34cb9352SDmitry Torokhov 	static const struct {
194*34cb9352SDmitry Torokhov 		const char *compatible;
195*34cb9352SDmitry Torokhov 		const char *gpio_propname;
196*34cb9352SDmitry Torokhov 		const char *polarity_propname;
197*34cb9352SDmitry Torokhov 	} gpios[] = {
198*34cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_FEC)
199*34cb9352SDmitry Torokhov 		/* Freescale Fast Ethernet Controller */
200*34cb9352SDmitry Torokhov 		{ "fsl,imx25-fec",   "phy-reset-gpios", "phy-reset-active-high" },
201*34cb9352SDmitry Torokhov 		{ "fsl,imx27-fec",   "phy-reset-gpios", "phy-reset-active-high" },
202*34cb9352SDmitry Torokhov 		{ "fsl,imx28-fec",   "phy-reset-gpios", "phy-reset-active-high" },
203*34cb9352SDmitry Torokhov 		{ "fsl,imx6q-fec",   "phy-reset-gpios", "phy-reset-active-high" },
204*34cb9352SDmitry Torokhov 		{ "fsl,mvf600-fec",  "phy-reset-gpios", "phy-reset-active-high" },
205*34cb9352SDmitry Torokhov 		{ "fsl,imx6sx-fec",  "phy-reset-gpios", "phy-reset-active-high" },
206*34cb9352SDmitry Torokhov 		{ "fsl,imx6ul-fec",  "phy-reset-gpios", "phy-reset-active-high" },
207*34cb9352SDmitry Torokhov 		{ "fsl,imx8mq-fec",  "phy-reset-gpios", "phy-reset-active-high" },
208*34cb9352SDmitry Torokhov 		{ "fsl,imx8qm-fec",  "phy-reset-gpios", "phy-reset-active-high" },
209*34cb9352SDmitry Torokhov 		{ "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" },
210*34cb9352SDmitry Torokhov #endif
21199d18d42SDmitry Torokhov 
212a603a2b8SLinus Walleij 		/*
213a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
214a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
215a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
216a603a2b8SLinus Walleij 		 * be actively ignored.
217a603a2b8SLinus Walleij 		 */
218*34cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE)
219*34cb9352SDmitry Torokhov 		{ "regulator-fixed",   "gpios",        "enable-active-high" },
220*34cb9352SDmitry Torokhov 		{ "regulator-fixed",   "gpio",         "enable-active-high" },
221*34cb9352SDmitry Torokhov 		{ "reg-fixed-voltage", "gpios",        "enable-active-high" },
222*34cb9352SDmitry Torokhov 		{ "reg-fixed-voltage", "gpio",         "enable-active-high" },
223*34cb9352SDmitry Torokhov #endif
224*34cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_GPIO)
225*34cb9352SDmitry Torokhov 		{ "regulator-gpio",    "enable-gpio",  "enable-active-high" },
226*34cb9352SDmitry Torokhov 		{ "regulator-gpio",    "enable-gpios", "enable-active-high" },
227*34cb9352SDmitry Torokhov #endif
228*34cb9352SDmitry Torokhov 	};
229*34cb9352SDmitry Torokhov 	unsigned int i;
230*34cb9352SDmitry Torokhov 	bool active_high;
231*34cb9352SDmitry Torokhov 
232*34cb9352SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
233*34cb9352SDmitry Torokhov 		if (of_device_is_compatible(np, gpios[i].compatible) &&
234*34cb9352SDmitry Torokhov 		    !strcmp(propname, gpios[i].gpio_propname)) {
235*34cb9352SDmitry Torokhov 			active_high = of_property_read_bool(np,
236*34cb9352SDmitry Torokhov 						gpios[i].polarity_propname);
237e3186e36SDmitry Torokhov 			of_gpio_quirk_polarity(np, active_high, flags);
238*34cb9352SDmitry Torokhov 			break;
239a603a2b8SLinus Walleij 		}
240*34cb9352SDmitry Torokhov 	}
241*34cb9352SDmitry Torokhov }
242*34cb9352SDmitry Torokhov 
243*34cb9352SDmitry Torokhov static void of_gpio_flags_quirks(const struct device_node *np,
244*34cb9352SDmitry Torokhov 				 const char *propname,
245*34cb9352SDmitry Torokhov 				 enum of_gpio_flags *flags,
246*34cb9352SDmitry Torokhov 				 int index)
247*34cb9352SDmitry Torokhov {
248*34cb9352SDmitry Torokhov 	of_gpio_try_fixup_polarity(np, propname, flags);
249*34cb9352SDmitry Torokhov 	of_gpio_set_polarity_by_property(np, propname, flags);
250*34cb9352SDmitry Torokhov 
251a603a2b8SLinus Walleij 	/*
252a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
253a603a2b8SLinus Walleij 	 */
254a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
255a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
256a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
257a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
258a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
259a603a2b8SLinus Walleij 			of_node_full_name(np));
260a603a2b8SLinus Walleij 	}
2616953c57aSLinus Walleij 
2626953c57aSLinus Walleij 	/*
2636953c57aSLinus Walleij 	 * Legacy handling of SPI active high chip select. If we have a
2646953c57aSLinus Walleij 	 * property named "cs-gpios" we need to inspect the child node
2656953c57aSLinus Walleij 	 * to determine if the flags should have inverted semantics.
2666953c57aSLinus Walleij 	 */
267da7f1349SLinus Walleij 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
268a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
2696953c57aSLinus Walleij 		struct device_node *child;
2706953c57aSLinus Walleij 		u32 cs;
2716953c57aSLinus Walleij 		int ret;
2726953c57aSLinus Walleij 
2736953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
2746953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
275c1c04ceaSLinus Walleij 			if (ret)
2766953c57aSLinus Walleij 				continue;
2776953c57aSLinus Walleij 			if (cs == index) {
2786953c57aSLinus Walleij 				/*
2796953c57aSLinus Walleij 				 * SPI children have active low chip selects
2806953c57aSLinus Walleij 				 * by default. This can be specified negatively
2816953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
2826953c57aSLinus Walleij 				 * device node, or actively by tagging on
2836953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
2846953c57aSLinus Walleij 				 * tree. If the line is simultaneously
2856953c57aSLinus Walleij 				 * tagged as active low in the device tree
2866953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
2876953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
2886953c57aSLinus Walleij 				 * take precedence.
2896953c57aSLinus Walleij 				 */
290e3186e36SDmitry Torokhov 				bool active_high = of_property_read_bool(child,
291e3186e36SDmitry Torokhov 								"spi-cs-high");
292e3186e36SDmitry Torokhov 				of_gpio_quirk_polarity(child, active_high,
293e3186e36SDmitry Torokhov 						       flags);
29489fea04cSNishka Dasgupta 				of_node_put(child);
2956953c57aSLinus Walleij 				break;
2966953c57aSLinus Walleij 			}
2976953c57aSLinus Walleij 		}
2986953c57aSLinus Walleij 	}
299edc1ef3fSMartin Blumenstingl 
300edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
301edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
302edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
303edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
304edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
305a603a2b8SLinus Walleij }
306a603a2b8SLinus Walleij 
307f141ed65SGrant Likely /**
308af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
309f141ed65SGrant Likely  * @np:		device node to get GPIO from
310f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
311f141ed65SGrant Likely  * @index:	index of the GPIO
312f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
313f141ed65SGrant Likely  *
314af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
315f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
316f141ed65SGrant Likely  * in flags for the GPIO.
317f141ed65SGrant Likely  */
318e6ae9a83SKrzysztof Kozlowski static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
319af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
320f141ed65SGrant Likely {
321762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
322762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
323762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
324f141ed65SGrant Likely 	int ret;
325f141ed65SGrant Likely 
326c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
327762c2e46SMasahiro Yamada 					     &gpiospec);
3283d0f7cf0SGrant Likely 	if (ret) {
3297eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
3307eb6ce2fSRob Herring 			__func__, propname, np, index);
331af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
3323d0f7cf0SGrant Likely 	}
333f141ed65SGrant Likely 
334c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
335762c2e46SMasahiro Yamada 	if (!chip) {
336762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
337762c2e46SMasahiro Yamada 		goto out;
338762c2e46SMasahiro Yamada 	}
3393d0f7cf0SGrant Likely 
34099468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
341762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
342762c2e46SMasahiro Yamada 		goto out;
343762c2e46SMasahiro Yamada 
344605f2d34SLinus Walleij 	if (flags)
34589a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
346a603a2b8SLinus Walleij 
3477eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
3487eb6ce2fSRob Herring 		 __func__, propname, np, index,
349762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
350762c2e46SMasahiro Yamada 
351762c2e46SMasahiro Yamada out:
352762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
353762c2e46SMasahiro Yamada 
354762c2e46SMasahiro Yamada 	return desc;
355f141ed65SGrant Likely }
356f141ed65SGrant Likely 
357e6ae9a83SKrzysztof Kozlowski int of_get_named_gpio_flags(const struct device_node *np, const char *list_name,
358f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
359f01d9075SAlexandre Courbot {
360f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
361f01d9075SAlexandre Courbot 
362f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
363f01d9075SAlexandre Courbot 
364f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
365f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
366f01d9075SAlexandre Courbot 	else
367f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
368f01d9075SAlexandre Courbot }
3696d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
370f01d9075SAlexandre Courbot 
371d9e7f0e3SDmitry Torokhov /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
372d9e7f0e3SDmitry Torokhov static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
373d9e7f0e3SDmitry Torokhov {
374d9e7f0e3SDmitry Torokhov 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
375d9e7f0e3SDmitry Torokhov 
376d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_ACTIVE_LOW)
377d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_ACTIVE_LOW;
378d9e7f0e3SDmitry Torokhov 
379d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_SINGLE_ENDED) {
380d9e7f0e3SDmitry Torokhov 		if (flags & OF_GPIO_OPEN_DRAIN)
381d9e7f0e3SDmitry Torokhov 			lflags |= GPIO_OPEN_DRAIN;
382d9e7f0e3SDmitry Torokhov 		else
383d9e7f0e3SDmitry Torokhov 			lflags |= GPIO_OPEN_SOURCE;
384d9e7f0e3SDmitry Torokhov 	}
385d9e7f0e3SDmitry Torokhov 
386d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_TRANSITORY)
387d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_TRANSITORY;
388d9e7f0e3SDmitry Torokhov 
389d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_UP)
390d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_UP;
391d9e7f0e3SDmitry Torokhov 
392d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_DOWN)
393d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_DOWN;
394d9e7f0e3SDmitry Torokhov 
395d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_DISABLE)
396d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_DISABLE;
397d9e7f0e3SDmitry Torokhov 
398d9e7f0e3SDmitry Torokhov 	return lflags;
399d9e7f0e3SDmitry Torokhov }
400d9e7f0e3SDmitry Torokhov 
401f626d6dfSLinus Walleij /**
402f626d6dfSLinus Walleij  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
403f626d6dfSLinus Walleij  * @node:	handle of the OF node
404f626d6dfSLinus Walleij  * @propname:	name of the DT property representing the GPIO
405f626d6dfSLinus Walleij  * @index:	index of the GPIO to obtain for the consumer
406f626d6dfSLinus Walleij  * @dflags:	GPIO initialization flags
407f626d6dfSLinus Walleij  * @label:	label to attach to the requested GPIO
408f626d6dfSLinus Walleij  *
409f626d6dfSLinus Walleij  * Returns:
410f626d6dfSLinus Walleij  * On successful request the GPIO pin is configured in accordance with
411f626d6dfSLinus Walleij  * provided @dflags.
412f626d6dfSLinus Walleij  *
413f626d6dfSLinus Walleij  * In case of error an ERR_PTR() is returned.
414f626d6dfSLinus Walleij  */
415e6ae9a83SKrzysztof Kozlowski struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
416f626d6dfSLinus Walleij 					 const char *propname, int index,
417f626d6dfSLinus Walleij 					 enum gpiod_flags dflags,
418f626d6dfSLinus Walleij 					 const char *label)
419f626d6dfSLinus Walleij {
420d9e7f0e3SDmitry Torokhov 	unsigned long lflags;
421f626d6dfSLinus Walleij 	struct gpio_desc *desc;
422d9e7f0e3SDmitry Torokhov 	enum of_gpio_flags of_flags;
423f626d6dfSLinus Walleij 	int ret;
424f626d6dfSLinus Walleij 
425d9e7f0e3SDmitry Torokhov 	desc = of_get_named_gpiod_flags(node, propname, index, &of_flags);
426d9e7f0e3SDmitry Torokhov 	if (!desc || IS_ERR(desc))
427f626d6dfSLinus Walleij 		return desc;
428f626d6dfSLinus Walleij 
429f626d6dfSLinus Walleij 	ret = gpiod_request(desc, label);
430be7ae45cSMarco Felsch 	if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
431f626d6dfSLinus Walleij 		return desc;
432f626d6dfSLinus Walleij 	if (ret)
433f626d6dfSLinus Walleij 		return ERR_PTR(ret);
434f626d6dfSLinus Walleij 
435d9e7f0e3SDmitry Torokhov 	lflags = of_convert_gpio_flags(of_flags);
43631bea231SNuno Sá 
437f626d6dfSLinus Walleij 	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
438f626d6dfSLinus Walleij 	if (ret < 0) {
439f626d6dfSLinus Walleij 		gpiod_put(desc);
440f626d6dfSLinus Walleij 		return ERR_PTR(ret);
441f626d6dfSLinus Walleij 	}
442f626d6dfSLinus Walleij 
443f626d6dfSLinus Walleij 	return desc;
444f626d6dfSLinus Walleij }
4456d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
446f141ed65SGrant Likely 
447b311c5cbSDmitry Torokhov static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
44898c3c940SDmitry Torokhov 					     const char *con_id,
44998c3c940SDmitry Torokhov 					     unsigned int idx,
450c8582339SLinus Walleij 					     enum of_gpio_flags *of_flags)
451c8582339SLinus Walleij {
452b311c5cbSDmitry Torokhov 	static const struct of_rename_gpio {
453b311c5cbSDmitry Torokhov 		const char *con_id;
454b311c5cbSDmitry Torokhov 		const char *legacy_id;	/* NULL - same as con_id */
455b311c5cbSDmitry Torokhov 		/*
456b311c5cbSDmitry Torokhov 		 * Compatible string can be set to NULL in case where
457b311c5cbSDmitry Torokhov 		 * matching to a particular compatible is not practical,
458b311c5cbSDmitry Torokhov 		 * but it should only be done for gpio names that have
459b311c5cbSDmitry Torokhov 		 * vendor prefix to reduce risk of false positives.
460b311c5cbSDmitry Torokhov 		 * Addition of such entries is strongly discouraged.
461b311c5cbSDmitry Torokhov 		 */
462b311c5cbSDmitry Torokhov 		const char *compatible;
463b311c5cbSDmitry Torokhov 	} gpios[] = {
464fbbbcd17SDmitry Torokhov #if !IS_ENABLED(CONFIG_LCD_HX8357)
465fbbbcd17SDmitry Torokhov 		/* Himax LCD controllers used "gpios-reset" */
466fbbbcd17SDmitry Torokhov 		{ "reset",	"gpios-reset",	"himax,hx8357" },
467fbbbcd17SDmitry Torokhov 		{ "reset",	"gpios-reset",	"himax,hx8369" },
468fbbbcd17SDmitry Torokhov #endif
469b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_MFD_ARIZONA)
470b311c5cbSDmitry Torokhov 		{ "wlf,reset",	NULL,		NULL },
471b311c5cbSDmitry Torokhov #endif
472eaf1a296SDmitry Torokhov #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
473eaf1a296SDmitry Torokhov 		{ "rtc-data",	"gpio-rtc-data",	"moxa,moxart-rtc" },
474eaf1a296SDmitry Torokhov 		{ "rtc-sclk",	"gpio-rtc-sclk",	"moxa,moxart-rtc" },
475eaf1a296SDmitry Torokhov 		{ "rtc-reset",	"gpio-rtc-reset",	"moxa,moxart-rtc" },
476eaf1a296SDmitry Torokhov #endif
4779c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
4789c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"marvell,nfc-i2c" },
4799c2cc717SDmitry Torokhov #endif
4809c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
4819c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"marvell,nfc-spi" },
4829c2cc717SDmitry Torokhov #endif
4839c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
4849c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"marvell,nfc-uart" },
4859c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"mrvl,nfc-uart" },
4869c2cc717SDmitry Torokhov #endif
487fbbbcd17SDmitry Torokhov #if !IS_ENABLED(CONFIG_PCI_LANTIQ)
488fbbbcd17SDmitry Torokhov 		/* MIPS Lantiq PCI */
489fbbbcd17SDmitry Torokhov 		{ "reset",	"gpios-reset",	"lantiq,pci-xway" },
490fbbbcd17SDmitry Torokhov #endif
491307c593bSDmitry Torokhov 
492b311c5cbSDmitry Torokhov 		/*
493b311c5cbSDmitry Torokhov 		 * Some regulator bindings happened before we managed to
494b311c5cbSDmitry Torokhov 		 * establish that GPIO properties should be named
495b311c5cbSDmitry Torokhov 		 * "foo-gpios" so we have this special kludge for them.
496b311c5cbSDmitry Torokhov 		 */
497307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
498b311c5cbSDmitry Torokhov 		{ "wlf,ldoena",  NULL,		NULL }, /* Arizona */
499307c593bSDmitry Torokhov #endif
500307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
501b311c5cbSDmitry Torokhov 		{ "wlf,ldo1ena", NULL,		NULL }, /* WM8994 */
502b311c5cbSDmitry Torokhov 		{ "wlf,ldo2ena", NULL,		NULL }, /* WM8994 */
503b311c5cbSDmitry Torokhov #endif
504c8582339SLinus Walleij 
505944004ebSDmitry Torokhov #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
506944004ebSDmitry Torokhov 		{ "reset",	"cirrus,gpio-nreset",	"cirrus,cs42l56" },
507944004ebSDmitry Torokhov #endif
508fbbbcd17SDmitry Torokhov #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
509fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3x" },
510fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic33" },
511fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3007" },
512fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3104" },
513fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3106" },
514fbbbcd17SDmitry Torokhov #endif
515307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_GPIO)
516c8582339SLinus Walleij 		/*
517b311c5cbSDmitry Torokhov 		 * The SPI GPIO bindings happened before we managed to
518b311c5cbSDmitry Torokhov 		 * establish that GPIO properties should be named
519b311c5cbSDmitry Torokhov 		 * "foo-gpios" so we have this special kludge for them.
520c8582339SLinus Walleij 		 */
521b311c5cbSDmitry Torokhov 		{ "miso",	"gpio-miso",	"spi-gpio" },
522b311c5cbSDmitry Torokhov 		{ "mosi",	"gpio-mosi",	"spi-gpio" },
523b311c5cbSDmitry Torokhov 		{ "sck",	"gpio-sck",	"spi-gpio" },
524307c593bSDmitry Torokhov #endif
525c8582339SLinus Walleij 
5266a537d48SLinus Walleij 		/*
527b311c5cbSDmitry Torokhov 		 * The old Freescale bindings use simply "gpios" as name
528b311c5cbSDmitry Torokhov 		 * for the chip select lines rather than "cs-gpios" like
529b311c5cbSDmitry Torokhov 		 * all other SPI hardware. Allow this specifically for
530b311c5cbSDmitry Torokhov 		 * Freescale and PPC devices.
531e3023bf8SLinus Walleij 		 */
532307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
533b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"fsl,spi" },
534b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"aeroflexgaisler,spictrl" },
535307c593bSDmitry Torokhov #endif
536307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_PPC4xx)
537b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"ibm,ppc4xx-spi" },
538b311c5cbSDmitry Torokhov #endif
539307c593bSDmitry Torokhov 
540b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
541e3023bf8SLinus Walleij 		/*
542b311c5cbSDmitry Torokhov 		 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
543b311c5cbSDmitry Torokhov 		 * property without the compulsory "-gpios" suffix.
544e3023bf8SLinus Walleij 		 */
545b311c5cbSDmitry Torokhov 		{ "fcs,int_n",	NULL,		"fcs,fusb302" },
546b311c5cbSDmitry Torokhov #endif
5476a537d48SLinus Walleij 	};
548b311c5cbSDmitry Torokhov 	struct gpio_desc *desc;
549b311c5cbSDmitry Torokhov 	const char *legacy_id;
550b311c5cbSDmitry Torokhov 	unsigned int i;
5516a537d48SLinus Walleij 
5526a537d48SLinus Walleij 	if (!con_id)
5536a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
5546a537d48SLinus Walleij 
555b311c5cbSDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
556b311c5cbSDmitry Torokhov 		if (strcmp(con_id, gpios[i].con_id))
557b311c5cbSDmitry Torokhov 			continue;
5586a537d48SLinus Walleij 
559b311c5cbSDmitry Torokhov 		if (gpios[i].compatible &&
560b311c5cbSDmitry Torokhov 		    !of_device_is_compatible(np, gpios[i].compatible))
561b311c5cbSDmitry Torokhov 			continue;
562b311c5cbSDmitry Torokhov 
563b311c5cbSDmitry Torokhov 		legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
564b311c5cbSDmitry Torokhov 		desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
565b311c5cbSDmitry Torokhov 		if (!gpiod_not_found(desc)) {
566b311c5cbSDmitry Torokhov 			pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
567b311c5cbSDmitry Torokhov 				of_node_full_name(np), legacy_id, con_id);
568b311c5cbSDmitry Torokhov 			return desc;
569b311c5cbSDmitry Torokhov 		}
5706a537d48SLinus Walleij 	}
5716a537d48SLinus Walleij 
57211c43bb0SDmitry Torokhov 	return ERR_PTR(-ENOENT);
5736e24826dSLinus Walleij }
5746e24826dSLinus Walleij 
575326c3753SDmitry Torokhov static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
576326c3753SDmitry Torokhov 					     const char *con_id,
577326c3753SDmitry Torokhov 					     unsigned int idx,
578326c3753SDmitry Torokhov 					     enum of_gpio_flags *of_flags)
579326c3753SDmitry Torokhov {
580326c3753SDmitry Torokhov 	struct gpio_desc *desc;
581326c3753SDmitry Torokhov 	const char *legacy_id;
582326c3753SDmitry Torokhov 
583326c3753SDmitry Torokhov 	if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
584326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
585326c3753SDmitry Torokhov 
586326c3753SDmitry Torokhov 	if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
587326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
588326c3753SDmitry Torokhov 
589326c3753SDmitry Torokhov 	if (!con_id || strcmp(con_id, "i2s1-in-sel"))
590326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
591326c3753SDmitry Torokhov 
592326c3753SDmitry Torokhov 	if (idx == 0)
593326c3753SDmitry Torokhov 		legacy_id = "i2s1-in-sel-gpio1";
594326c3753SDmitry Torokhov 	else if (idx == 1)
595326c3753SDmitry Torokhov 		legacy_id = "i2s1-in-sel-gpio2";
596326c3753SDmitry Torokhov 	else
597326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
598326c3753SDmitry Torokhov 
599326c3753SDmitry Torokhov 	desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
600326c3753SDmitry Torokhov 	if (!gpiod_not_found(desc))
601326c3753SDmitry Torokhov 		pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
602326c3753SDmitry Torokhov 			of_node_full_name(np), legacy_id, con_id);
603326c3753SDmitry Torokhov 
604326c3753SDmitry Torokhov 	return desc;
605326c3753SDmitry Torokhov }
606326c3753SDmitry Torokhov 
607a2b5e207SDmitry Torokhov typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
608a2b5e207SDmitry Torokhov 						const char *con_id,
609a2b5e207SDmitry Torokhov 						unsigned int idx,
610a2b5e207SDmitry Torokhov 						enum of_gpio_flags *of_flags);
611a2b5e207SDmitry Torokhov static const of_find_gpio_quirk of_find_gpio_quirks[] = {
612b311c5cbSDmitry Torokhov 	of_find_gpio_rename,
613326c3753SDmitry Torokhov 	of_find_mt2701_gpio,
6148b10ca2fSMichael Walle 	NULL
615a2b5e207SDmitry Torokhov };
616a2b5e207SDmitry Torokhov 
617ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
618fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
619ea713bc4SLinus Walleij {
620ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
621ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
622a2b5e207SDmitry Torokhov 	const of_find_gpio_quirk *q;
623ea713bc4SLinus Walleij 	struct gpio_desc *desc;
624ea713bc4SLinus Walleij 	unsigned int i;
625ea713bc4SLinus Walleij 
626c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
627ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
628ea713bc4SLinus Walleij 		if (con_id)
629ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
630ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
631ea713bc4SLinus Walleij 		else
632ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
633ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
634ea713bc4SLinus Walleij 
635ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
636ea713bc4SLinus Walleij 						&of_flags);
6376662ae6aSMaxime Ripard 
6387b58696dSAndy Shevchenko 		if (!gpiod_not_found(desc))
639ea713bc4SLinus Walleij 			break;
640ea713bc4SLinus Walleij 	}
641ea713bc4SLinus Walleij 
642a2b5e207SDmitry Torokhov 	/* Properly named GPIO was not found, try workarounds */
643a2b5e207SDmitry Torokhov 	for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
644a2b5e207SDmitry Torokhov 		desc = (*q)(dev->of_node, con_id, idx, &of_flags);
6456e24826dSLinus Walleij 
646ea713bc4SLinus Walleij 	if (IS_ERR(desc))
647ea713bc4SLinus Walleij 		return desc;
648ea713bc4SLinus Walleij 
649d9e7f0e3SDmitry Torokhov 	*flags = of_convert_gpio_flags(of_flags);
650d449991cSThomas Petazzoni 
651ea713bc4SLinus Walleij 	return desc;
652ea713bc4SLinus Walleij }
653ea713bc4SLinus Walleij 
654f141ed65SGrant Likely /**
655fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
656f625d460SBenoit Parrot  * @np:		device node to get GPIO from
657be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
658a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
659f625d460SBenoit Parrot  * @name:	GPIO line name
660fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
661fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
662f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
663f625d460SBenoit Parrot  *
664f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
665f625d460SBenoit Parrot  * value on the error condition.
666f625d460SBenoit Parrot  */
667fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
668be715343SMasahiro Yamada 					   struct gpio_chip *chip,
669a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
670fed7026aSAndy Shevchenko 					   unsigned long *lflags,
671f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
672f625d460SBenoit Parrot {
673f625d460SBenoit Parrot 	struct device_node *chip_np;
674f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
675be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
676be715343SMasahiro Yamada 	struct gpio_desc *desc;
677a79fead5SGeert Uytterhoeven 	unsigned int i;
678f625d460SBenoit Parrot 	u32 tmp;
6793f9547e1SMasahiro Yamada 	int ret;
680f625d460SBenoit Parrot 
681be715343SMasahiro Yamada 	chip_np = chip->of_node;
682f625d460SBenoit Parrot 	if (!chip_np)
683f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
684f625d460SBenoit Parrot 
685f625d460SBenoit Parrot 	xlate_flags = 0;
6862d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
68740941954SAndy Shevchenko 	*dflags = GPIOD_ASIS;
688f625d460SBenoit Parrot 
689f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
690f625d460SBenoit Parrot 	if (ret)
691f625d460SBenoit Parrot 		return ERR_PTR(ret);
692f625d460SBenoit Parrot 
693be715343SMasahiro Yamada 	gpiospec.np = chip_np;
694be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
695f625d460SBenoit Parrot 
696a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
697a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
698a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
699f625d460SBenoit Parrot 		if (ret)
700f625d460SBenoit Parrot 			return ERR_PTR(ret);
701a79fead5SGeert Uytterhoeven 	}
702f625d460SBenoit Parrot 
70399468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
704be715343SMasahiro Yamada 	if (IS_ERR(desc))
705be715343SMasahiro Yamada 		return desc;
706f625d460SBenoit Parrot 
707d9e7f0e3SDmitry Torokhov 	*lflags = of_convert_gpio_flags(xlate_flags);
708f625d460SBenoit Parrot 
709f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
710f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
711f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
712f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
713f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
714f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
715f625d460SBenoit Parrot 	else {
71662cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
71762cdcb6cSRob Herring 			desc_to_gpio(desc), np);
718f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
719f625d460SBenoit Parrot 	}
720f625d460SBenoit Parrot 
721f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
722f625d460SBenoit Parrot 		*name = np->name;
723f625d460SBenoit Parrot 
724be715343SMasahiro Yamada 	return desc;
725f625d460SBenoit Parrot }
726f625d460SBenoit Parrot 
727f625d460SBenoit Parrot /**
728bc21077eSGeert Uytterhoeven  * of_gpiochip_add_hog - Add all hogs in a hog device node
729bc21077eSGeert Uytterhoeven  * @chip:	gpio chip to act on
730bc21077eSGeert Uytterhoeven  * @hog:	device node describing the hogs
731bc21077eSGeert Uytterhoeven  *
732bc21077eSGeert Uytterhoeven  * Returns error if it fails otherwise 0 on success.
733bc21077eSGeert Uytterhoeven  */
734bc21077eSGeert Uytterhoeven static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
735bc21077eSGeert Uytterhoeven {
736bc21077eSGeert Uytterhoeven 	enum gpiod_flags dflags;
737bc21077eSGeert Uytterhoeven 	struct gpio_desc *desc;
738bc21077eSGeert Uytterhoeven 	unsigned long lflags;
739bc21077eSGeert Uytterhoeven 	const char *name;
740bc21077eSGeert Uytterhoeven 	unsigned int i;
741bc21077eSGeert Uytterhoeven 	int ret;
742bc21077eSGeert Uytterhoeven 
743bc21077eSGeert Uytterhoeven 	for (i = 0;; i++) {
744bc21077eSGeert Uytterhoeven 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
745bc21077eSGeert Uytterhoeven 		if (IS_ERR(desc))
746bc21077eSGeert Uytterhoeven 			break;
747bc21077eSGeert Uytterhoeven 
748bc21077eSGeert Uytterhoeven 		ret = gpiod_hog(desc, name, lflags, dflags);
749bc21077eSGeert Uytterhoeven 		if (ret < 0)
750bc21077eSGeert Uytterhoeven 			return ret;
75163636d95SGeert Uytterhoeven 
75263636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
75363636d95SGeert Uytterhoeven 		desc->hog = hog;
75463636d95SGeert Uytterhoeven #endif
755bc21077eSGeert Uytterhoeven 	}
756bc21077eSGeert Uytterhoeven 
757bc21077eSGeert Uytterhoeven 	return 0;
758bc21077eSGeert Uytterhoeven }
759bc21077eSGeert Uytterhoeven 
760bc21077eSGeert Uytterhoeven /**
761fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
762f625d460SBenoit Parrot  * @chip:	gpio chip to act on
763f625d460SBenoit Parrot  *
764f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
765f625d460SBenoit Parrot  * configuration.
766ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
767f625d460SBenoit Parrot  */
768dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
769f625d460SBenoit Parrot {
770f625d460SBenoit Parrot 	struct device_node *np;
771dfbd379bSLaxman Dewangan 	int ret;
772f625d460SBenoit Parrot 
773d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
774f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
775f625d460SBenoit Parrot 			continue;
776f625d460SBenoit Parrot 
777bc21077eSGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, np);
77809e258afSWei Yongjun 		if (ret < 0) {
77909e258afSWei Yongjun 			of_node_put(np);
780dfbd379bSLaxman Dewangan 			return ret;
781f625d460SBenoit Parrot 		}
78263636d95SGeert Uytterhoeven 
78363636d95SGeert Uytterhoeven 		of_node_set_flag(np, OF_POPULATED);
78409e258afSWei Yongjun 	}
785dfbd379bSLaxman Dewangan 
786dfbd379bSLaxman Dewangan 	return 0;
787f625d460SBenoit Parrot }
788f625d460SBenoit Parrot 
78963636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
79063636d95SGeert Uytterhoeven /**
79163636d95SGeert Uytterhoeven  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
79263636d95SGeert Uytterhoeven  * @chip:	gpio chip to act on
79363636d95SGeert Uytterhoeven  * @hog:	device node describing the hogs
79463636d95SGeert Uytterhoeven  */
79563636d95SGeert Uytterhoeven static void of_gpiochip_remove_hog(struct gpio_chip *chip,
79663636d95SGeert Uytterhoeven 				   struct device_node *hog)
79763636d95SGeert Uytterhoeven {
79880c78fbeSAndy Shevchenko 	struct gpio_desc *desc;
79963636d95SGeert Uytterhoeven 
80057017eddSAndy Shevchenko 	for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
80180c78fbeSAndy Shevchenko 		if (desc->hog == hog)
80280c78fbeSAndy Shevchenko 			gpiochip_free_own_desc(desc);
80363636d95SGeert Uytterhoeven }
80463636d95SGeert Uytterhoeven 
80563636d95SGeert Uytterhoeven static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
80663636d95SGeert Uytterhoeven {
807597a8a88SAndy Shevchenko 	return device_match_of_node(&chip->gpiodev->dev, data);
80863636d95SGeert Uytterhoeven }
80963636d95SGeert Uytterhoeven 
81063636d95SGeert Uytterhoeven static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
81163636d95SGeert Uytterhoeven {
81263636d95SGeert Uytterhoeven 	return gpiochip_find(np, of_gpiochip_match_node);
81363636d95SGeert Uytterhoeven }
81463636d95SGeert Uytterhoeven 
81563636d95SGeert Uytterhoeven static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
81663636d95SGeert Uytterhoeven 			  void *arg)
81763636d95SGeert Uytterhoeven {
81863636d95SGeert Uytterhoeven 	struct of_reconfig_data *rd = arg;
81963636d95SGeert Uytterhoeven 	struct gpio_chip *chip;
82063636d95SGeert Uytterhoeven 	int ret;
82163636d95SGeert Uytterhoeven 
82263636d95SGeert Uytterhoeven 	/*
82363636d95SGeert Uytterhoeven 	 * This only supports adding and removing complete gpio-hog nodes.
82463636d95SGeert Uytterhoeven 	 * Modifying an existing gpio-hog node is not supported (except for
82563636d95SGeert Uytterhoeven 	 * changing its "status" property, which is treated the same as
82663636d95SGeert Uytterhoeven 	 * addition/removal).
82763636d95SGeert Uytterhoeven 	 */
82863636d95SGeert Uytterhoeven 	switch (of_reconfig_get_state_change(action, arg)) {
82963636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_ADD:
83063636d95SGeert Uytterhoeven 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
83163636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
83263636d95SGeert Uytterhoeven 
83363636d95SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
83463636d95SGeert Uytterhoeven 			return NOTIFY_OK;
83563636d95SGeert Uytterhoeven 
83663636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
83763636d95SGeert Uytterhoeven 		if (chip == NULL)
83863636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
83963636d95SGeert Uytterhoeven 
84063636d95SGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, rd->dn);
84163636d95SGeert Uytterhoeven 		if (ret < 0) {
84263636d95SGeert Uytterhoeven 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
84363636d95SGeert Uytterhoeven 			       rd->dn);
84463636d95SGeert Uytterhoeven 			of_node_clear_flag(rd->dn, OF_POPULATED);
84563636d95SGeert Uytterhoeven 			return notifier_from_errno(ret);
84663636d95SGeert Uytterhoeven 		}
84763636d95SGeert Uytterhoeven 		break;
84863636d95SGeert Uytterhoeven 
84963636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_REMOVE:
85063636d95SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
85163636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* already depopulated */
85263636d95SGeert Uytterhoeven 
85363636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
85463636d95SGeert Uytterhoeven 		if (chip == NULL)
85563636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
85663636d95SGeert Uytterhoeven 
85763636d95SGeert Uytterhoeven 		of_gpiochip_remove_hog(chip, rd->dn);
85863636d95SGeert Uytterhoeven 		of_node_clear_flag(rd->dn, OF_POPULATED);
85963636d95SGeert Uytterhoeven 		break;
86063636d95SGeert Uytterhoeven 	}
86163636d95SGeert Uytterhoeven 
86263636d95SGeert Uytterhoeven 	return NOTIFY_OK;
86363636d95SGeert Uytterhoeven }
86463636d95SGeert Uytterhoeven 
86563636d95SGeert Uytterhoeven struct notifier_block gpio_of_notifier = {
86663636d95SGeert Uytterhoeven 	.notifier_call = of_gpio_notify,
86763636d95SGeert Uytterhoeven };
86863636d95SGeert Uytterhoeven #endif /* CONFIG_OF_DYNAMIC */
86963636d95SGeert Uytterhoeven 
870f625d460SBenoit Parrot /**
87167049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
872f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
87367049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
874f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
875f141ed65SGrant Likely  *
876f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
87767049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
878f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
879f141ed65SGrant Likely  */
880b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc,
881b0c7e73bSGeert Uytterhoeven 				const struct of_phandle_args *gpiospec,
882b0c7e73bSGeert Uytterhoeven 				u32 *flags)
883f141ed65SGrant Likely {
884f141ed65SGrant Likely 	/*
885f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
88620a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
887f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
888f141ed65SGrant Likely 	 * but not recommended).
889f141ed65SGrant Likely 	 */
890f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
891f141ed65SGrant Likely 		WARN_ON(1);
892f141ed65SGrant Likely 		return -EINVAL;
893f141ed65SGrant Likely 	}
894f141ed65SGrant Likely 
895f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
896f141ed65SGrant Likely 		return -EINVAL;
897f141ed65SGrant Likely 
8987b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
899f141ed65SGrant Likely 		return -EINVAL;
900f141ed65SGrant Likely 
901f141ed65SGrant Likely 	if (flags)
902f141ed65SGrant Likely 		*flags = gpiospec->args[1];
903f141ed65SGrant Likely 
904f141ed65SGrant Likely 	return gpiospec->args[0];
905f141ed65SGrant Likely }
906f141ed65SGrant Likely 
907f141ed65SGrant Likely /**
9083208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
909f141ed65SGrant Likely  * @np:		device node of the GPIO chip
910f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
9113208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
912f141ed65SGrant Likely  *
913f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
914f141ed65SGrant Likely  *
915f141ed65SGrant Likely  * 1) In the gpio_chip structure:
916f141ed65SGrant Likely  *    - all the callbacks
917f141ed65SGrant Likely  *    - of_gpio_n_cells
918f141ed65SGrant Likely  *    - of_xlate callback (optional)
919f141ed65SGrant Likely  *
920f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
921f141ed65SGrant Likely  *    - save_regs callback (optional)
922f141ed65SGrant Likely  *
923f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
924f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
925f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
926f141ed65SGrant Likely  */
9273208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
9283208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
9293208b0f0SLinus Walleij 			    void *data)
930f141ed65SGrant Likely {
931f141ed65SGrant Likely 	int ret = -ENOMEM;
932f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
933f141ed65SGrant Likely 
9347eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
935f141ed65SGrant Likely 	if (!gc->label)
936f141ed65SGrant Likely 		goto err0;
937f141ed65SGrant Likely 
938f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
939f141ed65SGrant Likely 	if (!mm_gc->regs)
940f141ed65SGrant Likely 		goto err1;
941f141ed65SGrant Likely 
942f141ed65SGrant Likely 	gc->base = -1;
943f141ed65SGrant Likely 
944f141ed65SGrant Likely 	if (mm_gc->save_regs)
945f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
946f141ed65SGrant Likely 
9475d07a692SLiang He 	of_node_put(mm_gc->gc.of_node);
9485d07a692SLiang He 	mm_gc->gc.of_node = of_node_get(np);
949f141ed65SGrant Likely 
9503208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
951f141ed65SGrant Likely 	if (ret)
952f141ed65SGrant Likely 		goto err2;
953f141ed65SGrant Likely 
954f141ed65SGrant Likely 	return 0;
955f141ed65SGrant Likely err2:
9565d07a692SLiang He 	of_node_put(np);
957f141ed65SGrant Likely 	iounmap(mm_gc->regs);
958f141ed65SGrant Likely err1:
959f141ed65SGrant Likely 	kfree(gc->label);
960f141ed65SGrant Likely err0:
9617eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
962f141ed65SGrant Likely 	return ret;
963f141ed65SGrant Likely }
9646d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
965f141ed65SGrant Likely 
966d621e8baSRicardo Ribalda Delgado /**
967d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
968d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
969d621e8baSRicardo Ribalda Delgado  */
970d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
971d621e8baSRicardo Ribalda Delgado {
972d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
973d621e8baSRicardo Ribalda Delgado 
974d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
975d621e8baSRicardo Ribalda Delgado 		return;
976d621e8baSRicardo Ribalda Delgado 
977d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
978d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
979d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
980d621e8baSRicardo Ribalda Delgado }
9816d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
982d621e8baSRicardo Ribalda Delgado 
983726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
984726cb3baSStephen Boyd {
985726cb3baSStephen Boyd 	int len, i;
986726cb3baSStephen Boyd 	u32 start, count;
987726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
988726cb3baSStephen Boyd 
989726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
990726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
991726cb3baSStephen Boyd 		return;
992726cb3baSStephen Boyd 
993726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
994726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
995726cb3baSStephen Boyd 					   i, &start);
996726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
997726cb3baSStephen Boyd 					   i + 1, &count);
998e75f88efSAndrei Lalaev 		if (start >= chip->ngpio || start + count > chip->ngpio)
999726cb3baSStephen Boyd 			continue;
1000726cb3baSStephen Boyd 
1001726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
1002726cb3baSStephen Boyd 	}
1003726cb3baSStephen Boyd };
1004726cb3baSStephen Boyd 
1005f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
100628355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
1007f23f1516SShiraz Hashim {
1008f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
1009f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
10101e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
1011f23f1516SShiraz Hashim 	int index = 0, ret;
1012586a87e6SChristian Ruppert 	const char *name;
1013586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
1014586a87e6SChristian Ruppert 	struct property *group_names;
1015f23f1516SShiraz Hashim 
1016f23f1516SShiraz Hashim 	if (!np)
101728355f81STomeu Vizoso 		return 0;
1018f23f1516SShiraz Hashim 
10193550bba2SStefan Wahren 	if (!of_property_read_bool(np, "gpio-ranges") &&
10203550bba2SStefan Wahren 	    chip->of_gpio_ranges_fallback) {
10213550bba2SStefan Wahren 		return chip->of_gpio_ranges_fallback(chip, np);
10223550bba2SStefan Wahren 	}
10233550bba2SStefan Wahren 
1024586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
1025586a87e6SChristian Ruppert 
1026ad4e1a7cSHaojian Zhuang 	for (;; index++) {
1027d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1028d9fe0039SStephen Warren 				index, &pinspec);
1029f23f1516SShiraz Hashim 		if (ret)
1030f23f1516SShiraz Hashim 			break;
1031f23f1516SShiraz Hashim 
10321e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
1033602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
10341e63d7b9SLinus Walleij 		if (!pctldev)
103528355f81STomeu Vizoso 			return -EPROBE_DEFER;
1036f23f1516SShiraz Hashim 
1037586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
1038586a87e6SChristian Ruppert 			if (group_names) {
103972858602SLaurent Navet 				of_property_read_string_index(np,
1040586a87e6SChristian Ruppert 						group_names_propname,
1041586a87e6SChristian Ruppert 						index, &name);
1042586a87e6SChristian Ruppert 				if (strlen(name)) {
10437eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
10447eb6ce2fSRob Herring 						np);
1045586a87e6SChristian Ruppert 					break;
1046586a87e6SChristian Ruppert 				}
1047586a87e6SChristian Ruppert 			}
1048586a87e6SChristian Ruppert 			/* npins != 0: linear range */
10491e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
1050ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
10511e63d7b9SLinus Walleij 					pinspec.args[0],
105286853c83SHaojian Zhuang 					pinspec.args[1],
105386853c83SHaojian Zhuang 					pinspec.args[2]);
10541e63d7b9SLinus Walleij 			if (ret)
105528355f81STomeu Vizoso 				return ret;
1056586a87e6SChristian Ruppert 		} else {
1057586a87e6SChristian Ruppert 			/* npins == 0: special range */
1058586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
10597eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
10607eb6ce2fSRob Herring 					np);
1061586a87e6SChristian Ruppert 				break;
1062586a87e6SChristian Ruppert 			}
1063586a87e6SChristian Ruppert 
1064586a87e6SChristian Ruppert 			if (!group_names) {
10657eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
10667eb6ce2fSRob Herring 					np, group_names_propname);
1067586a87e6SChristian Ruppert 				break;
1068586a87e6SChristian Ruppert 			}
1069586a87e6SChristian Ruppert 
1070586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
1071586a87e6SChristian Ruppert 						group_names_propname,
1072586a87e6SChristian Ruppert 						index, &name);
1073586a87e6SChristian Ruppert 			if (ret)
1074586a87e6SChristian Ruppert 				break;
1075586a87e6SChristian Ruppert 
1076586a87e6SChristian Ruppert 			if (!strlen(name)) {
10777eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
10787eb6ce2fSRob Herring 				np);
1079586a87e6SChristian Ruppert 				break;
1080586a87e6SChristian Ruppert 			}
1081586a87e6SChristian Ruppert 
1082586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
1083586a87e6SChristian Ruppert 						pinspec.args[0], name);
1084586a87e6SChristian Ruppert 			if (ret)
108528355f81STomeu Vizoso 				return ret;
1086586a87e6SChristian Ruppert 		}
1087ad4e1a7cSHaojian Zhuang 	}
108828355f81STomeu Vizoso 
108928355f81STomeu Vizoso 	return 0;
1090f23f1516SShiraz Hashim }
1091f23f1516SShiraz Hashim 
1092f23f1516SShiraz Hashim #else
109328355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1094f23f1516SShiraz Hashim #endif
1095f23f1516SShiraz Hashim 
109628355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
1097f141ed65SGrant Likely {
1098f0d1ab05SLinus Walleij 	int ret;
109928355f81STomeu Vizoso 
1100f141ed65SGrant Likely 	if (!chip->of_node)
110128355f81STomeu Vizoso 		return 0;
1102f141ed65SGrant Likely 
1103f141ed65SGrant Likely 	if (!chip->of_xlate) {
1104f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
1105f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
1106f141ed65SGrant Likely 	}
1107f141ed65SGrant Likely 
11081020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
11091020dfd1SMasahiro Yamada 		return -EINVAL;
11101020dfd1SMasahiro Yamada 
1111726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
1112726cb3baSStephen Boyd 
1113f0d1ab05SLinus Walleij 	ret = of_gpiochip_add_pin_range(chip);
1114f0d1ab05SLinus Walleij 	if (ret)
1115f0d1ab05SLinus Walleij 		return ret;
111628355f81STomeu Vizoso 
1117f141ed65SGrant Likely 	of_node_get(chip->of_node);
1118f625d460SBenoit Parrot 
1119f0d1ab05SLinus Walleij 	ret = of_gpiochip_scan_gpios(chip);
11202f4133bbSAndy Shevchenko 	if (ret)
1121f7299d44SGeert Uytterhoeven 		of_node_put(chip->of_node);
1122f7299d44SGeert Uytterhoeven 
1123f0d1ab05SLinus Walleij 	return ret;
1124f141ed65SGrant Likely }
1125f141ed65SGrant Likely 
1126f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
1127f141ed65SGrant Likely {
1128f141ed65SGrant Likely 	of_node_put(chip->of_node);
1129f141ed65SGrant Likely }
11304731210cSSaravana Kannan 
11314731210cSSaravana Kannan void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
11324731210cSSaravana Kannan {
11331df62542SAndy Shevchenko 	/* Set default OF node to parent's one if present */
11341df62542SAndy Shevchenko 	if (gc->parent)
11351df62542SAndy Shevchenko 		gdev->dev.of_node = gc->parent->of_node;
11361df62542SAndy Shevchenko 
1137ac627260SBartosz Golaszewski 	if (gc->fwnode)
1138ac627260SBartosz Golaszewski 		gc->of_node = to_of_node(gc->fwnode);
1139ac627260SBartosz Golaszewski 
11404731210cSSaravana Kannan 	/* If the gpiochip has an assigned OF node this takes precedence */
11414731210cSSaravana Kannan 	if (gc->of_node)
11424731210cSSaravana Kannan 		gdev->dev.of_node = gc->of_node;
11434731210cSSaravana Kannan 	else
11444731210cSSaravana Kannan 		gc->of_node = gdev->dev.of_node;
11454731210cSSaravana Kannan }
1146