Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 1 | /* |
| 2 | * OF helpers for the GPIO API |
| 3 | * |
| 4 | * Copyright (c) 2007-2008 MontaVista Software, Inc. |
| 5 | * |
| 6 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 14 | #include <linux/device.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 15 | #include <linux/errno.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 16 | #include <linux/module.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/of.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 19 | #include <linux/of_address.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 20 | #include <linux/of_gpio.h> |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 21 | #include <linux/pinctrl/pinctrl.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 22 | #include <linux/slab.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 23 | |
| 24 | /** |
John Bonesio | a6b0919 | 2011-06-27 16:49:57 -0700 | [diff] [blame] | 25 | * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 26 | * @np: device node to get GPIO from |
John Bonesio | a6b0919 | 2011-06-27 16:49:57 -0700 | [diff] [blame] | 27 | * @propname: property name containing gpio specifier(s) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 28 | * @index: index of the GPIO |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 29 | * @flags: a flags pointer to fill in |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 30 | * |
| 31 | * Returns GPIO number to use with Linux generic GPIO API, or one of the errno |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 32 | * value on the error condition. If @flags is not NULL the function also fills |
| 33 | * in flags for the GPIO. |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 34 | */ |
John Bonesio | a6b0919 | 2011-06-27 16:49:57 -0700 | [diff] [blame] | 35 | int of_get_named_gpio_flags(struct device_node *np, const char *propname, |
| 36 | int index, enum of_gpio_flags *flags) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 37 | { |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 38 | int ret; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 39 | struct gpio_chip *gc; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 40 | struct of_phandle_args gpiospec; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 41 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 42 | ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, |
| 43 | &gpiospec); |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 44 | if (ret) { |
| 45 | pr_debug("%s: can't parse gpios property\n", __func__); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 46 | goto err0; |
| 47 | } |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 48 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 49 | gc = of_node_to_gpiochip(gpiospec.np); |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 50 | if (!gc) { |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 51 | pr_debug("%s: gpio controller %s isn't registered\n", |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 52 | np->full_name, gpiospec.np->full_name); |
Roland Stigge | bc841ce | 2012-06-28 00:32:14 +0200 | [diff] [blame] | 53 | ret = -EPROBE_DEFER; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 54 | goto err1; |
| 55 | } |
| 56 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 57 | if (gpiospec.args_count != gc->of_gpio_n_cells) { |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 58 | pr_debug("%s: wrong #gpio-cells for %s\n", |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 59 | np->full_name, gpiospec.np->full_name); |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 60 | ret = -EINVAL; |
| 61 | goto err1; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 62 | } |
| 63 | |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 64 | /* .xlate might decide to not fill in the flags, so clear it. */ |
| 65 | if (flags) |
| 66 | *flags = 0; |
| 67 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 68 | ret = gc->of_xlate(gc, &gpiospec, flags); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 69 | if (ret < 0) |
| 70 | goto err1; |
| 71 | |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 72 | ret += gc->base; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 73 | err1: |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 74 | of_node_put(gpiospec.np); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 75 | err0: |
| 76 | pr_debug("%s exited with status %d\n", __func__, ret); |
| 77 | return ret; |
| 78 | } |
John Bonesio | a6b0919 | 2011-06-27 16:49:57 -0700 | [diff] [blame] | 79 | EXPORT_SYMBOL(of_get_named_gpio_flags); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 80 | |
| 81 | /** |
Jean-Christophe PLAGNIOL-VILLARD | ff64abe | 2012-02-02 16:20:01 +0100 | [diff] [blame] | 82 | * of_gpio_named_count - Count GPIOs for a device |
Anton Vorontsov | 7498209 | 2008-12-05 08:15:54 +0000 | [diff] [blame] | 83 | * @np: device node to count GPIOs for |
Jean-Christophe PLAGNIOL-VILLARD | ff64abe | 2012-02-02 16:20:01 +0100 | [diff] [blame] | 84 | * @propname: property name containing gpio specifier(s) |
Anton Vorontsov | 7498209 | 2008-12-05 08:15:54 +0000 | [diff] [blame] | 85 | * |
| 86 | * The function returns the count of GPIOs specified for a node. |
| 87 | * |
| 88 | * Note that the empty GPIO specifiers counts too. For example, |
| 89 | * |
| 90 | * gpios = <0 |
| 91 | * &pio1 1 2 |
| 92 | * 0 |
| 93 | * &pio2 3 4>; |
| 94 | * |
| 95 | * defines four GPIOs (so this function will return 4), two of which |
| 96 | * are not specified. |
| 97 | */ |
Jean-Christophe PLAGNIOL-VILLARD | ff64abe | 2012-02-02 16:20:01 +0100 | [diff] [blame] | 98 | unsigned int of_gpio_named_count(struct device_node *np, const char* propname) |
Anton Vorontsov | 7498209 | 2008-12-05 08:15:54 +0000 | [diff] [blame] | 99 | { |
| 100 | unsigned int cnt = 0; |
| 101 | |
| 102 | do { |
| 103 | int ret; |
| 104 | |
Jean-Christophe PLAGNIOL-VILLARD | ff64abe | 2012-02-02 16:20:01 +0100 | [diff] [blame] | 105 | ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 106 | cnt, NULL); |
Anton Vorontsov | 7498209 | 2008-12-05 08:15:54 +0000 | [diff] [blame] | 107 | /* A hole in the gpios = <> counts anyway. */ |
| 108 | if (ret < 0 && ret != -EEXIST) |
| 109 | break; |
| 110 | } while (++cnt); |
| 111 | |
| 112 | return cnt; |
| 113 | } |
Jean-Christophe PLAGNIOL-VILLARD | ff64abe | 2012-02-02 16:20:01 +0100 | [diff] [blame] | 114 | EXPORT_SYMBOL(of_gpio_named_count); |
Anton Vorontsov | 7498209 | 2008-12-05 08:15:54 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 117 | * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 118 | * @gc: pointer to the gpio_chip structure |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 119 | * @np: device node of the GPIO chip |
| 120 | * @gpio_spec: gpio specifier as found in the device tree |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 121 | * @flags: a flags pointer to fill in |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 122 | * |
| 123 | * This is simple translation function, suitable for the most 1:1 mapped |
| 124 | * gpio chips. This function performs only one sanity check: whether gpio |
| 125 | * is less than ngpios (that is specified in the gpio_chip). |
| 126 | */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 127 | int of_gpio_simple_xlate(struct gpio_chip *gc, |
| 128 | const struct of_phandle_args *gpiospec, u32 *flags) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 129 | { |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 130 | /* |
| 131 | * We're discouraging gpio_cells < 2, since that way you'll have to |
| 132 | * write your own xlate function (that will have to retrive the GPIO |
| 133 | * number and the flags from a single gpio cell -- this is possible, |
| 134 | * but not recommended). |
| 135 | */ |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 136 | if (gc->of_gpio_n_cells < 2) { |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 137 | WARN_ON(1); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 141 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
| 142 | return -EINVAL; |
| 143 | |
Roland Stigge | 6270d83 | 2012-04-04 02:02:58 +0200 | [diff] [blame] | 144 | if (gpiospec->args[0] >= gc->ngpio) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 145 | return -EINVAL; |
| 146 | |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 147 | if (flags) |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 148 | *flags = gpiospec->args[1]; |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 149 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 150 | return gpiospec->args[0]; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 151 | } |
Jamie Iles | 3038bbd | 2011-07-28 16:25:41 +0100 | [diff] [blame] | 152 | EXPORT_SYMBOL(of_gpio_simple_xlate); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 153 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 154 | /** |
| 155 | * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank) |
| 156 | * @np: device node of the GPIO chip |
| 157 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure |
| 158 | * |
| 159 | * To use this function you should allocate and fill mm_gc with: |
| 160 | * |
| 161 | * 1) In the gpio_chip structure: |
| 162 | * - all the callbacks |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 163 | * - of_gpio_n_cells |
| 164 | * - of_xlate callback (optional) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 165 | * |
| 166 | * 3) In the of_mm_gpio_chip structure: |
| 167 | * - save_regs callback (optional) |
| 168 | * |
| 169 | * If succeeded, this function will map bank's memory and will |
| 170 | * do all necessary work for you. Then you'll able to use .regs |
| 171 | * to manage GPIOs from the callbacks. |
| 172 | */ |
| 173 | int of_mm_gpiochip_add(struct device_node *np, |
| 174 | struct of_mm_gpio_chip *mm_gc) |
| 175 | { |
| 176 | int ret = -ENOMEM; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 177 | struct gpio_chip *gc = &mm_gc->gc; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 178 | |
| 179 | gc->label = kstrdup(np->full_name, GFP_KERNEL); |
| 180 | if (!gc->label) |
| 181 | goto err0; |
| 182 | |
| 183 | mm_gc->regs = of_iomap(np, 0); |
| 184 | if (!mm_gc->regs) |
| 185 | goto err1; |
| 186 | |
Anton Vorontsov | 2145115 | 2008-04-30 00:05:24 +1000 | [diff] [blame] | 187 | gc->base = -1; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 188 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 189 | if (mm_gc->save_regs) |
| 190 | mm_gc->save_regs(mm_gc); |
| 191 | |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 192 | mm_gc->gc.of_node = np; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 193 | |
| 194 | ret = gpiochip_add(gc); |
| 195 | if (ret) |
| 196 | goto err2; |
| 197 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 198 | return 0; |
| 199 | err2: |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 200 | iounmap(mm_gc->regs); |
| 201 | err1: |
| 202 | kfree(gc->label); |
| 203 | err0: |
| 204 | pr_err("%s: GPIO chip registration failed with status %d\n", |
| 205 | np->full_name, ret); |
| 206 | return ret; |
| 207 | } |
| 208 | EXPORT_SYMBOL(of_mm_gpiochip_add); |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 209 | |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 210 | #ifdef CONFIG_PINCTRL |
| 211 | void of_gpiochip_add_pin_range(struct gpio_chip *chip) |
| 212 | { |
| 213 | struct device_node *np = chip->of_node; |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 214 | struct of_phandle_args pinspec; |
Linus Walleij | 4aca036 | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 215 | struct pinctrl_dev *pctldev; |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 216 | int index = 0, ret; |
| 217 | |
| 218 | if (!np) |
| 219 | return; |
| 220 | |
| 221 | do { |
| 222 | ret = of_parse_phandle_with_args(np, "gpio-ranges", |
| 223 | "#gpio-range-cells", index, &pinspec); |
| 224 | if (ret) |
| 225 | break; |
| 226 | |
Linus Walleij | 4aca036 | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 227 | pctldev = of_pinctrl_get(pinspec.np); |
| 228 | if (!pctldev) |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 229 | break; |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 230 | |
Linus Walleij | 4aca036 | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 231 | ret = gpiochip_add_pin_range(chip, |
| 232 | pinctrl_dev_get_name(pctldev), |
| 233 | pinspec.args[0], |
| 234 | pinspec.args[1]); |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 235 | |
Linus Walleij | 4aca036 | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 236 | if (ret) |
| 237 | break; |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 238 | |
| 239 | } while (index++); |
| 240 | } |
| 241 | |
| 242 | void of_gpiochip_remove_pin_range(struct gpio_chip *chip) |
| 243 | { |
| 244 | struct gpio_pin_range *pin_range, *tmp; |
| 245 | |
| 246 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
| 247 | list_del(&pin_range->node); |
| 248 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 249 | &pin_range->range); |
| 250 | } |
| 251 | } |
| 252 | #else |
| 253 | void of_gpiochip_add_pin_range(struct gpio_chip *chip) {} |
| 254 | void of_gpiochip_remove_pin_range(struct gpio_chip *chip) {} |
| 255 | #endif |
| 256 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 257 | void of_gpiochip_add(struct gpio_chip *chip) |
| 258 | { |
| 259 | if ((!chip->of_node) && (chip->dev)) |
| 260 | chip->of_node = chip->dev->of_node; |
| 261 | |
| 262 | if (!chip->of_node) |
| 263 | return; |
| 264 | |
| 265 | if (!chip->of_xlate) { |
| 266 | chip->of_gpio_n_cells = 2; |
| 267 | chip->of_xlate = of_gpio_simple_xlate; |
| 268 | } |
| 269 | |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 270 | of_gpiochip_add_pin_range(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 271 | of_node_get(chip->of_node); |
| 272 | } |
| 273 | |
| 274 | void of_gpiochip_remove(struct gpio_chip *chip) |
| 275 | { |
Shiraz Hashim | e77d22c | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 276 | of_gpiochip_remove_pin_range(chip); |
| 277 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 278 | if (chip->of_node) |
| 279 | of_node_put(chip->of_node); |
| 280 | } |
| 281 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 282 | /* Private function for resolving node pointer to gpio_chip */ |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 283 | static int of_gpiochip_is_match(struct gpio_chip *chip, const void *data) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 284 | { |
| 285 | return chip->of_node == data; |
| 286 | } |
| 287 | |
| 288 | struct gpio_chip *of_node_to_gpiochip(struct device_node *np) |
| 289 | { |
| 290 | return gpiochip_find(np, of_gpiochip_is_match); |
| 291 | } |