• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * OF helpers for the GPIO API
4  *
5  * Copyright (c) 2007-2008  MontaVista Software, Inc.
6  *
7  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8  */
9 
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_gpio.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio/machine.h>
24 
25 #include "gpiolib.h"
26 #include "gpiolib-of.h"
27 
28 /*
29  * This is Linux-specific flags. By default controllers' and Linux' mapping
30  * match, but GPIO controllers are free to translate their own flags to
31  * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended.
32  */
33 enum of_gpio_flags {
34 	OF_GPIO_ACTIVE_LOW = 0x1,
35 	OF_GPIO_SINGLE_ENDED = 0x2,
36 	OF_GPIO_OPEN_DRAIN = 0x4,
37 	OF_GPIO_TRANSITORY = 0x8,
38 	OF_GPIO_PULL_UP = 0x10,
39 	OF_GPIO_PULL_DOWN = 0x20,
40 	OF_GPIO_PULL_DISABLE = 0x40,
41 };
42 
43 /**
44  * of_gpio_named_count() - Count GPIOs for a device
45  * @np:		device node to count GPIOs for
46  * @propname:	property name containing gpio specifier(s)
47  *
48  * The function returns the count of GPIOs specified for a node.
49  * NOTE: The empty GPIO specifiers count too.
50  *
51  * Returns:
52  * Either number of GPIOs defined in the property, or
53  * *  %-EINVAL for an incorrectly formed "gpios" property, or
54  * *  %-ENOENT for a missing "gpios" property.
55  *
56  * Example::
57  *
58  *     gpios = <0
59  *              &gpio1 1 2
60  *              0
61  *              &gpio2 3 4>;
62  *
63  * The above example defines four GPIOs, two of which are not specified.
64  * This function will return '4'
65  */
of_gpio_named_count(const struct device_node * np,const char * propname)66 static int of_gpio_named_count(const struct device_node *np,
67 			       const char *propname)
68 {
69 	return of_count_phandle_with_args(np, propname, "#gpio-cells");
70 }
71 
72 /**
73  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
74  * @np:    Consuming device node
75  * @con_id: Function within the GPIO consumer
76  *
77  * Some elder GPIO controllers need special quirks. Currently we handle
78  * the Freescale and PPC GPIO controller with bindings that doesn't use the
79  * established "cs-gpios" for chip selects but instead rely on
80  * "gpios" for the chip select lines. If we detect this, we redirect
81  * the counting of "cs-gpios" to count "gpios" transparent to the
82  * driver.
83  *
84  * Returns:
85  * Either number of GPIOs defined in the property, or
86  * *  %-EINVAL for an incorrectly formed "gpios" property, or
87  * *  %-ENOENT for a missing "gpios" property.
88  */
of_gpio_spi_cs_get_count(const struct device_node * np,const char * con_id)89 static int of_gpio_spi_cs_get_count(const struct device_node *np,
90 				    const char *con_id)
91 {
92 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
93 		return 0;
94 	if (!con_id || strcmp(con_id, "cs"))
95 		return 0;
96 	if (!of_device_is_compatible(np, "fsl,spi") &&
97 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
98 	    !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
99 		return 0;
100 	return of_gpio_named_count(np, "gpios");
101 }
102 
of_gpio_count(const struct fwnode_handle * fwnode,const char * con_id)103 int of_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
104 {
105 	const struct device_node *np = to_of_node(fwnode);
106 	int ret;
107 	char propname[32];
108 
109 	ret = of_gpio_spi_cs_get_count(np, con_id);
110 	if (ret > 0)
111 		return ret;
112 
113 	for_each_gpio_property_name(propname, con_id) {
114 		ret = of_gpio_named_count(np, propname);
115 		if (ret > 0)
116 			break;
117 	}
118 	return ret ? ret : -ENOENT;
119 }
120 
of_gpiochip_match_node_and_xlate(struct gpio_chip * chip,const void * data)121 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip,
122 					    const void *data)
123 {
124 	const struct of_phandle_args *gpiospec = data;
125 
126 	return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) &&
127 				chip->of_xlate &&
128 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
129 }
130 
131 static struct gpio_device *
of_find_gpio_device_by_xlate(const struct of_phandle_args * gpiospec)132 of_find_gpio_device_by_xlate(const struct of_phandle_args *gpiospec)
133 {
134 	return gpio_device_find(gpiospec, of_gpiochip_match_node_and_xlate);
135 }
136 
of_xlate_and_get_gpiod_flags(struct gpio_chip * chip,struct of_phandle_args * gpiospec,enum of_gpio_flags * flags)137 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
138 					struct of_phandle_args *gpiospec,
139 					enum of_gpio_flags *flags)
140 {
141 	int ret;
142 
143 	if (chip->of_gpio_n_cells != gpiospec->args_count)
144 		return ERR_PTR(-EINVAL);
145 
146 	ret = chip->of_xlate(chip, gpiospec, flags);
147 	if (ret < 0)
148 		return ERR_PTR(ret);
149 
150 	return gpiochip_get_desc(chip, ret);
151 }
152 
153 /*
154  * Overrides stated polarity of a gpio line and warns when there is a
155  * discrepancy.
156  */
of_gpio_quirk_polarity(const struct device_node * np,bool active_high,enum of_gpio_flags * flags)157 static void of_gpio_quirk_polarity(const struct device_node *np,
158 				   bool active_high,
159 				   enum of_gpio_flags *flags)
160 {
161 	if (active_high) {
162 		if (*flags & OF_GPIO_ACTIVE_LOW) {
163 			pr_warn("%s GPIO handle specifies active low - ignored\n",
164 				of_node_full_name(np));
165 			*flags &= ~OF_GPIO_ACTIVE_LOW;
166 		}
167 	} else {
168 		if (!(*flags & OF_GPIO_ACTIVE_LOW))
169 			pr_info("%s enforce active low on GPIO handle\n",
170 				of_node_full_name(np));
171 		*flags |= OF_GPIO_ACTIVE_LOW;
172 	}
173 }
174 
175 /*
176  * This quirk does static polarity overrides in cases where existing
177  * DTS specified incorrect polarity.
178  */
of_gpio_try_fixup_polarity(const struct device_node * np,const char * propname,enum of_gpio_flags * flags)179 static void of_gpio_try_fixup_polarity(const struct device_node *np,
180 				       const char *propname,
181 				       enum of_gpio_flags *flags)
182 {
183 	static const struct {
184 		const char *compatible;
185 		const char *propname;
186 		bool active_high;
187 	} gpios[] = {
188 #if IS_ENABLED(CONFIG_LCD_HX8357)
189 		/*
190 		 * Himax LCD controllers used incorrectly named
191 		 * "gpios-reset" property and also specified wrong
192 		 * polarity.
193 		 */
194 		{ "himax,hx8357",	"gpios-reset",	false },
195 		{ "himax,hx8369",	"gpios-reset",	false },
196 #endif
197 #if IS_ENABLED(CONFIG_MTD_NAND_JZ4780)
198 		/*
199 		 * The rb-gpios semantics was undocumented and qi,lb60 (along with
200 		 * the ingenic driver) got it wrong. The active state encodes the
201 		 * NAND ready state, which is high level. Since there's no signal
202 		 * inverter on this board, it should be active-high. Let's fix that
203 		 * here for older DTs so we can re-use the generic nand_gpio_waitrdy()
204 		 * helper, and be consistent with what other drivers do.
205 		 */
206 		{ "qi,lb60",		"rb-gpios",	true },
207 #endif
208 #if IS_ENABLED(CONFIG_PCI_LANTIQ)
209 		/*
210 		 * According to the PCI specification, the RST# pin is an
211 		 * active-low signal. However, most of the device trees that
212 		 * have been widely used for a long time incorrectly describe
213 		 * reset GPIO as active-high, and were also using wrong name
214 		 * for the property.
215 		 */
216 		{ "lantiq,pci-xway",	"gpio-reset",	false },
217 #endif
218 #if IS_ENABLED(CONFIG_REGULATOR_S5M8767)
219 		/*
220 		 * According to S5M8767, the DVS and DS pin are
221 		 * active-high signals. However, exynos5250-spring.dts use
222 		 * active-low setting.
223 		 */
224 		{ "samsung,s5m8767-pmic", "s5m8767,pmic-buck-dvs-gpios", true },
225 		{ "samsung,s5m8767-pmic", "s5m8767,pmic-buck-ds-gpios", true },
226 #endif
227 #if IS_ENABLED(CONFIG_TOUCHSCREEN_TSC2005)
228 		/*
229 		 * DTS for Nokia N900 incorrectly specified "active high"
230 		 * polarity for the reset line, while the chip actually
231 		 * treats it as "active low".
232 		 */
233 		{ "ti,tsc2005",		"reset-gpios",	false },
234 #endif
235 	};
236 	unsigned int i;
237 
238 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
239 		if (of_device_is_compatible(np, gpios[i].compatible) &&
240 		    !strcmp(propname, gpios[i].propname)) {
241 			of_gpio_quirk_polarity(np, gpios[i].active_high, flags);
242 			break;
243 		}
244 	}
245 }
246 
of_gpio_set_polarity_by_property(const struct device_node * np,const char * propname,enum of_gpio_flags * flags)247 static void of_gpio_set_polarity_by_property(const struct device_node *np,
248 					     const char *propname,
249 					     enum of_gpio_flags *flags)
250 {
251 	const struct device_node *np_compat = np;
252 	const struct device_node *np_propname = np;
253 	static const struct {
254 		const char *compatible;
255 		const char *gpio_propname;
256 		const char *polarity_propname;
257 	} gpios[] = {
258 #if IS_ENABLED(CONFIG_FEC)
259 		/* Freescale Fast Ethernet Controller */
260 		{ "fsl,imx25-fec",   "phy-reset-gpios", "phy-reset-active-high" },
261 		{ "fsl,imx27-fec",   "phy-reset-gpios", "phy-reset-active-high" },
262 		{ "fsl,imx28-fec",   "phy-reset-gpios", "phy-reset-active-high" },
263 		{ "fsl,imx6q-fec",   "phy-reset-gpios", "phy-reset-active-high" },
264 		{ "fsl,mvf600-fec",  "phy-reset-gpios", "phy-reset-active-high" },
265 		{ "fsl,imx6sx-fec",  "phy-reset-gpios", "phy-reset-active-high" },
266 		{ "fsl,imx6ul-fec",  "phy-reset-gpios", "phy-reset-active-high" },
267 		{ "fsl,imx8mq-fec",  "phy-reset-gpios", "phy-reset-active-high" },
268 		{ "fsl,imx8qm-fec",  "phy-reset-gpios", "phy-reset-active-high" },
269 		{ "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" },
270 #endif
271 #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
272 		{ "atmel,hsmci",       "cd-gpios",     "cd-inverted" },
273 #endif
274 #if IS_ENABLED(CONFIG_PCI_IMX6)
275 		{ "fsl,imx6q-pcie",  "reset-gpio", "reset-gpio-active-high" },
276 		{ "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" },
277 		{ "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" },
278 		{ "fsl,imx7d-pcie",  "reset-gpio", "reset-gpio-active-high" },
279 		{ "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" },
280 		{ "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" },
281 		{ "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" },
282 #endif
283 
284 		/*
285 		 * The regulator GPIO handles are specified such that the
286 		 * presence or absence of "enable-active-high" solely controls
287 		 * the polarity of the GPIO line. Any phandle flags must
288 		 * be actively ignored.
289 		 */
290 #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE)
291 		{ "regulator-fixed",   "gpios",        "enable-active-high" },
292 		{ "regulator-fixed",   "gpio",         "enable-active-high" },
293 		{ "reg-fixed-voltage", "gpios",        "enable-active-high" },
294 		{ "reg-fixed-voltage", "gpio",         "enable-active-high" },
295 #endif
296 #if IS_ENABLED(CONFIG_REGULATOR_GPIO)
297 		{ "regulator-gpio",    "enable-gpio",  "enable-active-high" },
298 		{ "regulator-gpio",    "enable-gpios", "enable-active-high" },
299 #endif
300 	};
301 	unsigned int i;
302 	bool active_high;
303 
304 #if IS_ENABLED(CONFIG_MMC_ATMELMCI)
305 	/*
306 	 * The Atmel HSMCI has compatible property in the parent node and
307 	 * gpio property in a child node
308 	 */
309 	if (of_device_is_compatible(np->parent, "atmel,hsmci")) {
310 		np_compat = np->parent;
311 		np_propname = np;
312 	}
313 #endif
314 
315 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
316 		if (of_device_is_compatible(np_compat, gpios[i].compatible) &&
317 		    !strcmp(propname, gpios[i].gpio_propname)) {
318 			active_high = of_property_read_bool(np_propname,
319 						gpios[i].polarity_propname);
320 			of_gpio_quirk_polarity(np, active_high, flags);
321 			break;
322 		}
323 	}
324 }
325 
of_gpio_flags_quirks(const struct device_node * np,const char * propname,enum of_gpio_flags * flags,int index)326 static void of_gpio_flags_quirks(const struct device_node *np,
327 				 const char *propname,
328 				 enum of_gpio_flags *flags,
329 				 int index)
330 {
331 	of_gpio_try_fixup_polarity(np, propname, flags);
332 	of_gpio_set_polarity_by_property(np, propname, flags);
333 
334 	/*
335 	 * Legacy open drain handling for fixed voltage regulators.
336 	 */
337 	if (IS_ENABLED(CONFIG_REGULATOR) &&
338 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
339 	    of_property_read_bool(np, "gpio-open-drain")) {
340 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
341 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
342 			of_node_full_name(np));
343 	}
344 
345 	/*
346 	 * Legacy handling of SPI active high chip select. If we have a
347 	 * property named "cs-gpios" we need to inspect the child node
348 	 * to determine if the flags should have inverted semantics.
349 	 */
350 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
351 	    of_property_read_bool(np, "cs-gpios")) {
352 		u32 cs;
353 		int ret;
354 
355 		for_each_child_of_node_scoped(np, child) {
356 			ret = of_property_read_u32(child, "reg", &cs);
357 			if (ret)
358 				continue;
359 			if (cs == index) {
360 				/*
361 				 * SPI children have active low chip selects
362 				 * by default. This can be specified negatively
363 				 * by just omitting "spi-cs-high" in the
364 				 * device node, or actively by tagging on
365 				 * GPIO_ACTIVE_LOW as flag in the device
366 				 * tree. If the line is simultaneously
367 				 * tagged as active low in the device tree
368 				 * and has the "spi-cs-high" set, we get a
369 				 * conflict and the "spi-cs-high" flag will
370 				 * take precedence.
371 				 */
372 				bool active_high = of_property_read_bool(child,
373 								"spi-cs-high");
374 				of_gpio_quirk_polarity(child, active_high,
375 						       flags);
376 				break;
377 			}
378 		}
379 	}
380 
381 	/* Legacy handling of stmmac's active-low PHY reset line */
382 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
383 	    !strcmp(propname, "snps,reset-gpio") &&
384 	    of_property_read_bool(np, "snps,reset-active-low"))
385 		*flags |= OF_GPIO_ACTIVE_LOW;
386 }
387 
388 /**
389  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
390  * @np:		device node to get GPIO from
391  * @propname:	property name containing gpio specifier(s)
392  * @index:	index of the GPIO
393  * @flags:	a flags pointer to fill in
394  *
395  * Returns:
396  * GPIO descriptor to use with Linux GPIO API, or one of the errno
397  * value on the error condition. If @flags is not NULL the function also fills
398  * in flags for the GPIO.
399  */
of_get_named_gpiod_flags(const struct device_node * np,const char * propname,int index,enum of_gpio_flags * flags)400 static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
401 		     const char *propname, int index, enum of_gpio_flags *flags)
402 {
403 	struct of_phandle_args gpiospec;
404 	struct gpio_desc *desc;
405 	int ret;
406 
407 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
408 					     &gpiospec);
409 	if (ret) {
410 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
411 			__func__, propname, np, index);
412 		return ERR_PTR(ret);
413 	}
414 
415 	struct gpio_device *gdev __free(gpio_device_put) =
416 				of_find_gpio_device_by_xlate(&gpiospec);
417 	if (!gdev) {
418 		desc = ERR_PTR(-EPROBE_DEFER);
419 		goto out;
420 	}
421 
422 	desc = of_xlate_and_get_gpiod_flags(gpio_device_get_chip(gdev),
423 					    &gpiospec, flags);
424 	if (IS_ERR(desc))
425 		goto out;
426 
427 	if (flags)
428 		of_gpio_flags_quirks(np, propname, flags, index);
429 
430 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
431 		 __func__, propname, np, index,
432 		 PTR_ERR_OR_ZERO(desc));
433 
434 out:
435 	of_node_put(gpiospec.np);
436 
437 	return desc;
438 }
439 
440 /**
441  * of_get_named_gpio() - Get a GPIO number to use with GPIO API
442  * @np:		device node to get GPIO from
443  * @propname:	Name of property containing gpio specifier(s)
444  * @index:	index of the GPIO
445  *
446  * **DEPRECATED** This function is deprecated and must not be used in new code.
447  *
448  * Returns:
449  * GPIO number to use with Linux generic GPIO API, or one of the errno
450  * value on the error condition.
451  */
of_get_named_gpio(const struct device_node * np,const char * propname,int index)452 int of_get_named_gpio(const struct device_node *np, const char *propname,
453 		      int index)
454 {
455 	struct gpio_desc *desc;
456 
457 	desc = of_get_named_gpiod_flags(np, propname, index, NULL);
458 
459 	if (IS_ERR(desc))
460 		return PTR_ERR(desc);
461 	else
462 		return desc_to_gpio(desc);
463 }
464 EXPORT_SYMBOL_GPL(of_get_named_gpio);
465 
466 /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
of_convert_gpio_flags(enum of_gpio_flags flags)467 static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
468 {
469 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
470 
471 	if (flags & OF_GPIO_ACTIVE_LOW)
472 		lflags |= GPIO_ACTIVE_LOW;
473 
474 	if (flags & OF_GPIO_SINGLE_ENDED) {
475 		if (flags & OF_GPIO_OPEN_DRAIN)
476 			lflags |= GPIO_OPEN_DRAIN;
477 		else
478 			lflags |= GPIO_OPEN_SOURCE;
479 	}
480 
481 	if (flags & OF_GPIO_TRANSITORY)
482 		lflags |= GPIO_TRANSITORY;
483 
484 	if (flags & OF_GPIO_PULL_UP)
485 		lflags |= GPIO_PULL_UP;
486 
487 	if (flags & OF_GPIO_PULL_DOWN)
488 		lflags |= GPIO_PULL_DOWN;
489 
490 	if (flags & OF_GPIO_PULL_DISABLE)
491 		lflags |= GPIO_PULL_DISABLE;
492 
493 	return lflags;
494 }
495 
of_find_gpio_rename(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)496 static struct gpio_desc *of_find_gpio_rename(struct device_node *np,
497 					     const char *con_id,
498 					     unsigned int idx,
499 					     enum of_gpio_flags *of_flags)
500 {
501 	static const struct of_rename_gpio {
502 		const char *con_id;
503 		const char *legacy_id;	/* NULL - same as con_id */
504 		/*
505 		 * Compatible string can be set to NULL in case where
506 		 * matching to a particular compatible is not practical,
507 		 * but it should only be done for gpio names that have
508 		 * vendor prefix to reduce risk of false positives.
509 		 * Addition of such entries is strongly discouraged.
510 		 */
511 		const char *compatible;
512 	} gpios[] = {
513 #if IS_ENABLED(CONFIG_LCD_HX8357)
514 		/* Himax LCD controllers used "gpios-reset" */
515 		{ "reset",	"gpios-reset",	"himax,hx8357" },
516 		{ "reset",	"gpios-reset",	"himax,hx8369" },
517 #endif
518 #if IS_ENABLED(CONFIG_MFD_ARIZONA)
519 		{ "wlf,reset",	NULL,		NULL },
520 #endif
521 #if IS_ENABLED(CONFIG_RTC_DRV_MOXART)
522 		{ "rtc-data",	"gpio-rtc-data",	"moxa,moxart-rtc" },
523 		{ "rtc-sclk",	"gpio-rtc-sclk",	"moxa,moxart-rtc" },
524 		{ "rtc-reset",	"gpio-rtc-reset",	"moxa,moxart-rtc" },
525 #endif
526 #if IS_ENABLED(CONFIG_NFC_MRVL_I2C)
527 		{ "reset",	"reset-n-io",	"marvell,nfc-i2c" },
528 #endif
529 #if IS_ENABLED(CONFIG_NFC_MRVL_SPI)
530 		{ "reset",	"reset-n-io",	"marvell,nfc-spi" },
531 #endif
532 #if IS_ENABLED(CONFIG_NFC_MRVL_UART)
533 		{ "reset",	"reset-n-io",	"marvell,nfc-uart" },
534 		{ "reset",	"reset-n-io",	"mrvl,nfc-uart" },
535 #endif
536 #if IS_ENABLED(CONFIG_PCI_LANTIQ)
537 		/* MIPS Lantiq PCI */
538 		{ "reset",	"gpio-reset",	"lantiq,pci-xway" },
539 #endif
540 
541 		/*
542 		 * Some regulator bindings happened before we managed to
543 		 * establish that GPIO properties should be named
544 		 * "foo-gpios" so we have this special kludge for them.
545 		 */
546 		{ "max77759,extbst-ctl",	NULL,	"maxim,max77759chrg" },
547 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
548 		{ "wlf,ldoena",  NULL,		NULL }, /* Arizona */
549 #endif
550 #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
551 		{ "wlf,ldo1ena", NULL,		NULL }, /* WM8994 */
552 		{ "wlf,ldo2ena", NULL,		NULL }, /* WM8994 */
553 #endif
554 
555 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
556 		{ "reset",	"cirrus,gpio-nreset",	"cirrus,cs42l56" },
557 #endif
558 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
559 		{ "i2s1-in-sel-gpio1",	NULL,	"mediatek,mt2701-cs42448-machine" },
560 		{ "i2s1-in-sel-gpio2",	NULL,	"mediatek,mt2701-cs42448-machine" },
561 #endif
562 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
563 		{ "reset",	"gpio-reset",	"ti,tlv320aic3x" },
564 		{ "reset",	"gpio-reset",	"ti,tlv320aic33" },
565 		{ "reset",	"gpio-reset",	"ti,tlv320aic3007" },
566 		{ "reset",	"gpio-reset",	"ti,tlv320aic3104" },
567 		{ "reset",	"gpio-reset",	"ti,tlv320aic3106" },
568 #endif
569 #if IS_ENABLED(CONFIG_SPI_GPIO)
570 		/*
571 		 * The SPI GPIO bindings happened before we managed to
572 		 * establish that GPIO properties should be named
573 		 * "foo-gpios" so we have this special kludge for them.
574 		 */
575 		{ "miso",	"gpio-miso",	"spi-gpio" },
576 		{ "mosi",	"gpio-mosi",	"spi-gpio" },
577 		{ "sck",	"gpio-sck",	"spi-gpio" },
578 #endif
579 
580 		/*
581 		 * The old Freescale bindings use simply "gpios" as name
582 		 * for the chip select lines rather than "cs-gpios" like
583 		 * all other SPI hardware. Allow this specifically for
584 		 * Freescale and PPC devices.
585 		 */
586 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
587 		{ "cs",		"gpios",	"fsl,spi" },
588 		{ "cs",		"gpios",	"aeroflexgaisler,spictrl" },
589 #endif
590 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
591 		{ "cs",		"gpios",	"ibm,ppc4xx-spi" },
592 #endif
593 
594 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
595 		/*
596 		 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
597 		 * property without the compulsory "-gpios" suffix.
598 		 */
599 		{ "fcs,int_n",	NULL,		"fcs,fusb302" },
600 #endif
601 	};
602 	struct gpio_desc *desc;
603 	const char *legacy_id;
604 	unsigned int i;
605 
606 	if (!con_id)
607 		return ERR_PTR(-ENOENT);
608 
609 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
610 		if (strcmp(con_id, gpios[i].con_id))
611 			continue;
612 
613 		if (gpios[i].compatible &&
614 		    !of_device_is_compatible(np, gpios[i].compatible))
615 			continue;
616 
617 		legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
618 		desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
619 		if (!gpiod_not_found(desc)) {
620 			pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
621 				of_node_full_name(np), legacy_id, con_id);
622 			return desc;
623 		}
624 	}
625 
626 	return ERR_PTR(-ENOENT);
627 }
628 
of_find_mt2701_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)629 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
630 					     const char *con_id,
631 					     unsigned int idx,
632 					     enum of_gpio_flags *of_flags)
633 {
634 	struct gpio_desc *desc;
635 	const char *legacy_id;
636 
637 	if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
638 		return ERR_PTR(-ENOENT);
639 
640 	if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
641 		return ERR_PTR(-ENOENT);
642 
643 	if (!con_id || strcmp(con_id, "i2s1-in-sel"))
644 		return ERR_PTR(-ENOENT);
645 
646 	if (idx == 0)
647 		legacy_id = "i2s1-in-sel-gpio1";
648 	else if (idx == 1)
649 		legacy_id = "i2s1-in-sel-gpio2";
650 	else
651 		return ERR_PTR(-ENOENT);
652 
653 	desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
654 	if (!gpiod_not_found(desc))
655 		pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
656 			of_node_full_name(np), legacy_id, con_id);
657 
658 	return desc;
659 }
660 
661 /*
662  * Trigger sources are special, they allow us to use any GPIO as a LED trigger
663  * and have the name "trigger-sources" no matter which kind of phandle it is
664  * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case
665  * we allow looking something up that is not named "foo-gpios".
666  */
of_find_trigger_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)667 static struct gpio_desc *of_find_trigger_gpio(struct device_node *np,
668 					      const char *con_id,
669 					      unsigned int idx,
670 					      enum of_gpio_flags *of_flags)
671 {
672 	struct gpio_desc *desc;
673 
674 	if (!IS_ENABLED(CONFIG_LEDS_TRIGGER_GPIO))
675 		return ERR_PTR(-ENOENT);
676 
677 	if (!con_id || strcmp(con_id, "trigger-sources"))
678 		return ERR_PTR(-ENOENT);
679 
680 	desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags);
681 	if (!gpiod_not_found(desc))
682 		pr_debug("%s is used as a trigger\n", of_node_full_name(np));
683 
684 	return desc;
685 }
686 
687 
688 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
689 						const char *con_id,
690 						unsigned int idx,
691 						enum of_gpio_flags *of_flags);
692 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
693 	of_find_gpio_rename,
694 	of_find_mt2701_gpio,
695 	of_find_trigger_gpio,
696 	NULL
697 };
698 
of_find_gpio(struct device_node * np,const char * con_id,unsigned int idx,unsigned long * flags)699 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
700 			       unsigned int idx, unsigned long *flags)
701 {
702 	char propname[32]; /* 32 is max size of property name */
703 	enum of_gpio_flags of_flags;
704 	const of_find_gpio_quirk *q;
705 	struct gpio_desc *desc;
706 
707 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
708 	for_each_gpio_property_name(propname, con_id) {
709 		desc = of_get_named_gpiod_flags(np, propname, idx, &of_flags);
710 		if (!gpiod_not_found(desc))
711 			break;
712 	}
713 
714 	/* Properly named GPIO was not found, try workarounds */
715 	for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
716 		desc = (*q)(np, con_id, idx, &of_flags);
717 
718 	if (IS_ERR(desc))
719 		return desc;
720 
721 	*flags = of_convert_gpio_flags(of_flags);
722 
723 	return desc;
724 }
725 
726 /**
727  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
728  * @np:		device node to get GPIO from
729  * @chip:	GPIO chip whose hog is parsed
730  * @idx:	Index of the GPIO to parse
731  * @name:	GPIO line name
732  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
733  *		of_find_gpio() or of_parse_own_gpio()
734  * @dflags:	gpiod_flags - optional GPIO initialization flags
735  *
736  * Returns:
737  * GPIO descriptor to use with Linux GPIO API, or one of the errno
738  * value on the error condition.
739  */
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)740 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
741 					   struct gpio_chip *chip,
742 					   unsigned int idx, const char **name,
743 					   unsigned long *lflags,
744 					   enum gpiod_flags *dflags)
745 {
746 	struct device_node *chip_np;
747 	enum of_gpio_flags xlate_flags;
748 	struct of_phandle_args gpiospec;
749 	struct gpio_desc *desc;
750 	unsigned int i;
751 	u32 tmp;
752 	int ret;
753 
754 	chip_np = dev_of_node(&chip->gpiodev->dev);
755 	if (!chip_np)
756 		return ERR_PTR(-EINVAL);
757 
758 	xlate_flags = 0;
759 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
760 	*dflags = GPIOD_ASIS;
761 
762 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
763 	if (ret)
764 		return ERR_PTR(ret);
765 
766 	gpiospec.np = chip_np;
767 	gpiospec.args_count = tmp;
768 
769 	for (i = 0; i < tmp; i++) {
770 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
771 						 &gpiospec.args[i]);
772 		if (ret)
773 			return ERR_PTR(ret);
774 	}
775 
776 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
777 	if (IS_ERR(desc))
778 		return desc;
779 
780 	*lflags = of_convert_gpio_flags(xlate_flags);
781 
782 	if (of_property_read_bool(np, "input"))
783 		*dflags |= GPIOD_IN;
784 	else if (of_property_read_bool(np, "output-low"))
785 		*dflags |= GPIOD_OUT_LOW;
786 	else if (of_property_read_bool(np, "output-high"))
787 		*dflags |= GPIOD_OUT_HIGH;
788 	else {
789 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
790 			desc_to_gpio(desc), np);
791 		return ERR_PTR(-EINVAL);
792 	}
793 
794 	if (name && of_property_read_string(np, "line-name", name))
795 		*name = np->name;
796 
797 	return desc;
798 }
799 
800 /**
801  * of_gpiochip_add_hog - Add all hogs in a hog device node
802  * @chip:	gpio chip to act on
803  * @hog:	device node describing the hogs
804  *
805  * Returns:
806  * 0 on success, or negative errno on failure.
807  */
of_gpiochip_add_hog(struct gpio_chip * chip,struct device_node * hog)808 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
809 {
810 	enum gpiod_flags dflags;
811 	struct gpio_desc *desc;
812 	unsigned long lflags;
813 	const char *name;
814 	unsigned int i;
815 	int ret;
816 
817 	for (i = 0;; i++) {
818 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
819 		if (IS_ERR(desc))
820 			break;
821 
822 		ret = gpiod_hog(desc, name, lflags, dflags);
823 		if (ret < 0)
824 			return ret;
825 
826 #ifdef CONFIG_OF_DYNAMIC
827 		WRITE_ONCE(desc->hog, hog);
828 #endif
829 	}
830 
831 	return 0;
832 }
833 
834 /**
835  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
836  * @chip:	gpio chip to act on
837  *
838  * This is only used by of_gpiochip_add to request/set GPIO initial
839  * configuration.
840  *
841  * Returns:
842  * 0 on success, or negative errno on failure.
843  */
of_gpiochip_scan_gpios(struct gpio_chip * chip)844 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
845 {
846 	int ret;
847 
848 	for_each_available_child_of_node_scoped(dev_of_node(&chip->gpiodev->dev), np) {
849 		if (!of_property_read_bool(np, "gpio-hog"))
850 			continue;
851 
852 		ret = of_gpiochip_add_hog(chip, np);
853 		if (ret < 0)
854 			return ret;
855 
856 		of_node_set_flag(np, OF_POPULATED);
857 	}
858 
859 	return 0;
860 }
861 
862 #ifdef CONFIG_OF_DYNAMIC
863 /**
864  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
865  * @chip:	gpio chip to act on
866  * @hog:	device node describing the hogs
867  */
of_gpiochip_remove_hog(struct gpio_chip * chip,struct device_node * hog)868 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
869 				   struct device_node *hog)
870 {
871 	struct gpio_desc *desc;
872 
873 	for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
874 		if (READ_ONCE(desc->hog) == hog)
875 			gpiochip_free_own_desc(desc);
876 }
877 
of_gpiochip_match_node(struct gpio_chip * chip,const void * data)878 static int of_gpiochip_match_node(struct gpio_chip *chip, const void *data)
879 {
880 	return device_match_of_node(&chip->gpiodev->dev, data);
881 }
882 
of_find_gpio_device_by_node(struct device_node * np)883 static struct gpio_device *of_find_gpio_device_by_node(struct device_node *np)
884 {
885 	return gpio_device_find(np, of_gpiochip_match_node);
886 }
887 
of_gpio_notify(struct notifier_block * nb,unsigned long action,void * arg)888 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
889 			  void *arg)
890 {
891 	struct gpio_device *gdev __free(gpio_device_put) = NULL;
892 	struct of_reconfig_data *rd = arg;
893 	int ret;
894 
895 	/*
896 	 * This only supports adding and removing complete gpio-hog nodes.
897 	 * Modifying an existing gpio-hog node is not supported (except for
898 	 * changing its "status" property, which is treated the same as
899 	 * addition/removal).
900 	 */
901 	switch (of_reconfig_get_state_change(action, arg)) {
902 	case OF_RECONFIG_CHANGE_ADD:
903 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
904 			return NOTIFY_DONE;	/* not for us */
905 
906 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
907 			return NOTIFY_DONE;
908 
909 		gdev = of_find_gpio_device_by_node(rd->dn->parent);
910 		if (!gdev)
911 			return NOTIFY_DONE;	/* not for us */
912 
913 		ret = of_gpiochip_add_hog(gpio_device_get_chip(gdev), rd->dn);
914 		if (ret < 0) {
915 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
916 			       rd->dn);
917 			of_node_clear_flag(rd->dn, OF_POPULATED);
918 			return notifier_from_errno(ret);
919 		}
920 		return NOTIFY_OK;
921 
922 	case OF_RECONFIG_CHANGE_REMOVE:
923 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
924 			return NOTIFY_DONE;	/* already depopulated */
925 
926 		gdev = of_find_gpio_device_by_node(rd->dn->parent);
927 		if (!gdev)
928 			return NOTIFY_DONE;	/* not for us */
929 
930 		of_gpiochip_remove_hog(gpio_device_get_chip(gdev), rd->dn);
931 		of_node_clear_flag(rd->dn, OF_POPULATED);
932 		return NOTIFY_OK;
933 	}
934 
935 	return NOTIFY_DONE;
936 }
937 
938 struct notifier_block gpio_of_notifier = {
939 	.notifier_call = of_gpio_notify,
940 };
941 #endif /* CONFIG_OF_DYNAMIC */
942 
943 /**
944  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
945  * @gc:		pointer to the gpio_chip structure
946  * @gpiospec:	GPIO specifier as found in the device tree
947  * @flags:	a flags pointer to fill in
948  *
949  * This is simple translation function, suitable for the most 1:1 mapped
950  * GPIO chips. This function performs only one sanity check: whether GPIO
951  * is less than ngpios (that is specified in the gpio_chip).
952  *
953  * Returns:
954  * GPIO number (>= 0) on success, negative errno on failure.
955  */
of_gpio_simple_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)956 static int of_gpio_simple_xlate(struct gpio_chip *gc,
957 				const struct of_phandle_args *gpiospec,
958 				u32 *flags)
959 {
960 	/*
961 	 * We're discouraging gpio_cells < 2, since that way you'll have to
962 	 * write your own xlate function (that will have to retrieve the GPIO
963 	 * number and the flags from a single gpio cell -- this is possible,
964 	 * but not recommended).
965 	 */
966 	if (gc->of_gpio_n_cells < 2) {
967 		WARN_ON(1);
968 		return -EINVAL;
969 	}
970 
971 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
972 		return -EINVAL;
973 
974 	if (gpiospec->args[0] >= gc->ngpio)
975 		return -EINVAL;
976 
977 	if (flags)
978 		*flags = gpiospec->args[1];
979 
980 	return gpiospec->args[0];
981 }
982 
983 #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
984 #include <linux/gpio/legacy-of-mm-gpiochip.h>
985 /**
986  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
987  * @np:		device node of the GPIO chip
988  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
989  * @data:	driver data to store in the struct gpio_chip
990  *
991  * To use this function you should allocate and fill mm_gc with:
992  *
993  * 1) In the gpio_chip structure:
994  *    - all the callbacks
995  *    - of_gpio_n_cells
996  *    - of_xlate callback (optional)
997  *
998  * 3) In the of_mm_gpio_chip structure:
999  *    - save_regs callback (optional)
1000  *
1001  * If succeeded, this function will map bank's memory and will
1002  * do all necessary work for you. Then you'll able to use .regs
1003  * to manage GPIOs from the callbacks.
1004  *
1005  * Returns:
1006  * 0 on success, or negative errno on failure.
1007  */
of_mm_gpiochip_add_data(struct device_node * np,struct of_mm_gpio_chip * mm_gc,void * data)1008 int of_mm_gpiochip_add_data(struct device_node *np,
1009 			    struct of_mm_gpio_chip *mm_gc,
1010 			    void *data)
1011 {
1012 	int ret = -ENOMEM;
1013 	struct gpio_chip *gc = &mm_gc->gc;
1014 
1015 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
1016 	if (!gc->label)
1017 		goto err0;
1018 
1019 	mm_gc->regs = of_iomap(np, 0);
1020 	if (!mm_gc->regs)
1021 		goto err1;
1022 
1023 	gc->base = -1;
1024 
1025 	if (mm_gc->save_regs)
1026 		mm_gc->save_regs(mm_gc);
1027 
1028 	fwnode_handle_put(mm_gc->gc.fwnode);
1029 	mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
1030 
1031 	ret = gpiochip_add_data(gc, data);
1032 	if (ret)
1033 		goto err2;
1034 
1035 	return 0;
1036 err2:
1037 	of_node_put(np);
1038 	iounmap(mm_gc->regs);
1039 err1:
1040 	kfree(gc->label);
1041 err0:
1042 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
1043 	return ret;
1044 }
1045 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
1046 
1047 /**
1048  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
1049  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
1050  */
of_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)1051 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
1052 {
1053 	struct gpio_chip *gc = &mm_gc->gc;
1054 
1055 	gpiochip_remove(gc);
1056 	iounmap(mm_gc->regs);
1057 	kfree(gc->label);
1058 }
1059 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
1060 #endif
1061 
1062 #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)1063 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
1064 {
1065 	struct of_phandle_args pinspec;
1066 	struct pinctrl_dev *pctldev;
1067 	struct device_node *np;
1068 	int index = 0, ret, trim;
1069 	const char *name;
1070 	static const char group_names_propname[] = "gpio-ranges-group-names";
1071 	bool has_group_names;
1072 
1073 	np = dev_of_node(&chip->gpiodev->dev);
1074 	if (!np)
1075 		return 0;
1076 
1077 	has_group_names = of_property_present(np, group_names_propname);
1078 
1079 	for (;; index++) {
1080 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
1081 				index, &pinspec);
1082 		if (ret)
1083 			break;
1084 
1085 		pctldev = of_pinctrl_get(pinspec.np);
1086 		of_node_put(pinspec.np);
1087 		if (!pctldev)
1088 			return -EPROBE_DEFER;
1089 
1090 		/* Ignore ranges outside of this GPIO chip */
1091 		if (pinspec.args[0] >= (chip->offset + chip->ngpio))
1092 			continue;
1093 		if (pinspec.args[0] + pinspec.args[2] <= chip->offset)
1094 			continue;
1095 
1096 		if (pinspec.args[2]) {
1097 			/* npins != 0: linear range */
1098 			if (has_group_names) {
1099 				of_property_read_string_index(np,
1100 						group_names_propname,
1101 						index, &name);
1102 				if (strlen(name)) {
1103 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1104 						np);
1105 					break;
1106 				}
1107 			}
1108 
1109 			/* Trim the range to fit this GPIO chip */
1110 			if (chip->offset > pinspec.args[0]) {
1111 				trim = chip->offset - pinspec.args[0];
1112 				pinspec.args[2] -= trim;
1113 				pinspec.args[1] += trim;
1114 				pinspec.args[0] = 0;
1115 			} else {
1116 				pinspec.args[0] -= chip->offset;
1117 			}
1118 			if ((pinspec.args[0] + pinspec.args[2]) > chip->ngpio)
1119 				pinspec.args[2] = chip->ngpio - pinspec.args[0];
1120 
1121 			ret = gpiochip_add_pin_range(chip,
1122 					pinctrl_dev_get_devname(pctldev),
1123 					pinspec.args[0],
1124 					pinspec.args[1],
1125 					pinspec.args[2]);
1126 			if (ret)
1127 				return ret;
1128 		} else {
1129 			/* npins == 0: special range */
1130 			if (pinspec.args[1]) {
1131 				pr_err("%pOF: Illegal gpio-range format.\n",
1132 					np);
1133 				break;
1134 			}
1135 
1136 			if (!has_group_names) {
1137 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
1138 					np, group_names_propname);
1139 				break;
1140 			}
1141 
1142 			ret = of_property_read_string_index(np,
1143 						group_names_propname,
1144 						index, &name);
1145 			if (ret)
1146 				break;
1147 
1148 			if (!strlen(name)) {
1149 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1150 				np);
1151 				break;
1152 			}
1153 
1154 			ret = gpiochip_add_pingroup_range(chip, pctldev,
1155 						pinspec.args[0], name);
1156 			if (ret)
1157 				return ret;
1158 		}
1159 	}
1160 
1161 	return 0;
1162 }
1163 
1164 #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)1165 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1166 #endif
1167 
of_gpiochip_add(struct gpio_chip * chip)1168 int of_gpiochip_add(struct gpio_chip *chip)
1169 {
1170 	struct device_node *np;
1171 	int ret;
1172 
1173 	np = dev_of_node(&chip->gpiodev->dev);
1174 	if (!np)
1175 		return 0;
1176 
1177 	if (!chip->of_xlate) {
1178 		chip->of_gpio_n_cells = 2;
1179 		chip->of_xlate = of_gpio_simple_xlate;
1180 	}
1181 
1182 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1183 		return -EINVAL;
1184 
1185 	ret = of_gpiochip_add_pin_range(chip);
1186 	if (ret)
1187 		return ret;
1188 
1189 	of_node_get(np);
1190 
1191 	ret = of_gpiochip_scan_gpios(chip);
1192 	if (ret)
1193 		of_node_put(np);
1194 
1195 	return ret;
1196 }
1197 
of_gpiochip_remove(struct gpio_chip * chip)1198 void of_gpiochip_remove(struct gpio_chip *chip)
1199 {
1200 	of_node_put(dev_of_node(&chip->gpiodev->dev));
1201 }
1202