127038c3eSVladimir Zapolskiy // SPDX-License-Identifier: GPL-2.0+ 2f141ed65SGrant Likely /* 3f141ed65SGrant Likely * OF helpers for the GPIO API 4f141ed65SGrant Likely * 5f141ed65SGrant Likely * Copyright (c) 2007-2008 MontaVista Software, Inc. 6f141ed65SGrant Likely * 7f141ed65SGrant Likely * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 8f141ed65SGrant Likely */ 9f141ed65SGrant Likely 10f141ed65SGrant Likely #include <linux/device.h> 11bea4dbeeSSachin Kamat #include <linux/err.h> 12f141ed65SGrant Likely #include <linux/errno.h> 13f141ed65SGrant Likely #include <linux/module.h> 14f141ed65SGrant Likely #include <linux/io.h> 15af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h> 16f141ed65SGrant Likely #include <linux/of.h> 17f141ed65SGrant Likely #include <linux/of_address.h> 18f141ed65SGrant Likely #include <linux/of_gpio.h> 19f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h> 20f141ed65SGrant Likely #include <linux/slab.h> 21f625d460SBenoit Parrot #include <linux/gpio/machine.h> 22f141ed65SGrant Likely 231bd6b601SAlexandre Courbot #include "gpiolib.h" 24f626d6dfSLinus Walleij #include "gpiolib-of.h" 25f626d6dfSLinus Walleij 2671b8f600SLinus Walleij /** 2771b8f600SLinus Walleij * of_gpio_spi_cs_get_count() - special GPIO counting for SPI 2871b8f600SLinus Walleij * Some elder GPIO controllers need special quirks. Currently we handle 2971b8f600SLinus Walleij * the Freescale GPIO controller with bindings that doesn't use the 3071b8f600SLinus Walleij * established "cs-gpios" for chip selects but instead rely on 3171b8f600SLinus Walleij * "gpios" for the chip select lines. If we detect this, we redirect 3271b8f600SLinus Walleij * the counting of "cs-gpios" to count "gpios" transparent to the 3371b8f600SLinus Walleij * driver. 3471b8f600SLinus Walleij */ 35a1f4c96bSYueHaibing static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) 3671b8f600SLinus Walleij { 3771b8f600SLinus Walleij struct device_node *np = dev->of_node; 3871b8f600SLinus Walleij 3971b8f600SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 4071b8f600SLinus Walleij return 0; 4171b8f600SLinus Walleij if (!con_id || strcmp(con_id, "cs")) 4271b8f600SLinus Walleij return 0; 4371b8f600SLinus Walleij if (!of_device_is_compatible(np, "fsl,spi") && 4471b8f600SLinus Walleij !of_device_is_compatible(np, "aeroflexgaisler,spictrl")) 4571b8f600SLinus Walleij return 0; 4671b8f600SLinus Walleij return of_gpio_named_count(np, "gpios"); 4771b8f600SLinus Walleij } 4871b8f600SLinus Walleij 49f626d6dfSLinus Walleij /* 50f626d6dfSLinus Walleij * This is used by external users of of_gpio_count() from <linux/of_gpio.h> 51f626d6dfSLinus Walleij * 52f626d6dfSLinus Walleij * FIXME: get rid of those external users by converting them to GPIO 53f626d6dfSLinus Walleij * descriptors and let them all use gpiod_get_count() 54f626d6dfSLinus Walleij */ 55f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id) 56f626d6dfSLinus Walleij { 57f626d6dfSLinus Walleij int ret; 58f626d6dfSLinus Walleij char propname[32]; 59f626d6dfSLinus Walleij unsigned int i; 60f626d6dfSLinus Walleij 6171b8f600SLinus Walleij ret = of_gpio_spi_cs_get_count(dev, con_id); 6271b8f600SLinus Walleij if (ret > 0) 6371b8f600SLinus Walleij return ret; 6471b8f600SLinus Walleij 65f626d6dfSLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 66f626d6dfSLinus Walleij if (con_id) 67f626d6dfSLinus Walleij snprintf(propname, sizeof(propname), "%s-%s", 68f626d6dfSLinus Walleij con_id, gpio_suffixes[i]); 69f626d6dfSLinus Walleij else 70f626d6dfSLinus Walleij snprintf(propname, sizeof(propname), "%s", 71f626d6dfSLinus Walleij gpio_suffixes[i]); 72f626d6dfSLinus Walleij 73f626d6dfSLinus Walleij ret = of_gpio_named_count(dev->of_node, propname); 74f626d6dfSLinus Walleij if (ret > 0) 75f626d6dfSLinus Walleij break; 76f626d6dfSLinus Walleij } 77f626d6dfSLinus Walleij return ret ? ret : -ENOENT; 78f626d6dfSLinus Walleij } 79af8b6375SAlexandre Courbot 80c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) 813d0f7cf0SGrant Likely { 82c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec = data; 83c7e9d398SMasahiro Yamada 84c7e9d398SMasahiro Yamada return chip->gpiodev->dev.of_node == gpiospec->np && 85d49b48f0SVincent Whitchurch chip->of_xlate && 86c7e9d398SMasahiro Yamada chip->of_xlate(chip, gpiospec, NULL) >= 0; 877b8792bbSHans Holmberg } 883d0f7cf0SGrant Likely 89c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate( 90c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec) 91762c2e46SMasahiro Yamada { 92c7e9d398SMasahiro Yamada return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); 933d0f7cf0SGrant Likely } 943d0f7cf0SGrant Likely 9599468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, 9699468c1aSMasahiro Yamada struct of_phandle_args *gpiospec, 9799468c1aSMasahiro Yamada enum of_gpio_flags *flags) 9899468c1aSMasahiro Yamada { 9999468c1aSMasahiro Yamada int ret; 10099468c1aSMasahiro Yamada 10199468c1aSMasahiro Yamada if (chip->of_gpio_n_cells != gpiospec->args_count) 10299468c1aSMasahiro Yamada return ERR_PTR(-EINVAL); 10399468c1aSMasahiro Yamada 10499468c1aSMasahiro Yamada ret = chip->of_xlate(chip, gpiospec, flags); 10599468c1aSMasahiro Yamada if (ret < 0) 10699468c1aSMasahiro Yamada return ERR_PTR(ret); 10799468c1aSMasahiro Yamada 10899468c1aSMasahiro Yamada return gpiochip_get_desc(chip, ret); 109f141ed65SGrant Likely } 110f141ed65SGrant Likely 111f626d6dfSLinus Walleij /** 112f626d6dfSLinus Walleij * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs 113f626d6dfSLinus Walleij * to set the .valid_mask 114f626d6dfSLinus Walleij * @dev: the device for the GPIO provider 115f626d6dfSLinus Walleij * @return: true if the valid mask needs to be set 116f626d6dfSLinus Walleij */ 11749281a22SStephen Boyd bool of_gpio_need_valid_mask(const struct gpio_chip *gc) 118f626d6dfSLinus Walleij { 119f626d6dfSLinus Walleij int size; 120f626d6dfSLinus Walleij struct device_node *np = gc->of_node; 121f626d6dfSLinus Walleij 122f626d6dfSLinus Walleij size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 123f626d6dfSLinus Walleij if (size > 0 && size % 2 == 0) 124f626d6dfSLinus Walleij return true; 125f626d6dfSLinus Walleij return false; 126f626d6dfSLinus Walleij } 127f626d6dfSLinus Walleij 128a603a2b8SLinus Walleij static void of_gpio_flags_quirks(struct device_node *np, 12989a5e15bSLinus Walleij const char *propname, 1306953c57aSLinus Walleij enum of_gpio_flags *flags, 1316953c57aSLinus Walleij int index) 132a603a2b8SLinus Walleij { 133a603a2b8SLinus Walleij /* 13481c85ec1SLinus Walleij * Handle MMC "cd-inverted" and "wp-inverted" semantics. 13581c85ec1SLinus Walleij */ 13681c85ec1SLinus Walleij if (IS_ENABLED(CONFIG_MMC)) { 13781c85ec1SLinus Walleij /* 13881c85ec1SLinus Walleij * Active low is the default according to the 13989a5e15bSLinus Walleij * SDHCI specification and the device tree 14089a5e15bSLinus Walleij * bindings. However the code in the current 14189a5e15bSLinus Walleij * kernel was written such that the phandle 14289a5e15bSLinus Walleij * flags were always respected, and "cd-inverted" 14389a5e15bSLinus Walleij * would invert the flag from the device phandle. 14481c85ec1SLinus Walleij */ 14589a5e15bSLinus Walleij if (!strcmp(propname, "cd-gpios")) { 14689a5e15bSLinus Walleij if (of_property_read_bool(np, "cd-inverted")) 14789a5e15bSLinus Walleij *flags ^= OF_GPIO_ACTIVE_LOW; 14881c85ec1SLinus Walleij } 14989a5e15bSLinus Walleij if (!strcmp(propname, "wp-gpios")) { 15089a5e15bSLinus Walleij if (of_property_read_bool(np, "wp-inverted")) 15189a5e15bSLinus Walleij *flags ^= OF_GPIO_ACTIVE_LOW; 15281c85ec1SLinus Walleij } 15381c85ec1SLinus Walleij } 15481c85ec1SLinus Walleij /* 155a603a2b8SLinus Walleij * Some GPIO fixed regulator quirks. 156a603a2b8SLinus Walleij * Note that active low is the default. 157a603a2b8SLinus Walleij */ 158a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 159906402a4SLinus Walleij (of_device_is_compatible(np, "regulator-fixed") || 160906402a4SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") || 161a71a81e7SGeert Uytterhoeven (!(strcmp(propname, "enable-gpio") && 162a71a81e7SGeert Uytterhoeven strcmp(propname, "enable-gpios")) && 163a71a81e7SGeert Uytterhoeven of_device_is_compatible(np, "regulator-gpio")))) { 164a603a2b8SLinus Walleij /* 165a603a2b8SLinus Walleij * The regulator GPIO handles are specified such that the 166a603a2b8SLinus Walleij * presence or absence of "enable-active-high" solely controls 167a603a2b8SLinus Walleij * the polarity of the GPIO line. Any phandle flags must 168a603a2b8SLinus Walleij * be actively ignored. 169a603a2b8SLinus Walleij */ 170a603a2b8SLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 171a603a2b8SLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 172a603a2b8SLinus Walleij of_node_full_name(np)); 173a603a2b8SLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 174a603a2b8SLinus Walleij } 175a603a2b8SLinus Walleij if (!of_property_read_bool(np, "enable-active-high")) 176a603a2b8SLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 177a603a2b8SLinus Walleij } 178a603a2b8SLinus Walleij /* 179a603a2b8SLinus Walleij * Legacy open drain handling for fixed voltage regulators. 180a603a2b8SLinus Walleij */ 181a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 182a603a2b8SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") && 183a603a2b8SLinus Walleij of_property_read_bool(np, "gpio-open-drain")) { 184a603a2b8SLinus Walleij *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); 185a603a2b8SLinus Walleij pr_info("%s uses legacy open drain flag - update the DTS if you can\n", 186a603a2b8SLinus Walleij of_node_full_name(np)); 187a603a2b8SLinus Walleij } 1886953c57aSLinus Walleij 1896953c57aSLinus Walleij /* 1906953c57aSLinus Walleij * Legacy handling of SPI active high chip select. If we have a 1916953c57aSLinus Walleij * property named "cs-gpios" we need to inspect the child node 1926953c57aSLinus Walleij * to determine if the flags should have inverted semantics. 1936953c57aSLinus Walleij */ 194da7f1349SLinus Walleij if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && 195a71a81e7SGeert Uytterhoeven of_property_read_bool(np, "cs-gpios")) { 1966953c57aSLinus Walleij struct device_node *child; 1976953c57aSLinus Walleij u32 cs; 1986953c57aSLinus Walleij int ret; 1996953c57aSLinus Walleij 2006953c57aSLinus Walleij for_each_child_of_node(np, child) { 2016953c57aSLinus Walleij ret = of_property_read_u32(child, "reg", &cs); 202c1c04ceaSLinus Walleij if (ret) 2036953c57aSLinus Walleij continue; 2046953c57aSLinus Walleij if (cs == index) { 2056953c57aSLinus Walleij /* 2066953c57aSLinus Walleij * SPI children have active low chip selects 2076953c57aSLinus Walleij * by default. This can be specified negatively 2086953c57aSLinus Walleij * by just omitting "spi-cs-high" in the 2096953c57aSLinus Walleij * device node, or actively by tagging on 2106953c57aSLinus Walleij * GPIO_ACTIVE_LOW as flag in the device 2116953c57aSLinus Walleij * tree. If the line is simultaneously 2126953c57aSLinus Walleij * tagged as active low in the device tree 2136953c57aSLinus Walleij * and has the "spi-cs-high" set, we get a 2146953c57aSLinus Walleij * conflict and the "spi-cs-high" flag will 2156953c57aSLinus Walleij * take precedence. 2166953c57aSLinus Walleij */ 2177ce40277SAndrey Smirnov if (of_property_read_bool(child, "spi-cs-high")) { 2186953c57aSLinus Walleij if (*flags & OF_GPIO_ACTIVE_LOW) { 2196953c57aSLinus Walleij pr_warn("%s GPIO handle specifies active low - ignored\n", 2207ce40277SAndrey Smirnov of_node_full_name(child)); 2216953c57aSLinus Walleij *flags &= ~OF_GPIO_ACTIVE_LOW; 2226953c57aSLinus Walleij } 2236953c57aSLinus Walleij } else { 2246953c57aSLinus Walleij if (!(*flags & OF_GPIO_ACTIVE_LOW)) 2256953c57aSLinus Walleij pr_info("%s enforce active low on chipselect handle\n", 2267ce40277SAndrey Smirnov of_node_full_name(child)); 2276953c57aSLinus Walleij *flags |= OF_GPIO_ACTIVE_LOW; 2286953c57aSLinus Walleij } 22989fea04cSNishka Dasgupta of_node_put(child); 2306953c57aSLinus Walleij break; 2316953c57aSLinus Walleij } 2326953c57aSLinus Walleij } 2336953c57aSLinus Walleij } 234edc1ef3fSMartin Blumenstingl 235edc1ef3fSMartin Blumenstingl /* Legacy handling of stmmac's active-low PHY reset line */ 236edc1ef3fSMartin Blumenstingl if (IS_ENABLED(CONFIG_STMMAC_ETH) && 237edc1ef3fSMartin Blumenstingl !strcmp(propname, "snps,reset-gpio") && 238edc1ef3fSMartin Blumenstingl of_property_read_bool(np, "snps,reset-active-low")) 239edc1ef3fSMartin Blumenstingl *flags |= OF_GPIO_ACTIVE_LOW; 240a603a2b8SLinus Walleij } 241a603a2b8SLinus Walleij 242f141ed65SGrant Likely /** 243af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 244f141ed65SGrant Likely * @np: device node to get GPIO from 245f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 246f141ed65SGrant Likely * @index: index of the GPIO 247f141ed65SGrant Likely * @flags: a flags pointer to fill in 248f141ed65SGrant Likely * 249af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 250f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 251f141ed65SGrant Likely * in flags for the GPIO. 252f141ed65SGrant Likely */ 253c83d3c77SGeert Uytterhoeven static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 254af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 255f141ed65SGrant Likely { 256762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 257762c2e46SMasahiro Yamada struct gpio_chip *chip; 258762c2e46SMasahiro Yamada struct gpio_desc *desc; 259f141ed65SGrant Likely int ret; 260f141ed65SGrant Likely 261c11e6f0fSStephen Boyd ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 262762c2e46SMasahiro Yamada &gpiospec); 2633d0f7cf0SGrant Likely if (ret) { 2647eb6ce2fSRob Herring pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 2657eb6ce2fSRob Herring __func__, propname, np, index); 266af8b6375SAlexandre Courbot return ERR_PTR(ret); 2673d0f7cf0SGrant Likely } 268f141ed65SGrant Likely 269c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 270762c2e46SMasahiro Yamada if (!chip) { 271762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 272762c2e46SMasahiro Yamada goto out; 273762c2e46SMasahiro Yamada } 2743d0f7cf0SGrant Likely 27599468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 276762c2e46SMasahiro Yamada if (IS_ERR(desc)) 277762c2e46SMasahiro Yamada goto out; 278762c2e46SMasahiro Yamada 279605f2d34SLinus Walleij if (flags) 28089a5e15bSLinus Walleij of_gpio_flags_quirks(np, propname, flags, index); 281a603a2b8SLinus Walleij 2827eb6ce2fSRob Herring pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 2837eb6ce2fSRob Herring __func__, propname, np, index, 284762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 285762c2e46SMasahiro Yamada 286762c2e46SMasahiro Yamada out: 287762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 288762c2e46SMasahiro Yamada 289762c2e46SMasahiro Yamada return desc; 290f141ed65SGrant Likely } 291f141ed65SGrant Likely 292f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 293f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 294f01d9075SAlexandre Courbot { 295f01d9075SAlexandre Courbot struct gpio_desc *desc; 296f01d9075SAlexandre Courbot 297f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 298f01d9075SAlexandre Courbot 299f01d9075SAlexandre Courbot if (IS_ERR(desc)) 300f01d9075SAlexandre Courbot return PTR_ERR(desc); 301f01d9075SAlexandre Courbot else 302f01d9075SAlexandre Courbot return desc_to_gpio(desc); 303f01d9075SAlexandre Courbot } 3046d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags); 305f01d9075SAlexandre Courbot 306f626d6dfSLinus Walleij /** 307f626d6dfSLinus Walleij * gpiod_get_from_of_node() - obtain a GPIO from an OF node 308f626d6dfSLinus Walleij * @node: handle of the OF node 309f626d6dfSLinus Walleij * @propname: name of the DT property representing the GPIO 310f626d6dfSLinus Walleij * @index: index of the GPIO to obtain for the consumer 311f626d6dfSLinus Walleij * @dflags: GPIO initialization flags 312f626d6dfSLinus Walleij * @label: label to attach to the requested GPIO 313f626d6dfSLinus Walleij * 314f626d6dfSLinus Walleij * Returns: 315f626d6dfSLinus Walleij * On successful request the GPIO pin is configured in accordance with 316f626d6dfSLinus Walleij * provided @dflags. 317f626d6dfSLinus Walleij * 318f626d6dfSLinus Walleij * In case of error an ERR_PTR() is returned. 319f626d6dfSLinus Walleij */ 320f626d6dfSLinus Walleij struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, 321f626d6dfSLinus Walleij const char *propname, int index, 322f626d6dfSLinus Walleij enum gpiod_flags dflags, 323f626d6dfSLinus Walleij const char *label) 324f626d6dfSLinus Walleij { 325f626d6dfSLinus Walleij unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 326f626d6dfSLinus Walleij struct gpio_desc *desc; 327f626d6dfSLinus Walleij enum of_gpio_flags flags; 328f626d6dfSLinus Walleij bool active_low = false; 329f626d6dfSLinus Walleij bool single_ended = false; 330f626d6dfSLinus Walleij bool open_drain = false; 331f626d6dfSLinus Walleij bool transitory = false; 332f626d6dfSLinus Walleij int ret; 333f626d6dfSLinus Walleij 334f626d6dfSLinus Walleij desc = of_get_named_gpiod_flags(node, propname, 335f626d6dfSLinus Walleij index, &flags); 336f626d6dfSLinus Walleij 337f626d6dfSLinus Walleij if (!desc || IS_ERR(desc)) { 338f626d6dfSLinus Walleij return desc; 339f626d6dfSLinus Walleij } 340f626d6dfSLinus Walleij 341f626d6dfSLinus Walleij active_low = flags & OF_GPIO_ACTIVE_LOW; 342f626d6dfSLinus Walleij single_ended = flags & OF_GPIO_SINGLE_ENDED; 343f626d6dfSLinus Walleij open_drain = flags & OF_GPIO_OPEN_DRAIN; 344f626d6dfSLinus Walleij transitory = flags & OF_GPIO_TRANSITORY; 345f626d6dfSLinus Walleij 346f626d6dfSLinus Walleij ret = gpiod_request(desc, label); 347f626d6dfSLinus Walleij if (ret == -EBUSY && (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 348f626d6dfSLinus Walleij return desc; 349f626d6dfSLinus Walleij if (ret) 350f626d6dfSLinus Walleij return ERR_PTR(ret); 351f626d6dfSLinus Walleij 352f626d6dfSLinus Walleij if (active_low) 353f626d6dfSLinus Walleij lflags |= GPIO_ACTIVE_LOW; 354f626d6dfSLinus Walleij 355f626d6dfSLinus Walleij if (single_ended) { 356f626d6dfSLinus Walleij if (open_drain) 357f626d6dfSLinus Walleij lflags |= GPIO_OPEN_DRAIN; 358f626d6dfSLinus Walleij else 359f626d6dfSLinus Walleij lflags |= GPIO_OPEN_SOURCE; 360f626d6dfSLinus Walleij } 361f626d6dfSLinus Walleij 362f626d6dfSLinus Walleij if (transitory) 363f626d6dfSLinus Walleij lflags |= GPIO_TRANSITORY; 364f626d6dfSLinus Walleij 365f626d6dfSLinus Walleij ret = gpiod_configure_flags(desc, propname, lflags, dflags); 366f626d6dfSLinus Walleij if (ret < 0) { 367f626d6dfSLinus Walleij gpiod_put(desc); 368f626d6dfSLinus Walleij return ERR_PTR(ret); 369f626d6dfSLinus Walleij } 370f626d6dfSLinus Walleij 371f626d6dfSLinus Walleij return desc; 372f626d6dfSLinus Walleij } 3736d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node); 374f141ed65SGrant Likely 375c8582339SLinus Walleij /* 376c8582339SLinus Walleij * The SPI GPIO bindings happened before we managed to establish that GPIO 377c8582339SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 378c8582339SLinus Walleij * them. 379c8582339SLinus Walleij */ 380c8582339SLinus Walleij static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, 381c8582339SLinus Walleij enum of_gpio_flags *of_flags) 382c8582339SLinus Walleij { 383c8582339SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 384c8582339SLinus Walleij struct device_node *np = dev->of_node; 385c8582339SLinus Walleij struct gpio_desc *desc; 386c8582339SLinus Walleij 387c8582339SLinus Walleij /* 388c8582339SLinus Walleij * Hopefully the compiler stubs the rest of the function if this 389c8582339SLinus Walleij * is false. 390c8582339SLinus Walleij */ 391c8582339SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 392c8582339SLinus Walleij return ERR_PTR(-ENOENT); 393c8582339SLinus Walleij 394c8582339SLinus Walleij /* Allow this specifically for "spi-gpio" devices */ 395c8582339SLinus Walleij if (!of_device_is_compatible(np, "spi-gpio") || !con_id) 396c8582339SLinus Walleij return ERR_PTR(-ENOENT); 397c8582339SLinus Walleij 398c8582339SLinus Walleij /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ 399c8582339SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); 400c8582339SLinus Walleij 401c8582339SLinus Walleij desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); 402c8582339SLinus Walleij return desc; 403c8582339SLinus Walleij } 404c8582339SLinus Walleij 4056a537d48SLinus Walleij /* 406e3023bf8SLinus Walleij * The old Freescale bindings use simply "gpios" as name for the chip select 407e3023bf8SLinus Walleij * lines rather than "cs-gpios" like all other SPI hardware. Account for this 408e3023bf8SLinus Walleij * with a special quirk. 409e3023bf8SLinus Walleij */ 410e3023bf8SLinus Walleij static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, 411e3023bf8SLinus Walleij const char *con_id, 412e3023bf8SLinus Walleij unsigned int idx, 413e3023bf8SLinus Walleij unsigned long *flags) 414e3023bf8SLinus Walleij { 415e3023bf8SLinus Walleij struct device_node *np = dev->of_node; 416e3023bf8SLinus Walleij 417e3023bf8SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 418e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 419e3023bf8SLinus Walleij 420e3023bf8SLinus Walleij /* Allow this specifically for Freescale devices */ 421e3023bf8SLinus Walleij if (!of_device_is_compatible(np, "fsl,spi") && 422e3023bf8SLinus Walleij !of_device_is_compatible(np, "aeroflexgaisler,spictrl")) 423e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 424e3023bf8SLinus Walleij /* Allow only if asking for "cs-gpios" */ 425e3023bf8SLinus Walleij if (!con_id || strcmp(con_id, "cs")) 426e3023bf8SLinus Walleij return ERR_PTR(-ENOENT); 427e3023bf8SLinus Walleij 428e3023bf8SLinus Walleij /* 429e3023bf8SLinus Walleij * While all other SPI controllers use "cs-gpios" the Freescale 430e3023bf8SLinus Walleij * uses just "gpios" so translate to that when "cs-gpios" is 431e3023bf8SLinus Walleij * requested. 432e3023bf8SLinus Walleij */ 433e3023bf8SLinus Walleij return of_find_gpio(dev, NULL, idx, flags); 434e3023bf8SLinus Walleij } 435e3023bf8SLinus Walleij 436e3023bf8SLinus Walleij /* 4376a537d48SLinus Walleij * Some regulator bindings happened before we managed to establish that GPIO 4386a537d48SLinus Walleij * properties should be named "foo-gpios" so we have this special kludge for 4396a537d48SLinus Walleij * them. 4406a537d48SLinus Walleij */ 4416a537d48SLinus Walleij static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, 4426a537d48SLinus Walleij enum of_gpio_flags *of_flags) 4436a537d48SLinus Walleij { 4446a537d48SLinus Walleij /* These are the connection IDs we accept as legacy GPIO phandles */ 4456a537d48SLinus Walleij const char *whitelist[] = { 4466a537d48SLinus Walleij "wlf,ldoena", /* Arizona */ 4476a537d48SLinus Walleij "wlf,ldo1ena", /* WM8994 */ 4486a537d48SLinus Walleij "wlf,ldo2ena", /* WM8994 */ 4496a537d48SLinus Walleij }; 4506a537d48SLinus Walleij struct device_node *np = dev->of_node; 4516a537d48SLinus Walleij struct gpio_desc *desc; 4526a537d48SLinus Walleij int i; 4536a537d48SLinus Walleij 4546a537d48SLinus Walleij if (!IS_ENABLED(CONFIG_REGULATOR)) 4556a537d48SLinus Walleij return ERR_PTR(-ENOENT); 4566a537d48SLinus Walleij 4576a537d48SLinus Walleij if (!con_id) 4586a537d48SLinus Walleij return ERR_PTR(-ENOENT); 4596a537d48SLinus Walleij 4604b21f94aSAndy Shevchenko i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id); 4614b21f94aSAndy Shevchenko if (i < 0) 4626a537d48SLinus Walleij return ERR_PTR(-ENOENT); 4636a537d48SLinus Walleij 4646a537d48SLinus Walleij desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags); 4656a537d48SLinus Walleij return desc; 4666a537d48SLinus Walleij } 4676a537d48SLinus Walleij 46811c43bb0SDmitry Torokhov static struct gpio_desc *of_find_arizona_gpio(struct device *dev, 46911c43bb0SDmitry Torokhov const char *con_id, 47011c43bb0SDmitry Torokhov enum of_gpio_flags *of_flags) 47111c43bb0SDmitry Torokhov { 47211c43bb0SDmitry Torokhov if (!IS_ENABLED(CONFIG_MFD_ARIZONA)) 47311c43bb0SDmitry Torokhov return ERR_PTR(-ENOENT); 47411c43bb0SDmitry Torokhov 47511c43bb0SDmitry Torokhov if (!con_id || strcmp(con_id, "wlf,reset")) 47611c43bb0SDmitry Torokhov return ERR_PTR(-ENOENT); 47711c43bb0SDmitry Torokhov 47811c43bb0SDmitry Torokhov return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags); 47911c43bb0SDmitry Torokhov } 48011c43bb0SDmitry Torokhov 481ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 482fed7026aSAndy Shevchenko unsigned int idx, unsigned long *flags) 483ea713bc4SLinus Walleij { 484ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 485ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 486ea713bc4SLinus Walleij struct gpio_desc *desc; 487ea713bc4SLinus Walleij unsigned int i; 488ea713bc4SLinus Walleij 489c8582339SLinus Walleij /* Try GPIO property "foo-gpios" and "foo-gpio" */ 490ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 491ea713bc4SLinus Walleij if (con_id) 492ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 493ea713bc4SLinus Walleij gpio_suffixes[i]); 494ea713bc4SLinus Walleij else 495ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 496ea713bc4SLinus Walleij gpio_suffixes[i]); 497ea713bc4SLinus Walleij 498ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 499ea713bc4SLinus Walleij &of_flags); 5006662ae6aSMaxime Ripard 5011dea33e8SDmitry Torokhov if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT) 502ea713bc4SLinus Walleij break; 503ea713bc4SLinus Walleij } 504ea713bc4SLinus Walleij 5051dea33e8SDmitry Torokhov if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) { 506c8582339SLinus Walleij /* Special handling for SPI GPIOs if used */ 507c8582339SLinus Walleij desc = of_find_spi_gpio(dev, con_id, &of_flags); 5081dea33e8SDmitry Torokhov } 5091dea33e8SDmitry Torokhov 5101dea33e8SDmitry Torokhov if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) { 511e3023bf8SLinus Walleij /* This quirk looks up flags and all */ 512e3023bf8SLinus Walleij desc = of_find_spi_cs_gpio(dev, con_id, idx, flags); 513e3023bf8SLinus Walleij if (!IS_ERR(desc)) 514e3023bf8SLinus Walleij return desc; 515e3023bf8SLinus Walleij } 516c8582339SLinus Walleij 5171dea33e8SDmitry Torokhov if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) { 5186a537d48SLinus Walleij /* Special handling for regulator GPIOs if used */ 5196a537d48SLinus Walleij desc = of_find_regulator_gpio(dev, con_id, &of_flags); 5201dea33e8SDmitry Torokhov } 5216a537d48SLinus Walleij 52211c43bb0SDmitry Torokhov if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) 52311c43bb0SDmitry Torokhov desc = of_find_arizona_gpio(dev, con_id, &of_flags); 52411c43bb0SDmitry Torokhov 525ea713bc4SLinus Walleij if (IS_ERR(desc)) 526ea713bc4SLinus Walleij return desc; 527ea713bc4SLinus Walleij 528ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 529ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 530ea713bc4SLinus Walleij 531ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 5324c0facddSLaxman Dewangan if (of_flags & OF_GPIO_OPEN_DRAIN) 533ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 534ea713bc4SLinus Walleij else 535ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 536ea713bc4SLinus Walleij } 537ea713bc4SLinus Walleij 538e10f72bfSAndrew Jeffery if (of_flags & OF_GPIO_TRANSITORY) 539e10f72bfSAndrew Jeffery *flags |= GPIO_TRANSITORY; 54005f479bfSCharles Keepax 541d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_UP) 542d449991cSThomas Petazzoni *flags |= GPIO_PULL_UP; 543d449991cSThomas Petazzoni if (of_flags & OF_GPIO_PULL_DOWN) 544d449991cSThomas Petazzoni *flags |= GPIO_PULL_DOWN; 545d449991cSThomas Petazzoni 546ea713bc4SLinus Walleij return desc; 547ea713bc4SLinus Walleij } 548ea713bc4SLinus Walleij 549f141ed65SGrant Likely /** 550fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 551f625d460SBenoit Parrot * @np: device node to get GPIO from 552be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 553a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 554f625d460SBenoit Parrot * @name: GPIO line name 555fed7026aSAndy Shevchenko * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 556fed7026aSAndy Shevchenko * of_find_gpio() or of_parse_own_gpio() 557f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 558f625d460SBenoit Parrot * 559f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 560f625d460SBenoit Parrot * value on the error condition. 561f625d460SBenoit Parrot */ 562fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 563be715343SMasahiro Yamada struct gpio_chip *chip, 564a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 565fed7026aSAndy Shevchenko unsigned long *lflags, 566f625d460SBenoit Parrot enum gpiod_flags *dflags) 567f625d460SBenoit Parrot { 568f625d460SBenoit Parrot struct device_node *chip_np; 569f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 570be715343SMasahiro Yamada struct of_phandle_args gpiospec; 571be715343SMasahiro Yamada struct gpio_desc *desc; 572a79fead5SGeert Uytterhoeven unsigned int i; 573f625d460SBenoit Parrot u32 tmp; 5743f9547e1SMasahiro Yamada int ret; 575f625d460SBenoit Parrot 576be715343SMasahiro Yamada chip_np = chip->of_node; 577f625d460SBenoit Parrot if (!chip_np) 578f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 579f625d460SBenoit Parrot 580f625d460SBenoit Parrot xlate_flags = 0; 5812d6c06f5SAndy Shevchenko *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 582f625d460SBenoit Parrot *dflags = 0; 583f625d460SBenoit Parrot 584f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 585f625d460SBenoit Parrot if (ret) 586f625d460SBenoit Parrot return ERR_PTR(ret); 587f625d460SBenoit Parrot 588be715343SMasahiro Yamada gpiospec.np = chip_np; 589be715343SMasahiro Yamada gpiospec.args_count = tmp; 590f625d460SBenoit Parrot 591a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 592a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 593a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 594f625d460SBenoit Parrot if (ret) 595f625d460SBenoit Parrot return ERR_PTR(ret); 596a79fead5SGeert Uytterhoeven } 597f625d460SBenoit Parrot 59899468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 599be715343SMasahiro Yamada if (IS_ERR(desc)) 600be715343SMasahiro Yamada return desc; 601f625d460SBenoit Parrot 602f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 603f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 604e10f72bfSAndrew Jeffery if (xlate_flags & OF_GPIO_TRANSITORY) 605e10f72bfSAndrew Jeffery *lflags |= GPIO_TRANSITORY; 606f625d460SBenoit Parrot 607f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 608f625d460SBenoit Parrot *dflags |= GPIOD_IN; 609f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 610f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 611f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 612f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 613f625d460SBenoit Parrot else { 61462cdcb6cSRob Herring pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 61562cdcb6cSRob Herring desc_to_gpio(desc), np); 616f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 617f625d460SBenoit Parrot } 618f625d460SBenoit Parrot 619f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 620f625d460SBenoit Parrot *name = np->name; 621f625d460SBenoit Parrot 622be715343SMasahiro Yamada return desc; 623f625d460SBenoit Parrot } 624f625d460SBenoit Parrot 625f625d460SBenoit Parrot /** 626fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 627f625d460SBenoit Parrot * @chip: gpio chip to act on 628f625d460SBenoit Parrot * 629f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 630f625d460SBenoit Parrot * configuration. 631ead066e6SGeert Uytterhoeven * It returns error if it fails otherwise 0 on success. 632f625d460SBenoit Parrot */ 633dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 634f625d460SBenoit Parrot { 635f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 636f625d460SBenoit Parrot struct device_node *np; 637f625d460SBenoit Parrot const char *name; 638fed7026aSAndy Shevchenko unsigned long lflags; 639f625d460SBenoit Parrot enum gpiod_flags dflags; 640a79fead5SGeert Uytterhoeven unsigned int i; 641dfbd379bSLaxman Dewangan int ret; 642f625d460SBenoit Parrot 643d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 644f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 645f625d460SBenoit Parrot continue; 646f625d460SBenoit Parrot 647a79fead5SGeert Uytterhoeven for (i = 0;; i++) { 648a79fead5SGeert Uytterhoeven desc = of_parse_own_gpio(np, chip, i, &name, &lflags, 649a79fead5SGeert Uytterhoeven &dflags); 650f625d460SBenoit Parrot if (IS_ERR(desc)) 651a79fead5SGeert Uytterhoeven break; 652f625d460SBenoit Parrot 653dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 65409e258afSWei Yongjun if (ret < 0) { 65509e258afSWei Yongjun of_node_put(np); 656dfbd379bSLaxman Dewangan return ret; 657f625d460SBenoit Parrot } 65809e258afSWei Yongjun } 659a79fead5SGeert Uytterhoeven } 660dfbd379bSLaxman Dewangan 661dfbd379bSLaxman Dewangan return 0; 662f625d460SBenoit Parrot } 663f625d460SBenoit Parrot 664f625d460SBenoit Parrot /** 66567049c50SThierry Reding * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags 666f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 66767049c50SThierry Reding * @gpiospec: GPIO specifier as found in the device tree 668f141ed65SGrant Likely * @flags: a flags pointer to fill in 669f141ed65SGrant Likely * 670f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 67167049c50SThierry Reding * GPIO chips. This function performs only one sanity check: whether GPIO 672f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 673f141ed65SGrant Likely */ 674b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc, 675b0c7e73bSGeert Uytterhoeven const struct of_phandle_args *gpiospec, 676b0c7e73bSGeert Uytterhoeven u32 *flags) 677f141ed65SGrant Likely { 678f141ed65SGrant Likely /* 679f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 68020a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 681f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 682f141ed65SGrant Likely * but not recommended). 683f141ed65SGrant Likely */ 684f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 685f141ed65SGrant Likely WARN_ON(1); 686f141ed65SGrant Likely return -EINVAL; 687f141ed65SGrant Likely } 688f141ed65SGrant Likely 689f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 690f141ed65SGrant Likely return -EINVAL; 691f141ed65SGrant Likely 6927b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 693f141ed65SGrant Likely return -EINVAL; 694f141ed65SGrant Likely 695f141ed65SGrant Likely if (flags) 696f141ed65SGrant Likely *flags = gpiospec->args[1]; 697f141ed65SGrant Likely 698f141ed65SGrant Likely return gpiospec->args[0]; 699f141ed65SGrant Likely } 700f141ed65SGrant Likely 701f141ed65SGrant Likely /** 7023208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 703f141ed65SGrant Likely * @np: device node of the GPIO chip 704f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 7053208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 706f141ed65SGrant Likely * 707f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 708f141ed65SGrant Likely * 709f141ed65SGrant Likely * 1) In the gpio_chip structure: 710f141ed65SGrant Likely * - all the callbacks 711f141ed65SGrant Likely * - of_gpio_n_cells 712f141ed65SGrant Likely * - of_xlate callback (optional) 713f141ed65SGrant Likely * 714f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 715f141ed65SGrant Likely * - save_regs callback (optional) 716f141ed65SGrant Likely * 717f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 718f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 719f141ed65SGrant Likely * to manage GPIOs from the callbacks. 720f141ed65SGrant Likely */ 7213208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 7223208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 7233208b0f0SLinus Walleij void *data) 724f141ed65SGrant Likely { 725f141ed65SGrant Likely int ret = -ENOMEM; 726f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 727f141ed65SGrant Likely 7287eb6ce2fSRob Herring gc->label = kasprintf(GFP_KERNEL, "%pOF", np); 729f141ed65SGrant Likely if (!gc->label) 730f141ed65SGrant Likely goto err0; 731f141ed65SGrant Likely 732f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 733f141ed65SGrant Likely if (!mm_gc->regs) 734f141ed65SGrant Likely goto err1; 735f141ed65SGrant Likely 736f141ed65SGrant Likely gc->base = -1; 737f141ed65SGrant Likely 738f141ed65SGrant Likely if (mm_gc->save_regs) 739f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 740f141ed65SGrant Likely 741f141ed65SGrant Likely mm_gc->gc.of_node = np; 742f141ed65SGrant Likely 7433208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 744f141ed65SGrant Likely if (ret) 745f141ed65SGrant Likely goto err2; 746f141ed65SGrant Likely 747f141ed65SGrant Likely return 0; 748f141ed65SGrant Likely err2: 749f141ed65SGrant Likely iounmap(mm_gc->regs); 750f141ed65SGrant Likely err1: 751f141ed65SGrant Likely kfree(gc->label); 752f141ed65SGrant Likely err0: 7537eb6ce2fSRob Herring pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); 754f141ed65SGrant Likely return ret; 755f141ed65SGrant Likely } 7566d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); 757f141ed65SGrant Likely 758d621e8baSRicardo Ribalda Delgado /** 759d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 760d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 761d621e8baSRicardo Ribalda Delgado */ 762d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 763d621e8baSRicardo Ribalda Delgado { 764d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 765d621e8baSRicardo Ribalda Delgado 766d621e8baSRicardo Ribalda Delgado if (!mm_gc) 767d621e8baSRicardo Ribalda Delgado return; 768d621e8baSRicardo Ribalda Delgado 769d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 770d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 771d621e8baSRicardo Ribalda Delgado kfree(gc->label); 772d621e8baSRicardo Ribalda Delgado } 7736d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); 774d621e8baSRicardo Ribalda Delgado 775726cb3baSStephen Boyd static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) 776726cb3baSStephen Boyd { 777726cb3baSStephen Boyd int len, i; 778726cb3baSStephen Boyd u32 start, count; 779726cb3baSStephen Boyd struct device_node *np = chip->of_node; 780726cb3baSStephen Boyd 781726cb3baSStephen Boyd len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 782726cb3baSStephen Boyd if (len < 0 || len % 2 != 0) 783726cb3baSStephen Boyd return; 784726cb3baSStephen Boyd 785726cb3baSStephen Boyd for (i = 0; i < len; i += 2) { 786726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 787726cb3baSStephen Boyd i, &start); 788726cb3baSStephen Boyd of_property_read_u32_index(np, "gpio-reserved-ranges", 789726cb3baSStephen Boyd i + 1, &count); 790726cb3baSStephen Boyd if (start >= chip->ngpio || start + count >= chip->ngpio) 791726cb3baSStephen Boyd continue; 792726cb3baSStephen Boyd 793726cb3baSStephen Boyd bitmap_clear(chip->valid_mask, start, count); 794726cb3baSStephen Boyd } 795726cb3baSStephen Boyd }; 796726cb3baSStephen Boyd 797f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 79828355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 799f23f1516SShiraz Hashim { 800f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 801f23f1516SShiraz Hashim struct of_phandle_args pinspec; 8021e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 803f23f1516SShiraz Hashim int index = 0, ret; 804586a87e6SChristian Ruppert const char *name; 805586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 806586a87e6SChristian Ruppert struct property *group_names; 807f23f1516SShiraz Hashim 808f23f1516SShiraz Hashim if (!np) 80928355f81STomeu Vizoso return 0; 810f23f1516SShiraz Hashim 811586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 812586a87e6SChristian Ruppert 813ad4e1a7cSHaojian Zhuang for (;; index++) { 814d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 815d9fe0039SStephen Warren index, &pinspec); 816f23f1516SShiraz Hashim if (ret) 817f23f1516SShiraz Hashim break; 818f23f1516SShiraz Hashim 8191e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 820602cf638SMasahiro Yamada of_node_put(pinspec.np); 8211e63d7b9SLinus Walleij if (!pctldev) 82228355f81STomeu Vizoso return -EPROBE_DEFER; 823f23f1516SShiraz Hashim 824586a87e6SChristian Ruppert if (pinspec.args[2]) { 825586a87e6SChristian Ruppert if (group_names) { 82672858602SLaurent Navet of_property_read_string_index(np, 827586a87e6SChristian Ruppert group_names_propname, 828586a87e6SChristian Ruppert index, &name); 829586a87e6SChristian Ruppert if (strlen(name)) { 8307eb6ce2fSRob Herring pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 8317eb6ce2fSRob Herring np); 832586a87e6SChristian Ruppert break; 833586a87e6SChristian Ruppert } 834586a87e6SChristian Ruppert } 835586a87e6SChristian Ruppert /* npins != 0: linear range */ 8361e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 837ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 8381e63d7b9SLinus Walleij pinspec.args[0], 83986853c83SHaojian Zhuang pinspec.args[1], 84086853c83SHaojian Zhuang pinspec.args[2]); 8411e63d7b9SLinus Walleij if (ret) 84228355f81STomeu Vizoso return ret; 843586a87e6SChristian Ruppert } else { 844586a87e6SChristian Ruppert /* npins == 0: special range */ 845586a87e6SChristian Ruppert if (pinspec.args[1]) { 8467eb6ce2fSRob Herring pr_err("%pOF: Illegal gpio-range format.\n", 8477eb6ce2fSRob Herring np); 848586a87e6SChristian Ruppert break; 849586a87e6SChristian Ruppert } 850586a87e6SChristian Ruppert 851586a87e6SChristian Ruppert if (!group_names) { 8527eb6ce2fSRob Herring pr_err("%pOF: GPIO group range requested but no %s property.\n", 8537eb6ce2fSRob Herring np, group_names_propname); 854586a87e6SChristian Ruppert break; 855586a87e6SChristian Ruppert } 856586a87e6SChristian Ruppert 857586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 858586a87e6SChristian Ruppert group_names_propname, 859586a87e6SChristian Ruppert index, &name); 860586a87e6SChristian Ruppert if (ret) 861586a87e6SChristian Ruppert break; 862586a87e6SChristian Ruppert 863586a87e6SChristian Ruppert if (!strlen(name)) { 8647eb6ce2fSRob Herring pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 8657eb6ce2fSRob Herring np); 866586a87e6SChristian Ruppert break; 867586a87e6SChristian Ruppert } 868586a87e6SChristian Ruppert 869586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 870586a87e6SChristian Ruppert pinspec.args[0], name); 871586a87e6SChristian Ruppert if (ret) 87228355f81STomeu Vizoso return ret; 873586a87e6SChristian Ruppert } 874ad4e1a7cSHaojian Zhuang } 87528355f81STomeu Vizoso 87628355f81STomeu Vizoso return 0; 877f23f1516SShiraz Hashim } 878f23f1516SShiraz Hashim 879f23f1516SShiraz Hashim #else 88028355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 881f23f1516SShiraz Hashim #endif 882f23f1516SShiraz Hashim 88328355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 884f141ed65SGrant Likely { 885f0d1ab05SLinus Walleij int ret; 88628355f81STomeu Vizoso 887f141ed65SGrant Likely if (!chip->of_node) 88828355f81STomeu Vizoso return 0; 889f141ed65SGrant Likely 890f141ed65SGrant Likely if (!chip->of_xlate) { 891f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 892f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 893f141ed65SGrant Likely } 894f141ed65SGrant Likely 8951020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 8961020dfd1SMasahiro Yamada return -EINVAL; 8971020dfd1SMasahiro Yamada 898726cb3baSStephen Boyd of_gpiochip_init_valid_mask(chip); 899726cb3baSStephen Boyd 900f0d1ab05SLinus Walleij ret = of_gpiochip_add_pin_range(chip); 901f0d1ab05SLinus Walleij if (ret) 902f0d1ab05SLinus Walleij return ret; 90328355f81STomeu Vizoso 904fd9c5531SLinus Walleij /* If the chip defines names itself, these take precedence */ 905fd9c5531SLinus Walleij if (!chip->names) 90682270335SChristophe Leroy devprop_gpiochip_set_names(chip, 90782270335SChristophe Leroy of_fwnode_handle(chip->of_node)); 908fd9c5531SLinus Walleij 909f141ed65SGrant Likely of_node_get(chip->of_node); 910f625d460SBenoit Parrot 911f0d1ab05SLinus Walleij ret = of_gpiochip_scan_gpios(chip); 912f0d1ab05SLinus Walleij if (ret) { 913f7299d44SGeert Uytterhoeven of_node_put(chip->of_node); 914f7299d44SGeert Uytterhoeven gpiochip_remove_pin_ranges(chip); 915f7299d44SGeert Uytterhoeven } 916f7299d44SGeert Uytterhoeven 917f0d1ab05SLinus Walleij return ret; 918f141ed65SGrant Likely } 919f141ed65SGrant Likely 920f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 921f141ed65SGrant Likely { 922e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 923f141ed65SGrant Likely of_node_put(chip->of_node); 924f141ed65SGrant Likely } 925