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