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