127038c3eSVladimir Zapolskiy // SPDX-License-Identifier: GPL-2.0+ 2f141ed65SGrant Likely /* 3f141ed65SGrant Likely * OF helpers for the GPIO API 4f141ed65SGrant Likely * 5f141ed65SGrant Likely * Copyright (c) 2007-2008 MontaVista Software, Inc. 6f141ed65SGrant Likely * 7f141ed65SGrant Likely * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 8f141ed65SGrant Likely */ 9f141ed65SGrant Likely 10f141ed65SGrant Likely #include <linux/device.h> 11bea4dbeeSSachin Kamat #include <linux/err.h> 12f141ed65SGrant Likely #include <linux/errno.h> 13f141ed65SGrant Likely #include <linux/module.h> 14f141ed65SGrant Likely #include <linux/io.h> 15af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h> 16f141ed65SGrant Likely #include <linux/of.h> 17f141ed65SGrant Likely #include <linux/of_address.h> 18f141ed65SGrant Likely #include <linux/of_gpio.h> 19f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h> 20f141ed65SGrant Likely #include <linux/slab.h> 21f625d460SBenoit Parrot #include <linux/gpio/machine.h> 22f141ed65SGrant Likely 231bd6b601SAlexandre Courbot #include "gpiolib.h" 24f626d6dfSLinus Walleij #include "gpiolib-of.h" 25f626d6dfSLinus Walleij 2671b8f600SLinus Walleij /** 2771b8f600SLinus Walleij * of_gpio_spi_cs_get_count() - special GPIO counting for SPI 28c5a66b97SLee Jones * @dev: Consuming device 29c5a66b97SLee Jones * @con_id: Function within the GPIO consumer 30c5a66b97SLee Jones * 3171b8f600SLinus Walleij * Some elder GPIO controllers need special quirks. Currently we handle 3247267732SLinus Walleij * the Freescale and PPC GPIO controller with bindings that doesn't use the 3371b8f600SLinus Walleij * established "cs-gpios" for chip selects but instead rely on 3471b8f600SLinus Walleij * "gpios" for the chip select lines. If we detect this, we redirect 3571b8f600SLinus Walleij * the counting of "cs-gpios" to count "gpios" transparent to the 3671b8f600SLinus Walleij * driver. 3771b8f600SLinus Walleij */ 38a1f4c96bSYueHaibing static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) 3971b8f600SLinus Walleij { 4071b8f600SLinus Walleij struct device_node *np = dev->of_node; 4171b8f600SLinus Walleij 4271b8f600SLinus Walleij if (!IS_ENABLED(CONFIG_SPI_MASTER)) 4371b8f600SLinus Walleij return 0; 4471b8f600SLinus Walleij if (!con_id || strcmp(con_id, "cs")) 4571b8f600SLinus Walleij return 0; 4671b8f600SLinus Walleij if (!of_device_is_compatible(np, "fsl,spi") && 4747267732SLinus Walleij !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && 4847267732SLinus Walleij !of_device_is_compatible(np, "ibm,ppc4xx-spi")) 4971b8f600SLinus Walleij return 0; 5071b8f600SLinus Walleij return of_gpio_named_count(np, "gpios"); 5171b8f600SLinus Walleij } 5271b8f600SLinus Walleij 53f626d6dfSLinus Walleij /* 54f626d6dfSLinus Walleij * This is used by external users of of_gpio_count() from <linux/of_gpio.h> 55f626d6dfSLinus Walleij * 56f626d6dfSLinus Walleij * FIXME: get rid of those external users by converting them to GPIO 57808b9931SGeert Uytterhoeven * descriptors and let them all use gpiod_count() 58f626d6dfSLinus Walleij */ 59f626d6dfSLinus Walleij int of_gpio_get_count(struct device *dev, const char *con_id) 60f626d6dfSLinus Walleij { 61f626d6dfSLinus Walleij int ret; 62f626d6dfSLinus Walleij char propname[32]; 63f626d6dfSLinus Walleij unsigned int i; 64f626d6dfSLinus Walleij 6571b8f600SLinus Walleij ret = of_gpio_spi_cs_get_count(dev, con_id); 6671b8f600SLinus Walleij if (ret > 0) 6771b8f600SLinus Walleij return ret; 6871b8f600SLinus Walleij 69f626d6dfSLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 70f626d6dfSLinus Walleij if (con_id) 71f626d6dfSLinus Walleij snprintf(propname, sizeof(propname), "%s-%s", 72f626d6dfSLinus Walleij con_id, gpio_suffixes[i]); 73f626d6dfSLinus Walleij else 74f626d6dfSLinus Walleij snprintf(propname, sizeof(propname), "%s", 75f626d6dfSLinus Walleij gpio_suffixes[i]); 76f626d6dfSLinus Walleij 77f626d6dfSLinus Walleij ret = of_gpio_named_count(dev->of_node, propname); 78f626d6dfSLinus Walleij if (ret > 0) 79f626d6dfSLinus Walleij break; 80f626d6dfSLinus Walleij } 81f626d6dfSLinus Walleij return ret ? ret : -ENOENT; 82f626d6dfSLinus Walleij } 83af8b6375SAlexandre Courbot 84c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) 853d0f7cf0SGrant Likely { 86c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec = data; 87c7e9d398SMasahiro Yamada 88d59fdbc7SAndy Shevchenko return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) && 89d49b48f0SVincent Whitchurch chip->of_xlate && 90c7e9d398SMasahiro Yamada chip->of_xlate(chip, gpiospec, NULL) >= 0; 917b8792bbSHans Holmberg } 923d0f7cf0SGrant Likely 93c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate( 94c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec) 95762c2e46SMasahiro Yamada { 96c7e9d398SMasahiro Yamada return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); 973d0f7cf0SGrant Likely } 983d0f7cf0SGrant Likely 9999468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, 10099468c1aSMasahiro Yamada struct of_phandle_args *gpiospec, 10199468c1aSMasahiro Yamada enum of_gpio_flags *flags) 10299468c1aSMasahiro Yamada { 10399468c1aSMasahiro Yamada int ret; 10499468c1aSMasahiro Yamada 10599468c1aSMasahiro Yamada if (chip->of_gpio_n_cells != gpiospec->args_count) 10699468c1aSMasahiro Yamada return ERR_PTR(-EINVAL); 10799468c1aSMasahiro Yamada 10899468c1aSMasahiro Yamada ret = chip->of_xlate(chip, gpiospec, flags); 10999468c1aSMasahiro Yamada if (ret < 0) 11099468c1aSMasahiro Yamada return ERR_PTR(ret); 11199468c1aSMasahiro Yamada 11299468c1aSMasahiro Yamada return gpiochip_get_desc(chip, ret); 113f141ed65SGrant Likely } 114f141ed65SGrant Likely 115e3186e36SDmitry Torokhov /* 116e3186e36SDmitry Torokhov * Overrides stated polarity of a gpio line and warns when there is a 117e3186e36SDmitry Torokhov * discrepancy. 118e3186e36SDmitry Torokhov */ 119e3186e36SDmitry Torokhov static void of_gpio_quirk_polarity(const struct device_node *np, 120e3186e36SDmitry Torokhov bool active_high, 121e3186e36SDmitry Torokhov enum of_gpio_flags *flags) 122e3186e36SDmitry Torokhov { 123e3186e36SDmitry Torokhov if (active_high) { 124e3186e36SDmitry Torokhov if (*flags & OF_GPIO_ACTIVE_LOW) { 125e3186e36SDmitry Torokhov pr_warn("%s GPIO handle specifies active low - ignored\n", 126e3186e36SDmitry Torokhov of_node_full_name(np)); 127e3186e36SDmitry Torokhov *flags &= ~OF_GPIO_ACTIVE_LOW; 128e3186e36SDmitry Torokhov } 129e3186e36SDmitry Torokhov } else { 130e3186e36SDmitry Torokhov if (!(*flags & OF_GPIO_ACTIVE_LOW)) 131e3186e36SDmitry Torokhov pr_info("%s enforce active low on GPIO handle\n", 132e3186e36SDmitry Torokhov of_node_full_name(np)); 133e3186e36SDmitry Torokhov *flags |= OF_GPIO_ACTIVE_LOW; 134e3186e36SDmitry Torokhov } 135e3186e36SDmitry Torokhov } 136e3186e36SDmitry Torokhov 13799d18d42SDmitry Torokhov /* 13899d18d42SDmitry Torokhov * This quirk does static polarity overrides in cases where existing 13999d18d42SDmitry Torokhov * DTS specified incorrect polarity. 14099d18d42SDmitry Torokhov */ 14199d18d42SDmitry Torokhov static void of_gpio_try_fixup_polarity(const struct device_node *np, 14299d18d42SDmitry Torokhov const char *propname, 14399d18d42SDmitry Torokhov enum of_gpio_flags *flags) 14499d18d42SDmitry Torokhov { 14599d18d42SDmitry Torokhov static const struct { 14699d18d42SDmitry Torokhov const char *compatible; 14799d18d42SDmitry Torokhov const char *propname; 14899d18d42SDmitry Torokhov bool active_high; 14999d18d42SDmitry Torokhov } gpios[] = { 15099d18d42SDmitry Torokhov #if !IS_ENABLED(CONFIG_LCD_HX8357) 15199d18d42SDmitry Torokhov /* 15299d18d42SDmitry Torokhov * Himax LCD controllers used incorrectly named 15399d18d42SDmitry Torokhov * "gpios-reset" property and also specified wrong 15499d18d42SDmitry Torokhov * polarity. 15599d18d42SDmitry Torokhov */ 15699d18d42SDmitry Torokhov { "himax,hx8357", "gpios-reset", false }, 15799d18d42SDmitry Torokhov { "himax,hx8369", "gpios-reset", false }, 15899d18d42SDmitry Torokhov #endif 15999d18d42SDmitry Torokhov }; 16099d18d42SDmitry Torokhov unsigned int i; 16199d18d42SDmitry Torokhov 16299d18d42SDmitry Torokhov for (i = 0; i < ARRAY_SIZE(gpios); i++) { 16399d18d42SDmitry Torokhov if (of_device_is_compatible(np, gpios[i].compatible) && 16499d18d42SDmitry Torokhov !strcmp(propname, gpios[i].propname)) { 16599d18d42SDmitry Torokhov of_gpio_quirk_polarity(np, gpios[i].active_high, flags); 16699d18d42SDmitry Torokhov break; 16799d18d42SDmitry Torokhov } 16899d18d42SDmitry Torokhov } 16999d18d42SDmitry Torokhov } 17099d18d42SDmitry Torokhov 17134cb9352SDmitry Torokhov static void of_gpio_set_polarity_by_property(const struct device_node *np, 17289a5e15bSLinus Walleij const char *propname, 17334cb9352SDmitry Torokhov enum of_gpio_flags *flags) 174a603a2b8SLinus Walleij { 17534cb9352SDmitry Torokhov static const struct { 17634cb9352SDmitry Torokhov const char *compatible; 17734cb9352SDmitry Torokhov const char *gpio_propname; 17834cb9352SDmitry Torokhov const char *polarity_propname; 17934cb9352SDmitry Torokhov } gpios[] = { 18034cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_FEC) 18134cb9352SDmitry Torokhov /* Freescale Fast Ethernet Controller */ 18234cb9352SDmitry Torokhov { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18334cb9352SDmitry Torokhov { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18434cb9352SDmitry Torokhov { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18534cb9352SDmitry Torokhov { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18634cb9352SDmitry Torokhov { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18734cb9352SDmitry Torokhov { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18834cb9352SDmitry Torokhov { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" }, 18934cb9352SDmitry Torokhov { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" }, 19034cb9352SDmitry Torokhov { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" }, 19134cb9352SDmitry Torokhov { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" }, 19234cb9352SDmitry Torokhov #endif 193b8b80348SDmitry Torokhov #if IS_ENABLED(CONFIG_PCI_IMX6) 194b8b80348SDmitry Torokhov { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" }, 195b8b80348SDmitry Torokhov { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" }, 196b8b80348SDmitry Torokhov { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" }, 197b8b80348SDmitry Torokhov { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" }, 198b8b80348SDmitry Torokhov { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" }, 199b8b80348SDmitry Torokhov { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" }, 200b8b80348SDmitry Torokhov { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" }, 201b8b80348SDmitry Torokhov #endif 20299d18d42SDmitry Torokhov 203a603a2b8SLinus Walleij /* 204a603a2b8SLinus Walleij * The regulator GPIO handles are specified such that the 205a603a2b8SLinus Walleij * presence or absence of "enable-active-high" solely controls 206a603a2b8SLinus Walleij * the polarity of the GPIO line. Any phandle flags must 207a603a2b8SLinus Walleij * be actively ignored. 208a603a2b8SLinus Walleij */ 20934cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE) 21034cb9352SDmitry Torokhov { "regulator-fixed", "gpios", "enable-active-high" }, 21134cb9352SDmitry Torokhov { "regulator-fixed", "gpio", "enable-active-high" }, 21234cb9352SDmitry Torokhov { "reg-fixed-voltage", "gpios", "enable-active-high" }, 21334cb9352SDmitry Torokhov { "reg-fixed-voltage", "gpio", "enable-active-high" }, 21434cb9352SDmitry Torokhov #endif 21534cb9352SDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_GPIO) 21634cb9352SDmitry Torokhov { "regulator-gpio", "enable-gpio", "enable-active-high" }, 21734cb9352SDmitry Torokhov { "regulator-gpio", "enable-gpios", "enable-active-high" }, 21834cb9352SDmitry Torokhov #endif 21934cb9352SDmitry Torokhov }; 22034cb9352SDmitry Torokhov unsigned int i; 22134cb9352SDmitry Torokhov bool active_high; 22234cb9352SDmitry Torokhov 22334cb9352SDmitry Torokhov for (i = 0; i < ARRAY_SIZE(gpios); i++) { 22434cb9352SDmitry Torokhov if (of_device_is_compatible(np, gpios[i].compatible) && 22534cb9352SDmitry Torokhov !strcmp(propname, gpios[i].gpio_propname)) { 22634cb9352SDmitry Torokhov active_high = of_property_read_bool(np, 22734cb9352SDmitry Torokhov gpios[i].polarity_propname); 228e3186e36SDmitry Torokhov of_gpio_quirk_polarity(np, active_high, flags); 22934cb9352SDmitry Torokhov break; 230a603a2b8SLinus Walleij } 23134cb9352SDmitry Torokhov } 23234cb9352SDmitry Torokhov } 23334cb9352SDmitry Torokhov 23434cb9352SDmitry Torokhov static void of_gpio_flags_quirks(const struct device_node *np, 23534cb9352SDmitry Torokhov const char *propname, 23634cb9352SDmitry Torokhov enum of_gpio_flags *flags, 23734cb9352SDmitry Torokhov int index) 23834cb9352SDmitry Torokhov { 23934cb9352SDmitry Torokhov of_gpio_try_fixup_polarity(np, propname, flags); 24034cb9352SDmitry Torokhov of_gpio_set_polarity_by_property(np, propname, flags); 24134cb9352SDmitry Torokhov 242a603a2b8SLinus Walleij /* 243a603a2b8SLinus Walleij * Legacy open drain handling for fixed voltage regulators. 244a603a2b8SLinus Walleij */ 245a603a2b8SLinus Walleij if (IS_ENABLED(CONFIG_REGULATOR) && 246a603a2b8SLinus Walleij of_device_is_compatible(np, "reg-fixed-voltage") && 247a603a2b8SLinus Walleij of_property_read_bool(np, "gpio-open-drain")) { 248a603a2b8SLinus Walleij *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); 249a603a2b8SLinus Walleij pr_info("%s uses legacy open drain flag - update the DTS if you can\n", 250a603a2b8SLinus Walleij of_node_full_name(np)); 251a603a2b8SLinus Walleij } 2526953c57aSLinus Walleij 2536953c57aSLinus Walleij /* 2546953c57aSLinus Walleij * Legacy handling of SPI active high chip select. If we have a 2556953c57aSLinus Walleij * property named "cs-gpios" we need to inspect the child node 2566953c57aSLinus Walleij * to determine if the flags should have inverted semantics. 2576953c57aSLinus Walleij */ 258da7f1349SLinus Walleij if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && 259a71a81e7SGeert Uytterhoeven of_property_read_bool(np, "cs-gpios")) { 2606953c57aSLinus Walleij struct device_node *child; 2616953c57aSLinus Walleij u32 cs; 2626953c57aSLinus Walleij int ret; 2636953c57aSLinus Walleij 2646953c57aSLinus Walleij for_each_child_of_node(np, child) { 2656953c57aSLinus Walleij ret = of_property_read_u32(child, "reg", &cs); 266c1c04ceaSLinus Walleij if (ret) 2676953c57aSLinus Walleij continue; 2686953c57aSLinus Walleij if (cs == index) { 2696953c57aSLinus Walleij /* 2706953c57aSLinus Walleij * SPI children have active low chip selects 2716953c57aSLinus Walleij * by default. This can be specified negatively 2726953c57aSLinus Walleij * by just omitting "spi-cs-high" in the 2736953c57aSLinus Walleij * device node, or actively by tagging on 2746953c57aSLinus Walleij * GPIO_ACTIVE_LOW as flag in the device 2756953c57aSLinus Walleij * tree. If the line is simultaneously 2766953c57aSLinus Walleij * tagged as active low in the device tree 2776953c57aSLinus Walleij * and has the "spi-cs-high" set, we get a 2786953c57aSLinus Walleij * conflict and the "spi-cs-high" flag will 2796953c57aSLinus Walleij * take precedence. 2806953c57aSLinus Walleij */ 281e3186e36SDmitry Torokhov bool active_high = of_property_read_bool(child, 282e3186e36SDmitry Torokhov "spi-cs-high"); 283e3186e36SDmitry Torokhov of_gpio_quirk_polarity(child, active_high, 284e3186e36SDmitry Torokhov flags); 28589fea04cSNishka Dasgupta of_node_put(child); 2866953c57aSLinus Walleij break; 2876953c57aSLinus Walleij } 2886953c57aSLinus Walleij } 2896953c57aSLinus Walleij } 290edc1ef3fSMartin Blumenstingl 291edc1ef3fSMartin Blumenstingl /* Legacy handling of stmmac's active-low PHY reset line */ 292edc1ef3fSMartin Blumenstingl if (IS_ENABLED(CONFIG_STMMAC_ETH) && 293edc1ef3fSMartin Blumenstingl !strcmp(propname, "snps,reset-gpio") && 294edc1ef3fSMartin Blumenstingl of_property_read_bool(np, "snps,reset-active-low")) 295edc1ef3fSMartin Blumenstingl *flags |= OF_GPIO_ACTIVE_LOW; 296a603a2b8SLinus Walleij } 297a603a2b8SLinus Walleij 298f141ed65SGrant Likely /** 299af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 300f141ed65SGrant Likely * @np: device node to get GPIO from 301f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 302f141ed65SGrant Likely * @index: index of the GPIO 303f141ed65SGrant Likely * @flags: a flags pointer to fill in 304f141ed65SGrant Likely * 305af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 306f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 307f141ed65SGrant Likely * in flags for the GPIO. 308f141ed65SGrant Likely */ 309e6ae9a83SKrzysztof Kozlowski static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np, 310af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 311f141ed65SGrant Likely { 312762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 313762c2e46SMasahiro Yamada struct gpio_chip *chip; 314762c2e46SMasahiro Yamada struct gpio_desc *desc; 315f141ed65SGrant Likely int ret; 316f141ed65SGrant Likely 317c11e6f0fSStephen Boyd ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, 318762c2e46SMasahiro Yamada &gpiospec); 3193d0f7cf0SGrant Likely if (ret) { 3207eb6ce2fSRob Herring pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", 3217eb6ce2fSRob Herring __func__, propname, np, index); 322af8b6375SAlexandre Courbot return ERR_PTR(ret); 3233d0f7cf0SGrant Likely } 324f141ed65SGrant Likely 325c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 326762c2e46SMasahiro Yamada if (!chip) { 327762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 328762c2e46SMasahiro Yamada goto out; 329762c2e46SMasahiro Yamada } 3303d0f7cf0SGrant Likely 33199468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 332762c2e46SMasahiro Yamada if (IS_ERR(desc)) 333762c2e46SMasahiro Yamada goto out; 334762c2e46SMasahiro Yamada 335605f2d34SLinus Walleij if (flags) 33689a5e15bSLinus Walleij of_gpio_flags_quirks(np, propname, flags, index); 337a603a2b8SLinus Walleij 3387eb6ce2fSRob Herring pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", 3397eb6ce2fSRob Herring __func__, propname, np, index, 340762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 341762c2e46SMasahiro Yamada 342762c2e46SMasahiro Yamada out: 343762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 344762c2e46SMasahiro Yamada 345762c2e46SMasahiro Yamada return desc; 346f141ed65SGrant Likely } 347f141ed65SGrant Likely 348e6ae9a83SKrzysztof Kozlowski int of_get_named_gpio_flags(const struct device_node *np, const char *list_name, 349f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 350f01d9075SAlexandre Courbot { 351f01d9075SAlexandre Courbot struct gpio_desc *desc; 352f01d9075SAlexandre Courbot 353f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 354f01d9075SAlexandre Courbot 355f01d9075SAlexandre Courbot if (IS_ERR(desc)) 356f01d9075SAlexandre Courbot return PTR_ERR(desc); 357f01d9075SAlexandre Courbot else 358f01d9075SAlexandre Courbot return desc_to_gpio(desc); 359f01d9075SAlexandre Courbot } 3606d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_get_named_gpio_flags); 361f01d9075SAlexandre Courbot 362d9e7f0e3SDmitry Torokhov /* Converts gpio_lookup_flags into bitmask of GPIO_* values */ 363d9e7f0e3SDmitry Torokhov static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags) 364d9e7f0e3SDmitry Torokhov { 365d9e7f0e3SDmitry Torokhov unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 366d9e7f0e3SDmitry Torokhov 367d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_ACTIVE_LOW) 368d9e7f0e3SDmitry Torokhov lflags |= GPIO_ACTIVE_LOW; 369d9e7f0e3SDmitry Torokhov 370d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_SINGLE_ENDED) { 371d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_OPEN_DRAIN) 372d9e7f0e3SDmitry Torokhov lflags |= GPIO_OPEN_DRAIN; 373d9e7f0e3SDmitry Torokhov else 374d9e7f0e3SDmitry Torokhov lflags |= GPIO_OPEN_SOURCE; 375d9e7f0e3SDmitry Torokhov } 376d9e7f0e3SDmitry Torokhov 377d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_TRANSITORY) 378d9e7f0e3SDmitry Torokhov lflags |= GPIO_TRANSITORY; 379d9e7f0e3SDmitry Torokhov 380d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_PULL_UP) 381d9e7f0e3SDmitry Torokhov lflags |= GPIO_PULL_UP; 382d9e7f0e3SDmitry Torokhov 383d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_PULL_DOWN) 384d9e7f0e3SDmitry Torokhov lflags |= GPIO_PULL_DOWN; 385d9e7f0e3SDmitry Torokhov 386d9e7f0e3SDmitry Torokhov if (flags & OF_GPIO_PULL_DISABLE) 387d9e7f0e3SDmitry Torokhov lflags |= GPIO_PULL_DISABLE; 388d9e7f0e3SDmitry Torokhov 389d9e7f0e3SDmitry Torokhov return lflags; 390d9e7f0e3SDmitry Torokhov } 391d9e7f0e3SDmitry Torokhov 392f626d6dfSLinus Walleij /** 393f626d6dfSLinus Walleij * gpiod_get_from_of_node() - obtain a GPIO from an OF node 394f626d6dfSLinus Walleij * @node: handle of the OF node 395f626d6dfSLinus Walleij * @propname: name of the DT property representing the GPIO 396f626d6dfSLinus Walleij * @index: index of the GPIO to obtain for the consumer 397f626d6dfSLinus Walleij * @dflags: GPIO initialization flags 398f626d6dfSLinus Walleij * @label: label to attach to the requested GPIO 399f626d6dfSLinus Walleij * 400f626d6dfSLinus Walleij * Returns: 401f626d6dfSLinus Walleij * On successful request the GPIO pin is configured in accordance with 402f626d6dfSLinus Walleij * provided @dflags. 403f626d6dfSLinus Walleij * 404f626d6dfSLinus Walleij * In case of error an ERR_PTR() is returned. 405f626d6dfSLinus Walleij */ 406e6ae9a83SKrzysztof Kozlowski struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node, 407f626d6dfSLinus Walleij const char *propname, int index, 408f626d6dfSLinus Walleij enum gpiod_flags dflags, 409f626d6dfSLinus Walleij const char *label) 410f626d6dfSLinus Walleij { 411d9e7f0e3SDmitry Torokhov unsigned long lflags; 412f626d6dfSLinus Walleij struct gpio_desc *desc; 413d9e7f0e3SDmitry Torokhov enum of_gpio_flags of_flags; 414f626d6dfSLinus Walleij int ret; 415f626d6dfSLinus Walleij 416d9e7f0e3SDmitry Torokhov desc = of_get_named_gpiod_flags(node, propname, index, &of_flags); 417d9e7f0e3SDmitry Torokhov if (!desc || IS_ERR(desc)) 418f626d6dfSLinus Walleij return desc; 419f626d6dfSLinus Walleij 420f626d6dfSLinus Walleij ret = gpiod_request(desc, label); 421be7ae45cSMarco Felsch if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 422f626d6dfSLinus Walleij return desc; 423f626d6dfSLinus Walleij if (ret) 424f626d6dfSLinus Walleij return ERR_PTR(ret); 425f626d6dfSLinus Walleij 426d9e7f0e3SDmitry Torokhov lflags = of_convert_gpio_flags(of_flags); 42731bea231SNuno Sá 428f626d6dfSLinus Walleij ret = gpiod_configure_flags(desc, propname, lflags, dflags); 429f626d6dfSLinus Walleij if (ret < 0) { 430f626d6dfSLinus Walleij gpiod_put(desc); 431f626d6dfSLinus Walleij return ERR_PTR(ret); 432f626d6dfSLinus Walleij } 433f626d6dfSLinus Walleij 434f626d6dfSLinus Walleij return desc; 435f626d6dfSLinus Walleij } 4366d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(gpiod_get_from_of_node); 437f141ed65SGrant Likely 438b311c5cbSDmitry Torokhov static struct gpio_desc *of_find_gpio_rename(struct device_node *np, 43998c3c940SDmitry Torokhov const char *con_id, 44098c3c940SDmitry Torokhov unsigned int idx, 441c8582339SLinus Walleij enum of_gpio_flags *of_flags) 442c8582339SLinus Walleij { 443b311c5cbSDmitry Torokhov static const struct of_rename_gpio { 444b311c5cbSDmitry Torokhov const char *con_id; 445b311c5cbSDmitry Torokhov const char *legacy_id; /* NULL - same as con_id */ 446b311c5cbSDmitry Torokhov /* 447b311c5cbSDmitry Torokhov * Compatible string can be set to NULL in case where 448b311c5cbSDmitry Torokhov * matching to a particular compatible is not practical, 449b311c5cbSDmitry Torokhov * but it should only be done for gpio names that have 450b311c5cbSDmitry Torokhov * vendor prefix to reduce risk of false positives. 451b311c5cbSDmitry Torokhov * Addition of such entries is strongly discouraged. 452b311c5cbSDmitry Torokhov */ 453b311c5cbSDmitry Torokhov const char *compatible; 454b311c5cbSDmitry Torokhov } gpios[] = { 455fbbbcd17SDmitry Torokhov #if !IS_ENABLED(CONFIG_LCD_HX8357) 456fbbbcd17SDmitry Torokhov /* Himax LCD controllers used "gpios-reset" */ 457fbbbcd17SDmitry Torokhov { "reset", "gpios-reset", "himax,hx8357" }, 458fbbbcd17SDmitry Torokhov { "reset", "gpios-reset", "himax,hx8369" }, 459fbbbcd17SDmitry Torokhov #endif 460b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_MFD_ARIZONA) 461b311c5cbSDmitry Torokhov { "wlf,reset", NULL, NULL }, 462b311c5cbSDmitry Torokhov #endif 463eaf1a296SDmitry Torokhov #if IS_ENABLED(CONFIG_RTC_DRV_MOXART) 464eaf1a296SDmitry Torokhov { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" }, 465eaf1a296SDmitry Torokhov { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" }, 466eaf1a296SDmitry Torokhov { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" }, 467eaf1a296SDmitry Torokhov #endif 4689c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_I2C) 4699c2cc717SDmitry Torokhov { "reset", "reset-n-io", "marvell,nfc-i2c" }, 4709c2cc717SDmitry Torokhov #endif 4719c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_SPI) 4729c2cc717SDmitry Torokhov { "reset", "reset-n-io", "marvell,nfc-spi" }, 4739c2cc717SDmitry Torokhov #endif 4749c2cc717SDmitry Torokhov #if IS_ENABLED(CONFIG_NFC_MRVL_UART) 4759c2cc717SDmitry Torokhov { "reset", "reset-n-io", "marvell,nfc-uart" }, 4769c2cc717SDmitry Torokhov { "reset", "reset-n-io", "mrvl,nfc-uart" }, 4779c2cc717SDmitry Torokhov #endif 478fbbbcd17SDmitry Torokhov #if !IS_ENABLED(CONFIG_PCI_LANTIQ) 479fbbbcd17SDmitry Torokhov /* MIPS Lantiq PCI */ 480fbbbcd17SDmitry Torokhov { "reset", "gpios-reset", "lantiq,pci-xway" }, 481fbbbcd17SDmitry Torokhov #endif 482307c593bSDmitry Torokhov 483b311c5cbSDmitry Torokhov /* 484b311c5cbSDmitry Torokhov * Some regulator bindings happened before we managed to 485b311c5cbSDmitry Torokhov * establish that GPIO properties should be named 486b311c5cbSDmitry Torokhov * "foo-gpios" so we have this special kludge for them. 487b311c5cbSDmitry Torokhov */ 488307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1) 489b311c5cbSDmitry Torokhov { "wlf,ldoena", NULL, NULL }, /* Arizona */ 490307c593bSDmitry Torokhov #endif 491307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_REGULATOR_WM8994) 492b311c5cbSDmitry Torokhov { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */ 493b311c5cbSDmitry Torokhov { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */ 494b311c5cbSDmitry Torokhov #endif 495c8582339SLinus Walleij 496944004ebSDmitry Torokhov #if IS_ENABLED(CONFIG_SND_SOC_CS42L56) 497944004ebSDmitry Torokhov { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" }, 498944004ebSDmitry Torokhov #endif 499fbbbcd17SDmitry Torokhov #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X) 500fbbbcd17SDmitry Torokhov { "reset", "gpio-reset", "ti,tlv320aic3x" }, 501fbbbcd17SDmitry Torokhov { "reset", "gpio-reset", "ti,tlv320aic33" }, 502fbbbcd17SDmitry Torokhov { "reset", "gpio-reset", "ti,tlv320aic3007" }, 503fbbbcd17SDmitry Torokhov { "reset", "gpio-reset", "ti,tlv320aic3104" }, 504fbbbcd17SDmitry Torokhov { "reset", "gpio-reset", "ti,tlv320aic3106" }, 505fbbbcd17SDmitry Torokhov #endif 506307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_GPIO) 507c8582339SLinus Walleij /* 508b311c5cbSDmitry Torokhov * The SPI GPIO bindings happened before we managed to 509b311c5cbSDmitry Torokhov * establish that GPIO properties should be named 510b311c5cbSDmitry Torokhov * "foo-gpios" so we have this special kludge for them. 511c8582339SLinus Walleij */ 512b311c5cbSDmitry Torokhov { "miso", "gpio-miso", "spi-gpio" }, 513b311c5cbSDmitry Torokhov { "mosi", "gpio-mosi", "spi-gpio" }, 514b311c5cbSDmitry Torokhov { "sck", "gpio-sck", "spi-gpio" }, 515307c593bSDmitry Torokhov #endif 516c8582339SLinus Walleij 5176a537d48SLinus Walleij /* 518b311c5cbSDmitry Torokhov * The old Freescale bindings use simply "gpios" as name 519b311c5cbSDmitry Torokhov * for the chip select lines rather than "cs-gpios" like 520b311c5cbSDmitry Torokhov * all other SPI hardware. Allow this specifically for 521b311c5cbSDmitry Torokhov * Freescale and PPC devices. 522e3023bf8SLinus Walleij */ 523307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_FSL_SPI) 524b311c5cbSDmitry Torokhov { "cs", "gpios", "fsl,spi" }, 525b311c5cbSDmitry Torokhov { "cs", "gpios", "aeroflexgaisler,spictrl" }, 526307c593bSDmitry Torokhov #endif 527307c593bSDmitry Torokhov #if IS_ENABLED(CONFIG_SPI_PPC4xx) 528b311c5cbSDmitry Torokhov { "cs", "gpios", "ibm,ppc4xx-spi" }, 529b311c5cbSDmitry Torokhov #endif 530307c593bSDmitry Torokhov 531b311c5cbSDmitry Torokhov #if IS_ENABLED(CONFIG_TYPEC_FUSB302) 532e3023bf8SLinus Walleij /* 533b311c5cbSDmitry Torokhov * Fairchild FUSB302 host is using undocumented "fcs,int_n" 534b311c5cbSDmitry Torokhov * property without the compulsory "-gpios" suffix. 535e3023bf8SLinus Walleij */ 536b311c5cbSDmitry Torokhov { "fcs,int_n", NULL, "fcs,fusb302" }, 537b311c5cbSDmitry Torokhov #endif 5386a537d48SLinus Walleij }; 539b311c5cbSDmitry Torokhov struct gpio_desc *desc; 540b311c5cbSDmitry Torokhov const char *legacy_id; 541b311c5cbSDmitry Torokhov unsigned int i; 5426a537d48SLinus Walleij 5436a537d48SLinus Walleij if (!con_id) 5446a537d48SLinus Walleij return ERR_PTR(-ENOENT); 5456a537d48SLinus Walleij 546b311c5cbSDmitry Torokhov for (i = 0; i < ARRAY_SIZE(gpios); i++) { 547b311c5cbSDmitry Torokhov if (strcmp(con_id, gpios[i].con_id)) 548b311c5cbSDmitry Torokhov continue; 5496a537d48SLinus Walleij 550b311c5cbSDmitry Torokhov if (gpios[i].compatible && 551b311c5cbSDmitry Torokhov !of_device_is_compatible(np, gpios[i].compatible)) 552b311c5cbSDmitry Torokhov continue; 553b311c5cbSDmitry Torokhov 554b311c5cbSDmitry Torokhov legacy_id = gpios[i].legacy_id ?: gpios[i].con_id; 555b311c5cbSDmitry Torokhov desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags); 556b311c5cbSDmitry Torokhov if (!gpiod_not_found(desc)) { 557b311c5cbSDmitry Torokhov pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n", 558b311c5cbSDmitry Torokhov of_node_full_name(np), legacy_id, con_id); 559b311c5cbSDmitry Torokhov return desc; 560b311c5cbSDmitry Torokhov } 5616a537d48SLinus Walleij } 5626a537d48SLinus Walleij 56311c43bb0SDmitry Torokhov return ERR_PTR(-ENOENT); 5646e24826dSLinus Walleij } 5656e24826dSLinus Walleij 566326c3753SDmitry Torokhov static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np, 567326c3753SDmitry Torokhov const char *con_id, 568326c3753SDmitry Torokhov unsigned int idx, 569326c3753SDmitry Torokhov enum of_gpio_flags *of_flags) 570326c3753SDmitry Torokhov { 571326c3753SDmitry Torokhov struct gpio_desc *desc; 572326c3753SDmitry Torokhov const char *legacy_id; 573326c3753SDmitry Torokhov 574326c3753SDmitry Torokhov if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)) 575326c3753SDmitry Torokhov return ERR_PTR(-ENOENT); 576326c3753SDmitry Torokhov 577326c3753SDmitry Torokhov if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine")) 578326c3753SDmitry Torokhov return ERR_PTR(-ENOENT); 579326c3753SDmitry Torokhov 580326c3753SDmitry Torokhov if (!con_id || strcmp(con_id, "i2s1-in-sel")) 581326c3753SDmitry Torokhov return ERR_PTR(-ENOENT); 582326c3753SDmitry Torokhov 583326c3753SDmitry Torokhov if (idx == 0) 584326c3753SDmitry Torokhov legacy_id = "i2s1-in-sel-gpio1"; 585326c3753SDmitry Torokhov else if (idx == 1) 586326c3753SDmitry Torokhov legacy_id = "i2s1-in-sel-gpio2"; 587326c3753SDmitry Torokhov else 588326c3753SDmitry Torokhov return ERR_PTR(-ENOENT); 589326c3753SDmitry Torokhov 590326c3753SDmitry Torokhov desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags); 591326c3753SDmitry Torokhov if (!gpiod_not_found(desc)) 592326c3753SDmitry Torokhov pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n", 593326c3753SDmitry Torokhov of_node_full_name(np), legacy_id, con_id); 594326c3753SDmitry Torokhov 595326c3753SDmitry Torokhov return desc; 596326c3753SDmitry Torokhov } 597326c3753SDmitry Torokhov 598a2b5e207SDmitry Torokhov typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np, 599a2b5e207SDmitry Torokhov const char *con_id, 600a2b5e207SDmitry Torokhov unsigned int idx, 601a2b5e207SDmitry Torokhov enum of_gpio_flags *of_flags); 602a2b5e207SDmitry Torokhov static const of_find_gpio_quirk of_find_gpio_quirks[] = { 603b311c5cbSDmitry Torokhov of_find_gpio_rename, 604326c3753SDmitry Torokhov of_find_mt2701_gpio, 6058b10ca2fSMichael Walle NULL 606a2b5e207SDmitry Torokhov }; 607a2b5e207SDmitry Torokhov 608*07445ae1SDmitry Torokhov struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, 609fed7026aSAndy Shevchenko unsigned int idx, unsigned long *flags) 610ea713bc4SLinus Walleij { 611ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 612ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 613a2b5e207SDmitry Torokhov const of_find_gpio_quirk *q; 614ea713bc4SLinus Walleij struct gpio_desc *desc; 615ea713bc4SLinus Walleij unsigned int i; 616ea713bc4SLinus Walleij 617c8582339SLinus Walleij /* Try GPIO property "foo-gpios" and "foo-gpio" */ 618ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 619ea713bc4SLinus Walleij if (con_id) 620ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 621ea713bc4SLinus Walleij gpio_suffixes[i]); 622ea713bc4SLinus Walleij else 623ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 624ea713bc4SLinus Walleij gpio_suffixes[i]); 625ea713bc4SLinus Walleij 626*07445ae1SDmitry Torokhov desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags); 6276662ae6aSMaxime Ripard 6287b58696dSAndy Shevchenko if (!gpiod_not_found(desc)) 629ea713bc4SLinus Walleij break; 630ea713bc4SLinus Walleij } 631ea713bc4SLinus Walleij 632a2b5e207SDmitry Torokhov /* Properly named GPIO was not found, try workarounds */ 633a2b5e207SDmitry Torokhov for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++) 634*07445ae1SDmitry Torokhov desc = (*q)(np, con_id, idx, &of_flags); 6356e24826dSLinus Walleij 636ea713bc4SLinus Walleij if (IS_ERR(desc)) 637ea713bc4SLinus Walleij return desc; 638ea713bc4SLinus Walleij 639d9e7f0e3SDmitry Torokhov *flags = of_convert_gpio_flags(of_flags); 640d449991cSThomas Petazzoni 641ea713bc4SLinus Walleij return desc; 642ea713bc4SLinus Walleij } 643ea713bc4SLinus Walleij 644f141ed65SGrant Likely /** 645fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 646f625d460SBenoit Parrot * @np: device node to get GPIO from 647be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 648a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 649f625d460SBenoit Parrot * @name: GPIO line name 650fed7026aSAndy Shevchenko * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 651fed7026aSAndy Shevchenko * of_find_gpio() or of_parse_own_gpio() 652f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 653f625d460SBenoit Parrot * 654f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 655f625d460SBenoit Parrot * value on the error condition. 656f625d460SBenoit Parrot */ 657fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 658be715343SMasahiro Yamada struct gpio_chip *chip, 659a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 660fed7026aSAndy Shevchenko unsigned long *lflags, 661f625d460SBenoit Parrot enum gpiod_flags *dflags) 662f625d460SBenoit Parrot { 663f625d460SBenoit Parrot struct device_node *chip_np; 664f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 665be715343SMasahiro Yamada struct of_phandle_args gpiospec; 666be715343SMasahiro Yamada struct gpio_desc *desc; 667a79fead5SGeert Uytterhoeven unsigned int i; 668f625d460SBenoit Parrot u32 tmp; 6693f9547e1SMasahiro Yamada int ret; 670f625d460SBenoit Parrot 671be715343SMasahiro Yamada chip_np = chip->of_node; 672f625d460SBenoit Parrot if (!chip_np) 673f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 674f625d460SBenoit Parrot 675f625d460SBenoit Parrot xlate_flags = 0; 6762d6c06f5SAndy Shevchenko *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 67740941954SAndy Shevchenko *dflags = GPIOD_ASIS; 678f625d460SBenoit Parrot 679f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 680f625d460SBenoit Parrot if (ret) 681f625d460SBenoit Parrot return ERR_PTR(ret); 682f625d460SBenoit Parrot 683be715343SMasahiro Yamada gpiospec.np = chip_np; 684be715343SMasahiro Yamada gpiospec.args_count = tmp; 685f625d460SBenoit Parrot 686a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 687a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 688a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 689f625d460SBenoit Parrot if (ret) 690f625d460SBenoit Parrot return ERR_PTR(ret); 691a79fead5SGeert Uytterhoeven } 692f625d460SBenoit Parrot 69399468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 694be715343SMasahiro Yamada if (IS_ERR(desc)) 695be715343SMasahiro Yamada return desc; 696f625d460SBenoit Parrot 697d9e7f0e3SDmitry Torokhov *lflags = of_convert_gpio_flags(xlate_flags); 698f625d460SBenoit Parrot 699f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 700f625d460SBenoit Parrot *dflags |= GPIOD_IN; 701f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 702f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 703f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 704f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 705f625d460SBenoit Parrot else { 70662cdcb6cSRob Herring pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", 70762cdcb6cSRob Herring desc_to_gpio(desc), np); 708f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 709f625d460SBenoit Parrot } 710f625d460SBenoit Parrot 711f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 712f625d460SBenoit Parrot *name = np->name; 713f625d460SBenoit Parrot 714be715343SMasahiro Yamada return desc; 715f625d460SBenoit Parrot } 716f625d460SBenoit Parrot 717f625d460SBenoit Parrot /** 718bc21077eSGeert Uytterhoeven * of_gpiochip_add_hog - Add all hogs in a hog device node 719bc21077eSGeert Uytterhoeven * @chip: gpio chip to act on 720bc21077eSGeert Uytterhoeven * @hog: device node describing the hogs 721bc21077eSGeert Uytterhoeven * 722bc21077eSGeert Uytterhoeven * Returns error if it fails otherwise 0 on success. 723bc21077eSGeert Uytterhoeven */ 724bc21077eSGeert Uytterhoeven static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) 725bc21077eSGeert Uytterhoeven { 726bc21077eSGeert Uytterhoeven enum gpiod_flags dflags; 727bc21077eSGeert Uytterhoeven struct gpio_desc *desc; 728bc21077eSGeert Uytterhoeven unsigned long lflags; 729bc21077eSGeert Uytterhoeven const char *name; 730bc21077eSGeert Uytterhoeven unsigned int i; 731bc21077eSGeert Uytterhoeven int ret; 732bc21077eSGeert Uytterhoeven 733bc21077eSGeert Uytterhoeven for (i = 0;; i++) { 734bc21077eSGeert Uytterhoeven desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); 735bc21077eSGeert Uytterhoeven if (IS_ERR(desc)) 736bc21077eSGeert Uytterhoeven break; 737bc21077eSGeert Uytterhoeven 738bc21077eSGeert Uytterhoeven ret = gpiod_hog(desc, name, lflags, dflags); 739bc21077eSGeert Uytterhoeven if (ret < 0) 740bc21077eSGeert Uytterhoeven return ret; 74163636d95SGeert Uytterhoeven 74263636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC 74363636d95SGeert Uytterhoeven desc->hog = hog; 74463636d95SGeert Uytterhoeven #endif 745bc21077eSGeert Uytterhoeven } 746bc21077eSGeert Uytterhoeven 747bc21077eSGeert Uytterhoeven return 0; 748bc21077eSGeert Uytterhoeven } 749bc21077eSGeert Uytterhoeven 750bc21077eSGeert Uytterhoeven /** 751fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 752f625d460SBenoit Parrot * @chip: gpio chip to act on 753f625d460SBenoit Parrot * 754f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 755f625d460SBenoit Parrot * configuration. 756ead066e6SGeert Uytterhoeven * It returns error if it fails otherwise 0 on success. 757f625d460SBenoit Parrot */ 758dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 759f625d460SBenoit Parrot { 760f625d460SBenoit Parrot struct device_node *np; 761dfbd379bSLaxman Dewangan int ret; 762f625d460SBenoit Parrot 763d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 764f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 765f625d460SBenoit Parrot continue; 766f625d460SBenoit Parrot 767bc21077eSGeert Uytterhoeven ret = of_gpiochip_add_hog(chip, np); 76809e258afSWei Yongjun if (ret < 0) { 76909e258afSWei Yongjun of_node_put(np); 770dfbd379bSLaxman Dewangan return ret; 771f625d460SBenoit Parrot } 77263636d95SGeert Uytterhoeven 77363636d95SGeert Uytterhoeven of_node_set_flag(np, OF_POPULATED); 77409e258afSWei Yongjun } 775dfbd379bSLaxman Dewangan 776dfbd379bSLaxman Dewangan return 0; 777f625d460SBenoit Parrot } 778f625d460SBenoit Parrot 77963636d95SGeert Uytterhoeven #ifdef CONFIG_OF_DYNAMIC 78063636d95SGeert Uytterhoeven /** 78163636d95SGeert Uytterhoeven * of_gpiochip_remove_hog - Remove all hogs in a hog device node 78263636d95SGeert Uytterhoeven * @chip: gpio chip to act on 78363636d95SGeert Uytterhoeven * @hog: device node describing the hogs 78463636d95SGeert Uytterhoeven */ 78563636d95SGeert Uytterhoeven static void of_gpiochip_remove_hog(struct gpio_chip *chip, 78663636d95SGeert Uytterhoeven struct device_node *hog) 78763636d95SGeert Uytterhoeven { 78880c78fbeSAndy Shevchenko struct gpio_desc *desc; 78963636d95SGeert Uytterhoeven 79057017eddSAndy Shevchenko for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED) 79180c78fbeSAndy Shevchenko if (desc->hog == hog) 79280c78fbeSAndy Shevchenko gpiochip_free_own_desc(desc); 79363636d95SGeert Uytterhoeven } 79463636d95SGeert Uytterhoeven 79563636d95SGeert Uytterhoeven static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) 79663636d95SGeert Uytterhoeven { 797597a8a88SAndy Shevchenko return device_match_of_node(&chip->gpiodev->dev, data); 79863636d95SGeert Uytterhoeven } 79963636d95SGeert Uytterhoeven 80063636d95SGeert Uytterhoeven static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) 80163636d95SGeert Uytterhoeven { 80263636d95SGeert Uytterhoeven return gpiochip_find(np, of_gpiochip_match_node); 80363636d95SGeert Uytterhoeven } 80463636d95SGeert Uytterhoeven 80563636d95SGeert Uytterhoeven static int of_gpio_notify(struct notifier_block *nb, unsigned long action, 80663636d95SGeert Uytterhoeven void *arg) 80763636d95SGeert Uytterhoeven { 80863636d95SGeert Uytterhoeven struct of_reconfig_data *rd = arg; 80963636d95SGeert Uytterhoeven struct gpio_chip *chip; 81063636d95SGeert Uytterhoeven int ret; 81163636d95SGeert Uytterhoeven 81263636d95SGeert Uytterhoeven /* 81363636d95SGeert Uytterhoeven * This only supports adding and removing complete gpio-hog nodes. 81463636d95SGeert Uytterhoeven * Modifying an existing gpio-hog node is not supported (except for 81563636d95SGeert Uytterhoeven * changing its "status" property, which is treated the same as 81663636d95SGeert Uytterhoeven * addition/removal). 81763636d95SGeert Uytterhoeven */ 81863636d95SGeert Uytterhoeven switch (of_reconfig_get_state_change(action, arg)) { 81963636d95SGeert Uytterhoeven case OF_RECONFIG_CHANGE_ADD: 82063636d95SGeert Uytterhoeven if (!of_property_read_bool(rd->dn, "gpio-hog")) 82163636d95SGeert Uytterhoeven return NOTIFY_OK; /* not for us */ 82263636d95SGeert Uytterhoeven 82363636d95SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) 82463636d95SGeert Uytterhoeven return NOTIFY_OK; 82563636d95SGeert Uytterhoeven 82663636d95SGeert Uytterhoeven chip = of_find_gpiochip_by_node(rd->dn->parent); 82763636d95SGeert Uytterhoeven if (chip == NULL) 82863636d95SGeert Uytterhoeven return NOTIFY_OK; /* not for us */ 82963636d95SGeert Uytterhoeven 83063636d95SGeert Uytterhoeven ret = of_gpiochip_add_hog(chip, rd->dn); 83163636d95SGeert Uytterhoeven if (ret < 0) { 83263636d95SGeert Uytterhoeven pr_err("%s: failed to add hogs for %pOF\n", __func__, 83363636d95SGeert Uytterhoeven rd->dn); 83463636d95SGeert Uytterhoeven of_node_clear_flag(rd->dn, OF_POPULATED); 83563636d95SGeert Uytterhoeven return notifier_from_errno(ret); 83663636d95SGeert Uytterhoeven } 83763636d95SGeert Uytterhoeven break; 83863636d95SGeert Uytterhoeven 83963636d95SGeert Uytterhoeven case OF_RECONFIG_CHANGE_REMOVE: 84063636d95SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 84163636d95SGeert Uytterhoeven return NOTIFY_OK; /* already depopulated */ 84263636d95SGeert Uytterhoeven 84363636d95SGeert Uytterhoeven chip = of_find_gpiochip_by_node(rd->dn->parent); 84463636d95SGeert Uytterhoeven if (chip == NULL) 84563636d95SGeert Uytterhoeven return NOTIFY_OK; /* not for us */ 84663636d95SGeert Uytterhoeven 84763636d95SGeert Uytterhoeven of_gpiochip_remove_hog(chip, rd->dn); 84863636d95SGeert Uytterhoeven of_node_clear_flag(rd->dn, OF_POPULATED); 84963636d95SGeert Uytterhoeven break; 85063636d95SGeert Uytterhoeven } 85163636d95SGeert Uytterhoeven 85263636d95SGeert Uytterhoeven return NOTIFY_OK; 85363636d95SGeert Uytterhoeven } 85463636d95SGeert Uytterhoeven 85563636d95SGeert Uytterhoeven struct notifier_block gpio_of_notifier = { 85663636d95SGeert Uytterhoeven .notifier_call = of_gpio_notify, 85763636d95SGeert Uytterhoeven }; 85863636d95SGeert Uytterhoeven #endif /* CONFIG_OF_DYNAMIC */ 85963636d95SGeert Uytterhoeven 860f625d460SBenoit Parrot /** 86167049c50SThierry Reding * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags 862f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 86367049c50SThierry Reding * @gpiospec: GPIO specifier as found in the device tree 864f141ed65SGrant Likely * @flags: a flags pointer to fill in 865f141ed65SGrant Likely * 866f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 86767049c50SThierry Reding * GPIO chips. This function performs only one sanity check: whether GPIO 868f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 869f141ed65SGrant Likely */ 870b0c7e73bSGeert Uytterhoeven static int of_gpio_simple_xlate(struct gpio_chip *gc, 871b0c7e73bSGeert Uytterhoeven const struct of_phandle_args *gpiospec, 872b0c7e73bSGeert Uytterhoeven u32 *flags) 873f141ed65SGrant Likely { 874f141ed65SGrant Likely /* 875f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 87620a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 877f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 878f141ed65SGrant Likely * but not recommended). 879f141ed65SGrant Likely */ 880f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 881f141ed65SGrant Likely WARN_ON(1); 882f141ed65SGrant Likely return -EINVAL; 883f141ed65SGrant Likely } 884f141ed65SGrant Likely 885f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 886f141ed65SGrant Likely return -EINVAL; 887f141ed65SGrant Likely 8887b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 889f141ed65SGrant Likely return -EINVAL; 890f141ed65SGrant Likely 891f141ed65SGrant Likely if (flags) 892f141ed65SGrant Likely *flags = gpiospec->args[1]; 893f141ed65SGrant Likely 894f141ed65SGrant Likely return gpiospec->args[0]; 895f141ed65SGrant Likely } 896f141ed65SGrant Likely 897f141ed65SGrant Likely /** 8983208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 899f141ed65SGrant Likely * @np: device node of the GPIO chip 900f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 9013208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 902f141ed65SGrant Likely * 903f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 904f141ed65SGrant Likely * 905f141ed65SGrant Likely * 1) In the gpio_chip structure: 906f141ed65SGrant Likely * - all the callbacks 907f141ed65SGrant Likely * - of_gpio_n_cells 908f141ed65SGrant Likely * - of_xlate callback (optional) 909f141ed65SGrant Likely * 910f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 911f141ed65SGrant Likely * - save_regs callback (optional) 912f141ed65SGrant Likely * 913f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 914f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 915f141ed65SGrant Likely * to manage GPIOs from the callbacks. 916f141ed65SGrant Likely */ 9173208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 9183208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 9193208b0f0SLinus Walleij void *data) 920f141ed65SGrant Likely { 921f141ed65SGrant Likely int ret = -ENOMEM; 922f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 923f141ed65SGrant Likely 9247eb6ce2fSRob Herring gc->label = kasprintf(GFP_KERNEL, "%pOF", np); 925f141ed65SGrant Likely if (!gc->label) 926f141ed65SGrant Likely goto err0; 927f141ed65SGrant Likely 928f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 929f141ed65SGrant Likely if (!mm_gc->regs) 930f141ed65SGrant Likely goto err1; 931f141ed65SGrant Likely 932f141ed65SGrant Likely gc->base = -1; 933f141ed65SGrant Likely 934f141ed65SGrant Likely if (mm_gc->save_regs) 935f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 936f141ed65SGrant Likely 9375d07a692SLiang He of_node_put(mm_gc->gc.of_node); 9385d07a692SLiang He mm_gc->gc.of_node = of_node_get(np); 939f141ed65SGrant Likely 9403208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 941f141ed65SGrant Likely if (ret) 942f141ed65SGrant Likely goto err2; 943f141ed65SGrant Likely 944f141ed65SGrant Likely return 0; 945f141ed65SGrant Likely err2: 9465d07a692SLiang He of_node_put(np); 947f141ed65SGrant Likely iounmap(mm_gc->regs); 948f141ed65SGrant Likely err1: 949f141ed65SGrant Likely kfree(gc->label); 950f141ed65SGrant Likely err0: 9517eb6ce2fSRob Herring pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); 952f141ed65SGrant Likely return ret; 953f141ed65SGrant Likely } 9546d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); 955f141ed65SGrant Likely 956d621e8baSRicardo Ribalda Delgado /** 957d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 958d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 959d621e8baSRicardo Ribalda Delgado */ 960d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 961d621e8baSRicardo Ribalda Delgado { 962d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 963d621e8baSRicardo Ribalda Delgado 964d621e8baSRicardo Ribalda Delgado if (!mm_gc) 965d621e8baSRicardo Ribalda Delgado return; 966d621e8baSRicardo Ribalda Delgado 967d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 968d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 969d621e8baSRicardo Ribalda Delgado kfree(gc->label); 970d621e8baSRicardo Ribalda Delgado } 9716d662455SGeert Uytterhoeven EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); 972d621e8baSRicardo Ribalda Delgado 973f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 97428355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 975f23f1516SShiraz Hashim { 976f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 977f23f1516SShiraz Hashim struct of_phandle_args pinspec; 9781e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 979f23f1516SShiraz Hashim int index = 0, ret; 980586a87e6SChristian Ruppert const char *name; 981586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 982586a87e6SChristian Ruppert struct property *group_names; 983f23f1516SShiraz Hashim 984f23f1516SShiraz Hashim if (!np) 98528355f81STomeu Vizoso return 0; 986f23f1516SShiraz Hashim 9873550bba2SStefan Wahren if (!of_property_read_bool(np, "gpio-ranges") && 9883550bba2SStefan Wahren chip->of_gpio_ranges_fallback) { 9893550bba2SStefan Wahren return chip->of_gpio_ranges_fallback(chip, np); 9903550bba2SStefan Wahren } 9913550bba2SStefan Wahren 992586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 993586a87e6SChristian Ruppert 994ad4e1a7cSHaojian Zhuang for (;; index++) { 995d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 996d9fe0039SStephen Warren index, &pinspec); 997f23f1516SShiraz Hashim if (ret) 998f23f1516SShiraz Hashim break; 999f23f1516SShiraz Hashim 10001e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 1001602cf638SMasahiro Yamada of_node_put(pinspec.np); 10021e63d7b9SLinus Walleij if (!pctldev) 100328355f81STomeu Vizoso return -EPROBE_DEFER; 1004f23f1516SShiraz Hashim 1005586a87e6SChristian Ruppert if (pinspec.args[2]) { 1006586a87e6SChristian Ruppert if (group_names) { 100772858602SLaurent Navet of_property_read_string_index(np, 1008586a87e6SChristian Ruppert group_names_propname, 1009586a87e6SChristian Ruppert index, &name); 1010586a87e6SChristian Ruppert if (strlen(name)) { 10117eb6ce2fSRob Herring pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", 10127eb6ce2fSRob Herring np); 1013586a87e6SChristian Ruppert break; 1014586a87e6SChristian Ruppert } 1015586a87e6SChristian Ruppert } 1016586a87e6SChristian Ruppert /* npins != 0: linear range */ 10171e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 1018ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 10191e63d7b9SLinus Walleij pinspec.args[0], 102086853c83SHaojian Zhuang pinspec.args[1], 102186853c83SHaojian Zhuang pinspec.args[2]); 10221e63d7b9SLinus Walleij if (ret) 102328355f81STomeu Vizoso return ret; 1024586a87e6SChristian Ruppert } else { 1025586a87e6SChristian Ruppert /* npins == 0: special range */ 1026586a87e6SChristian Ruppert if (pinspec.args[1]) { 10277eb6ce2fSRob Herring pr_err("%pOF: Illegal gpio-range format.\n", 10287eb6ce2fSRob Herring np); 1029586a87e6SChristian Ruppert break; 1030586a87e6SChristian Ruppert } 1031586a87e6SChristian Ruppert 1032586a87e6SChristian Ruppert if (!group_names) { 10337eb6ce2fSRob Herring pr_err("%pOF: GPIO group range requested but no %s property.\n", 10347eb6ce2fSRob Herring np, group_names_propname); 1035586a87e6SChristian Ruppert break; 1036586a87e6SChristian Ruppert } 1037586a87e6SChristian Ruppert 1038586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 1039586a87e6SChristian Ruppert group_names_propname, 1040586a87e6SChristian Ruppert index, &name); 1041586a87e6SChristian Ruppert if (ret) 1042586a87e6SChristian Ruppert break; 1043586a87e6SChristian Ruppert 1044586a87e6SChristian Ruppert if (!strlen(name)) { 10457eb6ce2fSRob Herring pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", 10467eb6ce2fSRob Herring np); 1047586a87e6SChristian Ruppert break; 1048586a87e6SChristian Ruppert } 1049586a87e6SChristian Ruppert 1050586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 1051586a87e6SChristian Ruppert pinspec.args[0], name); 1052586a87e6SChristian Ruppert if (ret) 105328355f81STomeu Vizoso return ret; 1054586a87e6SChristian Ruppert } 1055ad4e1a7cSHaojian Zhuang } 105628355f81STomeu Vizoso 105728355f81STomeu Vizoso return 0; 1058f23f1516SShiraz Hashim } 1059f23f1516SShiraz Hashim 1060f23f1516SShiraz Hashim #else 106128355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 1062f23f1516SShiraz Hashim #endif 1063f23f1516SShiraz Hashim 106428355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 1065f141ed65SGrant Likely { 10668afe8255SAndy Shevchenko struct device_node *np; 1067f0d1ab05SLinus Walleij int ret; 106828355f81STomeu Vizoso 10698afe8255SAndy Shevchenko np = to_of_node(chip->fwnode); 10708afe8255SAndy Shevchenko if (!np) 107128355f81STomeu Vizoso return 0; 1072f141ed65SGrant Likely 1073f141ed65SGrant Likely if (!chip->of_xlate) { 1074f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 1075f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 1076f141ed65SGrant Likely } 1077f141ed65SGrant Likely 10781020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 10791020dfd1SMasahiro Yamada return -EINVAL; 10801020dfd1SMasahiro Yamada 1081f0d1ab05SLinus Walleij ret = of_gpiochip_add_pin_range(chip); 1082f0d1ab05SLinus Walleij if (ret) 1083f0d1ab05SLinus Walleij return ret; 108428355f81STomeu Vizoso 10858afe8255SAndy Shevchenko fwnode_handle_get(chip->fwnode); 1086f625d460SBenoit Parrot 1087f0d1ab05SLinus Walleij ret = of_gpiochip_scan_gpios(chip); 10882f4133bbSAndy Shevchenko if (ret) 10898afe8255SAndy Shevchenko fwnode_handle_put(chip->fwnode); 1090f7299d44SGeert Uytterhoeven 1091f0d1ab05SLinus Walleij return ret; 1092f141ed65SGrant Likely } 1093f141ed65SGrant Likely 1094f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 1095f141ed65SGrant Likely { 10968afe8255SAndy Shevchenko fwnode_handle_put(chip->fwnode); 1097f141ed65SGrant Likely } 10984731210cSSaravana Kannan 10994731210cSSaravana Kannan void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev) 11004731210cSSaravana Kannan { 11011df62542SAndy Shevchenko /* Set default OF node to parent's one if present */ 11021df62542SAndy Shevchenko if (gc->parent) 11031df62542SAndy Shevchenko gdev->dev.of_node = gc->parent->of_node; 11041df62542SAndy Shevchenko 1105ac627260SBartosz Golaszewski if (gc->fwnode) 1106ac627260SBartosz Golaszewski gc->of_node = to_of_node(gc->fwnode); 1107ac627260SBartosz Golaszewski 11084731210cSSaravana Kannan /* If the gpiochip has an assigned OF node this takes precedence */ 11094731210cSSaravana Kannan if (gc->of_node) 11104731210cSSaravana Kannan gdev->dev.of_node = gc->of_node; 11114731210cSSaravana Kannan else 11124731210cSSaravana Kannan gc->of_node = gdev->dev.of_node; 11134731210cSSaravana Kannan } 1114