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