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 29762c2e46SMasahiro Yamada static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) 303d0f7cf0SGrant Likely { 31762c2e46SMasahiro Yamada return chip->gpiodev->dev.of_node == data; 327b8792bbSHans Holmberg } 333d0f7cf0SGrant Likely 34762c2e46SMasahiro Yamada static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) 35762c2e46SMasahiro Yamada { 36762c2e46SMasahiro Yamada return gpiochip_find(np, of_gpiochip_match_node); 373d0f7cf0SGrant Likely } 383d0f7cf0SGrant Likely 3999468c1aSMasahiro Yamada static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, 4099468c1aSMasahiro Yamada struct of_phandle_args *gpiospec, 4199468c1aSMasahiro Yamada enum of_gpio_flags *flags) 4299468c1aSMasahiro Yamada { 4399468c1aSMasahiro Yamada int ret; 4499468c1aSMasahiro Yamada 4599468c1aSMasahiro Yamada if (chip->of_gpio_n_cells != gpiospec->args_count) 4699468c1aSMasahiro Yamada return ERR_PTR(-EINVAL); 4799468c1aSMasahiro Yamada 4899468c1aSMasahiro Yamada ret = chip->of_xlate(chip, gpiospec, flags); 4999468c1aSMasahiro Yamada if (ret < 0) 5099468c1aSMasahiro Yamada return ERR_PTR(ret); 5199468c1aSMasahiro Yamada 5299468c1aSMasahiro Yamada return gpiochip_get_desc(chip, ret); 53f141ed65SGrant Likely } 54f141ed65SGrant Likely 55f141ed65SGrant Likely /** 56af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 57f141ed65SGrant Likely * @np: device node to get GPIO from 58f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 59f141ed65SGrant Likely * @index: index of the GPIO 60f141ed65SGrant Likely * @flags: a flags pointer to fill in 61f141ed65SGrant Likely * 62af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 63f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 64f141ed65SGrant Likely * in flags for the GPIO. 65f141ed65SGrant Likely */ 66af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 67af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 68f141ed65SGrant Likely { 69762c2e46SMasahiro Yamada struct of_phandle_args gpiospec; 70762c2e46SMasahiro Yamada struct gpio_chip *chip; 71762c2e46SMasahiro Yamada struct gpio_desc *desc; 72f141ed65SGrant Likely int ret; 73f141ed65SGrant Likely 743d0f7cf0SGrant Likely ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, 75762c2e46SMasahiro Yamada &gpiospec); 763d0f7cf0SGrant Likely if (ret) { 7785ea29acSTushar Behera pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n", 7885ea29acSTushar Behera __func__, propname, np->full_name, index); 79af8b6375SAlexandre Courbot return ERR_PTR(ret); 803d0f7cf0SGrant Likely } 81f141ed65SGrant Likely 82762c2e46SMasahiro Yamada chip = of_find_gpiochip_by_node(gpiospec.np); 83762c2e46SMasahiro Yamada if (!chip) { 84762c2e46SMasahiro Yamada desc = ERR_PTR(-EPROBE_DEFER); 85762c2e46SMasahiro Yamada goto out; 86762c2e46SMasahiro Yamada } 873d0f7cf0SGrant Likely 8899468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); 89762c2e46SMasahiro Yamada if (IS_ERR(desc)) 90762c2e46SMasahiro Yamada goto out; 91762c2e46SMasahiro Yamada 9285ea29acSTushar Behera pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n", 9385ea29acSTushar Behera __func__, propname, np->full_name, index, 94762c2e46SMasahiro Yamada PTR_ERR_OR_ZERO(desc)); 95762c2e46SMasahiro Yamada 96762c2e46SMasahiro Yamada out: 97762c2e46SMasahiro Yamada of_node_put(gpiospec.np); 98762c2e46SMasahiro Yamada 99762c2e46SMasahiro Yamada return desc; 100f141ed65SGrant Likely } 101f141ed65SGrant Likely 102f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 103f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 104f01d9075SAlexandre Courbot { 105f01d9075SAlexandre Courbot struct gpio_desc *desc; 106f01d9075SAlexandre Courbot 107f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 108f01d9075SAlexandre Courbot 109f01d9075SAlexandre Courbot if (IS_ERR(desc)) 110f01d9075SAlexandre Courbot return PTR_ERR(desc); 111f01d9075SAlexandre Courbot else 112f01d9075SAlexandre Courbot return desc_to_gpio(desc); 113f01d9075SAlexandre Courbot } 114f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 115f01d9075SAlexandre Courbot 116ea713bc4SLinus Walleij struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, 117ea713bc4SLinus Walleij unsigned int idx, 118ea713bc4SLinus Walleij enum gpio_lookup_flags *flags) 119ea713bc4SLinus Walleij { 120ea713bc4SLinus Walleij char prop_name[32]; /* 32 is max size of property name */ 121ea713bc4SLinus Walleij enum of_gpio_flags of_flags; 122ea713bc4SLinus Walleij struct gpio_desc *desc; 123ea713bc4SLinus Walleij unsigned int i; 124ea713bc4SLinus Walleij 125ea713bc4SLinus Walleij for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 126ea713bc4SLinus Walleij if (con_id) 127ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, 128ea713bc4SLinus Walleij gpio_suffixes[i]); 129ea713bc4SLinus Walleij else 130ea713bc4SLinus Walleij snprintf(prop_name, sizeof(prop_name), "%s", 131ea713bc4SLinus Walleij gpio_suffixes[i]); 132ea713bc4SLinus Walleij 133ea713bc4SLinus Walleij desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 134ea713bc4SLinus Walleij &of_flags); 135ea713bc4SLinus Walleij if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) 136ea713bc4SLinus Walleij break; 137ea713bc4SLinus Walleij } 138ea713bc4SLinus Walleij 139ea713bc4SLinus Walleij if (IS_ERR(desc)) 140ea713bc4SLinus Walleij return desc; 141ea713bc4SLinus Walleij 142ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 143ea713bc4SLinus Walleij *flags |= GPIO_ACTIVE_LOW; 144ea713bc4SLinus Walleij 145ea713bc4SLinus Walleij if (of_flags & OF_GPIO_SINGLE_ENDED) { 146ea713bc4SLinus Walleij if (of_flags & OF_GPIO_ACTIVE_LOW) 147ea713bc4SLinus Walleij *flags |= GPIO_OPEN_DRAIN; 148ea713bc4SLinus Walleij else 149ea713bc4SLinus Walleij *flags |= GPIO_OPEN_SOURCE; 150ea713bc4SLinus Walleij } 151ea713bc4SLinus Walleij 152ea713bc4SLinus Walleij return desc; 153ea713bc4SLinus Walleij } 154ea713bc4SLinus Walleij 155f141ed65SGrant Likely /** 156fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 157f625d460SBenoit Parrot * @np: device node to get GPIO from 158be715343SMasahiro Yamada * @chip: GPIO chip whose hog is parsed 159f625d460SBenoit Parrot * @name: GPIO line name 160f625d460SBenoit Parrot * @lflags: gpio_lookup_flags - returned from of_find_gpio() or 161fd7337fdSMarkus Pargmann * of_parse_own_gpio() 162f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 163f625d460SBenoit Parrot * 164f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 165f625d460SBenoit Parrot * value on the error condition. 166f625d460SBenoit Parrot */ 167fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 168be715343SMasahiro Yamada struct gpio_chip *chip, 169f625d460SBenoit Parrot const char **name, 170f625d460SBenoit Parrot enum gpio_lookup_flags *lflags, 171f625d460SBenoit Parrot enum gpiod_flags *dflags) 172f625d460SBenoit Parrot { 173f625d460SBenoit Parrot struct device_node *chip_np; 174f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 175be715343SMasahiro Yamada struct of_phandle_args gpiospec; 176be715343SMasahiro Yamada struct gpio_desc *desc; 177f625d460SBenoit Parrot u32 tmp; 1783f9547e1SMasahiro Yamada int ret; 179f625d460SBenoit Parrot 180be715343SMasahiro Yamada chip_np = chip->of_node; 181f625d460SBenoit Parrot if (!chip_np) 182f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 183f625d460SBenoit Parrot 184f625d460SBenoit Parrot xlate_flags = 0; 185f625d460SBenoit Parrot *lflags = 0; 186f625d460SBenoit Parrot *dflags = 0; 187f625d460SBenoit Parrot 188f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 189f625d460SBenoit Parrot if (ret) 190f625d460SBenoit Parrot return ERR_PTR(ret); 191f625d460SBenoit Parrot 192be715343SMasahiro Yamada gpiospec.np = chip_np; 193be715343SMasahiro Yamada gpiospec.args_count = tmp; 194f625d460SBenoit Parrot 195be715343SMasahiro Yamada ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp); 196f625d460SBenoit Parrot if (ret) 197f625d460SBenoit Parrot return ERR_PTR(ret); 198f625d460SBenoit Parrot 19999468c1aSMasahiro Yamada desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); 200be715343SMasahiro Yamada if (IS_ERR(desc)) 201be715343SMasahiro Yamada return desc; 202f625d460SBenoit Parrot 203f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 204f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 205f625d460SBenoit Parrot 206f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 207f625d460SBenoit Parrot *dflags |= GPIOD_IN; 208f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 209f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 210f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 211f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 212f625d460SBenoit Parrot else { 213f625d460SBenoit Parrot pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n", 214be715343SMasahiro Yamada desc_to_gpio(desc), np->name); 215f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 216f625d460SBenoit Parrot } 217f625d460SBenoit Parrot 218f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 219f625d460SBenoit Parrot *name = np->name; 220f625d460SBenoit Parrot 221be715343SMasahiro Yamada return desc; 222f625d460SBenoit Parrot } 223f625d460SBenoit Parrot 224f625d460SBenoit Parrot /** 225fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 226f625d460SBenoit Parrot * @chip: gpio chip to act on 227f625d460SBenoit Parrot * 228f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 229f625d460SBenoit Parrot * configuration. 230dfbd379bSLaxman Dewangan * It retures error if it fails otherwise 0 on success. 231f625d460SBenoit Parrot */ 232dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 233f625d460SBenoit Parrot { 234f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 235f625d460SBenoit Parrot struct device_node *np; 236f625d460SBenoit Parrot const char *name; 237f625d460SBenoit Parrot enum gpio_lookup_flags lflags; 238f625d460SBenoit Parrot enum gpiod_flags dflags; 239dfbd379bSLaxman Dewangan int ret; 240f625d460SBenoit Parrot 241d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 242f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 243f625d460SBenoit Parrot continue; 244f625d460SBenoit Parrot 245be715343SMasahiro Yamada desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags); 246f625d460SBenoit Parrot if (IS_ERR(desc)) 247f625d460SBenoit Parrot continue; 248f625d460SBenoit Parrot 249dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 250dfbd379bSLaxman Dewangan if (ret < 0) 251dfbd379bSLaxman Dewangan return ret; 252f625d460SBenoit Parrot } 253dfbd379bSLaxman Dewangan 254dfbd379bSLaxman Dewangan return 0; 255f625d460SBenoit Parrot } 256f625d460SBenoit Parrot 257f625d460SBenoit Parrot /** 258f141ed65SGrant Likely * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 259f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 260f141ed65SGrant Likely * @np: device node of the GPIO chip 261f141ed65SGrant Likely * @gpio_spec: gpio specifier as found in the device tree 262f141ed65SGrant Likely * @flags: a flags pointer to fill in 263f141ed65SGrant Likely * 264f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 265f141ed65SGrant Likely * gpio chips. This function performs only one sanity check: whether gpio 266f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 267f141ed65SGrant Likely */ 268f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 269f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 270f141ed65SGrant Likely { 271f141ed65SGrant Likely /* 272f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 27320a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 274f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 275f141ed65SGrant Likely * but not recommended). 276f141ed65SGrant Likely */ 277f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 278f141ed65SGrant Likely WARN_ON(1); 279f141ed65SGrant Likely return -EINVAL; 280f141ed65SGrant Likely } 281f141ed65SGrant Likely 282f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 283f141ed65SGrant Likely return -EINVAL; 284f141ed65SGrant Likely 2857b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 286f141ed65SGrant Likely return -EINVAL; 287f141ed65SGrant Likely 288f141ed65SGrant Likely if (flags) 289f141ed65SGrant Likely *flags = gpiospec->args[1]; 290f141ed65SGrant Likely 291f141ed65SGrant Likely return gpiospec->args[0]; 292f141ed65SGrant Likely } 293f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 294f141ed65SGrant Likely 295f141ed65SGrant Likely /** 2963208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 297f141ed65SGrant Likely * @np: device node of the GPIO chip 298f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 2993208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 300f141ed65SGrant Likely * 301f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 302f141ed65SGrant Likely * 303f141ed65SGrant Likely * 1) In the gpio_chip structure: 304f141ed65SGrant Likely * - all the callbacks 305f141ed65SGrant Likely * - of_gpio_n_cells 306f141ed65SGrant Likely * - of_xlate callback (optional) 307f141ed65SGrant Likely * 308f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 309f141ed65SGrant Likely * - save_regs callback (optional) 310f141ed65SGrant Likely * 311f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 312f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 313f141ed65SGrant Likely * to manage GPIOs from the callbacks. 314f141ed65SGrant Likely */ 3153208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 3163208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 3173208b0f0SLinus Walleij void *data) 318f141ed65SGrant Likely { 319f141ed65SGrant Likely int ret = -ENOMEM; 320f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 321f141ed65SGrant Likely 322f141ed65SGrant Likely gc->label = kstrdup(np->full_name, GFP_KERNEL); 323f141ed65SGrant Likely if (!gc->label) 324f141ed65SGrant Likely goto err0; 325f141ed65SGrant Likely 326f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 327f141ed65SGrant Likely if (!mm_gc->regs) 328f141ed65SGrant Likely goto err1; 329f141ed65SGrant Likely 330f141ed65SGrant Likely gc->base = -1; 331f141ed65SGrant Likely 332f141ed65SGrant Likely if (mm_gc->save_regs) 333f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 334f141ed65SGrant Likely 335f141ed65SGrant Likely mm_gc->gc.of_node = np; 336f141ed65SGrant Likely 3373208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 338f141ed65SGrant Likely if (ret) 339f141ed65SGrant Likely goto err2; 340f141ed65SGrant Likely 341f141ed65SGrant Likely return 0; 342f141ed65SGrant Likely err2: 343f141ed65SGrant Likely iounmap(mm_gc->regs); 344f141ed65SGrant Likely err1: 345f141ed65SGrant Likely kfree(gc->label); 346f141ed65SGrant Likely err0: 347f141ed65SGrant Likely pr_err("%s: GPIO chip registration failed with status %d\n", 348f141ed65SGrant Likely np->full_name, ret); 349f141ed65SGrant Likely return ret; 350f141ed65SGrant Likely } 3513208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data); 352f141ed65SGrant Likely 353d621e8baSRicardo Ribalda Delgado /** 354d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 355d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 356d621e8baSRicardo Ribalda Delgado */ 357d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 358d621e8baSRicardo Ribalda Delgado { 359d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 360d621e8baSRicardo Ribalda Delgado 361d621e8baSRicardo Ribalda Delgado if (!mm_gc) 362d621e8baSRicardo Ribalda Delgado return; 363d621e8baSRicardo Ribalda Delgado 364d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 365d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 366d621e8baSRicardo Ribalda Delgado kfree(gc->label); 367d621e8baSRicardo Ribalda Delgado } 368d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove); 369d621e8baSRicardo Ribalda Delgado 370f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 37128355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 372f23f1516SShiraz Hashim { 373f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 374f23f1516SShiraz Hashim struct of_phandle_args pinspec; 3751e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 376f23f1516SShiraz Hashim int index = 0, ret; 377586a87e6SChristian Ruppert const char *name; 378586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 379586a87e6SChristian Ruppert struct property *group_names; 380f23f1516SShiraz Hashim 381f23f1516SShiraz Hashim if (!np) 38228355f81STomeu Vizoso return 0; 383f23f1516SShiraz Hashim 384586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 385586a87e6SChristian Ruppert 386ad4e1a7cSHaojian Zhuang for (;; index++) { 387d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 388d9fe0039SStephen Warren index, &pinspec); 389f23f1516SShiraz Hashim if (ret) 390f23f1516SShiraz Hashim break; 391f23f1516SShiraz Hashim 3921e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 393602cf638SMasahiro Yamada of_node_put(pinspec.np); 3941e63d7b9SLinus Walleij if (!pctldev) 39528355f81STomeu Vizoso return -EPROBE_DEFER; 396f23f1516SShiraz Hashim 397586a87e6SChristian Ruppert if (pinspec.args[2]) { 398586a87e6SChristian Ruppert if (group_names) { 39972858602SLaurent Navet of_property_read_string_index(np, 400586a87e6SChristian Ruppert group_names_propname, 401586a87e6SChristian Ruppert index, &name); 402586a87e6SChristian Ruppert if (strlen(name)) { 403586a87e6SChristian Ruppert pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n", 404586a87e6SChristian Ruppert np->full_name); 405586a87e6SChristian Ruppert break; 406586a87e6SChristian Ruppert } 407586a87e6SChristian Ruppert } 408586a87e6SChristian Ruppert /* npins != 0: linear range */ 4091e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 410ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 4111e63d7b9SLinus Walleij pinspec.args[0], 41286853c83SHaojian Zhuang pinspec.args[1], 41386853c83SHaojian Zhuang pinspec.args[2]); 4141e63d7b9SLinus Walleij if (ret) 41528355f81STomeu Vizoso return ret; 416586a87e6SChristian Ruppert } else { 417586a87e6SChristian Ruppert /* npins == 0: special range */ 418586a87e6SChristian Ruppert if (pinspec.args[1]) { 419586a87e6SChristian Ruppert pr_err("%s: Illegal gpio-range format.\n", 420586a87e6SChristian Ruppert np->full_name); 421586a87e6SChristian Ruppert break; 422586a87e6SChristian Ruppert } 423586a87e6SChristian Ruppert 424586a87e6SChristian Ruppert if (!group_names) { 425586a87e6SChristian Ruppert pr_err("%s: GPIO group range requested but no %s property.\n", 426586a87e6SChristian Ruppert np->full_name, group_names_propname); 427586a87e6SChristian Ruppert break; 428586a87e6SChristian Ruppert } 429586a87e6SChristian Ruppert 430586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 431586a87e6SChristian Ruppert group_names_propname, 432586a87e6SChristian Ruppert index, &name); 433586a87e6SChristian Ruppert if (ret) 434586a87e6SChristian Ruppert break; 435586a87e6SChristian Ruppert 436586a87e6SChristian Ruppert if (!strlen(name)) { 437586a87e6SChristian Ruppert pr_err("%s: Group name of GPIO group range cannot be the empty string.\n", 438586a87e6SChristian Ruppert np->full_name); 439586a87e6SChristian Ruppert break; 440586a87e6SChristian Ruppert } 441586a87e6SChristian Ruppert 442586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 443586a87e6SChristian Ruppert pinspec.args[0], name); 444586a87e6SChristian Ruppert if (ret) 44528355f81STomeu Vizoso return ret; 446586a87e6SChristian Ruppert } 447ad4e1a7cSHaojian Zhuang } 44828355f81STomeu Vizoso 44928355f81STomeu Vizoso return 0; 450f23f1516SShiraz Hashim } 451f23f1516SShiraz Hashim 452f23f1516SShiraz Hashim #else 45328355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 454f23f1516SShiraz Hashim #endif 455f23f1516SShiraz Hashim 45628355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 457f141ed65SGrant Likely { 45828355f81STomeu Vizoso int status; 45928355f81STomeu Vizoso 46058383c78SLinus Walleij if ((!chip->of_node) && (chip->parent)) 46158383c78SLinus Walleij chip->of_node = chip->parent->of_node; 462f141ed65SGrant Likely 463f141ed65SGrant Likely if (!chip->of_node) 46428355f81STomeu Vizoso return 0; 465f141ed65SGrant Likely 466f141ed65SGrant Likely if (!chip->of_xlate) { 467f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 468f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 469f141ed65SGrant Likely } 470f141ed65SGrant Likely 4711020dfd1SMasahiro Yamada if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) 4721020dfd1SMasahiro Yamada return -EINVAL; 4731020dfd1SMasahiro Yamada 47428355f81STomeu Vizoso status = of_gpiochip_add_pin_range(chip); 47528355f81STomeu Vizoso if (status) 47628355f81STomeu Vizoso return status; 47728355f81STomeu Vizoso 478fd9c5531SLinus Walleij /* If the chip defines names itself, these take precedence */ 479fd9c5531SLinus Walleij if (!chip->names) 480*9427ecbeSMika Westerberg devprop_gpiochip_set_names(chip); 481fd9c5531SLinus Walleij 482f141ed65SGrant Likely of_node_get(chip->of_node); 483f625d460SBenoit Parrot 484dfbd379bSLaxman Dewangan return of_gpiochip_scan_gpios(chip); 485f141ed65SGrant Likely } 486f141ed65SGrant Likely 487f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 488f141ed65SGrant Likely { 489e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 490f141ed65SGrant Likely of_node_put(chip->of_node); 491f141ed65SGrant Likely } 492