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