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