127038c3eSVladimir Zapolskiy // SPDX-License-Identifier: GPL-2.0+ 2f141ed65SGrant Likely /* 3f141ed65SGrant Likely * OF helpers for the GPIO API 4f141ed65SGrant Likely * 5f141ed65SGrant Likely * Copyright (c) 2007-2008 MontaVista Software, Inc. 6f141ed65SGrant Likely * 7f141ed65SGrant Likely * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 8f141ed65SGrant Likely */ 9f141ed65SGrant Likely 10f141ed65SGrant Likely #include <linux/device.h> 11bea4dbeeSSachin Kamat #include <linux/err.h> 12f141ed65SGrant Likely #include <linux/errno.h> 13f141ed65SGrant Likely #include <linux/module.h> 14f141ed65SGrant Likely #include <linux/io.h> 15af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h> 16f141ed65SGrant Likely #include <linux/of.h> 17f141ed65SGrant Likely #include <linux/of_address.h> 18f141ed65SGrant Likely #include <linux/of_gpio.h> 19f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h> 20f141ed65SGrant Likely #include <linux/slab.h> 21f625d460SBenoit Parrot #include <linux/gpio/machine.h> 22f141ed65SGrant Likely 231bd6b601SAlexandre Courbot #include "gpiolib.h" 24f626d6dfSLinus Walleij #include "gpiolib-of.h" 25f626d6dfSLinus Walleij 2671b8f600SLinus Walleij /** 2771b8f600SLinus Walleij * of_gpio_spi_cs_get_count() - special GPIO counting for SPI 28c5a66b97SLee Jones * @dev: Consuming device 29c5a66b97SLee Jones * @con_id: Function within the GPIO consumer 30c5a66b97SLee Jones * 3171b8f600SLinus Walleij * Some elder GPIO controllers need special quirks. Currently we handle 3247267732SLinus Walleij * the Freescale and PPC GPIO controller with bindings that doesn't use the 3371b8f600SLinus Walleij * established "cs-gpios" for chip selects but instead rely on 3471b8f600SLinus Walleij * "gpios" for the chip select lines. If we detect this, we redirect 3571b8f600SLinus Walleij * the counting of "cs-gpios" to count "gpios" transparent to the 3671b8f600SLinus Walleij * driver. 3771b8f600SLinus Walleij */ 38a1f4c96bSYueHaibing static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) 3971b8f600SLinus Walleij { 4071b8f600SLinus Walleij struct device_node *np = dev->of_node; 4171b8f600SLinus Walleij 4271b8f600SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 4371b8f600SLinus Walleij return 0; 4471b8f600SLinus Walleij if (!con_id || strcmp(con_id, "cs")) 4571b8f600SLinus Walleij return 0; 4671b8f600SLinus Walleij if (!of_device_is_compatible(np, "fsl,spi") && 4747267732SLinus Walleij !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && 4847267732SLinus Walleij !of_device_is_compatible(np, "ibm,ppc4xx-spi")) 4971b8f600SLinus Walleij return 0; 5071b8f600SLinus Walleij return of_gpio_named_count(np, "gpios"); 5171b8f600SLinus Walleij } 5271b8f600SLinus Walleij 53f626d6dfSLinus Walleij /* 54f626d6dfSLinus Walleij * This is used by external users of of_gpio_count() from <linux/of_gpio.h> 55f626d6dfSLinus Walleij * 56f626d6dfSLinus Walleij * FIXME: get rid of those external users by converting them to GPIO 57808b9931SGeert Uytterhoeven * descriptors and let them all use gpiod_count() 58f626d6dfSLinus Walleij */ 59f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id) 60f626d6dfSLinus Walleij { 61f626d6dfSLinus Walleij int ret; 62f626d6dfSLinus Walleij char propname[32]; 63f626d6dfSLinus Walleij unsigned int i; 64f626d6dfSLinus Walleij 6571b8f600SLinus Walleij ret = of_gpio_spi_cs_get_count(dev, con_id); 6671b8f600SLinus Walleij if (ret > 0) 6771b8f600SLinus Walleij return ret; 6871b8f600SLinus Walleij 69f626d6dfSLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 70f626d6dfSLinus Walleij if (con_id) 71f626d6dfSLinus Walleij snprintf(propname, sizeof(propname), "%s-%s", 72f626d6dfSLinus Walleij con_id, gpio_suffixes[i]); 73f626d6dfSLinus Walleij else 74f626d6dfSLinus Walleij snprintf(propname, sizeof(propname), "%s", 75f626d6dfSLinus Walleij gpio_suffixes[i]); 76f626d6dfSLinus Walleij 77f626d6dfSLinus Walleij ret = of_gpio_named_count(dev->of_node, propname); 78f626d6dfSLinus Walleij if (ret > 0) 79f626d6dfSLinus Walleij break; 80f626d6dfSLinus Walleij } 81f626d6dfSLinus Walleij return ret ? ret : -ENOENT; 82f626d6dfSLinus Walleij } 83af8b6375SAlexandre Courbot 84c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) 853d0f7cf0SGrant Likely { 86c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec = data; 87c7e9d398SMasahiro Yamada 88c7e9d398SMasahiro Yamada return chip->gpiodev->dev.of_node == gpiospec->np && 89d49b48f0SVincent Whitchurch chip->of_xlate && 90c7e9d398SMasahiro Yamada chip->of_xlate(chip, gpiospec, NULL) >= 0; 917b8792bbSHans Holmberg } 923d0f7cf0SGrant Likely 93c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate( 94c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec) 95762c2e46SMasahiro Yamada { 96c7e9d398SMasahiro Yamada return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); 973d0f7cf0SGrant Likely } 983d0f7cf0SGrant Likely 9999468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, 10099468c1aSMasahiro Yamada struct of_phandle_args *gpiospec, 10199468c1aSMasahiro Yamada enum of_gpio_flags *flags) 10299468c1aSMasahiro Yamada { 10399468c1aSMasahiro Yamada int ret; 10499468c1aSMasahiro Yamada 10599468c1aSMasahiro Yamada if (chip->of_gpio_n_cells != gpiospec->args_count) 10699468c1aSMasahiro Yamada return ERR_PTR(-EINVAL); 10799468c1aSMasahiro Yamada 10899468c1aSMasahiro Yamada ret = chip->of_xlate(chip, gpiospec, flags); 10999468c1aSMasahiro Yamada if (ret < 0) 11099468c1aSMasahiro Yamada return ERR_PTR(ret); 11199468c1aSMasahiro Yamada 11299468c1aSMasahiro Yamada return gpiochip_get_desc(chip, ret); 113f141ed65SGrant Likely } 114f141ed65SGrant Likely 115f626d6dfSLinus Walleij /** 116f626d6dfSLinus Walleij * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs 117f626d6dfSLinus Walleij * to set the .valid_mask 11814e8c535SRandy Dunlap * @gc: the target gpio_chip 11914e8c535SRandy Dunlap * 12014e8c535SRandy Dunlap * Return: true if the valid mask needs to be set 121f626d6dfSLinus Walleij */ 12249281a22SStephen Boyd bool of_gpio_need_valid_mask(const struct gpio_chip *gc) 123f626d6dfSLinus Walleij { 124f626d6dfSLinus Walleij int size; 1258990899dSKrzysztof Kozlowski const struct device_node *np = gc->of_node; 126f626d6dfSLinus Walleij 127f626d6dfSLinus Walleij size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 128f626d6dfSLinus Walleij if (size > 0 && size % 2 == 0) 129f626d6dfSLinus Walleij return true; 130f626d6dfSLinus Walleij return false; 131f626d6dfSLinus Walleij } 132f626d6dfSLinus Walleij 133e6ae9a83SKrzysztof Kozlowski static void of_gpio_flags_quirks(const struct device_node *np, 13489a5e15bSLinus Walleij const char *propname, 1356953c57aSLinus Walleij enum of_gpio_flags *flags, 1366953c57aSLinus Walleij int index) 137a603a2b8SLinus Walleij { 138a603a2b8SLinus Walleij /* 139a603a2b8SLinus Walleij * Some GPIO fixed regulator quirks. 140a603a2b8SLinus Walleij * Note that active low is the default. 141a603a2b8SLinus Walleij */ 142a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 143906402a4SLinus Walleij (of_device_is_compatible(np, "regulator-fixed") || 144906402a4SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") || 145a71a81e7SGeert Uytterhoeven (!(strcmp(propname, "enable-gpio") && 146a71a81e7SGeert Uytterhoeven strcmp(propname, "enable-gpios")) && 147a71a81e7SGeert Uytterhoeven of_device_is_compatible(np, "regulator-gpio")))) { 148228fc010SLucas Stach bool active_low = !of_property_read_bool(np, 149228fc010SLucas Stach "enable-active-high"); 150a603a2b8SLinus Walleij /* 151a603a2b8SLinus Walleij * The regulator GPIO handles are specified such that the 152a603a2b8SLinus Walleij * presence or absence of "enable-active-high" solely controls 153a603a2b8SLinus Walleij * the polarity of the GPIO line. Any phandle flags must 154a603a2b8SLinus Walleij * be actively ignored. 155a603a2b8SLinus Walleij */ 156228fc010SLucas Stach if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) { 157a603a2b8SLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 158a603a2b8SLinus Walleij of_node_full_name(np)); 159a603a2b8SLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 160a603a2b8SLinus Walleij } 161228fc010SLucas Stach if (active_low) 162a603a2b8SLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 163a603a2b8SLinus Walleij } 164a603a2b8SLinus Walleij /* 165a603a2b8SLinus Walleij * Legacy open drain handling for fixed voltage regulators. 166a603a2b8SLinus Walleij */ 167a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 168a603a2b8SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") && 169a603a2b8SLinus Walleij of_property_read_bool(np, "gpio-open-drain")) { 170a603a2b8SLinus Walleij *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); 171a603a2b8SLinus Walleij pr_info("%s uses legacy open drain flag - update the DTS if you can\n", 172a603a2b8SLinus Walleij of_node_full_name(np)); 173a603a2b8SLinus Walleij } 1746953c57aSLinus Walleij 1756953c57aSLinus Walleij /* 1766953c57aSLinus Walleij * Legacy handling of SPI active high chip select. If we have a 1776953c57aSLinus Walleij * property named "cs-gpios" we need to inspect the child node 1786953c57aSLinus Walleij * to determine if the flags should have inverted semantics. 1796953c57aSLinus Walleij */ 180da7f1349SLinus Walleij if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && 181a71a81e7SGeert Uytterhoeven of_property_read_bool(np, "cs-gpios")) { 1826953c57aSLinus Walleij struct device_node *child; 1836953c57aSLinus Walleij u32 cs; 1846953c57aSLinus Walleij int ret; 1856953c57aSLinus Walleij 1866953c57aSLinus Walleij for_each_child_of_node(np, child) { 1876953c57aSLinus Walleij ret = of_property_read_u32(child, "reg", &cs); 188c1c04ceaSLinus Walleij if (ret) 1896953c57aSLinus Walleij continue; 1906953c57aSLinus Walleij if (cs == index) { 1916953c57aSLinus Walleij /* 1926953c57aSLinus Walleij * SPI children have active low chip selects 1936953c57aSLinus Walleij * by default. This can be specified negatively 1946953c57aSLinus Walleij * by just omitting "spi-cs-high" in the 1956953c57aSLinus Walleij * device node, or actively by tagging on 1966953c57aSLinus Walleij * GPIO_ACTIVE_LOW as flag in the device 1976953c57aSLinus Walleij * tree. If the line is simultaneously 1986953c57aSLinus Walleij * tagged as active low in the device tree 1996953c57aSLinus Walleij * and has the "spi-cs-high" set, we get a 2006953c57aSLinus Walleij * conflict and the "spi-cs-high" flag will 2016953c57aSLinus Walleij * take precedence. 2026953c57aSLinus Walleij */ 2037ce40277SAndrey Smirnov if (of_property_read_bool(child, "spi-cs-high")) { 2046953c57aSLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 2056953c57aSLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 2067ce40277SAndrey Smirnov of_node_full_name(child)); 2076953c57aSLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 2086953c57aSLinus Walleij } 2096953c57aSLinus Walleij } else { 2106953c57aSLinus Walleij if (!(*flags & OF_GPIO_ACTIVE_LOW)) 2116953c57aSLinus Walleij pr_info("%s enforce active low on chipselect handle\n", 2127ce40277SAndrey Smirnov of_node_full_name(child)); 2136953c57aSLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 2146953c57aSLinus Walleij } 21589fea04cSNishka Dasgupta of_node_put(child); 2166953c57aSLinus Walleij break; 2176953c57aSLinus Walleij } 2186953c57aSLinus Walleij } 2196953c57aSLinus Walleij } 220edc1ef3fSMartin Blumenstingl 221edc1ef3fSMartin Blumenstingl /* Legacy handling of stmmac's active-low PHY reset line */ 222edc1ef3fSMartin Blumenstingl if (IS_ENABLED(CONFIG_STMMAC_ETH) && 223edc1ef3fSMartin Blumenstingl !strcmp(propname, "snps,reset-gpio") && 224edc1ef3fSMartin Blumenstingl of_property_read_bool(np, "snps,reset-active-low")) 225edc1ef3fSMartin Blumenstingl *flags |= OF_GPIO_ACTIVE_LOW; 226a603a2b8SLinus Walleij } 227a603a2b8SLinus Walleij 228f141ed65SGrant Likely /** 229af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 230f141ed65SGrant Likely * @np: device node to get GPIO from 231f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 232f141ed65SGrant Likely * @index: index of the GPIO 233f141ed65SGrant Likely * @flags: a flags pointer to fill in 234f141ed65SGrant Likely * 235af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 236f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 237f141ed65SGrant Likely * in flags for the GPIO. 238f141ed65SGrant Likely */ 239e6ae9a83SKrzysztof Kozlowski static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np, 240af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 241f141ed65SGrant Likely { 242762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 243762c2e46SMasahiro Yamada struct gpio_chip *chip; 244762c2e46SMasahiro Yamada struct gpio_desc *desc; 245f141ed65SGrant Likely int ret; 246f141ed65SGrant Likely 247c11e6f0fSStephen Boyd ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 248762c2e46SMasahiro Yamada &gpiospec); 2493d0f7cf0SGrant Likely if (ret) { 2507eb6ce2fSRob Herring pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 2517eb6ce2fSRob Herring __func__, propname, np, index); 252af8b6375SAlexandre Courbot return ERR_PTR(ret); 2533d0f7cf0SGrant Likely } 254f141ed65SGrant Likely 255c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 256762c2e46SMasahiro Yamada if (!chip) { 257762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 258762c2e46SMasahiro Yamada goto out; 259762c2e46SMasahiro Yamada } 2603d0f7cf0SGrant Likely 26199468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 262762c2e46SMasahiro Yamada if (IS_ERR(desc)) 263762c2e46SMasahiro Yamada goto out; 264762c2e46SMasahiro Yamada 265605f2d34SLinus Walleij if (flags) 26689a5e15bSLinus Walleij of_gpio_flags_quirks(np, propname, flags, index); 267a603a2b8SLinus Walleij 2687eb6ce2fSRob Herring pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 2697eb6ce2fSRob Herring __func__, propname, np, index, 270762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 271762c2e46SMasahiro Yamada 272762c2e46SMasahiro Yamada out: 273762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 274762c2e46SMasahiro Yamada 275762c2e46SMasahiro Yamada return desc; 276f141ed65SGrant Likely } 277f141ed65SGrant Likely 278e6ae9a83SKrzysztof Kozlowski int of_get_named_gpio_flags(const struct device_node *np, const char *list_name, 279f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 280f01d9075SAlexandre Courbot { 281f01d9075SAlexandre Courbot struct gpio_desc *desc; 282f01d9075SAlexandre Courbot 283f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 284f01d9075SAlexandre Courbot 285f01d9075SAlexandre Courbot if (IS_ERR(desc)) 286f01d9075SAlexandre Courbot return PTR_ERR(desc); 287f01d9075SAlexandre Courbot else 288f01d9075SAlexandre Courbot return desc_to_gpio(desc); 289f01d9075SAlexandre Courbot } 2906d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags); 291f01d9075SAlexandre Courbot 292f626d6dfSLinus Walleij /** 293f626d6dfSLinus Walleij * gpiod_get_from_of_node() - obtain a GPIO from an OF node 294f626d6dfSLinus Walleij * @node: handle of the OF node 295f626d6dfSLinus Walleij * @propname: name of the DT property representing the GPIO 296f626d6dfSLinus Walleij * @index: index of the GPIO to obtain for the consumer 297f626d6dfSLinus Walleij * @dflags: GPIO initialization flags 298f626d6dfSLinus Walleij * @label: label to attach to the requested GPIO 299f626d6dfSLinus Walleij * 300f626d6dfSLinus Walleij * Returns: 301f626d6dfSLinus Walleij * On successful request the GPIO pin is configured in accordance with 302f626d6dfSLinus Walleij * provided @dflags. 303f626d6dfSLinus Walleij * 304f626d6dfSLinus Walleij * In case of error an ERR_PTR() is returned. 305f626d6dfSLinus Walleij */ 306e6ae9a83SKrzysztof Kozlowski struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node, 307f626d6dfSLinus Walleij const char *propname, int index, 308f626d6dfSLinus Walleij enum gpiod_flags dflags, 309f626d6dfSLinus Walleij const char *label) 310f626d6dfSLinus Walleij { 311f626d6dfSLinus Walleij unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 312f626d6dfSLinus Walleij struct gpio_desc *desc; 313f626d6dfSLinus Walleij enum of_gpio_flags flags; 314f626d6dfSLinus Walleij bool active_low = false; 315f626d6dfSLinus Walleij bool single_ended = false; 316f626d6dfSLinus Walleij bool open_drain = false; 317f626d6dfSLinus Walleij bool transitory = false; 318f626d6dfSLinus Walleij int ret; 319f626d6dfSLinus Walleij 320f626d6dfSLinus Walleij desc = of_get_named_gpiod_flags(node, propname, 321f626d6dfSLinus Walleij index, &flags); 322f626d6dfSLinus Walleij 323f626d6dfSLinus Walleij if (!desc || IS_ERR(desc)) { 324f626d6dfSLinus Walleij return desc; 325f626d6dfSLinus Walleij } 326f626d6dfSLinus Walleij 327f626d6dfSLinus Walleij active_low = flags & OF_GPIO_ACTIVE_LOW; 328f626d6dfSLinus Walleij single_ended = flags & OF_GPIO_SINGLE_ENDED; 329f626d6dfSLinus Walleij open_drain = flags & OF_GPIO_OPEN_DRAIN; 330f626d6dfSLinus Walleij transitory = flags & OF_GPIO_TRANSITORY; 331f626d6dfSLinus Walleij 332f626d6dfSLinus Walleij ret = gpiod_request(desc, label); 333be7ae45cSMarco Felsch if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 334f626d6dfSLinus Walleij return desc; 335f626d6dfSLinus Walleij if (ret) 336f626d6dfSLinus Walleij return ERR_PTR(ret); 337f626d6dfSLinus Walleij 338f626d6dfSLinus Walleij if (active_low) 339f626d6dfSLinus Walleij lflags |= GPIO_ACTIVE_LOW; 340f626d6dfSLinus Walleij 341f626d6dfSLinus Walleij if (single_ended) { 342f626d6dfSLinus Walleij if (open_drain) 343f626d6dfSLinus Walleij lflags |= GPIO_OPEN_DRAIN; 344f626d6dfSLinus Walleij else 345f626d6dfSLinus Walleij lflags |= GPIO_OPEN_SOURCE; 346f626d6dfSLinus Walleij } 347f626d6dfSLinus Walleij 348f626d6dfSLinus Walleij if (transitory) 349f626d6dfSLinus Walleij lflags |= GPIO_TRANSITORY; 350f626d6dfSLinus Walleij 351ea06a482SAdam Ford if (flags & OF_GPIO_PULL_UP) 352ea06a482SAdam Ford lflags |= GPIO_PULL_UP; 353ea06a482SAdam Ford 354ea06a482SAdam Ford if (flags & OF_GPIO_PULL_DOWN) 355ea06a482SAdam Ford lflags |= GPIO_PULL_DOWN; 356ea06a482SAdam Ford 357f626d6dfSLinus Walleij ret = gpiod_configure_flags(desc, propname, lflags, dflags); 358f626d6dfSLinus Walleij if (ret < 0) { 359f626d6dfSLinus Walleij gpiod_put(desc); 360f626d6dfSLinus Walleij return ERR_PTR(ret); 361f626d6dfSLinus Walleij } 362f626d6dfSLinus Walleij 363f626d6dfSLinus Walleij return desc; 364f626d6dfSLinus Walleij } 3656d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node); 366f141ed65SGrant Likely 367c8582339SLinus Walleij /* 368c8582339SLinus Walleij * The SPI GPIO bindings happened before we managed to establish that GPIO 369c8582339SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 370c8582339SLinus Walleij * them. 371c8582339SLinus Walleij */ 372c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, 373c8582339SLinus Walleij enum of_gpio_flags *of_flags) 374c8582339SLinus Walleij { 375c8582339SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 3768990899dSKrzysztof Kozlowski const struct device_node *np = dev->of_node; 377c8582339SLinus Walleij struct gpio_desc *desc; 378c8582339SLinus Walleij 379c8582339SLinus Walleij /* 380c8582339SLinus Walleij * Hopefully the compiler stubs the rest of the function if this 381c8582339SLinus Walleij * is false. 382c8582339SLinus Walleij */ 383c8582339SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 384c8582339SLinus Walleij return ERR_PTR(-ENOENT); 385c8582339SLinus Walleij 386c8582339SLinus Walleij /* Allow this specifically for "spi-gpio" devices */ 387c8582339SLinus Walleij if (!of_device_is_compatible(np, "spi-gpio") || !con_id) 388c8582339SLinus Walleij return ERR_PTR(-ENOENT); 389c8582339SLinus Walleij 390c8582339SLinus Walleij /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ 391c8582339SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); 392c8582339SLinus Walleij 393c8582339SLinus Walleij desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); 394c8582339SLinus Walleij return desc; 395c8582339SLinus Walleij } 396c8582339SLinus Walleij 3976a537d48SLinus Walleij /* 398e3023bf8SLinus Walleij * The old Freescale bindings use simply "gpios" as name for the chip select 399e3023bf8SLinus Walleij * lines rather than "cs-gpios" like all other SPI hardware. Account for this 400e3023bf8SLinus Walleij * with a special quirk. 401e3023bf8SLinus Walleij */ 402e3023bf8SLinus Walleij static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, 403e3023bf8SLinus Walleij const char *con_id, 404e3023bf8SLinus Walleij unsigned int idx, 405e3023bf8SLinus Walleij unsigned long *flags) 406e3023bf8SLinus Walleij { 4078990899dSKrzysztof Kozlowski const struct device_node *np = dev->of_node; 408e3023bf8SLinus Walleij 409e3023bf8SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 410e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 411e3023bf8SLinus Walleij 41247267732SLinus Walleij /* Allow this specifically for Freescale and PPC devices */ 413e3023bf8SLinus Walleij if (!of_device_is_compatible(np, "fsl,spi") && 41447267732SLinus Walleij !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && 41547267732SLinus Walleij !of_device_is_compatible(np, "ibm,ppc4xx-spi")) 416e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 417e3023bf8SLinus Walleij /* Allow only if asking for "cs-gpios" */ 418e3023bf8SLinus Walleij if (!con_id || strcmp(con_id, "cs")) 419e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 420e3023bf8SLinus Walleij 421e3023bf8SLinus Walleij /* 422e3023bf8SLinus Walleij * While all other SPI controllers use "cs-gpios" the Freescale 423e3023bf8SLinus Walleij * uses just "gpios" so translate to that when "cs-gpios" is 424e3023bf8SLinus Walleij * requested. 425e3023bf8SLinus Walleij */ 426e3023bf8SLinus Walleij return of_find_gpio(dev, NULL, idx, flags); 427e3023bf8SLinus Walleij } 428e3023bf8SLinus Walleij 429e3023bf8SLinus Walleij /* 4306a537d48SLinus Walleij * Some regulator bindings happened before we managed to establish that GPIO 4316a537d48SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 4326a537d48SLinus Walleij * them. 4336a537d48SLinus Walleij */ 4346a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, 4356a537d48SLinus Walleij enum of_gpio_flags *of_flags) 4366a537d48SLinus Walleij { 4376a537d48SLinus Walleij /* These are the connection IDs we accept as legacy GPIO phandles */ 4386a537d48SLinus Walleij const char *whitelist[] = { 4396a537d48SLinus Walleij "wlf,ldoena", /* Arizona */ 4406a537d48SLinus Walleij "wlf,ldo1ena", /* WM8994 */ 4416a537d48SLinus Walleij "wlf,ldo2ena", /* WM8994 */ 4426a537d48SLinus Walleij }; 4438990899dSKrzysztof Kozlowski const struct device_node *np = dev->of_node; 4446a537d48SLinus Walleij struct gpio_desc *desc; 4456a537d48SLinus Walleij int i; 4466a537d48SLinus Walleij 4476a537d48SLinus Walleij if (!IS_ENABLED(CONFIG_REGULATOR)) 4486a537d48SLinus Walleij return ERR_PTR(-ENOENT); 4496a537d48SLinus Walleij 4506a537d48SLinus Walleij if (!con_id) 4516a537d48SLinus Walleij return ERR_PTR(-ENOENT); 4526a537d48SLinus Walleij 4534b21f94aSAndy Shevchenko i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id); 4544b21f94aSAndy Shevchenko if (i < 0) 4556a537d48SLinus Walleij return ERR_PTR(-ENOENT); 4566a537d48SLinus Walleij 4576a537d48SLinus Walleij desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags); 4586a537d48SLinus Walleij return desc; 4596a537d48SLinus Walleij } 4606a537d48SLinus Walleij 46111c43bb0SDmitry Torokhov static struct gpio_desc *of_find_arizona_gpio(struct device *dev, 46211c43bb0SDmitry Torokhov const char *con_id, 46311c43bb0SDmitry Torokhov enum of_gpio_flags *of_flags) 46411c43bb0SDmitry Torokhov { 46511c43bb0SDmitry Torokhov if (!IS_ENABLED(CONFIG_MFD_ARIZONA)) 46611c43bb0SDmitry Torokhov return ERR_PTR(-ENOENT); 46711c43bb0SDmitry Torokhov 46811c43bb0SDmitry Torokhov if (!con_id || strcmp(con_id, "wlf,reset")) 46911c43bb0SDmitry Torokhov return ERR_PTR(-ENOENT); 47011c43bb0SDmitry Torokhov 47111c43bb0SDmitry Torokhov return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); 47211c43bb0SDmitry Torokhov } 47311c43bb0SDmitry Torokhov 4746e24826dSLinus Walleij static struct gpio_desc *of_find_usb_gpio(struct device *dev, 4756e24826dSLinus Walleij const char *con_id, 4766e24826dSLinus Walleij enum of_gpio_flags *of_flags) 4776e24826dSLinus Walleij { 4786e24826dSLinus Walleij /* 4796e24826dSLinus Walleij * Currently this USB quirk is only for the Fairchild FUSB302 host which is using 4806e24826dSLinus Walleij * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios" 4816e24826dSLinus Walleij * suffix. 4826e24826dSLinus Walleij */ 4836e24826dSLinus Walleij if (!IS_ENABLED(CONFIG_TYPEC_FUSB302)) 4846e24826dSLinus Walleij return ERR_PTR(-ENOENT); 4856e24826dSLinus Walleij 4866e24826dSLinus Walleij if (!con_id || strcmp(con_id, "fcs,int_n")) 4876e24826dSLinus Walleij return ERR_PTR(-ENOENT); 4886e24826dSLinus Walleij 4896e24826dSLinus Walleij return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); 4906e24826dSLinus Walleij } 4916e24826dSLinus Walleij 492ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 493fed7026aSAndy Shevchenko unsigned int idx, unsigned long *flags) 494ea713bc4SLinus Walleij { 495ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 496ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 497ea713bc4SLinus Walleij struct gpio_desc *desc; 498ea713bc4SLinus Walleij unsigned int i; 499ea713bc4SLinus Walleij 500c8582339SLinus Walleij /* Try GPIO property "foo-gpios" and "foo-gpio" */ 501ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 502ea713bc4SLinus Walleij if (con_id) 503ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 504ea713bc4SLinus Walleij gpio_suffixes[i]); 505ea713bc4SLinus Walleij else 506ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 507ea713bc4SLinus Walleij gpio_suffixes[i]); 508ea713bc4SLinus Walleij 509ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 510ea713bc4SLinus Walleij &of_flags); 5116662ae6aSMaxime Ripard 5127b58696dSAndy Shevchenko if (!gpiod_not_found(desc)) 513ea713bc4SLinus Walleij break; 514ea713bc4SLinus Walleij } 515ea713bc4SLinus Walleij 5167b58696dSAndy Shevchenko if (gpiod_not_found(desc)) { 517c8582339SLinus Walleij /* Special handling for SPI GPIOs if used */ 518c8582339SLinus Walleij desc = of_find_spi_gpio(dev, con_id, &of_flags); 5191dea33e8SDmitry Torokhov } 5201dea33e8SDmitry Torokhov 5217b58696dSAndy Shevchenko if (gpiod_not_found(desc)) { 522e3023bf8SLinus Walleij /* This quirk looks up flags and all */ 523e3023bf8SLinus Walleij desc = of_find_spi_cs_gpio(dev, con_id, idx, flags); 524e3023bf8SLinus Walleij if (!IS_ERR(desc)) 525e3023bf8SLinus Walleij return desc; 526e3023bf8SLinus Walleij } 527c8582339SLinus Walleij 5287b58696dSAndy Shevchenko if (gpiod_not_found(desc)) { 5296a537d48SLinus Walleij /* Special handling for regulator GPIOs if used */ 5306a537d48SLinus Walleij desc = of_find_regulator_gpio(dev, con_id, &of_flags); 5311dea33e8SDmitry Torokhov } 5326a537d48SLinus Walleij 5337b58696dSAndy Shevchenko if (gpiod_not_found(desc)) 53411c43bb0SDmitry Torokhov desc = of_find_arizona_gpio(dev, con_id, &of_flags); 53511c43bb0SDmitry Torokhov 5367b58696dSAndy Shevchenko if (gpiod_not_found(desc)) 5376e24826dSLinus Walleij desc = of_find_usb_gpio(dev, con_id, &of_flags); 5386e24826dSLinus Walleij 539ea713bc4SLinus Walleij if (IS_ERR(desc)) 540ea713bc4SLinus Walleij return desc; 541ea713bc4SLinus Walleij 542ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 543ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 544ea713bc4SLinus Walleij 545ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 5464c0facddSLaxman Dewangan if (of_flags & OF_GPIO_OPEN_DRAIN) 547ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 548ea713bc4SLinus Walleij else 549ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 550ea713bc4SLinus Walleij } 551ea713bc4SLinus Walleij 552e10f72bfSAndrew Jeffery if (of_flags & OF_GPIO_TRANSITORY) 553e10f72bfSAndrew Jeffery *flags |= GPIO_TRANSITORY; 55405f479bfSCharles Keepax 555d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_UP) 556d449991cSThomas Petazzoni *flags |= GPIO_PULL_UP; 557d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_DOWN) 558d449991cSThomas Petazzoni *flags |= GPIO_PULL_DOWN; 559d449991cSThomas Petazzoni 560ea713bc4SLinus Walleij return desc; 561ea713bc4SLinus Walleij } 562ea713bc4SLinus Walleij 563f141ed65SGrant Likely /** 564fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 565f625d460SBenoit Parrot * @np: device node to get GPIO from 566be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 567a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 568f625d460SBenoit Parrot * @name: GPIO line name 569fed7026aSAndy Shevchenko * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 570fed7026aSAndy Shevchenko * of_find_gpio() or of_parse_own_gpio() 571f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 572f625d460SBenoit Parrot * 573f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 574f625d460SBenoit Parrot * value on the error condition. 575f625d460SBenoit Parrot */ 576fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 577be715343SMasahiro Yamada struct gpio_chip *chip, 578a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 579fed7026aSAndy Shevchenko unsigned long *lflags, 580f625d460SBenoit Parrot enum gpiod_flags *dflags) 581f625d460SBenoit Parrot { 582f625d460SBenoit Parrot struct device_node *chip_np; 583f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 584be715343SMasahiro Yamada struct of_phandle_args gpiospec; 585be715343SMasahiro Yamada struct gpio_desc *desc; 586a79fead5SGeert Uytterhoeven unsigned int i; 587f625d460SBenoit Parrot u32 tmp; 5883f9547e1SMasahiro Yamada int ret; 589f625d460SBenoit Parrot 590be715343SMasahiro Yamada chip_np = chip->of_node; 591f625d460SBenoit Parrot if (!chip_np) 592f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 593f625d460SBenoit Parrot 594f625d460SBenoit Parrot xlate_flags = 0; 5952d6c06f5SAndy Shevchenko *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 59640941954SAndy Shevchenko *dflags = GPIOD_ASIS; 597f625d460SBenoit Parrot 598f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 599f625d460SBenoit Parrot if (ret) 600f625d460SBenoit Parrot return ERR_PTR(ret); 601f625d460SBenoit Parrot 602be715343SMasahiro Yamada gpiospec.np = chip_np; 603be715343SMasahiro Yamada gpiospec.args_count = tmp; 604f625d460SBenoit Parrot 605a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 606a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 607a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 608f625d460SBenoit Parrot if (ret) 609f625d460SBenoit Parrot return ERR_PTR(ret); 610a79fead5SGeert Uytterhoeven } 611f625d460SBenoit Parrot 61299468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 613be715343SMasahiro Yamada if (IS_ERR(desc)) 614be715343SMasahiro Yamada return desc; 615f625d460SBenoit Parrot 616f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 617f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 618e10f72bfSAndrew Jeffery if (xlate_flags & OF_GPIO_TRANSITORY) 619e10f72bfSAndrew Jeffery *lflags |= GPIO_TRANSITORY; 620ea06a482SAdam Ford if (xlate_flags & OF_GPIO_PULL_UP) 621ea06a482SAdam Ford *lflags |= GPIO_PULL_UP; 622ea06a482SAdam Ford if (xlate_flags & OF_GPIO_PULL_DOWN) 623ea06a482SAdam Ford *lflags |= GPIO_PULL_DOWN; 624f625d460SBenoit Parrot 625f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 626f625d460SBenoit Parrot *dflags |= GPIOD_IN; 627f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 628f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 629f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 630f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 631f625d460SBenoit Parrot else { 63262cdcb6cSRob Herring pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 63362cdcb6cSRob Herring desc_to_gpio(desc), np); 634f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 635f625d460SBenoit Parrot } 636f625d460SBenoit Parrot 637f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 638f625d460SBenoit Parrot *name = np->name; 639f625d460SBenoit Parrot 640be715343SMasahiro Yamada return desc; 641f625d460SBenoit Parrot } 642f625d460SBenoit Parrot 643f625d460SBenoit Parrot /** 644bc21077eSGeert Uytterhoeven * of_gpiochip_add_hog - Add all hogs in a hog device node 645bc21077eSGeert Uytterhoeven * @chip: gpio chip to act on 646bc21077eSGeert Uytterhoeven * @hog: device node describing the hogs 647bc21077eSGeert Uytterhoeven * 648bc21077eSGeert Uytterhoeven * Returns error if it fails otherwise 0 on success. 649bc21077eSGeert Uytterhoeven */ 650bc21077eSGeert Uytterhoeven static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) 651bc21077eSGeert Uytterhoeven { 652bc21077eSGeert Uytterhoeven enum gpiod_flags dflags; 653bc21077eSGeert Uytterhoeven struct gpio_desc *desc; 654bc21077eSGeert Uytterhoeven unsigned long lflags; 655bc21077eSGeert Uytterhoeven const char *name; 656bc21077eSGeert Uytterhoeven unsigned int i; 657bc21077eSGeert Uytterhoeven int ret; 658bc21077eSGeert Uytterhoeven 659bc21077eSGeert Uytterhoeven for (i = 0;; i++) { 660bc21077eSGeert Uytterhoeven desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); 661bc21077eSGeert Uytterhoeven if (IS_ERR(desc)) 662bc21077eSGeert Uytterhoeven break; 663bc21077eSGeert Uytterhoeven 664bc21077eSGeert Uytterhoeven ret = gpiod_hog(desc, name, lflags, dflags); 665bc21077eSGeert Uytterhoeven if (ret < 0) 666bc21077eSGeert Uytterhoeven return ret; 66763636d95SGeert Uytterhoeven 66863636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC 66963636d95SGeert Uytterhoeven desc->hog = hog; 67063636d95SGeert Uytterhoeven #endif 671bc21077eSGeert Uytterhoeven } 672bc21077eSGeert Uytterhoeven 673bc21077eSGeert Uytterhoeven return 0; 674bc21077eSGeert Uytterhoeven } 675bc21077eSGeert Uytterhoeven 676bc21077eSGeert Uytterhoeven /** 677fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 678f625d460SBenoit Parrot * @chip: gpio chip to act on 679f625d460SBenoit Parrot * 680f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 681f625d460SBenoit Parrot * configuration. 682ead066e6SGeert Uytterhoeven * It returns error if it fails otherwise 0 on success. 683f625d460SBenoit Parrot */ 684dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 685f625d460SBenoit Parrot { 686f625d460SBenoit Parrot struct device_node *np; 687dfbd379bSLaxman Dewangan int ret; 688f625d460SBenoit Parrot 689d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 690f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 691f625d460SBenoit Parrot continue; 692f625d460SBenoit Parrot 693bc21077eSGeert Uytterhoeven ret = of_gpiochip_add_hog(chip, np); 69409e258afSWei Yongjun if (ret < 0) { 69509e258afSWei Yongjun of_node_put(np); 696dfbd379bSLaxman Dewangan return ret; 697f625d460SBenoit Parrot } 69863636d95SGeert Uytterhoeven 69963636d95SGeert Uytterhoeven of_node_set_flag(np, OF_POPULATED); 70009e258afSWei Yongjun } 701dfbd379bSLaxman Dewangan 702dfbd379bSLaxman Dewangan return 0; 703f625d460SBenoit Parrot } 704f625d460SBenoit Parrot 70563636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC 70663636d95SGeert Uytterhoeven /** 70763636d95SGeert Uytterhoeven * of_gpiochip_remove_hog - Remove all hogs in a hog device node 70863636d95SGeert Uytterhoeven * @chip: gpio chip to act on 70963636d95SGeert Uytterhoeven * @hog: device node describing the hogs 71063636d95SGeert Uytterhoeven */ 71163636d95SGeert Uytterhoeven static void of_gpiochip_remove_hog(struct gpio_chip *chip, 71263636d95SGeert Uytterhoeven struct device_node *hog) 71363636d95SGeert Uytterhoeven { 71480c78fbeSAndy Shevchenko struct gpio_desc *desc; 71563636d95SGeert Uytterhoeven unsigned int i; 71663636d95SGeert Uytterhoeven 71780c78fbeSAndy Shevchenko for_each_gpio_desc_with_flag(i, chip, desc, FLAG_IS_HOGGED) 71880c78fbeSAndy Shevchenko if (desc->hog == hog) 71980c78fbeSAndy Shevchenko gpiochip_free_own_desc(desc); 72063636d95SGeert Uytterhoeven } 72163636d95SGeert Uytterhoeven 72263636d95SGeert Uytterhoeven static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) 72363636d95SGeert Uytterhoeven { 72463636d95SGeert Uytterhoeven return chip->gpiodev->dev.of_node == data; 72563636d95SGeert Uytterhoeven } 72663636d95SGeert Uytterhoeven 72763636d95SGeert Uytterhoeven static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) 72863636d95SGeert Uytterhoeven { 72963636d95SGeert Uytterhoeven return gpiochip_find(np, of_gpiochip_match_node); 73063636d95SGeert Uytterhoeven } 73163636d95SGeert Uytterhoeven 73263636d95SGeert Uytterhoeven static int of_gpio_notify(struct notifier_block *nb, unsigned long action, 73363636d95SGeert Uytterhoeven void *arg) 73463636d95SGeert Uytterhoeven { 73563636d95SGeert Uytterhoeven struct of_reconfig_data *rd = arg; 73663636d95SGeert Uytterhoeven struct gpio_chip *chip; 73763636d95SGeert Uytterhoeven int ret; 73863636d95SGeert Uytterhoeven 73963636d95SGeert Uytterhoeven /* 74063636d95SGeert Uytterhoeven * This only supports adding and removing complete gpio-hog nodes. 74163636d95SGeert Uytterhoeven * Modifying an existing gpio-hog node is not supported (except for 74263636d95SGeert Uytterhoeven * changing its "status" property, which is treated the same as 74363636d95SGeert Uytterhoeven * addition/removal). 74463636d95SGeert Uytterhoeven */ 74563636d95SGeert Uytterhoeven switch (of_reconfig_get_state_change(action, arg)) { 74663636d95SGeert Uytterhoeven case OF_RECONFIG_CHANGE_ADD: 74763636d95SGeert Uytterhoeven if (!of_property_read_bool(rd->dn, "gpio-hog")) 74863636d95SGeert Uytterhoeven return NOTIFY_OK; /* not for us */ 74963636d95SGeert Uytterhoeven 75063636d95SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) 75163636d95SGeert Uytterhoeven return NOTIFY_OK; 75263636d95SGeert Uytterhoeven 75363636d95SGeert Uytterhoeven chip = of_find_gpiochip_by_node(rd->dn->parent); 75463636d95SGeert Uytterhoeven if (chip == NULL) 75563636d95SGeert Uytterhoeven return NOTIFY_OK; /* not for us */ 75663636d95SGeert Uytterhoeven 75763636d95SGeert Uytterhoeven ret = of_gpiochip_add_hog(chip, rd->dn); 75863636d95SGeert Uytterhoeven if (ret < 0) { 75963636d95SGeert Uytterhoeven pr_err("%s: failed to add hogs for %pOF\n", __func__, 76063636d95SGeert Uytterhoeven rd->dn); 76163636d95SGeert Uytterhoeven of_node_clear_flag(rd->dn, OF_POPULATED); 76263636d95SGeert Uytterhoeven return notifier_from_errno(ret); 76363636d95SGeert Uytterhoeven } 76463636d95SGeert Uytterhoeven break; 76563636d95SGeert Uytterhoeven 76663636d95SGeert Uytterhoeven case OF_RECONFIG_CHANGE_REMOVE: 76763636d95SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 76863636d95SGeert Uytterhoeven return NOTIFY_OK; /* already depopulated */ 76963636d95SGeert Uytterhoeven 77063636d95SGeert Uytterhoeven chip = of_find_gpiochip_by_node(rd->dn->parent); 77163636d95SGeert Uytterhoeven if (chip == NULL) 77263636d95SGeert Uytterhoeven return NOTIFY_OK; /* not for us */ 77363636d95SGeert Uytterhoeven 77463636d95SGeert Uytterhoeven of_gpiochip_remove_hog(chip, rd->dn); 77563636d95SGeert Uytterhoeven of_node_clear_flag(rd->dn, OF_POPULATED); 77663636d95SGeert Uytterhoeven break; 77763636d95SGeert Uytterhoeven } 77863636d95SGeert Uytterhoeven 77963636d95SGeert Uytterhoeven return NOTIFY_OK; 78063636d95SGeert Uytterhoeven } 78163636d95SGeert Uytterhoeven 78263636d95SGeert Uytterhoeven struct notifier_block gpio_of_notifier = { 78363636d95SGeert Uytterhoeven .notifier_call = of_gpio_notify, 78463636d95SGeert Uytterhoeven }; 78563636d95SGeert Uytterhoeven #endif /* CONFIG_OF_DYNAMIC */ 78663636d95SGeert Uytterhoeven 787f625d460SBenoit Parrot /** 78867049c50SThierry Reding * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags 789f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 79067049c50SThierry Reding * @gpiospec: GPIO specifier as found in the device tree 791f141ed65SGrant Likely * @flags: a flags pointer to fill in 792f141ed65SGrant Likely * 793f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 79467049c50SThierry Reding * GPIO chips. This function performs only one sanity check: whether GPIO 795f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 796f141ed65SGrant Likely */ 797b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc, 798b0c7e73bSGeert Uytterhoeven const struct of_phandle_args *gpiospec, 799b0c7e73bSGeert Uytterhoeven u32 *flags) 800f141ed65SGrant Likely { 801f141ed65SGrant Likely /* 802f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 80320a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 804f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 805f141ed65SGrant Likely * but not recommended). 806f141ed65SGrant Likely */ 807f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 808f141ed65SGrant Likely WARN_ON(1); 809f141ed65SGrant Likely return -EINVAL; 810f141ed65SGrant Likely } 811f141ed65SGrant Likely 812f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 813f141ed65SGrant Likely return -EINVAL; 814f141ed65SGrant Likely 8157b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 816f141ed65SGrant Likely return -EINVAL; 817f141ed65SGrant Likely 818f141ed65SGrant Likely if (flags) 819f141ed65SGrant Likely *flags = gpiospec->args[1]; 820f141ed65SGrant Likely 821f141ed65SGrant Likely return gpiospec->args[0]; 822f141ed65SGrant Likely } 823f141ed65SGrant Likely 824f141ed65SGrant Likely /** 8253208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 826f141ed65SGrant Likely * @np: device node of the GPIO chip 827f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 8283208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 829f141ed65SGrant Likely * 830f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 831f141ed65SGrant Likely * 832f141ed65SGrant Likely * 1) In the gpio_chip structure: 833f141ed65SGrant Likely * - all the callbacks 834f141ed65SGrant Likely * - of_gpio_n_cells 835f141ed65SGrant Likely * - of_xlate callback (optional) 836f141ed65SGrant Likely * 837f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 838f141ed65SGrant Likely * - save_regs callback (optional) 839f141ed65SGrant Likely * 840f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 841f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 842f141ed65SGrant Likely * to manage GPIOs from the callbacks. 843f141ed65SGrant Likely */ 8443208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 8453208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 8463208b0f0SLinus Walleij void *data) 847f141ed65SGrant Likely { 848f141ed65SGrant Likely int ret = -ENOMEM; 849f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 850f141ed65SGrant Likely 8517eb6ce2fSRob Herring gc->label = kasprintf(GFP_KERNEL, "%pOF", np); 852f141ed65SGrant Likely if (!gc->label) 853f141ed65SGrant Likely goto err0; 854f141ed65SGrant Likely 855f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 856f141ed65SGrant Likely if (!mm_gc->regs) 857f141ed65SGrant Likely goto err1; 858f141ed65SGrant Likely 859f141ed65SGrant Likely gc->base = -1; 860f141ed65SGrant Likely 861f141ed65SGrant Likely if (mm_gc->save_regs) 862f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 863f141ed65SGrant Likely 864f141ed65SGrant Likely mm_gc->gc.of_node = np; 865f141ed65SGrant Likely 8663208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 867f141ed65SGrant Likely if (ret) 868f141ed65SGrant Likely goto err2; 869f141ed65SGrant Likely 870f141ed65SGrant Likely return 0; 871f141ed65SGrant Likely err2: 872f141ed65SGrant Likely iounmap(mm_gc->regs); 873f141ed65SGrant Likely err1: 874f141ed65SGrant Likely kfree(gc->label); 875f141ed65SGrant Likely err0: 8767eb6ce2fSRob Herring pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); 877f141ed65SGrant Likely return ret; 878f141ed65SGrant Likely } 8796d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); 880f141ed65SGrant Likely 881d621e8baSRicardo Ribalda Delgado /** 882d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 883d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 884d621e8baSRicardo Ribalda Delgado */ 885d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 886d621e8baSRicardo Ribalda Delgado { 887d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 888d621e8baSRicardo Ribalda Delgado 889d621e8baSRicardo Ribalda Delgado if (!mm_gc) 890d621e8baSRicardo Ribalda Delgado return; 891d621e8baSRicardo Ribalda Delgado 892d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 893d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 894d621e8baSRicardo Ribalda Delgado kfree(gc->label); 895d621e8baSRicardo Ribalda Delgado } 8966d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); 897d621e8baSRicardo Ribalda Delgado 898726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) 899726cb3baSStephen Boyd { 900726cb3baSStephen Boyd int len, i; 901726cb3baSStephen Boyd u32 start, count; 902726cb3baSStephen Boyd struct device_node *np = chip->of_node; 903726cb3baSStephen Boyd 904726cb3baSStephen Boyd len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 905726cb3baSStephen Boyd if (len < 0 || len % 2 != 0) 906726cb3baSStephen Boyd return; 907726cb3baSStephen Boyd 908726cb3baSStephen Boyd for (i = 0; i < len; i += 2) { 909726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 910726cb3baSStephen Boyd i, &start); 911726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 912726cb3baSStephen Boyd i + 1, &count); 913726cb3baSStephen Boyd if (start >= chip->ngpio || start + count >= chip->ngpio) 914726cb3baSStephen Boyd continue; 915726cb3baSStephen Boyd 916726cb3baSStephen Boyd bitmap_clear(chip->valid_mask, start, count); 917726cb3baSStephen Boyd } 918726cb3baSStephen Boyd }; 919726cb3baSStephen Boyd 920f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 92128355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 922f23f1516SShiraz Hashim { 923f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 924f23f1516SShiraz Hashim struct of_phandle_args pinspec; 9251e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 926f23f1516SShiraz Hashim int index = 0, ret; 927586a87e6SChristian Ruppert const char *name; 928586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 929586a87e6SChristian Ruppert struct property *group_names; 930f23f1516SShiraz Hashim 931f23f1516SShiraz Hashim if (!np) 93228355f81STomeu Vizoso return 0; 933f23f1516SShiraz Hashim 934*3550bba2SStefan Wahren if (!of_property_read_bool(np, "gpio-ranges") && 935*3550bba2SStefan Wahren chip->of_gpio_ranges_fallback) { 936*3550bba2SStefan Wahren return chip->of_gpio_ranges_fallback(chip, np); 937*3550bba2SStefan Wahren } 938*3550bba2SStefan Wahren 939586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 940586a87e6SChristian Ruppert 941ad4e1a7cSHaojian Zhuang for (;; index++) { 942d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 943d9fe0039SStephen Warren index, &pinspec); 944f23f1516SShiraz Hashim if (ret) 945f23f1516SShiraz Hashim break; 946f23f1516SShiraz Hashim 9471e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 948602cf638SMasahiro Yamada of_node_put(pinspec.np); 9491e63d7b9SLinus Walleij if (!pctldev) 95028355f81STomeu Vizoso return -EPROBE_DEFER; 951f23f1516SShiraz Hashim 952586a87e6SChristian Ruppert if (pinspec.args[2]) { 953586a87e6SChristian Ruppert if (group_names) { 95472858602SLaurent Navet of_property_read_string_index(np, 955586a87e6SChristian Ruppert group_names_propname, 956586a87e6SChristian Ruppert index, &name); 957586a87e6SChristian Ruppert if (strlen(name)) { 9587eb6ce2fSRob Herring pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 9597eb6ce2fSRob Herring np); 960586a87e6SChristian Ruppert break; 961586a87e6SChristian Ruppert } 962586a87e6SChristian Ruppert } 963586a87e6SChristian Ruppert /* npins != 0: linear range */ 9641e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 965ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 9661e63d7b9SLinus Walleij pinspec.args[0], 96786853c83SHaojian Zhuang pinspec.args[1], 96886853c83SHaojian Zhuang pinspec.args[2]); 9691e63d7b9SLinus Walleij if (ret) 97028355f81STomeu Vizoso return ret; 971586a87e6SChristian Ruppert } else { 972586a87e6SChristian Ruppert /* npins == 0: special range */ 973586a87e6SChristian Ruppert if (pinspec.args[1]) { 9747eb6ce2fSRob Herring pr_err("%pOF: Illegal gpio-range format.\n", 9757eb6ce2fSRob Herring np); 976586a87e6SChristian Ruppert break; 977586a87e6SChristian Ruppert } 978586a87e6SChristian Ruppert 979586a87e6SChristian Ruppert if (!group_names) { 9807eb6ce2fSRob Herring pr_err("%pOF: GPIO group range requested but no %s property.\n", 9817eb6ce2fSRob Herring np, group_names_propname); 982586a87e6SChristian Ruppert break; 983586a87e6SChristian Ruppert } 984586a87e6SChristian Ruppert 985586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 986586a87e6SChristian Ruppert group_names_propname, 987586a87e6SChristian Ruppert index, &name); 988586a87e6SChristian Ruppert if (ret) 989586a87e6SChristian Ruppert break; 990586a87e6SChristian Ruppert 991586a87e6SChristian Ruppert if (!strlen(name)) { 9927eb6ce2fSRob Herring pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 9937eb6ce2fSRob Herring np); 994586a87e6SChristian Ruppert break; 995586a87e6SChristian Ruppert } 996586a87e6SChristian Ruppert 997586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 998586a87e6SChristian Ruppert pinspec.args[0], name); 999586a87e6SChristian Ruppert if (ret) 100028355f81STomeu Vizoso return ret; 1001586a87e6SChristian Ruppert } 1002ad4e1a7cSHaojian Zhuang } 100328355f81STomeu Vizoso 100428355f81STomeu Vizoso return 0; 1005f23f1516SShiraz Hashim } 1006f23f1516SShiraz Hashim 1007f23f1516SShiraz Hashim #else 100828355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 1009f23f1516SShiraz Hashim #endif 1010f23f1516SShiraz Hashim 101128355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 1012f141ed65SGrant Likely { 1013f0d1ab05SLinus Walleij int ret; 101428355f81STomeu Vizoso 1015f141ed65SGrant Likely if (!chip->of_node) 101628355f81STomeu Vizoso return 0; 1017f141ed65SGrant Likely 1018f141ed65SGrant Likely if (!chip->of_xlate) { 1019f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 1020f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 1021f141ed65SGrant Likely } 1022f141ed65SGrant Likely 10231020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 10241020dfd1SMasahiro Yamada return -EINVAL; 10251020dfd1SMasahiro Yamada 1026726cb3baSStephen Boyd of_gpiochip_init_valid_mask(chip); 1027726cb3baSStephen Boyd 1028f0d1ab05SLinus Walleij ret = of_gpiochip_add_pin_range(chip); 1029f0d1ab05SLinus Walleij if (ret) 1030f0d1ab05SLinus Walleij return ret; 103128355f81STomeu Vizoso 1032f141ed65SGrant Likely of_node_get(chip->of_node); 1033f625d460SBenoit Parrot 1034f0d1ab05SLinus Walleij ret = of_gpiochip_scan_gpios(chip); 10352f4133bbSAndy Shevchenko if (ret) 1036f7299d44SGeert Uytterhoeven of_node_put(chip->of_node); 1037f7299d44SGeert Uytterhoeven 1038f0d1ab05SLinus Walleij return ret; 1039f141ed65SGrant Likely } 1040f141ed65SGrant Likely 1041f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 1042f141ed65SGrant Likely { 1043f141ed65SGrant Likely of_node_put(chip->of_node); 1044f141ed65SGrant Likely } 10454731210cSSaravana Kannan 10464731210cSSaravana Kannan void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) 10474731210cSSaravana Kannan { 10481df62542SAndy Shevchenko /* Set default OF node to parent's one if present */ 10491df62542SAndy Shevchenko if (gc->parent) 10501df62542SAndy Shevchenko gdev->dev.of_node = gc->parent->of_node; 10511df62542SAndy Shevchenko 1052ac627260SBartosz Golaszewski if (gc->fwnode) 1053ac627260SBartosz Golaszewski gc->of_node = to_of_node(gc->fwnode); 1054ac627260SBartosz Golaszewski 10554731210cSSaravana Kannan /* If the gpiochip has an assigned OF node this takes precedence */ 10564731210cSSaravana Kannan if (gc->of_node) 10574731210cSSaravana Kannan gdev->dev.of_node = gc->of_node; 10584731210cSSaravana Kannan else 10594731210cSSaravana Kannan gc->of_node = gdev->dev.of_node; 10604731210cSSaravana Kannan } 1061