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