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/fwnode.h> 14 #include <linux/io.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_address.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 */ 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 */ 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 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 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 * 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 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 */ 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 */ 179 static void of_gpio_try_fixup_polarity(const struct device_node *np, 180 const char *propname, 181 enum of_gpio_flags *flags) 182 { 183 static const struct { 184 const char *compatible; 185 const char *propname; 186 bool active_high; 187 } gpios[] = { 188 #if IS_ENABLED(CONFIG_LCD_HX8357) 189 /* 190 * Himax LCD controllers used incorrectly named 191 * "gpios-reset" property and also specified wrong 192 * polarity. 193 */ 194 { "himax,hx8357", "gpios-reset", false }, 195 { "himax,hx8369", "gpios-reset", false }, 196 #endif 197 #if IS_ENABLED(CONFIG_MTD_NAND_JZ4780) 198 /* 199 * The rb-gpios semantics was undocumented and qi,lb60 (along with 200 * the ingenic driver) got it wrong. The active state encodes the 201 * NAND ready state, which is high level. Since there's no signal 202 * inverter on this board, it should be active-high. Let's fix that 203 * here for older DTs so we can re-use the generic nand_gpio_waitrdy() 204 * helper, and be consistent with what other drivers do. 205 */ 206 { "qi,lb60", "rb-gpios", true }, 207 #endif 208 #if IS_ENABLED(CONFIG_IEEE802154_CA8210) 209 /* 210 * According to the datasheet, the NRST pin 27 is an active-low 211 * signal. However, the device tree schema and admittedly 212 * the out-of-tree implementations have been used for a long 213 * time incorrectly by describing reset GPIO as active-high. 214 */ 215 { "cascoda,ca8210", "reset-gpio", false }, 216 #endif 217 #if IS_ENABLED(CONFIG_PCI_LANTIQ) 218 /* 219 * According to the PCI specification, the RST# pin is an 220 * active-low signal. However, most of the device trees that 221 * have been widely used for a long time incorrectly describe 222 * reset GPIO as active-high, and were also using wrong name 223 * for the property. 224 */ 225 { "lantiq,pci-xway", "gpio-reset", false }, 226 #endif 227 #if IS_ENABLED(CONFIG_REGULATOR_S5M8767) 228 /* 229 * According to S5M8767, the DVS and DS pin are 230 * active-high signals. However, exynos5250-spring.dts use 231 * active-low setting. 232 */ 233 { "samsung,s5m8767-pmic", "s5m8767,pmic-buck-dvs-gpios", true }, 234 { "samsung,s5m8767-pmic", "s5m8767,pmic-buck-ds-gpios", true }, 235 #endif 236 #if IS_ENABLED(CONFIG_TOUCHSCREEN_TSC2005) 237 /* 238 * DTS for Nokia N900 incorrectly specified "active high" 239 * polarity for the reset line, while the chip actually 240 * treats it as "active low". 241 */ 242 { "ti,tsc2005", "reset-gpios", false }, 243 #endif 244 #if IS_ENABLED(CONFIG_SND_SOC_WSA881X) 245 /* 246 * WSA881 powerdown is always active low, but some device trees 247 * missed this when first contributed. It also has a very strange 248 * compatible. 249 */ 250 { "sdw10217201000", "powerdown-gpios", false }, 251 #endif 252 }; 253 unsigned int i; 254 255 for (i = 0; i < ARRAY_SIZE(gpios); i++) { 256 if (of_device_is_compatible(np, gpios[i].compatible) && 257 !strcmp(propname, gpios[i].propname)) { 258 of_gpio_quirk_polarity(np, gpios[i].active_high, flags); 259 break; 260 } 261 } 262 } 263 264 static void of_gpio_set_polarity_by_property(const struct device_node *np, 265 const char *propname, 266 enum of_gpio_flags *flags) 267 { 268 const struct device_node *np_compat = np; 269 const struct device_node *np_propname = np; 270 static const struct { 271 const char *compatible; 272 const char *gpio_propname; 273 const char *polarity_propname; 274 } gpios[] = { 275 #if IS_ENABLED(CONFIG_FEC) 276 /* Freescale Fast Ethernet Controller */ 277 { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" }, 278 { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" }, 279 { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" }, 280 { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" }, 281 { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" }, 282 { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" }, 283 { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" }, 284 { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" }, 285 { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" }, 286 { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" }, 287 #endif 288 #if IS_ENABLED(CONFIG_MMC_ATMELMCI) 289 { "atmel,hsmci", "cd-gpios", "cd-inverted" }, 290 #endif 291 #if IS_ENABLED(CONFIG_PCI_IMX6) 292 { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" }, 293 { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" }, 294 { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" }, 295 { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" }, 296 { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" }, 297 { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" }, 298 { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" }, 299 #endif 300 301 /* 302 * The regulator GPIO handles are specified such that the 303 * presence or absence of "enable-active-high" solely controls 304 * the polarity of the GPIO line. Any phandle flags must 305 * be actively ignored. 306 */ 307 #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE) 308 { "regulator-fixed", "gpios", "enable-active-high" }, 309 { "regulator-fixed", "gpio", "enable-active-high" }, 310 { "reg-fixed-voltage", "gpios", "enable-active-high" }, 311 { "reg-fixed-voltage", "gpio", "enable-active-high" }, 312 #endif 313 #if IS_ENABLED(CONFIG_REGULATOR_GPIO) 314 { "regulator-gpio", "enable-gpio", "enable-active-high" }, 315 { "regulator-gpio", "enable-gpios", "enable-active-high" }, 316 #endif 317 }; 318 unsigned int i; 319 bool active_high; 320 321 #if IS_ENABLED(CONFIG_MMC_ATMELMCI) 322 /* 323 * The Atmel HSMCI has compatible property in the parent node and 324 * gpio property in a child node 325 */ 326 if (of_device_is_compatible(np->parent, "atmel,hsmci")) { 327 np_compat = np->parent; 328 np_propname = np; 329 } 330 #endif 331 332 for (i = 0; i < ARRAY_SIZE(gpios); i++) { 333 if (of_device_is_compatible(np_compat, gpios[i].compatible) && 334 !strcmp(propname, gpios[i].gpio_propname)) { 335 active_high = of_property_read_bool(np_propname, 336 gpios[i].polarity_propname); 337 of_gpio_quirk_polarity(np, active_high, flags); 338 break; 339 } 340 } 341 } 342 343 static void of_gpio_flags_quirks(const struct device_node *np, 344 const char *propname, 345 enum of_gpio_flags *flags, 346 int index) 347 { 348 of_gpio_try_fixup_polarity(np, propname, flags); 349 of_gpio_set_polarity_by_property(np, propname, flags); 350 351 /* 352 * Legacy open drain handling for fixed voltage regulators. 353 */ 354 if (IS_ENABLED(CONFIG_REGULATOR) && 355 of_device_is_compatible(np, "reg-fixed-voltage") && 356 of_property_read_bool(np, "gpio-open-drain")) { 357 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); 358 pr_info("%s uses legacy open drain flag - update the DTS if you can\n", 359 of_node_full_name(np)); 360 } 361 362 /* 363 * Legacy handling of SPI active high chip select. If we have a 364 * property named "cs-gpios" we need to inspect the child node 365 * to determine if the flags should have inverted semantics. 366 */ 367 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && 368 of_property_present(np, "cs-gpios")) { 369 u32 cs; 370 int ret; 371 372 for_each_child_of_node_scoped(np, child) { 373 ret = of_property_read_u32(child, "reg", &cs); 374 if (ret) 375 continue; 376 if (cs == index) { 377 /* 378 * SPI children have active low chip selects 379 * by default. This can be specified negatively 380 * by just omitting "spi-cs-high" in the 381 * device node, or actively by tagging on 382 * GPIO_ACTIVE_LOW as flag in the device 383 * tree. If the line is simultaneously 384 * tagged as active low in the device tree 385 * and has the "spi-cs-high" set, we get a 386 * conflict and the "spi-cs-high" flag will 387 * take precedence. 388 */ 389 bool active_high = of_property_read_bool(child, 390 "spi-cs-high"); 391 of_gpio_quirk_polarity(child, active_high, 392 flags); 393 break; 394 } 395 } 396 } 397 398 /* Legacy handling of stmmac's active-low PHY reset line */ 399 if (IS_ENABLED(CONFIG_STMMAC_ETH) && 400 !strcmp(propname, "snps,reset-gpio") && 401 of_property_read_bool(np, "snps,reset-active-low")) 402 *flags |= OF_GPIO_ACTIVE_LOW; 403 } 404 405 /** 406 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 407 * @np: device node to get GPIO from 408 * @propname: property name containing gpio specifier(s) 409 * @index: index of the GPIO 410 * @flags: a flags pointer to fill in 411 * 412 * Returns: 413 * GPIO descriptor to use with Linux GPIO API, or one of the errno 414 * value on the error condition. If @flags is not NULL the function also fills 415 * in flags for the GPIO. 416 */ 417 static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np, 418 const char *propname, int index, enum of_gpio_flags *flags) 419 { 420 struct of_phandle_args gpiospec; 421 struct gpio_desc *desc; 422 int ret; 423 424 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 425 &gpiospec); 426 if (ret) { 427 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 428 __func__, propname, np, index); 429 return ERR_PTR(ret); 430 } 431 432 struct gpio_device *gdev __free(gpio_device_put) = 433 of_find_gpio_device_by_xlate(&gpiospec); 434 if (!gdev) { 435 desc = ERR_PTR(-EPROBE_DEFER); 436 goto out; 437 } 438 439 desc = of_xlate_and_get_gpiod_flags(gpio_device_get_chip(gdev), 440 &gpiospec, flags); 441 if (IS_ERR(desc)) 442 goto out; 443 444 if (flags) 445 of_gpio_flags_quirks(np, propname, flags, index); 446 447 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 448 __func__, propname, np, index, 449 PTR_ERR_OR_ZERO(desc)); 450 451 out: 452 of_node_put(gpiospec.np); 453 454 return desc; 455 } 456 457 /* Converts gpio_lookup_flags into bitmask of GPIO_* values */ 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 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_NFC_S3FWRN5_I2C) 528 { "en", "s3fwrn5,en-gpios", "samsung,s3fwrn5-i2c" }, 529 { "wake", "s3fwrn5,fw-gpios", "samsung,s3fwrn5-i2c" }, 530 #endif 531 #if IS_ENABLED(CONFIG_PCI_LANTIQ) 532 /* MIPS Lantiq PCI */ 533 { "reset", "gpio-reset", "lantiq,pci-xway" }, 534 #endif 535 536 /* 537 * Some regulator bindings happened before we managed to 538 * establish that GPIO properties should be named 539 * "foo-gpios" so we have this special kludge for them. 540 */ 541 #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1) 542 { "wlf,ldoena", NULL, NULL }, /* Arizona */ 543 #endif 544 #if IS_ENABLED(CONFIG_REGULATOR_WM8994) 545 { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */ 546 { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */ 547 #endif 548 549 #if IS_ENABLED(CONFIG_SND_SOC_CS42L56) 550 { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" }, 551 #endif 552 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448) 553 { "i2s1-in-sel-gpio1", NULL, "mediatek,mt2701-cs42448-machine" }, 554 { "i2s1-in-sel-gpio2", NULL, "mediatek,mt2701-cs42448-machine" }, 555 #endif 556 #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X) 557 { "reset", "gpio-reset", "ti,tlv320aic3x" }, 558 { "reset", "gpio-reset", "ti,tlv320aic33" }, 559 { "reset", "gpio-reset", "ti,tlv320aic3007" }, 560 { "reset", "gpio-reset", "ti,tlv320aic3104" }, 561 { "reset", "gpio-reset", "ti,tlv320aic3106" }, 562 #endif 563 #if IS_ENABLED(CONFIG_SPI_GPIO) 564 /* 565 * The SPI GPIO bindings happened before we managed to 566 * establish that GPIO properties should be named 567 * "foo-gpios" so we have this special kludge for them. 568 */ 569 { "miso", "gpio-miso", "spi-gpio" }, 570 { "mosi", "gpio-mosi", "spi-gpio" }, 571 { "sck", "gpio-sck", "spi-gpio" }, 572 #endif 573 574 /* 575 * The old Freescale bindings use simply "gpios" as name 576 * for the chip select lines rather than "cs-gpios" like 577 * all other SPI hardware. Allow this specifically for 578 * Freescale and PPC devices. 579 */ 580 #if IS_ENABLED(CONFIG_SPI_FSL_SPI) 581 { "cs", "gpios", "fsl,spi" }, 582 { "cs", "gpios", "aeroflexgaisler,spictrl" }, 583 #endif 584 #if IS_ENABLED(CONFIG_SPI_PPC4xx) 585 { "cs", "gpios", "ibm,ppc4xx-spi" }, 586 #endif 587 588 #if IS_ENABLED(CONFIG_TYPEC_FUSB302) 589 /* 590 * Fairchild FUSB302 host is using undocumented "fcs,int_n" 591 * property without the compulsory "-gpios" suffix. 592 */ 593 { "fcs,int_n", NULL, "fcs,fusb302" }, 594 #endif 595 }; 596 struct gpio_desc *desc; 597 const char *legacy_id; 598 unsigned int i; 599 600 if (!con_id) 601 return ERR_PTR(-ENOENT); 602 603 for (i = 0; i < ARRAY_SIZE(gpios); i++) { 604 if (strcmp(con_id, gpios[i].con_id)) 605 continue; 606 607 if (gpios[i].compatible && 608 !of_device_is_compatible(np, gpios[i].compatible)) 609 continue; 610 611 legacy_id = gpios[i].legacy_id ?: gpios[i].con_id; 612 desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags); 613 if (!gpiod_not_found(desc)) { 614 pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n", 615 of_node_full_name(np), legacy_id, con_id); 616 return desc; 617 } 618 } 619 620 return ERR_PTR(-ENOENT); 621 } 622 623 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448) 624 static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np, 625 const char *con_id, 626 unsigned int idx, 627 enum of_gpio_flags *of_flags) 628 { 629 struct gpio_desc *desc; 630 const char *legacy_id; 631 632 if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)) 633 return ERR_PTR(-ENOENT); 634 635 if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine")) 636 return ERR_PTR(-ENOENT); 637 638 if (!con_id || strcmp(con_id, "i2s1-in-sel")) 639 return ERR_PTR(-ENOENT); 640 641 if (idx == 0) 642 legacy_id = "i2s1-in-sel-gpio1"; 643 else if (idx == 1) 644 legacy_id = "i2s1-in-sel-gpio2"; 645 else 646 return ERR_PTR(-ENOENT); 647 648 desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags); 649 if (!gpiod_not_found(desc)) 650 pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n", 651 of_node_full_name(np), legacy_id, con_id); 652 653 return desc; 654 } 655 #endif 656 657 /* 658 * Trigger sources are special, they allow us to use any GPIO as a LED trigger 659 * and have the name "trigger-sources" no matter which kind of phandle it is 660 * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case 661 * we allow looking something up that is not named "foo-gpios". 662 */ 663 static struct gpio_desc *of_find_trigger_gpio(struct device_node *np, 664 const char *con_id, 665 unsigned int idx, 666 enum of_gpio_flags *of_flags) 667 { 668 struct gpio_desc *desc; 669 670 if (!IS_ENABLED(CONFIG_LEDS_TRIGGER_GPIO)) 671 return ERR_PTR(-ENOENT); 672 673 if (!con_id || strcmp(con_id, "trigger-sources")) 674 return ERR_PTR(-ENOENT); 675 676 desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags); 677 if (!gpiod_not_found(desc)) 678 pr_debug("%s is used as a trigger\n", of_node_full_name(np)); 679 680 return desc; 681 } 682 683 684 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np, 685 const char *con_id, 686 unsigned int idx, 687 enum of_gpio_flags *of_flags); 688 static const of_find_gpio_quirk of_find_gpio_quirks[] = { 689 of_find_gpio_rename, 690 #if IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448) 691 of_find_mt2701_gpio, 692 #endif 693 of_find_trigger_gpio, 694 NULL 695 }; 696 697 struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, 698 unsigned int idx, unsigned long *flags) 699 { 700 char propname[32]; /* 32 is max size of property name */ 701 enum of_gpio_flags of_flags = 0; 702 const of_find_gpio_quirk *q; 703 struct gpio_desc *desc; 704 705 /* Try GPIO property "foo-gpios" and "foo-gpio" */ 706 for_each_gpio_property_name(propname, con_id) { 707 desc = of_get_named_gpiod_flags(np, propname, idx, &of_flags); 708 if (!gpiod_not_found(desc)) 709 break; 710 } 711 712 /* Properly named GPIO was not found, try workarounds */ 713 for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++) 714 desc = (*q)(np, con_id, idx, &of_flags); 715 716 if (IS_ERR(desc)) 717 return desc; 718 719 *flags = of_convert_gpio_flags(of_flags); 720 721 return desc; 722 } 723 724 int of_gpiochip_get_lflags(struct gpio_chip *chip, 725 struct fwnode_reference_args *gpiospec, 726 unsigned long *lflags) 727 { 728 enum of_gpio_flags xlate_flags; 729 struct of_phandle_args args; 730 struct gpio_desc *desc; 731 732 args.np = to_of_node(gpiospec->fwnode); 733 args.args_count = gpiospec->nargs; 734 735 for (int i = 0; i < args.args_count; i++) 736 args.args[i] = gpiospec->args[i]; 737 738 desc = of_xlate_and_get_gpiod_flags(chip, &args, &xlate_flags); 739 if (IS_ERR(desc)) 740 return PTR_ERR(desc); 741 742 *lflags = of_convert_gpio_flags(xlate_flags); 743 744 return 0; 745 } 746 747 #ifdef CONFIG_OF_DYNAMIC 748 /** 749 * of_gpiochip_remove_hog - Remove all hogs in a hog device node 750 * @chip: gpio chip to act on 751 * @hog: device node describing the hogs 752 */ 753 static void of_gpiochip_remove_hog(struct gpio_chip *chip, 754 struct device_node *hog) 755 { 756 struct gpio_desc *desc; 757 758 for_each_gpio_desc_with_flag(chip, desc, GPIOD_FLAG_IS_HOGGED) 759 if (READ_ONCE(desc->hog) == hog) 760 gpiochip_free_own_desc(desc); 761 } 762 763 static int of_gpiochip_match_node(struct gpio_chip *chip, const void *data) 764 { 765 return device_match_of_node(&chip->gpiodev->dev, data); 766 } 767 768 static struct gpio_device *of_find_gpio_device_by_node(struct device_node *np) 769 { 770 return gpio_device_find(np, of_gpiochip_match_node); 771 } 772 773 static int of_gpio_notify(struct notifier_block *nb, unsigned long action, 774 void *arg) 775 { 776 struct gpio_device *gdev __free(gpio_device_put) = NULL; 777 struct of_reconfig_data *rd = arg; 778 int ret; 779 780 /* 781 * This only supports adding and removing complete gpio-hog nodes. 782 * Modifying an existing gpio-hog node is not supported (except for 783 * changing its "status" property, which is treated the same as 784 * addition/removal). 785 */ 786 switch (of_reconfig_get_state_change(action, arg)) { 787 case OF_RECONFIG_CHANGE_ADD: 788 if (!of_property_read_bool(rd->dn, "gpio-hog")) 789 return NOTIFY_DONE; /* not for us */ 790 791 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) 792 return NOTIFY_DONE; 793 794 gdev = of_find_gpio_device_by_node(rd->dn->parent); 795 if (!gdev) 796 return NOTIFY_DONE; /* not for us */ 797 798 ret = gpiochip_add_hog(gpio_device_get_chip(gdev), of_fwnode_handle(rd->dn)); 799 if (ret < 0) { 800 pr_err("%s: failed to add hogs for %pOF\n", __func__, 801 rd->dn); 802 of_node_clear_flag(rd->dn, OF_POPULATED); 803 return notifier_from_errno(ret); 804 } 805 return NOTIFY_OK; 806 807 case OF_RECONFIG_CHANGE_REMOVE: 808 if (!of_node_check_flag(rd->dn, OF_POPULATED)) 809 return NOTIFY_DONE; /* already depopulated */ 810 811 gdev = of_find_gpio_device_by_node(rd->dn->parent); 812 if (!gdev) 813 return NOTIFY_DONE; /* not for us */ 814 815 of_gpiochip_remove_hog(gpio_device_get_chip(gdev), rd->dn); 816 of_node_clear_flag(rd->dn, OF_POPULATED); 817 return NOTIFY_OK; 818 } 819 820 return NOTIFY_DONE; 821 } 822 823 struct notifier_block gpio_of_notifier = { 824 .notifier_call = of_gpio_notify, 825 }; 826 #endif /* CONFIG_OF_DYNAMIC */ 827 828 /** 829 * of_gpio_twocell_xlate - translate twocell gpiospec to the GPIO number and flags 830 * @gc: pointer to the gpio_chip structure 831 * @gpiospec: GPIO specifier as found in the device tree 832 * @flags: a flags pointer to fill in 833 * 834 * This is simple translation function, suitable for the most 1:1 mapped 835 * GPIO chips. This function performs only one sanity check: whether GPIO 836 * is less than ngpios (that is specified in the gpio_chip). 837 * 838 * Returns: 839 * GPIO number (>= 0) on success, negative errno on failure. 840 */ 841 static int of_gpio_twocell_xlate(struct gpio_chip *gc, 842 const struct of_phandle_args *gpiospec, 843 u32 *flags) 844 { 845 /* 846 * We're discouraging gpio_cells < 2, since that way you'll have to 847 * write your own xlate function (that will have to retrieve the GPIO 848 * number and the flags from a single gpio cell -- this is possible, 849 * but not recommended). 850 */ 851 if (gc->of_gpio_n_cells != 2) { 852 WARN_ON(1); 853 return -EINVAL; 854 } 855 856 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 857 return -EINVAL; 858 859 if (gpiospec->args[0] >= gc->ngpio) 860 return -EINVAL; 861 862 if (flags) 863 *flags = gpiospec->args[1]; 864 865 return gpiospec->args[0]; 866 } 867 868 /** 869 * of_gpio_threecell_xlate - translate threecell gpiospec to the GPIO number and flags 870 * @gc: pointer to the gpio_chip structure 871 * @gpiospec: GPIO specifier as found in the device tree 872 * @flags: a flags pointer to fill in 873 * 874 * This is simple translation function, suitable for the most 1:n mapped 875 * GPIO chips, i.e. several GPIO chip instances from one device tree node. 876 * In this case the following binding is implied: 877 * 878 * foo-gpios = <&gpio instance offset flags>; 879 * 880 * Returns: 881 * GPIO number (>= 0) on success, negative errno on failure. 882 */ 883 static int of_gpio_threecell_xlate(struct gpio_chip *gc, 884 const struct of_phandle_args *gpiospec, 885 u32 *flags) 886 { 887 if (gc->of_gpio_n_cells != 3) { 888 WARN_ON(1); 889 return -EINVAL; 890 } 891 892 if (WARN_ON(gpiospec->args_count != 3)) 893 return -EINVAL; 894 895 /* 896 * Check chip instance number, the driver responds with true if 897 * this is the chip we are looking for. 898 */ 899 if (!gc->of_node_instance_match(gc, gpiospec->args[0])) 900 return -EINVAL; 901 902 if (gpiospec->args[1] >= gc->ngpio) 903 return -EINVAL; 904 905 if (flags) 906 *flags = gpiospec->args[2]; 907 908 return gpiospec->args[1]; 909 } 910 911 #ifdef CONFIG_PINCTRL 912 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 913 { 914 struct of_phandle_args pinspec; 915 struct pinctrl_dev *pctldev; 916 struct device_node *np; 917 int index = 0, ret, trim; 918 const char *name; 919 static const char group_names_propname[] = "gpio-ranges-group-names"; 920 bool has_group_names; 921 int offset; /* Offset of the first GPIO line on the chip */ 922 int pin; /* Pin base number in the range */ 923 int count; /* Number of pins/GPIO lines to map */ 924 925 np = dev_of_node(&chip->gpiodev->dev); 926 if (!np) 927 return 0; 928 929 has_group_names = of_property_present(np, group_names_propname); 930 931 for (;; index++) { 932 /* 933 * Ordinary phandles contain 2-3 cells: 934 * gpios = <&gpio [instance] offset flags>; 935 * Ranges always contain one more cell: 936 * gpio-ranges <&pinctrl [gpio_instance] gpio_offet pin_offet count>; 937 * This is why we parse chip->of_gpio_n_cells + 1 cells 938 */ 939 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 940 chip->of_gpio_n_cells + 1, 941 index, &pinspec); 942 if (ret) 943 break; 944 945 pctldev = of_pinctrl_get(pinspec.np); 946 of_node_put(pinspec.np); 947 if (!pctldev) 948 return -EPROBE_DEFER; 949 950 if (chip->of_gpio_n_cells == 3) { 951 /* First cell is the gpiochip instance number */ 952 offset = pinspec.args[1]; 953 pin = pinspec.args[2]; 954 count = pinspec.args[3]; 955 } else { 956 offset = pinspec.args[0]; 957 pin = pinspec.args[1]; 958 count = pinspec.args[2]; 959 } 960 961 /* 962 * With multiple GPIO chips per node, check that this chip is the 963 * right instance. 964 */ 965 if (chip->of_node_instance_match && 966 (chip->of_gpio_n_cells == 3) && 967 !chip->of_node_instance_match(chip, pinspec.args[0])) 968 continue; 969 970 /* Ignore ranges outside of this GPIO chip */ 971 if (offset >= (chip->offset + chip->ngpio)) 972 continue; 973 if (offset + count <= chip->offset) 974 continue; 975 976 if (count) { 977 /* npins != 0: linear range */ 978 if (has_group_names) { 979 of_property_read_string_index(np, 980 group_names_propname, 981 index, &name); 982 if (strlen(name)) { 983 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 984 np); 985 break; 986 } 987 } 988 989 /* Trim the range to fit this GPIO chip */ 990 if (chip->offset > offset) { 991 trim = chip->offset - offset; 992 count -= trim; 993 pin += trim; 994 offset = 0; 995 } else { 996 offset -= chip->offset; 997 } 998 if ((offset + count) > chip->ngpio) 999 count = chip->ngpio - offset; 1000 1001 ret = gpiochip_add_pin_range(chip, 1002 pinctrl_dev_get_devname(pctldev), 1003 offset, 1004 pin, 1005 count); 1006 if (ret) 1007 return ret; 1008 } else { 1009 /* npins == 0: special range */ 1010 if (pin) { 1011 pr_err("%pOF: Illegal gpio-range format.\n", 1012 np); 1013 break; 1014 } 1015 1016 if (!has_group_names) { 1017 pr_err("%pOF: GPIO group range requested but no %s property.\n", 1018 np, group_names_propname); 1019 break; 1020 } 1021 1022 ret = of_property_read_string_index(np, 1023 group_names_propname, 1024 index, &name); 1025 if (ret) 1026 break; 1027 1028 if (!strlen(name)) { 1029 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 1030 np); 1031 break; 1032 } 1033 1034 ret = gpiochip_add_pingroup_range(chip, pctldev, 1035 offset, name); 1036 if (ret) 1037 return ret; 1038 } 1039 } 1040 1041 return 0; 1042 } 1043 1044 #else 1045 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 1046 #endif 1047 1048 int of_gpiochip_add(struct gpio_chip *chip) 1049 { 1050 struct device_node *np; 1051 int ret; 1052 1053 np = dev_of_node(&chip->gpiodev->dev); 1054 if (!np) 1055 return 0; 1056 1057 if (!chip->of_xlate) { 1058 if (chip->of_gpio_n_cells == 3) { 1059 if (!chip->of_node_instance_match) 1060 return -EINVAL; 1061 chip->of_xlate = of_gpio_threecell_xlate; 1062 } else { 1063 chip->of_gpio_n_cells = 2; 1064 chip->of_xlate = of_gpio_twocell_xlate; 1065 } 1066 } 1067 1068 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 1069 return -EINVAL; 1070 1071 ret = of_gpiochip_add_pin_range(chip); 1072 if (ret) 1073 return ret; 1074 1075 of_node_get(np); 1076 1077 return ret; 1078 } 1079 1080 void of_gpiochip_remove(struct gpio_chip *chip) 1081 { 1082 struct device_node *np = dev_of_node(&chip->gpiodev->dev); 1083 1084 for_each_child_of_node_scoped(np, child) { 1085 if (of_property_present(child, "gpio-hog")) 1086 of_node_clear_flag(child, OF_POPULATED); 1087 } 1088 1089 of_node_put(np); 1090 } 1091 1092 bool of_gpiochip_instance_match(struct gpio_chip *gc, unsigned int index) 1093 { 1094 if (gc->of_node_instance_match) 1095 return gc->of_node_instance_match(gc, index); 1096 1097 return false; 1098 } 1099