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