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