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