xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision 605f2d34)
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 &&
34c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
357b8792bbSHans Holmberg }
363d0f7cf0SGrant Likely 
37c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
38c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
39762c2e46SMasahiro Yamada {
40c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
413d0f7cf0SGrant Likely }
423d0f7cf0SGrant Likely 
4399468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
4499468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
4599468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
4699468c1aSMasahiro Yamada {
4799468c1aSMasahiro Yamada 	int ret;
4899468c1aSMasahiro Yamada 
4999468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
5099468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
5199468c1aSMasahiro Yamada 
5299468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
5399468c1aSMasahiro Yamada 	if (ret < 0)
5499468c1aSMasahiro Yamada 		return ERR_PTR(ret);
5599468c1aSMasahiro Yamada 
5699468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
57f141ed65SGrant Likely }
58f141ed65SGrant Likely 
59a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np,
60a603a2b8SLinus Walleij 				 enum of_gpio_flags *flags)
61a603a2b8SLinus Walleij {
62a603a2b8SLinus Walleij 	/*
63a603a2b8SLinus Walleij 	 * Some GPIO fixed regulator quirks.
64a603a2b8SLinus Walleij 	 * Note that active low is the default.
65a603a2b8SLinus Walleij 	 */
66a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
67a603a2b8SLinus Walleij 	    (of_device_is_compatible(np, "reg-fixed-voltage") ||
68a603a2b8SLinus Walleij 	     of_device_is_compatible(np, "regulator-gpio"))) {
69a603a2b8SLinus Walleij 		/*
70a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
71a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
72a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
73a603a2b8SLinus Walleij 		 * be actively ignored.
74a603a2b8SLinus Walleij 		 */
75a603a2b8SLinus Walleij 		if (*flags & OF_GPIO_ACTIVE_LOW) {
76a603a2b8SLinus Walleij 			pr_warn("%s GPIO handle specifies active low - ignored\n",
77a603a2b8SLinus Walleij 				of_node_full_name(np));
78a603a2b8SLinus Walleij 			*flags &= ~OF_GPIO_ACTIVE_LOW;
79a603a2b8SLinus Walleij 		}
80a603a2b8SLinus Walleij 		if (!of_property_read_bool(np, "enable-active-high"))
81a603a2b8SLinus Walleij 			*flags |= OF_GPIO_ACTIVE_LOW;
82a603a2b8SLinus Walleij 	}
83a603a2b8SLinus Walleij 	/*
84a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
85a603a2b8SLinus Walleij 	 */
86a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
87a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
88a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
89a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
90a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
91a603a2b8SLinus Walleij 			of_node_full_name(np));
92a603a2b8SLinus Walleij 	}
93a603a2b8SLinus Walleij }
94a603a2b8SLinus Walleij 
95f141ed65SGrant Likely /**
96af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
97f141ed65SGrant Likely  * @np:		device node to get GPIO from
98f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
99f141ed65SGrant Likely  * @index:	index of the GPIO
100f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
101f141ed65SGrant Likely  *
102af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
103f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
104f141ed65SGrant Likely  * in flags for the GPIO.
105f141ed65SGrant Likely  */
106af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
107af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
108f141ed65SGrant Likely {
109762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
110762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
111762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
112f141ed65SGrant Likely 	int ret;
113f141ed65SGrant Likely 
1143d0f7cf0SGrant Likely 	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
115762c2e46SMasahiro Yamada 					 &gpiospec);
1163d0f7cf0SGrant Likely 	if (ret) {
1177eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
1187eb6ce2fSRob Herring 			__func__, propname, np, index);
119af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
1203d0f7cf0SGrant Likely 	}
121f141ed65SGrant Likely 
122c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
123762c2e46SMasahiro Yamada 	if (!chip) {
124762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
125762c2e46SMasahiro Yamada 		goto out;
126762c2e46SMasahiro Yamada 	}
1273d0f7cf0SGrant Likely 
12899468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
129762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
130762c2e46SMasahiro Yamada 		goto out;
131762c2e46SMasahiro Yamada 
132605f2d34SLinus Walleij 	if (flags)
133a603a2b8SLinus Walleij 		of_gpio_flags_quirks(np, flags);
134a603a2b8SLinus Walleij 
1357eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
1367eb6ce2fSRob Herring 		 __func__, propname, np, index,
137762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
138762c2e46SMasahiro Yamada 
139762c2e46SMasahiro Yamada out:
140762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
141762c2e46SMasahiro Yamada 
142762c2e46SMasahiro Yamada 	return desc;
143f141ed65SGrant Likely }
144f141ed65SGrant Likely 
145f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
146f01d9075SAlexandre Courbot 			    int index, enum of_gpio_flags *flags)
147f01d9075SAlexandre Courbot {
148f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
149f01d9075SAlexandre Courbot 
150f01d9075SAlexandre Courbot 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
151f01d9075SAlexandre Courbot 
152f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
153f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
154f01d9075SAlexandre Courbot 	else
155f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
156f01d9075SAlexandre Courbot }
157f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags);
158f01d9075SAlexandre Courbot 
159c8582339SLinus Walleij /*
160c8582339SLinus Walleij  * The SPI GPIO bindings happened before we managed to establish that GPIO
161c8582339SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
162c8582339SLinus Walleij  * them.
163c8582339SLinus Walleij  */
164c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
165c8582339SLinus Walleij 					  enum of_gpio_flags *of_flags)
166c8582339SLinus Walleij {
167c8582339SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
168c8582339SLinus Walleij 	struct device_node *np = dev->of_node;
169c8582339SLinus Walleij 	struct gpio_desc *desc;
170c8582339SLinus Walleij 
171c8582339SLinus Walleij 	/*
172c8582339SLinus Walleij 	 * Hopefully the compiler stubs the rest of the function if this
173c8582339SLinus Walleij 	 * is false.
174c8582339SLinus Walleij 	 */
175c8582339SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
176c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
177c8582339SLinus Walleij 
178c8582339SLinus Walleij 	/* Allow this specifically for "spi-gpio" devices */
179c8582339SLinus Walleij 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
180c8582339SLinus Walleij 		return ERR_PTR(-ENOENT);
181c8582339SLinus Walleij 
182c8582339SLinus Walleij 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
183c8582339SLinus Walleij 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
184c8582339SLinus Walleij 
185c8582339SLinus Walleij 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
186c8582339SLinus Walleij 	return desc;
187c8582339SLinus Walleij }
188c8582339SLinus Walleij 
1896a537d48SLinus Walleij /*
1906a537d48SLinus Walleij  * Some regulator bindings happened before we managed to establish that GPIO
1916a537d48SLinus Walleij  * properties should be named "foo-gpios" so we have this special kludge for
1926a537d48SLinus Walleij  * them.
1936a537d48SLinus Walleij  */
1946a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
1956a537d48SLinus Walleij 						enum of_gpio_flags *of_flags)
1966a537d48SLinus Walleij {
1976a537d48SLinus Walleij 	/* These are the connection IDs we accept as legacy GPIO phandles */
1986a537d48SLinus Walleij 	const char *whitelist[] = {
1996a537d48SLinus Walleij 		"wlf,ldoena", /* Arizona */
2006a537d48SLinus Walleij 		"wlf,ldo1ena", /* WM8994 */
2016a537d48SLinus Walleij 		"wlf,ldo2ena", /* WM8994 */
2026a537d48SLinus Walleij 	};
2036a537d48SLinus Walleij 	struct device_node *np = dev->of_node;
2046a537d48SLinus Walleij 	struct gpio_desc *desc;
2056a537d48SLinus Walleij 	int i;
2066a537d48SLinus Walleij 
2076a537d48SLinus Walleij 	if (!IS_ENABLED(CONFIG_REGULATOR))
2086a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2096a537d48SLinus Walleij 
2106a537d48SLinus Walleij 	if (!con_id)
2116a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2126a537d48SLinus Walleij 
2136a537d48SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(whitelist); i++)
2146a537d48SLinus Walleij 		if (!strcmp(con_id, whitelist[i]))
2156a537d48SLinus Walleij 			break;
2166a537d48SLinus Walleij 
2176a537d48SLinus Walleij 	if (i == ARRAY_SIZE(whitelist))
2186a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
2196a537d48SLinus Walleij 
2206a537d48SLinus Walleij 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
2216a537d48SLinus Walleij 	return desc;
2226a537d48SLinus Walleij }
2236a537d48SLinus Walleij 
224ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
225ea713bc4SLinus Walleij 			       unsigned int idx,
226ea713bc4SLinus Walleij 			       enum gpio_lookup_flags *flags)
227ea713bc4SLinus Walleij {
228ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
229ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
230ea713bc4SLinus Walleij 	struct gpio_desc *desc;
231ea713bc4SLinus Walleij 	unsigned int i;
232ea713bc4SLinus Walleij 
233c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
234ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
235ea713bc4SLinus Walleij 		if (con_id)
236ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
237ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
238ea713bc4SLinus Walleij 		else
239ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
240ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
241ea713bc4SLinus Walleij 
242ea713bc4SLinus Walleij 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
243ea713bc4SLinus Walleij 						&of_flags);
244ea713bc4SLinus Walleij 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
245ea713bc4SLinus Walleij 			break;
246ea713bc4SLinus Walleij 	}
247ea713bc4SLinus Walleij 
248c8582339SLinus Walleij 	/* Special handling for SPI GPIOs if used */
249c8582339SLinus Walleij 	if (IS_ERR(desc))
250c8582339SLinus Walleij 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
251c8582339SLinus Walleij 
2526a537d48SLinus Walleij 	/* Special handling for regulator GPIOs if used */
2536a537d48SLinus Walleij 	if (IS_ERR(desc))
2546a537d48SLinus Walleij 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
2556a537d48SLinus Walleij 
256ea713bc4SLinus Walleij 	if (IS_ERR(desc))
257ea713bc4SLinus Walleij 		return desc;
258ea713bc4SLinus Walleij 
259ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_ACTIVE_LOW)
260ea713bc4SLinus Walleij 		*flags |= GPIO_ACTIVE_LOW;
261ea713bc4SLinus Walleij 
262ea713bc4SLinus Walleij 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
2634c0facddSLaxman Dewangan 		if (of_flags & OF_GPIO_OPEN_DRAIN)
264ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_DRAIN;
265ea713bc4SLinus Walleij 		else
266ea713bc4SLinus Walleij 			*flags |= GPIO_OPEN_SOURCE;
267ea713bc4SLinus Walleij 	}
268ea713bc4SLinus Walleij 
269e10f72bfSAndrew Jeffery 	if (of_flags & OF_GPIO_TRANSITORY)
270e10f72bfSAndrew Jeffery 		*flags |= GPIO_TRANSITORY;
27105f479bfSCharles Keepax 
272ea713bc4SLinus Walleij 	return desc;
273ea713bc4SLinus Walleij }
274ea713bc4SLinus Walleij 
275f141ed65SGrant Likely /**
276fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
277f625d460SBenoit Parrot  * @np:		device node to get GPIO from
278be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
279a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
280f625d460SBenoit Parrot  * @name:	GPIO line name
281f625d460SBenoit Parrot  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
282fd7337fdSMarkus Pargmann  *		of_parse_own_gpio()
283f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
284f625d460SBenoit Parrot  *
285f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
286f625d460SBenoit Parrot  * value on the error condition.
287f625d460SBenoit Parrot  */
288fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
289be715343SMasahiro Yamada 					   struct gpio_chip *chip,
290a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
291f625d460SBenoit Parrot 					   enum gpio_lookup_flags *lflags,
292f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
293f625d460SBenoit Parrot {
294f625d460SBenoit Parrot 	struct device_node *chip_np;
295f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
296be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
297be715343SMasahiro Yamada 	struct gpio_desc *desc;
298a79fead5SGeert Uytterhoeven 	unsigned int i;
299f625d460SBenoit Parrot 	u32 tmp;
3003f9547e1SMasahiro Yamada 	int ret;
301f625d460SBenoit Parrot 
302be715343SMasahiro Yamada 	chip_np = chip->of_node;
303f625d460SBenoit Parrot 	if (!chip_np)
304f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
305f625d460SBenoit Parrot 
306f625d460SBenoit Parrot 	xlate_flags = 0;
307f625d460SBenoit Parrot 	*lflags = 0;
308f625d460SBenoit Parrot 	*dflags = 0;
309f625d460SBenoit Parrot 
310f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
311f625d460SBenoit Parrot 	if (ret)
312f625d460SBenoit Parrot 		return ERR_PTR(ret);
313f625d460SBenoit Parrot 
314be715343SMasahiro Yamada 	gpiospec.np = chip_np;
315be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
316f625d460SBenoit Parrot 
317a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
318a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
319a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
320f625d460SBenoit Parrot 		if (ret)
321f625d460SBenoit Parrot 			return ERR_PTR(ret);
322a79fead5SGeert Uytterhoeven 	}
323f625d460SBenoit Parrot 
32499468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
325be715343SMasahiro Yamada 	if (IS_ERR(desc))
326be715343SMasahiro Yamada 		return desc;
327f625d460SBenoit Parrot 
328f625d460SBenoit Parrot 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
329f625d460SBenoit Parrot 		*lflags |= GPIO_ACTIVE_LOW;
330e10f72bfSAndrew Jeffery 	if (xlate_flags & OF_GPIO_TRANSITORY)
331e10f72bfSAndrew Jeffery 		*lflags |= GPIO_TRANSITORY;
332f625d460SBenoit Parrot 
333f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
334f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
335f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
336f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
337f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
338f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
339f625d460SBenoit Parrot 	else {
340f625d460SBenoit Parrot 		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
341be715343SMasahiro Yamada 			desc_to_gpio(desc), np->name);
342f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
343f625d460SBenoit Parrot 	}
344f625d460SBenoit Parrot 
345f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
346f625d460SBenoit Parrot 		*name = np->name;
347f625d460SBenoit Parrot 
348be715343SMasahiro Yamada 	return desc;
349f625d460SBenoit Parrot }
350f625d460SBenoit Parrot 
351f625d460SBenoit Parrot /**
352fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
353f625d460SBenoit Parrot  * @chip:	gpio chip to act on
354f625d460SBenoit Parrot  *
355f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
356f625d460SBenoit Parrot  * configuration.
357ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
358f625d460SBenoit Parrot  */
359dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
360f625d460SBenoit Parrot {
361f625d460SBenoit Parrot 	struct gpio_desc *desc = NULL;
362f625d460SBenoit Parrot 	struct device_node *np;
363f625d460SBenoit Parrot 	const char *name;
364f625d460SBenoit Parrot 	enum gpio_lookup_flags lflags;
365f625d460SBenoit Parrot 	enum gpiod_flags dflags;
366a79fead5SGeert Uytterhoeven 	unsigned int i;
367dfbd379bSLaxman Dewangan 	int ret;
368f625d460SBenoit Parrot 
369d1279d94SLaxman Dewangan 	for_each_available_child_of_node(chip->of_node, np) {
370f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
371f625d460SBenoit Parrot 			continue;
372f625d460SBenoit Parrot 
373a79fead5SGeert Uytterhoeven 		for (i = 0;; i++) {
374a79fead5SGeert Uytterhoeven 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
375a79fead5SGeert Uytterhoeven 						 &dflags);
376f625d460SBenoit Parrot 			if (IS_ERR(desc))
377a79fead5SGeert Uytterhoeven 				break;
378f625d460SBenoit Parrot 
379dfbd379bSLaxman Dewangan 			ret = gpiod_hog(desc, name, lflags, dflags);
38009e258afSWei Yongjun 			if (ret < 0) {
38109e258afSWei Yongjun 				of_node_put(np);
382dfbd379bSLaxman Dewangan 				return ret;
383f625d460SBenoit Parrot 			}
38409e258afSWei Yongjun 		}
385a79fead5SGeert Uytterhoeven 	}
386dfbd379bSLaxman Dewangan 
387dfbd379bSLaxman Dewangan 	return 0;
388f625d460SBenoit Parrot }
389f625d460SBenoit Parrot 
390f625d460SBenoit Parrot /**
39167049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
392f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
39367049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
394f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
395f141ed65SGrant Likely  *
396f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
39767049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
398f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
399f141ed65SGrant Likely  */
400f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc,
401f141ed65SGrant Likely 			 const struct of_phandle_args *gpiospec, u32 *flags)
402f141ed65SGrant Likely {
403f141ed65SGrant Likely 	/*
404f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
40520a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
406f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
407f141ed65SGrant Likely 	 * but not recommended).
408f141ed65SGrant Likely 	 */
409f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
410f141ed65SGrant Likely 		WARN_ON(1);
411f141ed65SGrant Likely 		return -EINVAL;
412f141ed65SGrant Likely 	}
413f141ed65SGrant Likely 
414f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
415f141ed65SGrant Likely 		return -EINVAL;
416f141ed65SGrant Likely 
4177b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
418f141ed65SGrant Likely 		return -EINVAL;
419f141ed65SGrant Likely 
420f141ed65SGrant Likely 	if (flags)
421f141ed65SGrant Likely 		*flags = gpiospec->args[1];
422f141ed65SGrant Likely 
423f141ed65SGrant Likely 	return gpiospec->args[0];
424f141ed65SGrant Likely }
425f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate);
426f141ed65SGrant Likely 
427f141ed65SGrant Likely /**
4283208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
429f141ed65SGrant Likely  * @np:		device node of the GPIO chip
430f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
4313208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
432f141ed65SGrant Likely  *
433f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
434f141ed65SGrant Likely  *
435f141ed65SGrant Likely  * 1) In the gpio_chip structure:
436f141ed65SGrant Likely  *    - all the callbacks
437f141ed65SGrant Likely  *    - of_gpio_n_cells
438f141ed65SGrant Likely  *    - of_xlate callback (optional)
439f141ed65SGrant Likely  *
440f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
441f141ed65SGrant Likely  *    - save_regs callback (optional)
442f141ed65SGrant Likely  *
443f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
444f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
445f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
446f141ed65SGrant Likely  */
4473208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
4483208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
4493208b0f0SLinus Walleij 			    void *data)
450f141ed65SGrant Likely {
451f141ed65SGrant Likely 	int ret = -ENOMEM;
452f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
453f141ed65SGrant Likely 
4547eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
455f141ed65SGrant Likely 	if (!gc->label)
456f141ed65SGrant Likely 		goto err0;
457f141ed65SGrant Likely 
458f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
459f141ed65SGrant Likely 	if (!mm_gc->regs)
460f141ed65SGrant Likely 		goto err1;
461f141ed65SGrant Likely 
462f141ed65SGrant Likely 	gc->base = -1;
463f141ed65SGrant Likely 
464f141ed65SGrant Likely 	if (mm_gc->save_regs)
465f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
466f141ed65SGrant Likely 
467f141ed65SGrant Likely 	mm_gc->gc.of_node = np;
468f141ed65SGrant Likely 
4693208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
470f141ed65SGrant Likely 	if (ret)
471f141ed65SGrant Likely 		goto err2;
472f141ed65SGrant Likely 
473f141ed65SGrant Likely 	return 0;
474f141ed65SGrant Likely err2:
475f141ed65SGrant Likely 	iounmap(mm_gc->regs);
476f141ed65SGrant Likely err1:
477f141ed65SGrant Likely 	kfree(gc->label);
478f141ed65SGrant Likely err0:
4797eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
480f141ed65SGrant Likely 	return ret;
481f141ed65SGrant Likely }
4823208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data);
483f141ed65SGrant Likely 
484d621e8baSRicardo Ribalda Delgado /**
485d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
486d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
487d621e8baSRicardo Ribalda Delgado  */
488d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
489d621e8baSRicardo Ribalda Delgado {
490d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
491d621e8baSRicardo Ribalda Delgado 
492d621e8baSRicardo Ribalda Delgado 	if (!mm_gc)
493d621e8baSRicardo Ribalda Delgado 		return;
494d621e8baSRicardo Ribalda Delgado 
495d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
496d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
497d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
498d621e8baSRicardo Ribalda Delgado }
499d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove);
500d621e8baSRicardo Ribalda Delgado 
501f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
50228355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
503f23f1516SShiraz Hashim {
504f23f1516SShiraz Hashim 	struct device_node *np = chip->of_node;
505f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
5061e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
507f23f1516SShiraz Hashim 	int index = 0, ret;
508586a87e6SChristian Ruppert 	const char *name;
509586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
510586a87e6SChristian Ruppert 	struct property *group_names;
511f23f1516SShiraz Hashim 
512f23f1516SShiraz Hashim 	if (!np)
51328355f81STomeu Vizoso 		return 0;
514f23f1516SShiraz Hashim 
515586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
516586a87e6SChristian Ruppert 
517ad4e1a7cSHaojian Zhuang 	for (;; index++) {
518d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
519d9fe0039SStephen Warren 				index, &pinspec);
520f23f1516SShiraz Hashim 		if (ret)
521f23f1516SShiraz Hashim 			break;
522f23f1516SShiraz Hashim 
5231e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
524602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
5251e63d7b9SLinus Walleij 		if (!pctldev)
52628355f81STomeu Vizoso 			return -EPROBE_DEFER;
527f23f1516SShiraz Hashim 
528586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
529586a87e6SChristian Ruppert 			if (group_names) {
53072858602SLaurent Navet 				of_property_read_string_index(np,
531586a87e6SChristian Ruppert 						group_names_propname,
532586a87e6SChristian Ruppert 						index, &name);
533586a87e6SChristian Ruppert 				if (strlen(name)) {
5347eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
5357eb6ce2fSRob Herring 						np);
536586a87e6SChristian Ruppert 					break;
537586a87e6SChristian Ruppert 				}
538586a87e6SChristian Ruppert 			}
539586a87e6SChristian Ruppert 			/* npins != 0: linear range */
5401e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
541ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
5421e63d7b9SLinus Walleij 					pinspec.args[0],
54386853c83SHaojian Zhuang 					pinspec.args[1],
54486853c83SHaojian Zhuang 					pinspec.args[2]);
5451e63d7b9SLinus Walleij 			if (ret)
54628355f81STomeu Vizoso 				return ret;
547586a87e6SChristian Ruppert 		} else {
548586a87e6SChristian Ruppert 			/* npins == 0: special range */
549586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
5507eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
5517eb6ce2fSRob Herring 					np);
552586a87e6SChristian Ruppert 				break;
553586a87e6SChristian Ruppert 			}
554586a87e6SChristian Ruppert 
555586a87e6SChristian Ruppert 			if (!group_names) {
5567eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
5577eb6ce2fSRob Herring 					np, group_names_propname);
558586a87e6SChristian Ruppert 				break;
559586a87e6SChristian Ruppert 			}
560586a87e6SChristian Ruppert 
561586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
562586a87e6SChristian Ruppert 						group_names_propname,
563586a87e6SChristian Ruppert 						index, &name);
564586a87e6SChristian Ruppert 			if (ret)
565586a87e6SChristian Ruppert 				break;
566586a87e6SChristian Ruppert 
567586a87e6SChristian Ruppert 			if (!strlen(name)) {
5687eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
5697eb6ce2fSRob Herring 				np);
570586a87e6SChristian Ruppert 				break;
571586a87e6SChristian Ruppert 			}
572586a87e6SChristian Ruppert 
573586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
574586a87e6SChristian Ruppert 						pinspec.args[0], name);
575586a87e6SChristian Ruppert 			if (ret)
57628355f81STomeu Vizoso 				return ret;
577586a87e6SChristian Ruppert 		}
578ad4e1a7cSHaojian Zhuang 	}
57928355f81STomeu Vizoso 
58028355f81STomeu Vizoso 	return 0;
581f23f1516SShiraz Hashim }
582f23f1516SShiraz Hashim 
583f23f1516SShiraz Hashim #else
58428355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
585f23f1516SShiraz Hashim #endif
586f23f1516SShiraz Hashim 
58728355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
588f141ed65SGrant Likely {
58928355f81STomeu Vizoso 	int status;
59028355f81STomeu Vizoso 
59158383c78SLinus Walleij 	if ((!chip->of_node) && (chip->parent))
59258383c78SLinus Walleij 		chip->of_node = chip->parent->of_node;
593f141ed65SGrant Likely 
594f141ed65SGrant Likely 	if (!chip->of_node)
59528355f81STomeu Vizoso 		return 0;
596f141ed65SGrant Likely 
597f141ed65SGrant Likely 	if (!chip->of_xlate) {
598f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
599f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
600f141ed65SGrant Likely 	}
601f141ed65SGrant Likely 
6021020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
6031020dfd1SMasahiro Yamada 		return -EINVAL;
6041020dfd1SMasahiro Yamada 
60528355f81STomeu Vizoso 	status = of_gpiochip_add_pin_range(chip);
60628355f81STomeu Vizoso 	if (status)
60728355f81STomeu Vizoso 		return status;
60828355f81STomeu Vizoso 
609fd9c5531SLinus Walleij 	/* If the chip defines names itself, these take precedence */
610fd9c5531SLinus Walleij 	if (!chip->names)
6119427ecbeSMika Westerberg 		devprop_gpiochip_set_names(chip);
612fd9c5531SLinus Walleij 
613f141ed65SGrant Likely 	of_node_get(chip->of_node);
614f625d460SBenoit Parrot 
615dfbd379bSLaxman Dewangan 	return of_gpiochip_scan_gpios(chip);
616f141ed65SGrant Likely }
617f141ed65SGrant Likely 
618f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
619f141ed65SGrant Likely {
620e93fa3f2SLinus Walleij 	gpiochip_remove_pin_ranges(chip);
621f141ed65SGrant Likely 	of_node_put(chip->of_node);
622f141ed65SGrant Likely }
623