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 290df2c999SDong Aisheng /* Private data structure for of_gpiochip_find_and_xlate */ 303d0f7cf0SGrant Likely struct gg_data { 313d0f7cf0SGrant Likely enum of_gpio_flags *flags; 323d0f7cf0SGrant Likely struct of_phandle_args gpiospec; 333d0f7cf0SGrant Likely 34af8b6375SAlexandre Courbot struct gpio_desc *out_gpio; 353d0f7cf0SGrant Likely }; 363d0f7cf0SGrant Likely 373d0f7cf0SGrant Likely /* Private function for resolving node pointer to gpio_chip */ 383d0f7cf0SGrant Likely static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data) 393d0f7cf0SGrant Likely { 403d0f7cf0SGrant Likely struct gg_data *gg_data = data; 413d0f7cf0SGrant Likely int ret; 423d0f7cf0SGrant Likely 433d0f7cf0SGrant Likely if ((gc->of_node != gg_data->gpiospec.np) || 443d0f7cf0SGrant Likely (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) || 453d0f7cf0SGrant Likely (!gc->of_xlate)) 463d0f7cf0SGrant Likely return false; 473d0f7cf0SGrant Likely 483d0f7cf0SGrant Likely ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags); 497b8792bbSHans Holmberg if (ret < 0) { 509cf75e9eSHans Holmberg /* We've found a gpio chip, but the translation failed. 519cf75e9eSHans Holmberg * Store translation error in out_gpio. 529cf75e9eSHans Holmberg * Return false to keep looking, as more than one gpio chip 539cf75e9eSHans Holmberg * could be registered per of-node. 547b8792bbSHans Holmberg */ 557b8792bbSHans Holmberg gg_data->out_gpio = ERR_PTR(ret); 569cf75e9eSHans Holmberg return false; 577b8792bbSHans Holmberg } 583d0f7cf0SGrant Likely 59ccd9726eSLinus Walleij gg_data->out_gpio = gpiochip_get_desc(gc, ret); 603d0f7cf0SGrant Likely return true; 613d0f7cf0SGrant Likely } 623d0f7cf0SGrant Likely 63f141ed65SGrant Likely /** 64af8b6375SAlexandre Courbot * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API 65f141ed65SGrant Likely * @np: device node to get GPIO from 66f141ed65SGrant Likely * @propname: property name containing gpio specifier(s) 67f141ed65SGrant Likely * @index: index of the GPIO 68f141ed65SGrant Likely * @flags: a flags pointer to fill in 69f141ed65SGrant Likely * 70af8b6375SAlexandre Courbot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 71f141ed65SGrant Likely * value on the error condition. If @flags is not NULL the function also fills 72f141ed65SGrant Likely * in flags for the GPIO. 73f141ed65SGrant Likely */ 74af8b6375SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 75af8b6375SAlexandre Courbot const char *propname, int index, enum of_gpio_flags *flags) 76f141ed65SGrant Likely { 774fbb0022SRoland Stigge /* Return -EPROBE_DEFER to support probe() functions to be called 784fbb0022SRoland Stigge * later when the GPIO actually becomes available 794fbb0022SRoland Stigge */ 80af8b6375SAlexandre Courbot struct gg_data gg_data = { 81af8b6375SAlexandre Courbot .flags = flags, 82af8b6375SAlexandre Courbot .out_gpio = ERR_PTR(-EPROBE_DEFER) 83af8b6375SAlexandre Courbot }; 84f141ed65SGrant Likely int ret; 85f141ed65SGrant Likely 863d0f7cf0SGrant Likely /* .of_xlate might decide to not fill in the flags, so clear it. */ 87f141ed65SGrant Likely if (flags) 88f141ed65SGrant Likely *flags = 0; 89f141ed65SGrant Likely 903d0f7cf0SGrant Likely ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, 913d0f7cf0SGrant Likely &gg_data.gpiospec); 923d0f7cf0SGrant Likely if (ret) { 9385ea29acSTushar Behera pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n", 9485ea29acSTushar Behera __func__, propname, np->full_name, index); 95af8b6375SAlexandre Courbot return ERR_PTR(ret); 963d0f7cf0SGrant Likely } 97f141ed65SGrant Likely 983d0f7cf0SGrant Likely gpiochip_find(&gg_data, of_gpiochip_find_and_xlate); 993d0f7cf0SGrant Likely 1003d0f7cf0SGrant Likely of_node_put(gg_data.gpiospec.np); 10185ea29acSTushar Behera pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n", 10285ea29acSTushar Behera __func__, propname, np->full_name, index, 103bea4dbeeSSachin Kamat PTR_ERR_OR_ZERO(gg_data.out_gpio)); 1043d0f7cf0SGrant Likely return gg_data.out_gpio; 105f141ed65SGrant Likely } 106f141ed65SGrant Likely 107f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 108f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 109f01d9075SAlexandre Courbot { 110f01d9075SAlexandre Courbot struct gpio_desc *desc; 111f01d9075SAlexandre Courbot 112f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 113f01d9075SAlexandre Courbot 114f01d9075SAlexandre Courbot if (IS_ERR(desc)) 115f01d9075SAlexandre Courbot return PTR_ERR(desc); 116f01d9075SAlexandre Courbot else 117f01d9075SAlexandre Courbot return desc_to_gpio(desc); 118f01d9075SAlexandre Courbot } 119f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 120f01d9075SAlexandre Courbot 121f141ed65SGrant Likely /** 122fd7337fdSMarkus Pargmann * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API 123f625d460SBenoit Parrot * @np: device node to get GPIO from 124f625d460SBenoit Parrot * @name: GPIO line name 125f625d460SBenoit Parrot * @lflags: gpio_lookup_flags - returned from of_find_gpio() or 126fd7337fdSMarkus Pargmann * of_parse_own_gpio() 127f625d460SBenoit Parrot * @dflags: gpiod_flags - optional GPIO initialization flags 128f625d460SBenoit Parrot * 129f625d460SBenoit Parrot * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno 130f625d460SBenoit Parrot * value on the error condition. 131f625d460SBenoit Parrot */ 132fd7337fdSMarkus Pargmann static struct gpio_desc *of_parse_own_gpio(struct device_node *np, 133f625d460SBenoit Parrot const char **name, 134f625d460SBenoit Parrot enum gpio_lookup_flags *lflags, 135f625d460SBenoit Parrot enum gpiod_flags *dflags) 136f625d460SBenoit Parrot { 137f625d460SBenoit Parrot struct device_node *chip_np; 138f625d460SBenoit Parrot enum of_gpio_flags xlate_flags; 139f625d460SBenoit Parrot struct gg_data gg_data = { 140f625d460SBenoit Parrot .flags = &xlate_flags, 141f625d460SBenoit Parrot }; 142f625d460SBenoit Parrot u32 tmp; 143f625d460SBenoit Parrot int i, ret; 144f625d460SBenoit Parrot 145f625d460SBenoit Parrot chip_np = np->parent; 146f625d460SBenoit Parrot if (!chip_np) 147f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 148f625d460SBenoit Parrot 149f625d460SBenoit Parrot xlate_flags = 0; 150f625d460SBenoit Parrot *lflags = 0; 151f625d460SBenoit Parrot *dflags = 0; 152f625d460SBenoit Parrot 153f625d460SBenoit Parrot ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); 154f625d460SBenoit Parrot if (ret) 155f625d460SBenoit Parrot return ERR_PTR(ret); 156f625d460SBenoit Parrot 157f625d460SBenoit Parrot if (tmp > MAX_PHANDLE_ARGS) 158f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 159f625d460SBenoit Parrot 160f625d460SBenoit Parrot gg_data.gpiospec.args_count = tmp; 161f625d460SBenoit Parrot gg_data.gpiospec.np = chip_np; 162f625d460SBenoit Parrot for (i = 0; i < tmp; i++) { 163f625d460SBenoit Parrot ret = of_property_read_u32_index(np, "gpios", i, 164f625d460SBenoit Parrot &gg_data.gpiospec.args[i]); 165f625d460SBenoit Parrot if (ret) 166f625d460SBenoit Parrot return ERR_PTR(ret); 167f625d460SBenoit Parrot } 168f625d460SBenoit Parrot 169f625d460SBenoit Parrot gpiochip_find(&gg_data, of_gpiochip_find_and_xlate); 170f625d460SBenoit Parrot if (!gg_data.out_gpio) { 171f625d460SBenoit Parrot if (np->parent == np) 172f625d460SBenoit Parrot return ERR_PTR(-ENXIO); 173f625d460SBenoit Parrot else 174f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 175f625d460SBenoit Parrot } 176f625d460SBenoit Parrot 177f625d460SBenoit Parrot if (xlate_flags & OF_GPIO_ACTIVE_LOW) 178f625d460SBenoit Parrot *lflags |= GPIO_ACTIVE_LOW; 179f625d460SBenoit Parrot 180f625d460SBenoit Parrot if (of_property_read_bool(np, "input")) 181f625d460SBenoit Parrot *dflags |= GPIOD_IN; 182f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-low")) 183f625d460SBenoit Parrot *dflags |= GPIOD_OUT_LOW; 184f625d460SBenoit Parrot else if (of_property_read_bool(np, "output-high")) 185f625d460SBenoit Parrot *dflags |= GPIOD_OUT_HIGH; 186f625d460SBenoit Parrot else { 187f625d460SBenoit Parrot pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n", 188f625d460SBenoit Parrot desc_to_gpio(gg_data.out_gpio), np->name); 189f625d460SBenoit Parrot return ERR_PTR(-EINVAL); 190f625d460SBenoit Parrot } 191f625d460SBenoit Parrot 192f625d460SBenoit Parrot if (name && of_property_read_string(np, "line-name", name)) 193f625d460SBenoit Parrot *name = np->name; 194f625d460SBenoit Parrot 19539561e8bSMasahiro Yamada return gg_data.out_gpio; 196f625d460SBenoit Parrot } 197f625d460SBenoit Parrot 198f625d460SBenoit Parrot /** 199fd7337fdSMarkus Pargmann * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions 200f625d460SBenoit Parrot * @chip: gpio chip to act on 201f625d460SBenoit Parrot * 202f625d460SBenoit Parrot * This is only used by of_gpiochip_add to request/set GPIO initial 203f625d460SBenoit Parrot * configuration. 204dfbd379bSLaxman Dewangan * It retures error if it fails otherwise 0 on success. 205f625d460SBenoit Parrot */ 206dfbd379bSLaxman Dewangan static int of_gpiochip_scan_gpios(struct gpio_chip *chip) 207f625d460SBenoit Parrot { 208f625d460SBenoit Parrot struct gpio_desc *desc = NULL; 209f625d460SBenoit Parrot struct device_node *np; 210f625d460SBenoit Parrot const char *name; 211f625d460SBenoit Parrot enum gpio_lookup_flags lflags; 212f625d460SBenoit Parrot enum gpiod_flags dflags; 213dfbd379bSLaxman Dewangan int ret; 214f625d460SBenoit Parrot 215d1279d94SLaxman Dewangan for_each_available_child_of_node(chip->of_node, np) { 216f625d460SBenoit Parrot if (!of_property_read_bool(np, "gpio-hog")) 217f625d460SBenoit Parrot continue; 218f625d460SBenoit Parrot 219fd7337fdSMarkus Pargmann desc = of_parse_own_gpio(np, &name, &lflags, &dflags); 220f625d460SBenoit Parrot if (IS_ERR(desc)) 221f625d460SBenoit Parrot continue; 222f625d460SBenoit Parrot 223dfbd379bSLaxman Dewangan ret = gpiod_hog(desc, name, lflags, dflags); 224dfbd379bSLaxman Dewangan if (ret < 0) 225dfbd379bSLaxman Dewangan return ret; 226f625d460SBenoit Parrot } 227dfbd379bSLaxman Dewangan 228dfbd379bSLaxman Dewangan return 0; 229f625d460SBenoit Parrot } 230f625d460SBenoit Parrot 231f625d460SBenoit Parrot /** 232f141ed65SGrant Likely * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 233f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 234f141ed65SGrant Likely * @np: device node of the GPIO chip 235f141ed65SGrant Likely * @gpio_spec: gpio specifier as found in the device tree 236f141ed65SGrant Likely * @flags: a flags pointer to fill in 237f141ed65SGrant Likely * 238f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 239f141ed65SGrant Likely * gpio chips. This function performs only one sanity check: whether gpio 240f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 241f141ed65SGrant Likely */ 242f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 243f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 244f141ed65SGrant Likely { 245f141ed65SGrant Likely /* 246f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 24720a8a968SColin Cronin * write your own xlate function (that will have to retrieve the GPIO 248f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 249f141ed65SGrant Likely * but not recommended). 250f141ed65SGrant Likely */ 251f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 252f141ed65SGrant Likely WARN_ON(1); 253f141ed65SGrant Likely return -EINVAL; 254f141ed65SGrant Likely } 255f141ed65SGrant Likely 256f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 257f141ed65SGrant Likely return -EINVAL; 258f141ed65SGrant Likely 2597b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 260f141ed65SGrant Likely return -EINVAL; 261f141ed65SGrant Likely 262f141ed65SGrant Likely if (flags) 263f141ed65SGrant Likely *flags = gpiospec->args[1]; 264f141ed65SGrant Likely 265f141ed65SGrant Likely return gpiospec->args[0]; 266f141ed65SGrant Likely } 267f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 268f141ed65SGrant Likely 269f141ed65SGrant Likely /** 2703208b0f0SLinus Walleij * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) 271f141ed65SGrant Likely * @np: device node of the GPIO chip 272f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 2733208b0f0SLinus Walleij * @data: driver data to store in the struct gpio_chip 274f141ed65SGrant Likely * 275f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 276f141ed65SGrant Likely * 277f141ed65SGrant Likely * 1) In the gpio_chip structure: 278f141ed65SGrant Likely * - all the callbacks 279f141ed65SGrant Likely * - of_gpio_n_cells 280f141ed65SGrant Likely * - of_xlate callback (optional) 281f141ed65SGrant Likely * 282f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 283f141ed65SGrant Likely * - save_regs callback (optional) 284f141ed65SGrant Likely * 285f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 286f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 287f141ed65SGrant Likely * to manage GPIOs from the callbacks. 288f141ed65SGrant Likely */ 2893208b0f0SLinus Walleij int of_mm_gpiochip_add_data(struct device_node *np, 2903208b0f0SLinus Walleij struct of_mm_gpio_chip *mm_gc, 2913208b0f0SLinus Walleij void *data) 292f141ed65SGrant Likely { 293f141ed65SGrant Likely int ret = -ENOMEM; 294f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 295f141ed65SGrant Likely 296f141ed65SGrant Likely gc->label = kstrdup(np->full_name, GFP_KERNEL); 297f141ed65SGrant Likely if (!gc->label) 298f141ed65SGrant Likely goto err0; 299f141ed65SGrant Likely 300f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 301f141ed65SGrant Likely if (!mm_gc->regs) 302f141ed65SGrant Likely goto err1; 303f141ed65SGrant Likely 304f141ed65SGrant Likely gc->base = -1; 305f141ed65SGrant Likely 306f141ed65SGrant Likely if (mm_gc->save_regs) 307f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 308f141ed65SGrant Likely 309f141ed65SGrant Likely mm_gc->gc.of_node = np; 310f141ed65SGrant Likely 3113208b0f0SLinus Walleij ret = gpiochip_add_data(gc, data); 312f141ed65SGrant Likely if (ret) 313f141ed65SGrant Likely goto err2; 314f141ed65SGrant Likely 315f141ed65SGrant Likely return 0; 316f141ed65SGrant Likely err2: 317f141ed65SGrant Likely iounmap(mm_gc->regs); 318f141ed65SGrant Likely err1: 319f141ed65SGrant Likely kfree(gc->label); 320f141ed65SGrant Likely err0: 321f141ed65SGrant Likely pr_err("%s: GPIO chip registration failed with status %d\n", 322f141ed65SGrant Likely np->full_name, ret); 323f141ed65SGrant Likely return ret; 324f141ed65SGrant Likely } 3253208b0f0SLinus Walleij EXPORT_SYMBOL(of_mm_gpiochip_add_data); 326f141ed65SGrant Likely 327d621e8baSRicardo Ribalda Delgado /** 328d621e8baSRicardo Ribalda Delgado * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) 329d621e8baSRicardo Ribalda Delgado * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 330d621e8baSRicardo Ribalda Delgado */ 331d621e8baSRicardo Ribalda Delgado void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) 332d621e8baSRicardo Ribalda Delgado { 333d621e8baSRicardo Ribalda Delgado struct gpio_chip *gc = &mm_gc->gc; 334d621e8baSRicardo Ribalda Delgado 335d621e8baSRicardo Ribalda Delgado if (!mm_gc) 336d621e8baSRicardo Ribalda Delgado return; 337d621e8baSRicardo Ribalda Delgado 338d621e8baSRicardo Ribalda Delgado gpiochip_remove(gc); 339d621e8baSRicardo Ribalda Delgado iounmap(mm_gc->regs); 340d621e8baSRicardo Ribalda Delgado kfree(gc->label); 341d621e8baSRicardo Ribalda Delgado } 342d621e8baSRicardo Ribalda Delgado EXPORT_SYMBOL(of_mm_gpiochip_remove); 343d621e8baSRicardo Ribalda Delgado 344f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 34528355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) 346f23f1516SShiraz Hashim { 347f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 348f23f1516SShiraz Hashim struct of_phandle_args pinspec; 3491e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 350f23f1516SShiraz Hashim int index = 0, ret; 351586a87e6SChristian Ruppert const char *name; 352586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 353586a87e6SChristian Ruppert struct property *group_names; 354f23f1516SShiraz Hashim 355f23f1516SShiraz Hashim if (!np) 35628355f81STomeu Vizoso return 0; 357f23f1516SShiraz Hashim 358586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 359586a87e6SChristian Ruppert 360ad4e1a7cSHaojian Zhuang for (;; index++) { 361d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 362d9fe0039SStephen Warren index, &pinspec); 363f23f1516SShiraz Hashim if (ret) 364f23f1516SShiraz Hashim break; 365f23f1516SShiraz Hashim 3661e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 3671e63d7b9SLinus Walleij if (!pctldev) 36828355f81STomeu Vizoso return -EPROBE_DEFER; 369f23f1516SShiraz Hashim 370586a87e6SChristian Ruppert if (pinspec.args[2]) { 371586a87e6SChristian Ruppert if (group_names) { 37272858602SLaurent Navet of_property_read_string_index(np, 373586a87e6SChristian Ruppert group_names_propname, 374586a87e6SChristian Ruppert index, &name); 375586a87e6SChristian Ruppert if (strlen(name)) { 376586a87e6SChristian Ruppert pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n", 377586a87e6SChristian Ruppert np->full_name); 378586a87e6SChristian Ruppert break; 379586a87e6SChristian Ruppert } 380586a87e6SChristian Ruppert } 381586a87e6SChristian Ruppert /* npins != 0: linear range */ 3821e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 383ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 3841e63d7b9SLinus Walleij pinspec.args[0], 38586853c83SHaojian Zhuang pinspec.args[1], 38686853c83SHaojian Zhuang pinspec.args[2]); 3871e63d7b9SLinus Walleij if (ret) 38828355f81STomeu Vizoso return ret; 389586a87e6SChristian Ruppert } else { 390586a87e6SChristian Ruppert /* npins == 0: special range */ 391586a87e6SChristian Ruppert if (pinspec.args[1]) { 392586a87e6SChristian Ruppert pr_err("%s: Illegal gpio-range format.\n", 393586a87e6SChristian Ruppert np->full_name); 394586a87e6SChristian Ruppert break; 395586a87e6SChristian Ruppert } 396586a87e6SChristian Ruppert 397586a87e6SChristian Ruppert if (!group_names) { 398586a87e6SChristian Ruppert pr_err("%s: GPIO group range requested but no %s property.\n", 399586a87e6SChristian Ruppert np->full_name, group_names_propname); 400586a87e6SChristian Ruppert break; 401586a87e6SChristian Ruppert } 402586a87e6SChristian Ruppert 403586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 404586a87e6SChristian Ruppert group_names_propname, 405586a87e6SChristian Ruppert index, &name); 406586a87e6SChristian Ruppert if (ret) 407586a87e6SChristian Ruppert break; 408586a87e6SChristian Ruppert 409586a87e6SChristian Ruppert if (!strlen(name)) { 410586a87e6SChristian Ruppert pr_err("%s: Group name of GPIO group range cannot be the empty string.\n", 411586a87e6SChristian Ruppert np->full_name); 412586a87e6SChristian Ruppert break; 413586a87e6SChristian Ruppert } 414586a87e6SChristian Ruppert 415586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 416586a87e6SChristian Ruppert pinspec.args[0], name); 417586a87e6SChristian Ruppert if (ret) 41828355f81STomeu Vizoso return ret; 419586a87e6SChristian Ruppert } 420ad4e1a7cSHaojian Zhuang } 42128355f81STomeu Vizoso 42228355f81STomeu Vizoso return 0; 423f23f1516SShiraz Hashim } 424f23f1516SShiraz Hashim 425f23f1516SShiraz Hashim #else 42628355f81STomeu Vizoso static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } 427f23f1516SShiraz Hashim #endif 428f23f1516SShiraz Hashim 42928355f81STomeu Vizoso int of_gpiochip_add(struct gpio_chip *chip) 430f141ed65SGrant Likely { 43128355f81STomeu Vizoso int status; 43228355f81STomeu Vizoso 43358383c78SLinus Walleij if ((!chip->of_node) && (chip->parent)) 43458383c78SLinus Walleij chip->of_node = chip->parent->of_node; 435f141ed65SGrant Likely 436f141ed65SGrant Likely if (!chip->of_node) 43728355f81STomeu Vizoso return 0; 438f141ed65SGrant Likely 439f141ed65SGrant Likely if (!chip->of_xlate) { 440f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 441f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 442f141ed65SGrant Likely } 443f141ed65SGrant Likely 44428355f81STomeu Vizoso status = of_gpiochip_add_pin_range(chip); 44528355f81STomeu Vizoso if (status) 44628355f81STomeu Vizoso return status; 44728355f81STomeu Vizoso 448f141ed65SGrant Likely of_node_get(chip->of_node); 449f625d460SBenoit Parrot 450dfbd379bSLaxman Dewangan return of_gpiochip_scan_gpios(chip); 451f141ed65SGrant Likely } 452f141ed65SGrant Likely 453f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 454f141ed65SGrant Likely { 455e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 456f141ed65SGrant Likely of_node_put(chip->of_node); 457f141ed65SGrant Likely } 458