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