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") || 89a603a2b8SLinus Walleij of_device_is_compatible(np, "regulator-gpio"))) { 90a603a2b8SLinus Walleij /* 91a603a2b8SLinus Walleij * The regulator GPIO handles are specified such that the 92a603a2b8SLinus Walleij * presence or absence of "enable-active-high" solely controls 93a603a2b8SLinus Walleij * the polarity of the GPIO line. Any phandle flags must 94a603a2b8SLinus Walleij * be actively ignored. 95a603a2b8SLinus Walleij */ 96a603a2b8SLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 97a603a2b8SLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 98a603a2b8SLinus Walleij of_node_full_name(np)); 99a603a2b8SLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 100a603a2b8SLinus Walleij } 101a603a2b8SLinus Walleij if (!of_property_read_bool(np, "enable-active-high")) 102a603a2b8SLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 103a603a2b8SLinus Walleij } 104a603a2b8SLinus Walleij /* 105a603a2b8SLinus Walleij * Legacy open drain handling for fixed voltage regulators. 106a603a2b8SLinus Walleij */ 107a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 108a603a2b8SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") && 109a603a2b8SLinus Walleij of_property_read_bool(np, "gpio-open-drain")) { 110a603a2b8SLinus Walleij *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); 111a603a2b8SLinus Walleij pr_info("%s uses legacy open drain flag - update the DTS if you can\n", 112a603a2b8SLinus Walleij of_node_full_name(np)); 113a603a2b8SLinus Walleij } 1146953c57aSLinus Walleij 1156953c57aSLinus Walleij /* 1166953c57aSLinus Walleij * Legacy handling of SPI active high chip select. If we have a 1176953c57aSLinus Walleij * property named "cs-gpios" we need to inspect the child node 1186953c57aSLinus Walleij * to determine if the flags should have inverted semantics. 1196953c57aSLinus Walleij */ 1206953c57aSLinus Walleij if (IS_ENABLED(CONFIG_SPI_MASTER) && 1216953c57aSLinus Walleij of_property_read_bool(np, "cs-gpios")) { 1226953c57aSLinus Walleij struct device_node *child; 1236953c57aSLinus Walleij u32 cs; 1246953c57aSLinus Walleij int ret; 1256953c57aSLinus Walleij 1266953c57aSLinus Walleij for_each_child_of_node(np, child) { 1276953c57aSLinus Walleij ret = of_property_read_u32(child, "reg", &cs); 1286953c57aSLinus Walleij if (!ret) 1296953c57aSLinus Walleij continue; 1306953c57aSLinus Walleij if (cs == index) { 1316953c57aSLinus Walleij /* 1326953c57aSLinus Walleij * SPI children have active low chip selects 1336953c57aSLinus Walleij * by default. This can be specified negatively 1346953c57aSLinus Walleij * by just omitting "spi-cs-high" in the 1356953c57aSLinus Walleij * device node, or actively by tagging on 1366953c57aSLinus Walleij * GPIO_ACTIVE_LOW as flag in the device 1376953c57aSLinus Walleij * tree. If the line is simultaneously 1386953c57aSLinus Walleij * tagged as active low in the device tree 1396953c57aSLinus Walleij * and has the "spi-cs-high" set, we get a 1406953c57aSLinus Walleij * conflict and the "spi-cs-high" flag will 1416953c57aSLinus Walleij * take precedence. 1426953c57aSLinus Walleij */ 1436953c57aSLinus Walleij if (of_property_read_bool(np, "spi-cs-high")) { 1446953c57aSLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 1456953c57aSLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 1466953c57aSLinus Walleij of_node_full_name(np)); 1476953c57aSLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 1486953c57aSLinus Walleij } 1496953c57aSLinus Walleij } else { 1506953c57aSLinus Walleij if (!(*flags & OF_GPIO_ACTIVE_LOW)) 1516953c57aSLinus Walleij pr_info("%s enforce active low on chipselect handle\n", 1526953c57aSLinus Walleij of_node_full_name(np)); 1536953c57aSLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 1546953c57aSLinus Walleij } 1556953c57aSLinus Walleij break; 1566953c57aSLinus Walleij } 1576953c57aSLinus Walleij } 1586953c57aSLinus Walleij } 159a603a2b8SLinus Walleij } 160a603a2b8SLinus Walleij 161f141ed65SGrant Likely /** 162af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 163f141ed65SGrant Likely * @np: device node to get GPIO from 164f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 165f141ed65SGrant Likely * @index: index of the GPIO 166f141ed65SGrant Likely * @flags: a flags pointer to fill in 167f141ed65SGrant Likely * 168af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 169f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 170f141ed65SGrant Likely * in flags for the GPIO. 171f141ed65SGrant Likely */ 172af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 173af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 174f141ed65SGrant Likely { 175762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 176762c2e46SMasahiro Yamada struct gpio_chip *chip; 177762c2e46SMasahiro Yamada struct gpio_desc *desc; 178f141ed65SGrant Likely int ret; 179f141ed65SGrant Likely 180c11e6f0fSStephen Boyd ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 181762c2e46SMasahiro Yamada &gpiospec); 1823d0f7cf0SGrant Likely if (ret) { 1837eb6ce2fSRob Herring pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 1847eb6ce2fSRob Herring __func__, propname, np, index); 185af8b6375SAlexandre Courbot return ERR_PTR(ret); 1863d0f7cf0SGrant Likely } 187f141ed65SGrant Likely 188c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 189762c2e46SMasahiro Yamada if (!chip) { 190762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 191762c2e46SMasahiro Yamada goto out; 192762c2e46SMasahiro Yamada } 1933d0f7cf0SGrant Likely 19499468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 195762c2e46SMasahiro Yamada if (IS_ERR(desc)) 196762c2e46SMasahiro Yamada goto out; 197762c2e46SMasahiro Yamada 198605f2d34SLinus Walleij if (flags) 19989a5e15bSLinus Walleij of_gpio_flags_quirks(np, propname, flags, index); 200a603a2b8SLinus Walleij 2017eb6ce2fSRob Herring pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 2027eb6ce2fSRob Herring __func__, propname, np, index, 203762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 204762c2e46SMasahiro Yamada 205762c2e46SMasahiro Yamada out: 206762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 207762c2e46SMasahiro Yamada 208762c2e46SMasahiro Yamada return desc; 209f141ed65SGrant Likely } 210f141ed65SGrant Likely 211f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 212f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 213f01d9075SAlexandre Courbot { 214f01d9075SAlexandre Courbot struct gpio_desc *desc; 215f01d9075SAlexandre Courbot 216f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 217f01d9075SAlexandre Courbot 218f01d9075SAlexandre Courbot if (IS_ERR(desc)) 219f01d9075SAlexandre Courbot return PTR_ERR(desc); 220f01d9075SAlexandre Courbot else 221f01d9075SAlexandre Courbot return desc_to_gpio(desc); 222f01d9075SAlexandre Courbot } 223f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 224f01d9075SAlexandre Courbot 225c8582339SLinus Walleij /* 226c8582339SLinus Walleij * The SPI GPIO bindings happened before we managed to establish that GPIO 227c8582339SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 228c8582339SLinus Walleij * them. 229c8582339SLinus Walleij */ 230c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, 231c8582339SLinus Walleij enum of_gpio_flags *of_flags) 232c8582339SLinus Walleij { 233c8582339SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 234c8582339SLinus Walleij struct device_node *np = dev->of_node; 235c8582339SLinus Walleij struct gpio_desc *desc; 236c8582339SLinus Walleij 237c8582339SLinus Walleij /* 238c8582339SLinus Walleij * Hopefully the compiler stubs the rest of the function if this 239c8582339SLinus Walleij * is false. 240c8582339SLinus Walleij */ 241c8582339SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 242c8582339SLinus Walleij return ERR_PTR(-ENOENT); 243c8582339SLinus Walleij 244c8582339SLinus Walleij /* Allow this specifically for "spi-gpio" devices */ 245c8582339SLinus Walleij if (!of_device_is_compatible(np, "spi-gpio") || !con_id) 246c8582339SLinus Walleij return ERR_PTR(-ENOENT); 247c8582339SLinus Walleij 248c8582339SLinus Walleij /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ 249c8582339SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); 250c8582339SLinus Walleij 251c8582339SLinus Walleij desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); 252c8582339SLinus Walleij return desc; 253c8582339SLinus Walleij } 254c8582339SLinus Walleij 2556a537d48SLinus Walleij /* 2566a537d48SLinus Walleij * Some regulator bindings happened before we managed to establish that GPIO 2576a537d48SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 2586a537d48SLinus Walleij * them. 2596a537d48SLinus Walleij */ 2606a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, 2616a537d48SLinus Walleij enum of_gpio_flags *of_flags) 2626a537d48SLinus Walleij { 2636a537d48SLinus Walleij /* These are the connection IDs we accept as legacy GPIO phandles */ 2646a537d48SLinus Walleij const char *whitelist[] = { 2656a537d48SLinus Walleij "wlf,ldoena", /* Arizona */ 2666a537d48SLinus Walleij "wlf,ldo1ena", /* WM8994 */ 2676a537d48SLinus Walleij "wlf,ldo2ena", /* WM8994 */ 2686a537d48SLinus Walleij }; 2696a537d48SLinus Walleij struct device_node *np = dev->of_node; 2706a537d48SLinus Walleij struct gpio_desc *desc; 2716a537d48SLinus Walleij int i; 2726a537d48SLinus Walleij 2736a537d48SLinus Walleij if (!IS_ENABLED(CONFIG_REGULATOR)) 2746a537d48SLinus Walleij return ERR_PTR(-ENOENT); 2756a537d48SLinus Walleij 2766a537d48SLinus Walleij if (!con_id) 2776a537d48SLinus Walleij return ERR_PTR(-ENOENT); 2786a537d48SLinus Walleij 2794b21f94aSAndy Shevchenko i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id); 2804b21f94aSAndy Shevchenko if (i < 0) 2816a537d48SLinus Walleij return ERR_PTR(-ENOENT); 2826a537d48SLinus Walleij 2836a537d48SLinus Walleij desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags); 2846a537d48SLinus Walleij return desc; 2856a537d48SLinus Walleij } 2866a537d48SLinus Walleij 287ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 288ea713bc4SLinus Walleij unsigned int idx, 289ea713bc4SLinus Walleij enum gpio_lookup_flags *flags) 290ea713bc4SLinus Walleij { 291ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 292ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 293ea713bc4SLinus Walleij struct gpio_desc *desc; 294ea713bc4SLinus Walleij unsigned int i; 295ea713bc4SLinus Walleij 296c8582339SLinus Walleij /* Try GPIO property "foo-gpios" and "foo-gpio" */ 297ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 298ea713bc4SLinus Walleij if (con_id) 299ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 300ea713bc4SLinus Walleij gpio_suffixes[i]); 301ea713bc4SLinus Walleij else 302ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 303ea713bc4SLinus Walleij gpio_suffixes[i]); 304ea713bc4SLinus Walleij 305ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 306ea713bc4SLinus Walleij &of_flags); 3076662ae6aSMaxime Ripard /* 3086662ae6aSMaxime Ripard * -EPROBE_DEFER in our case means that we found a 3096662ae6aSMaxime Ripard * valid GPIO property, but no controller has been 3106662ae6aSMaxime Ripard * registered so far. 3116662ae6aSMaxime Ripard * 3126662ae6aSMaxime Ripard * This means we don't need to look any further for 3136662ae6aSMaxime Ripard * alternate name conventions, and we should really 3146662ae6aSMaxime Ripard * preserve the return code for our user to be able to 3156662ae6aSMaxime Ripard * retry probing later. 3166662ae6aSMaxime Ripard */ 3176662ae6aSMaxime Ripard if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER) 3186662ae6aSMaxime Ripard return desc; 3196662ae6aSMaxime Ripard 320ea713bc4SLinus Walleij if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) 321ea713bc4SLinus Walleij break; 322ea713bc4SLinus Walleij } 323ea713bc4SLinus Walleij 324c8582339SLinus Walleij /* Special handling for SPI GPIOs if used */ 325c8582339SLinus Walleij if (IS_ERR(desc)) 326c8582339SLinus Walleij desc = of_find_spi_gpio(dev, con_id, &of_flags); 327c8582339SLinus Walleij 3286a537d48SLinus Walleij /* Special handling for regulator GPIOs if used */ 329ce27fb2cSChen-Yu Tsai if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) 3306a537d48SLinus Walleij desc = of_find_regulator_gpio(dev, con_id, &of_flags); 3316a537d48SLinus Walleij 332ea713bc4SLinus Walleij if (IS_ERR(desc)) 333ea713bc4SLinus Walleij return desc; 334ea713bc4SLinus Walleij 335ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 336ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 337ea713bc4SLinus Walleij 338ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 3394c0facddSLaxman Dewangan if (of_flags & OF_GPIO_OPEN_DRAIN) 340ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 341ea713bc4SLinus Walleij else 342ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 343ea713bc4SLinus Walleij } 344ea713bc4SLinus Walleij 345e10f72bfSAndrew Jeffery if (of_flags & OF_GPIO_TRANSITORY) 346e10f72bfSAndrew Jeffery *flags |= GPIO_TRANSITORY; 34705f479bfSCharles Keepax 348d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_UP) 349d449991cSThomas Petazzoni *flags |= GPIO_PULL_UP; 350d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_DOWN) 351d449991cSThomas Petazzoni *flags |= GPIO_PULL_DOWN; 352d449991cSThomas Petazzoni 353ea713bc4SLinus Walleij return desc; 354ea713bc4SLinus Walleij } 355ea713bc4SLinus Walleij 356f141ed65SGrant Likely /** 357fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 358f625d460SBenoit Parrot * @np: device node to get GPIO from 359be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 360a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 361f625d460SBenoit Parrot * @name: GPIO line name 362f625d460SBenoit Parrot * @lflags: gpio_lookup_flags - returned from of_find_gpio() or 363fd7337fdSMarkus Pargmann * of_parse_own_gpio() 364f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 365f625d460SBenoit Parrot * 366f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 367f625d460SBenoit Parrot * value on the error condition. 368f625d460SBenoit Parrot */ 369fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 370be715343SMasahiro Yamada struct gpio_chip *chip, 371a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 372f625d460SBenoit Parrot enum gpio_lookup_flags *lflags, 373f625d460SBenoit Parrot enum gpiod_flags *dflags) 374f625d460SBenoit Parrot { 375f625d460SBenoit Parrot struct device_node *chip_np; 376f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 377be715343SMasahiro Yamada struct of_phandle_args gpiospec; 378be715343SMasahiro Yamada struct gpio_desc *desc; 379a79fead5SGeert Uytterhoeven unsigned int i; 380f625d460SBenoit Parrot u32 tmp; 3813f9547e1SMasahiro Yamada int ret; 382f625d460SBenoit Parrot 383be715343SMasahiro Yamada chip_np = chip->of_node; 384f625d460SBenoit Parrot if (!chip_np) 385f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 386f625d460SBenoit Parrot 387f625d460SBenoit Parrot xlate_flags = 0; 388f625d460SBenoit Parrot *lflags = 0; 389f625d460SBenoit Parrot *dflags = 0; 390f625d460SBenoit Parrot 391f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 392f625d460SBenoit Parrot if (ret) 393f625d460SBenoit Parrot return ERR_PTR(ret); 394f625d460SBenoit Parrot 395be715343SMasahiro Yamada gpiospec.np = chip_np; 396be715343SMasahiro Yamada gpiospec.args_count = tmp; 397f625d460SBenoit Parrot 398a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 399a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 400a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 401f625d460SBenoit Parrot if (ret) 402f625d460SBenoit Parrot return ERR_PTR(ret); 403a79fead5SGeert Uytterhoeven } 404f625d460SBenoit Parrot 40599468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 406be715343SMasahiro Yamada if (IS_ERR(desc)) 407be715343SMasahiro Yamada return desc; 408f625d460SBenoit Parrot 409f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 410f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 411e10f72bfSAndrew Jeffery if (xlate_flags & OF_GPIO_TRANSITORY) 412e10f72bfSAndrew Jeffery *lflags |= GPIO_TRANSITORY; 413f625d460SBenoit Parrot 414f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 415f625d460SBenoit Parrot *dflags |= GPIOD_IN; 416f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 417f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 418f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 419f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 420f625d460SBenoit Parrot else { 42162cdcb6cSRob Herring pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 42262cdcb6cSRob Herring desc_to_gpio(desc), np); 423f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 424f625d460SBenoit Parrot } 425f625d460SBenoit Parrot 426f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 427f625d460SBenoit Parrot *name = np->name; 428f625d460SBenoit Parrot 429be715343SMasahiro Yamada return desc; 430f625d460SBenoit Parrot } 431f625d460SBenoit Parrot 432f625d460SBenoit Parrot /** 433fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 434f625d460SBenoit Parrot * @chip: gpio chip to act on 435f625d460SBenoit Parrot * 436f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 437f625d460SBenoit Parrot * configuration. 438ead066e6SGeert Uytterhoeven * It returns error if it fails otherwise 0 on success. 439f625d460SBenoit Parrot */ 440dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 441f625d460SBenoit Parrot { 442f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 443f625d460SBenoit Parrot struct device_node *np; 444f625d460SBenoit Parrot const char *name; 445f625d460SBenoit Parrot enum gpio_lookup_flags lflags; 446f625d460SBenoit Parrot enum gpiod_flags dflags; 447a79fead5SGeert Uytterhoeven unsigned int i; 448dfbd379bSLaxman Dewangan int ret; 449f625d460SBenoit Parrot 450d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 451f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 452f625d460SBenoit Parrot continue; 453f625d460SBenoit Parrot 454a79fead5SGeert Uytterhoeven for (i = 0;; i++) { 455a79fead5SGeert Uytterhoeven desc = of_parse_own_gpio(np, chip, i, &name, &lflags, 456a79fead5SGeert Uytterhoeven &dflags); 457f625d460SBenoit Parrot if (IS_ERR(desc)) 458a79fead5SGeert Uytterhoeven break; 459f625d460SBenoit Parrot 460dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 46109e258afSWei Yongjun if (ret < 0) { 46209e258afSWei Yongjun of_node_put(np); 463dfbd379bSLaxman Dewangan return ret; 464f625d460SBenoit Parrot } 46509e258afSWei Yongjun } 466a79fead5SGeert Uytterhoeven } 467dfbd379bSLaxman Dewangan 468dfbd379bSLaxman Dewangan return 0; 469f625d460SBenoit Parrot } 470f625d460SBenoit Parrot 471f625d460SBenoit Parrot /** 47267049c50SThierry Reding * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags 473f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 47467049c50SThierry Reding * @gpiospec: GPIO specifier as found in the device tree 475f141ed65SGrant Likely * @flags: a flags pointer to fill in 476f141ed65SGrant Likely * 477f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 47867049c50SThierry Reding * GPIO chips. This function performs only one sanity check: whether GPIO 479f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 480f141ed65SGrant Likely */ 481f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 482f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 483f141ed65SGrant Likely { 484f141ed65SGrant Likely /* 485f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 48620a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 487f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 488f141ed65SGrant Likely * but not recommended). 489f141ed65SGrant Likely */ 490f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 491f141ed65SGrant Likely WARN_ON(1); 492f141ed65SGrant Likely return -EINVAL; 493f141ed65SGrant Likely } 494f141ed65SGrant Likely 495f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 496f141ed65SGrant Likely return -EINVAL; 497f141ed65SGrant Likely 4987b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 499f141ed65SGrant Likely return -EINVAL; 500f141ed65SGrant Likely 501f141ed65SGrant Likely if (flags) 502f141ed65SGrant Likely *flags = gpiospec->args[1]; 503f141ed65SGrant Likely 504f141ed65SGrant Likely return gpiospec->args[0]; 505f141ed65SGrant Likely } 506f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 507f141ed65SGrant Likely 508f141ed65SGrant Likely /** 5093208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 510f141ed65SGrant Likely * @np: device node of the GPIO chip 511f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 5123208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 513f141ed65SGrant Likely * 514f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 515f141ed65SGrant Likely * 516f141ed65SGrant Likely * 1) In the gpio_chip structure: 517f141ed65SGrant Likely * - all the callbacks 518f141ed65SGrant Likely * - of_gpio_n_cells 519f141ed65SGrant Likely * - of_xlate callback (optional) 520f141ed65SGrant Likely * 521f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 522f141ed65SGrant Likely * - save_regs callback (optional) 523f141ed65SGrant Likely * 524f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 525f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 526f141ed65SGrant Likely * to manage GPIOs from the callbacks. 527f141ed65SGrant Likely */ 5283208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 5293208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 5303208b0f0SLinus Walleij void *data) 531f141ed65SGrant Likely { 532f141ed65SGrant Likely int ret = -ENOMEM; 533f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 534f141ed65SGrant Likely 5357eb6ce2fSRob Herring gc->label = kasprintf(GFP_KERNEL, "%pOF", np); 536f141ed65SGrant Likely if (!gc->label) 537f141ed65SGrant Likely goto err0; 538f141ed65SGrant Likely 539f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 540f141ed65SGrant Likely if (!mm_gc->regs) 541f141ed65SGrant Likely goto err1; 542f141ed65SGrant Likely 543f141ed65SGrant Likely gc->base = -1; 544f141ed65SGrant Likely 545f141ed65SGrant Likely if (mm_gc->save_regs) 546f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 547f141ed65SGrant Likely 548f141ed65SGrant Likely mm_gc->gc.of_node = np; 549f141ed65SGrant Likely 5503208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 551f141ed65SGrant Likely if (ret) 552f141ed65SGrant Likely goto err2; 553f141ed65SGrant Likely 554f141ed65SGrant Likely return 0; 555f141ed65SGrant Likely err2: 556f141ed65SGrant Likely iounmap(mm_gc->regs); 557f141ed65SGrant Likely err1: 558f141ed65SGrant Likely kfree(gc->label); 559f141ed65SGrant Likely err0: 5607eb6ce2fSRob Herring pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); 561f141ed65SGrant Likely return ret; 562f141ed65SGrant Likely } 5633208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data); 564f141ed65SGrant Likely 565d621e8baSRicardo Ribalda Delgado /** 566d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 567d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 568d621e8baSRicardo Ribalda Delgado */ 569d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 570d621e8baSRicardo Ribalda Delgado { 571d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 572d621e8baSRicardo Ribalda Delgado 573d621e8baSRicardo Ribalda Delgado if (!mm_gc) 574d621e8baSRicardo Ribalda Delgado return; 575d621e8baSRicardo Ribalda Delgado 576d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 577d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 578d621e8baSRicardo Ribalda Delgado kfree(gc->label); 579d621e8baSRicardo Ribalda Delgado } 580d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove); 581d621e8baSRicardo Ribalda Delgado 582726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) 583726cb3baSStephen Boyd { 584726cb3baSStephen Boyd int len, i; 585726cb3baSStephen Boyd u32 start, count; 586726cb3baSStephen Boyd struct device_node *np = chip->of_node; 587726cb3baSStephen Boyd 588726cb3baSStephen Boyd len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 589726cb3baSStephen Boyd if (len < 0 || len % 2 != 0) 590726cb3baSStephen Boyd return; 591726cb3baSStephen Boyd 592726cb3baSStephen Boyd for (i = 0; i < len; i += 2) { 593726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 594726cb3baSStephen Boyd i, &start); 595726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 596726cb3baSStephen Boyd i + 1, &count); 597726cb3baSStephen Boyd if (start >= chip->ngpio || start + count >= chip->ngpio) 598726cb3baSStephen Boyd continue; 599726cb3baSStephen Boyd 600726cb3baSStephen Boyd bitmap_clear(chip->valid_mask, start, count); 601726cb3baSStephen Boyd } 602726cb3baSStephen Boyd }; 603726cb3baSStephen Boyd 604f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 60528355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 606f23f1516SShiraz Hashim { 607f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 608f23f1516SShiraz Hashim struct of_phandle_args pinspec; 6091e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 610f23f1516SShiraz Hashim int index = 0, ret; 611586a87e6SChristian Ruppert const char *name; 612586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 613586a87e6SChristian Ruppert struct property *group_names; 614f23f1516SShiraz Hashim 615f23f1516SShiraz Hashim if (!np) 61628355f81STomeu Vizoso return 0; 617f23f1516SShiraz Hashim 618586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 619586a87e6SChristian Ruppert 620ad4e1a7cSHaojian Zhuang for (;; index++) { 621d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 622d9fe0039SStephen Warren index, &pinspec); 623f23f1516SShiraz Hashim if (ret) 624f23f1516SShiraz Hashim break; 625f23f1516SShiraz Hashim 6261e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 627602cf638SMasahiro Yamada of_node_put(pinspec.np); 6281e63d7b9SLinus Walleij if (!pctldev) 62928355f81STomeu Vizoso return -EPROBE_DEFER; 630f23f1516SShiraz Hashim 631586a87e6SChristian Ruppert if (pinspec.args[2]) { 632586a87e6SChristian Ruppert if (group_names) { 63372858602SLaurent Navet of_property_read_string_index(np, 634586a87e6SChristian Ruppert group_names_propname, 635586a87e6SChristian Ruppert index, &name); 636586a87e6SChristian Ruppert if (strlen(name)) { 6377eb6ce2fSRob Herring pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 6387eb6ce2fSRob Herring np); 639586a87e6SChristian Ruppert break; 640586a87e6SChristian Ruppert } 641586a87e6SChristian Ruppert } 642586a87e6SChristian Ruppert /* npins != 0: linear range */ 6431e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 644ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 6451e63d7b9SLinus Walleij pinspec.args[0], 64686853c83SHaojian Zhuang pinspec.args[1], 64786853c83SHaojian Zhuang pinspec.args[2]); 6481e63d7b9SLinus Walleij if (ret) 64928355f81STomeu Vizoso return ret; 650586a87e6SChristian Ruppert } else { 651586a87e6SChristian Ruppert /* npins == 0: special range */ 652586a87e6SChristian Ruppert if (pinspec.args[1]) { 6537eb6ce2fSRob Herring pr_err("%pOF: Illegal gpio-range format.\n", 6547eb6ce2fSRob Herring np); 655586a87e6SChristian Ruppert break; 656586a87e6SChristian Ruppert } 657586a87e6SChristian Ruppert 658586a87e6SChristian Ruppert if (!group_names) { 6597eb6ce2fSRob Herring pr_err("%pOF: GPIO group range requested but no %s property.\n", 6607eb6ce2fSRob Herring np, group_names_propname); 661586a87e6SChristian Ruppert break; 662586a87e6SChristian Ruppert } 663586a87e6SChristian Ruppert 664586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 665586a87e6SChristian Ruppert group_names_propname, 666586a87e6SChristian Ruppert index, &name); 667586a87e6SChristian Ruppert if (ret) 668586a87e6SChristian Ruppert break; 669586a87e6SChristian Ruppert 670586a87e6SChristian Ruppert if (!strlen(name)) { 6717eb6ce2fSRob Herring pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 6727eb6ce2fSRob Herring np); 673586a87e6SChristian Ruppert break; 674586a87e6SChristian Ruppert } 675586a87e6SChristian Ruppert 676586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 677586a87e6SChristian Ruppert pinspec.args[0], name); 678586a87e6SChristian Ruppert if (ret) 67928355f81STomeu Vizoso return ret; 680586a87e6SChristian Ruppert } 681ad4e1a7cSHaojian Zhuang } 68228355f81STomeu Vizoso 68328355f81STomeu Vizoso return 0; 684f23f1516SShiraz Hashim } 685f23f1516SShiraz Hashim 686f23f1516SShiraz Hashim #else 68728355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 688f23f1516SShiraz Hashim #endif 689f23f1516SShiraz Hashim 69028355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 691f141ed65SGrant Likely { 69228355f81STomeu Vizoso int status; 69328355f81STomeu Vizoso 694f141ed65SGrant Likely if (!chip->of_node) 69528355f81STomeu Vizoso return 0; 696f141ed65SGrant Likely 697f141ed65SGrant Likely if (!chip->of_xlate) { 698f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 699f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 700f141ed65SGrant Likely } 701f141ed65SGrant Likely 7021020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 7031020dfd1SMasahiro Yamada return -EINVAL; 7041020dfd1SMasahiro Yamada 705726cb3baSStephen Boyd of_gpiochip_init_valid_mask(chip); 706726cb3baSStephen Boyd 70728355f81STomeu Vizoso status = of_gpiochip_add_pin_range(chip); 70828355f81STomeu Vizoso if (status) 70928355f81STomeu Vizoso return status; 71028355f81STomeu Vizoso 711fd9c5531SLinus Walleij /* If the chip defines names itself, these take precedence */ 712fd9c5531SLinus Walleij if (!chip->names) 71382270335SChristophe Leroy devprop_gpiochip_set_names(chip, 71482270335SChristophe Leroy of_fwnode_handle(chip->of_node)); 715fd9c5531SLinus Walleij 716f141ed65SGrant Likely of_node_get(chip->of_node); 717f625d460SBenoit Parrot 718dfbd379bSLaxman Dewangan return of_gpiochip_scan_gpios(chip); 719f141ed65SGrant Likely } 720f141ed65SGrant Likely 721f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 722f141ed65SGrant Likely { 723e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 724f141ed65SGrant Likely of_node_put(chip->of_node); 725f141ed65SGrant Likely } 726