xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 14e8c535)
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 
26f626d6dfSLinus Walleij /*
27f626d6dfSLinus Walleij  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
28f626d6dfSLinus Walleij  *
29f626d6dfSLinus Walleij  * FIXME: get rid of those external users by converting them to GPIO
30f626d6dfSLinus Walleij  * descriptors and let them all use gpiod_get_count()
31f626d6dfSLinus Walleij  */
32f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id)
33f626d6dfSLinus Walleij {
34f626d6dfSLinus Walleij 	int ret;
35f626d6dfSLinus Walleij 	char propname[32];
36f626d6dfSLinus Walleij 	unsigned int i;
37f626d6dfSLinus Walleij 
38f626d6dfSLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
39f626d6dfSLinus Walleij 		if (con_id)
40f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s-%s",
41f626d6dfSLinus Walleij 				 con_id, gpio_suffixes[i]);
42f626d6dfSLinus Walleij 		else
43f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s",
44f626d6dfSLinus Walleij 				 gpio_suffixes[i]);
45f626d6dfSLinus Walleij 
46f626d6dfSLinus Walleij 		ret = of_gpio_named_count(dev->of_node, propname);
47f626d6dfSLinus Walleij 		if (ret > 0)
48f626d6dfSLinus Walleij 			break;
49f626d6dfSLinus Walleij 	}
50f626d6dfSLinus Walleij 	return ret ? ret : -ENOENT;
51f626d6dfSLinus Walleij }
52af8b6375SAlexandre Courbot 
53c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
543d0f7cf0SGrant Likely {
55c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
56c7e9d398SMasahiro Yamada 
57c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
58d49b48f0SVincent Whitchurch 				chip->of_xlate &&
59c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
607b8792bbSHans Holmberg }
613d0f7cf0SGrant Likely 
62c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
63c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
64762c2e46SMasahiro Yamada {
65c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
663d0f7cf0SGrant Likely }
673d0f7cf0SGrant Likely 
6899468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
6999468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
7099468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
7199468c1aSMasahiro Yamada {
7299468c1aSMasahiro Yamada 	int ret;
7399468c1aSMasahiro Yamada 
7499468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
7599468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
7699468c1aSMasahiro Yamada 
7799468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
7899468c1aSMasahiro Yamada 	if (ret < 0)
7999468c1aSMasahiro Yamada 		return ERR_PTR(ret);
8099468c1aSMasahiro Yamada 
8199468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
82f141ed65SGrant Likely }
83f141ed65SGrant Likely 
84f626d6dfSLinus Walleij /**
85f626d6dfSLinus Walleij  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
86f626d6dfSLinus Walleij  * to set the .valid_mask
8714e8c535SRandy Dunlap  * @gc: the target gpio_chip
8814e8c535SRandy Dunlap  *
8914e8c535SRandy Dunlap  * Return: true if the valid mask needs to be set
90f626d6dfSLinus Walleij  */
9149281a22SStephen Boyd bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
92f626d6dfSLinus Walleij {
93f626d6dfSLinus Walleij 	int size;
94f626d6dfSLinus Walleij 	struct device_node *np = gc->of_node;
95f626d6dfSLinus Walleij 
96f626d6dfSLinus Walleij 	size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
97f626d6dfSLinus Walleij 	if (size > 0 && size % 2 == 0)
98f626d6dfSLinus Walleij 		return true;
99f626d6dfSLinus Walleij 	return false;
100f626d6dfSLinus Walleij }
101f626d6dfSLinus Walleij 
102a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np,
10389a5e15bSLinus Walleij 				 const char *propname,
1046953c57aSLinus Walleij 				 enum of_gpio_flags *flags,
1056953c57aSLinus Walleij 				 int index)
106a603a2b8SLinus Walleij {
107a603a2b8SLinus Walleij 	/*
10881c85ec1SLinus Walleij 	 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
10981c85ec1SLinus Walleij 	 */
11081c85ec1SLinus Walleij 	if (IS_ENABLED(CONFIG_MMC)) {
11181c85ec1SLinus Walleij 		/*
11281c85ec1SLinus Walleij 		 * Active low is the default according to the
11389a5e15bSLinus Walleij 		 * SDHCI specification and the device tree
11489a5e15bSLinus Walleij 		 * bindings. However the code in the current
11589a5e15bSLinus Walleij 		 * kernel was written such that the phandle
11689a5e15bSLinus Walleij 		 * flags were always respected, and "cd-inverted"
11789a5e15bSLinus Walleij 		 * would invert the flag from the device phandle.
11881c85ec1SLinus Walleij 		 */
11989a5e15bSLinus Walleij 		if (!strcmp(propname, "cd-gpios")) {
12089a5e15bSLinus Walleij 			if (of_property_read_bool(np, "cd-inverted"))
12189a5e15bSLinus Walleij 				*flags ^= OF_GPIO_ACTIVE_LOW;
12281c85ec1SLinus Walleij 		}
12389a5e15bSLinus Walleij 		if (!strcmp(propname, "wp-gpios")) {
12489a5e15bSLinus Walleij 			if (of_property_read_bool(np, "wp-inverted"))
12589a5e15bSLinus Walleij 				*flags ^= OF_GPIO_ACTIVE_LOW;
12681c85ec1SLinus Walleij 		}
12781c85ec1SLinus Walleij 	}
12881c85ec1SLinus Walleij 	/*
129a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
130a603a2b8SLinus Walleij 	 * Note that active low is the default.
131a603a2b8SLinus Walleij 	 */
132a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
133906402a4SLinus Walleij 	    (of_device_is_compatible(np, "regulator-fixed") ||
134906402a4SLinus Walleij 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
135a71a81e7SGeert Uytterhoeven 	     (!(strcmp(propname, "enable-gpio") &&
136a71a81e7SGeert Uytterhoeven 		strcmp(propname, "enable-gpios")) &&
137a71a81e7SGeert Uytterhoeven 	      of_device_is_compatible(np, "regulator-gpio")))) {
138a603a2b8SLinus Walleij 		/*
139a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
140a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
141a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
142a603a2b8SLinus Walleij 		 * be actively ignored.
143a603a2b8SLinus Walleij 		 */
144a603a2b8SLinus Walleij 		if (*flags & OF_GPIO_ACTIVE_LOW) {
145a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
146a603a2b8SLinus Walleij 				of_node_full_name(np));
147a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
148a603a2b8SLinus Walleij 		}
149a603a2b8SLinus Walleij 		if (!of_property_read_bool(np, "enable-active-high"))
150a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
151a603a2b8SLinus Walleij 	}
152a603a2b8SLinus Walleij 	/*
153a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
154a603a2b8SLinus Walleij 	 */
155a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
156a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
157a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
158a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
159a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
160a603a2b8SLinus Walleij 			of_node_full_name(np));
161a603a2b8SLinus Walleij 	}
1626953c57aSLinus Walleij 
1636953c57aSLinus Walleij 	/*
1646953c57aSLinus Walleij 	 * Legacy handling of SPI active high chip select. If we have a
1656953c57aSLinus Walleij 	 * property named "cs-gpios" we need to inspect the child node
1666953c57aSLinus Walleij 	 * to determine if the flags should have inverted semantics.
1676953c57aSLinus Walleij 	 */
168da7f1349SLinus Walleij 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
169a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
1706953c57aSLinus Walleij 		struct device_node *child;
1716953c57aSLinus Walleij 		u32 cs;
1726953c57aSLinus Walleij 		int ret;
1736953c57aSLinus Walleij 
1746953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
1756953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
176c1c04ceaSLinus Walleij 			if (ret)
1776953c57aSLinus Walleij 				continue;
1786953c57aSLinus Walleij 			if (cs == index) {
1796953c57aSLinus Walleij 				/*
1806953c57aSLinus Walleij 				 * SPI children have active low chip selects
1816953c57aSLinus Walleij 				 * by default. This can be specified negatively
1826953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
1836953c57aSLinus Walleij 				 * device node, or actively by tagging on
1846953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
1856953c57aSLinus Walleij 				 * tree. If the line is simultaneously
1866953c57aSLinus Walleij 				 * tagged as active low in the device tree
1876953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
1886953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
1896953c57aSLinus Walleij 				 * take precedence.
1906953c57aSLinus Walleij 				 */
1917ce40277SAndrey Smirnov 				if (of_property_read_bool(child, "spi-cs-high")) {
1926953c57aSLinus Walleij 					if (*flags & OF_GPIO_ACTIVE_LOW) {
1936953c57aSLinus Walleij 						pr_warn("%s GPIO handle specifies active low - ignored\n",
1947ce40277SAndrey Smirnov 							of_node_full_name(child));
1956953c57aSLinus Walleij 						*flags &= ~OF_GPIO_ACTIVE_LOW;
1966953c57aSLinus Walleij 					}
1976953c57aSLinus Walleij 				} else {
1986953c57aSLinus Walleij 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
1996953c57aSLinus Walleij 						pr_info("%s enforce active low on chipselect handle\n",
2007ce40277SAndrey Smirnov 							of_node_full_name(child));
2016953c57aSLinus Walleij 					*flags |= OF_GPIO_ACTIVE_LOW;
2026953c57aSLinus Walleij 				}
20389fea04cSNishka Dasgupta 				of_node_put(child);
2046953c57aSLinus Walleij 				break;
2056953c57aSLinus Walleij 			}
2066953c57aSLinus Walleij 		}
2076953c57aSLinus Walleij 	}
208edc1ef3fSMartin Blumenstingl 
209edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
210edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
211edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
212edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
213edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
214a603a2b8SLinus Walleij }
215a603a2b8SLinus Walleij 
216f141ed65SGrant Likely /**
217af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
218f141ed65SGrant Likely  * @np:		device node to get GPIO from
219f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
220f141ed65SGrant Likely  * @index:	index of the GPIO
221f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
222f141ed65SGrant Likely  *
223af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
224f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
225f141ed65SGrant Likely  * in flags for the GPIO.
226f141ed65SGrant Likely  */
227c83d3c77SGeert Uytterhoeven static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
228af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
229f141ed65SGrant Likely {
230762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
231762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
232762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
233f141ed65SGrant Likely 	int ret;
234f141ed65SGrant Likely 
235c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
236762c2e46SMasahiro Yamada 					     &gpiospec);
2373d0f7cf0SGrant Likely 	if (ret) {
2387eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
2397eb6ce2fSRob Herring 			__func__, propname, np, index);
240af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
2413d0f7cf0SGrant Likely 	}
242f141ed65SGrant Likely 
243c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
244762c2e46SMasahiro Yamada 	if (!chip) {
245762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
246762c2e46SMasahiro Yamada 		goto out;
247762c2e46SMasahiro Yamada 	}
2483d0f7cf0SGrant Likely 
24999468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
250762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
251762c2e46SMasahiro Yamada 		goto out;
252762c2e46SMasahiro Yamada 
253605f2d34SLinus Walleij 	if (flags)
25489a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
255a603a2b8SLinus Walleij 
2567eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
2577eb6ce2fSRob Herring 		 __func__, propname, np, index,
258762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
259762c2e46SMasahiro Yamada 
260762c2e46SMasahiro Yamada out:
261762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
262762c2e46SMasahiro Yamada 
263762c2e46SMasahiro Yamada 	return desc;
264f141ed65SGrant Likely }
265f141ed65SGrant Likely 
266f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
267f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
268f01d9075SAlexandre Courbot {
269f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
270f01d9075SAlexandre Courbot 
271f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
272f01d9075SAlexandre Courbot 
273f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
274f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
275f01d9075SAlexandre Courbot 	else
276f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
277f01d9075SAlexandre Courbot }
2786d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
279f01d9075SAlexandre Courbot 
280f626d6dfSLinus Walleij /**
281f626d6dfSLinus Walleij  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
282f626d6dfSLinus Walleij  * @node:	handle of the OF node
283f626d6dfSLinus Walleij  * @propname:	name of the DT property representing the GPIO
284f626d6dfSLinus Walleij  * @index:	index of the GPIO to obtain for the consumer
285f626d6dfSLinus Walleij  * @dflags:	GPIO initialization flags
286f626d6dfSLinus Walleij  * @label:	label to attach to the requested GPIO
287f626d6dfSLinus Walleij  *
288f626d6dfSLinus Walleij  * Returns:
289f626d6dfSLinus Walleij  * On successful request the GPIO pin is configured in accordance with
290f626d6dfSLinus Walleij  * provided @dflags.
291f626d6dfSLinus Walleij  *
292f626d6dfSLinus Walleij  * In case of error an ERR_PTR() is returned.
293f626d6dfSLinus Walleij  */
294f626d6dfSLinus Walleij struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
295f626d6dfSLinus Walleij 					 const char *propname, int index,
296f626d6dfSLinus Walleij 					 enum gpiod_flags dflags,
297f626d6dfSLinus Walleij 					 const char *label)
298f626d6dfSLinus Walleij {
299f626d6dfSLinus Walleij 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
300f626d6dfSLinus Walleij 	struct gpio_desc *desc;
301f626d6dfSLinus Walleij 	enum of_gpio_flags flags;
302f626d6dfSLinus Walleij 	bool active_low = false;
303f626d6dfSLinus Walleij 	bool single_ended = false;
304f626d6dfSLinus Walleij 	bool open_drain = false;
305f626d6dfSLinus Walleij 	bool transitory = false;
306f626d6dfSLinus Walleij 	int ret;
307f626d6dfSLinus Walleij 
308f626d6dfSLinus Walleij 	desc = of_get_named_gpiod_flags(node, propname,
309f626d6dfSLinus Walleij 					index, &flags);
310f626d6dfSLinus Walleij 
311f626d6dfSLinus Walleij 	if (!desc || IS_ERR(desc)) {
312f626d6dfSLinus Walleij 		return desc;
313f626d6dfSLinus Walleij 	}
314f626d6dfSLinus Walleij 
315f626d6dfSLinus Walleij 	active_low = flags & OF_GPIO_ACTIVE_LOW;
316f626d6dfSLinus Walleij 	single_ended = flags & OF_GPIO_SINGLE_ENDED;
317f626d6dfSLinus Walleij 	open_drain = flags & OF_GPIO_OPEN_DRAIN;
318f626d6dfSLinus Walleij 	transitory = flags & OF_GPIO_TRANSITORY;
319f626d6dfSLinus Walleij 
320f626d6dfSLinus Walleij 	ret = gpiod_request(desc, label);
321f626d6dfSLinus Walleij 	if (ret == -EBUSY && (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
322f626d6dfSLinus Walleij 		return desc;
323f626d6dfSLinus Walleij 	if (ret)
324f626d6dfSLinus Walleij 		return ERR_PTR(ret);
325f626d6dfSLinus Walleij 
326f626d6dfSLinus Walleij 	if (active_low)
327f626d6dfSLinus Walleij 		lflags |= GPIO_ACTIVE_LOW;
328f626d6dfSLinus Walleij 
329f626d6dfSLinus Walleij 	if (single_ended) {
330f626d6dfSLinus Walleij 		if (open_drain)
331f626d6dfSLinus Walleij 			lflags |= GPIO_OPEN_DRAIN;
332f626d6dfSLinus Walleij 		else
333f626d6dfSLinus Walleij 			lflags |= GPIO_OPEN_SOURCE;
334f626d6dfSLinus Walleij 	}
335f626d6dfSLinus Walleij 
336f626d6dfSLinus Walleij 	if (transitory)
337f626d6dfSLinus Walleij 		lflags |= GPIO_TRANSITORY;
338f626d6dfSLinus Walleij 
339f626d6dfSLinus Walleij 	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
340f626d6dfSLinus Walleij 	if (ret < 0) {
341f626d6dfSLinus Walleij 		gpiod_put(desc);
342f626d6dfSLinus Walleij 		return ERR_PTR(ret);
343f626d6dfSLinus Walleij 	}
344f626d6dfSLinus Walleij 
345f626d6dfSLinus Walleij 	return desc;
346f626d6dfSLinus Walleij }
3476d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
348f141ed65SGrant Likely 
349c8582339SLinus Walleij /*
350c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
351c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
352c8582339SLinus Walleij  * them.
353c8582339SLinus Walleij  */
354c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
355c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
356c8582339SLinus Walleij {
357c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
358c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
359c8582339SLinus Walleij 	struct gpio_desc *desc;
360c8582339SLinus Walleij 
361c8582339SLinus Walleij 	/*
362c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
363c8582339SLinus Walleij 	 * is false.
364c8582339SLinus Walleij 	 */
365c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
366c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
367c8582339SLinus Walleij 
368c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
369c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
370c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
371c8582339SLinus Walleij 
372c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
373c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
374c8582339SLinus Walleij 
375c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
376c8582339SLinus Walleij 	return desc;
377c8582339SLinus Walleij }
378c8582339SLinus Walleij 
3796a537d48SLinus Walleij /*
380e3023bf8SLinus Walleij  * The old Freescale bindings use simply "gpios" as name for the chip select
381e3023bf8SLinus Walleij  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
382e3023bf8SLinus Walleij  * with a special quirk.
383e3023bf8SLinus Walleij  */
384e3023bf8SLinus Walleij static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
385e3023bf8SLinus Walleij 					     const char *con_id,
386e3023bf8SLinus Walleij 					     unsigned int idx,
387e3023bf8SLinus Walleij 					     unsigned long *flags)
388e3023bf8SLinus Walleij {
389e3023bf8SLinus Walleij 	struct device_node *np = dev->of_node;
390e3023bf8SLinus Walleij 
391e3023bf8SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
392e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
393e3023bf8SLinus Walleij 
394e3023bf8SLinus Walleij 	/* Allow this specifically for Freescale devices */
395e3023bf8SLinus Walleij 	if (!of_device_is_compatible(np, "fsl,spi") &&
396e3023bf8SLinus Walleij 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
397e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
398e3023bf8SLinus Walleij 	/* Allow only if asking for "cs-gpios" */
399e3023bf8SLinus Walleij 	if (!con_id || strcmp(con_id, "cs"))
400e3023bf8SLinus Walleij 		return ERR_PTR(-ENOENT);
401e3023bf8SLinus Walleij 
402e3023bf8SLinus Walleij 	/*
403e3023bf8SLinus Walleij 	 * While all other SPI controllers use "cs-gpios" the Freescale
404e3023bf8SLinus Walleij 	 * uses just "gpios" so translate to that when "cs-gpios" is
405e3023bf8SLinus Walleij 	 * requested.
406e3023bf8SLinus Walleij 	 */
407e3023bf8SLinus Walleij 	return of_find_gpio(dev, NULL, idx, flags);
408e3023bf8SLinus Walleij }
409e3023bf8SLinus Walleij 
410e3023bf8SLinus Walleij /*
4116a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
4126a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
4136a537d48SLinus Walleij  * them.
4146a537d48SLinus Walleij  */
4156a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
4166a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
4176a537d48SLinus Walleij {
4186a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
4196a537d48SLinus Walleij 	const char *whitelist[] = {
4206a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
4216a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
4226a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
4236a537d48SLinus Walleij 	};
4246a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
4256a537d48SLinus Walleij 	struct gpio_desc *desc;
4266a537d48SLinus Walleij 	int i;
4276a537d48SLinus Walleij 
4286a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
4296a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4306a537d48SLinus Walleij 
4316a537d48SLinus Walleij 	if (!con_id)
4326a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4336a537d48SLinus Walleij 
4344b21f94aSAndy Shevchenko 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
4354b21f94aSAndy Shevchenko 	if (i < 0)
4366a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
4376a537d48SLinus Walleij 
4386a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
4396a537d48SLinus Walleij 	return desc;
4406a537d48SLinus Walleij }
4416a537d48SLinus Walleij 
44211c43bb0SDmitry Torokhov static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
44311c43bb0SDmitry Torokhov 					      const char *con_id,
44411c43bb0SDmitry Torokhov 					      enum of_gpio_flags *of_flags)
44511c43bb0SDmitry Torokhov {
44611c43bb0SDmitry Torokhov 	if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
44711c43bb0SDmitry Torokhov 		return ERR_PTR(-ENOENT);
44811c43bb0SDmitry Torokhov 
44911c43bb0SDmitry Torokhov 	if (!con_id || strcmp(con_id, "wlf,reset"))
45011c43bb0SDmitry Torokhov 		return ERR_PTR(-ENOENT);
45111c43bb0SDmitry Torokhov 
45211c43bb0SDmitry Torokhov 	return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
45311c43bb0SDmitry Torokhov }
45411c43bb0SDmitry Torokhov 
455ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
456fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
457ea713bc4SLinus Walleij {
458ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
459ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
460ea713bc4SLinus Walleij 	struct gpio_desc *desc;
461ea713bc4SLinus Walleij 	unsigned int i;
462ea713bc4SLinus Walleij 
463c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
464ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
465ea713bc4SLinus Walleij 		if (con_id)
466ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
467ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
468ea713bc4SLinus Walleij 		else
469ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
470ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
471ea713bc4SLinus Walleij 
472ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
473ea713bc4SLinus Walleij 						&of_flags);
4746662ae6aSMaxime Ripard 
4751dea33e8SDmitry Torokhov 		if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
476ea713bc4SLinus Walleij 			break;
477ea713bc4SLinus Walleij 	}
478ea713bc4SLinus Walleij 
4791dea33e8SDmitry Torokhov 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
480c8582339SLinus Walleij 		/* Special handling for SPI GPIOs if used */
481c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
4821dea33e8SDmitry Torokhov 	}
4831dea33e8SDmitry Torokhov 
4841dea33e8SDmitry Torokhov 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
485e3023bf8SLinus Walleij 		/* This quirk looks up flags and all */
486e3023bf8SLinus Walleij 		desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
487e3023bf8SLinus Walleij 		if (!IS_ERR(desc))
488e3023bf8SLinus Walleij 			return desc;
489e3023bf8SLinus Walleij 	}
490c8582339SLinus Walleij 
4911dea33e8SDmitry Torokhov 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
4926a537d48SLinus Walleij 		/* Special handling for regulator GPIOs if used */
4936a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
4941dea33e8SDmitry Torokhov 	}
4956a537d48SLinus Walleij 
49611c43bb0SDmitry Torokhov 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
49711c43bb0SDmitry Torokhov 		desc = of_find_arizona_gpio(dev, con_id, &of_flags);
49811c43bb0SDmitry Torokhov 
499ea713bc4SLinus Walleij 	if (IS_ERR(desc))
500ea713bc4SLinus Walleij 		return desc;
501ea713bc4SLinus Walleij 
502ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
503ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
504ea713bc4SLinus Walleij 
505ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
5064c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
507ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
508ea713bc4SLinus Walleij 		else
509ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
510ea713bc4SLinus Walleij 	}
511ea713bc4SLinus Walleij 
512e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
513e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
51405f479bfSCharles Keepax 
515d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_UP)
516d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_UP;
517d449991cSThomas Petazzoni 	if (of_flags & OF_GPIO_PULL_DOWN)
518d449991cSThomas Petazzoni 		*flags |= GPIO_PULL_DOWN;
519d449991cSThomas Petazzoni 
520ea713bc4SLinus Walleij 	return desc;
521ea713bc4SLinus Walleij }
522ea713bc4SLinus Walleij 
523f141ed65SGrant Likely /**
524fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
525f625d460SBenoit Parrot  * @np:		device node to get GPIO from
526be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
527a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
528f625d460SBenoit Parrot  * @name:	GPIO line name
529fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
530fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
531f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
532f625d460SBenoit Parrot  *
533f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
534f625d460SBenoit Parrot  * value on the error condition.
535f625d460SBenoit Parrot  */
536fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
537be715343SMasahiro Yamada 					   struct gpio_chip *chip,
538a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
539fed7026aSAndy Shevchenko 					   unsigned long *lflags,
540f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
541f625d460SBenoit Parrot {
542f625d460SBenoit Parrot 	struct device_node *chip_np;
543f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
544be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
545be715343SMasahiro Yamada 	struct gpio_desc *desc;
546a79fead5SGeert Uytterhoeven 	unsigned int i;
547f625d460SBenoit Parrot 	u32 tmp;
5483f9547e1SMasahiro Yamada 	int ret;
549f625d460SBenoit Parrot 
550be715343SMasahiro Yamada 	chip_np = chip->of_node;
551f625d460SBenoit Parrot 	if (!chip_np)
552f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
553f625d460SBenoit Parrot 
554f625d460SBenoit Parrot 	xlate_flags = 0;
5552d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
556f625d460SBenoit Parrot 	*dflags = 0;
557f625d460SBenoit Parrot 
558f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
559f625d460SBenoit Parrot 	if (ret)
560f625d460SBenoit Parrot 		return ERR_PTR(ret);
561f625d460SBenoit Parrot 
562be715343SMasahiro Yamada 	gpiospec.np = chip_np;
563be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
564f625d460SBenoit Parrot 
565a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
566a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
567a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
568f625d460SBenoit Parrot 		if (ret)
569f625d460SBenoit Parrot 			return ERR_PTR(ret);
570a79fead5SGeert Uytterhoeven 	}
571f625d460SBenoit Parrot 
57299468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
573be715343SMasahiro Yamada 	if (IS_ERR(desc))
574be715343SMasahiro Yamada 		return desc;
575f625d460SBenoit Parrot 
576f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
577f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
578e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
579e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
580f625d460SBenoit Parrot 
581f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
582f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
583f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
584f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
585f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
586f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
587f625d460SBenoit Parrot 	else {
58862cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
58962cdcb6cSRob Herring 			desc_to_gpio(desc), np);
590f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
591f625d460SBenoit Parrot 	}
592f625d460SBenoit Parrot 
593f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
594f625d460SBenoit Parrot 		*name = np->name;
595f625d460SBenoit Parrot 
596be715343SMasahiro Yamada 	return desc;
597f625d460SBenoit Parrot }
598f625d460SBenoit Parrot 
599f625d460SBenoit Parrot /**
600fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
601f625d460SBenoit Parrot  * @chip:	gpio chip to act on
602f625d460SBenoit Parrot  *
603f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
604f625d460SBenoit Parrot  * configuration.
605ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
606f625d460SBenoit Parrot  */
607dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
608f625d460SBenoit Parrot {
609f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
610f625d460SBenoit Parrot 	struct device_node *np;
611f625d460SBenoit Parrot 	const char *name;
612fed7026aSAndy Shevchenko 	unsigned long lflags;
613f625d460SBenoit Parrot 	enum gpiod_flags dflags;
614a79fead5SGeert Uytterhoeven 	unsigned int i;
615dfbd379bSLaxman Dewangan 	int ret;
616f625d460SBenoit Parrot 
617d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
618f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
619f625d460SBenoit Parrot 			continue;
620f625d460SBenoit Parrot 
621a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
622a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
623a79fead5SGeert Uytterhoeven 						 &dflags);
624f625d460SBenoit Parrot 			if (IS_ERR(desc))
625a79fead5SGeert Uytterhoeven 				break;
626f625d460SBenoit Parrot 
627dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
62809e258afSWei Yongjun 			if (ret < 0) {
62909e258afSWei Yongjun 				of_node_put(np);
630dfbd379bSLaxman Dewangan 				return ret;
631f625d460SBenoit Parrot 			}
63209e258afSWei Yongjun 		}
633a79fead5SGeert Uytterhoeven 	}
634dfbd379bSLaxman Dewangan 
635dfbd379bSLaxman Dewangan 	return 0;
636f625d460SBenoit Parrot }
637f625d460SBenoit Parrot 
638f625d460SBenoit Parrot /**
63967049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
640f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
64167049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
642f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
643f141ed65SGrant Likely  *
644f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
64567049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
646f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
647f141ed65SGrant Likely  */
648b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc,
649b0c7e73bSGeert Uytterhoeven 				const struct of_phandle_args *gpiospec,
650b0c7e73bSGeert Uytterhoeven 				u32 *flags)
651f141ed65SGrant Likely {
652f141ed65SGrant Likely 	/*
653f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
65420a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
655f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
656f141ed65SGrant Likely 	 * but not recommended).
657f141ed65SGrant Likely 	 */
658f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
659f141ed65SGrant Likely 		WARN_ON(1);
660f141ed65SGrant Likely 		return -EINVAL;
661f141ed65SGrant Likely 	}
662f141ed65SGrant Likely 
663f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
664f141ed65SGrant Likely 		return -EINVAL;
665f141ed65SGrant Likely 
6667b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
667f141ed65SGrant Likely 		return -EINVAL;
668f141ed65SGrant Likely 
669f141ed65SGrant Likely 	if (flags)
670f141ed65SGrant Likely 		*flags = gpiospec->args[1];
671f141ed65SGrant Likely 
672f141ed65SGrant Likely 	return gpiospec->args[0];
673f141ed65SGrant Likely }
674f141ed65SGrant Likely 
675f141ed65SGrant Likely /**
6763208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
677f141ed65SGrant Likely  * @np:		device node of the GPIO chip
678f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
6793208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
680f141ed65SGrant Likely  *
681f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
682f141ed65SGrant Likely  *
683f141ed65SGrant Likely  * 1) In the gpio_chip structure:
684f141ed65SGrant Likely  *    - all the callbacks
685f141ed65SGrant Likely  *    - of_gpio_n_cells
686f141ed65SGrant Likely  *    - of_xlate callback (optional)
687f141ed65SGrant Likely  *
688f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
689f141ed65SGrant Likely  *    - save_regs callback (optional)
690f141ed65SGrant Likely  *
691f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
692f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
693f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
694f141ed65SGrant Likely  */
6953208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
6963208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
6973208b0f0SLinus Walleij 			    void *data)
698f141ed65SGrant Likely {
699f141ed65SGrant Likely 	int ret = -ENOMEM;
700f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
701f141ed65SGrant Likely 
7027eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
703f141ed65SGrant Likely 	if (!gc->label)
704f141ed65SGrant Likely 		goto err0;
705f141ed65SGrant Likely 
706f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
707f141ed65SGrant Likely 	if (!mm_gc->regs)
708f141ed65SGrant Likely 		goto err1;
709f141ed65SGrant Likely 
710f141ed65SGrant Likely 	gc->base = -1;
711f141ed65SGrant Likely 
712f141ed65SGrant Likely 	if (mm_gc->save_regs)
713f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
714f141ed65SGrant Likely 
715f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
716f141ed65SGrant Likely 
7173208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
718f141ed65SGrant Likely 	if (ret)
719f141ed65SGrant Likely 		goto err2;
720f141ed65SGrant Likely 
721f141ed65SGrant Likely 	return 0;
722f141ed65SGrant Likely err2:
723f141ed65SGrant Likely 	iounmap(mm_gc->regs);
724f141ed65SGrant Likely err1:
725f141ed65SGrant Likely 	kfree(gc->label);
726f141ed65SGrant Likely err0:
7277eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
728f141ed65SGrant Likely 	return ret;
729f141ed65SGrant Likely }
7306d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
731f141ed65SGrant Likely 
732d621e8baSRicardo Ribalda Delgado /**
733d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
734d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
735d621e8baSRicardo Ribalda Delgado  */
736d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
737d621e8baSRicardo Ribalda Delgado {
738d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
739d621e8baSRicardo Ribalda Delgado 
740d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
741d621e8baSRicardo Ribalda Delgado 		return;
742d621e8baSRicardo Ribalda Delgado 
743d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
744d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
745d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
746d621e8baSRicardo Ribalda Delgado }
7476d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
748d621e8baSRicardo Ribalda Delgado 
749726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
750726cb3baSStephen Boyd {
751726cb3baSStephen Boyd 	int len, i;
752726cb3baSStephen Boyd 	u32 start, count;
753726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
754726cb3baSStephen Boyd 
755726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
756726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
757726cb3baSStephen Boyd 		return;
758726cb3baSStephen Boyd 
759726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
760726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
761726cb3baSStephen Boyd 					   i, &start);
762726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
763726cb3baSStephen Boyd 					   i + 1, &count);
764726cb3baSStephen Boyd 		if (start >= chip->ngpio || start + count >= chip->ngpio)
765726cb3baSStephen Boyd 			continue;
766726cb3baSStephen Boyd 
767726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
768726cb3baSStephen Boyd 	}
769726cb3baSStephen Boyd };
770726cb3baSStephen Boyd 
771f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
77228355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
773f23f1516SShiraz Hashim {
774f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
775f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
7761e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
777f23f1516SShiraz Hashim 	int index = 0, ret;
778586a87e6SChristian Ruppert 	const char *name;
779586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
780586a87e6SChristian Ruppert 	struct property *group_names;
781f23f1516SShiraz Hashim 
782f23f1516SShiraz Hashim 	if (!np)
78328355f81STomeu Vizoso 		return 0;
784f23f1516SShiraz Hashim 
785586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
786586a87e6SChristian Ruppert 
787ad4e1a7cSHaojian Zhuang 	for (;; index++) {
788d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
789d9fe0039SStephen Warren 				index, &pinspec);
790f23f1516SShiraz Hashim 		if (ret)
791f23f1516SShiraz Hashim 			break;
792f23f1516SShiraz Hashim 
7931e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
794602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
7951e63d7b9SLinus Walleij 		if (!pctldev)
79628355f81STomeu Vizoso 			return -EPROBE_DEFER;
797f23f1516SShiraz Hashim 
798586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
799586a87e6SChristian Ruppert 			if (group_names) {
80072858602SLaurent Navet 				of_property_read_string_index(np,
801586a87e6SChristian Ruppert 						group_names_propname,
802586a87e6SChristian Ruppert 						index, &name);
803586a87e6SChristian Ruppert 				if (strlen(name)) {
8047eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
8057eb6ce2fSRob Herring 						np);
806586a87e6SChristian Ruppert 					break;
807586a87e6SChristian Ruppert 				}
808586a87e6SChristian Ruppert 			}
809586a87e6SChristian Ruppert 			/* npins != 0: linear range */
8101e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
811ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
8121e63d7b9SLinus Walleij 					pinspec.args[0],
81386853c83SHaojian Zhuang 					pinspec.args[1],
81486853c83SHaojian Zhuang 					pinspec.args[2]);
8151e63d7b9SLinus Walleij 			if (ret)
81628355f81STomeu Vizoso 				return ret;
817586a87e6SChristian Ruppert 		} else {
818586a87e6SChristian Ruppert 			/* npins == 0: special range */
819586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
8207eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
8217eb6ce2fSRob Herring 					np);
822586a87e6SChristian Ruppert 				break;
823586a87e6SChristian Ruppert 			}
824586a87e6SChristian Ruppert 
825586a87e6SChristian Ruppert 			if (!group_names) {
8267eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
8277eb6ce2fSRob Herring 					np, group_names_propname);
828586a87e6SChristian Ruppert 				break;
829586a87e6SChristian Ruppert 			}
830586a87e6SChristian Ruppert 
831586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
832586a87e6SChristian Ruppert 						group_names_propname,
833586a87e6SChristian Ruppert 						index, &name);
834586a87e6SChristian Ruppert 			if (ret)
835586a87e6SChristian Ruppert 				break;
836586a87e6SChristian Ruppert 
837586a87e6SChristian Ruppert 			if (!strlen(name)) {
8387eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
8397eb6ce2fSRob Herring 				np);
840586a87e6SChristian Ruppert 				break;
841586a87e6SChristian Ruppert 			}
842586a87e6SChristian Ruppert 
843586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
844586a87e6SChristian Ruppert 						pinspec.args[0], name);
845586a87e6SChristian Ruppert 			if (ret)
84628355f81STomeu Vizoso 				return ret;
847586a87e6SChristian Ruppert 		}
848ad4e1a7cSHaojian Zhuang 	}
84928355f81STomeu Vizoso 
85028355f81STomeu Vizoso 	return 0;
851f23f1516SShiraz Hashim }
852f23f1516SShiraz Hashim 
853f23f1516SShiraz Hashim #else
85428355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
855f23f1516SShiraz Hashim #endif
856f23f1516SShiraz Hashim 
85728355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
858f141ed65SGrant Likely {
859f0d1ab05SLinus Walleij 	int ret;
86028355f81STomeu Vizoso 
861f141ed65SGrant Likely 	if (!chip->of_node)
86228355f81STomeu Vizoso 		return 0;
863f141ed65SGrant Likely 
864f141ed65SGrant Likely 	if (!chip->of_xlate) {
865f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
866f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
867f141ed65SGrant Likely 	}
868f141ed65SGrant Likely 
8691020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
8701020dfd1SMasahiro Yamada 		return -EINVAL;
8711020dfd1SMasahiro Yamada 
872726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
873726cb3baSStephen Boyd 
874f0d1ab05SLinus Walleij 	ret = of_gpiochip_add_pin_range(chip);
875f0d1ab05SLinus Walleij 	if (ret)
876f0d1ab05SLinus Walleij 		return ret;
87728355f81STomeu Vizoso 
878fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
879fd9c5531SLinus Walleij 	if (!chip->names)
88082270335SChristophe Leroy 		devprop_gpiochip_set_names(chip,
88182270335SChristophe Leroy 					   of_fwnode_handle(chip->of_node));
882fd9c5531SLinus Walleij 
883f141ed65SGrant Likely 	of_node_get(chip->of_node);
884f625d460SBenoit Parrot 
885f0d1ab05SLinus Walleij 	ret = of_gpiochip_scan_gpios(chip);
886f0d1ab05SLinus Walleij 	if (ret) {
887f7299d44SGeert Uytterhoeven 		of_node_put(chip->of_node);
888f7299d44SGeert Uytterhoeven 		gpiochip_remove_pin_ranges(chip);
889f7299d44SGeert Uytterhoeven 	}
890f7299d44SGeert Uytterhoeven 
891f0d1ab05SLinus Walleij 	return ret;
892f141ed65SGrant Likely }
893f141ed65SGrant Likely 
894f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
895f141ed65SGrant Likely {
896e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
897f141ed65SGrant Likely 	of_node_put(chip->of_node);
898f141ed65SGrant Likely }
899