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