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> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 18 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 19 | #include "gpiolib.h" |
| 20 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 21 | #define CREATE_TRACE_POINTS |
| 22 | #include <trace/events/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 23 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 24 | /* Implementation infrastructure for GPIO interfaces. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 25 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 26 | * The GPIO programming interface allows for inlining speed-critical |
| 27 | * get/set operations for common cases, so that access to SOC-integrated |
| 28 | * GPIOs can sometimes cost only an instruction or two per bit. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | |
| 32 | /* When debugging, extend minimal trust to callers and platform code. |
| 33 | * Also emit diagnostic messages that may help initial bringup, when |
| 34 | * board setup or driver bugs are most common. |
| 35 | * |
| 36 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 37 | */ |
| 38 | #ifdef DEBUG |
| 39 | #define extra_checks 1 |
| 40 | #else |
| 41 | #define extra_checks 0 |
| 42 | #endif |
| 43 | |
| 44 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 45 | * While any GPIO is requested, its gpio_chip is not removable; |
| 46 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 47 | */ |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 48 | DEFINE_SPINLOCK(gpio_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 49 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 50 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
| 51 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 52 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
| 53 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 54 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 55 | static LIST_HEAD(gpio_lookup_list); |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 56 | LIST_HEAD(gpio_chips); |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 57 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 58 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 59 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 60 | d->label = label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 63 | /** |
| 64 | * Convert a GPIO number to its descriptor |
| 65 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 66 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 67 | { |
| 68 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) |
| 69 | return NULL; |
| 70 | else |
| 71 | return &gpio_desc[gpio]; |
| 72 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 74 | |
| 75 | /** |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 76 | * 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] | 77 | */ |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 78 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
| 79 | u16 hwnum) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 80 | { |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 81 | if (hwnum >= chip->ngpio) |
Alexandre Courbot | b7d0a28 | 2013-12-03 12:31:11 +0900 | [diff] [blame] | 82 | return ERR_PTR(-EINVAL); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 83 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 84 | return &chip->desc[hwnum]; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 85 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Convert a GPIO descriptor to the integer namespace. |
| 89 | * This should disappear in the future but is needed since we still |
| 90 | * use GPIO numbers for error messages and sysfs nodes |
| 91 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 92 | int desc_to_gpio(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 93 | { |
Alexandre Courbot | 8c0fca8 | 2013-10-04 10:59:57 -0700 | [diff] [blame] | 94 | return desc - &gpio_desc[0]; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 95 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 97 | |
| 98 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 99 | /** |
| 100 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 101 | * @desc: descriptor to return the chip of |
| 102 | */ |
| 103 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 104 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 105 | return desc ? desc->chip : NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 106 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 107 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 108 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 109 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 110 | static int gpiochip_find_base(int ngpio) |
| 111 | { |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 112 | struct gpio_chip *chip; |
| 113 | int base = ARCH_NR_GPIOS - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 114 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 115 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
| 116 | /* found a free space? */ |
| 117 | if (chip->base + chip->ngpio <= base) |
| 118 | break; |
| 119 | else |
| 120 | /* nope, check the space right before the chip */ |
| 121 | base = chip->base - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 124 | if (gpio_is_valid(base)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 125 | pr_debug("%s: found new base at %d\n", __func__, base); |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 126 | return base; |
| 127 | } else { |
| 128 | pr_err("%s: cannot find free range\n", __func__); |
| 129 | return -ENOSPC; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 130 | } |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 133 | /** |
| 134 | * gpiod_get_direction - return the current direction of a GPIO |
| 135 | * @desc: GPIO to get the direction of |
| 136 | * |
| 137 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. |
| 138 | * |
| 139 | * This function may sleep if gpiod_cansleep() is true. |
| 140 | */ |
| 141 | int gpiod_get_direction(const struct gpio_desc *desc) |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 142 | { |
| 143 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 144 | unsigned offset; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 145 | int status = -EINVAL; |
| 146 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 147 | chip = gpiod_to_chip(desc); |
| 148 | offset = gpio_chip_hwgpio(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 149 | |
| 150 | if (!chip->get_direction) |
| 151 | return status; |
| 152 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 153 | status = chip->get_direction(chip, offset); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 154 | if (status > 0) { |
| 155 | /* GPIOF_DIR_IN, or other positive */ |
| 156 | status = 1; |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 157 | /* FLAG_IS_OUT is just a cache of the result of get_direction(), |
| 158 | * so it does not affect constness per se */ |
| 159 | clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 160 | } |
| 161 | if (status == 0) { |
| 162 | /* GPIOF_DIR_OUT */ |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 163 | set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 164 | } |
| 165 | return status; |
| 166 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 168 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 169 | /* |
| 170 | * Add a new chip to the global chips list, keeping the list of chips sorted |
| 171 | * by base order. |
| 172 | * |
| 173 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 174 | * space. |
| 175 | */ |
| 176 | static int gpiochip_add_to_list(struct gpio_chip *chip) |
| 177 | { |
| 178 | struct list_head *pos = &gpio_chips; |
| 179 | struct gpio_chip *_chip; |
| 180 | int err = 0; |
| 181 | |
| 182 | /* find where to insert our chip */ |
| 183 | list_for_each(pos, &gpio_chips) { |
| 184 | _chip = list_entry(pos, struct gpio_chip, list); |
| 185 | /* shall we insert before _chip? */ |
| 186 | if (_chip->base >= chip->base + chip->ngpio) |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | /* are we stepping on the chip right before? */ |
| 191 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { |
| 192 | _chip = list_entry(pos->prev, struct gpio_chip, list); |
| 193 | if (_chip->base + _chip->ngpio > chip->base) { |
| 194 | dev_err(chip->dev, |
| 195 | "GPIO integer space overlap, cannot add chip\n"); |
| 196 | err = -EBUSY; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (!err) |
| 201 | list_add_tail(&chip->list, pos); |
| 202 | |
| 203 | return err; |
| 204 | } |
| 205 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 206 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 207 | * gpiochip_add() - register a gpio_chip |
| 208 | * @chip: the chip to register, with chip->base initialized |
| 209 | * Context: potentially before irqs or kmalloc will work |
| 210 | * |
| 211 | * Returns a negative errno if the chip can't be registered, such as |
| 212 | * because the chip->base is invalid or already associated with a |
| 213 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 214 | * |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 215 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 216 | * can be freely used, the chip->dev device must be registered before |
| 217 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 218 | * for GPIOs will fail rudely. |
| 219 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 220 | * If chip->base is negative, this requests dynamic assignment of |
| 221 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 222 | */ |
| 223 | int gpiochip_add(struct gpio_chip *chip) |
| 224 | { |
| 225 | unsigned long flags; |
| 226 | int status = 0; |
| 227 | unsigned id; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 228 | int base = chip->base; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 229 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 230 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 231 | && base >= 0) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 232 | status = -EINVAL; |
| 233 | goto fail; |
| 234 | } |
| 235 | |
| 236 | spin_lock_irqsave(&gpio_lock, flags); |
| 237 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 238 | if (base < 0) { |
| 239 | base = gpiochip_find_base(chip->ngpio); |
| 240 | if (base < 0) { |
| 241 | status = base; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 242 | goto unlock; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 243 | } |
| 244 | chip->base = base; |
| 245 | } |
| 246 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 247 | status = gpiochip_add_to_list(chip); |
| 248 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 249 | if (status == 0) { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 250 | chip->desc = &gpio_desc[chip->base]; |
| 251 | |
| 252 | for (id = 0; id < chip->ngpio; id++) { |
| 253 | struct gpio_desc *desc = &chip->desc[id]; |
| 254 | desc->chip = chip; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 255 | |
| 256 | /* REVISIT: most hardware initializes GPIOs as |
| 257 | * inputs (often with pullups enabled) so power |
| 258 | * usage is minimized. Linux code should set the |
| 259 | * gpio direction first thing; but until it does, |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 260 | * and in case chip->get_direction is not set, |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 261 | * we may expose the wrong direction in sysfs. |
| 262 | */ |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 263 | desc->flags = !chip->direction_input |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 264 | ? (1 << FLAG_IS_OUT) |
| 265 | : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 269 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 270 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 271 | #ifdef CONFIG_PINCTRL |
| 272 | INIT_LIST_HEAD(&chip->pin_ranges); |
| 273 | #endif |
| 274 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 275 | of_gpiochip_add(chip); |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 276 | acpi_gpiochip_add(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 277 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 278 | if (status) |
| 279 | goto fail; |
| 280 | |
| 281 | status = gpiochip_export(chip); |
| 282 | if (status) |
| 283 | goto fail; |
| 284 | |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 285 | 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] | 286 | chip->base, chip->base + chip->ngpio - 1, |
| 287 | chip->label ? : "generic"); |
| 288 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 289 | return 0; |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 290 | |
| 291 | unlock: |
| 292 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 293 | fail: |
| 294 | /* failures here can mean systems won't boot... */ |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 295 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 296 | chip->base, chip->base + chip->ngpio - 1, |
| 297 | chip->label ? : "generic"); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 298 | return status; |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 301 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 302 | /* Forward-declaration */ |
| 303 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
| 304 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 305 | /** |
| 306 | * gpiochip_remove() - unregister a gpio_chip |
| 307 | * @chip: the chip to unregister |
| 308 | * |
| 309 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 310 | */ |
| 311 | int gpiochip_remove(struct gpio_chip *chip) |
| 312 | { |
| 313 | unsigned long flags; |
| 314 | int status = 0; |
| 315 | unsigned id; |
| 316 | |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 317 | acpi_gpiochip_remove(chip); |
| 318 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 319 | spin_lock_irqsave(&gpio_lock, flags); |
| 320 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 321 | gpiochip_irqchip_remove(chip); |
Linus Walleij | 9ef0d6f | 2012-11-06 15:15:44 +0100 | [diff] [blame] | 322 | gpiochip_remove_pin_ranges(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 323 | of_gpiochip_remove(chip); |
| 324 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 325 | for (id = 0; id < chip->ngpio; id++) { |
| 326 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 327 | status = -EBUSY; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | if (status == 0) { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 332 | for (id = 0; id < chip->ngpio; id++) |
| 333 | chip->desc[id].chip = NULL; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 334 | |
| 335 | list_del(&chip->list); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 339 | |
| 340 | if (status == 0) |
| 341 | gpiochip_unexport(chip); |
| 342 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 343 | return status; |
| 344 | } |
| 345 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 346 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 347 | /** |
| 348 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 349 | * @data: data to pass to match function |
| 350 | * @callback: Callback function to check gpio_chip |
| 351 | * |
| 352 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 353 | * determined by a user supplied @match callback. The callback should return |
| 354 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 355 | * non-zero, this function will return to the caller and not iterate over any |
| 356 | * more gpio_chips. |
| 357 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 358 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 359 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 360 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 361 | { |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 362 | struct gpio_chip *chip; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 363 | unsigned long flags; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 364 | |
| 365 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 366 | list_for_each_entry(chip, &gpio_chips, list) |
| 367 | if (match(chip, data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 368 | break; |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 369 | |
| 370 | /* No match? */ |
| 371 | if (&chip->list == &gpio_chips) |
| 372 | chip = NULL; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 373 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 374 | |
| 375 | return chip; |
| 376 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 377 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 378 | |
Alexandre Courbot | 79697ef | 2013-11-16 21:39:32 +0900 | [diff] [blame] | 379 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
| 380 | { |
| 381 | const char *name = data; |
| 382 | |
| 383 | return !strcmp(chip->label, name); |
| 384 | } |
| 385 | |
| 386 | static struct gpio_chip *find_chip_by_name(const char *name) |
| 387 | { |
| 388 | return gpiochip_find((void *)name, gpiochip_match_name); |
| 389 | } |
| 390 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 391 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 392 | |
| 393 | /* |
| 394 | * The following is irqchip helper code for gpiochips. |
| 395 | */ |
| 396 | |
| 397 | /** |
| 398 | * gpiochip_add_chained_irqchip() - adds a chained irqchip to a gpiochip |
| 399 | * @gpiochip: the gpiochip to add the irqchip to |
| 400 | * @irqchip: the irqchip to add to the gpiochip |
| 401 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 402 | * chained irqchip |
| 403 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
| 404 | * coming out of the gpiochip |
| 405 | */ |
| 406 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 407 | struct irq_chip *irqchip, |
| 408 | int parent_irq, |
| 409 | irq_flow_handler_t parent_handler) |
| 410 | { |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 411 | if (gpiochip->can_sleep) { |
| 412 | chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n"); |
| 413 | return; |
| 414 | } |
| 415 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 416 | irq_set_chained_handler(parent_irq, parent_handler); |
| 417 | /* |
| 418 | * The parent irqchip is already using the chip_data for this |
| 419 | * irqchip, so our callbacks simply use the handler_data. |
| 420 | */ |
| 421 | irq_set_handler_data(parent_irq, gpiochip); |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
| 424 | |
Linus Walleij | e45d1c8 | 2014-04-22 14:01:46 +0200 | [diff] [blame] | 425 | /* |
| 426 | * This lock class tells lockdep that GPIO irqs are in a different |
| 427 | * category than their parents, so it won't report false recursion. |
| 428 | */ |
| 429 | static struct lock_class_key gpiochip_irq_lock_class; |
| 430 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 431 | /** |
| 432 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip |
| 433 | * @d: the irqdomain used by this irqchip |
| 434 | * @irq: the global irq number used by this GPIO irqchip irq |
| 435 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip |
| 436 | * |
| 437 | * This function will set up the mapping for a certain IRQ line on a |
| 438 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip |
| 439 | * stored inside the gpiochip. |
| 440 | */ |
| 441 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
| 442 | irq_hw_number_t hwirq) |
| 443 | { |
| 444 | struct gpio_chip *chip = d->host_data; |
| 445 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 446 | irq_set_chip_data(irq, chip); |
Linus Walleij | e45d1c8 | 2014-04-22 14:01:46 +0200 | [diff] [blame] | 447 | irq_set_lockdep_class(irq, &gpiochip_irq_lock_class); |
Linus Walleij | 7633fb9 | 2014-04-09 13:20:38 +0200 | [diff] [blame] | 448 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 449 | /* Chips that can sleep need nested thread handlers */ |
| 450 | if (chip->can_sleep) |
| 451 | irq_set_nested_thread(irq, 1); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 452 | #ifdef CONFIG_ARM |
| 453 | set_irq_flags(irq, IRQF_VALID); |
| 454 | #else |
| 455 | irq_set_noprobe(irq); |
| 456 | #endif |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 457 | /* |
| 458 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 459 | * is passed as default type. |
| 460 | */ |
| 461 | if (chip->irq_default_type != IRQ_TYPE_NONE) |
| 462 | irq_set_irq_type(irq, chip->irq_default_type); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 467 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
| 468 | { |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 469 | struct gpio_chip *chip = d->host_data; |
| 470 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 471 | #ifdef CONFIG_ARM |
| 472 | set_irq_flags(irq, 0); |
| 473 | #endif |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 474 | if (chip->can_sleep) |
| 475 | irq_set_nested_thread(irq, 0); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 476 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 477 | irq_set_chip_data(irq, NULL); |
| 478 | } |
| 479 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 480 | static const struct irq_domain_ops gpiochip_domain_ops = { |
| 481 | .map = gpiochip_irq_map, |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 482 | .unmap = gpiochip_irq_unmap, |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 483 | /* Virtually all GPIO irqchips are twocell:ed */ |
| 484 | .xlate = irq_domain_xlate_twocell, |
| 485 | }; |
| 486 | |
| 487 | static int gpiochip_irq_reqres(struct irq_data *d) |
| 488 | { |
| 489 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 490 | |
| 491 | if (gpio_lock_as_irq(chip, d->hwirq)) { |
| 492 | chip_err(chip, |
| 493 | "unable to lock HW IRQ %lu for IRQ\n", |
| 494 | d->hwirq); |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static void gpiochip_irq_relres(struct irq_data *d) |
| 501 | { |
| 502 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 503 | |
| 504 | gpio_unlock_as_irq(chip, d->hwirq); |
| 505 | } |
| 506 | |
| 507 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
| 508 | { |
| 509 | return irq_find_mapping(chip->irqdomain, offset); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip |
| 514 | * @gpiochip: the gpiochip to remove the irqchip from |
| 515 | * |
| 516 | * This is called only from gpiochip_remove() |
| 517 | */ |
| 518 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) |
| 519 | { |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 520 | unsigned int offset; |
| 521 | |
| 522 | /* Remove all IRQ mappings and delete the domain */ |
| 523 | if (gpiochip->irqdomain) { |
| 524 | for (offset = 0; offset < gpiochip->ngpio; offset++) |
| 525 | irq_dispose_mapping(gpiochip->irq_base + offset); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 526 | irq_domain_remove(gpiochip->irqdomain); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 527 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 528 | |
| 529 | if (gpiochip->irqchip) { |
| 530 | gpiochip->irqchip->irq_request_resources = NULL; |
| 531 | gpiochip->irqchip->irq_release_resources = NULL; |
| 532 | gpiochip->irqchip = NULL; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * gpiochip_irqchip_add() - adds an irqchip to a gpiochip |
| 538 | * @gpiochip: the gpiochip to add the irqchip to |
| 539 | * @irqchip: the irqchip to add to the gpiochip |
| 540 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 541 | * allocate gpiochip irqs from |
| 542 | * @handler: the irq handler to use (often a predefined irq core function) |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 543 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 544 | * to have the core avoid setting up any default type in the hardware. |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 545 | * |
| 546 | * This function closely associates a certain irqchip with a certain |
| 547 | * gpiochip, providing an irq domain to translate the local IRQs to |
| 548 | * global irqs in the gpiolib core, and making sure that the gpiochip |
| 549 | * is passed as chip data to all related functions. Driver callbacks |
| 550 | * need to use container_of() to get their local state containers back |
| 551 | * from the gpiochip passed as chip data. An irqdomain will be stored |
| 552 | * in the gpiochip that shall be used by the driver to handle IRQ number |
| 553 | * translation. The gpiochip will need to be initialized and registered |
| 554 | * before calling this function. |
| 555 | * |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 556 | * This function will handle two cell:ed simple IRQs and assumes all |
| 557 | * the pins on the gpiochip can generate a unique IRQ. Everything else |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 558 | * need to be open coded. |
| 559 | */ |
| 560 | int gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
| 561 | struct irq_chip *irqchip, |
| 562 | unsigned int first_irq, |
| 563 | irq_flow_handler_t handler, |
| 564 | unsigned int type) |
| 565 | { |
| 566 | struct device_node *of_node; |
| 567 | unsigned int offset; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 568 | unsigned irq_base = 0; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 569 | |
| 570 | if (!gpiochip || !irqchip) |
| 571 | return -EINVAL; |
| 572 | |
| 573 | if (!gpiochip->dev) { |
| 574 | pr_err("missing gpiochip .dev parent pointer\n"); |
| 575 | return -EINVAL; |
| 576 | } |
| 577 | of_node = gpiochip->dev->of_node; |
| 578 | #ifdef CONFIG_OF_GPIO |
| 579 | /* |
| 580 | * If the gpiochip has an assigned OF node this takes precendence |
| 581 | * FIXME: get rid of this and use gpiochip->dev->of_node everywhere |
| 582 | */ |
| 583 | if (gpiochip->of_node) |
| 584 | of_node = gpiochip->of_node; |
| 585 | #endif |
| 586 | gpiochip->irqchip = irqchip; |
| 587 | gpiochip->irq_handler = handler; |
| 588 | gpiochip->irq_default_type = type; |
| 589 | gpiochip->to_irq = gpiochip_to_irq; |
| 590 | gpiochip->irqdomain = irq_domain_add_simple(of_node, |
| 591 | gpiochip->ngpio, first_irq, |
| 592 | &gpiochip_domain_ops, gpiochip); |
| 593 | if (!gpiochip->irqdomain) { |
| 594 | gpiochip->irqchip = NULL; |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | irqchip->irq_request_resources = gpiochip_irq_reqres; |
| 598 | irqchip->irq_release_resources = gpiochip_irq_relres; |
| 599 | |
| 600 | /* |
| 601 | * Prepare the mapping since the irqchip shall be orthogonal to |
| 602 | * any gpiochip calls. If the first_irq was zero, this is |
| 603 | * necessary to allocate descriptors for all IRQs. |
| 604 | */ |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 605 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
| 606 | irq_base = irq_create_mapping(gpiochip->irqdomain, offset); |
| 607 | if (offset == 0) |
| 608 | /* |
| 609 | * Store the base into the gpiochip to be used when |
| 610 | * unmapping the irqs. |
| 611 | */ |
| 612 | gpiochip->irq_base = irq_base; |
| 613 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_add); |
| 618 | |
| 619 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
| 620 | |
| 621 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
| 622 | |
| 623 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
| 624 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 625 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 626 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 627 | /** |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 628 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 629 | * @chip: the gpiochip to add the range for |
| 630 | * @pinctrl: the dev_name() of the pin controller to map to |
| 631 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 632 | * @pin_group: name of the pin group inside the pin controller |
| 633 | */ |
| 634 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 635 | struct pinctrl_dev *pctldev, |
| 636 | unsigned int gpio_offset, const char *pin_group) |
| 637 | { |
| 638 | struct gpio_pin_range *pin_range; |
| 639 | int ret; |
| 640 | |
| 641 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 642 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 643 | chip_err(chip, "failed to allocate pin ranges\n"); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 644 | return -ENOMEM; |
| 645 | } |
| 646 | |
| 647 | /* Use local offset as range ID */ |
| 648 | pin_range->range.id = gpio_offset; |
| 649 | pin_range->range.gc = chip; |
| 650 | pin_range->range.name = chip->label; |
| 651 | pin_range->range.base = chip->base + gpio_offset; |
| 652 | pin_range->pctldev = pctldev; |
| 653 | |
| 654 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 655 | &pin_range->range.pins, |
| 656 | &pin_range->range.npins); |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 657 | if (ret < 0) { |
| 658 | kfree(pin_range); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 659 | return ret; |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 660 | } |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 661 | |
| 662 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 663 | |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 664 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 665 | gpio_offset, gpio_offset + pin_range->range.npins - 1, |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 666 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 667 | |
| 668 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 673 | |
| 674 | /** |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 675 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 676 | * @chip: the gpiochip to add the range for |
| 677 | * @pinctrl_name: the dev_name() of the pin controller to map to |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 678 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 679 | * @pin_offset: the start offset in the pin controller number space |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 680 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 681 | * pin controller) to accumulate in this range |
| 682 | */ |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 683 | 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] | 684 | unsigned int gpio_offset, unsigned int pin_offset, |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 685 | unsigned int npins) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 686 | { |
| 687 | struct gpio_pin_range *pin_range; |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 688 | int ret; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 689 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 690 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 691 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 692 | chip_err(chip, "failed to allocate pin ranges\n"); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 693 | return -ENOMEM; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 694 | } |
| 695 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 696 | /* Use local offset as range ID */ |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 697 | pin_range->range.id = gpio_offset; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 698 | pin_range->range.gc = chip; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 699 | pin_range->range.name = chip->label; |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 700 | pin_range->range.base = chip->base + gpio_offset; |
| 701 | pin_range->range.pin_base = pin_offset; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 702 | pin_range->range.npins = npins; |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 703 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 704 | &pin_range->range); |
Linus Walleij | 8f23ca1 | 2012-11-20 14:56:25 +0100 | [diff] [blame] | 705 | if (IS_ERR(pin_range->pctldev)) { |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 706 | ret = PTR_ERR(pin_range->pctldev); |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 707 | chip_err(chip, "could not create pin range\n"); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 708 | kfree(pin_range); |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 709 | return ret; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 710 | } |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 711 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 712 | gpio_offset, gpio_offset + npins - 1, |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 713 | pinctl_name, |
| 714 | pin_offset, pin_offset + npins - 1); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 715 | |
| 716 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 717 | |
| 718 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 719 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 720 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 721 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 722 | /** |
| 723 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 724 | * @chip: the chip to remove all the mappings for |
| 725 | */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 726 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 727 | { |
| 728 | struct gpio_pin_range *pin_range, *tmp; |
| 729 | |
| 730 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
| 731 | list_del(&pin_range->node); |
| 732 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 733 | &pin_range->range); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 734 | kfree(pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 735 | } |
| 736 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 737 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 738 | |
| 739 | #endif /* CONFIG_PINCTRL */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 740 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 741 | /* These "optional" allocation calls help prevent drivers from stomping |
| 742 | * on each other, and help provide better diagnostics in debugfs. |
| 743 | * They're called even less than the "set direction" calls. |
| 744 | */ |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 745 | static int __gpiod_request(struct gpio_desc *desc, const char *label) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 746 | { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 747 | struct gpio_chip *chip = desc->chip; |
| 748 | int status; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 749 | unsigned long flags; |
| 750 | |
| 751 | spin_lock_irqsave(&gpio_lock, flags); |
| 752 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 753 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 754 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 755 | */ |
| 756 | |
| 757 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 758 | desc_set_label(desc, label ? : "?"); |
| 759 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 760 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 761 | status = -EBUSY; |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 762 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | if (chip->request) { |
| 766 | /* chip->request may sleep */ |
| 767 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 768 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 769 | spin_lock_irqsave(&gpio_lock, flags); |
| 770 | |
| 771 | if (status < 0) { |
| 772 | desc_set_label(desc, NULL); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 773 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 774 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 775 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 776 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 777 | if (chip->get_direction) { |
| 778 | /* chip->get_direction may sleep */ |
| 779 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 780 | gpiod_get_direction(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 781 | spin_lock_irqsave(&gpio_lock, flags); |
| 782 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 783 | done: |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 784 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 785 | return status; |
| 786 | } |
| 787 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 788 | int gpiod_request(struct gpio_desc *desc, const char *label) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 789 | { |
| 790 | int status = -EPROBE_DEFER; |
| 791 | struct gpio_chip *chip; |
| 792 | |
| 793 | if (!desc) { |
| 794 | pr_warn("%s: invalid GPIO\n", __func__); |
| 795 | return -EINVAL; |
| 796 | } |
| 797 | |
| 798 | chip = desc->chip; |
| 799 | if (!chip) |
| 800 | goto done; |
| 801 | |
| 802 | if (try_module_get(chip->owner)) { |
| 803 | status = __gpiod_request(desc, label); |
| 804 | if (status < 0) |
| 805 | module_put(chip->owner); |
| 806 | } |
| 807 | |
| 808 | done: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 809 | if (status) |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 810 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 811 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 812 | return status; |
| 813 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 814 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 815 | static bool __gpiod_free(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 816 | { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 817 | bool ret = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 818 | unsigned long flags; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 819 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 820 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 821 | might_sleep(); |
| 822 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 823 | gpiod_unexport(desc); |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 824 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 825 | spin_lock_irqsave(&gpio_lock, flags); |
| 826 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 827 | chip = desc->chip; |
| 828 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 829 | if (chip->free) { |
| 830 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 831 | might_sleep_if(chip->can_sleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 832 | chip->free(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 833 | spin_lock_irqsave(&gpio_lock, flags); |
| 834 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 835 | desc_set_label(desc, NULL); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 836 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 837 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 838 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 839 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 840 | ret = true; |
| 841 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 842 | |
| 843 | spin_unlock_irqrestore(&gpio_lock, flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 844 | return ret; |
| 845 | } |
| 846 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 847 | void gpiod_free(struct gpio_desc *desc) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 848 | { |
| 849 | if (desc && __gpiod_free(desc)) |
| 850 | module_put(desc->chip->owner); |
| 851 | else |
| 852 | WARN_ON(extra_checks); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 853 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 854 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 855 | /** |
| 856 | * gpiochip_is_requested - return string iff signal was requested |
| 857 | * @chip: controller managing the signal |
| 858 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 859 | * |
| 860 | * Returns NULL if the GPIO is not currently requested, else a string. |
Alexandre Courbot | 9c8318f | 2014-07-01 14:45:14 +0900 | [diff] [blame] | 861 | * The string returned is the label passed to gpio_request(); if none has been |
| 862 | * passed it is a meaningless, non-NULL constant. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 863 | * |
| 864 | * This function is for use by GPIO controller drivers. The label can |
| 865 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 866 | * can help avoid accidentally multiplexing it to another controller. |
| 867 | */ |
| 868 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 869 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 870 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 871 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 872 | if (!GPIO_OFFSET_VALID(chip, offset)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 873 | return NULL; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 874 | |
| 875 | desc = &chip->desc[offset]; |
| 876 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 877 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 878 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 879 | return desc->label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 880 | } |
| 881 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 882 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 883 | /** |
| 884 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor |
| 885 | * @desc: GPIO descriptor to request |
| 886 | * @label: label for the GPIO |
| 887 | * |
| 888 | * Function allows GPIO chip drivers to request and use their own GPIO |
| 889 | * descriptors via gpiolib API. Difference to gpiod_request() is that this |
| 890 | * function will not increase reference count of the GPIO chip module. This |
| 891 | * allows the GPIO chip module to be unloaded as needed (we assume that the |
| 892 | * GPIO chip driver handles freeing the GPIOs it has requested). |
| 893 | */ |
| 894 | int gpiochip_request_own_desc(struct gpio_desc *desc, const char *label) |
| 895 | { |
| 896 | if (!desc || !desc->chip) |
| 897 | return -EINVAL; |
| 898 | |
| 899 | return __gpiod_request(desc, label); |
| 900 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 901 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 902 | |
| 903 | /** |
| 904 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver |
| 905 | * @desc: GPIO descriptor to free |
| 906 | * |
| 907 | * Function frees the given GPIO requested previously with |
| 908 | * gpiochip_request_own_desc(). |
| 909 | */ |
| 910 | void gpiochip_free_own_desc(struct gpio_desc *desc) |
| 911 | { |
| 912 | if (desc) |
| 913 | __gpiod_free(desc); |
| 914 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 915 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 916 | |
| 917 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 918 | * some cases this is done in early boot, before IRQs are enabled. |
| 919 | * |
| 920 | * As a rule these aren't called more than once (except for drivers |
| 921 | * using the open-drain emulation idiom) so these are natural places |
| 922 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 923 | * rely on gpio_request() having been called beforehand. |
| 924 | */ |
| 925 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 926 | /** |
| 927 | * gpiod_direction_input - set the GPIO direction to input |
| 928 | * @desc: GPIO to set to input |
| 929 | * |
| 930 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 931 | * be called safely on it. |
| 932 | * |
| 933 | * Return 0 in case of success, else an error code. |
| 934 | */ |
| 935 | int gpiod_direction_input(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 936 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 937 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 938 | int status = -EINVAL; |
| 939 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 940 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 941 | pr_warn("%s: invalid GPIO\n", __func__); |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 945 | chip = desc->chip; |
| 946 | if (!chip->get || !chip->direction_input) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 947 | gpiod_warn(desc, |
| 948 | "%s: missing get() or direction_input() operations\n", |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 949 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 950 | return -EIO; |
| 951 | } |
| 952 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 953 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 954 | if (status == 0) |
| 955 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 956 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 957 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 958 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 959 | return status; |
| 960 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 961 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 962 | |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 963 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 964 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 965 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 966 | int status = -EINVAL; |
| 967 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 968 | /* GPIOs used for IRQs shall not be set as output */ |
| 969 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
| 970 | gpiod_err(desc, |
| 971 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 972 | __func__); |
| 973 | return -EIO; |
| 974 | } |
| 975 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 976 | /* Open drain pin should not be driven to 1 */ |
| 977 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 978 | return gpiod_direction_input(desc); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 979 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 980 | /* Open source pin should not be driven to 0 */ |
| 981 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 982 | return gpiod_direction_input(desc); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 983 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 984 | chip = desc->chip; |
| 985 | if (!chip->set || !chip->direction_output) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 986 | gpiod_warn(desc, |
| 987 | "%s: missing set() or direction_output() operations\n", |
| 988 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 989 | return -EIO; |
| 990 | } |
| 991 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 992 | status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 993 | if (status == 0) |
| 994 | set_bit(FLAG_IS_OUT, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 995 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 996 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 997 | return status; |
| 998 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 999 | |
| 1000 | /** |
| 1001 | * gpiod_direction_output_raw - set the GPIO direction to output |
| 1002 | * @desc: GPIO to set to output |
| 1003 | * @value: initial output value of the GPIO |
| 1004 | * |
| 1005 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1006 | * be called safely on it. The initial value of the output must be specified |
| 1007 | * as raw value on the physical line without regard for the ACTIVE_LOW status. |
| 1008 | * |
| 1009 | * Return 0 in case of success, else an error code. |
| 1010 | */ |
| 1011 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 1012 | { |
| 1013 | if (!desc || !desc->chip) { |
| 1014 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1015 | return -EINVAL; |
| 1016 | } |
| 1017 | return _gpiod_direction_output_raw(desc, value); |
| 1018 | } |
| 1019 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); |
| 1020 | |
| 1021 | /** |
Rahul Bedarkar | 90df4fe | 2014-02-08 11:55:25 +0530 | [diff] [blame] | 1022 | * gpiod_direction_output - set the GPIO direction to output |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 1023 | * @desc: GPIO to set to output |
| 1024 | * @value: initial output value of the GPIO |
| 1025 | * |
| 1026 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1027 | * be called safely on it. The initial value of the output must be specified |
| 1028 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1029 | * account. |
| 1030 | * |
| 1031 | * Return 0 in case of success, else an error code. |
| 1032 | */ |
| 1033 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 1034 | { |
| 1035 | if (!desc || !desc->chip) { |
| 1036 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1037 | return -EINVAL; |
| 1038 | } |
| 1039 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1040 | value = !value; |
| 1041 | return _gpiod_direction_output_raw(desc, value); |
| 1042 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1043 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1044 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1045 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1046 | * gpiod_set_debounce - sets @debounce time for a @gpio |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1047 | * @gpio: the gpio to set debounce time |
| 1048 | * @debounce: debounce time is microseconds |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1049 | * |
| 1050 | * returns -ENOTSUPP if the controller does not support setting |
| 1051 | * debounce. |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1052 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1053 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1054 | { |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1055 | struct gpio_chip *chip; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1056 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1057 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1058 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1059 | return -EINVAL; |
| 1060 | } |
| 1061 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1062 | chip = desc->chip; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1063 | if (!chip->set || !chip->set_debounce) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1064 | gpiod_dbg(desc, |
| 1065 | "%s: missing set() or set_debounce() operations\n", |
| 1066 | __func__); |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1067 | return -ENOTSUPP; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 1070 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1071 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1072 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1073 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1074 | /** |
| 1075 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 1076 | * @desc: the gpio descriptor to test |
| 1077 | * |
| 1078 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 1079 | */ |
| 1080 | int gpiod_is_active_low(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1081 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1082 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1083 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1084 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1085 | |
| 1086 | /* I/O calls are only valid after configuration completed; the relevant |
| 1087 | * "is this a valid GPIO" error checks should already have been done. |
| 1088 | * |
| 1089 | * "Get" operations are often inlinable as reading a pin value register, |
| 1090 | * and masking the relevant bit in that register. |
| 1091 | * |
| 1092 | * When "set" operations are inlinable, they involve writing that mask to |
| 1093 | * one register to set a low value, or a different register to set it high. |
| 1094 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1095 | * |
| 1096 | *------------------------------------------------------------------------ |
| 1097 | * |
| 1098 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1099 | * have requested the GPIO. That can include implicit requesting by |
| 1100 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1101 | * in memory, guaranteeing that these table lookups need no more locking |
| 1102 | * and that gpiochip_remove() will fail. |
| 1103 | * |
| 1104 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1105 | * that the GPIO was actually requested. |
| 1106 | */ |
| 1107 | |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1108 | static bool _gpiod_get_raw_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1109 | { |
| 1110 | struct gpio_chip *chip; |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1111 | bool value; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1112 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1113 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1114 | chip = desc->chip; |
| 1115 | offset = gpio_chip_hwgpio(desc); |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1116 | value = chip->get ? chip->get(chip, offset) : false; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1117 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1118 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1119 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1120 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1121 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1122 | * gpiod_get_raw_value() - return a gpio's raw value |
| 1123 | * @desc: gpio whose value will be returned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1124 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1125 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 1126 | * its ACTIVE_LOW status. |
| 1127 | * |
| 1128 | * This function should be called from contexts where we cannot sleep, and will |
| 1129 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1130 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1131 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1132 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1133 | if (!desc) |
| 1134 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1135 | /* Should be using gpio_get_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 1136 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1137 | return _gpiod_get_raw_value(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1138 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1139 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1140 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1141 | /** |
| 1142 | * gpiod_get_value() - return a gpio's value |
| 1143 | * @desc: gpio whose value will be returned |
| 1144 | * |
| 1145 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 1146 | * account. |
| 1147 | * |
| 1148 | * This function should be called from contexts where we cannot sleep, and will |
| 1149 | * complain if the GPIO chip functions potentially sleep. |
| 1150 | */ |
| 1151 | int gpiod_get_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1152 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1153 | int value; |
| 1154 | if (!desc) |
| 1155 | return 0; |
| 1156 | /* Should be using gpio_get_value_cansleep() */ |
| 1157 | WARN_ON(desc->chip->can_sleep); |
| 1158 | |
| 1159 | value = _gpiod_get_raw_value(desc); |
| 1160 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1161 | value = !value; |
| 1162 | |
| 1163 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1164 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1165 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1166 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1167 | /* |
| 1168 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1169 | * @desc: gpio descriptor whose state need to be set. |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1170 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1171 | */ |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1172 | 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] | 1173 | { |
| 1174 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1175 | struct gpio_chip *chip = desc->chip; |
| 1176 | int offset = gpio_chip_hwgpio(desc); |
| 1177 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1178 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1179 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1180 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1181 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1182 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1183 | err = chip->direction_output(chip, offset, 0); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1184 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1185 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1186 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1187 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1188 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1189 | gpiod_err(desc, |
| 1190 | "%s: Error in set_value for open drain err %d\n", |
| 1191 | __func__, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1192 | } |
| 1193 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1194 | /* |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1195 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 1196 | * @desc: gpio descriptor whose state need to be set. |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1197 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1198 | */ |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1199 | 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] | 1200 | { |
| 1201 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1202 | struct gpio_chip *chip = desc->chip; |
| 1203 | int offset = gpio_chip_hwgpio(desc); |
| 1204 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1205 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1206 | err = chip->direction_output(chip, offset, 1); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1207 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1208 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1209 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1210 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1211 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1212 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1213 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1214 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1215 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1216 | gpiod_err(desc, |
| 1217 | "%s: Error in set_value for open source err %d\n", |
| 1218 | __func__, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1219 | } |
| 1220 | |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 1221 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1222 | { |
| 1223 | struct gpio_chip *chip; |
| 1224 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1225 | chip = desc->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1226 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1227 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 1228 | _gpio_set_open_drain_value(desc, value); |
| 1229 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 1230 | _gpio_set_open_source_value(desc, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1231 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1232 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
| 1233 | } |
| 1234 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1235 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1236 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 1237 | * @desc: gpio whose value will be assigned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1238 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1239 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1240 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 1241 | * regard for its ACTIVE_LOW status. |
| 1242 | * |
| 1243 | * This function should be called from contexts where we cannot sleep, and will |
| 1244 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1245 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1246 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1247 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1248 | if (!desc) |
| 1249 | return; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1250 | /* Should be using gpio_set_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 1251 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1252 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1253 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1254 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1255 | |
| 1256 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1257 | * gpiod_set_value() - assign a gpio's value |
| 1258 | * @desc: gpio whose value will be assigned |
| 1259 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1260 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1261 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1262 | * account |
| 1263 | * |
| 1264 | * This function should be called from contexts where we cannot sleep, and will |
| 1265 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1266 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1267 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 1268 | { |
| 1269 | if (!desc) |
| 1270 | return; |
| 1271 | /* Should be using gpio_set_value_cansleep() */ |
| 1272 | WARN_ON(desc->chip->can_sleep); |
| 1273 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1274 | value = !value; |
| 1275 | _gpiod_set_raw_value(desc, value); |
| 1276 | } |
| 1277 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 1278 | |
| 1279 | /** |
| 1280 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 1281 | * @desc: gpio to check |
| 1282 | * |
| 1283 | */ |
| 1284 | int gpiod_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1285 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1286 | if (!desc) |
| 1287 | return 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1288 | return desc->chip->can_sleep; |
| 1289 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1290 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1291 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1292 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1293 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 1294 | * @desc: gpio whose IRQ will be returned (already requested) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1295 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1296 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 1297 | * error. |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1298 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1299 | int gpiod_to_irq(const struct gpio_desc *desc) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1300 | { |
| 1301 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1302 | int offset; |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1303 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1304 | if (!desc) |
| 1305 | return -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1306 | chip = desc->chip; |
| 1307 | offset = gpio_chip_hwgpio(desc); |
| 1308 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
| 1309 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1310 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1311 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1312 | /** |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1313 | * gpio_lock_as_irq() - lock a GPIO to be used as IRQ |
| 1314 | * @chip: the chip the GPIO to lock belongs to |
| 1315 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1316 | * |
| 1317 | * This is used directly by GPIO drivers that want to lock down |
Linus Walleij | f438acd | 2014-03-07 10:12:49 +0800 | [diff] [blame] | 1318 | * a certain GPIO line to be used for IRQs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1319 | */ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1320 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1321 | { |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1322 | if (offset >= chip->ngpio) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1323 | return -EINVAL; |
| 1324 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1325 | if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) { |
| 1326 | chip_err(chip, |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1327 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 1328 | __func__); |
| 1329 | return -EIO; |
| 1330 | } |
| 1331 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1332 | set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1333 | return 0; |
| 1334 | } |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1335 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1336 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1337 | /** |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1338 | * gpio_unlock_as_irq() - unlock a GPIO used as IRQ |
| 1339 | * @chip: the chip the GPIO to lock belongs to |
| 1340 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1341 | * |
| 1342 | * This is used directly by GPIO drivers that want to indicate |
| 1343 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 1344 | */ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1345 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1346 | { |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1347 | if (offset >= chip->ngpio) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1348 | return; |
| 1349 | |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1350 | clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1351 | } |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 1352 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1353 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1354 | /** |
| 1355 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 1356 | * @desc: gpio whose value will be returned |
| 1357 | * |
| 1358 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 1359 | * its ACTIVE_LOW status. |
| 1360 | * |
| 1361 | * This function is to be called from contexts that can sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1362 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1363 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1364 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1365 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1366 | if (!desc) |
| 1367 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1368 | return _gpiod_get_raw_value(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1369 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1370 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1371 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1372 | /** |
| 1373 | * gpiod_get_value_cansleep() - return a gpio's value |
| 1374 | * @desc: gpio whose value will be returned |
| 1375 | * |
| 1376 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 1377 | * account. |
| 1378 | * |
| 1379 | * This function is to be called from contexts that can sleep. |
| 1380 | */ |
| 1381 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1382 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1383 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1384 | |
| 1385 | might_sleep_if(extra_checks); |
| 1386 | if (!desc) |
| 1387 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1388 | |
| 1389 | value = _gpiod_get_raw_value(desc); |
| 1390 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1391 | value = !value; |
| 1392 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1393 | return value; |
| 1394 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1395 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1396 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1397 | /** |
| 1398 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 1399 | * @desc: gpio whose value will be assigned |
| 1400 | * @value: value to assign |
| 1401 | * |
| 1402 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 1403 | * regard for its ACTIVE_LOW status. |
| 1404 | * |
| 1405 | * This function is to be called from contexts that can sleep. |
| 1406 | */ |
| 1407 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1408 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1409 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1410 | if (!desc) |
| 1411 | return; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1412 | _gpiod_set_raw_value(desc, value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1413 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1414 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1415 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1416 | /** |
| 1417 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 1418 | * @desc: gpio whose value will be assigned |
| 1419 | * @value: value to assign |
| 1420 | * |
| 1421 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1422 | * account |
| 1423 | * |
| 1424 | * This function is to be called from contexts that can sleep. |
| 1425 | */ |
| 1426 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1427 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1428 | might_sleep_if(extra_checks); |
| 1429 | if (!desc) |
| 1430 | return; |
| 1431 | |
| 1432 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1433 | value = !value; |
| 1434 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1435 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1436 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1437 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1438 | /** |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1439 | * gpiod_add_lookup_table() - register GPIO device consumers |
| 1440 | * @table: table of consumers to register |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1441 | */ |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1442 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1443 | { |
| 1444 | mutex_lock(&gpio_lookup_lock); |
| 1445 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1446 | list_add_tail(&table->list, &gpio_lookup_list); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1447 | |
| 1448 | mutex_unlock(&gpio_lookup_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1451 | 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] | 1452 | unsigned int idx, |
| 1453 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1454 | { |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1455 | static const char *suffixes[] = { "gpios", "gpio" }; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1456 | char prop_name[32]; /* 32 is max size of property name */ |
| 1457 | enum of_gpio_flags of_flags; |
| 1458 | struct gpio_desc *desc; |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1459 | unsigned int i; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1460 | |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1461 | for (i = 0; i < ARRAY_SIZE(suffixes); i++) { |
| 1462 | if (con_id) |
| 1463 | snprintf(prop_name, 32, "%s-%s", con_id, suffixes[i]); |
| 1464 | else |
| 1465 | snprintf(prop_name, 32, "%s", suffixes[i]); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1466 | |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1467 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
| 1468 | &of_flags); |
Tony Lindgren | 06fc3b7 | 2014-06-02 16:13:46 -0700 | [diff] [blame] | 1469 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
Thierry Reding | dd34c37 | 2014-04-23 17:28:09 +0200 | [diff] [blame] | 1470 | break; |
| 1471 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1472 | |
| 1473 | if (IS_ERR(desc)) |
| 1474 | return desc; |
| 1475 | |
| 1476 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1477 | *flags |= GPIO_ACTIVE_LOW; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1478 | |
| 1479 | return desc; |
| 1480 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1481 | |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1482 | 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] | 1483 | unsigned int idx, |
| 1484 | enum gpio_lookup_flags *flags) |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1485 | { |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 1486 | struct acpi_gpio_info info; |
| 1487 | struct gpio_desc *desc; |
| 1488 | |
| 1489 | desc = acpi_get_gpiod_by_index(dev, idx, &info); |
| 1490 | if (IS_ERR(desc)) |
| 1491 | return desc; |
| 1492 | |
| 1493 | if (info.gpioint && info.active_low) |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1494 | *flags |= GPIO_ACTIVE_LOW; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 1495 | |
| 1496 | return desc; |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1497 | } |
| 1498 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1499 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
| 1500 | { |
| 1501 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 1502 | struct gpiod_lookup_table *table; |
| 1503 | |
| 1504 | mutex_lock(&gpio_lookup_lock); |
| 1505 | |
| 1506 | list_for_each_entry(table, &gpio_lookup_list, list) { |
| 1507 | if (table->dev_id && dev_id) { |
| 1508 | /* |
| 1509 | * Valid strings on both ends, must be identical to have |
| 1510 | * a match |
| 1511 | */ |
| 1512 | if (!strcmp(table->dev_id, dev_id)) |
| 1513 | goto found; |
| 1514 | } else { |
| 1515 | /* |
| 1516 | * One of the pointers is NULL, so both must be to have |
| 1517 | * a match |
| 1518 | */ |
| 1519 | if (dev_id == table->dev_id) |
| 1520 | goto found; |
| 1521 | } |
| 1522 | } |
| 1523 | table = NULL; |
| 1524 | |
| 1525 | found: |
| 1526 | mutex_unlock(&gpio_lookup_lock); |
| 1527 | return table; |
| 1528 | } |
| 1529 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1530 | 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] | 1531 | unsigned int idx, |
| 1532 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1533 | { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1534 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1535 | struct gpiod_lookup_table *table; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1536 | struct gpiod_lookup *p; |
| 1537 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1538 | table = gpiod_find_lookup_table(dev); |
| 1539 | if (!table) |
| 1540 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1541 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1542 | for (p = &table->table[0]; p->chip_label; p++) { |
| 1543 | struct gpio_chip *chip; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1544 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1545 | /* idx must always match exactly */ |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1546 | if (p->idx != idx) |
| 1547 | continue; |
| 1548 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1549 | /* If the lookup entry has a con_id, require exact match */ |
| 1550 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) |
| 1551 | continue; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1552 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1553 | chip = find_chip_by_name(p->chip_label); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1554 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1555 | if (!chip) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1556 | dev_err(dev, "cannot find GPIO chip %s\n", |
| 1557 | p->chip_label); |
| 1558 | return ERR_PTR(-ENODEV); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1559 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1560 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1561 | if (chip->ngpio <= p->chip_hwnum) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1562 | dev_err(dev, |
| 1563 | "requested GPIO %d is out of range [0..%d] for chip %s\n", |
| 1564 | idx, chip->ngpio, chip->label); |
| 1565 | return ERR_PTR(-EINVAL); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1566 | } |
| 1567 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 1568 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1569 | *flags = p->flags; |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1570 | |
| 1571 | return desc; |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1572 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1573 | |
| 1574 | return desc; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
Thierry Reding | 0879162 | 2014-04-25 16:54:22 +0200 | [diff] [blame] | 1578 | * gpiod_get - obtain a GPIO for a given GPIO function |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 1579 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1580 | * @con_id: function within the GPIO consumer |
| 1581 | * |
| 1582 | * Return the GPIO descriptor corresponding to the function con_id of device |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1583 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
| 1584 | * another IS_ERR() code if an error occured while trying to acquire the GPIO. |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1585 | */ |
| 1586 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) |
| 1587 | { |
| 1588 | return gpiod_get_index(dev, con_id, 0); |
| 1589 | } |
| 1590 | EXPORT_SYMBOL_GPL(gpiod_get); |
| 1591 | |
| 1592 | /** |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 1593 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function |
| 1594 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 1595 | * @con_id: function within the GPIO consumer |
| 1596 | * |
| 1597 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to |
| 1598 | * the requested function it will return NULL. This is convenient for drivers |
| 1599 | * that need to handle optional GPIOs. |
| 1600 | */ |
| 1601 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
| 1602 | const char *con_id) |
| 1603 | { |
| 1604 | return gpiod_get_index_optional(dev, con_id, 0); |
| 1605 | } |
| 1606 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
| 1607 | |
| 1608 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1609 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
Andy Shevchenko | fdd6a5f | 2013-12-05 11:26:26 +0200 | [diff] [blame] | 1610 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1611 | * @con_id: function within the GPIO consumer |
| 1612 | * @idx: index of the GPIO to obtain in the consumer |
| 1613 | * |
| 1614 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 1615 | * defined one for functions that define several GPIOs. |
| 1616 | * |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1617 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
| 1618 | * requested function and/or index, or another IS_ERR() code if an error |
| 1619 | * occured while trying to acquire the GPIO. |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1620 | */ |
| 1621 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 1622 | const char *con_id, |
| 1623 | unsigned int idx) |
| 1624 | { |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 1625 | struct gpio_desc *desc = NULL; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1626 | int status; |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1627 | enum gpio_lookup_flags flags = 0; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1628 | |
| 1629 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 1630 | |
| 1631 | /* Using device tree? */ |
| 1632 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { |
| 1633 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 1634 | desc = of_find_gpio(dev, con_id, idx, &flags); |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 1635 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { |
| 1636 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
| 1637 | desc = acpi_find_gpio(dev, con_id, idx, &flags); |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Either we are not using DT or ACPI, or their lookup did not return |
| 1642 | * a result. In that case, use platform lookup as a fallback. |
| 1643 | */ |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1644 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1645 | dev_dbg(dev, "using lookup tables for GPIO lookup"); |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 1646 | desc = gpiod_find(dev, con_id, idx, &flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | if (IS_ERR(desc)) { |
Heikki Krogerus | 351cfe0 | 2013-11-29 15:47:34 +0200 | [diff] [blame] | 1650 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1651 | return desc; |
| 1652 | } |
| 1653 | |
| 1654 | status = gpiod_request(desc, con_id); |
| 1655 | |
| 1656 | if (status < 0) |
| 1657 | return ERR_PTR(status); |
| 1658 | |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1659 | if (flags & GPIO_ACTIVE_LOW) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1660 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 1661 | if (flags & GPIO_OPEN_DRAIN) |
| 1662 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 1663 | if (flags & GPIO_OPEN_SOURCE) |
| 1664 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1665 | |
| 1666 | return desc; |
| 1667 | } |
| 1668 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
| 1669 | |
| 1670 | /** |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 1671 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO |
| 1672 | * function |
| 1673 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 1674 | * @con_id: function within the GPIO consumer |
| 1675 | * @index: index of the GPIO to obtain in the consumer |
| 1676 | * |
| 1677 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the |
| 1678 | * specified index was assigned to the requested function it will return NULL. |
| 1679 | * This is convenient for drivers that need to handle optional GPIOs. |
| 1680 | */ |
| 1681 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
| 1682 | const char *con_id, |
| 1683 | unsigned int index) |
| 1684 | { |
| 1685 | struct gpio_desc *desc; |
| 1686 | |
| 1687 | desc = gpiod_get_index(dev, con_id, index); |
| 1688 | if (IS_ERR(desc)) { |
| 1689 | if (PTR_ERR(desc) == -ENOENT) |
| 1690 | return NULL; |
| 1691 | } |
| 1692 | |
| 1693 | return desc; |
| 1694 | } |
| 1695 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
| 1696 | |
| 1697 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 1698 | * gpiod_put - dispose of a GPIO descriptor |
| 1699 | * @desc: GPIO descriptor to dispose of |
| 1700 | * |
| 1701 | * No descriptor can be used after gpiod_put() has been called on it. |
| 1702 | */ |
| 1703 | void gpiod_put(struct gpio_desc *desc) |
| 1704 | { |
| 1705 | gpiod_free(desc); |
| 1706 | } |
| 1707 | EXPORT_SYMBOL_GPL(gpiod_put); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1708 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1709 | #ifdef CONFIG_DEBUG_FS |
| 1710 | |
| 1711 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 1712 | { |
| 1713 | unsigned i; |
| 1714 | unsigned gpio = chip->base; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1715 | struct gpio_desc *gdesc = &chip->desc[0]; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1716 | int is_out; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1717 | int is_irq; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1718 | |
| 1719 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
| 1720 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
| 1721 | continue; |
| 1722 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1723 | gpiod_get_direction(gdesc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1724 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1725 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
| 1726 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1727 | gpio, gdesc->label, |
| 1728 | is_out ? "out" : "in ", |
| 1729 | chip->get |
| 1730 | ? (chip->get(chip, i) ? "hi" : "lo") |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1731 | : "? ", |
| 1732 | is_irq ? "IRQ" : " "); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1733 | seq_printf(s, "\n"); |
| 1734 | } |
| 1735 | } |
| 1736 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1737 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1738 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1739 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1740 | struct gpio_chip *chip = NULL; |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 1741 | loff_t index = *pos; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1742 | |
| 1743 | s->private = ""; |
| 1744 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1745 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 1746 | list_for_each_entry(chip, &gpio_chips, list) |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1747 | if (index-- == 0) { |
| 1748 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 1749 | return chip; |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1750 | } |
| 1751 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 1752 | |
| 1753 | return NULL; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1757 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1758 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1759 | struct gpio_chip *chip = v; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1760 | void *ret = NULL; |
| 1761 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1762 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 1763 | if (list_is_last(&chip->list, &gpio_chips)) |
| 1764 | ret = NULL; |
| 1765 | else |
| 1766 | ret = list_entry(chip->list.next, struct gpio_chip, list); |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 1767 | spin_unlock_irqrestore(&gpio_lock, flags); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1768 | |
| 1769 | s->private = "\n"; |
| 1770 | ++*pos; |
| 1771 | |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
| 1775 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 1776 | { |
| 1777 | } |
| 1778 | |
| 1779 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 1780 | { |
| 1781 | struct gpio_chip *chip = v; |
| 1782 | struct device *dev; |
| 1783 | |
| 1784 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
| 1785 | chip->base, chip->base + chip->ngpio - 1); |
| 1786 | dev = chip->dev; |
| 1787 | if (dev) |
| 1788 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
| 1789 | dev_name(dev)); |
| 1790 | if (chip->label) |
| 1791 | seq_printf(s, ", %s", chip->label); |
| 1792 | if (chip->can_sleep) |
| 1793 | seq_printf(s, ", can sleep"); |
| 1794 | seq_printf(s, ":\n"); |
| 1795 | |
| 1796 | if (chip->dbg_show) |
| 1797 | chip->dbg_show(s, chip); |
| 1798 | else |
| 1799 | gpiolib_dbg_show(s, chip); |
| 1800 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1801 | return 0; |
| 1802 | } |
| 1803 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1804 | static const struct seq_operations gpiolib_seq_ops = { |
| 1805 | .start = gpiolib_seq_start, |
| 1806 | .next = gpiolib_seq_next, |
| 1807 | .stop = gpiolib_seq_stop, |
| 1808 | .show = gpiolib_seq_show, |
| 1809 | }; |
| 1810 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1811 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 1812 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1813 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1816 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1817 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1818 | .open = gpiolib_open, |
| 1819 | .read = seq_read, |
| 1820 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1821 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1822 | }; |
| 1823 | |
| 1824 | static int __init gpiolib_debugfs_init(void) |
| 1825 | { |
| 1826 | /* /sys/kernel/debug/gpio */ |
| 1827 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 1828 | NULL, NULL, &gpiolib_operations); |
| 1829 | return 0; |
| 1830 | } |
| 1831 | subsys_initcall(gpiolib_debugfs_init); |
| 1832 | |
| 1833 | #endif /* DEBUG_FS */ |