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> 25f141ed65SGrant Likely 26af8b6375SAlexandre Courbot struct gpio_desc; 27af8b6375SAlexandre Courbot 280df2c999SDong Aisheng /* Private data structure for of_gpiochip_find_and_xlate */ 293d0f7cf0SGrant Likely struct gg_data { 303d0f7cf0SGrant Likely enum of_gpio_flags *flags; 313d0f7cf0SGrant Likely struct of_phandle_args gpiospec; 323d0f7cf0SGrant Likely 33af8b6375SAlexandre Courbot struct gpio_desc *out_gpio; 343d0f7cf0SGrant Likely }; 353d0f7cf0SGrant Likely 363d0f7cf0SGrant Likely /* Private function for resolving node pointer to gpio_chip */ 373d0f7cf0SGrant Likely static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data) 383d0f7cf0SGrant Likely { 393d0f7cf0SGrant Likely struct gg_data *gg_data = data; 403d0f7cf0SGrant Likely int ret; 413d0f7cf0SGrant Likely 423d0f7cf0SGrant Likely if ((gc->of_node != gg_data->gpiospec.np) || 433d0f7cf0SGrant Likely (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) || 443d0f7cf0SGrant Likely (!gc->of_xlate)) 453d0f7cf0SGrant Likely return false; 463d0f7cf0SGrant Likely 473d0f7cf0SGrant Likely ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags); 483d0f7cf0SGrant Likely if (ret < 0) 493d0f7cf0SGrant Likely return false; 503d0f7cf0SGrant Likely 51ccd9726eSLinus Walleij gg_data->out_gpio = gpiochip_get_desc(gc, ret); 523d0f7cf0SGrant Likely return true; 533d0f7cf0SGrant Likely } 543d0f7cf0SGrant 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 { 694fbb0022SRoland Stigge /* Return -EPROBE_DEFER to support probe() functions to be called 704fbb0022SRoland Stigge * later when the GPIO actually becomes available 714fbb0022SRoland Stigge */ 72af8b6375SAlexandre Courbot struct gg_data gg_data = { 73af8b6375SAlexandre Courbot .flags = flags, 74af8b6375SAlexandre Courbot .out_gpio = ERR_PTR(-EPROBE_DEFER) 75af8b6375SAlexandre Courbot }; 76f141ed65SGrant Likely int ret; 77f141ed65SGrant Likely 783d0f7cf0SGrant Likely /* .of_xlate might decide to not fill in the flags, so clear it. */ 79f141ed65SGrant Likely if (flags) 80f141ed65SGrant Likely *flags = 0; 81f141ed65SGrant Likely 823d0f7cf0SGrant Likely ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, 833d0f7cf0SGrant Likely &gg_data.gpiospec); 843d0f7cf0SGrant Likely if (ret) { 85eddf8176SLothar Waßmann pr_debug("%s: can't parse gpios property of node '%s[%d]'\n", 86eddf8176SLothar Waßmann __func__, np->full_name, index); 87af8b6375SAlexandre Courbot return ERR_PTR(ret); 883d0f7cf0SGrant Likely } 89f141ed65SGrant Likely 903d0f7cf0SGrant Likely gpiochip_find(&gg_data, of_gpiochip_find_and_xlate); 913d0f7cf0SGrant Likely 923d0f7cf0SGrant Likely of_node_put(gg_data.gpiospec.np); 93af8b6375SAlexandre Courbot pr_debug("%s exited with status %d\n", __func__, 94bea4dbeeSSachin Kamat PTR_ERR_OR_ZERO(gg_data.out_gpio)); 953d0f7cf0SGrant Likely return gg_data.out_gpio; 96f141ed65SGrant Likely } 97af8b6375SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpiod_flags); 98f141ed65SGrant Likely 99f01d9075SAlexandre Courbot int of_get_named_gpio_flags(struct device_node *np, const char *list_name, 100f01d9075SAlexandre Courbot int index, enum of_gpio_flags *flags) 101f01d9075SAlexandre Courbot { 102f01d9075SAlexandre Courbot struct gpio_desc *desc; 103f01d9075SAlexandre Courbot 104f01d9075SAlexandre Courbot desc = of_get_named_gpiod_flags(np, list_name, index, flags); 105f01d9075SAlexandre Courbot 106f01d9075SAlexandre Courbot if (IS_ERR(desc)) 107f01d9075SAlexandre Courbot return PTR_ERR(desc); 108f01d9075SAlexandre Courbot else 109f01d9075SAlexandre Courbot return desc_to_gpio(desc); 110f01d9075SAlexandre Courbot } 111f01d9075SAlexandre Courbot EXPORT_SYMBOL(of_get_named_gpio_flags); 112f01d9075SAlexandre Courbot 113f141ed65SGrant Likely /** 114f141ed65SGrant Likely * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 115f141ed65SGrant Likely * @gc: pointer to the gpio_chip structure 116f141ed65SGrant Likely * @np: device node of the GPIO chip 117f141ed65SGrant Likely * @gpio_spec: gpio specifier as found in the device tree 118f141ed65SGrant Likely * @flags: a flags pointer to fill in 119f141ed65SGrant Likely * 120f141ed65SGrant Likely * This is simple translation function, suitable for the most 1:1 mapped 121f141ed65SGrant Likely * gpio chips. This function performs only one sanity check: whether gpio 122f141ed65SGrant Likely * is less than ngpios (that is specified in the gpio_chip). 123f141ed65SGrant Likely */ 124f141ed65SGrant Likely int of_gpio_simple_xlate(struct gpio_chip *gc, 125f141ed65SGrant Likely const struct of_phandle_args *gpiospec, u32 *flags) 126f141ed65SGrant Likely { 127f141ed65SGrant Likely /* 128f141ed65SGrant Likely * We're discouraging gpio_cells < 2, since that way you'll have to 129f141ed65SGrant Likely * write your own xlate function (that will have to retrive the GPIO 130f141ed65SGrant Likely * number and the flags from a single gpio cell -- this is possible, 131f141ed65SGrant Likely * but not recommended). 132f141ed65SGrant Likely */ 133f141ed65SGrant Likely if (gc->of_gpio_n_cells < 2) { 134f141ed65SGrant Likely WARN_ON(1); 135f141ed65SGrant Likely return -EINVAL; 136f141ed65SGrant Likely } 137f141ed65SGrant Likely 138f141ed65SGrant Likely if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) 139f141ed65SGrant Likely return -EINVAL; 140f141ed65SGrant Likely 1417b96c686SGrant Likely if (gpiospec->args[0] >= gc->ngpio) 142f141ed65SGrant Likely return -EINVAL; 143f141ed65SGrant Likely 144f141ed65SGrant Likely if (flags) 145f141ed65SGrant Likely *flags = gpiospec->args[1]; 146f141ed65SGrant Likely 147f141ed65SGrant Likely return gpiospec->args[0]; 148f141ed65SGrant Likely } 149f141ed65SGrant Likely EXPORT_SYMBOL(of_gpio_simple_xlate); 150f141ed65SGrant Likely 151f141ed65SGrant Likely /** 152f141ed65SGrant Likely * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank) 153f141ed65SGrant Likely * @np: device node of the GPIO chip 154f141ed65SGrant Likely * @mm_gc: pointer to the of_mm_gpio_chip allocated structure 155f141ed65SGrant Likely * 156f141ed65SGrant Likely * To use this function you should allocate and fill mm_gc with: 157f141ed65SGrant Likely * 158f141ed65SGrant Likely * 1) In the gpio_chip structure: 159f141ed65SGrant Likely * - all the callbacks 160f141ed65SGrant Likely * - of_gpio_n_cells 161f141ed65SGrant Likely * - of_xlate callback (optional) 162f141ed65SGrant Likely * 163f141ed65SGrant Likely * 3) In the of_mm_gpio_chip structure: 164f141ed65SGrant Likely * - save_regs callback (optional) 165f141ed65SGrant Likely * 166f141ed65SGrant Likely * If succeeded, this function will map bank's memory and will 167f141ed65SGrant Likely * do all necessary work for you. Then you'll able to use .regs 168f141ed65SGrant Likely * to manage GPIOs from the callbacks. 169f141ed65SGrant Likely */ 170f141ed65SGrant Likely int of_mm_gpiochip_add(struct device_node *np, 171f141ed65SGrant Likely struct of_mm_gpio_chip *mm_gc) 172f141ed65SGrant Likely { 173f141ed65SGrant Likely int ret = -ENOMEM; 174f141ed65SGrant Likely struct gpio_chip *gc = &mm_gc->gc; 175f141ed65SGrant Likely 176f141ed65SGrant Likely gc->label = kstrdup(np->full_name, GFP_KERNEL); 177f141ed65SGrant Likely if (!gc->label) 178f141ed65SGrant Likely goto err0; 179f141ed65SGrant Likely 180f141ed65SGrant Likely mm_gc->regs = of_iomap(np, 0); 181f141ed65SGrant Likely if (!mm_gc->regs) 182f141ed65SGrant Likely goto err1; 183f141ed65SGrant Likely 184f141ed65SGrant Likely gc->base = -1; 185f141ed65SGrant Likely 186f141ed65SGrant Likely if (mm_gc->save_regs) 187f141ed65SGrant Likely mm_gc->save_regs(mm_gc); 188f141ed65SGrant Likely 189f141ed65SGrant Likely mm_gc->gc.of_node = np; 190f141ed65SGrant Likely 191f141ed65SGrant Likely ret = gpiochip_add(gc); 192f141ed65SGrant Likely if (ret) 193f141ed65SGrant Likely goto err2; 194f141ed65SGrant Likely 195f141ed65SGrant Likely return 0; 196f141ed65SGrant Likely err2: 197f141ed65SGrant Likely iounmap(mm_gc->regs); 198f141ed65SGrant Likely err1: 199f141ed65SGrant Likely kfree(gc->label); 200f141ed65SGrant Likely err0: 201f141ed65SGrant Likely pr_err("%s: GPIO chip registration failed with status %d\n", 202f141ed65SGrant Likely np->full_name, ret); 203f141ed65SGrant Likely return ret; 204f141ed65SGrant Likely } 205f141ed65SGrant Likely EXPORT_SYMBOL(of_mm_gpiochip_add); 206f141ed65SGrant Likely 207f23f1516SShiraz Hashim #ifdef CONFIG_PINCTRL 208167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip) 209f23f1516SShiraz Hashim { 210f23f1516SShiraz Hashim struct device_node *np = chip->of_node; 211f23f1516SShiraz Hashim struct of_phandle_args pinspec; 2121e63d7b9SLinus Walleij struct pinctrl_dev *pctldev; 213f23f1516SShiraz Hashim int index = 0, ret; 214586a87e6SChristian Ruppert const char *name; 215586a87e6SChristian Ruppert static const char group_names_propname[] = "gpio-ranges-group-names"; 216586a87e6SChristian Ruppert struct property *group_names; 217f23f1516SShiraz Hashim 218f23f1516SShiraz Hashim if (!np) 219f23f1516SShiraz Hashim return; 220f23f1516SShiraz Hashim 221586a87e6SChristian Ruppert group_names = of_find_property(np, group_names_propname, NULL); 222586a87e6SChristian Ruppert 223ad4e1a7cSHaojian Zhuang for (;; index++) { 224d9fe0039SStephen Warren ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 225d9fe0039SStephen Warren index, &pinspec); 226f23f1516SShiraz Hashim if (ret) 227f23f1516SShiraz Hashim break; 228f23f1516SShiraz Hashim 2291e63d7b9SLinus Walleij pctldev = of_pinctrl_get(pinspec.np); 2301e63d7b9SLinus Walleij if (!pctldev) 231f23f1516SShiraz Hashim break; 232f23f1516SShiraz Hashim 233586a87e6SChristian Ruppert if (pinspec.args[2]) { 234586a87e6SChristian Ruppert if (group_names) { 235586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 236586a87e6SChristian Ruppert group_names_propname, 237586a87e6SChristian Ruppert index, &name); 238586a87e6SChristian Ruppert if (strlen(name)) { 239586a87e6SChristian Ruppert pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n", 240586a87e6SChristian Ruppert np->full_name); 241586a87e6SChristian Ruppert break; 242586a87e6SChristian Ruppert } 243586a87e6SChristian Ruppert } 244586a87e6SChristian Ruppert /* npins != 0: linear range */ 2451e63d7b9SLinus Walleij ret = gpiochip_add_pin_range(chip, 246ef5e3eefSHaojian Zhuang pinctrl_dev_get_devname(pctldev), 2471e63d7b9SLinus Walleij pinspec.args[0], 24886853c83SHaojian Zhuang pinspec.args[1], 24986853c83SHaojian Zhuang pinspec.args[2]); 2501e63d7b9SLinus Walleij if (ret) 2511e63d7b9SLinus Walleij break; 252586a87e6SChristian Ruppert } else { 253586a87e6SChristian Ruppert /* npins == 0: special range */ 254586a87e6SChristian Ruppert if (pinspec.args[1]) { 255586a87e6SChristian Ruppert pr_err("%s: Illegal gpio-range format.\n", 256586a87e6SChristian Ruppert np->full_name); 257586a87e6SChristian Ruppert break; 258586a87e6SChristian Ruppert } 259586a87e6SChristian Ruppert 260586a87e6SChristian Ruppert if (!group_names) { 261586a87e6SChristian Ruppert pr_err("%s: GPIO group range requested but no %s property.\n", 262586a87e6SChristian Ruppert np->full_name, group_names_propname); 263586a87e6SChristian Ruppert break; 264586a87e6SChristian Ruppert } 265586a87e6SChristian Ruppert 266586a87e6SChristian Ruppert ret = of_property_read_string_index(np, 267586a87e6SChristian Ruppert group_names_propname, 268586a87e6SChristian Ruppert index, &name); 269586a87e6SChristian Ruppert if (ret) 270586a87e6SChristian Ruppert break; 271586a87e6SChristian Ruppert 272586a87e6SChristian Ruppert if (!strlen(name)) { 273586a87e6SChristian Ruppert pr_err("%s: Group name of GPIO group range cannot be the empty string.\n", 274586a87e6SChristian Ruppert np->full_name); 275586a87e6SChristian Ruppert break; 276586a87e6SChristian Ruppert } 277586a87e6SChristian Ruppert 278586a87e6SChristian Ruppert ret = gpiochip_add_pingroup_range(chip, pctldev, 279586a87e6SChristian Ruppert pinspec.args[0], name); 280586a87e6SChristian Ruppert if (ret) 281586a87e6SChristian Ruppert break; 282586a87e6SChristian Ruppert } 283ad4e1a7cSHaojian Zhuang } 284f23f1516SShiraz Hashim } 285f23f1516SShiraz Hashim 286f23f1516SShiraz Hashim #else 287167c1af9SLinus Walleij static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {} 288f23f1516SShiraz Hashim #endif 289f23f1516SShiraz Hashim 290f141ed65SGrant Likely void of_gpiochip_add(struct gpio_chip *chip) 291f141ed65SGrant Likely { 292f141ed65SGrant Likely if ((!chip->of_node) && (chip->dev)) 293f141ed65SGrant Likely chip->of_node = chip->dev->of_node; 294f141ed65SGrant Likely 295f141ed65SGrant Likely if (!chip->of_node) 296f141ed65SGrant Likely return; 297f141ed65SGrant Likely 298f141ed65SGrant Likely if (!chip->of_xlate) { 299f141ed65SGrant Likely chip->of_gpio_n_cells = 2; 300f141ed65SGrant Likely chip->of_xlate = of_gpio_simple_xlate; 301f141ed65SGrant Likely } 302f141ed65SGrant Likely 303f23f1516SShiraz Hashim of_gpiochip_add_pin_range(chip); 304f141ed65SGrant Likely of_node_get(chip->of_node); 305f141ed65SGrant Likely } 306f141ed65SGrant Likely 307f141ed65SGrant Likely void of_gpiochip_remove(struct gpio_chip *chip) 308f141ed65SGrant Likely { 309e93fa3f2SLinus Walleij gpiochip_remove_pin_ranges(chip); 310f23f1516SShiraz Hashim 311f141ed65SGrant Likely if (chip->of_node) 312f141ed65SGrant Likely of_node_put(chip->of_node); 313f141ed65SGrant Likely } 314