David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 3 | #include <linux/interrupt.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4 | #include <linux/irq.h> |
| 5 | #include <linux/spinlock.h> |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 6 | #include <linux/list.h> |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/gpio.h> |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 12 | #include <linux/of_gpio.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 13 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame] | 15 | #include <linux/acpi.h> |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 16 | #include <linux/gpio/driver.h> |
Linus Walleij | 0a6d315 | 2014-07-24 20:08:55 +0200 | [diff] [blame] | 17 | #include <linux/gpio/machine.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 18 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 19 | #include "gpiolib.h" |
| 20 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 21 | #define CREATE_TRACE_POINTS |
| 22 | #include <trace/events/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 23 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 24 | /* Implementation infrastructure for GPIO interfaces. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 25 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 26 | * The GPIO programming interface allows for inlining speed-critical |
| 27 | * get/set operations for common cases, so that access to SOC-integrated |
| 28 | * GPIOs can sometimes cost only an instruction or two per bit. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | |
| 32 | /* When debugging, extend minimal trust to callers and platform code. |
| 33 | * Also emit diagnostic messages that may help initial bringup, when |
| 34 | * board setup or driver bugs are most common. |
| 35 | * |
| 36 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 37 | */ |
| 38 | #ifdef DEBUG |
| 39 | #define extra_checks 1 |
| 40 | #else |
| 41 | #define extra_checks 0 |
| 42 | #endif |
| 43 | |
| 44 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 45 | * While any GPIO is requested, its gpio_chip is not removable; |
| 46 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 47 | */ |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 48 | DEFINE_SPINLOCK(gpio_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 49 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 50 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
| 51 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 52 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 53 | static LIST_HEAD(gpio_lookup_list); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 54 | LIST_HEAD(gpio_chips); |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 55 | |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 56 | |
| 57 | static void gpiochip_free_hogs(struct gpio_chip *chip); |
| 58 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
| 59 | |
| 60 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 61 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 62 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 63 | d->label = label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 66 | /** |
| 67 | * Convert a GPIO number to its descriptor |
| 68 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 69 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 70 | { |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 71 | struct gpio_chip *chip; |
| 72 | unsigned long flags; |
| 73 | |
| 74 | spin_lock_irqsave(&gpio_lock, flags); |
| 75 | |
| 76 | list_for_each_entry(chip, &gpio_chips, list) { |
| 77 | if (chip->base <= gpio && chip->base + chip->ngpio > gpio) { |
| 78 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 79 | return &chip->desc[gpio - chip->base]; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 84 | |
Alexandre Courbot | 0e9a5ed | 2014-12-02 23:15:05 +0900 | [diff] [blame] | 85 | if (!gpio_is_valid(gpio)) |
| 86 | WARN(1, "invalid GPIO %d\n", gpio); |
| 87 | |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 88 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 89 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 91 | |
| 92 | /** |
Markus Pargmann | c0017ed | 2015-08-14 16:10:59 +0200 | [diff] [blame] | 93 | * Convert a GPIO name to its descriptor |
| 94 | */ |
| 95 | struct gpio_desc *gpio_name_to_desc(const char * const name) |
| 96 | { |
| 97 | struct gpio_chip *chip; |
| 98 | unsigned long flags; |
| 99 | |
| 100 | spin_lock_irqsave(&gpio_lock, flags); |
| 101 | |
| 102 | list_for_each_entry(chip, &gpio_chips, list) { |
| 103 | int i; |
| 104 | |
| 105 | for (i = 0; i != chip->ngpio; ++i) { |
| 106 | struct gpio_desc *gpio = &chip->desc[i]; |
| 107 | |
| 108 | if (!gpio->name) |
| 109 | continue; |
| 110 | |
| 111 | if (!strcmp(gpio->name, name)) { |
| 112 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 113 | return gpio; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(gpio_name_to_desc); |
| 123 | |
| 124 | /** |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 125 | * Get the GPIO descriptor corresponding to the given hw number for this chip. |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 126 | */ |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 127 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
| 128 | u16 hwnum) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 129 | { |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 130 | if (hwnum >= chip->ngpio) |
Alexandre Courbot | b7d0a28 | 2013-12-03 12:31:11 +0900 | [diff] [blame] | 131 | return ERR_PTR(-EINVAL); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 132 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 133 | return &chip->desc[hwnum]; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 134 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Convert a GPIO descriptor to the integer namespace. |
| 138 | * This should disappear in the future but is needed since we still |
| 139 | * use GPIO numbers for error messages and sysfs nodes |
| 140 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 141 | int desc_to_gpio(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 142 | { |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 143 | return desc->chip->base + (desc - &desc->chip->desc[0]); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 144 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 145 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 146 | |
| 147 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 148 | /** |
| 149 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 150 | * @desc: descriptor to return the chip of |
| 151 | */ |
| 152 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 153 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 154 | return desc ? desc->chip : NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 155 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 156 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 157 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 158 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 159 | static int gpiochip_find_base(int ngpio) |
| 160 | { |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 161 | struct gpio_chip *chip; |
| 162 | int base = ARCH_NR_GPIOS - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 163 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 164 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
| 165 | /* found a free space? */ |
| 166 | if (chip->base + chip->ngpio <= base) |
| 167 | break; |
| 168 | else |
| 169 | /* nope, check the space right before the chip */ |
| 170 | base = chip->base - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 173 | if (gpio_is_valid(base)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 174 | pr_debug("%s: found new base at %d\n", __func__, base); |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 175 | return base; |
| 176 | } else { |
| 177 | pr_err("%s: cannot find free range\n", __func__); |
| 178 | return -ENOSPC; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 179 | } |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 182 | /** |
| 183 | * gpiod_get_direction - return the current direction of a GPIO |
| 184 | * @desc: GPIO to get the direction of |
| 185 | * |
| 186 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. |
| 187 | * |
| 188 | * This function may sleep if gpiod_cansleep() is true. |
| 189 | */ |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 190 | int gpiod_get_direction(struct gpio_desc *desc) |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 191 | { |
| 192 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 193 | unsigned offset; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 194 | int status = -EINVAL; |
| 195 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 196 | chip = gpiod_to_chip(desc); |
| 197 | offset = gpio_chip_hwgpio(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 198 | |
| 199 | if (!chip->get_direction) |
| 200 | return status; |
| 201 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 202 | status = chip->get_direction(chip, offset); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 203 | if (status > 0) { |
| 204 | /* GPIOF_DIR_IN, or other positive */ |
| 205 | status = 1; |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 206 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 207 | } |
| 208 | if (status == 0) { |
| 209 | /* GPIOF_DIR_OUT */ |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 210 | set_bit(FLAG_IS_OUT, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 211 | } |
| 212 | return status; |
| 213 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 215 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 216 | /* |
| 217 | * Add a new chip to the global chips list, keeping the list of chips sorted |
| 218 | * by base order. |
| 219 | * |
| 220 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 221 | * space. |
| 222 | */ |
| 223 | static int gpiochip_add_to_list(struct gpio_chip *chip) |
| 224 | { |
Masahiro Yamada | d1aceb8 | 2015-07-21 14:45:40 +0900 | [diff] [blame] | 225 | struct list_head *pos; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 226 | struct gpio_chip *_chip; |
| 227 | int err = 0; |
| 228 | |
| 229 | /* find where to insert our chip */ |
| 230 | list_for_each(pos, &gpio_chips) { |
| 231 | _chip = list_entry(pos, struct gpio_chip, list); |
| 232 | /* shall we insert before _chip? */ |
| 233 | if (_chip->base >= chip->base + chip->ngpio) |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | /* are we stepping on the chip right before? */ |
| 238 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { |
| 239 | _chip = list_entry(pos->prev, struct gpio_chip, list); |
| 240 | if (_chip->base + _chip->ngpio > chip->base) { |
| 241 | dev_err(chip->dev, |
| 242 | "GPIO integer space overlap, cannot add chip\n"); |
| 243 | err = -EBUSY; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (!err) |
| 248 | list_add_tail(&chip->list, pos); |
| 249 | |
| 250 | return err; |
| 251 | } |
| 252 | |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 253 | /* |
| 254 | * Takes the names from gc->names and checks if they are all unique. If they |
| 255 | * are, they are assigned to their gpio descriptors. |
| 256 | * |
| 257 | * Returns -EEXIST if one of the names is already used for a different GPIO. |
| 258 | */ |
| 259 | static int gpiochip_set_desc_names(struct gpio_chip *gc) |
| 260 | { |
| 261 | int i; |
| 262 | |
| 263 | if (!gc->names) |
| 264 | return 0; |
| 265 | |
| 266 | /* First check all names if they are unique */ |
| 267 | for (i = 0; i != gc->ngpio; ++i) { |
| 268 | struct gpio_desc *gpio; |
| 269 | |
| 270 | gpio = gpio_name_to_desc(gc->names[i]); |
| 271 | if (gpio) { |
| 272 | dev_err(gc->dev, "Detected name collision for GPIO name '%s'\n", |
| 273 | gc->names[i]); |
| 274 | return -EEXIST; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Then add all names to the GPIO descriptors */ |
| 279 | for (i = 0; i != gc->ngpio; ++i) |
| 280 | gc->desc[i].name = gc->names[i]; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 285 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 286 | * gpiochip_add() - register a gpio_chip |
| 287 | * @chip: the chip to register, with chip->base initialized |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 288 | * Context: potentially before irqs will work |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 289 | * |
| 290 | * Returns a negative errno if the chip can't be registered, such as |
| 291 | * because the chip->base is invalid or already associated with a |
| 292 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 293 | * |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 294 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 295 | * can be freely used, the chip->dev device must be registered before |
| 296 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 297 | * for GPIOs will fail rudely. |
| 298 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 299 | * If chip->base is negative, this requests dynamic assignment of |
| 300 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 301 | */ |
| 302 | int gpiochip_add(struct gpio_chip *chip) |
| 303 | { |
| 304 | unsigned long flags; |
| 305 | int status = 0; |
| 306 | unsigned id; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 307 | int base = chip->base; |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 308 | struct gpio_desc *descs; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 309 | |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 310 | descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL); |
| 311 | if (!descs) |
| 312 | return -ENOMEM; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 313 | |
| 314 | spin_lock_irqsave(&gpio_lock, flags); |
| 315 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 316 | if (base < 0) { |
| 317 | base = gpiochip_find_base(chip->ngpio); |
| 318 | if (base < 0) { |
| 319 | status = base; |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 320 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 321 | goto err_free_descs; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 322 | } |
| 323 | chip->base = base; |
| 324 | } |
| 325 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 326 | status = gpiochip_add_to_list(chip); |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 327 | if (status) { |
| 328 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 329 | goto err_free_descs; |
| 330 | } |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 331 | |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 332 | for (id = 0; id < chip->ngpio; id++) { |
| 333 | struct gpio_desc *desc = &descs[id]; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 334 | |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 335 | desc->chip = chip; |
| 336 | |
| 337 | /* REVISIT: most hardware initializes GPIOs as inputs (often |
| 338 | * with pullups enabled) so power usage is minimized. Linux |
| 339 | * code should set the gpio direction first thing; but until |
| 340 | * it does, and in case chip->get_direction is not set, we may |
| 341 | * expose the wrong direction in sysfs. |
| 342 | */ |
| 343 | desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 346 | chip->desc = descs; |
| 347 | |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 348 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 349 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 350 | #ifdef CONFIG_PINCTRL |
| 351 | INIT_LIST_HEAD(&chip->pin_ranges); |
| 352 | #endif |
| 353 | |
Grygorii Strashko | 3726960 | 2015-06-25 20:30:51 +0300 | [diff] [blame] | 354 | if (!chip->owner && chip->dev && chip->dev->driver) |
| 355 | chip->owner = chip->dev->driver->owner; |
| 356 | |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 357 | status = gpiochip_set_desc_names(chip); |
| 358 | if (status) |
| 359 | goto err_remove_from_list; |
| 360 | |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 361 | status = of_gpiochip_add(chip); |
| 362 | if (status) |
| 363 | goto err_remove_chip; |
| 364 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 365 | acpi_gpiochip_add(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 366 | |
Johan Hovold | 426577b | 2015-05-04 17:10:32 +0200 | [diff] [blame] | 367 | status = gpiochip_sysfs_register(chip); |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 368 | if (status) |
| 369 | goto err_remove_chip; |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 370 | |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 371 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
Grant Likely | 64842aa | 2011-11-06 11:36:18 -0700 | [diff] [blame] | 372 | chip->base, chip->base + chip->ngpio - 1, |
| 373 | chip->label ? : "generic"); |
| 374 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 375 | return 0; |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 376 | |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 377 | err_remove_chip: |
| 378 | acpi_gpiochip_remove(chip); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 379 | gpiochip_free_hogs(chip); |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 380 | of_gpiochip_remove(chip); |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 381 | err_remove_from_list: |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 382 | spin_lock_irqsave(&gpio_lock, flags); |
| 383 | list_del(&chip->list); |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 384 | spin_unlock_irqrestore(&gpio_lock, flags); |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 385 | chip->desc = NULL; |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 386 | err_free_descs: |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 387 | kfree(descs); |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 388 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 389 | /* failures here can mean systems won't boot... */ |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 390 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 391 | chip->base, chip->base + chip->ngpio - 1, |
| 392 | chip->label ? : "generic"); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 393 | return status; |
| 394 | } |
| 395 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 396 | |
| 397 | /** |
| 398 | * gpiochip_remove() - unregister a gpio_chip |
| 399 | * @chip: the chip to unregister |
| 400 | * |
| 401 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 402 | */ |
abdoulaye berthe | e1db170 | 2014-07-05 18:28:50 +0200 | [diff] [blame] | 403 | void gpiochip_remove(struct gpio_chip *chip) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 404 | { |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 405 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 406 | unsigned long flags; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 407 | unsigned id; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 408 | bool requested = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 409 | |
Johan Hovold | 426577b | 2015-05-04 17:10:32 +0200 | [diff] [blame] | 410 | gpiochip_sysfs_unregister(chip); |
Johan Hovold | 01cca93 | 2015-01-12 17:12:29 +0100 | [diff] [blame] | 411 | |
Johan Hovold | 00acc3d | 2015-01-12 17:12:27 +0100 | [diff] [blame] | 412 | gpiochip_irqchip_remove(chip); |
| 413 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 414 | acpi_gpiochip_remove(chip); |
Linus Walleij | 9ef0d6f | 2012-11-06 15:15:44 +0100 | [diff] [blame] | 415 | gpiochip_remove_pin_ranges(chip); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 416 | gpiochip_free_hogs(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 417 | of_gpiochip_remove(chip); |
| 418 | |
Johan Hovold | 6798aca | 2015-01-12 17:12:28 +0100 | [diff] [blame] | 419 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 420 | for (id = 0; id < chip->ngpio; id++) { |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 421 | desc = &chip->desc[id]; |
| 422 | desc->chip = NULL; |
| 423 | if (test_bit(FLAG_REQUESTED, &desc->flags)) |
| 424 | requested = true; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 425 | } |
abdoulaye berthe | e1db170 | 2014-07-05 18:28:50 +0200 | [diff] [blame] | 426 | list_del(&chip->list); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 427 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 428 | |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 429 | if (requested) |
| 430 | dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
| 431 | |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 432 | kfree(chip->desc); |
| 433 | chip->desc = NULL; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 434 | } |
| 435 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 436 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 437 | /** |
| 438 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 439 | * @data: data to pass to match function |
| 440 | * @callback: Callback function to check gpio_chip |
| 441 | * |
| 442 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 443 | * determined by a user supplied @match callback. The callback should return |
| 444 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 445 | * non-zero, this function will return to the caller and not iterate over any |
| 446 | * more gpio_chips. |
| 447 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 448 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 449 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 450 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 451 | { |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 452 | struct gpio_chip *chip; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 453 | unsigned long flags; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 454 | |
| 455 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 456 | list_for_each_entry(chip, &gpio_chips, list) |
| 457 | if (match(chip, data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 458 | break; |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 459 | |
| 460 | /* No match? */ |
| 461 | if (&chip->list == &gpio_chips) |
| 462 | chip = NULL; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 463 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 464 | |
| 465 | return chip; |
| 466 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 467 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 468 | |
Alexandre Courbot | 79697ef | 2013-11-16 21:39:32 +0900 | [diff] [blame] | 469 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
| 470 | { |
| 471 | const char *name = data; |
| 472 | |
| 473 | return !strcmp(chip->label, name); |
| 474 | } |
| 475 | |
| 476 | static struct gpio_chip *find_chip_by_name(const char *name) |
| 477 | { |
| 478 | return gpiochip_find((void *)name, gpiochip_match_name); |
| 479 | } |
| 480 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 481 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 482 | |
| 483 | /* |
| 484 | * The following is irqchip helper code for gpiochips. |
| 485 | */ |
| 486 | |
| 487 | /** |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 488 | * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip |
| 489 | * @gpiochip: the gpiochip to set the irqchip chain to |
| 490 | * @irqchip: the irqchip to chain to the gpiochip |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 491 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 492 | * chained irqchip |
| 493 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 494 | * coming out of the gpiochip. If the interrupt is nested rather than |
| 495 | * cascaded, pass NULL in this handler argument |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 496 | */ |
| 497 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 498 | struct irq_chip *irqchip, |
| 499 | int parent_irq, |
| 500 | irq_flow_handler_t parent_handler) |
| 501 | { |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 502 | unsigned int offset; |
| 503 | |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 504 | if (!gpiochip->irqdomain) { |
| 505 | chip_err(gpiochip, "called %s before setting up irqchip\n", |
| 506 | __func__); |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 510 | if (parent_handler) { |
| 511 | if (gpiochip->can_sleep) { |
| 512 | chip_err(gpiochip, |
| 513 | "you cannot have chained interrupts on a " |
| 514 | "chip that may sleep\n"); |
| 515 | return; |
| 516 | } |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 517 | /* |
| 518 | * The parent irqchip is already using the chip_data for this |
| 519 | * irqchip, so our callbacks simply use the handler_data. |
| 520 | */ |
Thomas Gleixner | f7f8775 | 2015-06-21 21:10:48 +0200 | [diff] [blame] | 521 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
| 522 | gpiochip); |
Dmitry Eremin-Solenikov | 25e4fe9 | 2015-05-12 20:12:23 +0300 | [diff] [blame] | 523 | |
| 524 | gpiochip->irq_parent = parent_irq; |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 525 | } |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 526 | |
| 527 | /* Set the parent IRQ for all affected IRQs */ |
| 528 | for (offset = 0; offset < gpiochip->ngpio; offset++) |
| 529 | irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), |
| 530 | parent_irq); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 531 | } |
| 532 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
| 533 | |
| 534 | /** |
| 535 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip |
| 536 | * @d: the irqdomain used by this irqchip |
| 537 | * @irq: the global irq number used by this GPIO irqchip irq |
| 538 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip |
| 539 | * |
| 540 | * This function will set up the mapping for a certain IRQ line on a |
| 541 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip |
| 542 | * stored inside the gpiochip. |
| 543 | */ |
| 544 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
| 545 | irq_hw_number_t hwirq) |
| 546 | { |
| 547 | struct gpio_chip *chip = d->host_data; |
| 548 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 549 | irq_set_chip_data(irq, chip); |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 550 | /* |
| 551 | * This lock class tells lockdep that GPIO irqs are in a different |
| 552 | * category than their parents, so it won't report false recursion. |
| 553 | */ |
| 554 | irq_set_lockdep_class(irq, chip->lock_key); |
Linus Walleij | 7633fb9 | 2014-04-09 13:20:38 +0200 | [diff] [blame] | 555 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 556 | /* Chips that can sleep need nested thread handlers */ |
Octavian Purdila | 295494a | 2014-09-19 23:22:44 +0300 | [diff] [blame] | 557 | if (chip->can_sleep && !chip->irq_not_threaded) |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 558 | irq_set_nested_thread(irq, 1); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 559 | irq_set_noprobe(irq); |
Rob Herring | 23393d4 | 2015-07-27 15:55:16 -0500 | [diff] [blame] | 560 | |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 561 | /* |
| 562 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 563 | * is passed as default type. |
| 564 | */ |
| 565 | if (chip->irq_default_type != IRQ_TYPE_NONE) |
| 566 | irq_set_irq_type(irq, chip->irq_default_type); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 571 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
| 572 | { |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 573 | struct gpio_chip *chip = d->host_data; |
| 574 | |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 575 | if (chip->can_sleep) |
| 576 | irq_set_nested_thread(irq, 0); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 577 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 578 | irq_set_chip_data(irq, NULL); |
| 579 | } |
| 580 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 581 | static const struct irq_domain_ops gpiochip_domain_ops = { |
| 582 | .map = gpiochip_irq_map, |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 583 | .unmap = gpiochip_irq_unmap, |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 584 | /* Virtually all GPIO irqchips are twocell:ed */ |
| 585 | .xlate = irq_domain_xlate_twocell, |
| 586 | }; |
| 587 | |
| 588 | static int gpiochip_irq_reqres(struct irq_data *d) |
| 589 | { |
| 590 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 591 | |
Grygorii Strashko | 5b76e79 | 2015-06-25 20:30:50 +0300 | [diff] [blame] | 592 | if (!try_module_get(chip->owner)) |
| 593 | return -ENODEV; |
| 594 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 595 | if (gpiochip_lock_as_irq(chip, d->hwirq)) { |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 596 | chip_err(chip, |
| 597 | "unable to lock HW IRQ %lu for IRQ\n", |
| 598 | d->hwirq); |
Grygorii Strashko | 5b76e79 | 2015-06-25 20:30:50 +0300 | [diff] [blame] | 599 | module_put(chip->owner); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 600 | return -EINVAL; |
| 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static void gpiochip_irq_relres(struct irq_data *d) |
| 606 | { |
| 607 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 608 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 609 | gpiochip_unlock_as_irq(chip, d->hwirq); |
Grygorii Strashko | 5b76e79 | 2015-06-25 20:30:50 +0300 | [diff] [blame] | 610 | module_put(chip->owner); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
| 614 | { |
| 615 | return irq_find_mapping(chip->irqdomain, offset); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip |
| 620 | * @gpiochip: the gpiochip to remove the irqchip from |
| 621 | * |
| 622 | * This is called only from gpiochip_remove() |
| 623 | */ |
| 624 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) |
| 625 | { |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 626 | unsigned int offset; |
| 627 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 628 | acpi_gpiochip_free_interrupts(gpiochip); |
| 629 | |
Dmitry Eremin-Solenikov | 25e4fe9 | 2015-05-12 20:12:23 +0300 | [diff] [blame] | 630 | if (gpiochip->irq_parent) { |
| 631 | irq_set_chained_handler(gpiochip->irq_parent, NULL); |
| 632 | irq_set_handler_data(gpiochip->irq_parent, NULL); |
| 633 | } |
| 634 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 635 | /* Remove all IRQ mappings and delete the domain */ |
| 636 | if (gpiochip->irqdomain) { |
| 637 | for (offset = 0; offset < gpiochip->ngpio; offset++) |
Grygorii Strashko | e389338 | 2014-09-25 19:09:23 +0300 | [diff] [blame] | 638 | irq_dispose_mapping( |
| 639 | irq_find_mapping(gpiochip->irqdomain, offset)); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 640 | irq_domain_remove(gpiochip->irqdomain); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 641 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 642 | |
| 643 | if (gpiochip->irqchip) { |
| 644 | gpiochip->irqchip->irq_request_resources = NULL; |
| 645 | gpiochip->irqchip->irq_release_resources = NULL; |
| 646 | gpiochip->irqchip = NULL; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * gpiochip_irqchip_add() - adds an irqchip to a gpiochip |
| 652 | * @gpiochip: the gpiochip to add the irqchip to |
| 653 | * @irqchip: the irqchip to add to the gpiochip |
| 654 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 655 | * allocate gpiochip irqs from |
| 656 | * @handler: the irq handler to use (often a predefined irq core function) |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 657 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 658 | * to have the core avoid setting up any default type in the hardware. |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 659 | * @lock_key: lockdep class |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 660 | * |
| 661 | * This function closely associates a certain irqchip with a certain |
| 662 | * gpiochip, providing an irq domain to translate the local IRQs to |
| 663 | * global irqs in the gpiolib core, and making sure that the gpiochip |
| 664 | * is passed as chip data to all related functions. Driver callbacks |
| 665 | * need to use container_of() to get their local state containers back |
| 666 | * from the gpiochip passed as chip data. An irqdomain will be stored |
| 667 | * in the gpiochip that shall be used by the driver to handle IRQ number |
| 668 | * translation. The gpiochip will need to be initialized and registered |
| 669 | * before calling this function. |
| 670 | * |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 671 | * This function will handle two cell:ed simple IRQs and assumes all |
| 672 | * the pins on the gpiochip can generate a unique IRQ. Everything else |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 673 | * need to be open coded. |
| 674 | */ |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 675 | int _gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
| 676 | struct irq_chip *irqchip, |
| 677 | unsigned int first_irq, |
| 678 | irq_flow_handler_t handler, |
| 679 | unsigned int type, |
| 680 | struct lock_class_key *lock_key) |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 681 | { |
| 682 | struct device_node *of_node; |
| 683 | unsigned int offset; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 684 | unsigned irq_base = 0; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 685 | |
| 686 | if (!gpiochip || !irqchip) |
| 687 | return -EINVAL; |
| 688 | |
| 689 | if (!gpiochip->dev) { |
| 690 | pr_err("missing gpiochip .dev parent pointer\n"); |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | of_node = gpiochip->dev->of_node; |
| 694 | #ifdef CONFIG_OF_GPIO |
| 695 | /* |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 696 | * If the gpiochip has an assigned OF node this takes precedence |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 697 | * FIXME: get rid of this and use gpiochip->dev->of_node everywhere |
| 698 | */ |
| 699 | if (gpiochip->of_node) |
| 700 | of_node = gpiochip->of_node; |
| 701 | #endif |
| 702 | gpiochip->irqchip = irqchip; |
| 703 | gpiochip->irq_handler = handler; |
| 704 | gpiochip->irq_default_type = type; |
| 705 | gpiochip->to_irq = gpiochip_to_irq; |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 706 | gpiochip->lock_key = lock_key; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 707 | gpiochip->irqdomain = irq_domain_add_simple(of_node, |
| 708 | gpiochip->ngpio, first_irq, |
| 709 | &gpiochip_domain_ops, gpiochip); |
| 710 | if (!gpiochip->irqdomain) { |
| 711 | gpiochip->irqchip = NULL; |
| 712 | return -EINVAL; |
| 713 | } |
Rabin Vincent | 8b67a1f | 2015-07-31 14:48:56 +0200 | [diff] [blame] | 714 | |
| 715 | /* |
| 716 | * It is possible for a driver to override this, but only if the |
| 717 | * alternative functions are both implemented. |
| 718 | */ |
| 719 | if (!irqchip->irq_request_resources && |
| 720 | !irqchip->irq_release_resources) { |
| 721 | irqchip->irq_request_resources = gpiochip_irq_reqres; |
| 722 | irqchip->irq_release_resources = gpiochip_irq_relres; |
| 723 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * Prepare the mapping since the irqchip shall be orthogonal to |
| 727 | * any gpiochip calls. If the first_irq was zero, this is |
| 728 | * necessary to allocate descriptors for all IRQs. |
| 729 | */ |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 730 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
| 731 | irq_base = irq_create_mapping(gpiochip->irqdomain, offset); |
| 732 | if (offset == 0) |
| 733 | /* |
| 734 | * Store the base into the gpiochip to be used when |
| 735 | * unmapping the irqs. |
| 736 | */ |
| 737 | gpiochip->irq_base = irq_base; |
| 738 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 739 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 740 | acpi_gpiochip_request_interrupts(gpiochip); |
| 741 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 742 | return 0; |
| 743 | } |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 744 | EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 745 | |
| 746 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
| 747 | |
| 748 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
| 749 | |
| 750 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
| 751 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 752 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 753 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 754 | /** |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 755 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 756 | * @chip: the gpiochip to add the range for |
Tomeu Vizoso | d32651f | 2015-06-17 15:42:11 +0200 | [diff] [blame] | 757 | * @pctldev: the pin controller to map to |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 758 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 759 | * @pin_group: name of the pin group inside the pin controller |
| 760 | */ |
| 761 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 762 | struct pinctrl_dev *pctldev, |
| 763 | unsigned int gpio_offset, const char *pin_group) |
| 764 | { |
| 765 | struct gpio_pin_range *pin_range; |
| 766 | int ret; |
| 767 | |
| 768 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 769 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 770 | chip_err(chip, "failed to allocate pin ranges\n"); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 771 | return -ENOMEM; |
| 772 | } |
| 773 | |
| 774 | /* Use local offset as range ID */ |
| 775 | pin_range->range.id = gpio_offset; |
| 776 | pin_range->range.gc = chip; |
| 777 | pin_range->range.name = chip->label; |
| 778 | pin_range->range.base = chip->base + gpio_offset; |
| 779 | pin_range->pctldev = pctldev; |
| 780 | |
| 781 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 782 | &pin_range->range.pins, |
| 783 | &pin_range->range.npins); |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 784 | if (ret < 0) { |
| 785 | kfree(pin_range); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 786 | return ret; |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 787 | } |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 788 | |
| 789 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 790 | |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 791 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 792 | gpio_offset, gpio_offset + pin_range->range.npins - 1, |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 793 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 794 | |
| 795 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 800 | |
| 801 | /** |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 802 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 803 | * @chip: the gpiochip to add the range for |
| 804 | * @pinctrl_name: the dev_name() of the pin controller to map to |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 805 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 806 | * @pin_offset: the start offset in the pin controller number space |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 807 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 808 | * pin controller) to accumulate in this range |
| 809 | */ |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 810 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 811 | unsigned int gpio_offset, unsigned int pin_offset, |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 812 | unsigned int npins) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 813 | { |
| 814 | struct gpio_pin_range *pin_range; |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 815 | int ret; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 816 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 817 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 818 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 819 | chip_err(chip, "failed to allocate pin ranges\n"); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 820 | return -ENOMEM; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 821 | } |
| 822 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 823 | /* Use local offset as range ID */ |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 824 | pin_range->range.id = gpio_offset; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 825 | pin_range->range.gc = chip; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 826 | pin_range->range.name = chip->label; |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 827 | pin_range->range.base = chip->base + gpio_offset; |
| 828 | pin_range->range.pin_base = pin_offset; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 829 | pin_range->range.npins = npins; |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 830 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 831 | &pin_range->range); |
Linus Walleij | 8f23ca1 | 2012-11-20 14:56:25 +0100 | [diff] [blame] | 832 | if (IS_ERR(pin_range->pctldev)) { |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 833 | ret = PTR_ERR(pin_range->pctldev); |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 834 | chip_err(chip, "could not create pin range\n"); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 835 | kfree(pin_range); |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 836 | return ret; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 837 | } |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 838 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 839 | gpio_offset, gpio_offset + npins - 1, |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 840 | pinctl_name, |
| 841 | pin_offset, pin_offset + npins - 1); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 842 | |
| 843 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 844 | |
| 845 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 846 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 847 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 848 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 849 | /** |
| 850 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 851 | * @chip: the chip to remove all the mappings for |
| 852 | */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 853 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 854 | { |
| 855 | struct gpio_pin_range *pin_range, *tmp; |
| 856 | |
| 857 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
| 858 | list_del(&pin_range->node); |
| 859 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 860 | &pin_range->range); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 861 | kfree(pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 862 | } |
| 863 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 864 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 865 | |
| 866 | #endif /* CONFIG_PINCTRL */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 867 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 868 | /* These "optional" allocation calls help prevent drivers from stomping |
| 869 | * on each other, and help provide better diagnostics in debugfs. |
| 870 | * They're called even less than the "set direction" calls. |
| 871 | */ |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 872 | static int __gpiod_request(struct gpio_desc *desc, const char *label) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 873 | { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 874 | struct gpio_chip *chip = desc->chip; |
| 875 | int status; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 876 | unsigned long flags; |
| 877 | |
| 878 | spin_lock_irqsave(&gpio_lock, flags); |
| 879 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 880 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 881 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 882 | */ |
| 883 | |
| 884 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 885 | desc_set_label(desc, label ? : "?"); |
| 886 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 887 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 888 | status = -EBUSY; |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 889 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | if (chip->request) { |
| 893 | /* chip->request may sleep */ |
| 894 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 895 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 896 | spin_lock_irqsave(&gpio_lock, flags); |
| 897 | |
| 898 | if (status < 0) { |
| 899 | desc_set_label(desc, NULL); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 900 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 901 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 902 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 903 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 904 | if (chip->get_direction) { |
| 905 | /* chip->get_direction may sleep */ |
| 906 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 907 | gpiod_get_direction(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 908 | spin_lock_irqsave(&gpio_lock, flags); |
| 909 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 910 | done: |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 911 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 912 | return status; |
| 913 | } |
| 914 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 915 | int gpiod_request(struct gpio_desc *desc, const char *label) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 916 | { |
| 917 | int status = -EPROBE_DEFER; |
| 918 | struct gpio_chip *chip; |
| 919 | |
| 920 | if (!desc) { |
| 921 | pr_warn("%s: invalid GPIO\n", __func__); |
| 922 | return -EINVAL; |
| 923 | } |
| 924 | |
| 925 | chip = desc->chip; |
| 926 | if (!chip) |
| 927 | goto done; |
| 928 | |
| 929 | if (try_module_get(chip->owner)) { |
| 930 | status = __gpiod_request(desc, label); |
| 931 | if (status < 0) |
| 932 | module_put(chip->owner); |
| 933 | } |
| 934 | |
| 935 | done: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 936 | if (status) |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 937 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 938 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 939 | return status; |
| 940 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 941 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 942 | static bool __gpiod_free(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 943 | { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 944 | bool ret = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 945 | unsigned long flags; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 946 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 947 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 948 | might_sleep(); |
| 949 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 950 | gpiod_unexport(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 951 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 952 | spin_lock_irqsave(&gpio_lock, flags); |
| 953 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 954 | chip = desc->chip; |
| 955 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 956 | if (chip->free) { |
| 957 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 958 | might_sleep_if(chip->can_sleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 959 | chip->free(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 960 | spin_lock_irqsave(&gpio_lock, flags); |
| 961 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 962 | desc_set_label(desc, NULL); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 963 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 964 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 965 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 966 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 967 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 968 | ret = true; |
| 969 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 970 | |
| 971 | spin_unlock_irqrestore(&gpio_lock, flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 972 | return ret; |
| 973 | } |
| 974 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 975 | void gpiod_free(struct gpio_desc *desc) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 976 | { |
| 977 | if (desc && __gpiod_free(desc)) |
| 978 | module_put(desc->chip->owner); |
| 979 | else |
| 980 | WARN_ON(extra_checks); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 981 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 982 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 983 | /** |
| 984 | * gpiochip_is_requested - return string iff signal was requested |
| 985 | * @chip: controller managing the signal |
| 986 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 987 | * |
| 988 | * Returns NULL if the GPIO is not currently requested, else a string. |
Alexandre Courbot | 9c8318f | 2014-07-01 14:45:14 +0900 | [diff] [blame] | 989 | * The string returned is the label passed to gpio_request(); if none has been |
| 990 | * passed it is a meaningless, non-NULL constant. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 991 | * |
| 992 | * This function is for use by GPIO controller drivers. The label can |
| 993 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 994 | * can help avoid accidentally multiplexing it to another controller. |
| 995 | */ |
| 996 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 997 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 998 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 999 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1000 | if (!GPIO_OFFSET_VALID(chip, offset)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1001 | return NULL; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1002 | |
| 1003 | desc = &chip->desc[offset]; |
| 1004 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1005 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1006 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1007 | return desc->label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1008 | } |
| 1009 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 1010 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1011 | /** |
| 1012 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor |
| 1013 | * @desc: GPIO descriptor to request |
| 1014 | * @label: label for the GPIO |
| 1015 | * |
| 1016 | * Function allows GPIO chip drivers to request and use their own GPIO |
| 1017 | * descriptors via gpiolib API. Difference to gpiod_request() is that this |
| 1018 | * function will not increase reference count of the GPIO chip module. This |
| 1019 | * allows the GPIO chip module to be unloaded as needed (we assume that the |
| 1020 | * GPIO chip driver handles freeing the GPIOs it has requested). |
| 1021 | */ |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 1022 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
| 1023 | const char *label) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1024 | { |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 1025 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
| 1026 | int err; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1027 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 1028 | if (IS_ERR(desc)) { |
| 1029 | chip_err(chip, "failed to get GPIO descriptor\n"); |
| 1030 | return desc; |
| 1031 | } |
| 1032 | |
| 1033 | err = __gpiod_request(desc, label); |
| 1034 | if (err < 0) |
| 1035 | return ERR_PTR(err); |
| 1036 | |
| 1037 | return desc; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1038 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 1039 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1040 | |
| 1041 | /** |
| 1042 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver |
| 1043 | * @desc: GPIO descriptor to free |
| 1044 | * |
| 1045 | * Function frees the given GPIO requested previously with |
| 1046 | * gpiochip_request_own_desc(). |
| 1047 | */ |
| 1048 | void gpiochip_free_own_desc(struct gpio_desc *desc) |
| 1049 | { |
| 1050 | if (desc) |
| 1051 | __gpiod_free(desc); |
| 1052 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 1053 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1054 | |
| 1055 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 1056 | * some cases this is done in early boot, before IRQs are enabled. |
| 1057 | * |
| 1058 | * As a rule these aren't called more than once (except for drivers |
| 1059 | * using the open-drain emulation idiom) so these are natural places |
| 1060 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 1061 | * rely on gpio_request() having been called beforehand. |
| 1062 | */ |
| 1063 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1064 | /** |
| 1065 | * gpiod_direction_input - set the GPIO direction to input |
| 1066 | * @desc: GPIO to set to input |
| 1067 | * |
| 1068 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 1069 | * be called safely on it. |
| 1070 | * |
| 1071 | * Return 0 in case of success, else an error code. |
| 1072 | */ |
| 1073 | int gpiod_direction_input(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1074 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1075 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1076 | int status = -EINVAL; |
| 1077 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1078 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1079 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1080 | return -EINVAL; |
| 1081 | } |
| 1082 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1083 | chip = desc->chip; |
| 1084 | if (!chip->get || !chip->direction_input) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1085 | gpiod_warn(desc, |
| 1086 | "%s: missing get() or direction_input() operations\n", |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 1087 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1088 | return -EIO; |
| 1089 | } |
| 1090 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 1091 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1092 | if (status == 0) |
| 1093 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1094 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1095 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 1096 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1097 | return status; |
| 1098 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1099 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1100 | |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 1101 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1102 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1103 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1104 | int status = -EINVAL; |
| 1105 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1106 | /* GPIOs used for IRQs shall not be set as output */ |
| 1107 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
| 1108 | gpiod_err(desc, |
| 1109 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 1110 | __func__); |
| 1111 | return -EIO; |
| 1112 | } |
| 1113 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1114 | /* Open drain pin should not be driven to 1 */ |
| 1115 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1116 | return gpiod_direction_input(desc); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1117 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1118 | /* Open source pin should not be driven to 0 */ |
| 1119 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1120 | return gpiod_direction_input(desc); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1121 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1122 | chip = desc->chip; |
| 1123 | if (!chip->set || !chip->direction_output) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1124 | gpiod_warn(desc, |
| 1125 | "%s: missing set() or direction_output() operations\n", |
| 1126 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1127 | return -EIO; |
| 1128 | } |
| 1129 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 1130 | status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1131 | if (status == 0) |
| 1132 | set_bit(FLAG_IS_OUT, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1133 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1134 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1135 | return status; |
| 1136 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 1137 | |
| 1138 | /** |
| 1139 | * gpiod_direction_output_raw - set the GPIO direction to output |
| 1140 | * @desc: GPIO to set to output |
| 1141 | * @value: initial output value of the GPIO |
| 1142 | * |
| 1143 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1144 | * be called safely on it. The initial value of the output must be specified |
| 1145 | * as raw value on the physical line without regard for the ACTIVE_LOW status. |
| 1146 | * |
| 1147 | * Return 0 in case of success, else an error code. |
| 1148 | */ |
| 1149 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 1150 | { |
| 1151 | if (!desc || !desc->chip) { |
| 1152 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1153 | return -EINVAL; |
| 1154 | } |
| 1155 | return _gpiod_direction_output_raw(desc, value); |
| 1156 | } |
| 1157 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); |
| 1158 | |
| 1159 | /** |
Rahul Bedarkar | 90df4fe | 2014-02-08 11:55:25 +0530 | [diff] [blame] | 1160 | * gpiod_direction_output - set the GPIO direction to output |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 1161 | * @desc: GPIO to set to output |
| 1162 | * @value: initial output value of the GPIO |
| 1163 | * |
| 1164 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1165 | * be called safely on it. The initial value of the output must be specified |
| 1166 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1167 | * account. |
| 1168 | * |
| 1169 | * Return 0 in case of success, else an error code. |
| 1170 | */ |
| 1171 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 1172 | { |
| 1173 | if (!desc || !desc->chip) { |
| 1174 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1178 | value = !value; |
| 1179 | return _gpiod_direction_output_raw(desc, value); |
| 1180 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1181 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1182 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1183 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1184 | * gpiod_set_debounce - sets @debounce time for a @gpio |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1185 | * @gpio: the gpio to set debounce time |
| 1186 | * @debounce: debounce time is microseconds |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1187 | * |
| 1188 | * returns -ENOTSUPP if the controller does not support setting |
| 1189 | * debounce. |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1190 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1191 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1192 | { |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1193 | struct gpio_chip *chip; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1194 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1195 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1196 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1200 | chip = desc->chip; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1201 | if (!chip->set || !chip->set_debounce) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1202 | gpiod_dbg(desc, |
| 1203 | "%s: missing set() or set_debounce() operations\n", |
| 1204 | __func__); |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1205 | return -ENOTSUPP; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 1208 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1209 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1210 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1211 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1212 | /** |
| 1213 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 1214 | * @desc: the gpio descriptor to test |
| 1215 | * |
| 1216 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 1217 | */ |
| 1218 | int gpiod_is_active_low(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1219 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1220 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1221 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1222 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1223 | |
| 1224 | /* I/O calls are only valid after configuration completed; the relevant |
| 1225 | * "is this a valid GPIO" error checks should already have been done. |
| 1226 | * |
| 1227 | * "Get" operations are often inlinable as reading a pin value register, |
| 1228 | * and masking the relevant bit in that register. |
| 1229 | * |
| 1230 | * When "set" operations are inlinable, they involve writing that mask to |
| 1231 | * one register to set a low value, or a different register to set it high. |
| 1232 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1233 | * |
| 1234 | *------------------------------------------------------------------------ |
| 1235 | * |
| 1236 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1237 | * have requested the GPIO. That can include implicit requesting by |
| 1238 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1239 | * in memory, guaranteeing that these table lookups need no more locking |
| 1240 | * and that gpiochip_remove() will fail. |
| 1241 | * |
| 1242 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1243 | * that the GPIO was actually requested. |
| 1244 | */ |
| 1245 | |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1246 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1247 | { |
| 1248 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1249 | int offset; |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1250 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1251 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1252 | chip = desc->chip; |
| 1253 | offset = gpio_chip_hwgpio(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1254 | value = chip->get ? chip->get(chip, offset) : -EIO; |
| 1255 | value = value < 0 ? value : !!value; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1256 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1257 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1258 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1259 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1260 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1261 | * gpiod_get_raw_value() - return a gpio's raw value |
| 1262 | * @desc: gpio whose value will be returned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1263 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1264 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1265 | * its ACTIVE_LOW status, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1266 | * |
| 1267 | * This function should be called from contexts where we cannot sleep, and will |
| 1268 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1269 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1270 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1271 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1272 | if (!desc) |
| 1273 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1274 | /* Should be using gpio_get_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 1275 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1276 | return _gpiod_get_raw_value(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1277 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1278 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1279 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1280 | /** |
| 1281 | * gpiod_get_value() - return a gpio's value |
| 1282 | * @desc: gpio whose value will be returned |
| 1283 | * |
| 1284 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1285 | * account, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1286 | * |
| 1287 | * This function should be called from contexts where we cannot sleep, and will |
| 1288 | * complain if the GPIO chip functions potentially sleep. |
| 1289 | */ |
| 1290 | int gpiod_get_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1291 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1292 | int value; |
| 1293 | if (!desc) |
| 1294 | return 0; |
| 1295 | /* Should be using gpio_get_value_cansleep() */ |
| 1296 | WARN_ON(desc->chip->can_sleep); |
| 1297 | |
| 1298 | value = _gpiod_get_raw_value(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1299 | if (value < 0) |
| 1300 | return value; |
| 1301 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1302 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1303 | value = !value; |
| 1304 | |
| 1305 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1306 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1307 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1308 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1309 | /* |
| 1310 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1311 | * @desc: gpio descriptor whose state need to be set. |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 1312 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1313 | */ |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1314 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1315 | { |
| 1316 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1317 | struct gpio_chip *chip = desc->chip; |
| 1318 | int offset = gpio_chip_hwgpio(desc); |
| 1319 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1320 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1321 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1322 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1323 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1324 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1325 | err = chip->direction_output(chip, offset, 0); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1326 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1327 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1328 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1329 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1330 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1331 | gpiod_err(desc, |
| 1332 | "%s: Error in set_value for open drain err %d\n", |
| 1333 | __func__, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1334 | } |
| 1335 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1336 | /* |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1337 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 1338 | * @desc: gpio descriptor whose state need to be set. |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 1339 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1340 | */ |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1341 | static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1342 | { |
| 1343 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1344 | struct gpio_chip *chip = desc->chip; |
| 1345 | int offset = gpio_chip_hwgpio(desc); |
| 1346 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1347 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1348 | err = chip->direction_output(chip, offset, 1); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1349 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1350 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1351 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1352 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1353 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1354 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1355 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1356 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1357 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1358 | gpiod_err(desc, |
| 1359 | "%s: Error in set_value for open source err %d\n", |
| 1360 | __func__, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1361 | } |
| 1362 | |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1363 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1364 | { |
| 1365 | struct gpio_chip *chip; |
| 1366 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1367 | chip = desc->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1368 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1369 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 1370 | _gpio_set_open_drain_value(desc, value); |
| 1371 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 1372 | _gpio_set_open_source_value(desc, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1373 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1374 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
| 1375 | } |
| 1376 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1377 | /* |
| 1378 | * set multiple outputs on the same chip; |
| 1379 | * use the chip's set_multiple function if available; |
| 1380 | * otherwise set the outputs sequentially; |
| 1381 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word |
| 1382 | * defines which outputs are to be changed |
| 1383 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word |
| 1384 | * defines the values the outputs specified by mask are to be set to |
| 1385 | */ |
| 1386 | static void gpio_chip_set_multiple(struct gpio_chip *chip, |
| 1387 | unsigned long *mask, unsigned long *bits) |
| 1388 | { |
| 1389 | if (chip->set_multiple) { |
| 1390 | chip->set_multiple(chip, mask, bits); |
| 1391 | } else { |
| 1392 | int i; |
| 1393 | for (i = 0; i < chip->ngpio; i++) { |
| 1394 | if (mask[BIT_WORD(i)] == 0) { |
| 1395 | /* no more set bits in this mask word; |
| 1396 | * skip ahead to the next word */ |
| 1397 | i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1; |
| 1398 | continue; |
| 1399 | } |
| 1400 | /* set outputs if the corresponding mask bit is set */ |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1401 | if (__test_and_clear_bit(i, mask)) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1402 | chip->set(chip, i, test_bit(i, bits)); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1407 | static void gpiod_set_array_value_priv(bool raw, bool can_sleep, |
| 1408 | unsigned int array_size, |
| 1409 | struct gpio_desc **desc_array, |
| 1410 | int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1411 | { |
| 1412 | int i = 0; |
| 1413 | |
| 1414 | while (i < array_size) { |
| 1415 | struct gpio_chip *chip = desc_array[i]->chip; |
| 1416 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; |
| 1417 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; |
| 1418 | int count = 0; |
| 1419 | |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1420 | if (!can_sleep) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1421 | WARN_ON(chip->can_sleep); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1422 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1423 | memset(mask, 0, sizeof(mask)); |
| 1424 | do { |
| 1425 | struct gpio_desc *desc = desc_array[i]; |
| 1426 | int hwgpio = gpio_chip_hwgpio(desc); |
| 1427 | int value = value_array[i]; |
| 1428 | |
| 1429 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1430 | value = !value; |
| 1431 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1432 | /* |
| 1433 | * collect all normal outputs belonging to the same chip |
| 1434 | * open drain and open source outputs are set individually |
| 1435 | */ |
| 1436 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1437 | _gpio_set_open_drain_value(desc, value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1438 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
| 1439 | _gpio_set_open_source_value(desc, value); |
| 1440 | } else { |
| 1441 | __set_bit(hwgpio, mask); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1442 | if (value) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1443 | __set_bit(hwgpio, bits); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1444 | else |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1445 | __clear_bit(hwgpio, bits); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1446 | count++; |
| 1447 | } |
| 1448 | i++; |
| 1449 | } while ((i < array_size) && (desc_array[i]->chip == chip)); |
| 1450 | /* push collected bits to outputs */ |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 1451 | if (count != 0) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1452 | gpio_chip_set_multiple(chip, mask, bits); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1456 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1457 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 1458 | * @desc: gpio whose value will be assigned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1459 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1460 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1461 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 1462 | * regard for its ACTIVE_LOW status. |
| 1463 | * |
| 1464 | * This function should be called from contexts where we cannot sleep, and will |
| 1465 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1466 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1467 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1468 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1469 | if (!desc) |
| 1470 | return; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1471 | /* Should be using gpio_set_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 1472 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1473 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1474 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1475 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1476 | |
| 1477 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1478 | * gpiod_set_value() - assign a gpio's value |
| 1479 | * @desc: gpio whose value will be assigned |
| 1480 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1481 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1482 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1483 | * account |
| 1484 | * |
| 1485 | * This function should be called from contexts where we cannot sleep, and will |
| 1486 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1487 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1488 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 1489 | { |
| 1490 | if (!desc) |
| 1491 | return; |
| 1492 | /* Should be using gpio_set_value_cansleep() */ |
| 1493 | WARN_ON(desc->chip->can_sleep); |
| 1494 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1495 | value = !value; |
| 1496 | _gpiod_set_raw_value(desc, value); |
| 1497 | } |
| 1498 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 1499 | |
| 1500 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1501 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1502 | * @array_size: number of elements in the descriptor / value arrays |
| 1503 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1504 | * @value_array: array of values to assign |
| 1505 | * |
| 1506 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 1507 | * without regard for their ACTIVE_LOW status. |
| 1508 | * |
| 1509 | * This function should be called from contexts where we cannot sleep, and will |
| 1510 | * complain if the GPIO chip functions potentially sleep. |
| 1511 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1512 | void gpiod_set_raw_array_value(unsigned int array_size, |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1513 | struct gpio_desc **desc_array, int *value_array) |
| 1514 | { |
| 1515 | if (!desc_array) |
| 1516 | return; |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1517 | gpiod_set_array_value_priv(true, false, array_size, desc_array, |
| 1518 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1519 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1520 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1521 | |
| 1522 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1523 | * gpiod_set_array_value() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1524 | * @array_size: number of elements in the descriptor / value arrays |
| 1525 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1526 | * @value_array: array of values to assign |
| 1527 | * |
| 1528 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 1529 | * into account. |
| 1530 | * |
| 1531 | * This function should be called from contexts where we cannot sleep, and will |
| 1532 | * complain if the GPIO chip functions potentially sleep. |
| 1533 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1534 | void gpiod_set_array_value(unsigned int array_size, |
| 1535 | struct gpio_desc **desc_array, int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1536 | { |
| 1537 | if (!desc_array) |
| 1538 | return; |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1539 | gpiod_set_array_value_priv(false, false, array_size, desc_array, |
| 1540 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1541 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1542 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1543 | |
| 1544 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1545 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 1546 | * @desc: gpio to check |
| 1547 | * |
| 1548 | */ |
| 1549 | int gpiod_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1550 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1551 | if (!desc) |
| 1552 | return 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1553 | return desc->chip->can_sleep; |
| 1554 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1555 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1556 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1557 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1558 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 1559 | * @desc: gpio whose IRQ will be returned (already requested) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1560 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1561 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 1562 | * error. |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1563 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1564 | int gpiod_to_irq(const struct gpio_desc *desc) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1565 | { |
| 1566 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1567 | int offset; |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1568 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1569 | if (!desc) |
| 1570 | return -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1571 | chip = desc->chip; |
| 1572 | offset = gpio_chip_hwgpio(desc); |
| 1573 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
| 1574 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1575 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1576 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1577 | /** |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1578 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1579 | * @chip: the chip the GPIO to lock belongs to |
| 1580 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1581 | * |
| 1582 | * This is used directly by GPIO drivers that want to lock down |
Linus Walleij | f438acd | 2014-03-07 10:12:49 +0800 | [diff] [blame] | 1583 | * a certain GPIO line to be used for IRQs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1584 | */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1585 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1586 | { |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1587 | if (offset >= chip->ngpio) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1588 | return -EINVAL; |
| 1589 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1590 | if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) { |
| 1591 | chip_err(chip, |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1592 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 1593 | __func__); |
| 1594 | return -EIO; |
| 1595 | } |
| 1596 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1597 | set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1598 | return 0; |
| 1599 | } |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1600 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1601 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1602 | /** |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1603 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1604 | * @chip: the chip the GPIO to lock belongs to |
| 1605 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1606 | * |
| 1607 | * This is used directly by GPIO drivers that want to indicate |
| 1608 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 1609 | */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1610 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1611 | { |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1612 | if (offset >= chip->ngpio) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1613 | return; |
| 1614 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1615 | clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1616 | } |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1617 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1618 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1619 | /** |
| 1620 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 1621 | * @desc: gpio whose value will be returned |
| 1622 | * |
| 1623 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1624 | * its ACTIVE_LOW status, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1625 | * |
| 1626 | * This function is to be called from contexts that can sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1627 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1628 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1629 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1630 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1631 | if (!desc) |
| 1632 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1633 | return _gpiod_get_raw_value(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1634 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1635 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1636 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1637 | /** |
| 1638 | * gpiod_get_value_cansleep() - return a gpio's value |
| 1639 | * @desc: gpio whose value will be returned |
| 1640 | * |
| 1641 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1642 | * account, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1643 | * |
| 1644 | * This function is to be called from contexts that can sleep. |
| 1645 | */ |
| 1646 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1647 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1648 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1649 | |
| 1650 | might_sleep_if(extra_checks); |
| 1651 | if (!desc) |
| 1652 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1653 | |
| 1654 | value = _gpiod_get_raw_value(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 1655 | if (value < 0) |
| 1656 | return value; |
| 1657 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1658 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1659 | value = !value; |
| 1660 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1661 | return value; |
| 1662 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1663 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1664 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1665 | /** |
| 1666 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 1667 | * @desc: gpio whose value will be assigned |
| 1668 | * @value: value to assign |
| 1669 | * |
| 1670 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 1671 | * regard for its ACTIVE_LOW status. |
| 1672 | * |
| 1673 | * This function is to be called from contexts that can sleep. |
| 1674 | */ |
| 1675 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1676 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1677 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1678 | if (!desc) |
| 1679 | return; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1680 | _gpiod_set_raw_value(desc, value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1681 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1682 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1683 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1684 | /** |
| 1685 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 1686 | * @desc: gpio whose value will be assigned |
| 1687 | * @value: value to assign |
| 1688 | * |
| 1689 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1690 | * account |
| 1691 | * |
| 1692 | * This function is to be called from contexts that can sleep. |
| 1693 | */ |
| 1694 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1695 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1696 | might_sleep_if(extra_checks); |
| 1697 | if (!desc) |
| 1698 | return; |
| 1699 | |
| 1700 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1701 | value = !value; |
| 1702 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1703 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1704 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1705 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1706 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1707 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1708 | * @array_size: number of elements in the descriptor / value arrays |
| 1709 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1710 | * @value_array: array of values to assign |
| 1711 | * |
| 1712 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 1713 | * without regard for their ACTIVE_LOW status. |
| 1714 | * |
| 1715 | * This function is to be called from contexts that can sleep. |
| 1716 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1717 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
| 1718 | struct gpio_desc **desc_array, |
| 1719 | int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1720 | { |
| 1721 | might_sleep_if(extra_checks); |
| 1722 | if (!desc_array) |
| 1723 | return; |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1724 | gpiod_set_array_value_priv(true, true, array_size, desc_array, |
| 1725 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1726 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1727 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1728 | |
| 1729 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1730 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1731 | * @array_size: number of elements in the descriptor / value arrays |
| 1732 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1733 | * @value_array: array of values to assign |
| 1734 | * |
| 1735 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 1736 | * into account. |
| 1737 | * |
| 1738 | * This function is to be called from contexts that can sleep. |
| 1739 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1740 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
| 1741 | struct gpio_desc **desc_array, |
| 1742 | int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1743 | { |
| 1744 | might_sleep_if(extra_checks); |
| 1745 | if (!desc_array) |
| 1746 | return; |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1747 | gpiod_set_array_value_priv(false, true, array_size, desc_array, |
| 1748 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1749 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 1750 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 1751 | |
| 1752 | /** |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1753 | * gpiod_add_lookup_table() - register GPIO device consumers |
| 1754 | * @table: table of consumers to register |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1755 | */ |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1756 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1757 | { |
| 1758 | mutex_lock(&gpio_lookup_lock); |
| 1759 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1760 | list_add_tail(&table->list, &gpio_lookup_list); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1761 | |
| 1762 | mutex_unlock(&gpio_lookup_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Shobhit Kumar | be9015a | 2015-06-26 14:32:04 +0530 | [diff] [blame] | 1765 | /** |
| 1766 | * gpiod_remove_lookup_table() - unregister GPIO device consumers |
| 1767 | * @table: table of consumers to unregister |
| 1768 | */ |
| 1769 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) |
| 1770 | { |
| 1771 | mutex_lock(&gpio_lookup_lock); |
| 1772 | |
| 1773 | list_del(&table->list); |
| 1774 | |
| 1775 | mutex_unlock(&gpio_lookup_lock); |
| 1776 | } |
| 1777 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1778 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1779 | unsigned int idx, |
| 1780 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1781 | { |
| 1782 | char prop_name[32]; /* 32 is max size of property name */ |
| 1783 | enum of_gpio_flags of_flags; |
| 1784 | struct gpio_desc *desc; |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1785 | unsigned int i; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1786 | |
Rojhalat Ibrahim | 7f2e553 | 2015-02-11 17:27:55 +0100 | [diff] [blame] | 1787 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1788 | if (con_id) |
Olliver Schinagl | 9e08924 | 2015-01-21 22:33:45 +0100 | [diff] [blame] | 1789 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, |
Rojhalat Ibrahim | 7f2e553 | 2015-02-11 17:27:55 +0100 | [diff] [blame] | 1790 | gpio_suffixes[i]); |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1791 | else |
Olliver Schinagl | 9e08924 | 2015-01-21 22:33:45 +0100 | [diff] [blame] | 1792 | snprintf(prop_name, sizeof(prop_name), "%s", |
Rojhalat Ibrahim | 7f2e553 | 2015-02-11 17:27:55 +0100 | [diff] [blame] | 1793 | gpio_suffixes[i]); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1794 | |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1795 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
| 1796 | &of_flags); |
Tony Lindgren | 06fc3b7 | 2014-06-02 16:13:46 -0700 | [diff] [blame] | 1797 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1798 | break; |
| 1799 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1800 | |
| 1801 | if (IS_ERR(desc)) |
| 1802 | return desc; |
| 1803 | |
| 1804 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1805 | *flags |= GPIO_ACTIVE_LOW; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1806 | |
| 1807 | return desc; |
| 1808 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1809 | |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1810 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1811 | unsigned int idx, |
| 1812 | enum gpio_lookup_flags *flags) |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1813 | { |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1814 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 1815 | struct acpi_gpio_info info; |
| 1816 | struct gpio_desc *desc; |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1817 | char propname[32]; |
| 1818 | int i; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 1819 | |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1820 | /* Try first from _DSD */ |
Rojhalat Ibrahim | 7f2e553 | 2015-02-11 17:27:55 +0100 | [diff] [blame] | 1821 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1822 | if (con_id && strcmp(con_id, "gpios")) { |
| 1823 | snprintf(propname, sizeof(propname), "%s-%s", |
Rojhalat Ibrahim | 7f2e553 | 2015-02-11 17:27:55 +0100 | [diff] [blame] | 1824 | con_id, gpio_suffixes[i]); |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1825 | } else { |
| 1826 | snprintf(propname, sizeof(propname), "%s", |
Rojhalat Ibrahim | 7f2e553 | 2015-02-11 17:27:55 +0100 | [diff] [blame] | 1827 | gpio_suffixes[i]); |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1828 | } |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 1829 | |
Mika Westerberg | 0d9a693 | 2014-10-29 15:41:01 +0100 | [diff] [blame] | 1830 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); |
| 1831 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
| 1832 | break; |
| 1833 | } |
| 1834 | |
| 1835 | /* Then from plain _CRS GPIOs */ |
| 1836 | if (IS_ERR(desc)) { |
| 1837 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); |
| 1838 | if (IS_ERR(desc)) |
| 1839 | return desc; |
| 1840 | } |
| 1841 | |
| 1842 | if (info.active_low) |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1843 | *flags |= GPIO_ACTIVE_LOW; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 1844 | |
| 1845 | return desc; |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1846 | } |
| 1847 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1848 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
| 1849 | { |
| 1850 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 1851 | struct gpiod_lookup_table *table; |
| 1852 | |
| 1853 | mutex_lock(&gpio_lookup_lock); |
| 1854 | |
| 1855 | list_for_each_entry(table, &gpio_lookup_list, list) { |
| 1856 | if (table->dev_id && dev_id) { |
| 1857 | /* |
| 1858 | * Valid strings on both ends, must be identical to have |
| 1859 | * a match |
| 1860 | */ |
| 1861 | if (!strcmp(table->dev_id, dev_id)) |
| 1862 | goto found; |
| 1863 | } else { |
| 1864 | /* |
| 1865 | * One of the pointers is NULL, so both must be to have |
| 1866 | * a match |
| 1867 | */ |
| 1868 | if (dev_id == table->dev_id) |
| 1869 | goto found; |
| 1870 | } |
| 1871 | } |
| 1872 | table = NULL; |
| 1873 | |
| 1874 | found: |
| 1875 | mutex_unlock(&gpio_lookup_lock); |
| 1876 | return table; |
| 1877 | } |
| 1878 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1879 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1880 | unsigned int idx, |
| 1881 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1882 | { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1883 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1884 | struct gpiod_lookup_table *table; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1885 | struct gpiod_lookup *p; |
| 1886 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1887 | table = gpiod_find_lookup_table(dev); |
| 1888 | if (!table) |
| 1889 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1890 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1891 | for (p = &table->table[0]; p->chip_label; p++) { |
| 1892 | struct gpio_chip *chip; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1893 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1894 | /* idx must always match exactly */ |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1895 | if (p->idx != idx) |
| 1896 | continue; |
| 1897 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1898 | /* If the lookup entry has a con_id, require exact match */ |
| 1899 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) |
| 1900 | continue; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1901 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1902 | chip = find_chip_by_name(p->chip_label); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1903 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1904 | if (!chip) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1905 | dev_err(dev, "cannot find GPIO chip %s\n", |
| 1906 | p->chip_label); |
| 1907 | return ERR_PTR(-ENODEV); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1908 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1909 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1910 | if (chip->ngpio <= p->chip_hwnum) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1911 | dev_err(dev, |
| 1912 | "requested GPIO %d is out of range [0..%d] for chip %s\n", |
| 1913 | idx, chip->ngpio, chip->label); |
| 1914 | return ERR_PTR(-EINVAL); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1915 | } |
| 1916 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 1917 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1918 | *flags = p->flags; |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1919 | |
| 1920 | return desc; |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1921 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1922 | |
| 1923 | return desc; |
| 1924 | } |
| 1925 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 1926 | static int dt_gpio_count(struct device *dev, const char *con_id) |
| 1927 | { |
| 1928 | int ret; |
| 1929 | char propname[32]; |
| 1930 | unsigned int i; |
| 1931 | |
| 1932 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 1933 | if (con_id) |
| 1934 | snprintf(propname, sizeof(propname), "%s-%s", |
| 1935 | con_id, gpio_suffixes[i]); |
| 1936 | else |
| 1937 | snprintf(propname, sizeof(propname), "%s", |
| 1938 | gpio_suffixes[i]); |
| 1939 | |
| 1940 | ret = of_gpio_named_count(dev->of_node, propname); |
| 1941 | if (ret >= 0) |
| 1942 | break; |
| 1943 | } |
| 1944 | return ret; |
| 1945 | } |
| 1946 | |
| 1947 | static int platform_gpio_count(struct device *dev, const char *con_id) |
| 1948 | { |
| 1949 | struct gpiod_lookup_table *table; |
| 1950 | struct gpiod_lookup *p; |
| 1951 | unsigned int count = 0; |
| 1952 | |
| 1953 | table = gpiod_find_lookup_table(dev); |
| 1954 | if (!table) |
| 1955 | return -ENOENT; |
| 1956 | |
| 1957 | for (p = &table->table[0]; p->chip_label; p++) { |
| 1958 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || |
| 1959 | (!con_id && !p->con_id)) |
| 1960 | count++; |
| 1961 | } |
| 1962 | if (!count) |
| 1963 | return -ENOENT; |
| 1964 | |
| 1965 | return count; |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * gpiod_count - return the number of GPIOs associated with a device / function |
| 1970 | * or -ENOENT if no GPIO has been assigned to the requested function |
| 1971 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 1972 | * @con_id: function within the GPIO consumer |
| 1973 | */ |
| 1974 | int gpiod_count(struct device *dev, const char *con_id) |
| 1975 | { |
| 1976 | int count = -ENOENT; |
| 1977 | |
| 1978 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
| 1979 | count = dt_gpio_count(dev, con_id); |
| 1980 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) |
| 1981 | count = acpi_gpio_count(dev, con_id); |
| 1982 | |
| 1983 | if (count < 0) |
| 1984 | count = platform_gpio_count(dev, con_id); |
| 1985 | |
| 1986 | return count; |
| 1987 | } |
| 1988 | EXPORT_SYMBOL_GPL(gpiod_count); |
| 1989 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1990 | /** |
Thierry Reding | 0879162 | 2014-04-25 16:54:22 +0200 | [diff] [blame] | 1991 | * gpiod_get - obtain a GPIO for a given GPIO function |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1992 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1993 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 1994 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1995 | * |
| 1996 | * Return the GPIO descriptor corresponding to the function con_id of device |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1997 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 1998 | * another IS_ERR() code if an error occurred while trying to acquire the GPIO. |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1999 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2000 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2001 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2002 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2003 | return gpiod_get_index(dev, con_id, 0, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2004 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2005 | EXPORT_SYMBOL_GPL(gpiod_get); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2006 | |
| 2007 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2008 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function |
| 2009 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2010 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2011 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2012 | * |
| 2013 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to |
| 2014 | * the requested function it will return NULL. This is convenient for drivers |
| 2015 | * that need to handle optional GPIOs. |
| 2016 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2017 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2018 | const char *con_id, |
| 2019 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2020 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2021 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2022 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2023 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2024 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2025 | |
| 2026 | /** |
| 2027 | * gpiod_configure_flags - helper function to configure a given GPIO |
| 2028 | * @desc: gpio whose value will be assigned |
| 2029 | * @con_id: function within the GPIO consumer |
| 2030 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
| 2031 | * of_get_gpio_hog() |
| 2032 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 2033 | * |
| 2034 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the |
| 2035 | * requested function and/or index, or another IS_ERR() code if an error |
| 2036 | * occurred while trying to acquire the GPIO. |
| 2037 | */ |
| 2038 | static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
| 2039 | unsigned long lflags, enum gpiod_flags dflags) |
| 2040 | { |
| 2041 | int status; |
| 2042 | |
| 2043 | if (lflags & GPIO_ACTIVE_LOW) |
| 2044 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 2045 | if (lflags & GPIO_OPEN_DRAIN) |
| 2046 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 2047 | if (lflags & GPIO_OPEN_SOURCE) |
| 2048 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 2049 | |
| 2050 | /* No particular flag request, return here... */ |
| 2051 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { |
| 2052 | pr_debug("no flags found for %s\n", con_id); |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | /* Process flags */ |
| 2057 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) |
| 2058 | status = gpiod_direction_output(desc, |
| 2059 | dflags & GPIOD_FLAGS_BIT_DIR_VAL); |
| 2060 | else |
| 2061 | status = gpiod_direction_input(desc); |
| 2062 | |
| 2063 | return status; |
| 2064 | } |
| 2065 | |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2066 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2067 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
Andy Shevchenko | fdd6a5f | 2013-12-05 11:26:26 +0200 | [diff] [blame] | 2068 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2069 | * @con_id: function within the GPIO consumer |
| 2070 | * @idx: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2071 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2072 | * |
| 2073 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 2074 | * defined one for functions that define several GPIOs. |
| 2075 | * |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 2076 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
| 2077 | * requested function and/or index, or another IS_ERR() code if an error |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 2078 | * occurred while trying to acquire the GPIO. |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2079 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2080 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2081 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2082 | unsigned int idx, |
| 2083 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2084 | { |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 2085 | struct gpio_desc *desc = NULL; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2086 | int status; |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2087 | enum gpio_lookup_flags lookupflags = 0; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2088 | |
| 2089 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 2090 | |
Rafael J. Wysocki | 4d8440b | 2015-03-10 23:08:57 +0100 | [diff] [blame] | 2091 | if (dev) { |
| 2092 | /* Using device tree? */ |
| 2093 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 2094 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 2095 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); |
| 2096 | } else if (ACPI_COMPANION(dev)) { |
| 2097 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
| 2098 | desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); |
| 2099 | } |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | /* |
| 2103 | * Either we are not using DT or ACPI, or their lookup did not return |
| 2104 | * a result. In that case, use platform lookup as a fallback. |
| 2105 | */ |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 2106 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
Alexander Shiyan | 43a8785 | 2014-09-19 11:39:25 +0400 | [diff] [blame] | 2107 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2108 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | if (IS_ERR(desc)) { |
Heikki Krogerus | 351cfe0 | 2013-11-29 15:47:34 +0200 | [diff] [blame] | 2112 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2113 | return desc; |
| 2114 | } |
| 2115 | |
| 2116 | status = gpiod_request(desc, con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2117 | if (status < 0) |
| 2118 | return ERR_PTR(status); |
| 2119 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2120 | status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2121 | if (status < 0) { |
| 2122 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
| 2123 | gpiod_put(desc); |
| 2124 | return ERR_PTR(status); |
| 2125 | } |
| 2126 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2127 | return desc; |
| 2128 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2129 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2130 | |
| 2131 | /** |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 2132 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node |
| 2133 | * @fwnode: handle of the firmware node |
| 2134 | * @propname: name of the firmware property representing the GPIO |
| 2135 | * |
| 2136 | * This function can be used for drivers that get their configuration |
| 2137 | * from firmware. |
| 2138 | * |
| 2139 | * Function properly finds the corresponding GPIO using whatever is the |
| 2140 | * underlying firmware interface and then makes sure that the GPIO |
| 2141 | * descriptor is requested before it is returned to the caller. |
| 2142 | * |
| 2143 | * In case of error an ERR_PTR() is returned. |
| 2144 | */ |
| 2145 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, |
| 2146 | const char *propname) |
| 2147 | { |
| 2148 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
| 2149 | bool active_low = false; |
| 2150 | int ret; |
| 2151 | |
| 2152 | if (!fwnode) |
| 2153 | return ERR_PTR(-EINVAL); |
| 2154 | |
| 2155 | if (is_of_node(fwnode)) { |
| 2156 | enum of_gpio_flags flags; |
| 2157 | |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 2158 | desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0, |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 2159 | &flags); |
| 2160 | if (!IS_ERR(desc)) |
| 2161 | active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 2162 | } else if (is_acpi_node(fwnode)) { |
| 2163 | struct acpi_gpio_info info; |
| 2164 | |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 2165 | desc = acpi_get_gpiod_by_index(to_acpi_node(fwnode), propname, 0, |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 2166 | &info); |
| 2167 | if (!IS_ERR(desc)) |
| 2168 | active_low = info.active_low; |
| 2169 | } |
| 2170 | |
| 2171 | if (IS_ERR(desc)) |
| 2172 | return desc; |
| 2173 | |
| 2174 | ret = gpiod_request(desc, NULL); |
| 2175 | if (ret) |
| 2176 | return ERR_PTR(ret); |
| 2177 | |
| 2178 | /* Only value flag can be set from both DT and ACPI is active_low */ |
| 2179 | if (active_low) |
| 2180 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 2181 | |
| 2182 | return desc; |
| 2183 | } |
| 2184 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); |
| 2185 | |
| 2186 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2187 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO |
| 2188 | * function |
| 2189 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2190 | * @con_id: function within the GPIO consumer |
| 2191 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2192 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2193 | * |
| 2194 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the |
| 2195 | * specified index was assigned to the requested function it will return NULL. |
| 2196 | * This is convenient for drivers that need to handle optional GPIOs. |
| 2197 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2198 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2199 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2200 | unsigned int index, |
| 2201 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2202 | { |
| 2203 | struct gpio_desc *desc; |
| 2204 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 2205 | desc = gpiod_get_index(dev, con_id, index, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2206 | if (IS_ERR(desc)) { |
| 2207 | if (PTR_ERR(desc) == -ENOENT) |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | |
| 2211 | return desc; |
| 2212 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 2213 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 2214 | |
| 2215 | /** |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2216 | * gpiod_hog - Hog the specified GPIO desc given the provided flags |
| 2217 | * @desc: gpio whose value will be assigned |
| 2218 | * @name: gpio line name |
| 2219 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
| 2220 | * of_get_gpio_hog() |
| 2221 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 2222 | */ |
| 2223 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
| 2224 | unsigned long lflags, enum gpiod_flags dflags) |
| 2225 | { |
| 2226 | struct gpio_chip *chip; |
| 2227 | struct gpio_desc *local_desc; |
| 2228 | int hwnum; |
| 2229 | int status; |
| 2230 | |
| 2231 | chip = gpiod_to_chip(desc); |
| 2232 | hwnum = gpio_chip_hwgpio(desc); |
| 2233 | |
| 2234 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); |
| 2235 | if (IS_ERR(local_desc)) { |
Linus Walleij | a713890 | 2015-06-05 11:36:10 +0200 | [diff] [blame] | 2236 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n", |
| 2237 | name, chip->label, hwnum); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2238 | return PTR_ERR(local_desc); |
| 2239 | } |
| 2240 | |
| 2241 | status = gpiod_configure_flags(desc, name, lflags, dflags); |
| 2242 | if (status < 0) { |
Linus Walleij | a713890 | 2015-06-05 11:36:10 +0200 | [diff] [blame] | 2243 | pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n", |
| 2244 | name, chip->label, hwnum); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2245 | gpiochip_free_own_desc(desc); |
| 2246 | return status; |
| 2247 | } |
| 2248 | |
| 2249 | /* Mark GPIO as hogged so it can be identified and removed later */ |
| 2250 | set_bit(FLAG_IS_HOGGED, &desc->flags); |
| 2251 | |
| 2252 | pr_info("GPIO line %d (%s) hogged as %s%s\n", |
| 2253 | desc_to_gpio(desc), name, |
| 2254 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
| 2255 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? |
| 2256 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); |
| 2257 | |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
| 2261 | /** |
| 2262 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog |
| 2263 | * @chip: gpio chip to act on |
| 2264 | * |
| 2265 | * This is only used by of_gpiochip_remove to free hogged gpios |
| 2266 | */ |
| 2267 | static void gpiochip_free_hogs(struct gpio_chip *chip) |
| 2268 | { |
| 2269 | int id; |
| 2270 | |
| 2271 | for (id = 0; id < chip->ngpio; id++) { |
| 2272 | if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags)) |
| 2273 | gpiochip_free_own_desc(&chip->desc[id]); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | /** |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 2278 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function |
| 2279 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2280 | * @con_id: function within the GPIO consumer |
| 2281 | * @flags: optional GPIO initialization flags |
| 2282 | * |
| 2283 | * This function acquires all the GPIOs defined under a given function. |
| 2284 | * |
| 2285 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if |
| 2286 | * no GPIO has been assigned to the requested function, or another IS_ERR() |
| 2287 | * code if an error occurred while trying to acquire the GPIOs. |
| 2288 | */ |
| 2289 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, |
| 2290 | const char *con_id, |
| 2291 | enum gpiod_flags flags) |
| 2292 | { |
| 2293 | struct gpio_desc *desc; |
| 2294 | struct gpio_descs *descs; |
| 2295 | int count; |
| 2296 | |
| 2297 | count = gpiod_count(dev, con_id); |
| 2298 | if (count < 0) |
| 2299 | return ERR_PTR(count); |
| 2300 | |
| 2301 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, |
| 2302 | GFP_KERNEL); |
| 2303 | if (!descs) |
| 2304 | return ERR_PTR(-ENOMEM); |
| 2305 | |
| 2306 | for (descs->ndescs = 0; descs->ndescs < count; ) { |
| 2307 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); |
| 2308 | if (IS_ERR(desc)) { |
| 2309 | gpiod_put_array(descs); |
| 2310 | return ERR_CAST(desc); |
| 2311 | } |
| 2312 | descs->desc[descs->ndescs] = desc; |
| 2313 | descs->ndescs++; |
| 2314 | } |
| 2315 | return descs; |
| 2316 | } |
| 2317 | EXPORT_SYMBOL_GPL(gpiod_get_array); |
| 2318 | |
| 2319 | /** |
| 2320 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO |
| 2321 | * function |
| 2322 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2323 | * @con_id: function within the GPIO consumer |
| 2324 | * @flags: optional GPIO initialization flags |
| 2325 | * |
| 2326 | * This is equivalent to gpiod_get_array(), except that when no GPIO was |
| 2327 | * assigned to the requested function it will return NULL. |
| 2328 | */ |
| 2329 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, |
| 2330 | const char *con_id, |
| 2331 | enum gpiod_flags flags) |
| 2332 | { |
| 2333 | struct gpio_descs *descs; |
| 2334 | |
| 2335 | descs = gpiod_get_array(dev, con_id, flags); |
| 2336 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
| 2337 | return NULL; |
| 2338 | |
| 2339 | return descs; |
| 2340 | } |
| 2341 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); |
| 2342 | |
| 2343 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2344 | * gpiod_put - dispose of a GPIO descriptor |
| 2345 | * @desc: GPIO descriptor to dispose of |
| 2346 | * |
| 2347 | * No descriptor can be used after gpiod_put() has been called on it. |
| 2348 | */ |
| 2349 | void gpiod_put(struct gpio_desc *desc) |
| 2350 | { |
| 2351 | gpiod_free(desc); |
| 2352 | } |
| 2353 | EXPORT_SYMBOL_GPL(gpiod_put); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2354 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 2355 | /** |
| 2356 | * gpiod_put_array - dispose of multiple GPIO descriptors |
| 2357 | * @descs: struct gpio_descs containing an array of descriptors |
| 2358 | */ |
| 2359 | void gpiod_put_array(struct gpio_descs *descs) |
| 2360 | { |
| 2361 | unsigned int i; |
| 2362 | |
| 2363 | for (i = 0; i < descs->ndescs; i++) |
| 2364 | gpiod_put(descs->desc[i]); |
| 2365 | |
| 2366 | kfree(descs); |
| 2367 | } |
| 2368 | EXPORT_SYMBOL_GPL(gpiod_put_array); |
| 2369 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2370 | #ifdef CONFIG_DEBUG_FS |
| 2371 | |
| 2372 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 2373 | { |
| 2374 | unsigned i; |
| 2375 | unsigned gpio = chip->base; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2376 | struct gpio_desc *gdesc = &chip->desc[0]; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2377 | int is_out; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2378 | int is_irq; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2379 | |
| 2380 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame^] | 2381 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
| 2382 | if (gdesc->name) { |
| 2383 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", |
| 2384 | gpio, gdesc->name); |
| 2385 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2386 | continue; |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame^] | 2387 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2388 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2389 | gpiod_get_direction(gdesc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2390 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2391 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame^] | 2392 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s", |
| 2393 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2394 | is_out ? "out" : "in ", |
| 2395 | chip->get |
| 2396 | ? (chip->get(chip, i) ? "hi" : "lo") |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2397 | : "? ", |
| 2398 | is_irq ? "IRQ" : " "); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2399 | seq_printf(s, "\n"); |
| 2400 | } |
| 2401 | } |
| 2402 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2403 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2404 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2405 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2406 | struct gpio_chip *chip = NULL; |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2407 | loff_t index = *pos; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2408 | |
| 2409 | s->private = ""; |
| 2410 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2411 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2412 | list_for_each_entry(chip, &gpio_chips, list) |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2413 | if (index-- == 0) { |
| 2414 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2415 | return chip; |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2416 | } |
| 2417 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2418 | |
| 2419 | return NULL; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2423 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2424 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2425 | struct gpio_chip *chip = v; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2426 | void *ret = NULL; |
| 2427 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2428 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2429 | if (list_is_last(&chip->list, &gpio_chips)) |
| 2430 | ret = NULL; |
| 2431 | else |
| 2432 | ret = list_entry(chip->list.next, struct gpio_chip, list); |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2433 | spin_unlock_irqrestore(&gpio_lock, flags); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2434 | |
| 2435 | s->private = "\n"; |
| 2436 | ++*pos; |
| 2437 | |
| 2438 | return ret; |
| 2439 | } |
| 2440 | |
| 2441 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 2442 | { |
| 2443 | } |
| 2444 | |
| 2445 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 2446 | { |
| 2447 | struct gpio_chip *chip = v; |
| 2448 | struct device *dev; |
| 2449 | |
| 2450 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
| 2451 | chip->base, chip->base + chip->ngpio - 1); |
| 2452 | dev = chip->dev; |
| 2453 | if (dev) |
| 2454 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
| 2455 | dev_name(dev)); |
| 2456 | if (chip->label) |
| 2457 | seq_printf(s, ", %s", chip->label); |
| 2458 | if (chip->can_sleep) |
| 2459 | seq_printf(s, ", can sleep"); |
| 2460 | seq_printf(s, ":\n"); |
| 2461 | |
| 2462 | if (chip->dbg_show) |
| 2463 | chip->dbg_show(s, chip); |
| 2464 | else |
| 2465 | gpiolib_dbg_show(s, chip); |
| 2466 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2467 | return 0; |
| 2468 | } |
| 2469 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2470 | static const struct seq_operations gpiolib_seq_ops = { |
| 2471 | .start = gpiolib_seq_start, |
| 2472 | .next = gpiolib_seq_next, |
| 2473 | .stop = gpiolib_seq_stop, |
| 2474 | .show = gpiolib_seq_show, |
| 2475 | }; |
| 2476 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2477 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 2478 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2479 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2480 | } |
| 2481 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2482 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2483 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2484 | .open = gpiolib_open, |
| 2485 | .read = seq_read, |
| 2486 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2487 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2488 | }; |
| 2489 | |
| 2490 | static int __init gpiolib_debugfs_init(void) |
| 2491 | { |
| 2492 | /* /sys/kernel/debug/gpio */ |
| 2493 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 2494 | NULL, NULL, &gpiolib_operations); |
| 2495 | return 0; |
| 2496 | } |
| 2497 | subsys_initcall(gpiolib_debugfs_init); |
| 2498 | |
| 2499 | #endif /* DEBUG_FS */ |