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