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