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