1f141ed65SGrant Likely /* 2f141ed65SGrant Likely * OF helpers for the GPIO API 3f141ed65SGrant Likely * 4f141ed65SGrant Likely * Copyright (c) 2007-2008 MontaVista Software, Inc. 5f141ed65SGrant Likely * 6f141ed65SGrant Likely * Author: Anton Vorontsov <avorontsov@ru.mvista.com> 7f141ed65SGrant Likely * 8f141ed65SGrant Likely * This program is free software; you can redistribute it and/or modify 9f141ed65SGrant Likely * it under the terms of the GNU General Public License as published by 10f141ed65SGrant Likely * the Free Software Foundation; either version 2 of the License, or 11f141ed65SGrant Likely * (at your option) any later version. 12f141ed65SGrant Likely */ 13f141ed65SGrant Likely 14f141ed65SGrant Likely #include <linux/device.h> 15bea4dbeeSSachin Kamat #include <linux/err.h> 16f141ed65SGrant Likely #include <linux/errno.h> 17f141ed65SGrant Likely #include <linux/module.h> 18f141ed65SGrant Likely #include <linux/io.h> 19af8b6375SAlexandre Courbot #include <linux/gpio/consumer.h> 20f141ed65SGrant Likely #include <linux/of.h> 21f141ed65SGrant Likely #include <linux/of_address.h> 22f141ed65SGrant Likely #include <linux/of_gpio.h> 23f23f1516SShiraz Hashim #include <linux/pinctrl/pinctrl.h> 24f141ed65SGrant Likely #include <linux/slab.h> 25f625d460SBenoit Parrot #include <linux/gpio/machine.h> 26f141ed65SGrant Likely 271bd6b601SAlexandre Courbot #include "gpiolib.h" 28af8b6375SAlexandre Courbot 29c7e9d398SMasahiro Yamada static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) 303d0f7cf0SGrant Likely { 31c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec = data; 32c7e9d398SMasahiro Yamada 33c7e9d398SMasahiro Yamada return chip->gpiodev->dev.of_node == gpiospec->np && 34c7e9d398SMasahiro Yamada chip->of_xlate(chip, gpiospec, NULL) >= 0; 357b8792bbSHans Holmberg } 363d0f7cf0SGrant Likely 37c7e9d398SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_xlate( 38c7e9d398SMasahiro Yamada struct of_phandle_args *gpiospec) 39762c2e46SMasahiro Yamada { 40c7e9d398SMasahiro Yamada return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); 413d0f7cf0SGrant Likely } 423d0f7cf0SGrant Likely 4399468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, 4499468c1aSMasahiro Yamada struct of_phandle_args *gpiospec, 4599468c1aSMasahiro Yamada enum of_gpio_flags *flags) 4699468c1aSMasahiro Yamada { 4799468c1aSMasahiro Yamada int ret; 4899468c1aSMasahiro Yamada 4999468c1aSMasahiro Yamada if (chip->of_gpio_n_cells != gpiospec->args_count) 5099468c1aSMasahiro Yamada return ERR_PTR(-EINVAL); 5199468c1aSMasahiro Yamada 5299468c1aSMasahiro Yamada ret = chip->of_xlate(chip, gpiospec, flags); 5399468c1aSMasahiro Yamada if (ret < 0) 5499468c1aSMasahiro Yamada return ERR_PTR(ret); 5599468c1aSMasahiro Yamada 5699468c1aSMasahiro Yamada return gpiochip_get_desc(chip, ret); 57f141ed65SGrant Likely } 58f141ed65SGrant Likely 59f141ed65SGrant Likely /** 60af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 61f141ed65SGrant Likely * @np: device node to get GPIO from 62f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 63f141ed65SGrant Likely * @index: index of the GPIO 64f141ed65SGrant Likely * @flags: a flags pointer to fill in 65f141ed65SGrant Likely * 66af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 67f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 68f141ed65SGrant Likely * in flags for the GPIO. 69f141ed65SGrant Likely */ 70af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 71af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 72f141ed65SGrant Likely { 73762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 74762c2e46SMasahiro Yamada struct gpio_chip *chip; 75762c2e46SMasahiro Yamada struct gpio_desc *desc; 76f141ed65SGrant Likely int ret; 77f141ed65SGrant Likely 783d0f7cf0SGrant Likely ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, 79762c2e46SMasahiro Yamada &gpiospec); 803d0f7cf0SGrant Likely if (ret) { 8185ea29acSTushar Behera pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n", 8285ea29acSTushar Behera __func__, propname, np->full_name, index); 83af8b6375SAlexandre Courbot return ERR_PTR(ret); 843d0f7cf0SGrant Likely } 85f141ed65SGrant Likely 86c7e9d398SMasahiro Yamada chip = of_find_gpiochip_by_xlate(&gpiospec); 87762c2e46SMasahiro Yamada if (!chip) { 88762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 89762c2e46SMasahiro Yamada goto out; 90762c2e46SMasahiro Yamada } 913d0f7cf0SGrant Likely 9299468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 93762c2e46SMasahiro Yamada if (IS_ERR(desc)) 94762c2e46SMasahiro Yamada goto out; 95762c2e46SMasahiro Yamada 9685ea29acSTushar Behera pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n", 9785ea29acSTushar Behera __func__, propname, np->full_name, index, 98762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 99762c2e46SMasahiro Yamada 100762c2e46SMasahiro Yamada out: 101762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 102762c2e46SMasahiro Yamada 103762c2e46SMasahiro Yamada return desc; 104f141ed65SGrant Likely } 105f141ed65SGrant Likely 106f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 107f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 108f01d9075SAlexandre Courbot { 109f01d9075SAlexandre Courbot struct gpio_desc *desc; 110f01d9075SAlexandre Courbot 111f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 112f01d9075SAlexandre Courbot 113f01d9075SAlexandre Courbot if (IS_ERR(desc)) 114f01d9075SAlexandre Courbot return PTR_ERR(desc); 115f01d9075SAlexandre Courbot else 116f01d9075SAlexandre Courbot return desc_to_gpio(desc); 117f01d9075SAlexandre Courbot } 118f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 119f01d9075SAlexandre Courbot 120ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 121ea713bc4SLinus Walleij unsigned int idx, 122ea713bc4SLinus Walleij enum gpio_lookup_flags *flags) 123ea713bc4SLinus Walleij { 124ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 125ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 126ea713bc4SLinus Walleij struct gpio_desc *desc; 127ea713bc4SLinus Walleij unsigned int i; 128ea713bc4SLinus Walleij 129ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 130ea713bc4SLinus Walleij if (con_id) 131ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 132ea713bc4SLinus Walleij gpio_suffixes[i]); 133ea713bc4SLinus Walleij else 134ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 135ea713bc4SLinus Walleij gpio_suffixes[i]); 136ea713bc4SLinus Walleij 137ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 138ea713bc4SLinus Walleij &of_flags); 139ea713bc4SLinus Walleij if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) 140ea713bc4SLinus Walleij break; 141ea713bc4SLinus Walleij } 142ea713bc4SLinus Walleij 143ea713bc4SLinus Walleij if (IS_ERR(desc)) 144ea713bc4SLinus Walleij return desc; 145ea713bc4SLinus Walleij 146ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 147ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 148ea713bc4SLinus Walleij 149ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 1504c0facddSLaxman Dewangan if (of_flags & OF_GPIO_OPEN_DRAIN) 151ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 152ea713bc4SLinus Walleij else 153ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 154ea713bc4SLinus Walleij } 155ea713bc4SLinus Walleij 15605f479bfSCharles Keepax if (of_flags & OF_GPIO_SLEEP_MAY_LOOSE_VALUE) 15705f479bfSCharles Keepax *flags |= GPIO_SLEEP_MAY_LOOSE_VALUE; 15805f479bfSCharles Keepax 159ea713bc4SLinus Walleij return desc; 160ea713bc4SLinus Walleij } 161ea713bc4SLinus Walleij 162f141ed65SGrant Likely /** 163fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 164f625d460SBenoit Parrot * @np: device node to get GPIO from 165be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 166a79fead5SGeert Uytterhoeven * @idx: Index of the GPIO to parse 167f625d460SBenoit Parrot * @name: GPIO line name 168f625d460SBenoit Parrot * @lflags: gpio_lookup_flags - returned from of_find_gpio() or 169fd7337fdSMarkus Pargmann * of_parse_own_gpio() 170f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 171f625d460SBenoit Parrot * 172f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 173f625d460SBenoit Parrot * value on the error condition. 174f625d460SBenoit Parrot */ 175fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 176be715343SMasahiro Yamada struct gpio_chip *chip, 177a79fead5SGeert Uytterhoeven unsigned int idx, const char **name, 178f625d460SBenoit Parrot enum gpio_lookup_flags *lflags, 179f625d460SBenoit Parrot enum gpiod_flags *dflags) 180f625d460SBenoit Parrot { 181f625d460SBenoit Parrot struct device_node *chip_np; 182f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 183be715343SMasahiro Yamada struct of_phandle_args gpiospec; 184be715343SMasahiro Yamada struct gpio_desc *desc; 185a79fead5SGeert Uytterhoeven unsigned int i; 186f625d460SBenoit Parrot u32 tmp; 1873f9547e1SMasahiro Yamada int ret; 188f625d460SBenoit Parrot 189be715343SMasahiro Yamada chip_np = chip->of_node; 190f625d460SBenoit Parrot if (!chip_np) 191f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 192f625d460SBenoit Parrot 193f625d460SBenoit Parrot xlate_flags = 0; 194f625d460SBenoit Parrot *lflags = 0; 195f625d460SBenoit Parrot *dflags = 0; 196f625d460SBenoit Parrot 197f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 198f625d460SBenoit Parrot if (ret) 199f625d460SBenoit Parrot return ERR_PTR(ret); 200f625d460SBenoit Parrot 201be715343SMasahiro Yamada gpiospec.np = chip_np; 202be715343SMasahiro Yamada gpiospec.args_count = tmp; 203f625d460SBenoit Parrot 204a79fead5SGeert Uytterhoeven for (i = 0; i < tmp; i++) { 205a79fead5SGeert Uytterhoeven ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, 206a79fead5SGeert Uytterhoeven &gpiospec.args[i]); 207f625d460SBenoit Parrot if (ret) 208f625d460SBenoit Parrot return ERR_PTR(ret); 209a79fead5SGeert Uytterhoeven } 210f625d460SBenoit Parrot 21199468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 212be715343SMasahiro Yamada if (IS_ERR(desc)) 213be715343SMasahiro Yamada return desc; 214f625d460SBenoit Parrot 215f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 216f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 217f625d460SBenoit Parrot 218f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 219f625d460SBenoit Parrot *dflags |= GPIOD_IN; 220f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 221f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 222f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 223f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 224f625d460SBenoit Parrot else { 225f625d460SBenoit Parrot pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n", 226be715343SMasahiro Yamada desc_to_gpio(desc), np->name); 227f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 228f625d460SBenoit Parrot } 229f625d460SBenoit Parrot 230f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 231f625d460SBenoit Parrot *name = np->name; 232f625d460SBenoit Parrot 233be715343SMasahiro Yamada return desc; 234f625d460SBenoit Parrot } 235f625d460SBenoit Parrot 236f625d460SBenoit Parrot /** 237fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 238f625d460SBenoit Parrot * @chip: gpio chip to act on 239f625d460SBenoit Parrot * 240f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 241f625d460SBenoit Parrot * configuration. 242dfbd379bSLaxman Dewangan * It retures error if it fails otherwise 0 on success. 243f625d460SBenoit Parrot */ 244dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 245f625d460SBenoit Parrot { 246f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 247f625d460SBenoit Parrot struct device_node *np; 248f625d460SBenoit Parrot const char *name; 249f625d460SBenoit Parrot enum gpio_lookup_flags lflags; 250f625d460SBenoit Parrot enum gpiod_flags dflags; 251a79fead5SGeert Uytterhoeven unsigned int i; 252dfbd379bSLaxman Dewangan int ret; 253f625d460SBenoit Parrot 254d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 255f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 256f625d460SBenoit Parrot continue; 257f625d460SBenoit Parrot 258a79fead5SGeert Uytterhoeven for (i = 0;; i++) { 259a79fead5SGeert Uytterhoeven desc = of_parse_own_gpio(np, chip, i, &name, &lflags, 260a79fead5SGeert Uytterhoeven &dflags); 261f625d460SBenoit Parrot if (IS_ERR(desc)) 262a79fead5SGeert Uytterhoeven break; 263f625d460SBenoit Parrot 264dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 26509e258afSWei Yongjun if (ret < 0) { 26609e258afSWei Yongjun of_node_put(np); 267dfbd379bSLaxman Dewangan return ret; 268f625d460SBenoit Parrot } 26909e258afSWei Yongjun } 270a79fead5SGeert Uytterhoeven } 271dfbd379bSLaxman Dewangan 272dfbd379bSLaxman Dewangan return 0; 273f625d460SBenoit Parrot } 274f625d460SBenoit Parrot 275f625d460SBenoit Parrot /** 276f141ed65SGrant Likely * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 277f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 278f141ed65SGrant Likely * @np: device node of the GPIO chip 279f141ed65SGrant Likely * @gpio_spec: gpio specifier as found in the device tree 280f141ed65SGrant Likely * @flags: a flags pointer to fill in 281f141ed65SGrant Likely * 282f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 283f141ed65SGrant Likely * gpio chips. This function performs only one sanity check: whether gpio 284f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 285f141ed65SGrant Likely */ 286f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 287f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 288f141ed65SGrant Likely { 289f141ed65SGrant Likely /* 290f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 29120a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 292f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 293f141ed65SGrant Likely * but not recommended). 294f141ed65SGrant Likely */ 295f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 296f141ed65SGrant Likely WARN_ON(1); 297f141ed65SGrant Likely return -EINVAL; 298f141ed65SGrant Likely } 299f141ed65SGrant Likely 300f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 301f141ed65SGrant Likely return -EINVAL; 302f141ed65SGrant Likely 3037b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 304f141ed65SGrant Likely return -EINVAL; 305f141ed65SGrant Likely 306f141ed65SGrant Likely if (flags) 307f141ed65SGrant Likely *flags = gpiospec->args[1]; 308f141ed65SGrant Likely 309f141ed65SGrant Likely return gpiospec->args[0]; 310f141ed65SGrant Likely } 311f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 312f141ed65SGrant Likely 313f141ed65SGrant Likely /** 3143208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 315f141ed65SGrant Likely * @np: device node of the GPIO chip 316f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 3173208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 318f141ed65SGrant Likely * 319f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 320f141ed65SGrant Likely * 321f141ed65SGrant Likely * 1) In the gpio_chip structure: 322f141ed65SGrant Likely * - all the callbacks 323f141ed65SGrant Likely * - of_gpio_n_cells 324f141ed65SGrant Likely * - of_xlate callback (optional) 325f141ed65SGrant Likely * 326f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 327f141ed65SGrant Likely * - save_regs callback (optional) 328f141ed65SGrant Likely * 329f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 330f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 331f141ed65SGrant Likely * to manage GPIOs from the callbacks. 332f141ed65SGrant Likely */ 3333208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 3343208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 3353208b0f0SLinus Walleij void *data) 336f141ed65SGrant Likely { 337f141ed65SGrant Likely int ret = -ENOMEM; 338f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 339f141ed65SGrant Likely 340f141ed65SGrant Likely gc->label = kstrdup(np->full_name, GFP_KERNEL); 341f141ed65SGrant Likely if (!gc->label) 342f141ed65SGrant Likely goto err0; 343f141ed65SGrant Likely 344f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 345f141ed65SGrant Likely if (!mm_gc->regs) 346f141ed65SGrant Likely goto err1; 347f141ed65SGrant Likely 348f141ed65SGrant Likely gc->base = -1; 349f141ed65SGrant Likely 350f141ed65SGrant Likely if (mm_gc->save_regs) 351f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 352f141ed65SGrant Likely 353f141ed65SGrant Likely mm_gc->gc.of_node = np; 354f141ed65SGrant Likely 3553208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 356f141ed65SGrant Likely if (ret) 357f141ed65SGrant Likely goto err2; 358f141ed65SGrant Likely 359f141ed65SGrant Likely return 0; 360f141ed65SGrant Likely err2: 361f141ed65SGrant Likely iounmap(mm_gc->regs); 362f141ed65SGrant Likely err1: 363f141ed65SGrant Likely kfree(gc->label); 364f141ed65SGrant Likely err0: 365f141ed65SGrant Likely pr_err("%s: GPIO chip registration failed with status %d\n", 366f141ed65SGrant Likely np->full_name, ret); 367f141ed65SGrant Likely return ret; 368f141ed65SGrant Likely } 3693208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data); 370f141ed65SGrant Likely 371d621e8baSRicardo Ribalda Delgado /** 372d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 373d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 374d621e8baSRicardo Ribalda Delgado */ 375d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 376d621e8baSRicardo Ribalda Delgado { 377d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 378d621e8baSRicardo Ribalda Delgado 379d621e8baSRicardo Ribalda Delgado if (!mm_gc) 380d621e8baSRicardo Ribalda Delgado return; 381d621e8baSRicardo Ribalda Delgado 382d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 383d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 384d621e8baSRicardo Ribalda Delgado kfree(gc->label); 385d621e8baSRicardo Ribalda Delgado } 386d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove); 387d621e8baSRicardo Ribalda Delgado 388f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 38928355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 390f23f1516SShiraz Hashim { 391f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 392f23f1516SShiraz Hashim struct of_phandle_args pinspec; 3931e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 394f23f1516SShiraz Hashim int index = 0, ret; 395586a87e6SChristian Ruppert const char *name; 396586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 397586a87e6SChristian Ruppert struct property *group_names; 398f23f1516SShiraz Hashim 399f23f1516SShiraz Hashim if (!np) 40028355f81STomeu Vizoso return 0; 401f23f1516SShiraz Hashim 402586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 403586a87e6SChristian Ruppert 404ad4e1a7cSHaojian Zhuang for (;; index++) { 405d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 406d9fe0039SStephen Warren index, &pinspec); 407f23f1516SShiraz Hashim if (ret) 408f23f1516SShiraz Hashim break; 409f23f1516SShiraz Hashim 4101e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 411602cf638SMasahiro Yamada of_node_put(pinspec.np); 4121e63d7b9SLinus Walleij if (!pctldev) 41328355f81STomeu Vizoso return -EPROBE_DEFER; 414f23f1516SShiraz Hashim 415586a87e6SChristian Ruppert if (pinspec.args[2]) { 416586a87e6SChristian Ruppert if (group_names) { 41772858602SLaurent Navet of_property_read_string_index(np, 418586a87e6SChristian Ruppert group_names_propname, 419586a87e6SChristian Ruppert index, &name); 420586a87e6SChristian Ruppert if (strlen(name)) { 421586a87e6SChristian Ruppert pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n", 422586a87e6SChristian Ruppert np->full_name); 423586a87e6SChristian Ruppert break; 424586a87e6SChristian Ruppert } 425586a87e6SChristian Ruppert } 426586a87e6SChristian Ruppert /* npins != 0: linear range */ 4271e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 428ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 4291e63d7b9SLinus Walleij pinspec.args[0], 43086853c83SHaojian Zhuang pinspec.args[1], 43186853c83SHaojian Zhuang pinspec.args[2]); 4321e63d7b9SLinus Walleij if (ret) 43328355f81STomeu Vizoso return ret; 434586a87e6SChristian Ruppert } else { 435586a87e6SChristian Ruppert /* npins == 0: special range */ 436586a87e6SChristian Ruppert if (pinspec.args[1]) { 437586a87e6SChristian Ruppert pr_err("%s: Illegal gpio-range format.\n", 438586a87e6SChristian Ruppert np->full_name); 439586a87e6SChristian Ruppert break; 440586a87e6SChristian Ruppert } 441586a87e6SChristian Ruppert 442586a87e6SChristian Ruppert if (!group_names) { 443586a87e6SChristian Ruppert pr_err("%s: GPIO group range requested but no %s property.\n", 444586a87e6SChristian Ruppert np->full_name, group_names_propname); 445586a87e6SChristian Ruppert break; 446586a87e6SChristian Ruppert } 447586a87e6SChristian Ruppert 448586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 449586a87e6SChristian Ruppert group_names_propname, 450586a87e6SChristian Ruppert index, &name); 451586a87e6SChristian Ruppert if (ret) 452586a87e6SChristian Ruppert break; 453586a87e6SChristian Ruppert 454586a87e6SChristian Ruppert if (!strlen(name)) { 455586a87e6SChristian Ruppert pr_err("%s: Group name of GPIO group range cannot be the empty string.\n", 456586a87e6SChristian Ruppert np->full_name); 457586a87e6SChristian Ruppert break; 458586a87e6SChristian Ruppert } 459586a87e6SChristian Ruppert 460586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 461586a87e6SChristian Ruppert pinspec.args[0], name); 462586a87e6SChristian Ruppert if (ret) 46328355f81STomeu Vizoso return ret; 464586a87e6SChristian Ruppert } 465ad4e1a7cSHaojian Zhuang } 46628355f81STomeu Vizoso 46728355f81STomeu Vizoso return 0; 468f23f1516SShiraz Hashim } 469f23f1516SShiraz Hashim 470f23f1516SShiraz Hashim #else 47128355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 472f23f1516SShiraz Hashim #endif 473f23f1516SShiraz Hashim 47428355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 475f141ed65SGrant Likely { 47628355f81STomeu Vizoso int status; 47728355f81STomeu Vizoso 47858383c78SLinus Walleij if ((!chip->of_node) && (chip->parent)) 47958383c78SLinus Walleij chip->of_node = chip->parent->of_node; 480f141ed65SGrant Likely 481f141ed65SGrant Likely if (!chip->of_node) 48228355f81STomeu Vizoso return 0; 483f141ed65SGrant Likely 484f141ed65SGrant Likely if (!chip->of_xlate) { 485f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 486f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 487f141ed65SGrant Likely } 488f141ed65SGrant Likely 4891020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 4901020dfd1SMasahiro Yamada return -EINVAL; 4911020dfd1SMasahiro Yamada 49228355f81STomeu Vizoso status = of_gpiochip_add_pin_range(chip); 49328355f81STomeu Vizoso if (status) 49428355f81STomeu Vizoso return status; 49528355f81STomeu Vizoso 496fd9c5531SLinus Walleij /* If the chip defines names itself, these take precedence */ 497fd9c5531SLinus Walleij if (!chip->names) 4989427ecbeSMika Westerberg devprop_gpiochip_set_names(chip); 499fd9c5531SLinus Walleij 500f141ed65SGrant Likely of_node_get(chip->of_node); 501f625d460SBenoit Parrot 502dfbd379bSLaxman Dewangan return of_gpiochip_scan_gpios(chip); 503f141ed65SGrant Likely } 504f141ed65SGrant Likely 505f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 506f141ed65SGrant Likely { 507e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 508f141ed65SGrant Likely of_node_put(chip->of_node); 509f141ed65SGrant Likely } 510