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" 24af8b6375SAlexandre Courbot 25c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) 263d0f7cf0SGrant Likely { 27c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec = data; 28c7e9d398SMasahiro Yamada 29c7e9d398SMasahiro Yamada return chip->gpiodev->dev.of_node == gpiospec->np && 30d49b48f0SVincent Whitchurch chip->of_xlate && 31c7e9d398SMasahiro Yamada chip->of_xlate(chip, gpiospec, NULL) >= 0; 327b8792bbSHans Holmberg } 333d0f7cf0SGrant Likely 34c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate( 35c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec) 36762c2e46SMasahiro Yamada { 37c7e9d398SMasahiro Yamada return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); 383d0f7cf0SGrant Likely } 393d0f7cf0SGrant Likely 4099468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, 4199468c1aSMasahiro Yamada struct of_phandle_args *gpiospec, 4299468c1aSMasahiro Yamada enum of_gpio_flags *flags) 4399468c1aSMasahiro Yamada { 4499468c1aSMasahiro Yamada int ret; 4599468c1aSMasahiro Yamada 4699468c1aSMasahiro Yamada if (chip->of_gpio_n_cells != gpiospec->args_count) 4799468c1aSMasahiro Yamada return ERR_PTR(-EINVAL); 4899468c1aSMasahiro Yamada 4999468c1aSMasahiro Yamada ret = chip->of_xlate(chip, gpiospec, flags); 5099468c1aSMasahiro Yamada if (ret < 0) 5199468c1aSMasahiro Yamada return ERR_PTR(ret); 5299468c1aSMasahiro Yamada 5399468c1aSMasahiro Yamada return gpiochip_get_desc(chip, ret); 54f141ed65SGrant Likely } 55f141ed65SGrant Likely 56a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np, 5789a5e15bSLinus Walleij const char *propname, 586953c57aSLinus Walleij enum of_gpio_flags *flags, 596953c57aSLinus Walleij int index) 60a603a2b8SLinus Walleij { 61a603a2b8SLinus Walleij /* 6281c85ec1SLinus Walleij * Handle MMC "cd-inverted" and "wp-inverted" semantics. 6381c85ec1SLinus Walleij */ 6481c85ec1SLinus Walleij if (IS_ENABLED(CONFIG_MMC)) { 6581c85ec1SLinus Walleij /* 6681c85ec1SLinus Walleij * Active low is the default according to the 6789a5e15bSLinus Walleij * SDHCI specification and the device tree 6889a5e15bSLinus Walleij * bindings. However the code in the current 6989a5e15bSLinus Walleij * kernel was written such that the phandle 7089a5e15bSLinus Walleij * flags were always respected, and "cd-inverted" 7189a5e15bSLinus Walleij * would invert the flag from the device phandle. 7281c85ec1SLinus Walleij */ 7389a5e15bSLinus Walleij if (!strcmp(propname, "cd-gpios")) { 7489a5e15bSLinus Walleij if (of_property_read_bool(np, "cd-inverted")) 7589a5e15bSLinus Walleij *flags ^= OF_GPIO_ACTIVE_LOW; 7681c85ec1SLinus Walleij } 7789a5e15bSLinus Walleij if (!strcmp(propname, "wp-gpios")) { 7889a5e15bSLinus Walleij if (of_property_read_bool(np, "wp-inverted")) 7989a5e15bSLinus Walleij *flags ^= OF_GPIO_ACTIVE_LOW; 8081c85ec1SLinus Walleij } 8181c85ec1SLinus Walleij } 8281c85ec1SLinus Walleij /* 83a603a2b8SLinus Walleij * Some GPIO fixed regulator quirks. 84a603a2b8SLinus Walleij * Note that active low is the default. 85a603a2b8SLinus Walleij */ 86a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 87906402a4SLinus Walleij (of_device_is_compatible(np, "regulator-fixed") || 88906402a4SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") || 89a71a81e7SGeert Uytterhoeven (!(strcmp(propname, "enable-gpio") && 90a71a81e7SGeert Uytterhoeven strcmp(propname, "enable-gpios")) && 91a71a81e7SGeert Uytterhoeven of_device_is_compatible(np, "regulator-gpio")))) { 92a603a2b8SLinus Walleij /* 93a603a2b8SLinus Walleij * The regulator GPIO handles are specified such that the 94a603a2b8SLinus Walleij * presence or absence of "enable-active-high" solely controls 95a603a2b8SLinus Walleij * the polarity of the GPIO line. Any phandle flags must 96a603a2b8SLinus Walleij * be actively ignored. 97a603a2b8SLinus Walleij */ 98a603a2b8SLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 99a603a2b8SLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 100a603a2b8SLinus Walleij of_node_full_name(np)); 101a603a2b8SLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 102a603a2b8SLinus Walleij } 103a603a2b8SLinus Walleij if (!of_property_read_bool(np, "enable-active-high")) 104a603a2b8SLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 105a603a2b8SLinus Walleij } 106a603a2b8SLinus Walleij /* 107a603a2b8SLinus Walleij * Legacy open drain handling for fixed voltage regulators. 108a603a2b8SLinus Walleij */ 109a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 110a603a2b8SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") && 111a603a2b8SLinus Walleij of_property_read_bool(np, "gpio-open-drain")) { 112a603a2b8SLinus Walleij *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); 113a603a2b8SLinus Walleij pr_info("%s uses legacy open drain flag - update the DTS if you can\n", 114a603a2b8SLinus Walleij of_node_full_name(np)); 115a603a2b8SLinus Walleij } 1166953c57aSLinus Walleij 1176953c57aSLinus Walleij /* 1186953c57aSLinus Walleij * Legacy handling of SPI active high chip select. If we have a 1196953c57aSLinus Walleij * property named "cs-gpios" we need to inspect the child node 1206953c57aSLinus Walleij * to determine if the flags should have inverted semantics. 1216953c57aSLinus Walleij */ 122a71a81e7SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && 123a71a81e7SGeert Uytterhoeven of_property_read_bool(np, "cs-gpios")) { 1246953c57aSLinus Walleij struct device_node *child; 1256953c57aSLinus Walleij u32 cs; 1266953c57aSLinus Walleij int ret; 1276953c57aSLinus Walleij 1286953c57aSLinus Walleij for_each_child_of_node(np, child) { 1296953c57aSLinus Walleij ret = of_property_read_u32(child, "reg", &cs); 130c1c04ceaSLinus Walleij if (ret) 1316953c57aSLinus Walleij continue; 1326953c57aSLinus Walleij if (cs == index) { 1336953c57aSLinus Walleij /* 1346953c57aSLinus Walleij * SPI children have active low chip selects 1356953c57aSLinus Walleij * by default. This can be specified negatively 1366953c57aSLinus Walleij * by just omitting "spi-cs-high" in the 1376953c57aSLinus Walleij * device node, or actively by tagging on 1386953c57aSLinus Walleij * GPIO_ACTIVE_LOW as flag in the device 1396953c57aSLinus Walleij * tree. If the line is simultaneously 1406953c57aSLinus Walleij * tagged as active low in the device tree 1416953c57aSLinus Walleij * and has the "spi-cs-high" set, we get a 1426953c57aSLinus Walleij * conflict and the "spi-cs-high" flag will 1436953c57aSLinus Walleij * take precedence. 1446953c57aSLinus Walleij */ 1457ce40277SAndrey Smirnov if (of_property_read_bool(child, "spi-cs-high")) { 1466953c57aSLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 1476953c57aSLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 1487ce40277SAndrey Smirnov of_node_full_name(child)); 1496953c57aSLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 1506953c57aSLinus Walleij } 1516953c57aSLinus Walleij } else { 1526953c57aSLinus Walleij if (!(*flags & OF_GPIO_ACTIVE_LOW)) 1536953c57aSLinus Walleij pr_info("%s enforce active low on chipselect handle\n", 1547ce40277SAndrey Smirnov of_node_full_name(child)); 1556953c57aSLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 1566953c57aSLinus Walleij } 1576953c57aSLinus Walleij break; 1586953c57aSLinus Walleij } 1596953c57aSLinus Walleij } 1606953c57aSLinus Walleij } 161a603a2b8SLinus Walleij } 162a603a2b8SLinus Walleij 163f141ed65SGrant Likely /** 164af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 165f141ed65SGrant Likely * @np: device node to get GPIO from 166f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 167f141ed65SGrant Likely * @index: index of the GPIO 168f141ed65SGrant Likely * @flags: a flags pointer to fill in 169f141ed65SGrant Likely * 170af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 171f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 172f141ed65SGrant Likely * in flags for the GPIO. 173f141ed65SGrant Likely */ 174af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 175af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 176f141ed65SGrant Likely { 177762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 178762c2e46SMasahiro Yamada struct gpio_chip *chip; 179762c2e46SMasahiro Yamada struct gpio_desc *desc; 180f141ed65SGrant Likely int ret; 181f141ed65SGrant Likely 182c11e6f0fSStephen Boyd ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 183762c2e46SMasahiro Yamada &gpiospec); 1843d0f7cf0SGrant Likely if (ret) { 1857eb6ce2fSRob Herring pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 1867eb6ce2fSRob Herring __func__, propname, np, index); 187af8b6375SAlexandre Courbot return ERR_PTR(ret); 1883d0f7cf0SGrant Likely } 189f141ed65SGrant Likely 190c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 191762c2e46SMasahiro Yamada if (!chip) { 192762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 193762c2e46SMasahiro Yamada goto out; 194762c2e46SMasahiro Yamada } 1953d0f7cf0SGrant Likely 19699468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 197762c2e46SMasahiro Yamada if (IS_ERR(desc)) 198762c2e46SMasahiro Yamada goto out; 199762c2e46SMasahiro Yamada 200605f2d34SLinus Walleij if (flags) 20189a5e15bSLinus Walleij of_gpio_flags_quirks(np, propname, flags, index); 202a603a2b8SLinus Walleij 2037eb6ce2fSRob Herring pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 2047eb6ce2fSRob Herring __func__, propname, np, index, 205762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 206762c2e46SMasahiro Yamada 207762c2e46SMasahiro Yamada out: 208762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 209762c2e46SMasahiro Yamada 210762c2e46SMasahiro Yamada return desc; 211f141ed65SGrant Likely } 212f141ed65SGrant Likely 213f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 214f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 215f01d9075SAlexandre Courbot { 216f01d9075SAlexandre Courbot struct gpio_desc *desc; 217f01d9075SAlexandre Courbot 218f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 219f01d9075SAlexandre Courbot 220f01d9075SAlexandre Courbot if (IS_ERR(desc)) 221f01d9075SAlexandre Courbot return PTR_ERR(desc); 222f01d9075SAlexandre Courbot else 223f01d9075SAlexandre Courbot return desc_to_gpio(desc); 224f01d9075SAlexandre Courbot } 225f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 226f01d9075SAlexandre Courbot 227c8582339SLinus Walleij /* 228c8582339SLinus Walleij * The SPI GPIO bindings happened before we managed to establish that GPIO 229c8582339SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 230c8582339SLinus Walleij * them. 231c8582339SLinus Walleij */ 232c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, 233c8582339SLinus Walleij enum of_gpio_flags *of_flags) 234c8582339SLinus Walleij { 235c8582339SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 236c8582339SLinus Walleij struct device_node *np = dev->of_node; 237c8582339SLinus Walleij struct gpio_desc *desc; 238c8582339SLinus Walleij 239c8582339SLinus Walleij /* 240c8582339SLinus Walleij * Hopefully the compiler stubs the rest of the function if this 241c8582339SLinus Walleij * is false. 242c8582339SLinus Walleij */ 243c8582339SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 244c8582339SLinus Walleij return ERR_PTR(-ENOENT); 245c8582339SLinus Walleij 246c8582339SLinus Walleij /* Allow this specifically for "spi-gpio" devices */ 247c8582339SLinus Walleij if (!of_device_is_compatible(np, "spi-gpio") || !con_id) 248c8582339SLinus Walleij return ERR_PTR(-ENOENT); 249c8582339SLinus Walleij 250c8582339SLinus Walleij /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ 251c8582339SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); 252c8582339SLinus Walleij 253c8582339SLinus Walleij desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); 254c8582339SLinus Walleij return desc; 255c8582339SLinus Walleij } 256c8582339SLinus Walleij 2576a537d48SLinus Walleij /* 2586a537d48SLinus Walleij * Some regulator bindings happened before we managed to establish that GPIO 2596a537d48SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 2606a537d48SLinus Walleij * them. 2616a537d48SLinus Walleij */ 2626a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, 2636a537d48SLinus Walleij enum of_gpio_flags *of_flags) 2646a537d48SLinus Walleij { 2656a537d48SLinus Walleij /* These are the connection IDs we accept as legacy GPIO phandles */ 2666a537d48SLinus Walleij const char *whitelist[] = { 2676a537d48SLinus Walleij "wlf,ldoena", /* Arizona */ 2686a537d48SLinus Walleij "wlf,ldo1ena", /* WM8994 */ 2696a537d48SLinus Walleij "wlf,ldo2ena", /* WM8994 */ 2706a537d48SLinus Walleij }; 2716a537d48SLinus Walleij struct device_node *np = dev->of_node; 2726a537d48SLinus Walleij struct gpio_desc *desc; 2736a537d48SLinus Walleij int i; 2746a537d48SLinus Walleij 2756a537d48SLinus Walleij if (!IS_ENABLED(CONFIG_REGULATOR)) 2766a537d48SLinus Walleij return ERR_PTR(-ENOENT); 2776a537d48SLinus Walleij 2786a537d48SLinus Walleij if (!con_id) 2796a537d48SLinus Walleij return ERR_PTR(-ENOENT); 2806a537d48SLinus Walleij 2814b21f94aSAndy Shevchenko i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id); 2824b21f94aSAndy Shevchenko if (i < 0) 2836a537d48SLinus Walleij return ERR_PTR(-ENOENT); 2846a537d48SLinus Walleij 2856a537d48SLinus Walleij desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags); 2866a537d48SLinus Walleij return desc; 2876a537d48SLinus Walleij } 2886a537d48SLinus Walleij 289ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 290fed7026aSAndy Shevchenko unsigned int idx, unsigned long *flags) 291ea713bc4SLinus Walleij { 292ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 293ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 294ea713bc4SLinus Walleij struct gpio_desc *desc; 295ea713bc4SLinus Walleij unsigned int i; 296ea713bc4SLinus Walleij 297c8582339SLinus Walleij /* Try GPIO property "foo-gpios" and "foo-gpio" */ 298ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 299ea713bc4SLinus Walleij if (con_id) 300ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 301ea713bc4SLinus Walleij gpio_suffixes[i]); 302ea713bc4SLinus Walleij else 303ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 304ea713bc4SLinus Walleij gpio_suffixes[i]); 305ea713bc4SLinus Walleij 306ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 307ea713bc4SLinus Walleij &of_flags); 3086662ae6aSMaxime Ripard /* 3096662ae6aSMaxime Ripard * -EPROBE_DEFER in our case means that we found a 3106662ae6aSMaxime Ripard * valid GPIO property, but no controller has been 3116662ae6aSMaxime Ripard * registered so far. 3126662ae6aSMaxime Ripard * 3136662ae6aSMaxime Ripard * This means we don't need to look any further for 3146662ae6aSMaxime Ripard * alternate name conventions, and we should really 3156662ae6aSMaxime Ripard * preserve the return code for our user to be able to 3166662ae6aSMaxime Ripard * retry probing later. 3176662ae6aSMaxime Ripard */ 3186662ae6aSMaxime Ripard if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER) 3196662ae6aSMaxime Ripard return desc; 3206662ae6aSMaxime Ripard 321ea713bc4SLinus Walleij if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) 322ea713bc4SLinus Walleij break; 323ea713bc4SLinus Walleij } 324ea713bc4SLinus Walleij 325c8582339SLinus Walleij /* Special handling for SPI GPIOs if used */ 326c8582339SLinus Walleij if (IS_ERR(desc)) 327c8582339SLinus Walleij desc = of_find_spi_gpio(dev, con_id, &of_flags); 328c8582339SLinus Walleij 3296a537d48SLinus Walleij /* Special handling for regulator GPIOs if used */ 330ce27fb2cSChen-Yu Tsai if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) 3316a537d48SLinus Walleij desc = of_find_regulator_gpio(dev, con_id, &of_flags); 3326a537d48SLinus Walleij 333ea713bc4SLinus Walleij if (IS_ERR(desc)) 334ea713bc4SLinus Walleij return desc; 335ea713bc4SLinus Walleij 336ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 337ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 338ea713bc4SLinus Walleij 339ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 3404c0facddSLaxman Dewangan if (of_flags & OF_GPIO_OPEN_DRAIN) 341ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 342ea713bc4SLinus Walleij else 343ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 344ea713bc4SLinus Walleij } 345ea713bc4SLinus Walleij 346e10f72bfSAndrew Jeffery if (of_flags & OF_GPIO_TRANSITORY) 347e10f72bfSAndrew Jeffery *flags |= GPIO_TRANSITORY; 34805f479bfSCharles Keepax 349d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_UP) 350d449991cSThomas Petazzoni *flags |= GPIO_PULL_UP; 351d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_DOWN) 352d449991cSThomas Petazzoni *flags |= GPIO_PULL_DOWN; 353d449991cSThomas Petazzoni 354ea713bc4SLinus Walleij return desc; 355ea713bc4SLinus Walleij } 356ea713bc4SLinus Walleij 357f141ed65SGrant Likely /** 358fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 359f625d460SBenoit Parrot * @np: device node to get GPIO from 360be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 361a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 362f625d460SBenoit Parrot * @name: GPIO line name 363fed7026aSAndy Shevchenko * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 364fed7026aSAndy Shevchenko * of_find_gpio() or of_parse_own_gpio() 365f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 366f625d460SBenoit Parrot * 367f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 368f625d460SBenoit Parrot * value on the error condition. 369f625d460SBenoit Parrot */ 370fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 371be715343SMasahiro Yamada struct gpio_chip *chip, 372a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 373fed7026aSAndy Shevchenko unsigned long *lflags, 374f625d460SBenoit Parrot enum gpiod_flags *dflags) 375f625d460SBenoit Parrot { 376f625d460SBenoit Parrot struct device_node *chip_np; 377f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 378be715343SMasahiro Yamada struct of_phandle_args gpiospec; 379be715343SMasahiro Yamada struct gpio_desc *desc; 380a79fead5SGeert Uytterhoeven unsigned int i; 381f625d460SBenoit Parrot u32 tmp; 3823f9547e1SMasahiro Yamada int ret; 383f625d460SBenoit Parrot 384be715343SMasahiro Yamada chip_np = chip->of_node; 385f625d460SBenoit Parrot if (!chip_np) 386f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 387f625d460SBenoit Parrot 388f625d460SBenoit Parrot xlate_flags = 0; 3892d6c06f5SAndy Shevchenko *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 390f625d460SBenoit Parrot *dflags = 0; 391f625d460SBenoit Parrot 392f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 393f625d460SBenoit Parrot if (ret) 394f625d460SBenoit Parrot return ERR_PTR(ret); 395f625d460SBenoit Parrot 396be715343SMasahiro Yamada gpiospec.np = chip_np; 397be715343SMasahiro Yamada gpiospec.args_count = tmp; 398f625d460SBenoit Parrot 399a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 400a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 401a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 402f625d460SBenoit Parrot if (ret) 403f625d460SBenoit Parrot return ERR_PTR(ret); 404a79fead5SGeert Uytterhoeven } 405f625d460SBenoit Parrot 40699468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 407be715343SMasahiro Yamada if (IS_ERR(desc)) 408be715343SMasahiro Yamada return desc; 409f625d460SBenoit Parrot 410f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 411f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 412e10f72bfSAndrew Jeffery if (xlate_flags & OF_GPIO_TRANSITORY) 413e10f72bfSAndrew Jeffery *lflags |= GPIO_TRANSITORY; 414f625d460SBenoit Parrot 415f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 416f625d460SBenoit Parrot *dflags |= GPIOD_IN; 417f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 418f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 419f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 420f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 421f625d460SBenoit Parrot else { 42262cdcb6cSRob Herring pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 42362cdcb6cSRob Herring desc_to_gpio(desc), np); 424f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 425f625d460SBenoit Parrot } 426f625d460SBenoit Parrot 427f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 428f625d460SBenoit Parrot *name = np->name; 429f625d460SBenoit Parrot 430be715343SMasahiro Yamada return desc; 431f625d460SBenoit Parrot } 432f625d460SBenoit Parrot 433f625d460SBenoit Parrot /** 434fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 435f625d460SBenoit Parrot * @chip: gpio chip to act on 436f625d460SBenoit Parrot * 437f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 438f625d460SBenoit Parrot * configuration. 439ead066e6SGeert Uytterhoeven * It returns error if it fails otherwise 0 on success. 440f625d460SBenoit Parrot */ 441dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 442f625d460SBenoit Parrot { 443f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 444f625d460SBenoit Parrot struct device_node *np; 445f625d460SBenoit Parrot const char *name; 446fed7026aSAndy Shevchenko unsigned long lflags; 447f625d460SBenoit Parrot enum gpiod_flags dflags; 448a79fead5SGeert Uytterhoeven unsigned int i; 449dfbd379bSLaxman Dewangan int ret; 450f625d460SBenoit Parrot 451d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 452f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 453f625d460SBenoit Parrot continue; 454f625d460SBenoit Parrot 455a79fead5SGeert Uytterhoeven for (i = 0;; i++) { 456a79fead5SGeert Uytterhoeven desc = of_parse_own_gpio(np, chip, i, &name, &lflags, 457a79fead5SGeert Uytterhoeven &dflags); 458f625d460SBenoit Parrot if (IS_ERR(desc)) 459a79fead5SGeert Uytterhoeven break; 460f625d460SBenoit Parrot 461dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 46209e258afSWei Yongjun if (ret < 0) { 46309e258afSWei Yongjun of_node_put(np); 464dfbd379bSLaxman Dewangan return ret; 465f625d460SBenoit Parrot } 46609e258afSWei Yongjun } 467a79fead5SGeert Uytterhoeven } 468dfbd379bSLaxman Dewangan 469dfbd379bSLaxman Dewangan return 0; 470f625d460SBenoit Parrot } 471f625d460SBenoit Parrot 472f625d460SBenoit Parrot /** 47367049c50SThierry Reding * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags 474f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 47567049c50SThierry Reding * @gpiospec: GPIO specifier as found in the device tree 476f141ed65SGrant Likely * @flags: a flags pointer to fill in 477f141ed65SGrant Likely * 478f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 47967049c50SThierry Reding * GPIO chips. This function performs only one sanity check: whether GPIO 480f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 481f141ed65SGrant Likely */ 482f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 483f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 484f141ed65SGrant Likely { 485f141ed65SGrant Likely /* 486f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 48720a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 488f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 489f141ed65SGrant Likely * but not recommended). 490f141ed65SGrant Likely */ 491f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 492f141ed65SGrant Likely WARN_ON(1); 493f141ed65SGrant Likely return -EINVAL; 494f141ed65SGrant Likely } 495f141ed65SGrant Likely 496f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 497f141ed65SGrant Likely return -EINVAL; 498f141ed65SGrant Likely 4997b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 500f141ed65SGrant Likely return -EINVAL; 501f141ed65SGrant Likely 502f141ed65SGrant Likely if (flags) 503f141ed65SGrant Likely *flags = gpiospec->args[1]; 504f141ed65SGrant Likely 505f141ed65SGrant Likely return gpiospec->args[0]; 506f141ed65SGrant Likely } 507f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 508f141ed65SGrant Likely 509f141ed65SGrant Likely /** 5103208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 511f141ed65SGrant Likely * @np: device node of the GPIO chip 512f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 5133208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 514f141ed65SGrant Likely * 515f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 516f141ed65SGrant Likely * 517f141ed65SGrant Likely * 1) In the gpio_chip structure: 518f141ed65SGrant Likely * - all the callbacks 519f141ed65SGrant Likely * - of_gpio_n_cells 520f141ed65SGrant Likely * - of_xlate callback (optional) 521f141ed65SGrant Likely * 522f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 523f141ed65SGrant Likely * - save_regs callback (optional) 524f141ed65SGrant Likely * 525f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 526f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 527f141ed65SGrant Likely * to manage GPIOs from the callbacks. 528f141ed65SGrant Likely */ 5293208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 5303208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 5313208b0f0SLinus Walleij void *data) 532f141ed65SGrant Likely { 533f141ed65SGrant Likely int ret = -ENOMEM; 534f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 535f141ed65SGrant Likely 5367eb6ce2fSRob Herring gc->label = kasprintf(GFP_KERNEL, "%pOF", np); 537f141ed65SGrant Likely if (!gc->label) 538f141ed65SGrant Likely goto err0; 539f141ed65SGrant Likely 540f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 541f141ed65SGrant Likely if (!mm_gc->regs) 542f141ed65SGrant Likely goto err1; 543f141ed65SGrant Likely 544f141ed65SGrant Likely gc->base = -1; 545f141ed65SGrant Likely 546f141ed65SGrant Likely if (mm_gc->save_regs) 547f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 548f141ed65SGrant Likely 549f141ed65SGrant Likely mm_gc->gc.of_node = np; 550f141ed65SGrant Likely 5513208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 552f141ed65SGrant Likely if (ret) 553f141ed65SGrant Likely goto err2; 554f141ed65SGrant Likely 555f141ed65SGrant Likely return 0; 556f141ed65SGrant Likely err2: 557f141ed65SGrant Likely iounmap(mm_gc->regs); 558f141ed65SGrant Likely err1: 559f141ed65SGrant Likely kfree(gc->label); 560f141ed65SGrant Likely err0: 5617eb6ce2fSRob Herring pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); 562f141ed65SGrant Likely return ret; 563f141ed65SGrant Likely } 5643208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data); 565f141ed65SGrant Likely 566d621e8baSRicardo Ribalda Delgado /** 567d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 568d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 569d621e8baSRicardo Ribalda Delgado */ 570d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 571d621e8baSRicardo Ribalda Delgado { 572d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 573d621e8baSRicardo Ribalda Delgado 574d621e8baSRicardo Ribalda Delgado if (!mm_gc) 575d621e8baSRicardo Ribalda Delgado return; 576d621e8baSRicardo Ribalda Delgado 577d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 578d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 579d621e8baSRicardo Ribalda Delgado kfree(gc->label); 580d621e8baSRicardo Ribalda Delgado } 581d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove); 582d621e8baSRicardo Ribalda Delgado 583726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) 584726cb3baSStephen Boyd { 585726cb3baSStephen Boyd int len, i; 586726cb3baSStephen Boyd u32 start, count; 587726cb3baSStephen Boyd struct device_node *np = chip->of_node; 588726cb3baSStephen Boyd 589726cb3baSStephen Boyd len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 590726cb3baSStephen Boyd if (len < 0 || len % 2 != 0) 591726cb3baSStephen Boyd return; 592726cb3baSStephen Boyd 593726cb3baSStephen Boyd for (i = 0; i < len; i += 2) { 594726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 595726cb3baSStephen Boyd i, &start); 596726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 597726cb3baSStephen Boyd i + 1, &count); 598726cb3baSStephen Boyd if (start >= chip->ngpio || start + count >= chip->ngpio) 599726cb3baSStephen Boyd continue; 600726cb3baSStephen Boyd 601726cb3baSStephen Boyd bitmap_clear(chip->valid_mask, start, count); 602726cb3baSStephen Boyd } 603726cb3baSStephen Boyd }; 604726cb3baSStephen Boyd 605f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 60628355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 607f23f1516SShiraz Hashim { 608f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 609f23f1516SShiraz Hashim struct of_phandle_args pinspec; 6101e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 611f23f1516SShiraz Hashim int index = 0, ret; 612586a87e6SChristian Ruppert const char *name; 613586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 614586a87e6SChristian Ruppert struct property *group_names; 615f23f1516SShiraz Hashim 616f23f1516SShiraz Hashim if (!np) 61728355f81STomeu Vizoso return 0; 618f23f1516SShiraz Hashim 619586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 620586a87e6SChristian Ruppert 621ad4e1a7cSHaojian Zhuang for (;; index++) { 622d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 623d9fe0039SStephen Warren index, &pinspec); 624f23f1516SShiraz Hashim if (ret) 625f23f1516SShiraz Hashim break; 626f23f1516SShiraz Hashim 6271e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 628602cf638SMasahiro Yamada of_node_put(pinspec.np); 6291e63d7b9SLinus Walleij if (!pctldev) 63028355f81STomeu Vizoso return -EPROBE_DEFER; 631f23f1516SShiraz Hashim 632586a87e6SChristian Ruppert if (pinspec.args[2]) { 633586a87e6SChristian Ruppert if (group_names) { 63472858602SLaurent Navet of_property_read_string_index(np, 635586a87e6SChristian Ruppert group_names_propname, 636586a87e6SChristian Ruppert index, &name); 637586a87e6SChristian Ruppert if (strlen(name)) { 6387eb6ce2fSRob Herring pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 6397eb6ce2fSRob Herring np); 640586a87e6SChristian Ruppert break; 641586a87e6SChristian Ruppert } 642586a87e6SChristian Ruppert } 643586a87e6SChristian Ruppert /* npins != 0: linear range */ 6441e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 645ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 6461e63d7b9SLinus Walleij pinspec.args[0], 64786853c83SHaojian Zhuang pinspec.args[1], 64886853c83SHaojian Zhuang pinspec.args[2]); 6491e63d7b9SLinus Walleij if (ret) 65028355f81STomeu Vizoso return ret; 651586a87e6SChristian Ruppert } else { 652586a87e6SChristian Ruppert /* npins == 0: special range */ 653586a87e6SChristian Ruppert if (pinspec.args[1]) { 6547eb6ce2fSRob Herring pr_err("%pOF: Illegal gpio-range format.\n", 6557eb6ce2fSRob Herring np); 656586a87e6SChristian Ruppert break; 657586a87e6SChristian Ruppert } 658586a87e6SChristian Ruppert 659586a87e6SChristian Ruppert if (!group_names) { 6607eb6ce2fSRob Herring pr_err("%pOF: GPIO group range requested but no %s property.\n", 6617eb6ce2fSRob Herring np, group_names_propname); 662586a87e6SChristian Ruppert break; 663586a87e6SChristian Ruppert } 664586a87e6SChristian Ruppert 665586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 666586a87e6SChristian Ruppert group_names_propname, 667586a87e6SChristian Ruppert index, &name); 668586a87e6SChristian Ruppert if (ret) 669586a87e6SChristian Ruppert break; 670586a87e6SChristian Ruppert 671586a87e6SChristian Ruppert if (!strlen(name)) { 6727eb6ce2fSRob Herring pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 6737eb6ce2fSRob Herring np); 674586a87e6SChristian Ruppert break; 675586a87e6SChristian Ruppert } 676586a87e6SChristian Ruppert 677586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 678586a87e6SChristian Ruppert pinspec.args[0], name); 679586a87e6SChristian Ruppert if (ret) 68028355f81STomeu Vizoso return ret; 681586a87e6SChristian Ruppert } 682ad4e1a7cSHaojian Zhuang } 68328355f81STomeu Vizoso 68428355f81STomeu Vizoso return 0; 685f23f1516SShiraz Hashim } 686f23f1516SShiraz Hashim 687f23f1516SShiraz Hashim #else 68828355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 689f23f1516SShiraz Hashim #endif 690f23f1516SShiraz Hashim 69128355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 692f141ed65SGrant Likely { 69328355f81STomeu Vizoso int status; 69428355f81STomeu Vizoso 695f141ed65SGrant Likely if (!chip->of_node) 69628355f81STomeu Vizoso return 0; 697f141ed65SGrant Likely 698f141ed65SGrant Likely if (!chip->of_xlate) { 699f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 700f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 701f141ed65SGrant Likely } 702f141ed65SGrant Likely 7031020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 7041020dfd1SMasahiro Yamada return -EINVAL; 7051020dfd1SMasahiro Yamada 706726cb3baSStephen Boyd of_gpiochip_init_valid_mask(chip); 707726cb3baSStephen Boyd 70828355f81STomeu Vizoso status = of_gpiochip_add_pin_range(chip); 70928355f81STomeu Vizoso if (status) 71028355f81STomeu Vizoso return status; 71128355f81STomeu Vizoso 712fd9c5531SLinus Walleij /* If the chip defines names itself, these take precedence */ 713fd9c5531SLinus Walleij if (!chip->names) 71482270335SChristophe Leroy devprop_gpiochip_set_names(chip, 71582270335SChristophe Leroy of_fwnode_handle(chip->of_node)); 716fd9c5531SLinus Walleij 717f141ed65SGrant Likely of_node_get(chip->of_node); 718f625d460SBenoit Parrot 719f7299d44SGeert Uytterhoeven status = of_gpiochip_scan_gpios(chip); 720f7299d44SGeert Uytterhoeven if (status) { 721f7299d44SGeert Uytterhoeven of_node_put(chip->of_node); 722f7299d44SGeert Uytterhoeven gpiochip_remove_pin_ranges(chip); 723f7299d44SGeert Uytterhoeven } 724f7299d44SGeert Uytterhoeven 725f7299d44SGeert Uytterhoeven return status; 726f141ed65SGrant Likely } 727f141ed65SGrant Likely 728f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 729f141ed65SGrant Likely { 730e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 731f141ed65SGrant Likely of_node_put(chip->of_node); 732f141ed65SGrant Likely } 733