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