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