xref: /openbmc/linux/drivers/gpio/gpiolib-of.c (revision ee1cd5048959de496cd005c50b137212a5b62062)
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/io.h>
14380c7ba3SAndy Shevchenko #include <linux/module.h>
15f141ed65SGrant Likely #include <linux/of.h>
16f141ed65SGrant Likely #include <linux/of_address.h>
17f141ed65SGrant Likely #include <linux/of_gpio.h>
18f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h>
19f141ed65SGrant Likely #include <linux/slab.h>
20380c7ba3SAndy Shevchenko #include <linux/string.h>
21380c7ba3SAndy Shevchenko 
22380c7ba3SAndy Shevchenko #include <linux/gpio/consumer.h>
23f625d460SBenoit Parrot #include <linux/gpio/machine.h>
24f141ed65SGrant Likely 
251bd6b601SAlexandre Courbot #include "gpiolib.h"
26f626d6dfSLinus Walleij #include "gpiolib-of.h"
27f626d6dfSLinus Walleij 
28eed5a3bfSAndy Shevchenko /*
29eed5a3bfSAndy Shevchenko  * This is Linux-specific flags. By default controllers' and Linux' mapping
30eed5a3bfSAndy Shevchenko  * match, but GPIO controllers are free to translate their own flags to
31eed5a3bfSAndy Shevchenko  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
32eed5a3bfSAndy Shevchenko  */
33eed5a3bfSAndy Shevchenko enum of_gpio_flags {
34eed5a3bfSAndy Shevchenko 	OF_GPIO_ACTIVE_LOW = 0x1,
35eed5a3bfSAndy Shevchenko 	OF_GPIO_SINGLE_ENDED = 0x2,
36eed5a3bfSAndy Shevchenko 	OF_GPIO_OPEN_DRAIN = 0x4,
37eed5a3bfSAndy Shevchenko 	OF_GPIO_TRANSITORY = 0x8,
38eed5a3bfSAndy Shevchenko 	OF_GPIO_PULL_UP = 0x10,
39eed5a3bfSAndy Shevchenko 	OF_GPIO_PULL_DOWN = 0x20,
40eed5a3bfSAndy Shevchenko 	OF_GPIO_PULL_DISABLE = 0x40,
41eed5a3bfSAndy Shevchenko };
42eed5a3bfSAndy Shevchenko 
4371b8f600SLinus Walleij /**
44c7835652SDmitry Torokhov  * of_gpio_named_count() - Count GPIOs for a device
45c7835652SDmitry Torokhov  * @np:		device node to count GPIOs for
46c7835652SDmitry Torokhov  * @propname:	property name containing gpio specifier(s)
47c7835652SDmitry Torokhov  *
48c7835652SDmitry Torokhov  * The function returns the count of GPIOs specified for a node.
49c7835652SDmitry Torokhov  * Note that the empty GPIO specifiers count too. Returns either
50c7835652SDmitry Torokhov  *   Number of gpios defined in property,
51c7835652SDmitry Torokhov  *   -EINVAL for an incorrectly formed gpios property, or
52c7835652SDmitry Torokhov  *   -ENOENT for a missing gpios property
53c7835652SDmitry Torokhov  *
54c7835652SDmitry Torokhov  * Example:
55c7835652SDmitry Torokhov  * gpios = <0
56c7835652SDmitry Torokhov  *          &gpio1 1 2
57c7835652SDmitry Torokhov  *          0
58c7835652SDmitry Torokhov  *          &gpio2 3 4>;
59c7835652SDmitry Torokhov  *
60c7835652SDmitry Torokhov  * The above example defines four GPIOs, two of which are not specified.
61c7835652SDmitry Torokhov  * This function will return '4'
62c7835652SDmitry Torokhov  */
of_gpio_named_count(const struct device_node * np,const char * propname)63c7835652SDmitry Torokhov static int of_gpio_named_count(const struct device_node *np,
64c7835652SDmitry Torokhov 			       const char *propname)
65c7835652SDmitry Torokhov {
66c7835652SDmitry Torokhov 	return of_count_phandle_with_args(np, propname, "#gpio-cells");
67c7835652SDmitry Torokhov }
68c7835652SDmitry Torokhov 
69c7835652SDmitry Torokhov /**
7071b8f600SLinus Walleij  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
71c5a66b97SLee Jones  * @dev:    Consuming device
72c5a66b97SLee Jones  * @con_id: Function within the GPIO consumer
73c5a66b97SLee Jones  *
7471b8f600SLinus Walleij  * Some elder GPIO controllers need special quirks. Currently we handle
7547267732SLinus Walleij  * the Freescale and PPC GPIO controller with bindings that doesn't use the
7671b8f600SLinus Walleij  * established "cs-gpios" for chip selects but instead rely on
7771b8f600SLinus Walleij  * "gpios" for the chip select lines. If we detect this, we redirect
7871b8f600SLinus Walleij  * the counting of "cs-gpios" to count "gpios" transparent to the
7971b8f600SLinus Walleij  * driver.
8071b8f600SLinus Walleij  */
of_gpio_spi_cs_get_count(struct device * dev,const char * con_id)81a1f4c96bSYueHaibing static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
8271b8f600SLinus Walleij {
8371b8f600SLinus Walleij 	struct device_node *np = dev->of_node;
8471b8f600SLinus Walleij 
8571b8f600SLinus Walleij 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
8671b8f600SLinus Walleij 		return 0;
8771b8f600SLinus Walleij 	if (!con_id || strcmp(con_id, "cs"))
8871b8f600SLinus Walleij 		return 0;
8971b8f600SLinus Walleij 	if (!of_device_is_compatible(np, "fsl,spi") &&
9047267732SLinus Walleij 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
9147267732SLinus Walleij 	    !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
9271b8f600SLinus Walleij 		return 0;
9371b8f600SLinus Walleij 	return of_gpio_named_count(np, "gpios");
9471b8f600SLinus Walleij }
9571b8f600SLinus Walleij 
of_gpio_get_count(struct device * dev,const char * con_id)96f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id)
97f626d6dfSLinus Walleij {
98f626d6dfSLinus Walleij 	int ret;
99f626d6dfSLinus Walleij 	char propname[32];
100f626d6dfSLinus Walleij 	unsigned int i;
101f626d6dfSLinus Walleij 
10271b8f600SLinus Walleij 	ret = of_gpio_spi_cs_get_count(dev, con_id);
10371b8f600SLinus Walleij 	if (ret > 0)
10471b8f600SLinus Walleij 		return ret;
10571b8f600SLinus Walleij 
106f626d6dfSLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
107f626d6dfSLinus Walleij 		if (con_id)
108f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s-%s",
109f626d6dfSLinus Walleij 				 con_id, gpio_suffixes[i]);
110f626d6dfSLinus Walleij 		else
111f626d6dfSLinus Walleij 			snprintf(propname, sizeof(propname), "%s",
112f626d6dfSLinus Walleij 				 gpio_suffixes[i]);
113f626d6dfSLinus Walleij 
114f626d6dfSLinus Walleij 		ret = of_gpio_named_count(dev->of_node, propname);
115f626d6dfSLinus Walleij 		if (ret > 0)
116f626d6dfSLinus Walleij 			break;
117f626d6dfSLinus Walleij 	}
118f626d6dfSLinus Walleij 	return ret ? ret : -ENOENT;
119f626d6dfSLinus Walleij }
120af8b6375SAlexandre Courbot 
of_gpiochip_match_node_and_xlate(struct gpio_chip * chip,void * data)121c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
1223d0f7cf0SGrant Likely {
123c7e9d398SMasahiro Yamada 	struct of_phandle_args *gpiospec = data;
124c7e9d398SMasahiro Yamada 
125d59fdbc7SAndy Shevchenko 	return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) &&
126d49b48f0SVincent Whitchurch 				chip->of_xlate &&
127c7e9d398SMasahiro Yamada 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
1287b8792bbSHans Holmberg }
1293d0f7cf0SGrant Likely 
of_find_gpiochip_by_xlate(struct of_phandle_args * gpiospec)130c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate(
131c7e9d398SMasahiro Yamada 					struct of_phandle_args *gpiospec)
132762c2e46SMasahiro Yamada {
133c7e9d398SMasahiro Yamada 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
1343d0f7cf0SGrant Likely }
1353d0f7cf0SGrant Likely 
of_xlate_and_get_gpiod_flags(struct gpio_chip * chip,struct of_phandle_args * gpiospec,enum of_gpio_flags * flags)13699468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
13799468c1aSMasahiro Yamada 					struct of_phandle_args *gpiospec,
13899468c1aSMasahiro Yamada 					enum of_gpio_flags *flags)
13999468c1aSMasahiro Yamada {
14099468c1aSMasahiro Yamada 	int ret;
14199468c1aSMasahiro Yamada 
14299468c1aSMasahiro Yamada 	if (chip->of_gpio_n_cells != gpiospec->args_count)
14399468c1aSMasahiro Yamada 		return ERR_PTR(-EINVAL);
14499468c1aSMasahiro Yamada 
14599468c1aSMasahiro Yamada 	ret = chip->of_xlate(chip, gpiospec, flags);
14699468c1aSMasahiro Yamada 	if (ret < 0)
14799468c1aSMasahiro Yamada 		return ERR_PTR(ret);
14899468c1aSMasahiro Yamada 
14999468c1aSMasahiro Yamada 	return gpiochip_get_desc(chip, ret);
150f141ed65SGrant Likely }
151f141ed65SGrant Likely 
152e3186e36SDmitry Torokhov /*
153e3186e36SDmitry Torokhov  * Overrides stated polarity of a gpio line and warns when there is a
154e3186e36SDmitry Torokhov  * discrepancy.
155e3186e36SDmitry Torokhov  */
of_gpio_quirk_polarity(const struct device_node * np,bool active_high,enum of_gpio_flags * flags)156e3186e36SDmitry Torokhov static void of_gpio_quirk_polarity(const struct device_node *np,
157e3186e36SDmitry Torokhov 				   bool active_high,
158e3186e36SDmitry Torokhov 				   enum of_gpio_flags *flags)
159e3186e36SDmitry Torokhov {
160e3186e36SDmitry Torokhov 	if (active_high) {
161e3186e36SDmitry Torokhov 		if (*flags & OF_GPIO_ACTIVE_LOW) {
162e3186e36SDmitry Torokhov 			pr_warn("%s GPIO handle specifies active low - ignored\n",
163e3186e36SDmitry Torokhov 				of_node_full_name(np));
164e3186e36SDmitry Torokhov 			*flags &= ~OF_GPIO_ACTIVE_LOW;
165e3186e36SDmitry Torokhov 		}
166e3186e36SDmitry Torokhov 	} else {
167e3186e36SDmitry Torokhov 		if (!(*flags & OF_GPIO_ACTIVE_LOW))
168e3186e36SDmitry Torokhov 			pr_info("%s enforce active low on GPIO handle\n",
169e3186e36SDmitry Torokhov 				of_node_full_name(np));
170e3186e36SDmitry Torokhov 		*flags |= OF_GPIO_ACTIVE_LOW;
171e3186e36SDmitry Torokhov 	}
172e3186e36SDmitry Torokhov }
173e3186e36SDmitry Torokhov 
17499d18d42SDmitry Torokhov /*
17599d18d42SDmitry Torokhov  * This quirk does static polarity overrides in cases where existing
17699d18d42SDmitry Torokhov  * DTS specified incorrect polarity.
17799d18d42SDmitry Torokhov  */
of_gpio_try_fixup_polarity(const struct device_node * np,const char * propname,enum of_gpio_flags * flags)17899d18d42SDmitry Torokhov static void of_gpio_try_fixup_polarity(const struct device_node *np,
17999d18d42SDmitry Torokhov 				       const char *propname,
18099d18d42SDmitry Torokhov 				       enum of_gpio_flags *flags)
18199d18d42SDmitry Torokhov {
18299d18d42SDmitry Torokhov 	static const struct {
18399d18d42SDmitry Torokhov 		const char *compatible;
18499d18d42SDmitry Torokhov 		const char *propname;
18599d18d42SDmitry Torokhov 		bool active_high;
18699d18d42SDmitry Torokhov 	} gpios[] = {
18799d18d42SDmitry Torokhov #if !IS_ENABLED(CONFIG_LCD_HX8357)
18899d18d42SDmitry Torokhov 		/*
18999d18d42SDmitry Torokhov 		 * Himax LCD controllers used incorrectly named
19099d18d42SDmitry Torokhov 		 * "gpios-reset" property and also specified wrong
19199d18d42SDmitry Torokhov 		 * polarity.
19299d18d42SDmitry Torokhov 		 */
19399d18d42SDmitry Torokhov 		{ "himax,hx8357",	"gpios-reset",	false },
19499d18d42SDmitry Torokhov 		{ "himax,hx8369",	"gpios-reset",	false },
19599d18d42SDmitry Torokhov #endif
196895f5633SDmitry Torokhov #if IS_ENABLED(CONFIG_PCI_LANTIQ)
197895f5633SDmitry Torokhov 		/*
198895f5633SDmitry Torokhov 		 * According to the PCI specification, the RST# pin is an
199895f5633SDmitry Torokhov 		 * active-low signal. However, most of the device trees that
200895f5633SDmitry Torokhov 		 * have been widely used for a long time incorrectly describe
201895f5633SDmitry Torokhov 		 * reset GPIO as active-high, and were also using wrong name
202895f5633SDmitry Torokhov 		 * for the property.
203895f5633SDmitry Torokhov 		 */
204895f5633SDmitry Torokhov 		{ "lantiq,pci-xway",	"gpio-reset",	false },
205895f5633SDmitry Torokhov #endif
206*b0a41715SDmitry Torokhov #if IS_ENABLED(CONFIG_TOUCHSCREEN_TSC2005)
207*b0a41715SDmitry Torokhov 		/*
208*b0a41715SDmitry Torokhov 		 * DTS for Nokia N900 incorrectly specified "active high"
209*b0a41715SDmitry Torokhov 		 * polarity for the reset line, while the chip actually
210*b0a41715SDmitry Torokhov 		 * treats it as "active low".
211*b0a41715SDmitry Torokhov 		 */
212*b0a41715SDmitry Torokhov 		{ "ti,tsc2005",		"reset-gpios",	false },
213*b0a41715SDmitry Torokhov #endif
21499d18d42SDmitry Torokhov 	};
21599d18d42SDmitry Torokhov 	unsigned int i;
21699d18d42SDmitry Torokhov 
21799d18d42SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
21899d18d42SDmitry Torokhov 		if (of_device_is_compatible(np, gpios[i].compatible) &&
21999d18d42SDmitry Torokhov 		    !strcmp(propname, gpios[i].propname)) {
22099d18d42SDmitry Torokhov 			of_gpio_quirk_polarity(np, gpios[i].active_high, flags);
22199d18d42SDmitry Torokhov 			break;
22299d18d42SDmitry Torokhov 		}
22399d18d42SDmitry Torokhov 	}
22499d18d42SDmitry Torokhov }
22599d18d42SDmitry Torokhov 
of_gpio_set_polarity_by_property(const struct device_node * np,const char * propname,enum of_gpio_flags * flags)22634cb9352SDmitry Torokhov static void of_gpio_set_polarity_by_property(const struct device_node *np,
22789a5e15bSLinus Walleij 					     const char *propname,
22834cb9352SDmitry Torokhov 					     enum of_gpio_flags *flags)
229a603a2b8SLinus Walleij {
23098ac9e4fSBalamanikandan Gunasundar 	const struct device_node *np_compat = np;
23198ac9e4fSBalamanikandan Gunasundar 	const struct device_node *np_propname = np;
23234cb9352SDmitry Torokhov 	static const struct {
23334cb9352SDmitry Torokhov 		const char *compatible;
23434cb9352SDmitry Torokhov 		const char *gpio_propname;
23534cb9352SDmitry Torokhov 		const char *polarity_propname;
23634cb9352SDmitry Torokhov 	} gpios[] = {
23734cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_FEC)
23834cb9352SDmitry Torokhov 		/* Freescale Fast Ethernet Controller */
23934cb9352SDmitry Torokhov 		{ "fsl,imx25-fec",   "phy-reset-gpios", "phy-reset-active-high" },
24034cb9352SDmitry Torokhov 		{ "fsl,imx27-fec",   "phy-reset-gpios", "phy-reset-active-high" },
24134cb9352SDmitry Torokhov 		{ "fsl,imx28-fec",   "phy-reset-gpios", "phy-reset-active-high" },
24234cb9352SDmitry Torokhov 		{ "fsl,imx6q-fec",   "phy-reset-gpios", "phy-reset-active-high" },
24334cb9352SDmitry Torokhov 		{ "fsl,mvf600-fec",  "phy-reset-gpios", "phy-reset-active-high" },
24434cb9352SDmitry Torokhov 		{ "fsl,imx6sx-fec",  "phy-reset-gpios", "phy-reset-active-high" },
24534cb9352SDmitry Torokhov 		{ "fsl,imx6ul-fec",  "phy-reset-gpios", "phy-reset-active-high" },
24634cb9352SDmitry Torokhov 		{ "fsl,imx8mq-fec",  "phy-reset-gpios", "phy-reset-active-high" },
24734cb9352SDmitry Torokhov 		{ "fsl,imx8qm-fec",  "phy-reset-gpios", "phy-reset-active-high" },
24834cb9352SDmitry Torokhov 		{ "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" },
24934cb9352SDmitry Torokhov #endif
250b8b80348SDmitry Torokhov #if IS_ENABLED(CONFIG_PCI_IMX6)
251b8b80348SDmitry Torokhov 		{ "fsl,imx6q-pcie",  "reset-gpio", "reset-gpio-active-high" },
252b8b80348SDmitry Torokhov 		{ "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" },
253b8b80348SDmitry Torokhov 		{ "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" },
254b8b80348SDmitry Torokhov 		{ "fsl,imx7d-pcie",  "reset-gpio", "reset-gpio-active-high" },
255b8b80348SDmitry Torokhov 		{ "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" },
256b8b80348SDmitry Torokhov 		{ "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
257b8b80348SDmitry Torokhov 		{ "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
258b8b80348SDmitry Torokhov #endif
25999d18d42SDmitry Torokhov 
260a603a2b8SLinus Walleij 		/*
261a603a2b8SLinus Walleij 		 * The regulator GPIO handles are specified such that the
262a603a2b8SLinus Walleij 		 * presence or absence of "enable-active-high" solely controls
263a603a2b8SLinus Walleij 		 * the polarity of the GPIO line. Any phandle flags must
264a603a2b8SLinus Walleij 		 * be actively ignored.
265a603a2b8SLinus Walleij 		 */
26634cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE)
26734cb9352SDmitry Torokhov 		{ "regulator-fixed",   "gpios",        "enable-active-high" },
26834cb9352SDmitry Torokhov 		{ "regulator-fixed",   "gpio",         "enable-active-high" },
26934cb9352SDmitry Torokhov 		{ "reg-fixed-voltage", "gpios",        "enable-active-high" },
27034cb9352SDmitry Torokhov 		{ "reg-fixed-voltage", "gpio",         "enable-active-high" },
27134cb9352SDmitry Torokhov #endif
27234cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_GPIO)
27334cb9352SDmitry Torokhov 		{ "regulator-gpio",    "enable-gpio",  "enable-active-high" },
27434cb9352SDmitry Torokhov 		{ "regulator-gpio",    "enable-gpios", "enable-active-high" },
27534cb9352SDmitry Torokhov #endif
27698ac9e4fSBalamanikandan Gunasundar #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
27798ac9e4fSBalamanikandan Gunasundar 		{ "atmel,hsmci",       "cd-gpios",     "cd-inverted" },
27898ac9e4fSBalamanikandan Gunasundar #endif
27934cb9352SDmitry Torokhov 	};
28034cb9352SDmitry Torokhov 	unsigned int i;
28134cb9352SDmitry Torokhov 	bool active_high;
28234cb9352SDmitry Torokhov 
28398ac9e4fSBalamanikandan Gunasundar #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
28498ac9e4fSBalamanikandan Gunasundar 	/*
28598ac9e4fSBalamanikandan Gunasundar 	 * The Atmel HSMCI has compatible property in the parent node and
28698ac9e4fSBalamanikandan Gunasundar 	 * gpio property in a child node
28798ac9e4fSBalamanikandan Gunasundar 	 */
28898ac9e4fSBalamanikandan Gunasundar 	if (of_device_is_compatible(np->parent, "atmel,hsmci")) {
28998ac9e4fSBalamanikandan Gunasundar 		np_compat = np->parent;
29098ac9e4fSBalamanikandan Gunasundar 		np_propname = np;
29198ac9e4fSBalamanikandan Gunasundar 	}
29298ac9e4fSBalamanikandan Gunasundar #endif
29398ac9e4fSBalamanikandan Gunasundar 
29434cb9352SDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
29598ac9e4fSBalamanikandan Gunasundar 		if (of_device_is_compatible(np_compat, gpios[i].compatible) &&
29634cb9352SDmitry Torokhov 		    !strcmp(propname, gpios[i].gpio_propname)) {
29798ac9e4fSBalamanikandan Gunasundar 			active_high = of_property_read_bool(np_propname,
29834cb9352SDmitry Torokhov 						gpios[i].polarity_propname);
299e3186e36SDmitry Torokhov 			of_gpio_quirk_polarity(np, active_high, flags);
30034cb9352SDmitry Torokhov 			break;
301a603a2b8SLinus Walleij 		}
30234cb9352SDmitry Torokhov 	}
30334cb9352SDmitry Torokhov }
30434cb9352SDmitry Torokhov 
of_gpio_flags_quirks(const struct device_node * np,const char * propname,enum of_gpio_flags * flags,int index)30534cb9352SDmitry Torokhov static void of_gpio_flags_quirks(const struct device_node *np,
30634cb9352SDmitry Torokhov 				 const char *propname,
30734cb9352SDmitry Torokhov 				 enum of_gpio_flags *flags,
30834cb9352SDmitry Torokhov 				 int index)
30934cb9352SDmitry Torokhov {
31034cb9352SDmitry Torokhov 	of_gpio_try_fixup_polarity(np, propname, flags);
31134cb9352SDmitry Torokhov 	of_gpio_set_polarity_by_property(np, propname, flags);
31234cb9352SDmitry Torokhov 
313a603a2b8SLinus Walleij 	/*
314a603a2b8SLinus Walleij 	 * Legacy open drain handling for fixed voltage regulators.
315a603a2b8SLinus Walleij 	 */
316a603a2b8SLinus Walleij 	if (IS_ENABLED(CONFIG_REGULATOR) &&
317a603a2b8SLinus Walleij 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
318a603a2b8SLinus Walleij 	    of_property_read_bool(np, "gpio-open-drain")) {
319a603a2b8SLinus Walleij 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
320a603a2b8SLinus Walleij 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
321a603a2b8SLinus Walleij 			of_node_full_name(np));
322a603a2b8SLinus Walleij 	}
3236953c57aSLinus Walleij 
3246953c57aSLinus Walleij 	/*
3256953c57aSLinus Walleij 	 * Legacy handling of SPI active high chip select. If we have a
3266953c57aSLinus Walleij 	 * property named "cs-gpios" we need to inspect the child node
3276953c57aSLinus Walleij 	 * to determine if the flags should have inverted semantics.
3286953c57aSLinus Walleij 	 */
329da7f1349SLinus Walleij 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
330a71a81e7SGeert Uytterhoeven 	    of_property_read_bool(np, "cs-gpios")) {
3316953c57aSLinus Walleij 		struct device_node *child;
3326953c57aSLinus Walleij 		u32 cs;
3336953c57aSLinus Walleij 		int ret;
3346953c57aSLinus Walleij 
3356953c57aSLinus Walleij 		for_each_child_of_node(np, child) {
3366953c57aSLinus Walleij 			ret = of_property_read_u32(child, "reg", &cs);
337c1c04ceaSLinus Walleij 			if (ret)
3386953c57aSLinus Walleij 				continue;
3396953c57aSLinus Walleij 			if (cs == index) {
3406953c57aSLinus Walleij 				/*
3416953c57aSLinus Walleij 				 * SPI children have active low chip selects
3426953c57aSLinus Walleij 				 * by default. This can be specified negatively
3436953c57aSLinus Walleij 				 * by just omitting "spi-cs-high" in the
3446953c57aSLinus Walleij 				 * device node, or actively by tagging on
3456953c57aSLinus Walleij 				 * GPIO_ACTIVE_LOW as flag in the device
3466953c57aSLinus Walleij 				 * tree. If the line is simultaneously
3476953c57aSLinus Walleij 				 * tagged as active low in the device tree
3486953c57aSLinus Walleij 				 * and has the "spi-cs-high" set, we get a
3496953c57aSLinus Walleij 				 * conflict and the "spi-cs-high" flag will
3506953c57aSLinus Walleij 				 * take precedence.
3516953c57aSLinus Walleij 				 */
352e3186e36SDmitry Torokhov 				bool active_high = of_property_read_bool(child,
353e3186e36SDmitry Torokhov 								"spi-cs-high");
354e3186e36SDmitry Torokhov 				of_gpio_quirk_polarity(child, active_high,
355e3186e36SDmitry Torokhov 						       flags);
35689fea04cSNishka Dasgupta 				of_node_put(child);
3576953c57aSLinus Walleij 				break;
3586953c57aSLinus Walleij 			}
3596953c57aSLinus Walleij 		}
3606953c57aSLinus Walleij 	}
361edc1ef3fSMartin Blumenstingl 
362edc1ef3fSMartin Blumenstingl 	/* Legacy handling of stmmac's active-low PHY reset line */
363edc1ef3fSMartin Blumenstingl 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
364edc1ef3fSMartin Blumenstingl 	    !strcmp(propname, "snps,reset-gpio") &&
365edc1ef3fSMartin Blumenstingl 	    of_property_read_bool(np, "snps,reset-active-low"))
366edc1ef3fSMartin Blumenstingl 		*flags |= OF_GPIO_ACTIVE_LOW;
367a603a2b8SLinus Walleij }
368a603a2b8SLinus Walleij 
369f141ed65SGrant Likely /**
370af8b6375SAlexandre Courbot  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
371f141ed65SGrant Likely  * @np:		device node to get GPIO from
372f141ed65SGrant Likely  * @propname:	property name containing gpio specifier(s)
373f141ed65SGrant Likely  * @index:	index of the GPIO
374f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
375f141ed65SGrant Likely  *
376af8b6375SAlexandre Courbot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
377f141ed65SGrant Likely  * value on the error condition. If @flags is not NULL the function also fills
378f141ed65SGrant Likely  * in flags for the GPIO.
379f141ed65SGrant Likely  */
of_get_named_gpiod_flags(const struct device_node * np,const char * propname,int index,enum of_gpio_flags * flags)380e6ae9a83SKrzysztof Kozlowski static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
381af8b6375SAlexandre Courbot 		     const char *propname, int index, enum of_gpio_flags *flags)
382f141ed65SGrant Likely {
383762c2e46SMasahiro Yamada 	struct of_phandle_args gpiospec;
384762c2e46SMasahiro Yamada 	struct gpio_chip *chip;
385762c2e46SMasahiro Yamada 	struct gpio_desc *desc;
386f141ed65SGrant Likely 	int ret;
387f141ed65SGrant Likely 
388c11e6f0fSStephen Boyd 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
389762c2e46SMasahiro Yamada 					     &gpiospec);
3903d0f7cf0SGrant Likely 	if (ret) {
3917eb6ce2fSRob Herring 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
3927eb6ce2fSRob Herring 			__func__, propname, np, index);
393af8b6375SAlexandre Courbot 		return ERR_PTR(ret);
3943d0f7cf0SGrant Likely 	}
395f141ed65SGrant Likely 
396c7e9d398SMasahiro Yamada 	chip = of_find_gpiochip_by_xlate(&gpiospec);
397762c2e46SMasahiro Yamada 	if (!chip) {
398762c2e46SMasahiro Yamada 		desc = ERR_PTR(-EPROBE_DEFER);
399762c2e46SMasahiro Yamada 		goto out;
400762c2e46SMasahiro Yamada 	}
4013d0f7cf0SGrant Likely 
40299468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
403762c2e46SMasahiro Yamada 	if (IS_ERR(desc))
404762c2e46SMasahiro Yamada 		goto out;
405762c2e46SMasahiro Yamada 
406605f2d34SLinus Walleij 	if (flags)
40789a5e15bSLinus Walleij 		of_gpio_flags_quirks(np, propname, flags, index);
408a603a2b8SLinus Walleij 
4097eb6ce2fSRob Herring 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
4107eb6ce2fSRob Herring 		 __func__, propname, np, index,
411762c2e46SMasahiro Yamada 		 PTR_ERR_OR_ZERO(desc));
412762c2e46SMasahiro Yamada 
413762c2e46SMasahiro Yamada out:
414762c2e46SMasahiro Yamada 	of_node_put(gpiospec.np);
415762c2e46SMasahiro Yamada 
416762c2e46SMasahiro Yamada 	return desc;
417f141ed65SGrant Likely }
418f141ed65SGrant Likely 
41940fc56eeSDmitry Torokhov /**
42040fc56eeSDmitry Torokhov  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
42140fc56eeSDmitry Torokhov  * @np:		device node to get GPIO from
42240fc56eeSDmitry Torokhov  * @propname:	Name of property containing gpio specifier(s)
42340fc56eeSDmitry Torokhov  * @index:	index of the GPIO
42440fc56eeSDmitry Torokhov  *
42540fc56eeSDmitry Torokhov  * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
42640fc56eeSDmitry Torokhov  * value on the error condition.
42740fc56eeSDmitry Torokhov  */
of_get_named_gpio(const struct device_node * np,const char * propname,int index)42840fc56eeSDmitry Torokhov int of_get_named_gpio(const struct device_node *np, const char *propname,
42940fc56eeSDmitry Torokhov 		      int index)
430f01d9075SAlexandre Courbot {
431f01d9075SAlexandre Courbot 	struct gpio_desc *desc;
432f01d9075SAlexandre Courbot 
43340fc56eeSDmitry Torokhov 	desc = of_get_named_gpiod_flags(np, propname, index, NULL);
434f01d9075SAlexandre Courbot 
435f01d9075SAlexandre Courbot 	if (IS_ERR(desc))
436f01d9075SAlexandre Courbot 		return PTR_ERR(desc);
437f01d9075SAlexandre Courbot 	else
438f01d9075SAlexandre Courbot 		return desc_to_gpio(desc);
439f01d9075SAlexandre Courbot }
44040fc56eeSDmitry Torokhov EXPORT_SYMBOL_GPL(of_get_named_gpio);
441f01d9075SAlexandre Courbot 
442d9e7f0e3SDmitry Torokhov /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
of_convert_gpio_flags(enum of_gpio_flags flags)443d9e7f0e3SDmitry Torokhov static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
444d9e7f0e3SDmitry Torokhov {
445d9e7f0e3SDmitry Torokhov 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
446d9e7f0e3SDmitry Torokhov 
447d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_ACTIVE_LOW)
448d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_ACTIVE_LOW;
449d9e7f0e3SDmitry Torokhov 
450d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_SINGLE_ENDED) {
451d9e7f0e3SDmitry Torokhov 		if (flags & OF_GPIO_OPEN_DRAIN)
452d9e7f0e3SDmitry Torokhov 			lflags |= GPIO_OPEN_DRAIN;
453d9e7f0e3SDmitry Torokhov 		else
454d9e7f0e3SDmitry Torokhov 			lflags |= GPIO_OPEN_SOURCE;
455d9e7f0e3SDmitry Torokhov 	}
456d9e7f0e3SDmitry Torokhov 
457d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_TRANSITORY)
458d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_TRANSITORY;
459d9e7f0e3SDmitry Torokhov 
460d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_UP)
461d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_UP;
462d9e7f0e3SDmitry Torokhov 
463d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_DOWN)
464d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_DOWN;
465d9e7f0e3SDmitry Torokhov 
466d9e7f0e3SDmitry Torokhov 	if (flags & OF_GPIO_PULL_DISABLE)
467d9e7f0e3SDmitry Torokhov 		lflags |= GPIO_PULL_DISABLE;
468d9e7f0e3SDmitry Torokhov 
469d9e7f0e3SDmitry Torokhov 	return lflags;
470d9e7f0e3SDmitry Torokhov }
471d9e7f0e3SDmitry Torokhov 
of_find_gpio_rename(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)472b311c5cbSDmitry Torokhov static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
47398c3c940SDmitry Torokhov 					     const char *con_id,
47498c3c940SDmitry Torokhov 					     unsigned int idx,
475c8582339SLinus Walleij 					     enum of_gpio_flags *of_flags)
476c8582339SLinus Walleij {
477b311c5cbSDmitry Torokhov 	static const struct of_rename_gpio {
478b311c5cbSDmitry Torokhov 		const char *con_id;
479b311c5cbSDmitry Torokhov 		const char *legacy_id;	/* NULL - same as con_id */
480b311c5cbSDmitry Torokhov 		/*
481b311c5cbSDmitry Torokhov 		 * Compatible string can be set to NULL in case where
482b311c5cbSDmitry Torokhov 		 * matching to a particular compatible is not practical,
483b311c5cbSDmitry Torokhov 		 * but it should only be done for gpio names that have
484b311c5cbSDmitry Torokhov 		 * vendor prefix to reduce risk of false positives.
485b311c5cbSDmitry Torokhov 		 * Addition of such entries is strongly discouraged.
486b311c5cbSDmitry Torokhov 		 */
487b311c5cbSDmitry Torokhov 		const char *compatible;
488b311c5cbSDmitry Torokhov 	} gpios[] = {
489fbbbcd17SDmitry Torokhov #if !IS_ENABLED(CONFIG_LCD_HX8357)
490fbbbcd17SDmitry Torokhov 		/* Himax LCD controllers used "gpios-reset" */
491fbbbcd17SDmitry Torokhov 		{ "reset",	"gpios-reset",	"himax,hx8357" },
492fbbbcd17SDmitry Torokhov 		{ "reset",	"gpios-reset",	"himax,hx8369" },
493fbbbcd17SDmitry Torokhov #endif
494b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_MFD_ARIZONA)
495b311c5cbSDmitry Torokhov 		{ "wlf,reset",	NULL,		NULL },
496b311c5cbSDmitry Torokhov #endif
497eaf1a296SDmitry Torokhov #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
498eaf1a296SDmitry Torokhov 		{ "rtc-data",	"gpio-rtc-data",	"moxa,moxart-rtc" },
499eaf1a296SDmitry Torokhov 		{ "rtc-sclk",	"gpio-rtc-sclk",	"moxa,moxart-rtc" },
500eaf1a296SDmitry Torokhov 		{ "rtc-reset",	"gpio-rtc-reset",	"moxa,moxart-rtc" },
501eaf1a296SDmitry Torokhov #endif
5029c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
5039c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"marvell,nfc-i2c" },
5049c2cc717SDmitry Torokhov #endif
5059c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
5069c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"marvell,nfc-spi" },
5079c2cc717SDmitry Torokhov #endif
5089c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
5099c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"marvell,nfc-uart" },
5109c2cc717SDmitry Torokhov 		{ "reset",	"reset-n-io",	"mrvl,nfc-uart" },
5119c2cc717SDmitry Torokhov #endif
512895f5633SDmitry Torokhov #if IS_ENABLED(CONFIG_PCI_LANTIQ)
513fbbbcd17SDmitry Torokhov 		/* MIPS Lantiq PCI */
514895f5633SDmitry Torokhov 		{ "reset",	"gpio-reset",	"lantiq,pci-xway" },
515fbbbcd17SDmitry Torokhov #endif
516307c593bSDmitry Torokhov 
517b311c5cbSDmitry Torokhov 		/*
518b311c5cbSDmitry Torokhov 		 * Some regulator bindings happened before we managed to
519b311c5cbSDmitry Torokhov 		 * establish that GPIO properties should be named
520b311c5cbSDmitry Torokhov 		 * "foo-gpios" so we have this special kludge for them.
521b311c5cbSDmitry Torokhov 		 */
522307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
523b311c5cbSDmitry Torokhov 		{ "wlf,ldoena",  NULL,		NULL }, /* Arizona */
524307c593bSDmitry Torokhov #endif
525307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
526b311c5cbSDmitry Torokhov 		{ "wlf,ldo1ena", NULL,		NULL }, /* WM8994 */
527b311c5cbSDmitry Torokhov 		{ "wlf,ldo2ena", NULL,		NULL }, /* WM8994 */
528b311c5cbSDmitry Torokhov #endif
529c8582339SLinus Walleij 
530944004ebSDmitry Torokhov #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
531944004ebSDmitry Torokhov 		{ "reset",	"cirrus,gpio-nreset",	"cirrus,cs42l56" },
532944004ebSDmitry Torokhov #endif
533b7d773eaSLinus Walleij #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
534b7d773eaSLinus Walleij 		{ "i2s1-in-sel-gpio1",	NULL,	"mediatek,mt2701-cs42448-machine" },
535b7d773eaSLinus Walleij 		{ "i2s1-in-sel-gpio2",	NULL,	"mediatek,mt2701-cs42448-machine" },
536b7d773eaSLinus Walleij #endif
537fbbbcd17SDmitry Torokhov #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
538fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3x" },
539fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic33" },
540fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3007" },
541fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3104" },
542fbbbcd17SDmitry Torokhov 		{ "reset",	"gpio-reset",	"ti,tlv320aic3106" },
543fbbbcd17SDmitry Torokhov #endif
544307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_GPIO)
545c8582339SLinus Walleij 		/*
546b311c5cbSDmitry Torokhov 		 * The SPI GPIO bindings happened before we managed to
547b311c5cbSDmitry Torokhov 		 * establish that GPIO properties should be named
548b311c5cbSDmitry Torokhov 		 * "foo-gpios" so we have this special kludge for them.
549c8582339SLinus Walleij 		 */
550b311c5cbSDmitry Torokhov 		{ "miso",	"gpio-miso",	"spi-gpio" },
551b311c5cbSDmitry Torokhov 		{ "mosi",	"gpio-mosi",	"spi-gpio" },
552b311c5cbSDmitry Torokhov 		{ "sck",	"gpio-sck",	"spi-gpio" },
553307c593bSDmitry Torokhov #endif
554c8582339SLinus Walleij 
5556a537d48SLinus Walleij 		/*
556b311c5cbSDmitry Torokhov 		 * The old Freescale bindings use simply "gpios" as name
557b311c5cbSDmitry Torokhov 		 * for the chip select lines rather than "cs-gpios" like
558b311c5cbSDmitry Torokhov 		 * all other SPI hardware. Allow this specifically for
559b311c5cbSDmitry Torokhov 		 * Freescale and PPC devices.
560e3023bf8SLinus Walleij 		 */
561307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
562b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"fsl,spi" },
563b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"aeroflexgaisler,spictrl" },
564307c593bSDmitry Torokhov #endif
565307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_PPC4xx)
566b311c5cbSDmitry Torokhov 		{ "cs",		"gpios",	"ibm,ppc4xx-spi" },
567b311c5cbSDmitry Torokhov #endif
568307c593bSDmitry Torokhov 
569b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
570e3023bf8SLinus Walleij 		/*
571b311c5cbSDmitry Torokhov 		 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
572b311c5cbSDmitry Torokhov 		 * property without the compulsory "-gpios" suffix.
573e3023bf8SLinus Walleij 		 */
574b311c5cbSDmitry Torokhov 		{ "fcs,int_n",	NULL,		"fcs,fusb302" },
575b311c5cbSDmitry Torokhov #endif
5766a537d48SLinus Walleij 	};
577b311c5cbSDmitry Torokhov 	struct gpio_desc *desc;
578b311c5cbSDmitry Torokhov 	const char *legacy_id;
579b311c5cbSDmitry Torokhov 	unsigned int i;
5806a537d48SLinus Walleij 
5816a537d48SLinus Walleij 	if (!con_id)
5826a537d48SLinus Walleij 		return ERR_PTR(-ENOENT);
5836a537d48SLinus Walleij 
584b311c5cbSDmitry Torokhov 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
585b311c5cbSDmitry Torokhov 		if (strcmp(con_id, gpios[i].con_id))
586b311c5cbSDmitry Torokhov 			continue;
5876a537d48SLinus Walleij 
588b311c5cbSDmitry Torokhov 		if (gpios[i].compatible &&
589b311c5cbSDmitry Torokhov 		    !of_device_is_compatible(np, gpios[i].compatible))
590b311c5cbSDmitry Torokhov 			continue;
591b311c5cbSDmitry Torokhov 
592b311c5cbSDmitry Torokhov 		legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
593b311c5cbSDmitry Torokhov 		desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
594b311c5cbSDmitry Torokhov 		if (!gpiod_not_found(desc)) {
595b311c5cbSDmitry Torokhov 			pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
596b311c5cbSDmitry Torokhov 				of_node_full_name(np), legacy_id, con_id);
597b311c5cbSDmitry Torokhov 			return desc;
598b311c5cbSDmitry Torokhov 		}
5996a537d48SLinus Walleij 	}
6006a537d48SLinus Walleij 
60111c43bb0SDmitry Torokhov 	return ERR_PTR(-ENOENT);
6026e24826dSLinus Walleij }
6036e24826dSLinus Walleij 
of_find_mt2701_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)604326c3753SDmitry Torokhov static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
605326c3753SDmitry Torokhov 					     const char *con_id,
606326c3753SDmitry Torokhov 					     unsigned int idx,
607326c3753SDmitry Torokhov 					     enum of_gpio_flags *of_flags)
608326c3753SDmitry Torokhov {
609326c3753SDmitry Torokhov 	struct gpio_desc *desc;
610326c3753SDmitry Torokhov 	const char *legacy_id;
611326c3753SDmitry Torokhov 
612326c3753SDmitry Torokhov 	if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
613326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
614326c3753SDmitry Torokhov 
615326c3753SDmitry Torokhov 	if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
616326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
617326c3753SDmitry Torokhov 
618326c3753SDmitry Torokhov 	if (!con_id || strcmp(con_id, "i2s1-in-sel"))
619326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
620326c3753SDmitry Torokhov 
621326c3753SDmitry Torokhov 	if (idx == 0)
622326c3753SDmitry Torokhov 		legacy_id = "i2s1-in-sel-gpio1";
623326c3753SDmitry Torokhov 	else if (idx == 1)
624326c3753SDmitry Torokhov 		legacy_id = "i2s1-in-sel-gpio2";
625326c3753SDmitry Torokhov 	else
626326c3753SDmitry Torokhov 		return ERR_PTR(-ENOENT);
627326c3753SDmitry Torokhov 
628326c3753SDmitry Torokhov 	desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
629326c3753SDmitry Torokhov 	if (!gpiod_not_found(desc))
630326c3753SDmitry Torokhov 		pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
631326c3753SDmitry Torokhov 			of_node_full_name(np), legacy_id, con_id);
632326c3753SDmitry Torokhov 
633326c3753SDmitry Torokhov 	return desc;
634326c3753SDmitry Torokhov }
635326c3753SDmitry Torokhov 
636a2b5e207SDmitry Torokhov typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
637a2b5e207SDmitry Torokhov 						const char *con_id,
638a2b5e207SDmitry Torokhov 						unsigned int idx,
639a2b5e207SDmitry Torokhov 						enum of_gpio_flags *of_flags);
640a2b5e207SDmitry Torokhov static const of_find_gpio_quirk of_find_gpio_quirks[] = {
641b311c5cbSDmitry Torokhov 	of_find_gpio_rename,
642326c3753SDmitry Torokhov 	of_find_mt2701_gpio,
6438b10ca2fSMichael Walle 	NULL
644a2b5e207SDmitry Torokhov };
645a2b5e207SDmitry Torokhov 
of_find_gpio(struct device_node * np,const char * con_id,unsigned int idx,unsigned long * flags)64607445ae1SDmitry Torokhov struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
647fed7026aSAndy Shevchenko 			       unsigned int idx, unsigned long *flags)
648ea713bc4SLinus Walleij {
649ea713bc4SLinus Walleij 	char prop_name[32]; /* 32 is max size of property name */
650ea713bc4SLinus Walleij 	enum of_gpio_flags of_flags;
651a2b5e207SDmitry Torokhov 	const of_find_gpio_quirk *q;
652ea713bc4SLinus Walleij 	struct gpio_desc *desc;
653ea713bc4SLinus Walleij 	unsigned int i;
654ea713bc4SLinus Walleij 
655c8582339SLinus Walleij 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
656ea713bc4SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
657ea713bc4SLinus Walleij 		if (con_id)
658ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
659ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
660ea713bc4SLinus Walleij 		else
661ea713bc4SLinus Walleij 			snprintf(prop_name, sizeof(prop_name), "%s",
662ea713bc4SLinus Walleij 				 gpio_suffixes[i]);
663ea713bc4SLinus Walleij 
66407445ae1SDmitry Torokhov 		desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags);
6656662ae6aSMaxime Ripard 
6667b58696dSAndy Shevchenko 		if (!gpiod_not_found(desc))
667ea713bc4SLinus Walleij 			break;
668ea713bc4SLinus Walleij 	}
669ea713bc4SLinus Walleij 
670a2b5e207SDmitry Torokhov 	/* Properly named GPIO was not found, try workarounds */
671a2b5e207SDmitry Torokhov 	for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
67207445ae1SDmitry Torokhov 		desc = (*q)(np, con_id, idx, &of_flags);
6736e24826dSLinus Walleij 
674ea713bc4SLinus Walleij 	if (IS_ERR(desc))
675ea713bc4SLinus Walleij 		return desc;
676ea713bc4SLinus Walleij 
677d9e7f0e3SDmitry Torokhov 	*flags = of_convert_gpio_flags(of_flags);
678d449991cSThomas Petazzoni 
679ea713bc4SLinus Walleij 	return desc;
680ea713bc4SLinus Walleij }
681ea713bc4SLinus Walleij 
682f141ed65SGrant Likely /**
683fd7337fdSMarkus Pargmann  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
684f625d460SBenoit Parrot  * @np:		device node to get GPIO from
685be715343SMasahiro Yamada  * @chip:	GPIO chip whose hog is parsed
686a79fead5SGeert Uytterhoeven  * @idx:	Index of the GPIO to parse
687f625d460SBenoit Parrot  * @name:	GPIO line name
688fed7026aSAndy Shevchenko  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
689fed7026aSAndy Shevchenko  *		of_find_gpio() or of_parse_own_gpio()
690f625d460SBenoit Parrot  * @dflags:	gpiod_flags - optional GPIO initialization flags
691f625d460SBenoit Parrot  *
692f625d460SBenoit Parrot  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
693f625d460SBenoit Parrot  * value on the error condition.
694f625d460SBenoit Parrot  */
of_parse_own_gpio(struct device_node * np,struct gpio_chip * chip,unsigned int idx,const char ** name,unsigned long * lflags,enum gpiod_flags * dflags)695fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
696be715343SMasahiro Yamada 					   struct gpio_chip *chip,
697a79fead5SGeert Uytterhoeven 					   unsigned int idx, const char **name,
698fed7026aSAndy Shevchenko 					   unsigned long *lflags,
699f625d460SBenoit Parrot 					   enum gpiod_flags *dflags)
700f625d460SBenoit Parrot {
701f625d460SBenoit Parrot 	struct device_node *chip_np;
702f625d460SBenoit Parrot 	enum of_gpio_flags xlate_flags;
703be715343SMasahiro Yamada 	struct of_phandle_args gpiospec;
704be715343SMasahiro Yamada 	struct gpio_desc *desc;
705a79fead5SGeert Uytterhoeven 	unsigned int i;
706f625d460SBenoit Parrot 	u32 tmp;
7073f9547e1SMasahiro Yamada 	int ret;
708f625d460SBenoit Parrot 
70970d0fc42SAndy Shevchenko 	chip_np = dev_of_node(&chip->gpiodev->dev);
710f625d460SBenoit Parrot 	if (!chip_np)
711f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
712f625d460SBenoit Parrot 
713f625d460SBenoit Parrot 	xlate_flags = 0;
7142d6c06f5SAndy Shevchenko 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
71540941954SAndy Shevchenko 	*dflags = GPIOD_ASIS;
716f625d460SBenoit Parrot 
717f625d460SBenoit Parrot 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
718f625d460SBenoit Parrot 	if (ret)
719f625d460SBenoit Parrot 		return ERR_PTR(ret);
720f625d460SBenoit Parrot 
721be715343SMasahiro Yamada 	gpiospec.np = chip_np;
722be715343SMasahiro Yamada 	gpiospec.args_count = tmp;
723f625d460SBenoit Parrot 
724a79fead5SGeert Uytterhoeven 	for (i = 0; i < tmp; i++) {
725a79fead5SGeert Uytterhoeven 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
726a79fead5SGeert Uytterhoeven 						 &gpiospec.args[i]);
727f625d460SBenoit Parrot 		if (ret)
728f625d460SBenoit Parrot 			return ERR_PTR(ret);
729a79fead5SGeert Uytterhoeven 	}
730f625d460SBenoit Parrot 
73199468c1aSMasahiro Yamada 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
732be715343SMasahiro Yamada 	if (IS_ERR(desc))
733be715343SMasahiro Yamada 		return desc;
734f625d460SBenoit Parrot 
735d9e7f0e3SDmitry Torokhov 	*lflags = of_convert_gpio_flags(xlate_flags);
736f625d460SBenoit Parrot 
737f625d460SBenoit Parrot 	if (of_property_read_bool(np, "input"))
738f625d460SBenoit Parrot 		*dflags |= GPIOD_IN;
739f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-low"))
740f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_LOW;
741f625d460SBenoit Parrot 	else if (of_property_read_bool(np, "output-high"))
742f625d460SBenoit Parrot 		*dflags |= GPIOD_OUT_HIGH;
743f625d460SBenoit Parrot 	else {
74462cdcb6cSRob Herring 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
74562cdcb6cSRob Herring 			desc_to_gpio(desc), np);
746f625d460SBenoit Parrot 		return ERR_PTR(-EINVAL);
747f625d460SBenoit Parrot 	}
748f625d460SBenoit Parrot 
749f625d460SBenoit Parrot 	if (name && of_property_read_string(np, "line-name", name))
750f625d460SBenoit Parrot 		*name = np->name;
751f625d460SBenoit Parrot 
752be715343SMasahiro Yamada 	return desc;
753f625d460SBenoit Parrot }
754f625d460SBenoit Parrot 
755f625d460SBenoit Parrot /**
756bc21077eSGeert Uytterhoeven  * of_gpiochip_add_hog - Add all hogs in a hog device node
757bc21077eSGeert Uytterhoeven  * @chip:	gpio chip to act on
758bc21077eSGeert Uytterhoeven  * @hog:	device node describing the hogs
759bc21077eSGeert Uytterhoeven  *
760bc21077eSGeert Uytterhoeven  * Returns error if it fails otherwise 0 on success.
761bc21077eSGeert Uytterhoeven  */
of_gpiochip_add_hog(struct gpio_chip * chip,struct device_node * hog)762bc21077eSGeert Uytterhoeven static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
763bc21077eSGeert Uytterhoeven {
764bc21077eSGeert Uytterhoeven 	enum gpiod_flags dflags;
765bc21077eSGeert Uytterhoeven 	struct gpio_desc *desc;
766bc21077eSGeert Uytterhoeven 	unsigned long lflags;
767bc21077eSGeert Uytterhoeven 	const char *name;
768bc21077eSGeert Uytterhoeven 	unsigned int i;
769bc21077eSGeert Uytterhoeven 	int ret;
770bc21077eSGeert Uytterhoeven 
771bc21077eSGeert Uytterhoeven 	for (i = 0;; i++) {
772bc21077eSGeert Uytterhoeven 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
773bc21077eSGeert Uytterhoeven 		if (IS_ERR(desc))
774bc21077eSGeert Uytterhoeven 			break;
775bc21077eSGeert Uytterhoeven 
776bc21077eSGeert Uytterhoeven 		ret = gpiod_hog(desc, name, lflags, dflags);
777bc21077eSGeert Uytterhoeven 		if (ret < 0)
778bc21077eSGeert Uytterhoeven 			return ret;
77963636d95SGeert Uytterhoeven 
78063636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
78163636d95SGeert Uytterhoeven 		desc->hog = hog;
78263636d95SGeert Uytterhoeven #endif
783bc21077eSGeert Uytterhoeven 	}
784bc21077eSGeert Uytterhoeven 
785bc21077eSGeert Uytterhoeven 	return 0;
786bc21077eSGeert Uytterhoeven }
787bc21077eSGeert Uytterhoeven 
788bc21077eSGeert Uytterhoeven /**
789fd7337fdSMarkus Pargmann  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
790f625d460SBenoit Parrot  * @chip:	gpio chip to act on
791f625d460SBenoit Parrot  *
792f625d460SBenoit Parrot  * This is only used by of_gpiochip_add to request/set GPIO initial
793f625d460SBenoit Parrot  * configuration.
794ead066e6SGeert Uytterhoeven  * It returns error if it fails otherwise 0 on success.
795f625d460SBenoit Parrot  */
of_gpiochip_scan_gpios(struct gpio_chip * chip)796dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
797f625d460SBenoit Parrot {
798f625d460SBenoit Parrot 	struct device_node *np;
799dfbd379bSLaxman Dewangan 	int ret;
800f625d460SBenoit Parrot 
80170d0fc42SAndy Shevchenko 	for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) {
802f625d460SBenoit Parrot 		if (!of_property_read_bool(np, "gpio-hog"))
803f625d460SBenoit Parrot 			continue;
804f625d460SBenoit Parrot 
805bc21077eSGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, np);
80609e258afSWei Yongjun 		if (ret < 0) {
80709e258afSWei Yongjun 			of_node_put(np);
808dfbd379bSLaxman Dewangan 			return ret;
809f625d460SBenoit Parrot 		}
81063636d95SGeert Uytterhoeven 
81163636d95SGeert Uytterhoeven 		of_node_set_flag(np, OF_POPULATED);
81209e258afSWei Yongjun 	}
813dfbd379bSLaxman Dewangan 
814dfbd379bSLaxman Dewangan 	return 0;
815f625d460SBenoit Parrot }
816f625d460SBenoit Parrot 
81763636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC
81863636d95SGeert Uytterhoeven /**
81963636d95SGeert Uytterhoeven  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
82063636d95SGeert Uytterhoeven  * @chip:	gpio chip to act on
82163636d95SGeert Uytterhoeven  * @hog:	device node describing the hogs
82263636d95SGeert Uytterhoeven  */
of_gpiochip_remove_hog(struct gpio_chip * chip,struct device_node * hog)82363636d95SGeert Uytterhoeven static void of_gpiochip_remove_hog(struct gpio_chip *chip,
82463636d95SGeert Uytterhoeven 				   struct device_node *hog)
82563636d95SGeert Uytterhoeven {
82680c78fbeSAndy Shevchenko 	struct gpio_desc *desc;
82763636d95SGeert Uytterhoeven 
82857017eddSAndy Shevchenko 	for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
82980c78fbeSAndy Shevchenko 		if (desc->hog == hog)
83080c78fbeSAndy Shevchenko 			gpiochip_free_own_desc(desc);
83163636d95SGeert Uytterhoeven }
83263636d95SGeert Uytterhoeven 
of_gpiochip_match_node(struct gpio_chip * chip,void * data)83363636d95SGeert Uytterhoeven static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
83463636d95SGeert Uytterhoeven {
835597a8a88SAndy Shevchenko 	return device_match_of_node(&chip->gpiodev->dev, data);
83663636d95SGeert Uytterhoeven }
83763636d95SGeert Uytterhoeven 
of_find_gpiochip_by_node(struct device_node * np)83863636d95SGeert Uytterhoeven static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
83963636d95SGeert Uytterhoeven {
84063636d95SGeert Uytterhoeven 	return gpiochip_find(np, of_gpiochip_match_node);
84163636d95SGeert Uytterhoeven }
84263636d95SGeert Uytterhoeven 
of_gpio_notify(struct notifier_block * nb,unsigned long action,void * arg)84363636d95SGeert Uytterhoeven static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
84463636d95SGeert Uytterhoeven 			  void *arg)
84563636d95SGeert Uytterhoeven {
84663636d95SGeert Uytterhoeven 	struct of_reconfig_data *rd = arg;
84763636d95SGeert Uytterhoeven 	struct gpio_chip *chip;
84863636d95SGeert Uytterhoeven 	int ret;
84963636d95SGeert Uytterhoeven 
85063636d95SGeert Uytterhoeven 	/*
85163636d95SGeert Uytterhoeven 	 * This only supports adding and removing complete gpio-hog nodes.
85263636d95SGeert Uytterhoeven 	 * Modifying an existing gpio-hog node is not supported (except for
85363636d95SGeert Uytterhoeven 	 * changing its "status" property, which is treated the same as
85463636d95SGeert Uytterhoeven 	 * addition/removal).
85563636d95SGeert Uytterhoeven 	 */
85663636d95SGeert Uytterhoeven 	switch (of_reconfig_get_state_change(action, arg)) {
85763636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_ADD:
85863636d95SGeert Uytterhoeven 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
85963636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
86063636d95SGeert Uytterhoeven 
86163636d95SGeert Uytterhoeven 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
86263636d95SGeert Uytterhoeven 			return NOTIFY_OK;
86363636d95SGeert Uytterhoeven 
86463636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
86563636d95SGeert Uytterhoeven 		if (chip == NULL)
86663636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
86763636d95SGeert Uytterhoeven 
86863636d95SGeert Uytterhoeven 		ret = of_gpiochip_add_hog(chip, rd->dn);
86963636d95SGeert Uytterhoeven 		if (ret < 0) {
87063636d95SGeert Uytterhoeven 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
87163636d95SGeert Uytterhoeven 			       rd->dn);
87263636d95SGeert Uytterhoeven 			of_node_clear_flag(rd->dn, OF_POPULATED);
87363636d95SGeert Uytterhoeven 			return notifier_from_errno(ret);
87463636d95SGeert Uytterhoeven 		}
87563636d95SGeert Uytterhoeven 		break;
87663636d95SGeert Uytterhoeven 
87763636d95SGeert Uytterhoeven 	case OF_RECONFIG_CHANGE_REMOVE:
87863636d95SGeert Uytterhoeven 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
87963636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* already depopulated */
88063636d95SGeert Uytterhoeven 
88163636d95SGeert Uytterhoeven 		chip = of_find_gpiochip_by_node(rd->dn->parent);
88263636d95SGeert Uytterhoeven 		if (chip == NULL)
88363636d95SGeert Uytterhoeven 			return NOTIFY_OK;	/* not for us */
88463636d95SGeert Uytterhoeven 
88563636d95SGeert Uytterhoeven 		of_gpiochip_remove_hog(chip, rd->dn);
88663636d95SGeert Uytterhoeven 		of_node_clear_flag(rd->dn, OF_POPULATED);
88763636d95SGeert Uytterhoeven 		break;
88863636d95SGeert Uytterhoeven 	}
88963636d95SGeert Uytterhoeven 
89063636d95SGeert Uytterhoeven 	return NOTIFY_OK;
89163636d95SGeert Uytterhoeven }
89263636d95SGeert Uytterhoeven 
89363636d95SGeert Uytterhoeven struct notifier_block gpio_of_notifier = {
89463636d95SGeert Uytterhoeven 	.notifier_call = of_gpio_notify,
89563636d95SGeert Uytterhoeven };
89663636d95SGeert Uytterhoeven #endif /* CONFIG_OF_DYNAMIC */
89763636d95SGeert Uytterhoeven 
898f625d460SBenoit Parrot /**
89967049c50SThierry Reding  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
900f141ed65SGrant Likely  * @gc:		pointer to the gpio_chip structure
90167049c50SThierry Reding  * @gpiospec:	GPIO specifier as found in the device tree
902f141ed65SGrant Likely  * @flags:	a flags pointer to fill in
903f141ed65SGrant Likely  *
904f141ed65SGrant Likely  * This is simple translation function, suitable for the most 1:1 mapped
90567049c50SThierry Reding  * GPIO chips. This function performs only one sanity check: whether GPIO
906f141ed65SGrant Likely  * is less than ngpios (that is specified in the gpio_chip).
907f141ed65SGrant Likely  */
of_gpio_simple_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)908b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc,
909b0c7e73bSGeert Uytterhoeven 				const struct of_phandle_args *gpiospec,
910b0c7e73bSGeert Uytterhoeven 				u32 *flags)
911f141ed65SGrant Likely {
912f141ed65SGrant Likely 	/*
913f141ed65SGrant Likely 	 * We're discouraging gpio_cells < 2, since that way you'll have to
91420a8a968SColin Cronin 	 * write your own xlate function (that will have to retrieve the GPIO
915f141ed65SGrant Likely 	 * number and the flags from a single gpio cell -- this is possible,
916f141ed65SGrant Likely 	 * but not recommended).
917f141ed65SGrant Likely 	 */
918f141ed65SGrant Likely 	if (gc->of_gpio_n_cells < 2) {
919f141ed65SGrant Likely 		WARN_ON(1);
920f141ed65SGrant Likely 		return -EINVAL;
921f141ed65SGrant Likely 	}
922f141ed65SGrant Likely 
923f141ed65SGrant Likely 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
924f141ed65SGrant Likely 		return -EINVAL;
925f141ed65SGrant Likely 
9267b96c686SGrant Likely 	if (gpiospec->args[0] >= gc->ngpio)
927f141ed65SGrant Likely 		return -EINVAL;
928f141ed65SGrant Likely 
929f141ed65SGrant Likely 	if (flags)
930f141ed65SGrant Likely 		*flags = gpiospec->args[1];
931f141ed65SGrant Likely 
932f141ed65SGrant Likely 	return gpiospec->args[0];
933f141ed65SGrant Likely }
934f141ed65SGrant Likely 
935a99cc668SArnd Bergmann #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
936a99cc668SArnd Bergmann #include <linux/gpio/legacy-of-mm-gpiochip.h>
937f141ed65SGrant Likely /**
9383208b0f0SLinus Walleij  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
939f141ed65SGrant Likely  * @np:		device node of the GPIO chip
940f141ed65SGrant Likely  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
9413208b0f0SLinus Walleij  * @data:	driver data to store in the struct gpio_chip
942f141ed65SGrant Likely  *
943f141ed65SGrant Likely  * To use this function you should allocate and fill mm_gc with:
944f141ed65SGrant Likely  *
945f141ed65SGrant Likely  * 1) In the gpio_chip structure:
946f141ed65SGrant Likely  *    - all the callbacks
947f141ed65SGrant Likely  *    - of_gpio_n_cells
948f141ed65SGrant Likely  *    - of_xlate callback (optional)
949f141ed65SGrant Likely  *
950f141ed65SGrant Likely  * 3) In the of_mm_gpio_chip structure:
951f141ed65SGrant Likely  *    - save_regs callback (optional)
952f141ed65SGrant Likely  *
953f141ed65SGrant Likely  * If succeeded, this function will map bank's memory and will
954f141ed65SGrant Likely  * do all necessary work for you. Then you'll able to use .regs
955f141ed65SGrant Likely  * to manage GPIOs from the callbacks.
956f141ed65SGrant Likely  */
of_mm_gpiochip_add_data(struct device_node * np,struct of_mm_gpio_chip * mm_gc,void * data)9573208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np,
9583208b0f0SLinus Walleij 			    struct of_mm_gpio_chip *mm_gc,
9593208b0f0SLinus Walleij 			    void *data)
960f141ed65SGrant Likely {
961f141ed65SGrant Likely 	int ret = -ENOMEM;
962f141ed65SGrant Likely 	struct gpio_chip *gc = &mm_gc->gc;
963f141ed65SGrant Likely 
9647eb6ce2fSRob Herring 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
965f141ed65SGrant Likely 	if (!gc->label)
966f141ed65SGrant Likely 		goto err0;
967f141ed65SGrant Likely 
968f141ed65SGrant Likely 	mm_gc->regs = of_iomap(np, 0);
969f141ed65SGrant Likely 	if (!mm_gc->regs)
970f141ed65SGrant Likely 		goto err1;
971f141ed65SGrant Likely 
972f141ed65SGrant Likely 	gc->base = -1;
973f141ed65SGrant Likely 
974f141ed65SGrant Likely 	if (mm_gc->save_regs)
975f141ed65SGrant Likely 		mm_gc->save_regs(mm_gc);
976f141ed65SGrant Likely 
97777289b2fSAndy Shevchenko 	fwnode_handle_put(mm_gc->gc.fwnode);
97877289b2fSAndy Shevchenko 	mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
979f141ed65SGrant Likely 
9803208b0f0SLinus Walleij 	ret = gpiochip_add_data(gc, data);
981f141ed65SGrant Likely 	if (ret)
982f141ed65SGrant Likely 		goto err2;
983f141ed65SGrant Likely 
984f141ed65SGrant Likely 	return 0;
985f141ed65SGrant Likely err2:
9865d07a692SLiang He 	of_node_put(np);
987f141ed65SGrant Likely 	iounmap(mm_gc->regs);
988f141ed65SGrant Likely err1:
989f141ed65SGrant Likely 	kfree(gc->label);
990f141ed65SGrant Likely err0:
9917eb6ce2fSRob Herring 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
992f141ed65SGrant Likely 	return ret;
993f141ed65SGrant Likely }
9946d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
995f141ed65SGrant Likely 
996d621e8baSRicardo Ribalda Delgado /**
997d621e8baSRicardo Ribalda Delgado  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
998d621e8baSRicardo Ribalda Delgado  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
999d621e8baSRicardo Ribalda Delgado  */
of_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)1000d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
1001d621e8baSRicardo Ribalda Delgado {
1002d621e8baSRicardo Ribalda Delgado 	struct gpio_chip *gc = &mm_gc->gc;
1003d621e8baSRicardo Ribalda Delgado 
1004d621e8baSRicardo Ribalda Delgado 	gpiochip_remove(gc);
1005d621e8baSRicardo Ribalda Delgado 	iounmap(mm_gc->regs);
1006d621e8baSRicardo Ribalda Delgado 	kfree(gc->label);
1007d621e8baSRicardo Ribalda Delgado }
10086d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
1009a99cc668SArnd Bergmann #endif
1010d621e8baSRicardo Ribalda Delgado 
1011f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)101228355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
1013f23f1516SShiraz Hashim {
1014f23f1516SShiraz Hashim 	struct of_phandle_args pinspec;
10151e63d7b9SLinus Walleij 	struct pinctrl_dev *pctldev;
101670d0fc42SAndy Shevchenko 	struct device_node *np;
1017f23f1516SShiraz Hashim 	int index = 0, ret;
1018586a87e6SChristian Ruppert 	const char *name;
1019586a87e6SChristian Ruppert 	static const char group_names_propname[] = "gpio-ranges-group-names";
1020586a87e6SChristian Ruppert 	struct property *group_names;
1021f23f1516SShiraz Hashim 
102270d0fc42SAndy Shevchenko 	np = dev_of_node(&chip->gpiodev->dev);
1023f23f1516SShiraz Hashim 	if (!np)
102428355f81STomeu Vizoso 		return 0;
1025f23f1516SShiraz Hashim 
1026586a87e6SChristian Ruppert 	group_names = of_find_property(np, group_names_propname, NULL);
1027586a87e6SChristian Ruppert 
1028ad4e1a7cSHaojian Zhuang 	for (;; index++) {
1029d9fe0039SStephen Warren 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1030d9fe0039SStephen Warren 				index, &pinspec);
1031f23f1516SShiraz Hashim 		if (ret)
1032f23f1516SShiraz Hashim 			break;
1033f23f1516SShiraz Hashim 
10341e63d7b9SLinus Walleij 		pctldev = of_pinctrl_get(pinspec.np);
1035602cf638SMasahiro Yamada 		of_node_put(pinspec.np);
10361e63d7b9SLinus Walleij 		if (!pctldev)
103728355f81STomeu Vizoso 			return -EPROBE_DEFER;
1038f23f1516SShiraz Hashim 
1039586a87e6SChristian Ruppert 		if (pinspec.args[2]) {
1040586a87e6SChristian Ruppert 			if (group_names) {
104172858602SLaurent Navet 				of_property_read_string_index(np,
1042586a87e6SChristian Ruppert 						group_names_propname,
1043586a87e6SChristian Ruppert 						index, &name);
1044586a87e6SChristian Ruppert 				if (strlen(name)) {
10457eb6ce2fSRob Herring 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
10467eb6ce2fSRob Herring 						np);
1047586a87e6SChristian Ruppert 					break;
1048586a87e6SChristian Ruppert 				}
1049586a87e6SChristian Ruppert 			}
1050586a87e6SChristian Ruppert 			/* npins != 0: linear range */
10511e63d7b9SLinus Walleij 			ret = gpiochip_add_pin_range(chip,
1052ef5e3eefSHaojian Zhuang 					pinctrl_dev_get_devname(pctldev),
10531e63d7b9SLinus Walleij 					pinspec.args[0],
105486853c83SHaojian Zhuang 					pinspec.args[1],
105586853c83SHaojian Zhuang 					pinspec.args[2]);
10561e63d7b9SLinus Walleij 			if (ret)
105728355f81STomeu Vizoso 				return ret;
1058586a87e6SChristian Ruppert 		} else {
1059586a87e6SChristian Ruppert 			/* npins == 0: special range */
1060586a87e6SChristian Ruppert 			if (pinspec.args[1]) {
10617eb6ce2fSRob Herring 				pr_err("%pOF: Illegal gpio-range format.\n",
10627eb6ce2fSRob Herring 					np);
1063586a87e6SChristian Ruppert 				break;
1064586a87e6SChristian Ruppert 			}
1065586a87e6SChristian Ruppert 
1066586a87e6SChristian Ruppert 			if (!group_names) {
10677eb6ce2fSRob Herring 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
10687eb6ce2fSRob Herring 					np, group_names_propname);
1069586a87e6SChristian Ruppert 				break;
1070586a87e6SChristian Ruppert 			}
1071586a87e6SChristian Ruppert 
1072586a87e6SChristian Ruppert 			ret = of_property_read_string_index(np,
1073586a87e6SChristian Ruppert 						group_names_propname,
1074586a87e6SChristian Ruppert 						index, &name);
1075586a87e6SChristian Ruppert 			if (ret)
1076586a87e6SChristian Ruppert 				break;
1077586a87e6SChristian Ruppert 
1078586a87e6SChristian Ruppert 			if (!strlen(name)) {
10797eb6ce2fSRob Herring 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
10807eb6ce2fSRob Herring 				np);
1081586a87e6SChristian Ruppert 				break;
1082586a87e6SChristian Ruppert 			}
1083586a87e6SChristian Ruppert 
1084586a87e6SChristian Ruppert 			ret = gpiochip_add_pingroup_range(chip, pctldev,
1085586a87e6SChristian Ruppert 						pinspec.args[0], name);
1086586a87e6SChristian Ruppert 			if (ret)
108728355f81STomeu Vizoso 				return ret;
1088586a87e6SChristian Ruppert 		}
1089ad4e1a7cSHaojian Zhuang 	}
109028355f81STomeu Vizoso 
109128355f81STomeu Vizoso 	return 0;
1092f23f1516SShiraz Hashim }
1093f23f1516SShiraz Hashim 
1094f23f1516SShiraz Hashim #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)109528355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1096f23f1516SShiraz Hashim #endif
1097f23f1516SShiraz Hashim 
of_gpiochip_add(struct gpio_chip * chip)109828355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip)
1099f141ed65SGrant Likely {
11008afe8255SAndy Shevchenko 	struct device_node *np;
1101f0d1ab05SLinus Walleij 	int ret;
110228355f81STomeu Vizoso 
110370d0fc42SAndy Shevchenko 	np = dev_of_node(&chip->gpiodev->dev);
11048afe8255SAndy Shevchenko 	if (!np)
110528355f81STomeu Vizoso 		return 0;
1106f141ed65SGrant Likely 
1107f141ed65SGrant Likely 	if (!chip->of_xlate) {
1108f141ed65SGrant Likely 		chip->of_gpio_n_cells = 2;
1109f141ed65SGrant Likely 		chip->of_xlate = of_gpio_simple_xlate;
1110f141ed65SGrant Likely 	}
1111f141ed65SGrant Likely 
11121020dfd1SMasahiro Yamada 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
11131020dfd1SMasahiro Yamada 		return -EINVAL;
11141020dfd1SMasahiro Yamada 
1115f0d1ab05SLinus Walleij 	ret = of_gpiochip_add_pin_range(chip);
1116f0d1ab05SLinus Walleij 	if (ret)
1117f0d1ab05SLinus Walleij 		return ret;
111828355f81STomeu Vizoso 
111959a4a351SAndy Shevchenko 	of_node_get(np);
1120f625d460SBenoit Parrot 
1121f0d1ab05SLinus Walleij 	ret = of_gpiochip_scan_gpios(chip);
11222f4133bbSAndy Shevchenko 	if (ret)
112359a4a351SAndy Shevchenko 		of_node_put(np);
1124f7299d44SGeert Uytterhoeven 
1125f0d1ab05SLinus Walleij 	return ret;
1126f141ed65SGrant Likely }
1127f141ed65SGrant Likely 
of_gpiochip_remove(struct gpio_chip * chip)1128f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip)
1129f141ed65SGrant Likely {
113059a4a351SAndy Shevchenko 	of_node_put(dev_of_node(&chip->gpiodev->dev));
1131f141ed65SGrant Likely }
1132