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