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 | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 19 | #include <linux/cdev.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/uaccess.h> |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 22 | #include <linux/compat.h> |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 23 | #include <linux/anon_inodes.h> |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 24 | #include <linux/kfifo.h> |
| 25 | #include <linux/poll.h> |
| 26 | #include <linux/timekeeping.h> |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 27 | #include <uapi/linux/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 28 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 29 | #include "gpiolib.h" |
| 30 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 31 | #define CREATE_TRACE_POINTS |
| 32 | #include <trace/events/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 33 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 34 | /* Implementation infrastructure for GPIO interfaces. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 35 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 36 | * The GPIO programming interface allows for inlining speed-critical |
| 37 | * get/set operations for common cases, so that access to SOC-integrated |
| 38 | * GPIOs can sometimes cost only an instruction or two per bit. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | |
| 42 | /* When debugging, extend minimal trust to callers and platform code. |
| 43 | * Also emit diagnostic messages that may help initial bringup, when |
| 44 | * board setup or driver bugs are most common. |
| 45 | * |
| 46 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 47 | */ |
| 48 | #ifdef DEBUG |
| 49 | #define extra_checks 1 |
| 50 | #else |
| 51 | #define extra_checks 0 |
| 52 | #endif |
| 53 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 54 | /* Device and char device-related information */ |
| 55 | static DEFINE_IDA(gpio_ida); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 56 | static dev_t gpio_devt; |
| 57 | #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */ |
| 58 | static struct bus_type gpio_bus_type = { |
| 59 | .name = "gpio", |
| 60 | }; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 61 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 62 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 63 | * While any GPIO is requested, its gpio_chip is not removable; |
| 64 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 65 | */ |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 66 | DEFINE_SPINLOCK(gpio_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 67 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 68 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 69 | static LIST_HEAD(gpio_lookup_list); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 70 | LIST_HEAD(gpio_devices); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 71 | |
| 72 | static void gpiochip_free_hogs(struct gpio_chip *chip); |
| 73 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 74 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); |
| 75 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 76 | |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 77 | static bool gpiolib_initialized; |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 78 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 79 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 80 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 81 | d->label = label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 84 | /** |
| 85 | * Convert a GPIO number to its descriptor |
| 86 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 87 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 88 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 89 | struct gpio_device *gdev; |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 90 | unsigned long flags; |
| 91 | |
| 92 | spin_lock_irqsave(&gpio_lock, flags); |
| 93 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 94 | list_for_each_entry(gdev, &gpio_devices, list) { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 95 | if (gdev->base <= gpio && |
| 96 | gdev->base + gdev->ngpio > gpio) { |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 97 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 98 | return &gdev->descs[gpio - gdev->base]; |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 103 | |
Alexandre Courbot | 0e9a5ed | 2014-12-02 23:15:05 +0900 | [diff] [blame] | 104 | if (!gpio_is_valid(gpio)) |
| 105 | WARN(1, "invalid GPIO %d\n", gpio); |
| 106 | |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 107 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 108 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 109 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 110 | |
| 111 | /** |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 112 | * 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] | 113 | */ |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 114 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
| 115 | u16 hwnum) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 116 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 117 | struct gpio_device *gdev = chip->gpiodev; |
| 118 | |
| 119 | if (hwnum >= gdev->ngpio) |
Alexandre Courbot | b7d0a28 | 2013-12-03 12:31:11 +0900 | [diff] [blame] | 120 | return ERR_PTR(-EINVAL); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 121 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 122 | return &gdev->descs[hwnum]; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 123 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Convert a GPIO descriptor to the integer namespace. |
| 127 | * This should disappear in the future but is needed since we still |
| 128 | * use GPIO numbers for error messages and sysfs nodes |
| 129 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 130 | int desc_to_gpio(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 131 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 132 | return desc->gdev->base + (desc - &desc->gdev->descs[0]); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 133 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 134 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 135 | |
| 136 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 137 | /** |
| 138 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 139 | * @desc: descriptor to return the chip of |
| 140 | */ |
| 141 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 142 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 143 | if (!desc || !desc->gdev || !desc->gdev->chip) |
| 144 | return NULL; |
| 145 | return desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 146 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 147 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 148 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 149 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 150 | static int gpiochip_find_base(int ngpio) |
| 151 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 152 | struct gpio_device *gdev; |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 153 | int base = ARCH_NR_GPIOS - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 154 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 155 | list_for_each_entry_reverse(gdev, &gpio_devices, list) { |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 156 | /* found a free space? */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 157 | if (gdev->base + gdev->ngpio <= base) |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 158 | break; |
| 159 | else |
| 160 | /* nope, check the space right before the chip */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 161 | base = gdev->base - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 164 | if (gpio_is_valid(base)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 165 | pr_debug("%s: found new base at %d\n", __func__, base); |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 166 | return base; |
| 167 | } else { |
| 168 | pr_err("%s: cannot find free range\n", __func__); |
| 169 | return -ENOSPC; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 170 | } |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 173 | /** |
| 174 | * gpiod_get_direction - return the current direction of a GPIO |
| 175 | * @desc: GPIO to get the direction of |
| 176 | * |
| 177 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. |
| 178 | * |
| 179 | * This function may sleep if gpiod_cansleep() is true. |
| 180 | */ |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 181 | int gpiod_get_direction(struct gpio_desc *desc) |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 182 | { |
| 183 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 184 | unsigned offset; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 185 | int status = -EINVAL; |
| 186 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 187 | chip = gpiod_to_chip(desc); |
| 188 | offset = gpio_chip_hwgpio(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 189 | |
| 190 | if (!chip->get_direction) |
| 191 | return status; |
| 192 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 193 | status = chip->get_direction(chip, offset); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 194 | if (status > 0) { |
| 195 | /* GPIOF_DIR_IN, or other positive */ |
| 196 | status = 1; |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 197 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 198 | } |
| 199 | if (status == 0) { |
| 200 | /* GPIOF_DIR_OUT */ |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 201 | set_bit(FLAG_IS_OUT, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 202 | } |
| 203 | return status; |
| 204 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 205 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 206 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 207 | /* |
| 208 | * 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] | 209 | * by range(means [base, base + ngpio - 1]) order. |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 210 | * |
| 211 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 212 | * space. |
| 213 | */ |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 214 | static int gpiodev_add_to_list(struct gpio_device *gdev) |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 215 | { |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 216 | struct gpio_device *prev, *next; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 217 | |
| 218 | if (list_empty(&gpio_devices)) { |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 219 | /* initial entry in list */ |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 220 | list_add_tail(&gdev->list, &gpio_devices); |
Sudip Mukherjee | e28ecca | 2015-12-27 19:06:50 +0530 | [diff] [blame] | 221 | return 0; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 224 | next = list_entry(gpio_devices.next, struct gpio_device, list); |
| 225 | if (gdev->base + gdev->ngpio <= next->base) { |
| 226 | /* add before first entry */ |
| 227 | list_add(&gdev->list, &gpio_devices); |
Julien Grossholtz | 96098df | 2016-01-07 16:46:45 -0500 | [diff] [blame] | 228 | return 0; |
| 229 | } |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 230 | |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 231 | prev = list_entry(gpio_devices.prev, struct gpio_device, list); |
| 232 | if (prev->base + prev->ngpio <= gdev->base) { |
| 233 | /* add behind last entry */ |
| 234 | list_add_tail(&gdev->list, &gpio_devices); |
| 235 | return 0; |
| 236 | } |
Bamvor Jian Zhang | ef7c755 | 2015-11-16 13:02:46 +0800 | [diff] [blame] | 237 | |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 238 | list_for_each_entry_safe(prev, next, &gpio_devices, list) { |
| 239 | /* at the end of the list */ |
| 240 | if (&next->list == &gpio_devices) |
| 241 | break; |
| 242 | |
| 243 | /* add between prev and next */ |
| 244 | if (prev->base + prev->ngpio <= gdev->base |
| 245 | && gdev->base + gdev->ngpio <= next->base) { |
| 246 | list_add(&gdev->list, &prev->list); |
| 247 | return 0; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); |
| 252 | return -EBUSY; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Convert a GPIO name to its descriptor |
| 257 | */ |
| 258 | static struct gpio_desc *gpio_name_to_desc(const char * const name) |
| 259 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 260 | struct gpio_device *gdev; |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 261 | unsigned long flags; |
| 262 | |
| 263 | spin_lock_irqsave(&gpio_lock, flags); |
| 264 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 265 | list_for_each_entry(gdev, &gpio_devices, list) { |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 266 | int i; |
| 267 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 268 | for (i = 0; i != gdev->ngpio; ++i) { |
| 269 | struct gpio_desc *desc = &gdev->descs[i]; |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 270 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 271 | if (!desc->name || !name) |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 272 | continue; |
| 273 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 274 | if (!strcmp(desc->name, name)) { |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 275 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 276 | return desc; |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 282 | |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 286 | /* |
| 287 | * Takes the names from gc->names and checks if they are all unique. If they |
| 288 | * are, they are assigned to their gpio descriptors. |
| 289 | * |
Bamvor Jian Zhang | ed37915 | 2015-11-14 16:43:20 +0800 | [diff] [blame] | 290 | * 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] | 291 | */ |
| 292 | static int gpiochip_set_desc_names(struct gpio_chip *gc) |
| 293 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 294 | struct gpio_device *gdev = gc->gpiodev; |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 295 | int i; |
| 296 | |
| 297 | if (!gc->names) |
| 298 | return 0; |
| 299 | |
| 300 | /* First check all names if they are unique */ |
| 301 | for (i = 0; i != gc->ngpio; ++i) { |
| 302 | struct gpio_desc *gpio; |
| 303 | |
| 304 | gpio = gpio_name_to_desc(gc->names[i]); |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 305 | if (gpio) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 306 | dev_warn(&gdev->dev, |
Linus Walleij | 34ffd85 | 2015-10-20 11:31:54 +0200 | [diff] [blame] | 307 | "Detected name collision for GPIO name '%s'\n", |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 308 | gc->names[i]); |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Then add all names to the GPIO descriptors */ |
| 312 | for (i = 0; i != gc->ngpio; ++i) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 313 | gdev->descs[i].name = gc->names[i]; |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 318 | /* |
| 319 | * GPIO line handle management |
| 320 | */ |
| 321 | |
| 322 | /** |
| 323 | * struct linehandle_state - contains the state of a userspace handle |
| 324 | * @gdev: the GPIO device the handle pertains to |
| 325 | * @label: consumer label used to tag descriptors |
| 326 | * @descs: the GPIO descriptors held by this handle |
| 327 | * @numdescs: the number of descriptors held in the descs array |
| 328 | */ |
| 329 | struct linehandle_state { |
| 330 | struct gpio_device *gdev; |
| 331 | const char *label; |
| 332 | struct gpio_desc *descs[GPIOHANDLES_MAX]; |
| 333 | u32 numdescs; |
| 334 | }; |
| 335 | |
| 336 | static long linehandle_ioctl(struct file *filep, unsigned int cmd, |
| 337 | unsigned long arg) |
| 338 | { |
| 339 | struct linehandle_state *lh = filep->private_data; |
| 340 | void __user *ip = (void __user *)arg; |
| 341 | struct gpiohandle_data ghd; |
| 342 | int i; |
| 343 | |
| 344 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 345 | int val; |
| 346 | |
| 347 | /* TODO: check if descriptors are really input */ |
| 348 | for (i = 0; i < lh->numdescs; i++) { |
| 349 | val = gpiod_get_value_cansleep(lh->descs[i]); |
| 350 | if (val < 0) |
| 351 | return val; |
| 352 | ghd.values[i] = val; |
| 353 | } |
| 354 | |
| 355 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 356 | return -EFAULT; |
| 357 | |
| 358 | return 0; |
| 359 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
| 360 | int vals[GPIOHANDLES_MAX]; |
| 361 | |
| 362 | /* TODO: check if descriptors are really output */ |
| 363 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
| 364 | return -EFAULT; |
| 365 | |
| 366 | /* Clamp all values to [0,1] */ |
| 367 | for (i = 0; i < lh->numdescs; i++) |
| 368 | vals[i] = !!ghd.values[i]; |
| 369 | |
| 370 | /* Reuse the array setting function */ |
| 371 | gpiod_set_array_value_complex(false, |
| 372 | true, |
| 373 | lh->numdescs, |
| 374 | lh->descs, |
| 375 | vals); |
| 376 | return 0; |
| 377 | } |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | #ifdef CONFIG_COMPAT |
| 382 | static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd, |
| 383 | unsigned long arg) |
| 384 | { |
| 385 | return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); |
| 386 | } |
| 387 | #endif |
| 388 | |
| 389 | static int linehandle_release(struct inode *inode, struct file *filep) |
| 390 | { |
| 391 | struct linehandle_state *lh = filep->private_data; |
| 392 | struct gpio_device *gdev = lh->gdev; |
| 393 | int i; |
| 394 | |
| 395 | for (i = 0; i < lh->numdescs; i++) |
| 396 | gpiod_free(lh->descs[i]); |
| 397 | kfree(lh->label); |
| 398 | kfree(lh); |
| 399 | put_device(&gdev->dev); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static const struct file_operations linehandle_fileops = { |
| 404 | .release = linehandle_release, |
| 405 | .owner = THIS_MODULE, |
| 406 | .llseek = noop_llseek, |
| 407 | .unlocked_ioctl = linehandle_ioctl, |
| 408 | #ifdef CONFIG_COMPAT |
| 409 | .compat_ioctl = linehandle_ioctl_compat, |
| 410 | #endif |
| 411 | }; |
| 412 | |
| 413 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) |
| 414 | { |
| 415 | struct gpiohandle_request handlereq; |
| 416 | struct linehandle_state *lh; |
| 417 | int fd, i, ret; |
| 418 | |
| 419 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) |
| 420 | return -EFAULT; |
| 421 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) |
| 422 | return -EINVAL; |
| 423 | |
| 424 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
| 425 | if (!lh) |
| 426 | return -ENOMEM; |
| 427 | lh->gdev = gdev; |
| 428 | get_device(&gdev->dev); |
| 429 | |
| 430 | /* Make sure this is terminated */ |
| 431 | handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; |
| 432 | if (strlen(handlereq.consumer_label)) { |
| 433 | lh->label = kstrdup(handlereq.consumer_label, |
| 434 | GFP_KERNEL); |
| 435 | if (!lh->label) { |
| 436 | ret = -ENOMEM; |
| 437 | goto out_free_lh; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* Request each GPIO */ |
| 442 | for (i = 0; i < handlereq.lines; i++) { |
| 443 | u32 offset = handlereq.lineoffsets[i]; |
| 444 | u32 lflags = handlereq.flags; |
| 445 | struct gpio_desc *desc; |
| 446 | |
| 447 | desc = &gdev->descs[offset]; |
| 448 | ret = gpiod_request(desc, lh->label); |
| 449 | if (ret) |
| 450 | goto out_free_descs; |
| 451 | lh->descs[i] = desc; |
| 452 | |
| 453 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) |
| 454 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 455 | if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) |
| 456 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 457 | if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) |
| 458 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 459 | |
| 460 | /* |
| 461 | * Lines have to be requested explicitly for input |
| 462 | * or output, else the line will be treated "as is". |
| 463 | */ |
| 464 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 465 | int val = !!handlereq.default_values[i]; |
| 466 | |
| 467 | ret = gpiod_direction_output(desc, val); |
| 468 | if (ret) |
| 469 | goto out_free_descs; |
| 470 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
| 471 | ret = gpiod_direction_input(desc); |
| 472 | if (ret) |
| 473 | goto out_free_descs; |
| 474 | } |
| 475 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", |
| 476 | offset); |
| 477 | } |
Linus Walleij | e2f608b | 2016-06-18 10:56:43 +0200 | [diff] [blame] | 478 | /* Let i point at the last handle */ |
| 479 | i--; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 480 | lh->numdescs = handlereq.lines; |
| 481 | |
| 482 | fd = anon_inode_getfd("gpio-linehandle", |
| 483 | &linehandle_fileops, |
| 484 | lh, |
| 485 | O_RDONLY | O_CLOEXEC); |
| 486 | if (fd < 0) { |
| 487 | ret = fd; |
| 488 | goto out_free_descs; |
| 489 | } |
| 490 | |
| 491 | handlereq.fd = fd; |
Linus Walleij | d932cd4 | 2016-07-04 13:13:04 +0200 | [diff] [blame] | 492 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
| 493 | ret = -EFAULT; |
| 494 | goto out_free_descs; |
| 495 | } |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 496 | |
| 497 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
| 498 | lh->numdescs); |
| 499 | |
| 500 | return 0; |
| 501 | |
| 502 | out_free_descs: |
| 503 | for (; i >= 0; i--) |
| 504 | gpiod_free(lh->descs[i]); |
| 505 | kfree(lh->label); |
| 506 | out_free_lh: |
| 507 | kfree(lh); |
| 508 | put_device(&gdev->dev); |
| 509 | return ret; |
| 510 | } |
| 511 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 512 | /* |
| 513 | * GPIO line event management |
| 514 | */ |
| 515 | |
| 516 | /** |
| 517 | * struct lineevent_state - contains the state of a userspace event |
| 518 | * @gdev: the GPIO device the event pertains to |
| 519 | * @label: consumer label used to tag descriptors |
| 520 | * @desc: the GPIO descriptor held by this event |
| 521 | * @eflags: the event flags this line was requested with |
| 522 | * @irq: the interrupt that trigger in response to events on this GPIO |
| 523 | * @wait: wait queue that handles blocking reads of events |
| 524 | * @events: KFIFO for the GPIO events |
| 525 | * @read_lock: mutex lock to protect reads from colliding with adding |
| 526 | * new events to the FIFO |
| 527 | */ |
| 528 | struct lineevent_state { |
| 529 | struct gpio_device *gdev; |
| 530 | const char *label; |
| 531 | struct gpio_desc *desc; |
| 532 | u32 eflags; |
| 533 | int irq; |
| 534 | wait_queue_head_t wait; |
| 535 | DECLARE_KFIFO(events, struct gpioevent_data, 16); |
| 536 | struct mutex read_lock; |
| 537 | }; |
| 538 | |
| 539 | static unsigned int lineevent_poll(struct file *filep, |
| 540 | struct poll_table_struct *wait) |
| 541 | { |
| 542 | struct lineevent_state *le = filep->private_data; |
| 543 | unsigned int events = 0; |
| 544 | |
| 545 | poll_wait(filep, &le->wait, wait); |
| 546 | |
| 547 | if (!kfifo_is_empty(&le->events)) |
| 548 | events = POLLIN | POLLRDNORM; |
| 549 | |
| 550 | return events; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | static ssize_t lineevent_read(struct file *filep, |
| 555 | char __user *buf, |
| 556 | size_t count, |
| 557 | loff_t *f_ps) |
| 558 | { |
| 559 | struct lineevent_state *le = filep->private_data; |
| 560 | unsigned int copied; |
| 561 | int ret; |
| 562 | |
| 563 | if (count < sizeof(struct gpioevent_data)) |
| 564 | return -EINVAL; |
| 565 | |
| 566 | do { |
| 567 | if (kfifo_is_empty(&le->events)) { |
| 568 | if (filep->f_flags & O_NONBLOCK) |
| 569 | return -EAGAIN; |
| 570 | |
| 571 | ret = wait_event_interruptible(le->wait, |
| 572 | !kfifo_is_empty(&le->events)); |
| 573 | if (ret) |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | if (mutex_lock_interruptible(&le->read_lock)) |
| 578 | return -ERESTARTSYS; |
| 579 | ret = kfifo_to_user(&le->events, buf, count, &copied); |
| 580 | mutex_unlock(&le->read_lock); |
| 581 | |
| 582 | if (ret) |
| 583 | return ret; |
| 584 | |
| 585 | /* |
| 586 | * If we couldn't read anything from the fifo (a different |
| 587 | * thread might have been faster) we either return -EAGAIN if |
| 588 | * the file descriptor is non-blocking, otherwise we go back to |
| 589 | * sleep and wait for more data to arrive. |
| 590 | */ |
| 591 | if (copied == 0 && (filep->f_flags & O_NONBLOCK)) |
| 592 | return -EAGAIN; |
| 593 | |
| 594 | } while (copied == 0); |
| 595 | |
| 596 | return copied; |
| 597 | } |
| 598 | |
| 599 | static int lineevent_release(struct inode *inode, struct file *filep) |
| 600 | { |
| 601 | struct lineevent_state *le = filep->private_data; |
| 602 | struct gpio_device *gdev = le->gdev; |
| 603 | |
| 604 | free_irq(le->irq, le); |
| 605 | gpiod_free(le->desc); |
| 606 | kfree(le->label); |
| 607 | kfree(le); |
| 608 | put_device(&gdev->dev); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static long lineevent_ioctl(struct file *filep, unsigned int cmd, |
| 613 | unsigned long arg) |
| 614 | { |
| 615 | struct lineevent_state *le = filep->private_data; |
| 616 | void __user *ip = (void __user *)arg; |
| 617 | struct gpiohandle_data ghd; |
| 618 | |
| 619 | /* |
| 620 | * We can get the value for an event line but not set it, |
| 621 | * because it is input by definition. |
| 622 | */ |
| 623 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 624 | int val; |
| 625 | |
| 626 | val = gpiod_get_value_cansleep(le->desc); |
| 627 | if (val < 0) |
| 628 | return val; |
| 629 | ghd.values[0] = val; |
| 630 | |
| 631 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 632 | return -EFAULT; |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | #ifdef CONFIG_COMPAT |
| 640 | static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd, |
| 641 | unsigned long arg) |
| 642 | { |
| 643 | return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); |
| 644 | } |
| 645 | #endif |
| 646 | |
| 647 | static const struct file_operations lineevent_fileops = { |
| 648 | .release = lineevent_release, |
| 649 | .read = lineevent_read, |
| 650 | .poll = lineevent_poll, |
| 651 | .owner = THIS_MODULE, |
| 652 | .llseek = noop_llseek, |
| 653 | .unlocked_ioctl = lineevent_ioctl, |
| 654 | #ifdef CONFIG_COMPAT |
| 655 | .compat_ioctl = lineevent_ioctl_compat, |
| 656 | #endif |
| 657 | }; |
| 658 | |
Ben Dooks | 33265b1 | 2016-06-17 16:03:13 +0100 | [diff] [blame] | 659 | static irqreturn_t lineevent_irq_thread(int irq, void *p) |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 660 | { |
| 661 | struct lineevent_state *le = p; |
| 662 | struct gpioevent_data ge; |
| 663 | int ret; |
| 664 | |
| 665 | ge.timestamp = ktime_get_real_ns(); |
| 666 | |
| 667 | if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) { |
| 668 | int level = gpiod_get_value_cansleep(le->desc); |
| 669 | |
| 670 | if (level) |
| 671 | /* Emit low-to-high event */ |
| 672 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 673 | else |
| 674 | /* Emit high-to-low event */ |
| 675 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
| 676 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { |
| 677 | /* Emit low-to-high event */ |
| 678 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 679 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
| 680 | /* Emit high-to-low event */ |
| 681 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
Arnd Bergmann | bc0207a | 2016-06-16 11:02:41 +0200 | [diff] [blame] | 682 | } else { |
| 683 | return IRQ_NONE; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | ret = kfifo_put(&le->events, ge); |
| 687 | if (ret != 0) |
| 688 | wake_up_poll(&le->wait, POLLIN); |
| 689 | |
| 690 | return IRQ_HANDLED; |
| 691 | } |
| 692 | |
| 693 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) |
| 694 | { |
| 695 | struct gpioevent_request eventreq; |
| 696 | struct lineevent_state *le; |
| 697 | struct gpio_desc *desc; |
| 698 | u32 offset; |
| 699 | u32 lflags; |
| 700 | u32 eflags; |
| 701 | int fd; |
| 702 | int ret; |
| 703 | int irqflags = 0; |
| 704 | |
| 705 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) |
| 706 | return -EFAULT; |
| 707 | |
| 708 | le = kzalloc(sizeof(*le), GFP_KERNEL); |
| 709 | if (!le) |
| 710 | return -ENOMEM; |
| 711 | le->gdev = gdev; |
| 712 | get_device(&gdev->dev); |
| 713 | |
| 714 | /* Make sure this is terminated */ |
| 715 | eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; |
| 716 | if (strlen(eventreq.consumer_label)) { |
| 717 | le->label = kstrdup(eventreq.consumer_label, |
| 718 | GFP_KERNEL); |
| 719 | if (!le->label) { |
| 720 | ret = -ENOMEM; |
| 721 | goto out_free_le; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | offset = eventreq.lineoffset; |
| 726 | lflags = eventreq.handleflags; |
| 727 | eflags = eventreq.eventflags; |
| 728 | |
| 729 | /* This is just wrong: we don't look for events on output lines */ |
| 730 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 731 | ret = -EINVAL; |
| 732 | goto out_free_label; |
| 733 | } |
| 734 | |
| 735 | desc = &gdev->descs[offset]; |
| 736 | ret = gpiod_request(desc, le->label); |
| 737 | if (ret) |
| 738 | goto out_free_desc; |
| 739 | le->desc = desc; |
| 740 | le->eflags = eflags; |
| 741 | |
| 742 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) |
| 743 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 744 | if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) |
| 745 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 746 | if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) |
| 747 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 748 | |
| 749 | ret = gpiod_direction_input(desc); |
| 750 | if (ret) |
| 751 | goto out_free_desc; |
| 752 | |
| 753 | le->irq = gpiod_to_irq(desc); |
| 754 | if (le->irq <= 0) { |
| 755 | ret = -ENODEV; |
| 756 | goto out_free_desc; |
| 757 | } |
| 758 | |
| 759 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) |
| 760 | irqflags |= IRQF_TRIGGER_RISING; |
| 761 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) |
| 762 | irqflags |= IRQF_TRIGGER_FALLING; |
| 763 | irqflags |= IRQF_ONESHOT; |
| 764 | irqflags |= IRQF_SHARED; |
| 765 | |
| 766 | INIT_KFIFO(le->events); |
| 767 | init_waitqueue_head(&le->wait); |
| 768 | mutex_init(&le->read_lock); |
| 769 | |
| 770 | /* Request a thread to read the events */ |
| 771 | ret = request_threaded_irq(le->irq, |
| 772 | NULL, |
| 773 | lineevent_irq_thread, |
| 774 | irqflags, |
| 775 | le->label, |
| 776 | le); |
| 777 | if (ret) |
| 778 | goto out_free_desc; |
| 779 | |
| 780 | fd = anon_inode_getfd("gpio-event", |
| 781 | &lineevent_fileops, |
| 782 | le, |
| 783 | O_RDONLY | O_CLOEXEC); |
| 784 | if (fd < 0) { |
| 785 | ret = fd; |
| 786 | goto out_free_irq; |
| 787 | } |
| 788 | |
| 789 | eventreq.fd = fd; |
Linus Walleij | d932cd4 | 2016-07-04 13:13:04 +0200 | [diff] [blame] | 790 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
| 791 | ret = -EFAULT; |
| 792 | goto out_free_irq; |
| 793 | } |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 794 | |
| 795 | return 0; |
| 796 | |
| 797 | out_free_irq: |
| 798 | free_irq(le->irq, le); |
| 799 | out_free_desc: |
| 800 | gpiod_free(le->desc); |
| 801 | out_free_label: |
| 802 | kfree(le->label); |
| 803 | out_free_le: |
| 804 | kfree(le); |
| 805 | put_device(&gdev->dev); |
| 806 | return ret; |
| 807 | } |
| 808 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 809 | /** |
| 810 | * gpio_ioctl() - ioctl handler for the GPIO chardev |
| 811 | */ |
| 812 | static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 813 | { |
| 814 | struct gpio_device *gdev = filp->private_data; |
| 815 | struct gpio_chip *chip = gdev->chip; |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 816 | void __user *ip = (void __user *)arg; |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 817 | |
| 818 | /* We fail any subsequent ioctl():s when the chip is gone */ |
| 819 | if (!chip) |
| 820 | return -ENODEV; |
| 821 | |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 822 | /* Fill in the struct and pass to userspace */ |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 823 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 824 | struct gpiochip_info chipinfo; |
| 825 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 826 | strncpy(chipinfo.name, dev_name(&gdev->dev), |
| 827 | sizeof(chipinfo.name)); |
| 828 | chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 829 | strncpy(chipinfo.label, gdev->label, |
| 830 | sizeof(chipinfo.label)); |
| 831 | chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 832 | chipinfo.lines = gdev->ngpio; |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 833 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
| 834 | return -EFAULT; |
| 835 | return 0; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 836 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
| 837 | struct gpioline_info lineinfo; |
| 838 | struct gpio_desc *desc; |
| 839 | |
| 840 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
| 841 | return -EFAULT; |
| 842 | if (lineinfo.line_offset > gdev->ngpio) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | desc = &gdev->descs[lineinfo.line_offset]; |
| 846 | if (desc->name) { |
| 847 | strncpy(lineinfo.name, desc->name, |
| 848 | sizeof(lineinfo.name)); |
| 849 | lineinfo.name[sizeof(lineinfo.name)-1] = '\0'; |
| 850 | } else { |
| 851 | lineinfo.name[0] = '\0'; |
| 852 | } |
| 853 | if (desc->label) { |
Linus Walleij | 214338e | 2016-02-25 21:01:48 +0100 | [diff] [blame] | 854 | strncpy(lineinfo.consumer, desc->label, |
| 855 | sizeof(lineinfo.consumer)); |
| 856 | lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0'; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 857 | } else { |
Linus Walleij | 214338e | 2016-02-25 21:01:48 +0100 | [diff] [blame] | 858 | lineinfo.consumer[0] = '\0'; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Userspace only need to know that the kernel is using |
| 863 | * this GPIO so it can't use it. |
| 864 | */ |
| 865 | lineinfo.flags = 0; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 866 | if (test_bit(FLAG_REQUESTED, &desc->flags) || |
| 867 | test_bit(FLAG_IS_HOGGED, &desc->flags) || |
| 868 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || |
| 869 | test_bit(FLAG_EXPORT, &desc->flags) || |
| 870 | test_bit(FLAG_SYSFS, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 871 | lineinfo.flags |= GPIOLINE_FLAG_KERNEL; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 872 | if (test_bit(FLAG_IS_OUT, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 873 | lineinfo.flags |= GPIOLINE_FLAG_IS_OUT; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 874 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 875 | lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 876 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 877 | lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 878 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 879 | lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE; |
| 880 | |
| 881 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) |
| 882 | return -EFAULT; |
| 883 | return 0; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 884 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
| 885 | return linehandle_create(gdev, ip); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 886 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
| 887 | return lineevent_create(gdev, ip); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 888 | } |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 892 | #ifdef CONFIG_COMPAT |
| 893 | static long gpio_ioctl_compat(struct file *filp, unsigned int cmd, |
| 894 | unsigned long arg) |
| 895 | { |
| 896 | return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
| 897 | } |
| 898 | #endif |
| 899 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 900 | /** |
| 901 | * gpio_chrdev_open() - open the chardev for ioctl operations |
| 902 | * @inode: inode for this chardev |
| 903 | * @filp: file struct for storing private data |
| 904 | * Returns 0 on success |
| 905 | */ |
| 906 | static int gpio_chrdev_open(struct inode *inode, struct file *filp) |
| 907 | { |
| 908 | struct gpio_device *gdev = container_of(inode->i_cdev, |
| 909 | struct gpio_device, chrdev); |
| 910 | |
| 911 | /* Fail on open if the backing gpiochip is gone */ |
| 912 | if (!gdev || !gdev->chip) |
| 913 | return -ENODEV; |
| 914 | get_device(&gdev->dev); |
| 915 | filp->private_data = gdev; |
Lars-Peter Clausen | f4e81c5 | 2016-11-30 13:05:21 +0100 | [diff] [blame^] | 916 | |
| 917 | return nonseekable_open(inode, filp); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /** |
| 921 | * gpio_chrdev_release() - close chardev after ioctl operations |
| 922 | * @inode: inode for this chardev |
| 923 | * @filp: file struct for storing private data |
| 924 | * Returns 0 on success |
| 925 | */ |
| 926 | static int gpio_chrdev_release(struct inode *inode, struct file *filp) |
| 927 | { |
| 928 | struct gpio_device *gdev = container_of(inode->i_cdev, |
| 929 | struct gpio_device, chrdev); |
| 930 | |
| 931 | if (!gdev) |
| 932 | return -ENODEV; |
| 933 | put_device(&gdev->dev); |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | |
| 938 | static const struct file_operations gpio_fileops = { |
| 939 | .release = gpio_chrdev_release, |
| 940 | .open = gpio_chrdev_open, |
| 941 | .owner = THIS_MODULE, |
Lars-Peter Clausen | f4e81c5 | 2016-11-30 13:05:21 +0100 | [diff] [blame^] | 942 | .llseek = no_llseek, |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 943 | .unlocked_ioctl = gpio_ioctl, |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 944 | #ifdef CONFIG_COMPAT |
| 945 | .compat_ioctl = gpio_ioctl_compat, |
| 946 | #endif |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 947 | }; |
| 948 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 949 | static void gpiodevice_release(struct device *dev) |
| 950 | { |
| 951 | struct gpio_device *gdev = dev_get_drvdata(dev); |
| 952 | |
| 953 | list_del(&gdev->list); |
| 954 | ida_simple_remove(&gpio_ida, gdev->id); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 955 | kfree(gdev->label); |
| 956 | kfree(gdev->descs); |
Linus Walleij | 9efd9e6 | 2016-02-09 14:27:42 +0100 | [diff] [blame] | 957 | kfree(gdev); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 960 | static int gpiochip_setup_dev(struct gpio_device *gdev) |
| 961 | { |
| 962 | int status; |
| 963 | |
| 964 | cdev_init(&gdev->chrdev, &gpio_fileops); |
| 965 | gdev->chrdev.owner = THIS_MODULE; |
| 966 | gdev->chrdev.kobj.parent = &gdev->dev.kobj; |
| 967 | gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id); |
| 968 | status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1); |
| 969 | if (status < 0) |
| 970 | chip_warn(gdev->chip, "failed to add char device %d:%d\n", |
| 971 | MAJOR(gpio_devt), gdev->id); |
| 972 | else |
| 973 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", |
| 974 | MAJOR(gpio_devt), gdev->id); |
| 975 | status = device_add(&gdev->dev); |
| 976 | if (status) |
| 977 | goto err_remove_chardev; |
| 978 | |
| 979 | status = gpiochip_sysfs_register(gdev); |
| 980 | if (status) |
| 981 | goto err_remove_device; |
| 982 | |
| 983 | /* From this point, the .release() function cleans up gpio_device */ |
| 984 | gdev->dev.release = gpiodevice_release; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 985 | pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n", |
| 986 | __func__, gdev->base, gdev->base + gdev->ngpio - 1, |
| 987 | dev_name(&gdev->dev), gdev->chip->label ? : "generic"); |
| 988 | |
| 989 | return 0; |
| 990 | |
| 991 | err_remove_device: |
| 992 | device_del(&gdev->dev); |
| 993 | err_remove_chardev: |
| 994 | cdev_del(&gdev->chrdev); |
| 995 | return status; |
| 996 | } |
| 997 | |
| 998 | static void gpiochip_setup_devs(void) |
| 999 | { |
| 1000 | struct gpio_device *gdev; |
| 1001 | int err; |
| 1002 | |
| 1003 | list_for_each_entry(gdev, &gpio_devices, list) { |
| 1004 | err = gpiochip_setup_dev(gdev); |
| 1005 | if (err) |
| 1006 | pr_err("%s: Failed to initialize gpio device (%d)\n", |
| 1007 | dev_name(&gdev->dev), err); |
| 1008 | } |
| 1009 | } |
| 1010 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 1011 | /** |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame] | 1012 | * gpiochip_add_data() - register a gpio_chip |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1013 | * @chip: the chip to register, with chip->base initialized |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1014 | * Context: potentially before irqs will work |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1015 | * |
| 1016 | * Returns a negative errno if the chip can't be registered, such as |
| 1017 | * because the chip->base is invalid or already associated with a |
| 1018 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1019 | * |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame] | 1020 | * 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] | 1021 | * can be freely used, the chip->parent device must be registered before |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1022 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 1023 | * for GPIOs will fail rudely. |
| 1024 | * |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1025 | * gpiochip_add_data() must only be called after gpiolib initialization, |
| 1026 | * ie after core_initcall(). |
| 1027 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1028 | * If chip->base is negative, this requests dynamic assignment of |
| 1029 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1030 | */ |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame] | 1031 | int gpiochip_add_data(struct gpio_chip *chip, void *data) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1032 | { |
| 1033 | unsigned long flags; |
| 1034 | int status = 0; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1035 | unsigned i; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1036 | int base = chip->base; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1037 | struct gpio_device *gdev; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1038 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1039 | /* |
| 1040 | * First: allocate and populate the internal stat container, and |
| 1041 | * set up the struct device. |
| 1042 | */ |
Josh Cartwright | 969f07b | 2016-02-17 16:44:15 -0600 | [diff] [blame] | 1043 | gdev = kzalloc(sizeof(*gdev), GFP_KERNEL); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1044 | if (!gdev) |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1045 | return -ENOMEM; |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1046 | gdev->dev.bus = &gpio_bus_type; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1047 | gdev->chip = chip; |
| 1048 | chip->gpiodev = gdev; |
| 1049 | if (chip->parent) { |
| 1050 | gdev->dev.parent = chip->parent; |
| 1051 | gdev->dev.of_node = chip->parent->of_node; |
Thierry Reding | acc6e33 | 2016-07-05 14:11:14 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1054 | #ifdef CONFIG_OF_GPIO |
| 1055 | /* If the gpiochip has an assigned OF node this takes precedence */ |
Thierry Reding | acc6e33 | 2016-07-05 14:11:14 +0200 | [diff] [blame] | 1056 | if (chip->of_node) |
| 1057 | gdev->dev.of_node = chip->of_node; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1058 | #endif |
Thierry Reding | acc6e33 | 2016-07-05 14:11:14 +0200 | [diff] [blame] | 1059 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1060 | gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); |
| 1061 | if (gdev->id < 0) { |
| 1062 | status = gdev->id; |
| 1063 | goto err_free_gdev; |
| 1064 | } |
| 1065 | dev_set_name(&gdev->dev, "gpiochip%d", gdev->id); |
| 1066 | device_initialize(&gdev->dev); |
| 1067 | dev_set_drvdata(&gdev->dev, gdev); |
| 1068 | if (chip->parent && chip->parent->driver) |
| 1069 | gdev->owner = chip->parent->driver->owner; |
| 1070 | else if (chip->owner) |
| 1071 | /* TODO: remove chip->owner */ |
| 1072 | gdev->owner = chip->owner; |
| 1073 | else |
| 1074 | gdev->owner = THIS_MODULE; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1075 | |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1076 | gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1077 | if (!gdev->descs) { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1078 | status = -ENOMEM; |
| 1079 | goto err_free_gdev; |
| 1080 | } |
| 1081 | |
Bamvor Jian Zhang | 5ed41cc | 2015-11-16 13:02:47 +0800 | [diff] [blame] | 1082 | if (chip->ngpio == 0) { |
| 1083 | 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] | 1084 | status = -EINVAL; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1085 | goto err_free_descs; |
Bamvor Jian Zhang | 5ed41cc | 2015-11-16 13:02:47 +0800 | [diff] [blame] | 1086 | } |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1087 | |
| 1088 | if (chip->label) |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1089 | gdev->label = kstrdup(chip->label, GFP_KERNEL); |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1090 | else |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1091 | gdev->label = kstrdup("unknown", GFP_KERNEL); |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1092 | if (!gdev->label) { |
| 1093 | status = -ENOMEM; |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1094 | goto err_free_descs; |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1097 | gdev->ngpio = chip->ngpio; |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1098 | gdev->data = data; |
Bamvor Jian Zhang | 5ed41cc | 2015-11-16 13:02:47 +0800 | [diff] [blame] | 1099 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1100 | spin_lock_irqsave(&gpio_lock, flags); |
| 1101 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1102 | /* |
| 1103 | * TODO: this allocates a Linux GPIO number base in the global |
| 1104 | * GPIO numberspace for this chip. In the long run we want to |
| 1105 | * get *rid* of this numberspace and use only descriptors, but |
| 1106 | * it may be a pipe dream. It will not happen before we get rid |
| 1107 | * of the sysfs interface anyways. |
| 1108 | */ |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1109 | if (base < 0) { |
| 1110 | base = gpiochip_find_base(chip->ngpio); |
| 1111 | if (base < 0) { |
| 1112 | status = base; |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1113 | spin_unlock_irqrestore(&gpio_lock, flags); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1114 | goto err_free_label; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1115 | } |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1116 | /* |
| 1117 | * TODO: it should not be necessary to reflect the assigned |
| 1118 | * base outside of the GPIO subsystem. Go over drivers and |
| 1119 | * see if anyone makes use of this, else drop this and assign |
| 1120 | * a poison instead. |
| 1121 | */ |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1122 | chip->base = base; |
| 1123 | } |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1124 | gdev->base = base; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1125 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1126 | status = gpiodev_add_to_list(gdev); |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 1127 | if (status) { |
| 1128 | spin_unlock_irqrestore(&gpio_lock, flags); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1129 | goto err_free_label; |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 1130 | } |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1131 | |
Linus Walleij | 545ebd9 | 2016-05-30 17:11:59 +0200 | [diff] [blame] | 1132 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1133 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1134 | for (i = 0; i < chip->ngpio; i++) { |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1135 | struct gpio_desc *desc = &gdev->descs[i]; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1136 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1137 | desc->gdev = gdev; |
Linus Walleij | 72d3200 | 2016-04-28 13:33:59 +0200 | [diff] [blame] | 1138 | /* |
| 1139 | * REVISIT: most hardware initializes GPIOs as inputs |
| 1140 | * (often with pullups enabled) so power usage is |
| 1141 | * minimized. Linux code should set the gpio direction |
| 1142 | * first thing; but until it does, and in case |
| 1143 | * chip->get_direction is not set, we may expose the |
| 1144 | * wrong direction in sysfs. |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 1145 | */ |
Linus Walleij | 72d3200 | 2016-04-28 13:33:59 +0200 | [diff] [blame] | 1146 | |
| 1147 | if (chip->get_direction) { |
| 1148 | /* |
| 1149 | * If we have .get_direction, set up the initial |
| 1150 | * direction flag from the hardware. |
| 1151 | */ |
| 1152 | int dir = chip->get_direction(chip, i); |
| 1153 | |
| 1154 | if (!dir) |
| 1155 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1156 | } else if (!chip->direction_input) { |
| 1157 | /* |
| 1158 | * If the chip lacks the .direction_input callback |
| 1159 | * we logically assume all lines are outputs. |
| 1160 | */ |
| 1161 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1162 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1163 | } |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1164 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1165 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 1166 | INIT_LIST_HEAD(&gdev->pin_ranges); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1167 | #endif |
| 1168 | |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 1169 | status = gpiochip_set_desc_names(chip); |
| 1170 | if (status) |
| 1171 | goto err_remove_from_list; |
| 1172 | |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1173 | status = gpiochip_irqchip_init_valid_mask(chip); |
| 1174 | if (status) |
| 1175 | goto err_remove_from_list; |
| 1176 | |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1177 | status = of_gpiochip_add(chip); |
| 1178 | if (status) |
| 1179 | goto err_remove_chip; |
| 1180 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 1181 | acpi_gpiochip_add(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1182 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1183 | /* |
| 1184 | * By first adding the chardev, and then adding the device, |
| 1185 | * we get a device node entry in sysfs under |
| 1186 | * /sys/bus/gpio/devices/gpiochipN/dev that can be used for |
| 1187 | * coldplug of device nodes and other udev business. |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1188 | * We can do this only if gpiolib has been initialized. |
| 1189 | * Otherwise, defer until later. |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1190 | */ |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1191 | if (gpiolib_initialized) { |
| 1192 | status = gpiochip_setup_dev(gdev); |
| 1193 | if (status) |
| 1194 | goto err_remove_chip; |
| 1195 | } |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1196 | return 0; |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1197 | |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1198 | err_remove_chip: |
| 1199 | acpi_gpiochip_remove(chip); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 1200 | gpiochip_free_hogs(chip); |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1201 | of_gpiochip_remove(chip); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1202 | gpiochip_irqchip_free_valid_mask(chip); |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 1203 | err_remove_from_list: |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1204 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1205 | list_del(&gdev->list); |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1206 | spin_unlock_irqrestore(&gpio_lock, flags); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1207 | err_free_label: |
| 1208 | kfree(gdev->label); |
| 1209 | err_free_descs: |
| 1210 | kfree(gdev->descs); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1211 | err_free_gdev: |
| 1212 | ida_simple_remove(&gpio_ida, gdev->id); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1213 | /* failures here can mean systems won't boot... */ |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 1214 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1215 | gdev->base, gdev->base + gdev->ngpio - 1, |
| 1216 | chip->label ? : "generic"); |
| 1217 | kfree(gdev); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1218 | return status; |
| 1219 | } |
Linus Walleij | b08ea35 | 2015-12-03 15:14:13 +0100 | [diff] [blame] | 1220 | EXPORT_SYMBOL_GPL(gpiochip_add_data); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1221 | |
| 1222 | /** |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1223 | * gpiochip_get_data() - get per-subdriver data for the chip |
| 1224 | */ |
| 1225 | void *gpiochip_get_data(struct gpio_chip *chip) |
| 1226 | { |
| 1227 | return chip->gpiodev->data; |
| 1228 | } |
| 1229 | EXPORT_SYMBOL_GPL(gpiochip_get_data); |
| 1230 | |
| 1231 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1232 | * gpiochip_remove() - unregister a gpio_chip |
| 1233 | * @chip: the chip to unregister |
| 1234 | * |
| 1235 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 1236 | */ |
abdoulaye berthe | e1db170 | 2014-07-05 18:28:50 +0200 | [diff] [blame] | 1237 | void gpiochip_remove(struct gpio_chip *chip) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1238 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1239 | struct gpio_device *gdev = chip->gpiodev; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1240 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1241 | unsigned long flags; |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1242 | unsigned i; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1243 | bool requested = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1244 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1245 | /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ |
Linus Walleij | afbc4f3 | 2016-02-09 13:21:06 +0100 | [diff] [blame] | 1246 | gpiochip_sysfs_unregister(gdev); |
Bamvor Jian Zhang | bd203bd | 2016-02-20 13:13:19 +0800 | [diff] [blame] | 1247 | /* Numb the device, cancelling all outstanding operations */ |
| 1248 | gdev->chip = NULL; |
Johan Hovold | 00acc3d | 2015-01-12 17:12:27 +0100 | [diff] [blame] | 1249 | gpiochip_irqchip_remove(chip); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 1250 | acpi_gpiochip_remove(chip); |
Linus Walleij | 9ef0d6f | 2012-11-06 15:15:44 +0100 | [diff] [blame] | 1251 | gpiochip_remove_pin_ranges(chip); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 1252 | gpiochip_free_hogs(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1253 | of_gpiochip_remove(chip); |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1254 | /* |
| 1255 | * We accept no more calls into the driver from this point, so |
| 1256 | * NULL the driver data pointer |
| 1257 | */ |
| 1258 | gdev->data = NULL; |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1259 | |
Johan Hovold | 6798aca | 2015-01-12 17:12:28 +0100 | [diff] [blame] | 1260 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1261 | for (i = 0; i < gdev->ngpio; i++) { |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1262 | desc = &gdev->descs[i]; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1263 | if (test_bit(FLAG_REQUESTED, &desc->flags)) |
| 1264 | requested = true; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1265 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1266 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1267 | |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1268 | if (requested) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1269 | dev_crit(&gdev->dev, |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1270 | "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1271 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1272 | /* |
| 1273 | * The gpiochip side puts its use of the device to rest here: |
| 1274 | * if there are no userspace clients, the chardev and device will |
| 1275 | * be removed, else it will be dangling until the last user is |
| 1276 | * gone. |
| 1277 | */ |
Ricardo Ribalda Delgado | f4833b8 | 2016-06-03 19:10:02 +0200 | [diff] [blame] | 1278 | cdev_del(&gdev->chrdev); |
| 1279 | device_del(&gdev->dev); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1280 | put_device(&gdev->dev); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1281 | } |
| 1282 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 1283 | |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1284 | static void devm_gpio_chip_release(struct device *dev, void *res) |
| 1285 | { |
| 1286 | struct gpio_chip *chip = *(struct gpio_chip **)res; |
| 1287 | |
| 1288 | gpiochip_remove(chip); |
| 1289 | } |
| 1290 | |
| 1291 | static int devm_gpio_chip_match(struct device *dev, void *res, void *data) |
| 1292 | |
| 1293 | { |
| 1294 | struct gpio_chip **r = res; |
| 1295 | |
| 1296 | if (!r || !*r) { |
| 1297 | WARN_ON(!r || !*r); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | return *r == data; |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * devm_gpiochip_add_data() - Resource manager piochip_add_data() |
| 1306 | * @dev: the device pointer on which irq_chip belongs to. |
| 1307 | * @chip: the chip to register, with chip->base initialized |
| 1308 | * Context: potentially before irqs will work |
| 1309 | * |
| 1310 | * Returns a negative errno if the chip can't be registered, such as |
| 1311 | * because the chip->base is invalid or already associated with a |
| 1312 | * different chip. Otherwise it returns zero as a success code. |
| 1313 | * |
| 1314 | * The gpio chip automatically be released when the device is unbound. |
| 1315 | */ |
| 1316 | int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip, |
| 1317 | void *data) |
| 1318 | { |
| 1319 | struct gpio_chip **ptr; |
| 1320 | int ret; |
| 1321 | |
| 1322 | ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr), |
| 1323 | GFP_KERNEL); |
| 1324 | if (!ptr) |
| 1325 | return -ENOMEM; |
| 1326 | |
| 1327 | ret = gpiochip_add_data(chip, data); |
| 1328 | if (ret < 0) { |
| 1329 | devres_free(ptr); |
| 1330 | return ret; |
| 1331 | } |
| 1332 | |
| 1333 | *ptr = chip; |
| 1334 | devres_add(dev, ptr); |
| 1335 | |
| 1336 | return 0; |
| 1337 | } |
| 1338 | EXPORT_SYMBOL_GPL(devm_gpiochip_add_data); |
| 1339 | |
| 1340 | /** |
| 1341 | * devm_gpiochip_remove() - Resource manager of gpiochip_remove() |
| 1342 | * @dev: device for which which resource was allocated |
| 1343 | * @chip: the chip to remove |
| 1344 | * |
| 1345 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 1346 | */ |
| 1347 | void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip) |
| 1348 | { |
| 1349 | int ret; |
| 1350 | |
| 1351 | ret = devres_release(dev, devm_gpio_chip_release, |
| 1352 | devm_gpio_chip_match, chip); |
| 1353 | if (!ret) |
| 1354 | WARN_ON(ret); |
| 1355 | } |
| 1356 | EXPORT_SYMBOL_GPL(devm_gpiochip_remove); |
| 1357 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1358 | /** |
| 1359 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 1360 | * @data: data to pass to match function |
| 1361 | * @callback: Callback function to check gpio_chip |
| 1362 | * |
| 1363 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 1364 | * determined by a user supplied @match callback. The callback should return |
| 1365 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 1366 | * non-zero, this function will return to the caller and not iterate over any |
| 1367 | * more gpio_chips. |
| 1368 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 1369 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 1370 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 1371 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1372 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1373 | struct gpio_device *gdev; |
Masahiro Yamada | acf06ff | 2016-08-12 01:21:58 +0900 | [diff] [blame] | 1374 | struct gpio_chip *chip = NULL; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1375 | unsigned long flags; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1376 | |
| 1377 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1378 | list_for_each_entry(gdev, &gpio_devices, list) |
Masahiro Yamada | acf06ff | 2016-08-12 01:21:58 +0900 | [diff] [blame] | 1379 | if (gdev->chip && match(gdev->chip, data)) { |
| 1380 | chip = gdev->chip; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1381 | break; |
Masahiro Yamada | acf06ff | 2016-08-12 01:21:58 +0900 | [diff] [blame] | 1382 | } |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1383 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1384 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1385 | |
| 1386 | return chip; |
| 1387 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 1388 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1389 | |
Alexandre Courbot | 79697ef | 2013-11-16 21:39:32 +0900 | [diff] [blame] | 1390 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
| 1391 | { |
| 1392 | const char *name = data; |
| 1393 | |
| 1394 | return !strcmp(chip->label, name); |
| 1395 | } |
| 1396 | |
| 1397 | static struct gpio_chip *find_chip_by_name(const char *name) |
| 1398 | { |
| 1399 | return gpiochip_find((void *)name, gpiochip_match_name); |
| 1400 | } |
| 1401 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1402 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 1403 | |
| 1404 | /* |
| 1405 | * The following is irqchip helper code for gpiochips. |
| 1406 | */ |
| 1407 | |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1408 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
| 1409 | { |
| 1410 | int i; |
| 1411 | |
| 1412 | if (!gpiochip->irq_need_valid_mask) |
| 1413 | return 0; |
| 1414 | |
| 1415 | gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio), |
| 1416 | sizeof(long), GFP_KERNEL); |
| 1417 | if (!gpiochip->irq_valid_mask) |
| 1418 | return -ENOMEM; |
| 1419 | |
| 1420 | /* Assume by default all GPIOs are valid */ |
| 1421 | for (i = 0; i < gpiochip->ngpio; i++) |
| 1422 | set_bit(i, gpiochip->irq_valid_mask); |
| 1423 | |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
| 1427 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
| 1428 | { |
| 1429 | kfree(gpiochip->irq_valid_mask); |
| 1430 | gpiochip->irq_valid_mask = NULL; |
| 1431 | } |
| 1432 | |
| 1433 | static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, |
| 1434 | unsigned int offset) |
| 1435 | { |
| 1436 | /* No mask means all valid */ |
| 1437 | if (likely(!gpiochip->irq_valid_mask)) |
| 1438 | return true; |
| 1439 | return test_bit(offset, gpiochip->irq_valid_mask); |
| 1440 | } |
| 1441 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1442 | /** |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1443 | * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip |
| 1444 | * @gpiochip: the gpiochip to set the irqchip chain to |
| 1445 | * @irqchip: the irqchip to chain to the gpiochip |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1446 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 1447 | * chained irqchip |
| 1448 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1449 | * coming out of the gpiochip. If the interrupt is nested rather than |
| 1450 | * cascaded, pass NULL in this handler argument |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1451 | */ |
| 1452 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 1453 | struct irq_chip *irqchip, |
| 1454 | int parent_irq, |
| 1455 | irq_flow_handler_t parent_handler) |
| 1456 | { |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 1457 | unsigned int offset; |
| 1458 | |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 1459 | if (!gpiochip->irqdomain) { |
| 1460 | chip_err(gpiochip, "called %s before setting up irqchip\n", |
| 1461 | __func__); |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 1462 | return; |
| 1463 | } |
| 1464 | |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1465 | if (parent_handler) { |
| 1466 | if (gpiochip->can_sleep) { |
| 1467 | chip_err(gpiochip, |
| 1468 | "you cannot have chained interrupts on a " |
| 1469 | "chip that may sleep\n"); |
| 1470 | return; |
| 1471 | } |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1472 | /* |
| 1473 | * The parent irqchip is already using the chip_data for this |
| 1474 | * irqchip, so our callbacks simply use the handler_data. |
| 1475 | */ |
Thomas Gleixner | f7f8775 | 2015-06-21 21:10:48 +0200 | [diff] [blame] | 1476 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
| 1477 | gpiochip); |
Dmitry Eremin-Solenikov | 25e4fe9 | 2015-05-12 20:12:23 +0300 | [diff] [blame] | 1478 | |
| 1479 | gpiochip->irq_parent = parent_irq; |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1480 | } |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 1481 | |
| 1482 | /* Set the parent IRQ for all affected IRQs */ |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1483 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
| 1484 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
| 1485 | continue; |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 1486 | irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), |
| 1487 | parent_irq); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1488 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1489 | } |
| 1490 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
| 1491 | |
| 1492 | /** |
| 1493 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip |
| 1494 | * @d: the irqdomain used by this irqchip |
| 1495 | * @irq: the global irq number used by this GPIO irqchip irq |
| 1496 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip |
| 1497 | * |
| 1498 | * This function will set up the mapping for a certain IRQ line on a |
| 1499 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip |
| 1500 | * stored inside the gpiochip. |
| 1501 | */ |
| 1502 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
| 1503 | irq_hw_number_t hwirq) |
| 1504 | { |
| 1505 | struct gpio_chip *chip = d->host_data; |
| 1506 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1507 | irq_set_chip_data(irq, chip); |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 1508 | /* |
| 1509 | * This lock class tells lockdep that GPIO irqs are in a different |
| 1510 | * category than their parents, so it won't report false recursion. |
| 1511 | */ |
| 1512 | irq_set_lockdep_class(irq, chip->lock_key); |
Linus Walleij | 7633fb9 | 2014-04-09 13:20:38 +0200 | [diff] [blame] | 1513 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 1514 | /* Chips that can sleep need nested thread handlers */ |
Octavian Purdila | 295494a | 2014-09-19 23:22:44 +0300 | [diff] [blame] | 1515 | if (chip->can_sleep && !chip->irq_not_threaded) |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 1516 | irq_set_nested_thread(irq, 1); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1517 | irq_set_noprobe(irq); |
Rob Herring | 23393d4 | 2015-07-27 15:55:16 -0500 | [diff] [blame] | 1518 | |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 1519 | /* |
| 1520 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 1521 | * is passed as default type. |
| 1522 | */ |
| 1523 | if (chip->irq_default_type != IRQ_TYPE_NONE) |
| 1524 | irq_set_irq_type(irq, chip->irq_default_type); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1529 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
| 1530 | { |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 1531 | struct gpio_chip *chip = d->host_data; |
| 1532 | |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 1533 | if (chip->can_sleep) |
| 1534 | irq_set_nested_thread(irq, 0); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1535 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 1536 | irq_set_chip_data(irq, NULL); |
| 1537 | } |
| 1538 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1539 | static const struct irq_domain_ops gpiochip_domain_ops = { |
| 1540 | .map = gpiochip_irq_map, |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1541 | .unmap = gpiochip_irq_unmap, |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1542 | /* Virtually all GPIO irqchips are twocell:ed */ |
| 1543 | .xlate = irq_domain_xlate_twocell, |
| 1544 | }; |
| 1545 | |
| 1546 | static int gpiochip_irq_reqres(struct irq_data *d) |
| 1547 | { |
| 1548 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 1549 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1550 | if (!try_module_get(chip->gpiodev->owner)) |
Grygorii Strashko | 5b76e79 | 2015-06-25 20:30:50 +0300 | [diff] [blame] | 1551 | return -ENODEV; |
| 1552 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1553 | if (gpiochip_lock_as_irq(chip, d->hwirq)) { |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1554 | chip_err(chip, |
| 1555 | "unable to lock HW IRQ %lu for IRQ\n", |
| 1556 | d->hwirq); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1557 | module_put(chip->gpiodev->owner); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1558 | return -EINVAL; |
| 1559 | } |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
| 1563 | static void gpiochip_irq_relres(struct irq_data *d) |
| 1564 | { |
| 1565 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 1566 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 1567 | gpiochip_unlock_as_irq(chip, d->hwirq); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1568 | module_put(chip->gpiodev->owner); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
| 1572 | { |
| 1573 | return irq_find_mapping(chip->irqdomain, offset); |
| 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip |
| 1578 | * @gpiochip: the gpiochip to remove the irqchip from |
| 1579 | * |
| 1580 | * This is called only from gpiochip_remove() |
| 1581 | */ |
| 1582 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) |
| 1583 | { |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1584 | unsigned int offset; |
| 1585 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 1586 | acpi_gpiochip_free_interrupts(gpiochip); |
| 1587 | |
Dmitry Eremin-Solenikov | 25e4fe9 | 2015-05-12 20:12:23 +0300 | [diff] [blame] | 1588 | if (gpiochip->irq_parent) { |
| 1589 | irq_set_chained_handler(gpiochip->irq_parent, NULL); |
| 1590 | irq_set_handler_data(gpiochip->irq_parent, NULL); |
| 1591 | } |
| 1592 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1593 | /* Remove all IRQ mappings and delete the domain */ |
| 1594 | if (gpiochip->irqdomain) { |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1595 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
| 1596 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
| 1597 | continue; |
Grygorii Strashko | e389338 | 2014-09-25 19:09:23 +0300 | [diff] [blame] | 1598 | irq_dispose_mapping( |
| 1599 | irq_find_mapping(gpiochip->irqdomain, offset)); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1600 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1601 | irq_domain_remove(gpiochip->irqdomain); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1602 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1603 | |
| 1604 | if (gpiochip->irqchip) { |
| 1605 | gpiochip->irqchip->irq_request_resources = NULL; |
| 1606 | gpiochip->irqchip->irq_release_resources = NULL; |
| 1607 | gpiochip->irqchip = NULL; |
| 1608 | } |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1609 | |
| 1610 | gpiochip_irqchip_free_valid_mask(gpiochip); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * gpiochip_irqchip_add() - adds an irqchip to a gpiochip |
| 1615 | * @gpiochip: the gpiochip to add the irqchip to |
| 1616 | * @irqchip: the irqchip to add to the gpiochip |
| 1617 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 1618 | * allocate gpiochip irqs from |
| 1619 | * @handler: the irq handler to use (often a predefined irq core function) |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 1620 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 1621 | * 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] | 1622 | * @lock_key: lockdep class |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1623 | * |
| 1624 | * This function closely associates a certain irqchip with a certain |
| 1625 | * gpiochip, providing an irq domain to translate the local IRQs to |
| 1626 | * global irqs in the gpiolib core, and making sure that the gpiochip |
| 1627 | * is passed as chip data to all related functions. Driver callbacks |
Linus Walleij | 09dd5f9 | 2015-12-07 15:31:58 +0100 | [diff] [blame] | 1628 | * 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] | 1629 | * from the gpiochip passed as chip data. An irqdomain will be stored |
| 1630 | * in the gpiochip that shall be used by the driver to handle IRQ number |
| 1631 | * translation. The gpiochip will need to be initialized and registered |
| 1632 | * before calling this function. |
| 1633 | * |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1634 | * This function will handle two cell:ed simple IRQs and assumes all |
| 1635 | * the pins on the gpiochip can generate a unique IRQ. Everything else |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1636 | * need to be open coded. |
| 1637 | */ |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 1638 | int _gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
| 1639 | struct irq_chip *irqchip, |
| 1640 | unsigned int first_irq, |
| 1641 | irq_flow_handler_t handler, |
| 1642 | unsigned int type, |
| 1643 | struct lock_class_key *lock_key) |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1644 | { |
| 1645 | struct device_node *of_node; |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1646 | bool irq_base_set = false; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1647 | unsigned int offset; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1648 | unsigned irq_base = 0; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1649 | |
| 1650 | if (!gpiochip || !irqchip) |
| 1651 | return -EINVAL; |
| 1652 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1653 | if (!gpiochip->parent) { |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1654 | pr_err("missing gpiochip .dev parent pointer\n"); |
| 1655 | return -EINVAL; |
| 1656 | } |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1657 | of_node = gpiochip->parent->of_node; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1658 | #ifdef CONFIG_OF_GPIO |
| 1659 | /* |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 1660 | * If the gpiochip has an assigned OF node this takes precedence |
Bamvor Jian Zhang | c88402c | 2015-11-18 17:07:07 +0800 | [diff] [blame] | 1661 | * FIXME: get rid of this and use gpiochip->parent->of_node |
| 1662 | * everywhere |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1663 | */ |
| 1664 | if (gpiochip->of_node) |
| 1665 | of_node = gpiochip->of_node; |
| 1666 | #endif |
Marc Zyngier | 332e99d | 2016-09-07 09:12:11 +0100 | [diff] [blame] | 1667 | /* |
Mika Westerberg | 0a1e005 | 2016-09-12 14:29:51 +0300 | [diff] [blame] | 1668 | * Specifying a default trigger is a terrible idea if DT or ACPI is |
Marc Zyngier | 332e99d | 2016-09-07 09:12:11 +0100 | [diff] [blame] | 1669 | * used to configure the interrupts, as you may end-up with |
| 1670 | * conflicting triggers. Tell the user, and reset to NONE. |
| 1671 | */ |
| 1672 | if (WARN(of_node && type != IRQ_TYPE_NONE, |
| 1673 | "%s: Ignoring %d default trigger\n", of_node->full_name, type)) |
| 1674 | type = IRQ_TYPE_NONE; |
Mika Westerberg | 0a1e005 | 2016-09-12 14:29:51 +0300 | [diff] [blame] | 1675 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
| 1676 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), |
| 1677 | "Ignoring %d default trigger\n", type); |
| 1678 | type = IRQ_TYPE_NONE; |
| 1679 | } |
Marc Zyngier | 332e99d | 2016-09-07 09:12:11 +0100 | [diff] [blame] | 1680 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1681 | gpiochip->irqchip = irqchip; |
| 1682 | gpiochip->irq_handler = handler; |
| 1683 | gpiochip->irq_default_type = type; |
| 1684 | gpiochip->to_irq = gpiochip_to_irq; |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 1685 | gpiochip->lock_key = lock_key; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1686 | gpiochip->irqdomain = irq_domain_add_simple(of_node, |
| 1687 | gpiochip->ngpio, first_irq, |
| 1688 | &gpiochip_domain_ops, gpiochip); |
| 1689 | if (!gpiochip->irqdomain) { |
| 1690 | gpiochip->irqchip = NULL; |
| 1691 | return -EINVAL; |
| 1692 | } |
Rabin Vincent | 8b67a1f | 2015-07-31 14:48:56 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* |
| 1695 | * It is possible for a driver to override this, but only if the |
| 1696 | * alternative functions are both implemented. |
| 1697 | */ |
| 1698 | if (!irqchip->irq_request_resources && |
| 1699 | !irqchip->irq_release_resources) { |
| 1700 | irqchip->irq_request_resources = gpiochip_irq_reqres; |
| 1701 | irqchip->irq_release_resources = gpiochip_irq_relres; |
| 1702 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1703 | |
| 1704 | /* |
| 1705 | * Prepare the mapping since the irqchip shall be orthogonal to |
| 1706 | * any gpiochip calls. If the first_irq was zero, this is |
| 1707 | * necessary to allocate descriptors for all IRQs. |
| 1708 | */ |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1709 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1710 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
| 1711 | continue; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1712 | irq_base = irq_create_mapping(gpiochip->irqdomain, offset); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1713 | if (!irq_base_set) { |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1714 | /* |
| 1715 | * Store the base into the gpiochip to be used when |
| 1716 | * unmapping the irqs. |
| 1717 | */ |
| 1718 | gpiochip->irq_base = irq_base; |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1719 | irq_base_set = true; |
| 1720 | } |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 1721 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1722 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 1723 | acpi_gpiochip_request_interrupts(gpiochip); |
| 1724 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1725 | return 0; |
| 1726 | } |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 1727 | EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1728 | |
| 1729 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
| 1730 | |
| 1731 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1732 | static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
| 1733 | { |
| 1734 | return 0; |
| 1735 | } |
| 1736 | static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
| 1737 | { } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1738 | |
| 1739 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
| 1740 | |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 1741 | /** |
| 1742 | * gpiochip_generic_request() - request the gpio function for a pin |
| 1743 | * @chip: the gpiochip owning the GPIO |
| 1744 | * @offset: the offset of the GPIO to request for GPIO function |
| 1745 | */ |
| 1746 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) |
| 1747 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1748 | return pinctrl_request_gpio(chip->gpiodev->base + offset); |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 1749 | } |
| 1750 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); |
| 1751 | |
| 1752 | /** |
| 1753 | * gpiochip_generic_free() - free the gpio function from a pin |
| 1754 | * @chip: the gpiochip to request the gpio function for |
| 1755 | * @offset: the offset of the GPIO to free from GPIO function |
| 1756 | */ |
| 1757 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) |
| 1758 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1759 | pinctrl_free_gpio(chip->gpiodev->base + offset); |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 1760 | } |
| 1761 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); |
| 1762 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1763 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1764 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1765 | /** |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1766 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 1767 | * @chip: the gpiochip to add the range for |
Tomeu Vizoso | d32651f | 2015-06-17 15:42:11 +0200 | [diff] [blame] | 1768 | * @pctldev: the pin controller to map to |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1769 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 1770 | * @pin_group: name of the pin group inside the pin controller |
| 1771 | */ |
| 1772 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 1773 | struct pinctrl_dev *pctldev, |
| 1774 | unsigned int gpio_offset, const char *pin_group) |
| 1775 | { |
| 1776 | struct gpio_pin_range *pin_range; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1777 | struct gpio_device *gdev = chip->gpiodev; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1778 | int ret; |
| 1779 | |
| 1780 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 1781 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 1782 | chip_err(chip, "failed to allocate pin ranges\n"); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1783 | return -ENOMEM; |
| 1784 | } |
| 1785 | |
| 1786 | /* Use local offset as range ID */ |
| 1787 | pin_range->range.id = gpio_offset; |
| 1788 | pin_range->range.gc = chip; |
| 1789 | pin_range->range.name = chip->label; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1790 | pin_range->range.base = gdev->base + gpio_offset; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1791 | pin_range->pctldev = pctldev; |
| 1792 | |
| 1793 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 1794 | &pin_range->range.pins, |
| 1795 | &pin_range->range.npins); |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 1796 | if (ret < 0) { |
| 1797 | kfree(pin_range); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1798 | return ret; |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 1799 | } |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1800 | |
| 1801 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 1802 | |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 1803 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 1804 | gpio_offset, gpio_offset + pin_range->range.npins - 1, |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1805 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 1806 | |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 1807 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1808 | |
| 1809 | return 0; |
| 1810 | } |
| 1811 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 1812 | |
| 1813 | /** |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1814 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 1815 | * @chip: the gpiochip to add the range for |
| 1816 | * @pinctrl_name: the dev_name() of the pin controller to map to |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1817 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 1818 | * @pin_offset: the start offset in the pin controller number space |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1819 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 1820 | * pin controller) to accumulate in this range |
| 1821 | */ |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1822 | 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] | 1823 | unsigned int gpio_offset, unsigned int pin_offset, |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1824 | unsigned int npins) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1825 | { |
| 1826 | struct gpio_pin_range *pin_range; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1827 | struct gpio_device *gdev = chip->gpiodev; |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1828 | int ret; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1829 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1830 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1831 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 1832 | chip_err(chip, "failed to allocate pin ranges\n"); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1833 | return -ENOMEM; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1834 | } |
| 1835 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1836 | /* Use local offset as range ID */ |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1837 | pin_range->range.id = gpio_offset; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1838 | pin_range->range.gc = chip; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1839 | pin_range->range.name = chip->label; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1840 | pin_range->range.base = gdev->base + gpio_offset; |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1841 | pin_range->range.pin_base = pin_offset; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1842 | pin_range->range.npins = npins; |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 1843 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1844 | &pin_range->range); |
Linus Walleij | 8f23ca1 | 2012-11-20 14:56:25 +0100 | [diff] [blame] | 1845 | if (IS_ERR(pin_range->pctldev)) { |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1846 | ret = PTR_ERR(pin_range->pctldev); |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 1847 | chip_err(chip, "could not create pin range\n"); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1848 | kfree(pin_range); |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1849 | return ret; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1850 | } |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 1851 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 1852 | gpio_offset, gpio_offset + npins - 1, |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1853 | pinctl_name, |
| 1854 | pin_offset, pin_offset + npins - 1); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1855 | |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 1856 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1857 | |
| 1858 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1859 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1860 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1861 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1862 | /** |
| 1863 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 1864 | * @chip: the chip to remove all the mappings for |
| 1865 | */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1866 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 1867 | { |
| 1868 | struct gpio_pin_range *pin_range, *tmp; |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 1869 | struct gpio_device *gdev = chip->gpiodev; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1870 | |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 1871 | list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1872 | list_del(&pin_range->node); |
| 1873 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 1874 | &pin_range->range); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1875 | kfree(pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1876 | } |
| 1877 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1878 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 1879 | |
| 1880 | #endif /* CONFIG_PINCTRL */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1881 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1882 | /* These "optional" allocation calls help prevent drivers from stomping |
| 1883 | * on each other, and help provide better diagnostics in debugfs. |
| 1884 | * They're called even less than the "set direction" calls. |
| 1885 | */ |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1886 | static int __gpiod_request(struct gpio_desc *desc, const char *label) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1887 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1888 | struct gpio_chip *chip = desc->gdev->chip; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1889 | int status; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1890 | unsigned long flags; |
| 1891 | |
| 1892 | spin_lock_irqsave(&gpio_lock, flags); |
| 1893 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1894 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1895 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1896 | */ |
| 1897 | |
| 1898 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 1899 | desc_set_label(desc, label ? : "?"); |
| 1900 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1901 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1902 | status = -EBUSY; |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 1903 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | if (chip->request) { |
| 1907 | /* chip->request may sleep */ |
| 1908 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1909 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1910 | spin_lock_irqsave(&gpio_lock, flags); |
| 1911 | |
| 1912 | if (status < 0) { |
| 1913 | desc_set_label(desc, NULL); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1914 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1915 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1916 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1917 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1918 | if (chip->get_direction) { |
| 1919 | /* chip->get_direction may sleep */ |
| 1920 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1921 | gpiod_get_direction(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1922 | spin_lock_irqsave(&gpio_lock, flags); |
| 1923 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1924 | done: |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1925 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1926 | return status; |
| 1927 | } |
| 1928 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1929 | /* |
| 1930 | * This descriptor validation needs to be inserted verbatim into each |
| 1931 | * function taking a descriptor, so we need to use a preprocessor |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 1932 | * macro to avoid endless duplication. If the desc is NULL it is an |
| 1933 | * optional GPIO and calls should just bail out. |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1934 | */ |
| 1935 | #define VALIDATE_DESC(desc) do { \ |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 1936 | if (!desc) \ |
| 1937 | return 0; \ |
Linus Walleij | bfbbe44 | 2016-06-16 11:55:55 +0200 | [diff] [blame] | 1938 | if (IS_ERR(desc)) { \ |
| 1939 | pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \ |
| 1940 | return PTR_ERR(desc); \ |
| 1941 | } \ |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 1942 | if (!desc->gdev) { \ |
Linus Walleij | bfbbe44 | 2016-06-16 11:55:55 +0200 | [diff] [blame] | 1943 | pr_warn("%s: invalid GPIO (no device)\n", __func__); \ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1944 | return -EINVAL; \ |
| 1945 | } \ |
| 1946 | if ( !desc->gdev->chip ) { \ |
| 1947 | dev_warn(&desc->gdev->dev, \ |
| 1948 | "%s: backing chip is gone\n", __func__); \ |
| 1949 | return 0; \ |
| 1950 | } } while (0) |
| 1951 | |
| 1952 | #define VALIDATE_DESC_VOID(desc) do { \ |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 1953 | if (!desc) \ |
| 1954 | return; \ |
Linus Walleij | bfbbe44 | 2016-06-16 11:55:55 +0200 | [diff] [blame] | 1955 | if (IS_ERR(desc)) { \ |
| 1956 | pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \ |
| 1957 | return; \ |
| 1958 | } \ |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 1959 | if (!desc->gdev) { \ |
Linus Walleij | bfbbe44 | 2016-06-16 11:55:55 +0200 | [diff] [blame] | 1960 | pr_warn("%s: invalid GPIO (no device)\n", __func__); \ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1961 | return; \ |
| 1962 | } \ |
| 1963 | if (!desc->gdev->chip) { \ |
| 1964 | dev_warn(&desc->gdev->dev, \ |
| 1965 | "%s: backing chip is gone\n", __func__); \ |
| 1966 | return; \ |
| 1967 | } } while (0) |
| 1968 | |
| 1969 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 1970 | int gpiod_request(struct gpio_desc *desc, const char *label) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1971 | { |
| 1972 | int status = -EPROBE_DEFER; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1973 | struct gpio_device *gdev; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1974 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1975 | VALIDATE_DESC(desc); |
| 1976 | gdev = desc->gdev; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1977 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1978 | if (try_module_get(gdev->owner)) { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1979 | status = __gpiod_request(desc, label); |
| 1980 | if (status < 0) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1981 | module_put(gdev->owner); |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 1982 | else |
| 1983 | get_device(&gdev->dev); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1986 | if (status) |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 1987 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1988 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1989 | return status; |
| 1990 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1991 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1992 | static bool __gpiod_free(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1993 | { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 1994 | bool ret = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1995 | unsigned long flags; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1996 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1997 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 1998 | might_sleep(); |
| 1999 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2000 | gpiod_unexport(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 2001 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2002 | spin_lock_irqsave(&gpio_lock, flags); |
| 2003 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2004 | chip = desc->gdev->chip; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2005 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 2006 | if (chip->free) { |
| 2007 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 2008 | might_sleep_if(chip->can_sleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2009 | chip->free(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2010 | spin_lock_irqsave(&gpio_lock, flags); |
| 2011 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2012 | desc_set_label(desc, NULL); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 2013 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2014 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2015 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2016 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2017 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2018 | ret = true; |
| 2019 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2020 | |
| 2021 | spin_unlock_irqrestore(&gpio_lock, flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2022 | return ret; |
| 2023 | } |
| 2024 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 2025 | void gpiod_free(struct gpio_desc *desc) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2026 | { |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 2027 | if (desc && desc->gdev && __gpiod_free(desc)) { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2028 | module_put(desc->gdev->owner); |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 2029 | put_device(&desc->gdev->dev); |
| 2030 | } else { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2031 | WARN_ON(extra_checks); |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 2032 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2033 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2034 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2035 | /** |
| 2036 | * gpiochip_is_requested - return string iff signal was requested |
| 2037 | * @chip: controller managing the signal |
| 2038 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 2039 | * |
| 2040 | * Returns NULL if the GPIO is not currently requested, else a string. |
Alexandre Courbot | 9c8318f | 2014-07-01 14:45:14 +0900 | [diff] [blame] | 2041 | * The string returned is the label passed to gpio_request(); if none has been |
| 2042 | * passed it is a meaningless, non-NULL constant. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2043 | * |
| 2044 | * This function is for use by GPIO controller drivers. The label can |
| 2045 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 2046 | * can help avoid accidentally multiplexing it to another controller. |
| 2047 | */ |
| 2048 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 2049 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2050 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2051 | |
Dirk Behme | 48b5953 | 2015-08-18 18:02:32 +0200 | [diff] [blame] | 2052 | if (offset >= chip->ngpio) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2053 | return NULL; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2054 | |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 2055 | desc = &chip->gpiodev->descs[offset]; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2056 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2057 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2058 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2059 | return desc->label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2060 | } |
| 2061 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 2062 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2063 | /** |
| 2064 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor |
| 2065 | * @desc: GPIO descriptor to request |
| 2066 | * @label: label for the GPIO |
| 2067 | * |
| 2068 | * Function allows GPIO chip drivers to request and use their own GPIO |
| 2069 | * descriptors via gpiolib API. Difference to gpiod_request() is that this |
| 2070 | * function will not increase reference count of the GPIO chip module. This |
| 2071 | * allows the GPIO chip module to be unloaded as needed (we assume that the |
| 2072 | * GPIO chip driver handles freeing the GPIOs it has requested). |
| 2073 | */ |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2074 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
| 2075 | const char *label) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2076 | { |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2077 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
| 2078 | int err; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2079 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2080 | if (IS_ERR(desc)) { |
| 2081 | chip_err(chip, "failed to get GPIO descriptor\n"); |
| 2082 | return desc; |
| 2083 | } |
| 2084 | |
| 2085 | err = __gpiod_request(desc, label); |
| 2086 | if (err < 0) |
| 2087 | return ERR_PTR(err); |
| 2088 | |
| 2089 | return desc; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2090 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 2091 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2092 | |
| 2093 | /** |
| 2094 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver |
| 2095 | * @desc: GPIO descriptor to free |
| 2096 | * |
| 2097 | * Function frees the given GPIO requested previously with |
| 2098 | * gpiochip_request_own_desc(). |
| 2099 | */ |
| 2100 | void gpiochip_free_own_desc(struct gpio_desc *desc) |
| 2101 | { |
| 2102 | if (desc) |
| 2103 | __gpiod_free(desc); |
| 2104 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 2105 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2106 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2107 | /* |
| 2108 | * Drivers MUST set GPIO direction before making get/set calls. In |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2109 | * some cases this is done in early boot, before IRQs are enabled. |
| 2110 | * |
| 2111 | * As a rule these aren't called more than once (except for drivers |
| 2112 | * using the open-drain emulation idiom) so these are natural places |
| 2113 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 2114 | * rely on gpio_request() having been called beforehand. |
| 2115 | */ |
| 2116 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2117 | /** |
| 2118 | * gpiod_direction_input - set the GPIO direction to input |
| 2119 | * @desc: GPIO to set to input |
| 2120 | * |
| 2121 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 2122 | * be called safely on it. |
| 2123 | * |
| 2124 | * Return 0 in case of success, else an error code. |
| 2125 | */ |
| 2126 | int gpiod_direction_input(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2127 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2128 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2129 | int status = -EINVAL; |
| 2130 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2131 | VALIDATE_DESC(desc); |
| 2132 | chip = desc->gdev->chip; |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2133 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 2134 | if (!chip->get || !chip->direction_input) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2135 | gpiod_warn(desc, |
| 2136 | "%s: missing get() or direction_input() operations\n", |
Andy Shevchenko | 7589e59 | 2013-12-05 11:26:23 +0200 | [diff] [blame] | 2137 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 2138 | return -EIO; |
| 2139 | } |
| 2140 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 2141 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2142 | if (status == 0) |
| 2143 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 2144 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2145 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 2146 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2147 | return status; |
| 2148 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2149 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2150 | |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 2151 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2152 | { |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2153 | struct gpio_chip *gc = desc->gdev->chip; |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 2154 | int val = !!value; |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2155 | int ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2156 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2157 | /* GPIOs used for IRQs shall not be set as output */ |
| 2158 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
| 2159 | gpiod_err(desc, |
| 2160 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 2161 | __func__); |
| 2162 | return -EIO; |
| 2163 | } |
| 2164 | |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2165 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
| 2166 | /* First see if we can enable open drain in hardware */ |
| 2167 | if (gc->set_single_ended) { |
| 2168 | ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc), |
| 2169 | LINE_MODE_OPEN_DRAIN); |
| 2170 | if (!ret) |
| 2171 | goto set_output_value; |
| 2172 | } |
| 2173 | /* Emulate open drain by not actively driving the line high */ |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 2174 | if (val) |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2175 | return gpiod_direction_input(desc); |
| 2176 | } |
| 2177 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
| 2178 | if (gc->set_single_ended) { |
| 2179 | ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc), |
| 2180 | LINE_MODE_OPEN_SOURCE); |
| 2181 | if (!ret) |
| 2182 | goto set_output_value; |
| 2183 | } |
| 2184 | /* Emulate open source by not actively driving the line low */ |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 2185 | if (!val) |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2186 | return gpiod_direction_input(desc); |
| 2187 | } else { |
| 2188 | /* Make sure to disable open drain/source hardware, if any */ |
| 2189 | if (gc->set_single_ended) |
| 2190 | gc->set_single_ended(gc, |
| 2191 | gpio_chip_hwgpio(desc), |
| 2192 | LINE_MODE_PUSH_PULL); |
| 2193 | } |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2194 | |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2195 | set_output_value: |
| 2196 | if (!gc->set || !gc->direction_output) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2197 | gpiod_warn(desc, |
| 2198 | "%s: missing set() or direction_output() operations\n", |
| 2199 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 2200 | return -EIO; |
| 2201 | } |
| 2202 | |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 2203 | ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2204 | if (!ret) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2205 | set_bit(FLAG_IS_OUT, &desc->flags); |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 2206 | trace_gpio_value(desc_to_gpio(desc), 0, val); |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 2207 | trace_gpio_direction(desc_to_gpio(desc), 0, ret); |
| 2208 | return ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2209 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 2210 | |
| 2211 | /** |
| 2212 | * gpiod_direction_output_raw - set the GPIO direction to output |
| 2213 | * @desc: GPIO to set to output |
| 2214 | * @value: initial output value of the GPIO |
| 2215 | * |
| 2216 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 2217 | * be called safely on it. The initial value of the output must be specified |
| 2218 | * as raw value on the physical line without regard for the ACTIVE_LOW status. |
| 2219 | * |
| 2220 | * Return 0 in case of success, else an error code. |
| 2221 | */ |
| 2222 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 2223 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2224 | VALIDATE_DESC(desc); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 2225 | return _gpiod_direction_output_raw(desc, value); |
| 2226 | } |
| 2227 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); |
| 2228 | |
| 2229 | /** |
Rahul Bedarkar | 90df4fe | 2014-02-08 11:55:25 +0530 | [diff] [blame] | 2230 | * gpiod_direction_output - set the GPIO direction to output |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 2231 | * @desc: GPIO to set to output |
| 2232 | * @value: initial output value of the GPIO |
| 2233 | * |
| 2234 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 2235 | * be called safely on it. The initial value of the output must be specified |
| 2236 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2237 | * account. |
| 2238 | * |
| 2239 | * Return 0 in case of success, else an error code. |
| 2240 | */ |
| 2241 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 2242 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2243 | VALIDATE_DESC(desc); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 2244 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2245 | value = !value; |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 2246 | else |
| 2247 | value = !!value; |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 2248 | return _gpiod_direction_output_raw(desc, value); |
| 2249 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2250 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2251 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2252 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2253 | * gpiod_set_debounce - sets @debounce time for a @gpio |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2254 | * @gpio: the gpio to set debounce time |
| 2255 | * @debounce: debounce time is microseconds |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 2256 | * |
| 2257 | * returns -ENOTSUPP if the controller does not support setting |
| 2258 | * debounce. |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2259 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2260 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2261 | { |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2262 | struct gpio_chip *chip; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2263 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2264 | VALIDATE_DESC(desc); |
| 2265 | chip = desc->gdev->chip; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 2266 | if (!chip->set || !chip->set_debounce) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2267 | gpiod_dbg(desc, |
| 2268 | "%s: missing set() or set_debounce() operations\n", |
| 2269 | __func__); |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 2270 | return -ENOTSUPP; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 2271 | } |
| 2272 | |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 2273 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 2274 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2275 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2276 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2277 | /** |
| 2278 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 2279 | * @desc: the gpio descriptor to test |
| 2280 | * |
| 2281 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 2282 | */ |
| 2283 | int gpiod_is_active_low(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2284 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2285 | VALIDATE_DESC(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2286 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2287 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2288 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2289 | |
| 2290 | /* I/O calls are only valid after configuration completed; the relevant |
| 2291 | * "is this a valid GPIO" error checks should already have been done. |
| 2292 | * |
| 2293 | * "Get" operations are often inlinable as reading a pin value register, |
| 2294 | * and masking the relevant bit in that register. |
| 2295 | * |
| 2296 | * When "set" operations are inlinable, they involve writing that mask to |
| 2297 | * one register to set a low value, or a different register to set it high. |
| 2298 | * Otherwise locking is needed, so there may be little value to inlining. |
| 2299 | * |
| 2300 | *------------------------------------------------------------------------ |
| 2301 | * |
| 2302 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 2303 | * have requested the GPIO. That can include implicit requesting by |
| 2304 | * a direction setting call. Marking a gpio as requested locks its chip |
| 2305 | * in memory, guaranteeing that these table lookups need no more locking |
| 2306 | * and that gpiochip_remove() will fail. |
| 2307 | * |
| 2308 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 2309 | * that the GPIO was actually requested. |
| 2310 | */ |
| 2311 | |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 2312 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2313 | { |
| 2314 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2315 | int offset; |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 2316 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2317 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2318 | chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2319 | offset = gpio_chip_hwgpio(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 2320 | value = chip->get ? chip->get(chip, offset) : -EIO; |
Linus Walleij | 723a630 | 2015-12-21 23:10:12 +0100 | [diff] [blame] | 2321 | value = value < 0 ? value : !!value; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2322 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 2323 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2324 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2325 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2326 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2327 | * gpiod_get_raw_value() - return a gpio's raw value |
| 2328 | * @desc: gpio whose value will be returned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2329 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2330 | * 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] | 2331 | * its ACTIVE_LOW status, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2332 | * |
| 2333 | * This function should be called from contexts where we cannot sleep, and will |
| 2334 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2335 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2336 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2337 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2338 | VALIDATE_DESC(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2339 | /* Should be using gpio_get_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2340 | WARN_ON(desc->gdev->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2341 | return _gpiod_get_raw_value(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2342 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2343 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2344 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2345 | /** |
| 2346 | * gpiod_get_value() - return a gpio's value |
| 2347 | * @desc: gpio whose value will be returned |
| 2348 | * |
| 2349 | * 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] | 2350 | * account, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2351 | * |
| 2352 | * This function should be called from contexts where we cannot sleep, and will |
| 2353 | * complain if the GPIO chip functions potentially sleep. |
| 2354 | */ |
| 2355 | int gpiod_get_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2356 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2357 | int value; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2358 | |
| 2359 | VALIDATE_DESC(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2360 | /* Should be using gpio_get_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2361 | WARN_ON(desc->gdev->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2362 | |
| 2363 | value = _gpiod_get_raw_value(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 2364 | if (value < 0) |
| 2365 | return value; |
| 2366 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2367 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2368 | value = !value; |
| 2369 | |
| 2370 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2371 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2372 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2373 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2374 | /* |
| 2375 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2376 | * @desc: gpio descriptor whose state need to be set. |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 2377 | * @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] | 2378 | */ |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 2379 | 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] | 2380 | { |
| 2381 | int err = 0; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2382 | struct gpio_chip *chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2383 | int offset = gpio_chip_hwgpio(desc); |
| 2384 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2385 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2386 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2387 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2388 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2389 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2390 | err = chip->direction_output(chip, offset, 0); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2391 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2392 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2393 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2394 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2395 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2396 | gpiod_err(desc, |
| 2397 | "%s: Error in set_value for open drain err %d\n", |
| 2398 | __func__, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2399 | } |
| 2400 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2401 | /* |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2402 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 2403 | * @desc: gpio descriptor whose state need to be set. |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 2404 | * @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] | 2405 | */ |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 2406 | 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] | 2407 | { |
| 2408 | int err = 0; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2409 | struct gpio_chip *chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2410 | int offset = gpio_chip_hwgpio(desc); |
| 2411 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2412 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2413 | err = chip->direction_output(chip, offset, 1); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2414 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2415 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2416 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2417 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2418 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2419 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2420 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2421 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2422 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2423 | gpiod_err(desc, |
| 2424 | "%s: Error in set_value for open source err %d\n", |
| 2425 | __func__, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2426 | } |
| 2427 | |
Alexandre Courbot | 2360096 | 2014-03-11 15:52:09 +0900 | [diff] [blame] | 2428 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2429 | { |
| 2430 | struct gpio_chip *chip; |
| 2431 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2432 | chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2433 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 2434 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 2435 | _gpio_set_open_drain_value(desc, value); |
| 2436 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 2437 | _gpio_set_open_source_value(desc, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2438 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2439 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
| 2440 | } |
| 2441 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2442 | /* |
| 2443 | * set multiple outputs on the same chip; |
| 2444 | * use the chip's set_multiple function if available; |
| 2445 | * otherwise set the outputs sequentially; |
| 2446 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word |
| 2447 | * defines which outputs are to be changed |
| 2448 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word |
| 2449 | * defines the values the outputs specified by mask are to be set to |
| 2450 | */ |
| 2451 | static void gpio_chip_set_multiple(struct gpio_chip *chip, |
| 2452 | unsigned long *mask, unsigned long *bits) |
| 2453 | { |
| 2454 | if (chip->set_multiple) { |
| 2455 | chip->set_multiple(chip, mask, bits); |
| 2456 | } else { |
| 2457 | int i; |
| 2458 | for (i = 0; i < chip->ngpio; i++) { |
| 2459 | if (mask[BIT_WORD(i)] == 0) { |
| 2460 | /* no more set bits in this mask word; |
| 2461 | * skip ahead to the next word */ |
| 2462 | i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1; |
| 2463 | continue; |
| 2464 | } |
| 2465 | /* set outputs if the corresponding mask bit is set */ |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2466 | if (__test_and_clear_bit(i, mask)) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2467 | chip->set(chip, i, test_bit(i, bits)); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | |
Linus Walleij | 44c7288 | 2016-04-24 11:36:59 +0200 | [diff] [blame] | 2472 | void gpiod_set_array_value_complex(bool raw, bool can_sleep, |
| 2473 | unsigned int array_size, |
| 2474 | struct gpio_desc **desc_array, |
| 2475 | int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2476 | { |
| 2477 | int i = 0; |
| 2478 | |
| 2479 | while (i < array_size) { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2480 | struct gpio_chip *chip = desc_array[i]->gdev->chip; |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2481 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; |
| 2482 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; |
| 2483 | int count = 0; |
| 2484 | |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2485 | if (!can_sleep) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2486 | WARN_ON(chip->can_sleep); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2487 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2488 | memset(mask, 0, sizeof(mask)); |
| 2489 | do { |
| 2490 | struct gpio_desc *desc = desc_array[i]; |
| 2491 | int hwgpio = gpio_chip_hwgpio(desc); |
| 2492 | int value = value_array[i]; |
| 2493 | |
| 2494 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2495 | value = !value; |
| 2496 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 2497 | /* |
| 2498 | * collect all normal outputs belonging to the same chip |
| 2499 | * open drain and open source outputs are set individually |
| 2500 | */ |
| 2501 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2502 | _gpio_set_open_drain_value(desc, value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2503 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
| 2504 | _gpio_set_open_source_value(desc, value); |
| 2505 | } else { |
| 2506 | __set_bit(hwgpio, mask); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2507 | if (value) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2508 | __set_bit(hwgpio, bits); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2509 | else |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2510 | __clear_bit(hwgpio, bits); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2511 | count++; |
| 2512 | } |
| 2513 | i++; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2514 | } while ((i < array_size) && |
| 2515 | (desc_array[i]->gdev->chip == chip)); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2516 | /* push collected bits to outputs */ |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 2517 | if (count != 0) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2518 | gpio_chip_set_multiple(chip, mask, bits); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2522 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2523 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 2524 | * @desc: gpio whose value will be assigned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2525 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2526 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2527 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2528 | * regard for its ACTIVE_LOW status. |
| 2529 | * |
| 2530 | * This function should be called from contexts where we cannot sleep, and will |
| 2531 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2532 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2533 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2534 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2535 | VALIDATE_DESC_VOID(desc); |
Geert Uytterhoeven | 1cfab8f | 2016-03-14 16:24:12 +0100 | [diff] [blame] | 2536 | /* Should be using gpiod_set_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2537 | WARN_ON(desc->gdev->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2538 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2539 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2540 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2541 | |
| 2542 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2543 | * gpiod_set_value() - assign a gpio's value |
| 2544 | * @desc: gpio whose value will be assigned |
| 2545 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2546 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2547 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2548 | * account |
| 2549 | * |
| 2550 | * This function should be called from contexts where we cannot sleep, and will |
| 2551 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2552 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2553 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 2554 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2555 | VALIDATE_DESC_VOID(desc); |
Geert Uytterhoeven | 1cfab8f | 2016-03-14 16:24:12 +0100 | [diff] [blame] | 2556 | /* Should be using gpiod_set_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2557 | WARN_ON(desc->gdev->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2558 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2559 | value = !value; |
| 2560 | _gpiod_set_raw_value(desc, value); |
| 2561 | } |
| 2562 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 2563 | |
| 2564 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2565 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2566 | * @array_size: number of elements in the descriptor / value arrays |
| 2567 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 2568 | * @value_array: array of values to assign |
| 2569 | * |
| 2570 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 2571 | * without regard for their ACTIVE_LOW status. |
| 2572 | * |
| 2573 | * This function should be called from contexts where we cannot sleep, and will |
| 2574 | * complain if the GPIO chip functions potentially sleep. |
| 2575 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2576 | void gpiod_set_raw_array_value(unsigned int array_size, |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2577 | struct gpio_desc **desc_array, int *value_array) |
| 2578 | { |
| 2579 | if (!desc_array) |
| 2580 | return; |
Linus Walleij | 44c7288 | 2016-04-24 11:36:59 +0200 | [diff] [blame] | 2581 | gpiod_set_array_value_complex(true, false, array_size, desc_array, |
| 2582 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2583 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2584 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2585 | |
| 2586 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2587 | * gpiod_set_array_value() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2588 | * @array_size: number of elements in the descriptor / value arrays |
| 2589 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 2590 | * @value_array: array of values to assign |
| 2591 | * |
| 2592 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 2593 | * into account. |
| 2594 | * |
| 2595 | * This function should be called from contexts where we cannot sleep, and will |
| 2596 | * complain if the GPIO chip functions potentially sleep. |
| 2597 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2598 | void gpiod_set_array_value(unsigned int array_size, |
| 2599 | struct gpio_desc **desc_array, int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2600 | { |
| 2601 | if (!desc_array) |
| 2602 | return; |
Linus Walleij | 44c7288 | 2016-04-24 11:36:59 +0200 | [diff] [blame] | 2603 | gpiod_set_array_value_complex(false, false, array_size, desc_array, |
| 2604 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2605 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2606 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2607 | |
| 2608 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2609 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 2610 | * @desc: gpio to check |
| 2611 | * |
| 2612 | */ |
| 2613 | int gpiod_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2614 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2615 | VALIDATE_DESC(desc); |
| 2616 | return desc->gdev->chip->can_sleep; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2617 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2618 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2619 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2620 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2621 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 2622 | * @desc: gpio whose IRQ will be returned (already requested) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2623 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2624 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 2625 | * error. |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2626 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2627 | int gpiod_to_irq(const struct gpio_desc *desc) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2628 | { |
Linus Walleij | 4c37ce8 | 2016-05-02 13:13:10 +0200 | [diff] [blame] | 2629 | struct gpio_chip *chip; |
| 2630 | int offset; |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2631 | |
Linus Walleij | 79bb71b | 2016-06-15 22:57:38 +0200 | [diff] [blame] | 2632 | /* |
| 2633 | * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics |
| 2634 | * requires this function to not return zero on an invalid descriptor |
| 2635 | * but rather a negative error number. |
| 2636 | */ |
Linus Walleij | bfbbe44 | 2016-06-16 11:55:55 +0200 | [diff] [blame] | 2637 | if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) |
Linus Walleij | 79bb71b | 2016-06-15 22:57:38 +0200 | [diff] [blame] | 2638 | return -EINVAL; |
| 2639 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2640 | chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2641 | offset = gpio_chip_hwgpio(desc); |
Linus Walleij | 4c37ce8 | 2016-05-02 13:13:10 +0200 | [diff] [blame] | 2642 | if (chip->to_irq) { |
| 2643 | int retirq = chip->to_irq(chip, offset); |
| 2644 | |
| 2645 | /* Zero means NO_IRQ */ |
| 2646 | if (!retirq) |
| 2647 | return -ENXIO; |
| 2648 | |
| 2649 | return retirq; |
| 2650 | } |
| 2651 | return -ENXIO; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2652 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2653 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2654 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2655 | /** |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 2656 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 2657 | * @chip: the chip the GPIO to lock belongs to |
| 2658 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2659 | * |
| 2660 | * This is used directly by GPIO drivers that want to lock down |
Linus Walleij | f438acd | 2014-03-07 10:12:49 +0800 | [diff] [blame] | 2661 | * a certain GPIO line to be used for IRQs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2662 | */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 2663 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2664 | { |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 2665 | struct gpio_desc *desc; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2666 | |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 2667 | desc = gpiochip_get_desc(chip, offset); |
| 2668 | if (IS_ERR(desc)) |
| 2669 | return PTR_ERR(desc); |
| 2670 | |
| 2671 | /* Flush direction if something changed behind our back */ |
| 2672 | if (chip->get_direction) { |
| 2673 | int dir = chip->get_direction(chip, offset); |
| 2674 | |
| 2675 | if (dir) |
| 2676 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 2677 | else |
| 2678 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 2679 | } |
| 2680 | |
| 2681 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 2682 | chip_err(chip, |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2683 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 2684 | __func__); |
| 2685 | return -EIO; |
| 2686 | } |
| 2687 | |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 2688 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 2689 | |
| 2690 | /* |
| 2691 | * If the consumer has not set up a label (such as when the |
| 2692 | * IRQ is referenced from .to_irq()) we set up a label here |
| 2693 | * so it is clear this is used as an interrupt. |
| 2694 | */ |
| 2695 | if (!desc->label) |
| 2696 | desc_set_label(desc, "interrupt"); |
| 2697 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2698 | return 0; |
| 2699 | } |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 2700 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2701 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2702 | /** |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 2703 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 2704 | * @chip: the chip the GPIO to lock belongs to |
| 2705 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2706 | * |
| 2707 | * This is used directly by GPIO drivers that want to indicate |
| 2708 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 2709 | */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 2710 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2711 | { |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 2712 | struct gpio_desc *desc; |
| 2713 | |
| 2714 | desc = gpiochip_get_desc(chip, offset); |
| 2715 | if (IS_ERR(desc)) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2716 | return; |
| 2717 | |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 2718 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2719 | |
| 2720 | /* If we only had this marking, erase it */ |
| 2721 | if (desc->label && !strcmp(desc->label, "interrupt")) |
| 2722 | desc_set_label(desc, NULL); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2723 | } |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 2724 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2725 | |
Linus Walleij | 6cee382 | 2016-02-11 20:16:45 +0100 | [diff] [blame] | 2726 | bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset) |
| 2727 | { |
| 2728 | if (offset >= chip->ngpio) |
| 2729 | return false; |
| 2730 | |
| 2731 | return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags); |
| 2732 | } |
| 2733 | EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); |
| 2734 | |
Linus Walleij | 143b65d | 2016-02-16 15:41:42 +0100 | [diff] [blame] | 2735 | bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset) |
| 2736 | { |
| 2737 | if (offset >= chip->ngpio) |
| 2738 | return false; |
| 2739 | |
| 2740 | return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags); |
| 2741 | } |
| 2742 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); |
| 2743 | |
| 2744 | bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset) |
| 2745 | { |
| 2746 | if (offset >= chip->ngpio) |
| 2747 | return false; |
| 2748 | |
| 2749 | return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags); |
| 2750 | } |
| 2751 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); |
| 2752 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2753 | /** |
| 2754 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 2755 | * @desc: gpio whose value will be returned |
| 2756 | * |
| 2757 | * 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] | 2758 | * its ACTIVE_LOW status, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2759 | * |
| 2760 | * This function is to be called from contexts that can sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2761 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2762 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2763 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2764 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2765 | VALIDATE_DESC(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2766 | return _gpiod_get_raw_value(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2767 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2768 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2769 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2770 | /** |
| 2771 | * gpiod_get_value_cansleep() - return a gpio's value |
| 2772 | * @desc: gpio whose value will be returned |
| 2773 | * |
| 2774 | * 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] | 2775 | * account, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2776 | * |
| 2777 | * This function is to be called from contexts that can sleep. |
| 2778 | */ |
| 2779 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2780 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2781 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2782 | |
| 2783 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2784 | VALIDATE_DESC(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2785 | value = _gpiod_get_raw_value(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 2786 | if (value < 0) |
| 2787 | return value; |
| 2788 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2789 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2790 | value = !value; |
| 2791 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2792 | return value; |
| 2793 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2794 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2795 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2796 | /** |
| 2797 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 2798 | * @desc: gpio whose value will be assigned |
| 2799 | * @value: value to assign |
| 2800 | * |
| 2801 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2802 | * regard for its ACTIVE_LOW status. |
| 2803 | * |
| 2804 | * This function is to be called from contexts that can sleep. |
| 2805 | */ |
| 2806 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2807 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2808 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2809 | VALIDATE_DESC_VOID(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2810 | _gpiod_set_raw_value(desc, value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2811 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2812 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2813 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2814 | /** |
| 2815 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 2816 | * @desc: gpio whose value will be assigned |
| 2817 | * @value: value to assign |
| 2818 | * |
| 2819 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2820 | * account |
| 2821 | * |
| 2822 | * This function is to be called from contexts that can sleep. |
| 2823 | */ |
| 2824 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2825 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2826 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2827 | VALIDATE_DESC_VOID(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2828 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2829 | value = !value; |
| 2830 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2831 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2832 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2833 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2834 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2835 | * 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] | 2836 | * @array_size: number of elements in the descriptor / value arrays |
| 2837 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 2838 | * @value_array: array of values to assign |
| 2839 | * |
| 2840 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 2841 | * without regard for their ACTIVE_LOW status. |
| 2842 | * |
| 2843 | * This function is to be called from contexts that can sleep. |
| 2844 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2845 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
| 2846 | struct gpio_desc **desc_array, |
| 2847 | int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2848 | { |
| 2849 | might_sleep_if(extra_checks); |
| 2850 | if (!desc_array) |
| 2851 | return; |
Linus Walleij | 44c7288 | 2016-04-24 11:36:59 +0200 | [diff] [blame] | 2852 | gpiod_set_array_value_complex(true, true, array_size, desc_array, |
| 2853 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2854 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2855 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2856 | |
| 2857 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2858 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2859 | * @array_size: number of elements in the descriptor / value arrays |
| 2860 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 2861 | * @value_array: array of values to assign |
| 2862 | * |
| 2863 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 2864 | * into account. |
| 2865 | * |
| 2866 | * This function is to be called from contexts that can sleep. |
| 2867 | */ |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2868 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
| 2869 | struct gpio_desc **desc_array, |
| 2870 | int *value_array) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2871 | { |
| 2872 | might_sleep_if(extra_checks); |
| 2873 | if (!desc_array) |
| 2874 | return; |
Linus Walleij | 44c7288 | 2016-04-24 11:36:59 +0200 | [diff] [blame] | 2875 | gpiod_set_array_value_complex(false, true, array_size, desc_array, |
| 2876 | value_array); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2877 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 2878 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 2879 | |
| 2880 | /** |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2881 | * gpiod_add_lookup_table() - register GPIO device consumers |
| 2882 | * @table: table of consumers to register |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2883 | */ |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2884 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2885 | { |
| 2886 | mutex_lock(&gpio_lookup_lock); |
| 2887 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2888 | list_add_tail(&table->list, &gpio_lookup_list); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2889 | |
| 2890 | mutex_unlock(&gpio_lookup_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2891 | } |
| 2892 | |
Shobhit Kumar | be9015a | 2015-06-26 14:32:04 +0530 | [diff] [blame] | 2893 | /** |
| 2894 | * gpiod_remove_lookup_table() - unregister GPIO device consumers |
| 2895 | * @table: table of consumers to unregister |
| 2896 | */ |
| 2897 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) |
| 2898 | { |
| 2899 | mutex_lock(&gpio_lookup_lock); |
| 2900 | |
| 2901 | list_del(&table->list); |
| 2902 | |
| 2903 | mutex_unlock(&gpio_lookup_lock); |
| 2904 | } |
| 2905 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2906 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
| 2907 | { |
| 2908 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 2909 | struct gpiod_lookup_table *table; |
| 2910 | |
| 2911 | mutex_lock(&gpio_lookup_lock); |
| 2912 | |
| 2913 | list_for_each_entry(table, &gpio_lookup_list, list) { |
| 2914 | if (table->dev_id && dev_id) { |
| 2915 | /* |
| 2916 | * Valid strings on both ends, must be identical to have |
| 2917 | * a match |
| 2918 | */ |
| 2919 | if (!strcmp(table->dev_id, dev_id)) |
| 2920 | goto found; |
| 2921 | } else { |
| 2922 | /* |
| 2923 | * One of the pointers is NULL, so both must be to have |
| 2924 | * a match |
| 2925 | */ |
| 2926 | if (dev_id == table->dev_id) |
| 2927 | goto found; |
| 2928 | } |
| 2929 | } |
| 2930 | table = NULL; |
| 2931 | |
| 2932 | found: |
| 2933 | mutex_unlock(&gpio_lookup_lock); |
| 2934 | return table; |
| 2935 | } |
| 2936 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2937 | 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] | 2938 | unsigned int idx, |
| 2939 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2940 | { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 2941 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2942 | struct gpiod_lookup_table *table; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2943 | struct gpiod_lookup *p; |
| 2944 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2945 | table = gpiod_find_lookup_table(dev); |
| 2946 | if (!table) |
| 2947 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2948 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2949 | for (p = &table->table[0]; p->chip_label; p++) { |
| 2950 | struct gpio_chip *chip; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2951 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2952 | /* idx must always match exactly */ |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2953 | if (p->idx != idx) |
| 2954 | continue; |
| 2955 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2956 | /* If the lookup entry has a con_id, require exact match */ |
| 2957 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) |
| 2958 | continue; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2959 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2960 | chip = find_chip_by_name(p->chip_label); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2961 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2962 | if (!chip) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 2963 | dev_err(dev, "cannot find GPIO chip %s\n", |
| 2964 | p->chip_label); |
| 2965 | return ERR_PTR(-ENODEV); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2966 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2967 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2968 | if (chip->ngpio <= p->chip_hwnum) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 2969 | dev_err(dev, |
| 2970 | "requested GPIO %d is out of range [0..%d] for chip %s\n", |
| 2971 | idx, chip->ngpio, chip->label); |
| 2972 | return ERR_PTR(-EINVAL); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2973 | } |
| 2974 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 2975 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2976 | *flags = p->flags; |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 2977 | |
| 2978 | return desc; |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 2979 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2980 | |
| 2981 | return desc; |
| 2982 | } |
| 2983 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 2984 | static int dt_gpio_count(struct device *dev, const char *con_id) |
| 2985 | { |
| 2986 | int ret; |
| 2987 | char propname[32]; |
| 2988 | unsigned int i; |
| 2989 | |
| 2990 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 2991 | if (con_id) |
| 2992 | snprintf(propname, sizeof(propname), "%s-%s", |
| 2993 | con_id, gpio_suffixes[i]); |
| 2994 | else |
| 2995 | snprintf(propname, sizeof(propname), "%s", |
| 2996 | gpio_suffixes[i]); |
| 2997 | |
| 2998 | ret = of_gpio_named_count(dev->of_node, propname); |
| 2999 | if (ret >= 0) |
| 3000 | break; |
| 3001 | } |
| 3002 | return ret; |
| 3003 | } |
| 3004 | |
| 3005 | static int platform_gpio_count(struct device *dev, const char *con_id) |
| 3006 | { |
| 3007 | struct gpiod_lookup_table *table; |
| 3008 | struct gpiod_lookup *p; |
| 3009 | unsigned int count = 0; |
| 3010 | |
| 3011 | table = gpiod_find_lookup_table(dev); |
| 3012 | if (!table) |
| 3013 | return -ENOENT; |
| 3014 | |
| 3015 | for (p = &table->table[0]; p->chip_label; p++) { |
| 3016 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || |
| 3017 | (!con_id && !p->con_id)) |
| 3018 | count++; |
| 3019 | } |
| 3020 | if (!count) |
| 3021 | return -ENOENT; |
| 3022 | |
| 3023 | return count; |
| 3024 | } |
| 3025 | |
| 3026 | /** |
| 3027 | * gpiod_count - return the number of GPIOs associated with a device / function |
| 3028 | * or -ENOENT if no GPIO has been assigned to the requested function |
| 3029 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 3030 | * @con_id: function within the GPIO consumer |
| 3031 | */ |
| 3032 | int gpiod_count(struct device *dev, const char *con_id) |
| 3033 | { |
| 3034 | int count = -ENOENT; |
| 3035 | |
| 3036 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
| 3037 | count = dt_gpio_count(dev, con_id); |
| 3038 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) |
| 3039 | count = acpi_gpio_count(dev, con_id); |
| 3040 | |
| 3041 | if (count < 0) |
| 3042 | count = platform_gpio_count(dev, con_id); |
| 3043 | |
| 3044 | return count; |
| 3045 | } |
| 3046 | EXPORT_SYMBOL_GPL(gpiod_count); |
| 3047 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3048 | /** |
Thierry Reding | 0879162 | 2014-04-25 16:54:22 +0200 | [diff] [blame] | 3049 | * gpiod_get - obtain a GPIO for a given GPIO function |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 3050 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3051 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3052 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3053 | * |
| 3054 | * Return the GPIO descriptor corresponding to the function con_id of device |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 3055 | * 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] | 3056 | * 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] | 3057 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3058 | 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] | 3059 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3060 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3061 | return gpiod_get_index(dev, con_id, 0, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3062 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3063 | EXPORT_SYMBOL_GPL(gpiod_get); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3064 | |
| 3065 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3066 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function |
| 3067 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 3068 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3069 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3070 | * |
| 3071 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to |
| 3072 | * the requested function it will return NULL. This is convenient for drivers |
| 3073 | * that need to handle optional GPIOs. |
| 3074 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3075 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3076 | const char *con_id, |
| 3077 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3078 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3079 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3080 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3081 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3082 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3083 | |
| 3084 | /** |
| 3085 | * gpiod_configure_flags - helper function to configure a given GPIO |
| 3086 | * @desc: gpio whose value will be assigned |
| 3087 | * @con_id: function within the GPIO consumer |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 3088 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
| 3089 | * of_get_gpio_hog() |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3090 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 3091 | * |
| 3092 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the |
| 3093 | * requested function and/or index, or another IS_ERR() code if an error |
| 3094 | * occurred while trying to acquire the GPIO. |
| 3095 | */ |
| 3096 | static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 3097 | unsigned long lflags, enum gpiod_flags dflags) |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3098 | { |
| 3099 | int status; |
| 3100 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 3101 | if (lflags & GPIO_ACTIVE_LOW) |
| 3102 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 3103 | if (lflags & GPIO_OPEN_DRAIN) |
| 3104 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 3105 | if (lflags & GPIO_OPEN_SOURCE) |
| 3106 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 3107 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3108 | /* No particular flag request, return here... */ |
| 3109 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { |
| 3110 | pr_debug("no flags found for %s\n", con_id); |
| 3111 | return 0; |
| 3112 | } |
| 3113 | |
| 3114 | /* Process flags */ |
| 3115 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) |
| 3116 | status = gpiod_direction_output(desc, |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 3117 | !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3118 | else |
| 3119 | status = gpiod_direction_input(desc); |
| 3120 | |
| 3121 | return status; |
| 3122 | } |
| 3123 | |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3124 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3125 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
Andy Shevchenko | fdd6a5f | 2013-12-05 11:26:26 +0200 | [diff] [blame] | 3126 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3127 | * @con_id: function within the GPIO consumer |
| 3128 | * @idx: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3129 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3130 | * |
| 3131 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 3132 | * defined one for functions that define several GPIOs. |
| 3133 | * |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 3134 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
| 3135 | * 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] | 3136 | * occurred while trying to acquire the GPIO. |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3137 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3138 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3139 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3140 | unsigned int idx, |
| 3141 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3142 | { |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 3143 | struct gpio_desc *desc = NULL; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3144 | int status; |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3145 | enum gpio_lookup_flags lookupflags = 0; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3146 | |
| 3147 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 3148 | |
Rafael J. Wysocki | 4d8440b | 2015-03-10 23:08:57 +0100 | [diff] [blame] | 3149 | if (dev) { |
| 3150 | /* Using device tree? */ |
| 3151 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 3152 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 3153 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); |
| 3154 | } else if (ACPI_COMPANION(dev)) { |
| 3155 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
Dmitry Torokhov | 2548753 | 2016-03-24 10:50:25 -0700 | [diff] [blame] | 3156 | desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags); |
Rafael J. Wysocki | 4d8440b | 2015-03-10 23:08:57 +0100 | [diff] [blame] | 3157 | } |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /* |
| 3161 | * Either we are not using DT or ACPI, or their lookup did not return |
| 3162 | * a result. In that case, use platform lookup as a fallback. |
| 3163 | */ |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 3164 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
Alexander Shiyan | 43a8785 | 2014-09-19 11:39:25 +0400 | [diff] [blame] | 3165 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3166 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | if (IS_ERR(desc)) { |
Heikki Krogerus | 351cfe0 | 2013-11-29 15:47:34 +0200 | [diff] [blame] | 3170 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3171 | return desc; |
| 3172 | } |
| 3173 | |
| 3174 | status = gpiod_request(desc, con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3175 | if (status < 0) |
| 3176 | return ERR_PTR(status); |
| 3177 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 3178 | status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3179 | if (status < 0) { |
| 3180 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
| 3181 | gpiod_put(desc); |
| 3182 | return ERR_PTR(status); |
| 3183 | } |
| 3184 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3185 | return desc; |
| 3186 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3187 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3188 | |
| 3189 | /** |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3190 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node |
| 3191 | * @fwnode: handle of the firmware node |
| 3192 | * @propname: name of the firmware property representing the GPIO |
| 3193 | * |
| 3194 | * This function can be used for drivers that get their configuration |
| 3195 | * from firmware. |
| 3196 | * |
| 3197 | * Function properly finds the corresponding GPIO using whatever is the |
| 3198 | * underlying firmware interface and then makes sure that the GPIO |
| 3199 | * descriptor is requested before it is returned to the caller. |
| 3200 | * |
| 3201 | * In case of error an ERR_PTR() is returned. |
| 3202 | */ |
| 3203 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, |
| 3204 | const char *propname) |
| 3205 | { |
| 3206 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
| 3207 | bool active_low = false; |
Laurent Pinchart | 90b665f | 2015-10-13 00:20:21 +0300 | [diff] [blame] | 3208 | bool single_ended = false; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3209 | int ret; |
| 3210 | |
| 3211 | if (!fwnode) |
| 3212 | return ERR_PTR(-EINVAL); |
| 3213 | |
| 3214 | if (is_of_node(fwnode)) { |
| 3215 | enum of_gpio_flags flags; |
| 3216 | |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 3217 | desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0, |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3218 | &flags); |
Laurent Pinchart | 90b665f | 2015-10-13 00:20:21 +0300 | [diff] [blame] | 3219 | if (!IS_ERR(desc)) { |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3220 | active_low = flags & OF_GPIO_ACTIVE_LOW; |
Laurent Pinchart | 90b665f | 2015-10-13 00:20:21 +0300 | [diff] [blame] | 3221 | single_ended = flags & OF_GPIO_SINGLE_ENDED; |
| 3222 | } |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3223 | } else if (is_acpi_node(fwnode)) { |
| 3224 | struct acpi_gpio_info info; |
| 3225 | |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 3226 | desc = acpi_node_get_gpiod(fwnode, propname, 0, &info); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3227 | if (!IS_ERR(desc)) |
Christophe RICARD | 5204472 | 2015-12-23 23:25:34 +0100 | [diff] [blame] | 3228 | active_low = info.polarity == GPIO_ACTIVE_LOW; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | if (IS_ERR(desc)) |
| 3232 | return desc; |
| 3233 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 3234 | ret = gpiod_request(desc, NULL); |
| 3235 | if (ret) |
| 3236 | return ERR_PTR(ret); |
| 3237 | |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3238 | if (active_low) |
| 3239 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 3240 | |
Laurent Pinchart | 90b665f | 2015-10-13 00:20:21 +0300 | [diff] [blame] | 3241 | if (single_ended) { |
| 3242 | if (active_low) |
| 3243 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 3244 | else |
| 3245 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 3246 | } |
| 3247 | |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 3248 | return desc; |
| 3249 | } |
| 3250 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); |
| 3251 | |
| 3252 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3253 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO |
| 3254 | * function |
| 3255 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 3256 | * @con_id: function within the GPIO consumer |
| 3257 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3258 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3259 | * |
| 3260 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the |
| 3261 | * specified index was assigned to the requested function it will return NULL. |
| 3262 | * This is convenient for drivers that need to handle optional GPIOs. |
| 3263 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3264 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3265 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3266 | unsigned int index, |
| 3267 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3268 | { |
| 3269 | struct gpio_desc *desc; |
| 3270 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 3271 | desc = gpiod_get_index(dev, con_id, index, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3272 | if (IS_ERR(desc)) { |
| 3273 | if (PTR_ERR(desc) == -ENOENT) |
| 3274 | return NULL; |
| 3275 | } |
| 3276 | |
| 3277 | return desc; |
| 3278 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 3279 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 3280 | |
| 3281 | /** |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3282 | * gpiod_hog - Hog the specified GPIO desc given the provided flags |
| 3283 | * @desc: gpio whose value will be assigned |
| 3284 | * @name: gpio line name |
| 3285 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
| 3286 | * of_get_gpio_hog() |
| 3287 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 3288 | */ |
| 3289 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
| 3290 | unsigned long lflags, enum gpiod_flags dflags) |
| 3291 | { |
| 3292 | struct gpio_chip *chip; |
| 3293 | struct gpio_desc *local_desc; |
| 3294 | int hwnum; |
| 3295 | int status; |
| 3296 | |
| 3297 | chip = gpiod_to_chip(desc); |
| 3298 | hwnum = gpio_chip_hwgpio(desc); |
| 3299 | |
| 3300 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); |
| 3301 | if (IS_ERR(local_desc)) { |
Laxman Dewangan | c31a571 | 2016-03-11 19:13:21 +0530 | [diff] [blame] | 3302 | status = PTR_ERR(local_desc); |
| 3303 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", |
| 3304 | name, chip->label, hwnum, status); |
| 3305 | return status; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3306 | } |
| 3307 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 3308 | status = gpiod_configure_flags(desc, name, lflags, dflags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3309 | if (status < 0) { |
Laxman Dewangan | c31a571 | 2016-03-11 19:13:21 +0530 | [diff] [blame] | 3310 | pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n", |
| 3311 | name, chip->label, hwnum, status); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3312 | gpiochip_free_own_desc(desc); |
| 3313 | return status; |
| 3314 | } |
| 3315 | |
| 3316 | /* Mark GPIO as hogged so it can be identified and removed later */ |
| 3317 | set_bit(FLAG_IS_HOGGED, &desc->flags); |
| 3318 | |
| 3319 | pr_info("GPIO line %d (%s) hogged as %s%s\n", |
| 3320 | desc_to_gpio(desc), name, |
| 3321 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
| 3322 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? |
| 3323 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); |
| 3324 | |
| 3325 | return 0; |
| 3326 | } |
| 3327 | |
| 3328 | /** |
| 3329 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog |
| 3330 | * @chip: gpio chip to act on |
| 3331 | * |
| 3332 | * This is only used by of_gpiochip_remove to free hogged gpios |
| 3333 | */ |
| 3334 | static void gpiochip_free_hogs(struct gpio_chip *chip) |
| 3335 | { |
| 3336 | int id; |
| 3337 | |
| 3338 | for (id = 0; id < chip->ngpio; id++) { |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 3339 | if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags)) |
| 3340 | gpiochip_free_own_desc(&chip->gpiodev->descs[id]); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 3341 | } |
| 3342 | } |
| 3343 | |
| 3344 | /** |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 3345 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function |
| 3346 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 3347 | * @con_id: function within the GPIO consumer |
| 3348 | * @flags: optional GPIO initialization flags |
| 3349 | * |
| 3350 | * This function acquires all the GPIOs defined under a given function. |
| 3351 | * |
| 3352 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if |
| 3353 | * no GPIO has been assigned to the requested function, or another IS_ERR() |
| 3354 | * code if an error occurred while trying to acquire the GPIOs. |
| 3355 | */ |
| 3356 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, |
| 3357 | const char *con_id, |
| 3358 | enum gpiod_flags flags) |
| 3359 | { |
| 3360 | struct gpio_desc *desc; |
| 3361 | struct gpio_descs *descs; |
| 3362 | int count; |
| 3363 | |
| 3364 | count = gpiod_count(dev, con_id); |
| 3365 | if (count < 0) |
| 3366 | return ERR_PTR(count); |
| 3367 | |
| 3368 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, |
| 3369 | GFP_KERNEL); |
| 3370 | if (!descs) |
| 3371 | return ERR_PTR(-ENOMEM); |
| 3372 | |
| 3373 | for (descs->ndescs = 0; descs->ndescs < count; ) { |
| 3374 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); |
| 3375 | if (IS_ERR(desc)) { |
| 3376 | gpiod_put_array(descs); |
| 3377 | return ERR_CAST(desc); |
| 3378 | } |
| 3379 | descs->desc[descs->ndescs] = desc; |
| 3380 | descs->ndescs++; |
| 3381 | } |
| 3382 | return descs; |
| 3383 | } |
| 3384 | EXPORT_SYMBOL_GPL(gpiod_get_array); |
| 3385 | |
| 3386 | /** |
| 3387 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO |
| 3388 | * function |
| 3389 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 3390 | * @con_id: function within the GPIO consumer |
| 3391 | * @flags: optional GPIO initialization flags |
| 3392 | * |
| 3393 | * This is equivalent to gpiod_get_array(), except that when no GPIO was |
| 3394 | * assigned to the requested function it will return NULL. |
| 3395 | */ |
| 3396 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, |
| 3397 | const char *con_id, |
| 3398 | enum gpiod_flags flags) |
| 3399 | { |
| 3400 | struct gpio_descs *descs; |
| 3401 | |
| 3402 | descs = gpiod_get_array(dev, con_id, flags); |
| 3403 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
| 3404 | return NULL; |
| 3405 | |
| 3406 | return descs; |
| 3407 | } |
| 3408 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); |
| 3409 | |
| 3410 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 3411 | * gpiod_put - dispose of a GPIO descriptor |
| 3412 | * @desc: GPIO descriptor to dispose of |
| 3413 | * |
| 3414 | * No descriptor can be used after gpiod_put() has been called on it. |
| 3415 | */ |
| 3416 | void gpiod_put(struct gpio_desc *desc) |
| 3417 | { |
| 3418 | gpiod_free(desc); |
| 3419 | } |
| 3420 | EXPORT_SYMBOL_GPL(gpiod_put); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3421 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 3422 | /** |
| 3423 | * gpiod_put_array - dispose of multiple GPIO descriptors |
| 3424 | * @descs: struct gpio_descs containing an array of descriptors |
| 3425 | */ |
| 3426 | void gpiod_put_array(struct gpio_descs *descs) |
| 3427 | { |
| 3428 | unsigned int i; |
| 3429 | |
| 3430 | for (i = 0; i < descs->ndescs; i++) |
| 3431 | gpiod_put(descs->desc[i]); |
| 3432 | |
| 3433 | kfree(descs); |
| 3434 | } |
| 3435 | EXPORT_SYMBOL_GPL(gpiod_put_array); |
| 3436 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 3437 | static int __init gpiolib_dev_init(void) |
| 3438 | { |
| 3439 | int ret; |
| 3440 | |
| 3441 | /* Register GPIO sysfs bus */ |
| 3442 | ret = bus_register(&gpio_bus_type); |
| 3443 | if (ret < 0) { |
| 3444 | pr_err("gpiolib: could not register GPIO bus type\n"); |
| 3445 | return ret; |
| 3446 | } |
| 3447 | |
| 3448 | ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip"); |
| 3449 | if (ret < 0) { |
| 3450 | pr_err("gpiolib: failed to allocate char dev region\n"); |
| 3451 | bus_unregister(&gpio_bus_type); |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 3452 | } else { |
| 3453 | gpiolib_initialized = true; |
| 3454 | gpiochip_setup_devs(); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 3455 | } |
| 3456 | return ret; |
| 3457 | } |
| 3458 | core_initcall(gpiolib_dev_init); |
| 3459 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3460 | #ifdef CONFIG_DEBUG_FS |
| 3461 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3462 | 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] | 3463 | { |
| 3464 | unsigned i; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3465 | struct gpio_chip *chip = gdev->chip; |
| 3466 | unsigned gpio = gdev->base; |
| 3467 | struct gpio_desc *gdesc = &gdev->descs[0]; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3468 | int is_out; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3469 | int is_irq; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3470 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3471 | for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame] | 3472 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
| 3473 | if (gdesc->name) { |
| 3474 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", |
| 3475 | gpio, gdesc->name); |
| 3476 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3477 | continue; |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame] | 3478 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3479 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3480 | gpiod_get_direction(gdesc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3481 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3482 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame] | 3483 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s", |
| 3484 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3485 | is_out ? "out" : "in ", |
| 3486 | chip->get |
| 3487 | ? (chip->get(chip, i) ? "hi" : "lo") |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3488 | : "? ", |
| 3489 | is_irq ? "IRQ" : " "); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3490 | seq_printf(s, "\n"); |
| 3491 | } |
| 3492 | } |
| 3493 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3494 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3495 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3496 | unsigned long flags; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3497 | struct gpio_device *gdev = NULL; |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 3498 | loff_t index = *pos; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3499 | |
| 3500 | s->private = ""; |
| 3501 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3502 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3503 | list_for_each_entry(gdev, &gpio_devices, list) |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3504 | if (index-- == 0) { |
| 3505 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3506 | return gdev; |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3507 | } |
| 3508 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 3509 | |
| 3510 | return NULL; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 3514 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3515 | unsigned long flags; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3516 | struct gpio_device *gdev = v; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3517 | void *ret = NULL; |
| 3518 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3519 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3520 | if (list_is_last(&gdev->list, &gpio_devices)) |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 3521 | ret = NULL; |
| 3522 | else |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3523 | ret = list_entry(gdev->list.next, struct gpio_device, list); |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 3524 | spin_unlock_irqrestore(&gpio_lock, flags); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3525 | |
| 3526 | s->private = "\n"; |
| 3527 | ++*pos; |
| 3528 | |
| 3529 | return ret; |
| 3530 | } |
| 3531 | |
| 3532 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 3533 | { |
| 3534 | } |
| 3535 | |
| 3536 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 3537 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3538 | struct gpio_device *gdev = v; |
| 3539 | struct gpio_chip *chip = gdev->chip; |
| 3540 | struct device *parent; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3541 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3542 | if (!chip) { |
| 3543 | seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, |
| 3544 | dev_name(&gdev->dev)); |
| 3545 | return 0; |
| 3546 | } |
| 3547 | |
| 3548 | seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, |
| 3549 | dev_name(&gdev->dev), |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3550 | gdev->base, gdev->base + gdev->ngpio - 1); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 3551 | parent = chip->parent; |
| 3552 | if (parent) |
| 3553 | seq_printf(s, ", parent: %s/%s", |
| 3554 | parent->bus ? parent->bus->name : "no-bus", |
| 3555 | dev_name(parent)); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3556 | if (chip->label) |
| 3557 | seq_printf(s, ", %s", chip->label); |
| 3558 | if (chip->can_sleep) |
| 3559 | seq_printf(s, ", can sleep"); |
| 3560 | seq_printf(s, ":\n"); |
| 3561 | |
| 3562 | if (chip->dbg_show) |
| 3563 | chip->dbg_show(s, chip); |
| 3564 | else |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3565 | gpiolib_dbg_show(s, gdev); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3566 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3567 | return 0; |
| 3568 | } |
| 3569 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3570 | static const struct seq_operations gpiolib_seq_ops = { |
| 3571 | .start = gpiolib_seq_start, |
| 3572 | .next = gpiolib_seq_next, |
| 3573 | .stop = gpiolib_seq_stop, |
| 3574 | .show = gpiolib_seq_show, |
| 3575 | }; |
| 3576 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3577 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 3578 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3579 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3580 | } |
| 3581 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 3582 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3583 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3584 | .open = gpiolib_open, |
| 3585 | .read = seq_read, |
| 3586 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 3587 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3588 | }; |
| 3589 | |
| 3590 | static int __init gpiolib_debugfs_init(void) |
| 3591 | { |
| 3592 | /* /sys/kernel/debug/gpio */ |
| 3593 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 3594 | NULL, NULL, &gpiolib_operations); |
| 3595 | return 0; |
| 3596 | } |
| 3597 | subsys_initcall(gpiolib_debugfs_init); |
| 3598 | |
| 3599 | #endif /* DEBUG_FS */ |