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_IEEE802154_CA8210)
209 		/*
210 		 * According to the datasheet, the NRST pin 27 is an active-low
211 		 * signal. However, the device tree schema and admittedly
212 		 * the out-of-tree implementations have been used for a long
213 		 * time incorrectly by describing reset GPIO as active-high.
214 		 */
215 		{ "cascoda,ca8210",	"reset-gpio",	false },
216 #endif
217 #if IS_ENABLED(CONFIG_PCI_LANTIQ)
218 		/*
219 		 * According to the PCI specification, the RST# pin is an
220 		 * active-low signal. However, most of the device trees that
221 		 * have been widely used for a long time incorrectly describe
222 		 * reset GPIO as active-high, and were also using wrong name
223 		 * for the property.
224 		 */
225 		{ "lantiq,pci-xway",	"gpio-reset",	false },
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_present(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 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1)
547 		{ "wlf,ldoena",  NULL,		NULL }, /* Arizona */
548 #endif
549 #if IS_ENABLED(CONFIG_REGULATOR_WM8994)
550 		{ "wlf,ldo1ena", NULL,		NULL }, /* WM8994 */
551 		{ "wlf,ldo2ena", NULL,		NULL }, /* WM8994 */
552 #endif
553 
554 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56)
555 		{ "reset",	"cirrus,gpio-nreset",	"cirrus,cs42l56" },
556 #endif
557 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)
558 		{ "i2s1-in-sel-gpio1",	NULL,	"mediatek,mt2701-cs42448-machine" },
559 		{ "i2s1-in-sel-gpio2",	NULL,	"mediatek,mt2701-cs42448-machine" },
560 #endif
561 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X)
562 		{ "reset",	"gpio-reset",	"ti,tlv320aic3x" },
563 		{ "reset",	"gpio-reset",	"ti,tlv320aic33" },
564 		{ "reset",	"gpio-reset",	"ti,tlv320aic3007" },
565 		{ "reset",	"gpio-reset",	"ti,tlv320aic3104" },
566 		{ "reset",	"gpio-reset",	"ti,tlv320aic3106" },
567 #endif
568 #if IS_ENABLED(CONFIG_SPI_GPIO)
569 		/*
570 		 * The SPI GPIO bindings happened before we managed to
571 		 * establish that GPIO properties should be named
572 		 * "foo-gpios" so we have this special kludge for them.
573 		 */
574 		{ "miso",	"gpio-miso",	"spi-gpio" },
575 		{ "mosi",	"gpio-mosi",	"spi-gpio" },
576 		{ "sck",	"gpio-sck",	"spi-gpio" },
577 #endif
578 
579 		/*
580 		 * The old Freescale bindings use simply "gpios" as name
581 		 * for the chip select lines rather than "cs-gpios" like
582 		 * all other SPI hardware. Allow this specifically for
583 		 * Freescale and PPC devices.
584 		 */
585 #if IS_ENABLED(CONFIG_SPI_FSL_SPI)
586 		{ "cs",		"gpios",	"fsl,spi" },
587 		{ "cs",		"gpios",	"aeroflexgaisler,spictrl" },
588 #endif
589 #if IS_ENABLED(CONFIG_SPI_PPC4xx)
590 		{ "cs",		"gpios",	"ibm,ppc4xx-spi" },
591 #endif
592 
593 #if IS_ENABLED(CONFIG_TYPEC_FUSB302)
594 		/*
595 		 * Fairchild FUSB302 host is using undocumented "fcs,int_n"
596 		 * property without the compulsory "-gpios" suffix.
597 		 */
598 		{ "fcs,int_n",	NULL,		"fcs,fusb302" },
599 #endif
600 	};
601 	struct gpio_desc *desc;
602 	const char *legacy_id;
603 	unsigned int i;
604 
605 	if (!con_id)
606 		return ERR_PTR(-ENOENT);
607 
608 	for (i = 0; i < ARRAY_SIZE(gpios); i++) {
609 		if (strcmp(con_id, gpios[i].con_id))
610 			continue;
611 
612 		if (gpios[i].compatible &&
613 		    !of_device_is_compatible(np, gpios[i].compatible))
614 			continue;
615 
616 		legacy_id = gpios[i].legacy_id ?: gpios[i].con_id;
617 		desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags);
618 		if (!gpiod_not_found(desc)) {
619 			pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n",
620 				of_node_full_name(np), legacy_id, con_id);
621 			return desc;
622 		}
623 	}
624 
625 	return ERR_PTR(-ENOENT);
626 }
627 
of_find_mt2701_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)628 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
629 					     const char *con_id,
630 					     unsigned int idx,
631 					     enum of_gpio_flags *of_flags)
632 {
633 	struct gpio_desc *desc;
634 	const char *legacy_id;
635 
636 	if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448))
637 		return ERR_PTR(-ENOENT);
638 
639 	if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine"))
640 		return ERR_PTR(-ENOENT);
641 
642 	if (!con_id || strcmp(con_id, "i2s1-in-sel"))
643 		return ERR_PTR(-ENOENT);
644 
645 	if (idx == 0)
646 		legacy_id = "i2s1-in-sel-gpio1";
647 	else if (idx == 1)
648 		legacy_id = "i2s1-in-sel-gpio2";
649 	else
650 		return ERR_PTR(-ENOENT);
651 
652 	desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags);
653 	if (!gpiod_not_found(desc))
654 		pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n",
655 			of_node_full_name(np), legacy_id, con_id);
656 
657 	return desc;
658 }
659 
660 /*
661  * Trigger sources are special, they allow us to use any GPIO as a LED trigger
662  * and have the name "trigger-sources" no matter which kind of phandle it is
663  * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case
664  * we allow looking something up that is not named "foo-gpios".
665  */
of_find_trigger_gpio(struct device_node * np,const char * con_id,unsigned int idx,enum of_gpio_flags * of_flags)666 static struct gpio_desc *of_find_trigger_gpio(struct device_node *np,
667 					      const char *con_id,
668 					      unsigned int idx,
669 					      enum of_gpio_flags *of_flags)
670 {
671 	struct gpio_desc *desc;
672 
673 	if (!IS_ENABLED(CONFIG_LEDS_TRIGGER_GPIO))
674 		return ERR_PTR(-ENOENT);
675 
676 	if (!con_id || strcmp(con_id, "trigger-sources"))
677 		return ERR_PTR(-ENOENT);
678 
679 	desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags);
680 	if (!gpiod_not_found(desc))
681 		pr_debug("%s is used as a trigger\n", of_node_full_name(np));
682 
683 	return desc;
684 }
685 
686 
687 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
688 						const char *con_id,
689 						unsigned int idx,
690 						enum of_gpio_flags *of_flags);
691 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
692 	of_find_gpio_rename,
693 	of_find_mt2701_gpio,
694 	of_find_trigger_gpio,
695 	NULL
696 };
697 
of_find_gpio(struct device_node * np,const char * con_id,unsigned int idx,unsigned long * flags)698 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
699 			       unsigned int idx, unsigned long *flags)
700 {
701 	char propname[32]; /* 32 is max size of property name */
702 	enum of_gpio_flags of_flags;
703 	const of_find_gpio_quirk *q;
704 	struct gpio_desc *desc;
705 
706 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
707 	for_each_gpio_property_name(propname, con_id) {
708 		desc = of_get_named_gpiod_flags(np, propname, idx, &of_flags);
709 		if (!gpiod_not_found(desc))
710 			break;
711 	}
712 
713 	/* Properly named GPIO was not found, try workarounds */
714 	for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
715 		desc = (*q)(np, con_id, idx, &of_flags);
716 
717 	if (IS_ERR(desc))
718 		return desc;
719 
720 	*flags = of_convert_gpio_flags(of_flags);
721 
722 	return desc;
723 }
724 
725 /**
726  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
727  * @np:		device node to get GPIO from
728  * @chip:	GPIO chip whose hog is parsed
729  * @idx:	Index of the GPIO to parse
730  * @name:	GPIO line name
731  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
732  *		of_find_gpio() or of_parse_own_gpio()
733  * @dflags:	gpiod_flags - optional GPIO initialization flags
734  *
735  * Returns:
736  * GPIO descriptor to use with Linux GPIO API, or one of the errno
737  * value on the error condition.
738  */
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)739 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
740 					   struct gpio_chip *chip,
741 					   unsigned int idx, const char **name,
742 					   unsigned long *lflags,
743 					   enum gpiod_flags *dflags)
744 {
745 	struct device_node *chip_np;
746 	enum of_gpio_flags xlate_flags;
747 	struct of_phandle_args gpiospec;
748 	struct gpio_desc *desc;
749 	unsigned int i;
750 	u32 tmp;
751 	int ret;
752 
753 	chip_np = dev_of_node(&chip->gpiodev->dev);
754 	if (!chip_np)
755 		return ERR_PTR(-EINVAL);
756 
757 	xlate_flags = 0;
758 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
759 	*dflags = GPIOD_ASIS;
760 
761 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
762 	if (ret)
763 		return ERR_PTR(ret);
764 
765 	gpiospec.np = chip_np;
766 	gpiospec.args_count = tmp;
767 
768 	for (i = 0; i < tmp; i++) {
769 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
770 						 &gpiospec.args[i]);
771 		if (ret)
772 			return ERR_PTR(ret);
773 	}
774 
775 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
776 	if (IS_ERR(desc))
777 		return desc;
778 
779 	*lflags = of_convert_gpio_flags(xlate_flags);
780 
781 	if (of_property_read_bool(np, "input"))
782 		*dflags |= GPIOD_IN;
783 	else if (of_property_read_bool(np, "output-low"))
784 		*dflags |= GPIOD_OUT_LOW;
785 	else if (of_property_read_bool(np, "output-high"))
786 		*dflags |= GPIOD_OUT_HIGH;
787 	else {
788 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
789 			desc_to_gpio(desc), np);
790 		return ERR_PTR(-EINVAL);
791 	}
792 
793 	if (name && of_property_read_string(np, "line-name", name))
794 		*name = np->name;
795 
796 	return desc;
797 }
798 
799 /**
800  * of_gpiochip_add_hog - Add all hogs in a hog device node
801  * @chip:	gpio chip to act on
802  * @hog:	device node describing the hogs
803  *
804  * Returns:
805  * 0 on success, or negative errno on failure.
806  */
of_gpiochip_add_hog(struct gpio_chip * chip,struct device_node * hog)807 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
808 {
809 	enum gpiod_flags dflags;
810 	struct gpio_desc *desc;
811 	unsigned long lflags;
812 	const char *name;
813 	unsigned int i;
814 	int ret;
815 
816 	for (i = 0;; i++) {
817 		desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
818 		if (IS_ERR(desc))
819 			break;
820 
821 		ret = gpiod_hog(desc, name, lflags, dflags);
822 		if (ret < 0)
823 			return ret;
824 
825 #ifdef CONFIG_OF_DYNAMIC
826 		WRITE_ONCE(desc->hog, hog);
827 #endif
828 	}
829 
830 	return 0;
831 }
832 
833 /**
834  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
835  * @chip:	gpio chip to act on
836  *
837  * This is only used by of_gpiochip_add to request/set GPIO initial
838  * configuration.
839  *
840  * Returns:
841  * 0 on success, or negative errno on failure.
842  */
of_gpiochip_scan_gpios(struct gpio_chip * chip)843 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
844 {
845 	int ret;
846 
847 	for_each_available_child_of_node_scoped(dev_of_node(&chip->gpiodev->dev), np) {
848 		if (!of_property_read_bool(np, "gpio-hog"))
849 			continue;
850 
851 		ret = of_gpiochip_add_hog(chip, np);
852 		if (ret < 0)
853 			return ret;
854 
855 		of_node_set_flag(np, OF_POPULATED);
856 	}
857 
858 	return 0;
859 }
860 
861 #ifdef CONFIG_OF_DYNAMIC
862 /**
863  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
864  * @chip:	gpio chip to act on
865  * @hog:	device node describing the hogs
866  */
of_gpiochip_remove_hog(struct gpio_chip * chip,struct device_node * hog)867 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
868 				   struct device_node *hog)
869 {
870 	struct gpio_desc *desc;
871 
872 	for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
873 		if (READ_ONCE(desc->hog) == hog)
874 			gpiochip_free_own_desc(desc);
875 }
876 
of_gpiochip_match_node(struct gpio_chip * chip,const void * data)877 static int of_gpiochip_match_node(struct gpio_chip *chip, const void *data)
878 {
879 	return device_match_of_node(&chip->gpiodev->dev, data);
880 }
881 
of_find_gpio_device_by_node(struct device_node * np)882 static struct gpio_device *of_find_gpio_device_by_node(struct device_node *np)
883 {
884 	return gpio_device_find(np, of_gpiochip_match_node);
885 }
886 
of_gpio_notify(struct notifier_block * nb,unsigned long action,void * arg)887 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
888 			  void *arg)
889 {
890 	struct gpio_device *gdev __free(gpio_device_put) = NULL;
891 	struct of_reconfig_data *rd = arg;
892 	int ret;
893 
894 	/*
895 	 * This only supports adding and removing complete gpio-hog nodes.
896 	 * Modifying an existing gpio-hog node is not supported (except for
897 	 * changing its "status" property, which is treated the same as
898 	 * addition/removal).
899 	 */
900 	switch (of_reconfig_get_state_change(action, arg)) {
901 	case OF_RECONFIG_CHANGE_ADD:
902 		if (!of_property_read_bool(rd->dn, "gpio-hog"))
903 			return NOTIFY_DONE;	/* not for us */
904 
905 		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
906 			return NOTIFY_DONE;
907 
908 		gdev = of_find_gpio_device_by_node(rd->dn->parent);
909 		if (!gdev)
910 			return NOTIFY_DONE;	/* not for us */
911 
912 		ret = of_gpiochip_add_hog(gpio_device_get_chip(gdev), rd->dn);
913 		if (ret < 0) {
914 			pr_err("%s: failed to add hogs for %pOF\n", __func__,
915 			       rd->dn);
916 			of_node_clear_flag(rd->dn, OF_POPULATED);
917 			return notifier_from_errno(ret);
918 		}
919 		return NOTIFY_OK;
920 
921 	case OF_RECONFIG_CHANGE_REMOVE:
922 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
923 			return NOTIFY_DONE;	/* already depopulated */
924 
925 		gdev = of_find_gpio_device_by_node(rd->dn->parent);
926 		if (!gdev)
927 			return NOTIFY_DONE;	/* not for us */
928 
929 		of_gpiochip_remove_hog(gpio_device_get_chip(gdev), rd->dn);
930 		of_node_clear_flag(rd->dn, OF_POPULATED);
931 		return NOTIFY_OK;
932 	}
933 
934 	return NOTIFY_DONE;
935 }
936 
937 struct notifier_block gpio_of_notifier = {
938 	.notifier_call = of_gpio_notify,
939 };
940 #endif /* CONFIG_OF_DYNAMIC */
941 
942 /**
943  * of_gpio_twocell_xlate - translate twocell gpiospec to the GPIO number and flags
944  * @gc:		pointer to the gpio_chip structure
945  * @gpiospec:	GPIO specifier as found in the device tree
946  * @flags:	a flags pointer to fill in
947  *
948  * This is simple translation function, suitable for the most 1:1 mapped
949  * GPIO chips. This function performs only one sanity check: whether GPIO
950  * is less than ngpios (that is specified in the gpio_chip).
951  *
952  * Returns:
953  * GPIO number (>= 0) on success, negative errno on failure.
954  */
of_gpio_twocell_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)955 static int of_gpio_twocell_xlate(struct gpio_chip *gc,
956 				 const struct of_phandle_args *gpiospec,
957 				 u32 *flags)
958 {
959 	/*
960 	 * We're discouraging gpio_cells < 2, since that way you'll have to
961 	 * write your own xlate function (that will have to retrieve the GPIO
962 	 * number and the flags from a single gpio cell -- this is possible,
963 	 * but not recommended).
964 	 */
965 	if (gc->of_gpio_n_cells != 2) {
966 		WARN_ON(1);
967 		return -EINVAL;
968 	}
969 
970 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
971 		return -EINVAL;
972 
973 	if (gpiospec->args[0] >= gc->ngpio)
974 		return -EINVAL;
975 
976 	if (flags)
977 		*flags = gpiospec->args[1];
978 
979 	return gpiospec->args[0];
980 }
981 
982 /**
983  * of_gpio_threecell_xlate - translate threecell gpiospec to the GPIO number and flags
984  * @gc:		pointer to the gpio_chip structure
985  * @gpiospec:	GPIO specifier as found in the device tree
986  * @flags:	a flags pointer to fill in
987  *
988  * This is simple translation function, suitable for the most 1:n mapped
989  * GPIO chips, i.e. several GPIO chip instances from one device tree node.
990  * In this case the following binding is implied:
991  *
992  * foo-gpios = <&gpio instance offset flags>;
993  *
994  * Returns:
995  * GPIO number (>= 0) on success, negative errno on failure.
996  */
of_gpio_threecell_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)997 static int of_gpio_threecell_xlate(struct gpio_chip *gc,
998 				   const struct of_phandle_args *gpiospec,
999 				   u32 *flags)
1000 {
1001 	if (gc->of_gpio_n_cells != 3) {
1002 		WARN_ON(1);
1003 		return -EINVAL;
1004 	}
1005 
1006 	if (WARN_ON(gpiospec->args_count != 3))
1007 		return -EINVAL;
1008 
1009 	/*
1010 	 * Check chip instance number, the driver responds with true if
1011 	 * this is the chip we are looking for.
1012 	 */
1013 	if (!gc->of_node_instance_match(gc, gpiospec->args[0]))
1014 		return -EINVAL;
1015 
1016 	if (gpiospec->args[1] >= gc->ngpio)
1017 		return -EINVAL;
1018 
1019 	if (flags)
1020 		*flags = gpiospec->args[2];
1021 
1022 	return gpiospec->args[1];
1023 }
1024 
1025 #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP)
1026 #include <linux/gpio/legacy-of-mm-gpiochip.h>
1027 /**
1028  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
1029  * @np:		device node of the GPIO chip
1030  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
1031  * @data:	driver data to store in the struct gpio_chip
1032  *
1033  * To use this function you should allocate and fill mm_gc with:
1034  *
1035  * 1) In the gpio_chip structure:
1036  *    - all the callbacks
1037  *    - of_gpio_n_cells
1038  *    - of_xlate callback (optional)
1039  *
1040  * 3) In the of_mm_gpio_chip structure:
1041  *    - save_regs callback (optional)
1042  *
1043  * If succeeded, this function will map bank's memory and will
1044  * do all necessary work for you. Then you'll able to use .regs
1045  * to manage GPIOs from the callbacks.
1046  *
1047  * Returns:
1048  * 0 on success, or negative errno on failure.
1049  */
of_mm_gpiochip_add_data(struct device_node * np,struct of_mm_gpio_chip * mm_gc,void * data)1050 int of_mm_gpiochip_add_data(struct device_node *np,
1051 			    struct of_mm_gpio_chip *mm_gc,
1052 			    void *data)
1053 {
1054 	int ret = -ENOMEM;
1055 	struct gpio_chip *gc = &mm_gc->gc;
1056 
1057 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
1058 	if (!gc->label)
1059 		goto err0;
1060 
1061 	mm_gc->regs = of_iomap(np, 0);
1062 	if (!mm_gc->regs)
1063 		goto err1;
1064 
1065 	gc->base = -1;
1066 
1067 	if (mm_gc->save_regs)
1068 		mm_gc->save_regs(mm_gc);
1069 
1070 	fwnode_handle_put(mm_gc->gc.fwnode);
1071 	mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np));
1072 
1073 	ret = gpiochip_add_data(gc, data);
1074 	if (ret)
1075 		goto err2;
1076 
1077 	return 0;
1078 err2:
1079 	of_node_put(np);
1080 	iounmap(mm_gc->regs);
1081 err1:
1082 	kfree(gc->label);
1083 err0:
1084 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
1085 	return ret;
1086 }
1087 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
1088 
1089 /**
1090  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
1091  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
1092  */
of_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)1093 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
1094 {
1095 	struct gpio_chip *gc = &mm_gc->gc;
1096 
1097 	gpiochip_remove(gc);
1098 	iounmap(mm_gc->regs);
1099 	kfree(gc->label);
1100 }
1101 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
1102 #endif
1103 
1104 #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)1105 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
1106 {
1107 	struct of_phandle_args pinspec;
1108 	struct pinctrl_dev *pctldev;
1109 	struct device_node *np;
1110 	int index = 0, ret, trim;
1111 	const char *name;
1112 	static const char group_names_propname[] = "gpio-ranges-group-names";
1113 	bool has_group_names;
1114 	int offset; /* Offset of the first GPIO line on the chip */
1115 	int pin; /* Pin base number in the range */
1116 	int count; /* Number of pins/GPIO lines to map */
1117 
1118 	np = dev_of_node(&chip->gpiodev->dev);
1119 	if (!np)
1120 		return 0;
1121 
1122 	has_group_names = of_property_present(np, group_names_propname);
1123 
1124 	for (;; index++) {
1125 		/*
1126 		 * Ordinary phandles contain 2-3 cells:
1127 		 * gpios = <&gpio [instance] offset flags>;
1128 		 * Ranges always contain one more cell:
1129 		 * gpio-ranges <&pinctrl [gpio_instance] gpio_offet pin_offet count>;
1130 		 * This is why we parse chip->of_gpio_n_cells + 1 cells
1131 		 */
1132 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges",
1133 				chip->of_gpio_n_cells + 1,
1134 				index, &pinspec);
1135 		if (ret)
1136 			break;
1137 
1138 		pctldev = of_pinctrl_get(pinspec.np);
1139 		of_node_put(pinspec.np);
1140 		if (!pctldev)
1141 			return -EPROBE_DEFER;
1142 
1143 		if (chip->of_gpio_n_cells == 3) {
1144 			/* First cell is the gpiochip instance number */
1145 			offset = pinspec.args[1];
1146 			pin = pinspec.args[2];
1147 			count = pinspec.args[3];
1148 		} else {
1149 			offset = pinspec.args[0];
1150 			pin = pinspec.args[1];
1151 			count = pinspec.args[2];
1152 		}
1153 
1154 		/*
1155 		 * With multiple GPIO chips per node, check that this chip is the
1156 		 * right instance.
1157 		 */
1158 		if (chip->of_node_instance_match &&
1159 		    (chip->of_gpio_n_cells == 3) &&
1160 		    !chip->of_node_instance_match(chip, pinspec.args[0]))
1161 			continue;
1162 
1163 		/* Ignore ranges outside of this GPIO chip */
1164 		if (offset >= (chip->offset + chip->ngpio))
1165 			continue;
1166 		if (offset + count <= chip->offset)
1167 			continue;
1168 
1169 		if (count) {
1170 			/* npins != 0: linear range */
1171 			if (has_group_names) {
1172 				of_property_read_string_index(np,
1173 						group_names_propname,
1174 						index, &name);
1175 				if (strlen(name)) {
1176 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
1177 						np);
1178 					break;
1179 				}
1180 			}
1181 
1182 			/* Trim the range to fit this GPIO chip */
1183 			if (chip->offset > offset) {
1184 				trim = chip->offset - offset;
1185 				count -= trim;
1186 				pin += trim;
1187 				offset = 0;
1188 			} else {
1189 				offset -= chip->offset;
1190 			}
1191 			if ((offset + count) > chip->ngpio)
1192 				count = chip->ngpio - offset;
1193 
1194 			ret = gpiochip_add_pin_range(chip,
1195 					pinctrl_dev_get_devname(pctldev),
1196 					offset,
1197 					pin,
1198 					count);
1199 			if (ret)
1200 				return ret;
1201 		} else {
1202 			/* npins == 0: special range */
1203 			if (pin) {
1204 				pr_err("%pOF: Illegal gpio-range format.\n",
1205 					np);
1206 				break;
1207 			}
1208 
1209 			if (!has_group_names) {
1210 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
1211 					np, group_names_propname);
1212 				break;
1213 			}
1214 
1215 			ret = of_property_read_string_index(np,
1216 						group_names_propname,
1217 						index, &name);
1218 			if (ret)
1219 				break;
1220 
1221 			if (!strlen(name)) {
1222 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1223 				np);
1224 				break;
1225 			}
1226 
1227 			ret = gpiochip_add_pingroup_range(chip, pctldev,
1228 						offset, name);
1229 			if (ret)
1230 				return ret;
1231 		}
1232 	}
1233 
1234 	return 0;
1235 }
1236 
1237 #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)1238 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1239 #endif
1240 
of_gpiochip_add(struct gpio_chip * chip)1241 int of_gpiochip_add(struct gpio_chip *chip)
1242 {
1243 	struct device_node *np;
1244 	int ret;
1245 
1246 	np = dev_of_node(&chip->gpiodev->dev);
1247 	if (!np)
1248 		return 0;
1249 
1250 	if (!chip->of_xlate) {
1251 		if (chip->of_gpio_n_cells == 3) {
1252 			if (!chip->of_node_instance_match)
1253 				return -EINVAL;
1254 			chip->of_xlate = of_gpio_threecell_xlate;
1255 		} else {
1256 			chip->of_gpio_n_cells = 2;
1257 			chip->of_xlate = of_gpio_twocell_xlate;
1258 		}
1259 	}
1260 
1261 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1262 		return -EINVAL;
1263 
1264 	ret = of_gpiochip_add_pin_range(chip);
1265 	if (ret)
1266 		return ret;
1267 
1268 	of_node_get(np);
1269 
1270 	ret = of_gpiochip_scan_gpios(chip);
1271 	if (ret)
1272 		of_node_put(np);
1273 
1274 	return ret;
1275 }
1276 
of_gpiochip_remove(struct gpio_chip * chip)1277 void of_gpiochip_remove(struct gpio_chip *chip)
1278 {
1279 	of_node_put(dev_of_node(&chip->gpiodev->dev));
1280 }
1281