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