xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision d49b48f0)
1f141ed65SGrant Likely /*
2f141ed65SGrant Likely  * OF helpers for the GPIO API
3f141ed65SGrant Likely  *
4f141ed65SGrant Likely  * Copyright (c) 2007-2008  MontaVista Software, Inc.
5f141ed65SGrant Likely  *
6f141ed65SGrant Likely  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7f141ed65SGrant Likely  *
8f141ed65SGrant Likely  * This program is free software; you can redistribute it and/or modify
9f141ed65SGrant Likely  * it under the terms of the GNU General Public License as published by
10f141ed65SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
11f141ed65SGrant Likely  * (at your option) any later version.
12f141ed65SGrant Likely  */
13f141ed65SGrant Likely 
14f141ed65SGrant Likely #include <linux/device.h>
15bea4dbeeSSachin Kamat #include <linux/err.h>
16f141ed65SGrant Likely #include <linux/errno.h>
17f141ed65SGrant Likely #include <linux/module.h>
18f141ed65SGrant Likely #include <linux/io.h>
19af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h>
20f141ed65SGrant Likely #include <linux/of.h>
21f141ed65SGrant Likely #include <linux/of_address.h>
22f141ed65SGrant Likely #include <linux/of_gpio.h>
23f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h>
24f141ed65SGrant Likely #include <linux/slab.h>
25f625d460SBenoit Parrot #include <linux/gpio/machine.h>
26f141ed65SGrant Likely 
271bd6b601SAlexandre Courbot #include "gpiolib.h"
28af8b6375SAlexandre Courbot 
29c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
303d0f7cf0SGrant Likely {
31c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
32c7e9d398SMasahiro Yamada 
33c7e9d398SMasahiro Yamada 	return chip->gpiodev->dev.of_node == gpiospec->np &&
34d49b48f0SVincent Whitchurch 				chip->of_xlate &&
35c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
367b8792bbSHans Holmberg }
373d0f7cf0SGrant Likely 
38c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
39c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
40762c2e46SMasahiro Yamada {
41c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
423d0f7cf0SGrant Likely }
433d0f7cf0SGrant Likely 
4499468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
4599468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
4699468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
4799468c1aSMasahiro Yamada {
4899468c1aSMasahiro Yamada 	int ret;
4999468c1aSMasahiro Yamada 
5099468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
5199468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
5299468c1aSMasahiro Yamada 
5399468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
5499468c1aSMasahiro Yamada 	if (ret < 0)
5599468c1aSMasahiro Yamada 		return ERR_PTR(ret);
5699468c1aSMasahiro Yamada 
5799468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
58f141ed65SGrant Likely }
59f141ed65SGrant Likely 
60a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np,
61a603a2b8SLinus Walleij 				 enum of_gpio_flags *flags)
62a603a2b8SLinus Walleij {
63a603a2b8SLinus Walleij 	/*
64a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
65a603a2b8SLinus Walleij 	 * Note that active low is the default.
66a603a2b8SLinus Walleij 	 */
67a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
68906402a4SLinus Walleij 	    (of_device_is_compatible(np, "regulator-fixed") ||
69906402a4SLinus Walleij 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
70a603a2b8SLinus Walleij 	     of_device_is_compatible(np, "regulator-gpio"))) {
71a603a2b8SLinus Walleij 		/*
72a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
73a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
74a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
75a603a2b8SLinus Walleij 		 * be actively ignored.
76a603a2b8SLinus Walleij 		 */
77a603a2b8SLinus Walleij 		if (*flags & OF_GPIO_ACTIVE_LOW) {
78a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
79a603a2b8SLinus Walleij 				of_node_full_name(np));
80a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
81a603a2b8SLinus Walleij 		}
82a603a2b8SLinus Walleij 		if (!of_property_read_bool(np, "enable-active-high"))
83a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
84a603a2b8SLinus Walleij 	}
85a603a2b8SLinus Walleij 	/*
86a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
87a603a2b8SLinus Walleij 	 */
88a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
89a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
90a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
91a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
92a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
93a603a2b8SLinus Walleij 			of_node_full_name(np));
94a603a2b8SLinus Walleij 	}
95a603a2b8SLinus Walleij }
96a603a2b8SLinus Walleij 
97f141ed65SGrant Likely /**
98af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
99f141ed65SGrant Likely  * @np:		device node to get GPIO from
100f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
101f141ed65SGrant Likely  * @index:	index of the GPIO
102f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
103f141ed65SGrant Likely  *
104af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
105f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
106f141ed65SGrant Likely  * in flags for the GPIO.
107f141ed65SGrant Likely  */
108af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
109af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
110f141ed65SGrant Likely {
111762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
112762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
113762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
114f141ed65SGrant Likely 	int ret;
115f141ed65SGrant Likely 
116c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
117762c2e46SMasahiro Yamada 					     &gpiospec);
1183d0f7cf0SGrant Likely 	if (ret) {
1197eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
1207eb6ce2fSRob Herring 			__func__, propname, np, index);
121af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
1223d0f7cf0SGrant Likely 	}
123f141ed65SGrant Likely 
124c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
125762c2e46SMasahiro Yamada 	if (!chip) {
126762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
127762c2e46SMasahiro Yamada 		goto out;
128762c2e46SMasahiro Yamada 	}
1293d0f7cf0SGrant Likely 
13099468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
131762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
132762c2e46SMasahiro Yamada 		goto out;
133762c2e46SMasahiro Yamada 
134605f2d34SLinus Walleij 	if (flags)
135a603a2b8SLinus Walleij 		of_gpio_flags_quirks(np, flags);
136a603a2b8SLinus Walleij 
1377eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
1387eb6ce2fSRob Herring 		 __func__, propname, np, index,
139762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
140762c2e46SMasahiro Yamada 
141762c2e46SMasahiro Yamada out:
142762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
143762c2e46SMasahiro Yamada 
144762c2e46SMasahiro Yamada 	return desc;
145f141ed65SGrant Likely }
146f141ed65SGrant Likely 
147f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
148f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
149f01d9075SAlexandre Courbot {
150f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
151f01d9075SAlexandre Courbot 
152f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
153f01d9075SAlexandre Courbot 
154f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
155f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
156f01d9075SAlexandre Courbot 	else
157f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
158f01d9075SAlexandre Courbot }
159f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
160f01d9075SAlexandre Courbot 
161c8582339SLinus Walleij /*
162c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
163c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
164c8582339SLinus Walleij  * them.
165c8582339SLinus Walleij  */
166c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
167c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
168c8582339SLinus Walleij {
169c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
170c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
171c8582339SLinus Walleij 	struct gpio_desc *desc;
172c8582339SLinus Walleij 
173c8582339SLinus Walleij 	/*
174c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
175c8582339SLinus Walleij 	 * is false.
176c8582339SLinus Walleij 	 */
177c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
178c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
179c8582339SLinus Walleij 
180c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
181c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
182c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
183c8582339SLinus Walleij 
184c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
185c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
186c8582339SLinus Walleij 
187c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
188c8582339SLinus Walleij 	return desc;
189c8582339SLinus Walleij }
190c8582339SLinus Walleij 
1916a537d48SLinus Walleij /*
1926a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
1936a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
1946a537d48SLinus Walleij  * them.
1956a537d48SLinus Walleij  */
1966a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
1976a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
1986a537d48SLinus Walleij {
1996a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
2006a537d48SLinus Walleij 	const char *whitelist[] = {
2016a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
2026a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
2036a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
2046a537d48SLinus Walleij 	};
2056a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
2066a537d48SLinus Walleij 	struct gpio_desc *desc;
2076a537d48SLinus Walleij 	int i;
2086a537d48SLinus Walleij 
2096a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
2106a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2116a537d48SLinus Walleij 
2126a537d48SLinus Walleij 	if (!con_id)
2136a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2146a537d48SLinus Walleij 
2154b21f94aSAndy Shevchenko 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
2164b21f94aSAndy Shevchenko 	if (i < 0)
2176a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2186a537d48SLinus Walleij 
2196a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
2206a537d48SLinus Walleij 	return desc;
2216a537d48SLinus Walleij }
2226a537d48SLinus Walleij 
223ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
224ea713bc4SLinus Walleij 			       unsigned int idx,
225ea713bc4SLinus Walleij 			       enum gpio_lookup_flags *flags)
226ea713bc4SLinus Walleij {
227ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
228ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
229ea713bc4SLinus Walleij 	struct gpio_desc *desc;
230ea713bc4SLinus Walleij 	unsigned int i;
231ea713bc4SLinus Walleij 
232c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
233ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
234ea713bc4SLinus Walleij 		if (con_id)
235ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
236ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
237ea713bc4SLinus Walleij 		else
238ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
239ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
240ea713bc4SLinus Walleij 
241ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
242ea713bc4SLinus Walleij 						&of_flags);
2436662ae6aSMaxime Ripard 		/*
2446662ae6aSMaxime Ripard 		 * -EPROBE_DEFER in our case means that we found a
2456662ae6aSMaxime Ripard 		 * valid GPIO property, but no controller has been
2466662ae6aSMaxime Ripard 		 * registered so far.
2476662ae6aSMaxime Ripard 		 *
2486662ae6aSMaxime Ripard 		 * This means we don't need to look any further for
2496662ae6aSMaxime Ripard 		 * alternate name conventions, and we should really
2506662ae6aSMaxime Ripard 		 * preserve the return code for our user to be able to
2516662ae6aSMaxime Ripard 		 * retry probing later.
2526662ae6aSMaxime Ripard 		 */
2536662ae6aSMaxime Ripard 		if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
2546662ae6aSMaxime Ripard 			return desc;
2556662ae6aSMaxime Ripard 
256ea713bc4SLinus Walleij 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
257ea713bc4SLinus Walleij 			break;
258ea713bc4SLinus Walleij 	}
259ea713bc4SLinus Walleij 
260c8582339SLinus Walleij 	/* Special handling for SPI GPIOs if used */
261c8582339SLinus Walleij 	if (IS_ERR(desc))
262c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
263c8582339SLinus Walleij 
2646a537d48SLinus Walleij 	/* Special handling for regulator GPIOs if used */
265ce27fb2cSChen-Yu Tsai 	if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
2666a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
2676a537d48SLinus Walleij 
268ea713bc4SLinus Walleij 	if (IS_ERR(desc))
269ea713bc4SLinus Walleij 		return desc;
270ea713bc4SLinus Walleij 
271ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
272ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
273ea713bc4SLinus Walleij 
274ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
2754c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
276ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
277ea713bc4SLinus Walleij 		else
278ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
279ea713bc4SLinus Walleij 	}
280ea713bc4SLinus Walleij 
281e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
282e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
28305f479bfSCharles Keepax 
284ea713bc4SLinus Walleij 	return desc;
285ea713bc4SLinus Walleij }
286ea713bc4SLinus Walleij 
287f141ed65SGrant Likely /**
288fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
289f625d460SBenoit Parrot  * @np:		device node to get GPIO from
290be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
291a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
292f625d460SBenoit Parrot  * @name:	GPIO line name
293f625d460SBenoit Parrot  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
294fd7337fdSMarkus Pargmann  *		of_parse_own_gpio()
295f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
296f625d460SBenoit Parrot  *
297f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
298f625d460SBenoit Parrot  * value on the error condition.
299f625d460SBenoit Parrot  */
300fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
301be715343SMasahiro Yamada 					   struct gpio_chip *chip,
302a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
303f625d460SBenoit Parrot 					   enum gpio_lookup_flags *lflags,
304f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
305f625d460SBenoit Parrot {
306f625d460SBenoit Parrot 	struct device_node *chip_np;
307f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
308be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
309be715343SMasahiro Yamada 	struct gpio_desc *desc;
310a79fead5SGeert Uytterhoeven 	unsigned int i;
311f625d460SBenoit Parrot 	u32 tmp;
3123f9547e1SMasahiro Yamada 	int ret;
313f625d460SBenoit Parrot 
314be715343SMasahiro Yamada 	chip_np = chip->of_node;
315f625d460SBenoit Parrot 	if (!chip_np)
316f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
317f625d460SBenoit Parrot 
318f625d460SBenoit Parrot 	xlate_flags = 0;
319f625d460SBenoit Parrot 	*lflags = 0;
320f625d460SBenoit Parrot 	*dflags = 0;
321f625d460SBenoit Parrot 
322f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
323f625d460SBenoit Parrot 	if (ret)
324f625d460SBenoit Parrot 		return ERR_PTR(ret);
325f625d460SBenoit Parrot 
326be715343SMasahiro Yamada 	gpiospec.np = chip_np;
327be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
328f625d460SBenoit Parrot 
329a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
330a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
331a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
332f625d460SBenoit Parrot 		if (ret)
333f625d460SBenoit Parrot 			return ERR_PTR(ret);
334a79fead5SGeert Uytterhoeven 	}
335f625d460SBenoit Parrot 
33699468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
337be715343SMasahiro Yamada 	if (IS_ERR(desc))
338be715343SMasahiro Yamada 		return desc;
339f625d460SBenoit Parrot 
340f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
341f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
342e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
343e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
344f625d460SBenoit Parrot 
345f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
346f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
347f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
348f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
349f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
350f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
351f625d460SBenoit Parrot 	else {
352f625d460SBenoit Parrot 		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
353be715343SMasahiro Yamada 			desc_to_gpio(desc), np->name);
354f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
355f625d460SBenoit Parrot 	}
356f625d460SBenoit Parrot 
357f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
358f625d460SBenoit Parrot 		*name = np->name;
359f625d460SBenoit Parrot 
360be715343SMasahiro Yamada 	return desc;
361f625d460SBenoit Parrot }
362f625d460SBenoit Parrot 
363f625d460SBenoit Parrot /**
364fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
365f625d460SBenoit Parrot  * @chip:	gpio chip to act on
366f625d460SBenoit Parrot  *
367f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
368f625d460SBenoit Parrot  * configuration.
369ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
370f625d460SBenoit Parrot  */
371dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
372f625d460SBenoit Parrot {
373f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
374f625d460SBenoit Parrot 	struct device_node *np;
375f625d460SBenoit Parrot 	const char *name;
376f625d460SBenoit Parrot 	enum gpio_lookup_flags lflags;
377f625d460SBenoit Parrot 	enum gpiod_flags dflags;
378a79fead5SGeert Uytterhoeven 	unsigned int i;
379dfbd379bSLaxman Dewangan 	int ret;
380f625d460SBenoit Parrot 
381d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
382f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
383f625d460SBenoit Parrot 			continue;
384f625d460SBenoit Parrot 
385a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
386a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
387a79fead5SGeert Uytterhoeven 						 &dflags);
388f625d460SBenoit Parrot 			if (IS_ERR(desc))
389a79fead5SGeert Uytterhoeven 				break;
390f625d460SBenoit Parrot 
391dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
39209e258afSWei Yongjun 			if (ret < 0) {
39309e258afSWei Yongjun 				of_node_put(np);
394dfbd379bSLaxman Dewangan 				return ret;
395f625d460SBenoit Parrot 			}
39609e258afSWei Yongjun 		}
397a79fead5SGeert Uytterhoeven 	}
398dfbd379bSLaxman Dewangan 
399dfbd379bSLaxman Dewangan 	return 0;
400f625d460SBenoit Parrot }
401f625d460SBenoit Parrot 
402f625d460SBenoit Parrot /**
40367049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
404f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
40567049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
406f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
407f141ed65SGrant Likely  *
408f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
40967049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
410f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
411f141ed65SGrant Likely  */
412f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
413f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
414f141ed65SGrant Likely {
415f141ed65SGrant Likely 	/*
416f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
41720a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
418f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
419f141ed65SGrant Likely 	 * but not recommended).
420f141ed65SGrant Likely 	 */
421f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
422f141ed65SGrant Likely 		WARN_ON(1);
423f141ed65SGrant Likely 		return -EINVAL;
424f141ed65SGrant Likely 	}
425f141ed65SGrant Likely 
426f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
427f141ed65SGrant Likely 		return -EINVAL;
428f141ed65SGrant Likely 
4297b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
430f141ed65SGrant Likely 		return -EINVAL;
431f141ed65SGrant Likely 
432f141ed65SGrant Likely 	if (flags)
433f141ed65SGrant Likely 		*flags = gpiospec->args[1];
434f141ed65SGrant Likely 
435f141ed65SGrant Likely 	return gpiospec->args[0];
436f141ed65SGrant Likely }
437f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
438f141ed65SGrant Likely 
439f141ed65SGrant Likely /**
4403208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
441f141ed65SGrant Likely  * @np:		device node of the GPIO chip
442f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
4433208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
444f141ed65SGrant Likely  *
445f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
446f141ed65SGrant Likely  *
447f141ed65SGrant Likely  * 1) In the gpio_chip structure:
448f141ed65SGrant Likely  *    - all the callbacks
449f141ed65SGrant Likely  *    - of_gpio_n_cells
450f141ed65SGrant Likely  *    - of_xlate callback (optional)
451f141ed65SGrant Likely  *
452f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
453f141ed65SGrant Likely  *    - save_regs callback (optional)
454f141ed65SGrant Likely  *
455f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
456f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
457f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
458f141ed65SGrant Likely  */
4593208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
4603208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
4613208b0f0SLinus Walleij 			    void *data)
462f141ed65SGrant Likely {
463f141ed65SGrant Likely 	int ret = -ENOMEM;
464f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
465f141ed65SGrant Likely 
4667eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
467f141ed65SGrant Likely 	if (!gc->label)
468f141ed65SGrant Likely 		goto err0;
469f141ed65SGrant Likely 
470f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
471f141ed65SGrant Likely 	if (!mm_gc->regs)
472f141ed65SGrant Likely 		goto err1;
473f141ed65SGrant Likely 
474f141ed65SGrant Likely 	gc->base = -1;
475f141ed65SGrant Likely 
476f141ed65SGrant Likely 	if (mm_gc->save_regs)
477f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
478f141ed65SGrant Likely 
479f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
480f141ed65SGrant Likely 
4813208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
482f141ed65SGrant Likely 	if (ret)
483f141ed65SGrant Likely 		goto err2;
484f141ed65SGrant Likely 
485f141ed65SGrant Likely 	return 0;
486f141ed65SGrant Likely err2:
487f141ed65SGrant Likely 	iounmap(mm_gc->regs);
488f141ed65SGrant Likely err1:
489f141ed65SGrant Likely 	kfree(gc->label);
490f141ed65SGrant Likely err0:
4917eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
492f141ed65SGrant Likely 	return ret;
493f141ed65SGrant Likely }
4943208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
495f141ed65SGrant Likely 
496d621e8baSRicardo Ribalda Delgado /**
497d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
498d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
499d621e8baSRicardo Ribalda Delgado  */
500d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
501d621e8baSRicardo Ribalda Delgado {
502d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
503d621e8baSRicardo Ribalda Delgado 
504d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
505d621e8baSRicardo Ribalda Delgado 		return;
506d621e8baSRicardo Ribalda Delgado 
507d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
508d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
509d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
510d621e8baSRicardo Ribalda Delgado }
511d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
512d621e8baSRicardo Ribalda Delgado 
513726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
514726cb3baSStephen Boyd {
515726cb3baSStephen Boyd 	int len, i;
516726cb3baSStephen Boyd 	u32 start, count;
517726cb3baSStephen Boyd 	struct device_node *np = chip->of_node;
518726cb3baSStephen Boyd 
519726cb3baSStephen Boyd 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
520726cb3baSStephen Boyd 	if (len < 0 || len % 2 != 0)
521726cb3baSStephen Boyd 		return;
522726cb3baSStephen Boyd 
523726cb3baSStephen Boyd 	for (i = 0; i < len; i += 2) {
524726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
525726cb3baSStephen Boyd 					   i, &start);
526726cb3baSStephen Boyd 		of_property_read_u32_index(np, "gpio-reserved-ranges",
527726cb3baSStephen Boyd 					   i + 1, &count);
528726cb3baSStephen Boyd 		if (start >= chip->ngpio || start + count >= chip->ngpio)
529726cb3baSStephen Boyd 			continue;
530726cb3baSStephen Boyd 
531726cb3baSStephen Boyd 		bitmap_clear(chip->valid_mask, start, count);
532726cb3baSStephen Boyd 	}
533726cb3baSStephen Boyd };
534726cb3baSStephen Boyd 
535f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
53628355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
537f23f1516SShiraz Hashim {
538f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
539f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
5401e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
541f23f1516SShiraz Hashim 	int index = 0, ret;
542586a87e6SChristian Ruppert 	const char *name;
543586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
544586a87e6SChristian Ruppert 	struct property *group_names;
545f23f1516SShiraz Hashim 
546f23f1516SShiraz Hashim 	if (!np)
54728355f81STomeu Vizoso 		return 0;
548f23f1516SShiraz Hashim 
549586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
550586a87e6SChristian Ruppert 
551ad4e1a7cSHaojian Zhuang 	for (;; index++) {
552d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
553d9fe0039SStephen Warren 				index, &pinspec);
554f23f1516SShiraz Hashim 		if (ret)
555f23f1516SShiraz Hashim 			break;
556f23f1516SShiraz Hashim 
5571e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
558602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
5591e63d7b9SLinus Walleij 		if (!pctldev)
56028355f81STomeu Vizoso 			return -EPROBE_DEFER;
561f23f1516SShiraz Hashim 
562586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
563586a87e6SChristian Ruppert 			if (group_names) {
56472858602SLaurent Navet 				of_property_read_string_index(np,
565586a87e6SChristian Ruppert 						group_names_propname,
566586a87e6SChristian Ruppert 						index, &name);
567586a87e6SChristian Ruppert 				if (strlen(name)) {
5687eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
5697eb6ce2fSRob Herring 						np);
570586a87e6SChristian Ruppert 					break;
571586a87e6SChristian Ruppert 				}
572586a87e6SChristian Ruppert 			}
573586a87e6SChristian Ruppert 			/* npins != 0: linear range */
5741e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
575ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
5761e63d7b9SLinus Walleij 					pinspec.args[0],
57786853c83SHaojian Zhuang 					pinspec.args[1],
57886853c83SHaojian Zhuang 					pinspec.args[2]);
5791e63d7b9SLinus Walleij 			if (ret)
58028355f81STomeu Vizoso 				return ret;
581586a87e6SChristian Ruppert 		} else {
582586a87e6SChristian Ruppert 			/* npins == 0: special range */
583586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
5847eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
5857eb6ce2fSRob Herring 					np);
586586a87e6SChristian Ruppert 				break;
587586a87e6SChristian Ruppert 			}
588586a87e6SChristian Ruppert 
589586a87e6SChristian Ruppert 			if (!group_names) {
5907eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
5917eb6ce2fSRob Herring 					np, group_names_propname);
592586a87e6SChristian Ruppert 				break;
593586a87e6SChristian Ruppert 			}
594586a87e6SChristian Ruppert 
595586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
596586a87e6SChristian Ruppert 						group_names_propname,
597586a87e6SChristian Ruppert 						index, &name);
598586a87e6SChristian Ruppert 			if (ret)
599586a87e6SChristian Ruppert 				break;
600586a87e6SChristian Ruppert 
601586a87e6SChristian Ruppert 			if (!strlen(name)) {
6027eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
6037eb6ce2fSRob Herring 				np);
604586a87e6SChristian Ruppert 				break;
605586a87e6SChristian Ruppert 			}
606586a87e6SChristian Ruppert 
607586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
608586a87e6SChristian Ruppert 						pinspec.args[0], name);
609586a87e6SChristian Ruppert 			if (ret)
61028355f81STomeu Vizoso 				return ret;
611586a87e6SChristian Ruppert 		}
612ad4e1a7cSHaojian Zhuang 	}
61328355f81STomeu Vizoso 
61428355f81STomeu Vizoso 	return 0;
615f23f1516SShiraz Hashim }
616f23f1516SShiraz Hashim 
617f23f1516SShiraz Hashim #else
61828355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
619f23f1516SShiraz Hashim #endif
620f23f1516SShiraz Hashim 
62128355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
622f141ed65SGrant Likely {
62328355f81STomeu Vizoso 	int status;
62428355f81STomeu Vizoso 
625f141ed65SGrant Likely 	if (!chip->of_node)
62628355f81STomeu Vizoso 		return 0;
627f141ed65SGrant Likely 
628f141ed65SGrant Likely 	if (!chip->of_xlate) {
629f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
630f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
631f141ed65SGrant Likely 	}
632f141ed65SGrant Likely 
6331020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
6341020dfd1SMasahiro Yamada 		return -EINVAL;
6351020dfd1SMasahiro Yamada 
636726cb3baSStephen Boyd 	of_gpiochip_init_valid_mask(chip);
637726cb3baSStephen Boyd 
63828355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
63928355f81STomeu Vizoso 	if (status)
64028355f81STomeu Vizoso 		return status;
64128355f81STomeu Vizoso 
642fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
643fd9c5531SLinus Walleij 	if (!chip->names)
64482270335SChristophe Leroy 		devprop_gpiochip_set_names(chip,
64582270335SChristophe Leroy 					   of_fwnode_handle(chip->of_node));
646fd9c5531SLinus Walleij 
647f141ed65SGrant Likely 	of_node_get(chip->of_node);
648f625d460SBenoit Parrot 
649dfbd379bSLaxman Dewangan 	return of_gpiochip_scan_gpios(chip);
650f141ed65SGrant Likely }
651f141ed65SGrant Likely 
652f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
653f141ed65SGrant Likely {
654e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
655f141ed65SGrant Likely 	of_node_put(chip->of_node);
656f141ed65SGrant Likely }
657