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 */ 122da7f1349SLinus Walleij 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 } 161edc1ef3fSMartin Blumenstingl 162edc1ef3fSMartin Blumenstingl /* Legacy handling of stmmac's active-low PHY reset line */ 163edc1ef3fSMartin Blumenstingl if (IS_ENABLED(CONFIG_STMMAC_ETH) && 164edc1ef3fSMartin Blumenstingl !strcmp(propname, "snps,reset-gpio") && 165edc1ef3fSMartin Blumenstingl of_property_read_bool(np, "snps,reset-active-low")) 166edc1ef3fSMartin Blumenstingl *flags |= OF_GPIO_ACTIVE_LOW; 167a603a2b8SLinus Walleij } 168a603a2b8SLinus Walleij 169f141ed65SGrant Likely /** 170af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 171f141ed65SGrant Likely * @np: device node to get GPIO from 172f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 173f141ed65SGrant Likely * @index: index of the GPIO 174f141ed65SGrant Likely * @flags: a flags pointer to fill in 175f141ed65SGrant Likely * 176af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 177f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 178f141ed65SGrant Likely * in flags for the GPIO. 179f141ed65SGrant Likely */ 180af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 181af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 182f141ed65SGrant Likely { 183762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 184762c2e46SMasahiro Yamada struct gpio_chip *chip; 185762c2e46SMasahiro Yamada struct gpio_desc *desc; 186f141ed65SGrant Likely int ret; 187f141ed65SGrant Likely 188c11e6f0fSStephen Boyd ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 189762c2e46SMasahiro Yamada &gpiospec); 1903d0f7cf0SGrant Likely if (ret) { 1917eb6ce2fSRob Herring pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 1927eb6ce2fSRob Herring __func__, propname, np, index); 193af8b6375SAlexandre Courbot return ERR_PTR(ret); 1943d0f7cf0SGrant Likely } 195f141ed65SGrant Likely 196c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 197762c2e46SMasahiro Yamada if (!chip) { 198762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 199762c2e46SMasahiro Yamada goto out; 200762c2e46SMasahiro Yamada } 2013d0f7cf0SGrant Likely 20299468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 203762c2e46SMasahiro Yamada if (IS_ERR(desc)) 204762c2e46SMasahiro Yamada goto out; 205762c2e46SMasahiro Yamada 206605f2d34SLinus Walleij if (flags) 20789a5e15bSLinus Walleij of_gpio_flags_quirks(np, propname, flags, index); 208a603a2b8SLinus Walleij 2097eb6ce2fSRob Herring pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 2107eb6ce2fSRob Herring __func__, propname, np, index, 211762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 212762c2e46SMasahiro Yamada 213762c2e46SMasahiro Yamada out: 214762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 215762c2e46SMasahiro Yamada 216762c2e46SMasahiro Yamada return desc; 217f141ed65SGrant Likely } 218f141ed65SGrant Likely 219f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 220f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 221f01d9075SAlexandre Courbot { 222f01d9075SAlexandre Courbot struct gpio_desc *desc; 223f01d9075SAlexandre Courbot 224f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 225f01d9075SAlexandre Courbot 226f01d9075SAlexandre Courbot if (IS_ERR(desc)) 227f01d9075SAlexandre Courbot return PTR_ERR(desc); 228f01d9075SAlexandre Courbot else 229f01d9075SAlexandre Courbot return desc_to_gpio(desc); 230f01d9075SAlexandre Courbot } 231f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 232f01d9075SAlexandre Courbot 233c8582339SLinus Walleij /* 234c8582339SLinus Walleij * The SPI GPIO bindings happened before we managed to establish that GPIO 235c8582339SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 236c8582339SLinus Walleij * them. 237c8582339SLinus Walleij */ 238c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, 239c8582339SLinus Walleij enum of_gpio_flags *of_flags) 240c8582339SLinus Walleij { 241c8582339SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 242c8582339SLinus Walleij struct device_node *np = dev->of_node; 243c8582339SLinus Walleij struct gpio_desc *desc; 244c8582339SLinus Walleij 245c8582339SLinus Walleij /* 246c8582339SLinus Walleij * Hopefully the compiler stubs the rest of the function if this 247c8582339SLinus Walleij * is false. 248c8582339SLinus Walleij */ 249c8582339SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 250c8582339SLinus Walleij return ERR_PTR(-ENOENT); 251c8582339SLinus Walleij 252c8582339SLinus Walleij /* Allow this specifically for "spi-gpio" devices */ 253c8582339SLinus Walleij if (!of_device_is_compatible(np, "spi-gpio") || !con_id) 254c8582339SLinus Walleij return ERR_PTR(-ENOENT); 255c8582339SLinus Walleij 256c8582339SLinus Walleij /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ 257c8582339SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); 258c8582339SLinus Walleij 259c8582339SLinus Walleij desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); 260c8582339SLinus Walleij return desc; 261c8582339SLinus Walleij } 262c8582339SLinus Walleij 2636a537d48SLinus Walleij /* 264e3023bf8SLinus Walleij * The old Freescale bindings use simply "gpios" as name for the chip select 265e3023bf8SLinus Walleij * lines rather than "cs-gpios" like all other SPI hardware. Account for this 266e3023bf8SLinus Walleij * with a special quirk. 267e3023bf8SLinus Walleij */ 268e3023bf8SLinus Walleij static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, 269e3023bf8SLinus Walleij const char *con_id, 270e3023bf8SLinus Walleij unsigned int idx, 271e3023bf8SLinus Walleij unsigned long *flags) 272e3023bf8SLinus Walleij { 273e3023bf8SLinus Walleij struct device_node *np = dev->of_node; 274e3023bf8SLinus Walleij 275e3023bf8SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 276e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 277e3023bf8SLinus Walleij 278e3023bf8SLinus Walleij /* Allow this specifically for Freescale devices */ 279e3023bf8SLinus Walleij if (!of_device_is_compatible(np, "fsl,spi") && 280e3023bf8SLinus Walleij !of_device_is_compatible(np, "aeroflexgaisler,spictrl")) 281e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 282e3023bf8SLinus Walleij /* Allow only if asking for "cs-gpios" */ 283e3023bf8SLinus Walleij if (!con_id || strcmp(con_id, "cs")) 284e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 285e3023bf8SLinus Walleij 286e3023bf8SLinus Walleij /* 287e3023bf8SLinus Walleij * While all other SPI controllers use "cs-gpios" the Freescale 288e3023bf8SLinus Walleij * uses just "gpios" so translate to that when "cs-gpios" is 289e3023bf8SLinus Walleij * requested. 290e3023bf8SLinus Walleij */ 291e3023bf8SLinus Walleij return of_find_gpio(dev, NULL, idx, flags); 292e3023bf8SLinus Walleij } 293e3023bf8SLinus Walleij 294e3023bf8SLinus Walleij /* 2956a537d48SLinus Walleij * Some regulator bindings happened before we managed to establish that GPIO 2966a537d48SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 2976a537d48SLinus Walleij * them. 2986a537d48SLinus Walleij */ 2996a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, 3006a537d48SLinus Walleij enum of_gpio_flags *of_flags) 3016a537d48SLinus Walleij { 3026a537d48SLinus Walleij /* These are the connection IDs we accept as legacy GPIO phandles */ 3036a537d48SLinus Walleij const char *whitelist[] = { 3046a537d48SLinus Walleij "wlf,ldoena", /* Arizona */ 3056a537d48SLinus Walleij "wlf,ldo1ena", /* WM8994 */ 3066a537d48SLinus Walleij "wlf,ldo2ena", /* WM8994 */ 3076a537d48SLinus Walleij }; 3086a537d48SLinus Walleij struct device_node *np = dev->of_node; 3096a537d48SLinus Walleij struct gpio_desc *desc; 3106a537d48SLinus Walleij int i; 3116a537d48SLinus Walleij 3126a537d48SLinus Walleij if (!IS_ENABLED(CONFIG_REGULATOR)) 3136a537d48SLinus Walleij return ERR_PTR(-ENOENT); 3146a537d48SLinus Walleij 3156a537d48SLinus Walleij if (!con_id) 3166a537d48SLinus Walleij return ERR_PTR(-ENOENT); 3176a537d48SLinus Walleij 3184b21f94aSAndy Shevchenko i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id); 3194b21f94aSAndy Shevchenko if (i < 0) 3206a537d48SLinus Walleij return ERR_PTR(-ENOENT); 3216a537d48SLinus Walleij 3226a537d48SLinus Walleij desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags); 3236a537d48SLinus Walleij return desc; 3246a537d48SLinus Walleij } 3256a537d48SLinus Walleij 326ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 327fed7026aSAndy Shevchenko unsigned int idx, unsigned long *flags) 328ea713bc4SLinus Walleij { 329ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 330ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 331ea713bc4SLinus Walleij struct gpio_desc *desc; 332ea713bc4SLinus Walleij unsigned int i; 333ea713bc4SLinus Walleij 334c8582339SLinus Walleij /* Try GPIO property "foo-gpios" and "foo-gpio" */ 335ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 336ea713bc4SLinus Walleij if (con_id) 337ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 338ea713bc4SLinus Walleij gpio_suffixes[i]); 339ea713bc4SLinus Walleij else 340ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 341ea713bc4SLinus Walleij gpio_suffixes[i]); 342ea713bc4SLinus Walleij 343ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 344ea713bc4SLinus Walleij &of_flags); 3456662ae6aSMaxime Ripard /* 3466662ae6aSMaxime Ripard * -EPROBE_DEFER in our case means that we found a 3476662ae6aSMaxime Ripard * valid GPIO property, but no controller has been 3486662ae6aSMaxime Ripard * registered so far. 3496662ae6aSMaxime Ripard * 3506662ae6aSMaxime Ripard * This means we don't need to look any further for 3516662ae6aSMaxime Ripard * alternate name conventions, and we should really 3526662ae6aSMaxime Ripard * preserve the return code for our user to be able to 3536662ae6aSMaxime Ripard * retry probing later. 3546662ae6aSMaxime Ripard */ 3556662ae6aSMaxime Ripard if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER) 3566662ae6aSMaxime Ripard return desc; 3576662ae6aSMaxime Ripard 358ea713bc4SLinus Walleij if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) 359ea713bc4SLinus Walleij break; 360ea713bc4SLinus Walleij } 361ea713bc4SLinus Walleij 362c8582339SLinus Walleij /* Special handling for SPI GPIOs if used */ 363c8582339SLinus Walleij if (IS_ERR(desc)) 364c8582339SLinus Walleij desc = of_find_spi_gpio(dev, con_id, &of_flags); 365e3023bf8SLinus Walleij if (IS_ERR(desc)) { 366e3023bf8SLinus Walleij /* This quirk looks up flags and all */ 367e3023bf8SLinus Walleij desc = of_find_spi_cs_gpio(dev, con_id, idx, flags); 368e3023bf8SLinus Walleij if (!IS_ERR(desc)) 369e3023bf8SLinus Walleij return desc; 370e3023bf8SLinus Walleij } 371c8582339SLinus Walleij 3726a537d48SLinus Walleij /* Special handling for regulator GPIOs if used */ 373ce27fb2cSChen-Yu Tsai if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) 3746a537d48SLinus Walleij desc = of_find_regulator_gpio(dev, con_id, &of_flags); 3756a537d48SLinus Walleij 376ea713bc4SLinus Walleij if (IS_ERR(desc)) 377ea713bc4SLinus Walleij return desc; 378ea713bc4SLinus Walleij 379ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 380ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 381ea713bc4SLinus Walleij 382ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 3834c0facddSLaxman Dewangan if (of_flags & OF_GPIO_OPEN_DRAIN) 384ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 385ea713bc4SLinus Walleij else 386ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 387ea713bc4SLinus Walleij } 388ea713bc4SLinus Walleij 389e10f72bfSAndrew Jeffery if (of_flags & OF_GPIO_TRANSITORY) 390e10f72bfSAndrew Jeffery *flags |= GPIO_TRANSITORY; 39105f479bfSCharles Keepax 392d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_UP) 393d449991cSThomas Petazzoni *flags |= GPIO_PULL_UP; 394d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_DOWN) 395d449991cSThomas Petazzoni *flags |= GPIO_PULL_DOWN; 396d449991cSThomas Petazzoni 397ea713bc4SLinus Walleij return desc; 398ea713bc4SLinus Walleij } 399ea713bc4SLinus Walleij 400f141ed65SGrant Likely /** 401fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 402f625d460SBenoit Parrot * @np: device node to get GPIO from 403be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 404a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 405f625d460SBenoit Parrot * @name: GPIO line name 406fed7026aSAndy Shevchenko * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 407fed7026aSAndy Shevchenko * of_find_gpio() or of_parse_own_gpio() 408f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 409f625d460SBenoit Parrot * 410f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 411f625d460SBenoit Parrot * value on the error condition. 412f625d460SBenoit Parrot */ 413fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 414be715343SMasahiro Yamada struct gpio_chip *chip, 415a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 416fed7026aSAndy Shevchenko unsigned long *lflags, 417f625d460SBenoit Parrot enum gpiod_flags *dflags) 418f625d460SBenoit Parrot { 419f625d460SBenoit Parrot struct device_node *chip_np; 420f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 421be715343SMasahiro Yamada struct of_phandle_args gpiospec; 422be715343SMasahiro Yamada struct gpio_desc *desc; 423a79fead5SGeert Uytterhoeven unsigned int i; 424f625d460SBenoit Parrot u32 tmp; 4253f9547e1SMasahiro Yamada int ret; 426f625d460SBenoit Parrot 427be715343SMasahiro Yamada chip_np = chip->of_node; 428f625d460SBenoit Parrot if (!chip_np) 429f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 430f625d460SBenoit Parrot 431f625d460SBenoit Parrot xlate_flags = 0; 4322d6c06f5SAndy Shevchenko *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 433f625d460SBenoit Parrot *dflags = 0; 434f625d460SBenoit Parrot 435f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 436f625d460SBenoit Parrot if (ret) 437f625d460SBenoit Parrot return ERR_PTR(ret); 438f625d460SBenoit Parrot 439be715343SMasahiro Yamada gpiospec.np = chip_np; 440be715343SMasahiro Yamada gpiospec.args_count = tmp; 441f625d460SBenoit Parrot 442a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 443a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 444a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 445f625d460SBenoit Parrot if (ret) 446f625d460SBenoit Parrot return ERR_PTR(ret); 447a79fead5SGeert Uytterhoeven } 448f625d460SBenoit Parrot 44999468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 450be715343SMasahiro Yamada if (IS_ERR(desc)) 451be715343SMasahiro Yamada return desc; 452f625d460SBenoit Parrot 453f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 454f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 455e10f72bfSAndrew Jeffery if (xlate_flags & OF_GPIO_TRANSITORY) 456e10f72bfSAndrew Jeffery *lflags |= GPIO_TRANSITORY; 457f625d460SBenoit Parrot 458f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 459f625d460SBenoit Parrot *dflags |= GPIOD_IN; 460f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 461f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 462f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 463f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 464f625d460SBenoit Parrot else { 46562cdcb6cSRob Herring pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 46662cdcb6cSRob Herring desc_to_gpio(desc), np); 467f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 468f625d460SBenoit Parrot } 469f625d460SBenoit Parrot 470f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 471f625d460SBenoit Parrot *name = np->name; 472f625d460SBenoit Parrot 473be715343SMasahiro Yamada return desc; 474f625d460SBenoit Parrot } 475f625d460SBenoit Parrot 476f625d460SBenoit Parrot /** 477fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 478f625d460SBenoit Parrot * @chip: gpio chip to act on 479f625d460SBenoit Parrot * 480f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 481f625d460SBenoit Parrot * configuration. 482ead066e6SGeert Uytterhoeven * It returns error if it fails otherwise 0 on success. 483f625d460SBenoit Parrot */ 484dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 485f625d460SBenoit Parrot { 486f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 487f625d460SBenoit Parrot struct device_node *np; 488f625d460SBenoit Parrot const char *name; 489fed7026aSAndy Shevchenko unsigned long lflags; 490f625d460SBenoit Parrot enum gpiod_flags dflags; 491a79fead5SGeert Uytterhoeven unsigned int i; 492dfbd379bSLaxman Dewangan int ret; 493f625d460SBenoit Parrot 494d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 495f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 496f625d460SBenoit Parrot continue; 497f625d460SBenoit Parrot 498a79fead5SGeert Uytterhoeven for (i = 0;; i++) { 499a79fead5SGeert Uytterhoeven desc = of_parse_own_gpio(np, chip, i, &name, &lflags, 500a79fead5SGeert Uytterhoeven &dflags); 501f625d460SBenoit Parrot if (IS_ERR(desc)) 502a79fead5SGeert Uytterhoeven break; 503f625d460SBenoit Parrot 504dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 50509e258afSWei Yongjun if (ret < 0) { 50609e258afSWei Yongjun of_node_put(np); 507dfbd379bSLaxman Dewangan return ret; 508f625d460SBenoit Parrot } 50909e258afSWei Yongjun } 510a79fead5SGeert Uytterhoeven } 511dfbd379bSLaxman Dewangan 512dfbd379bSLaxman Dewangan return 0; 513f625d460SBenoit Parrot } 514f625d460SBenoit Parrot 515f625d460SBenoit Parrot /** 51667049c50SThierry Reding * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags 517f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 51867049c50SThierry Reding * @gpiospec: GPIO specifier as found in the device tree 519f141ed65SGrant Likely * @flags: a flags pointer to fill in 520f141ed65SGrant Likely * 521f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 52267049c50SThierry Reding * GPIO chips. This function performs only one sanity check: whether GPIO 523f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 524f141ed65SGrant Likely */ 525f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 526f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 527f141ed65SGrant Likely { 528f141ed65SGrant Likely /* 529f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 53020a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 531f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 532f141ed65SGrant Likely * but not recommended). 533f141ed65SGrant Likely */ 534f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 535f141ed65SGrant Likely WARN_ON(1); 536f141ed65SGrant Likely return -EINVAL; 537f141ed65SGrant Likely } 538f141ed65SGrant Likely 539f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 540f141ed65SGrant Likely return -EINVAL; 541f141ed65SGrant Likely 5427b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 543f141ed65SGrant Likely return -EINVAL; 544f141ed65SGrant Likely 545f141ed65SGrant Likely if (flags) 546f141ed65SGrant Likely *flags = gpiospec->args[1]; 547f141ed65SGrant Likely 548f141ed65SGrant Likely return gpiospec->args[0]; 549f141ed65SGrant Likely } 550f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 551f141ed65SGrant Likely 552f141ed65SGrant Likely /** 5533208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 554f141ed65SGrant Likely * @np: device node of the GPIO chip 555f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 5563208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 557f141ed65SGrant Likely * 558f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 559f141ed65SGrant Likely * 560f141ed65SGrant Likely * 1) In the gpio_chip structure: 561f141ed65SGrant Likely * - all the callbacks 562f141ed65SGrant Likely * - of_gpio_n_cells 563f141ed65SGrant Likely * - of_xlate callback (optional) 564f141ed65SGrant Likely * 565f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 566f141ed65SGrant Likely * - save_regs callback (optional) 567f141ed65SGrant Likely * 568f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 569f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 570f141ed65SGrant Likely * to manage GPIOs from the callbacks. 571f141ed65SGrant Likely */ 5723208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 5733208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 5743208b0f0SLinus Walleij void *data) 575f141ed65SGrant Likely { 576f141ed65SGrant Likely int ret = -ENOMEM; 577f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 578f141ed65SGrant Likely 5797eb6ce2fSRob Herring gc->label = kasprintf(GFP_KERNEL, "%pOF", np); 580f141ed65SGrant Likely if (!gc->label) 581f141ed65SGrant Likely goto err0; 582f141ed65SGrant Likely 583f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 584f141ed65SGrant Likely if (!mm_gc->regs) 585f141ed65SGrant Likely goto err1; 586f141ed65SGrant Likely 587f141ed65SGrant Likely gc->base = -1; 588f141ed65SGrant Likely 589f141ed65SGrant Likely if (mm_gc->save_regs) 590f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 591f141ed65SGrant Likely 592f141ed65SGrant Likely mm_gc->gc.of_node = np; 593f141ed65SGrant Likely 5943208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 595f141ed65SGrant Likely if (ret) 596f141ed65SGrant Likely goto err2; 597f141ed65SGrant Likely 598f141ed65SGrant Likely return 0; 599f141ed65SGrant Likely err2: 600f141ed65SGrant Likely iounmap(mm_gc->regs); 601f141ed65SGrant Likely err1: 602f141ed65SGrant Likely kfree(gc->label); 603f141ed65SGrant Likely err0: 6047eb6ce2fSRob Herring pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); 605f141ed65SGrant Likely return ret; 606f141ed65SGrant Likely } 6073208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data); 608f141ed65SGrant Likely 609d621e8baSRicardo Ribalda Delgado /** 610d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 611d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 612d621e8baSRicardo Ribalda Delgado */ 613d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 614d621e8baSRicardo Ribalda Delgado { 615d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 616d621e8baSRicardo Ribalda Delgado 617d621e8baSRicardo Ribalda Delgado if (!mm_gc) 618d621e8baSRicardo Ribalda Delgado return; 619d621e8baSRicardo Ribalda Delgado 620d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 621d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 622d621e8baSRicardo Ribalda Delgado kfree(gc->label); 623d621e8baSRicardo Ribalda Delgado } 624d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove); 625d621e8baSRicardo Ribalda Delgado 626726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) 627726cb3baSStephen Boyd { 628726cb3baSStephen Boyd int len, i; 629726cb3baSStephen Boyd u32 start, count; 630726cb3baSStephen Boyd struct device_node *np = chip->of_node; 631726cb3baSStephen Boyd 632726cb3baSStephen Boyd len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 633726cb3baSStephen Boyd if (len < 0 || len % 2 != 0) 634726cb3baSStephen Boyd return; 635726cb3baSStephen Boyd 636726cb3baSStephen Boyd for (i = 0; i < len; i += 2) { 637726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 638726cb3baSStephen Boyd i, &start); 639726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 640726cb3baSStephen Boyd i + 1, &count); 641726cb3baSStephen Boyd if (start >= chip->ngpio || start + count >= chip->ngpio) 642726cb3baSStephen Boyd continue; 643726cb3baSStephen Boyd 644726cb3baSStephen Boyd bitmap_clear(chip->valid_mask, start, count); 645726cb3baSStephen Boyd } 646726cb3baSStephen Boyd }; 647726cb3baSStephen Boyd 648f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 64928355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 650f23f1516SShiraz Hashim { 651f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 652f23f1516SShiraz Hashim struct of_phandle_args pinspec; 6531e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 654f23f1516SShiraz Hashim int index = 0, ret; 655586a87e6SChristian Ruppert const char *name; 656586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 657586a87e6SChristian Ruppert struct property *group_names; 658f23f1516SShiraz Hashim 659f23f1516SShiraz Hashim if (!np) 66028355f81STomeu Vizoso return 0; 661f23f1516SShiraz Hashim 662586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 663586a87e6SChristian Ruppert 664ad4e1a7cSHaojian Zhuang for (;; index++) { 665d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 666d9fe0039SStephen Warren index, &pinspec); 667f23f1516SShiraz Hashim if (ret) 668f23f1516SShiraz Hashim break; 669f23f1516SShiraz Hashim 6701e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 671602cf638SMasahiro Yamada of_node_put(pinspec.np); 6721e63d7b9SLinus Walleij if (!pctldev) 67328355f81STomeu Vizoso return -EPROBE_DEFER; 674f23f1516SShiraz Hashim 675586a87e6SChristian Ruppert if (pinspec.args[2]) { 676586a87e6SChristian Ruppert if (group_names) { 67772858602SLaurent Navet of_property_read_string_index(np, 678586a87e6SChristian Ruppert group_names_propname, 679586a87e6SChristian Ruppert index, &name); 680586a87e6SChristian Ruppert if (strlen(name)) { 6817eb6ce2fSRob Herring pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 6827eb6ce2fSRob Herring np); 683586a87e6SChristian Ruppert break; 684586a87e6SChristian Ruppert } 685586a87e6SChristian Ruppert } 686586a87e6SChristian Ruppert /* npins != 0: linear range */ 6871e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 688ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 6891e63d7b9SLinus Walleij pinspec.args[0], 69086853c83SHaojian Zhuang pinspec.args[1], 69186853c83SHaojian Zhuang pinspec.args[2]); 6921e63d7b9SLinus Walleij if (ret) 69328355f81STomeu Vizoso return ret; 694586a87e6SChristian Ruppert } else { 695586a87e6SChristian Ruppert /* npins == 0: special range */ 696586a87e6SChristian Ruppert if (pinspec.args[1]) { 6977eb6ce2fSRob Herring pr_err("%pOF: Illegal gpio-range format.\n", 6987eb6ce2fSRob Herring np); 699586a87e6SChristian Ruppert break; 700586a87e6SChristian Ruppert } 701586a87e6SChristian Ruppert 702586a87e6SChristian Ruppert if (!group_names) { 7037eb6ce2fSRob Herring pr_err("%pOF: GPIO group range requested but no %s property.\n", 7047eb6ce2fSRob Herring np, group_names_propname); 705586a87e6SChristian Ruppert break; 706586a87e6SChristian Ruppert } 707586a87e6SChristian Ruppert 708586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 709586a87e6SChristian Ruppert group_names_propname, 710586a87e6SChristian Ruppert index, &name); 711586a87e6SChristian Ruppert if (ret) 712586a87e6SChristian Ruppert break; 713586a87e6SChristian Ruppert 714586a87e6SChristian Ruppert if (!strlen(name)) { 7157eb6ce2fSRob Herring pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 7167eb6ce2fSRob Herring np); 717586a87e6SChristian Ruppert break; 718586a87e6SChristian Ruppert } 719586a87e6SChristian Ruppert 720586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 721586a87e6SChristian Ruppert pinspec.args[0], name); 722586a87e6SChristian Ruppert if (ret) 72328355f81STomeu Vizoso return ret; 724586a87e6SChristian Ruppert } 725ad4e1a7cSHaojian Zhuang } 72628355f81STomeu Vizoso 72728355f81STomeu Vizoso return 0; 728f23f1516SShiraz Hashim } 729f23f1516SShiraz Hashim 730f23f1516SShiraz Hashim #else 73128355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 732f23f1516SShiraz Hashim #endif 733f23f1516SShiraz Hashim 73428355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 735f141ed65SGrant Likely { 73628355f81STomeu Vizoso int status; 73728355f81STomeu Vizoso 738f141ed65SGrant Likely if (!chip->of_node) 73928355f81STomeu Vizoso return 0; 740f141ed65SGrant Likely 741f141ed65SGrant Likely if (!chip->of_xlate) { 742f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 743f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 744f141ed65SGrant Likely } 745f141ed65SGrant Likely 7461020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 7471020dfd1SMasahiro Yamada return -EINVAL; 7481020dfd1SMasahiro Yamada 749726cb3baSStephen Boyd of_gpiochip_init_valid_mask(chip); 750726cb3baSStephen Boyd 75128355f81STomeu Vizoso status = of_gpiochip_add_pin_range(chip); 75228355f81STomeu Vizoso if (status) 75328355f81STomeu Vizoso return status; 75428355f81STomeu Vizoso 755fd9c5531SLinus Walleij /* If the chip defines names itself, these take precedence */ 756fd9c5531SLinus Walleij if (!chip->names) 75782270335SChristophe Leroy devprop_gpiochip_set_names(chip, 75882270335SChristophe Leroy of_fwnode_handle(chip->of_node)); 759fd9c5531SLinus Walleij 760f141ed65SGrant Likely of_node_get(chip->of_node); 761f625d460SBenoit Parrot 762f7299d44SGeert Uytterhoeven status = of_gpiochip_scan_gpios(chip); 763f7299d44SGeert Uytterhoeven if (status) { 764f7299d44SGeert Uytterhoeven of_node_put(chip->of_node); 765f7299d44SGeert Uytterhoeven gpiochip_remove_pin_ranges(chip); 766f7299d44SGeert Uytterhoeven } 767f7299d44SGeert Uytterhoeven 768f7299d44SGeert Uytterhoeven return status; 769f141ed65SGrant Likely } 770f141ed65SGrant Likely 771f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 772f141ed65SGrant Likely { 773e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 774f141ed65SGrant Likely of_node_put(chip->of_node); 775f141ed65SGrant Likely } 776