Linus Walleij | dae5f0a | 2018-09-25 09:08:48 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andy Shevchenko | 923a654 | 2017-05-25 16:08:38 +0300 | [diff] [blame] | 2 | #include <linux/bitmap.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3 | #include <linux/kernel.h> |
| 4 | #include <linux/module.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 5 | #include <linux/interrupt.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 6 | #include <linux/irq.h> |
| 7 | #include <linux/spinlock.h> |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 8 | #include <linux/list.h> |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/debugfs.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/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" |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 32 | #include "gpiolib-of.h" |
Andy Shevchenko | 77cb907 | 2019-07-30 13:43:36 +0300 | [diff] [blame] | 33 | #include "gpiolib-acpi.h" |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 34 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 35 | #define CREATE_TRACE_POINTS |
| 36 | #include <trace/events/gpio.h> |
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 | /* Implementation infrastructure for GPIO interfaces. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 39 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 40 | * The GPIO programming interface allows for inlining speed-critical |
| 41 | * get/set operations for common cases, so that access to SOC-integrated |
| 42 | * GPIOs can sometimes cost only an instruction or two per bit. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | |
| 46 | /* When debugging, extend minimal trust to callers and platform code. |
| 47 | * Also emit diagnostic messages that may help initial bringup, when |
| 48 | * board setup or driver bugs are most common. |
| 49 | * |
| 50 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 51 | */ |
| 52 | #ifdef DEBUG |
| 53 | #define extra_checks 1 |
| 54 | #else |
| 55 | #define extra_checks 0 |
| 56 | #endif |
| 57 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 58 | /* Device and char device-related information */ |
| 59 | static DEFINE_IDA(gpio_ida); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 60 | static dev_t gpio_devt; |
| 61 | #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */ |
| 62 | static struct bus_type gpio_bus_type = { |
| 63 | .name = "gpio", |
| 64 | }; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 65 | |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Number of GPIOs to use for the fast path in set array |
| 68 | */ |
| 69 | #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT |
| 70 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 71 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 72 | * While any GPIO is requested, its gpio_chip is not removable; |
| 73 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 74 | */ |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 75 | DEFINE_SPINLOCK(gpio_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 76 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 77 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 78 | static LIST_HEAD(gpio_lookup_list); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 79 | LIST_HEAD(gpio_devices); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 80 | |
Bartosz Golaszewski | a411e81 | 2018-04-10 22:30:28 +0200 | [diff] [blame] | 81 | static DEFINE_MUTEX(gpio_machine_hogs_mutex); |
| 82 | static LIST_HEAD(gpio_machine_hogs); |
| 83 | |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 84 | static void gpiochip_free_hogs(struct gpio_chip *chip); |
Thierry Reding | 959bc7b | 2017-11-07 19:15:59 +0100 | [diff] [blame] | 85 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 86 | struct lock_class_key *lock_key, |
| 87 | struct lock_class_key *request_key); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 88 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
Andy Shevchenko | 9411e3a | 2019-10-09 17:34:44 +0300 | [diff] [blame] | 89 | static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 90 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); |
| 91 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 92 | |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 93 | static bool gpiolib_initialized; |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 94 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 95 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 96 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 97 | d->label = label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 100 | /** |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 101 | * gpio_to_desc - Convert a GPIO number to its descriptor |
| 102 | * @gpio: global GPIO number |
| 103 | * |
| 104 | * Returns: |
| 105 | * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO |
| 106 | * with the given number exists in the system. |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 107 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 108 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 109 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 110 | struct gpio_device *gdev; |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 111 | unsigned long flags; |
| 112 | |
| 113 | spin_lock_irqsave(&gpio_lock, flags); |
| 114 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 115 | list_for_each_entry(gdev, &gpio_devices, list) { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 116 | if (gdev->base <= gpio && |
| 117 | gdev->base + gdev->ngpio > gpio) { |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 118 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 119 | return &gdev->descs[gpio - gdev->base]; |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 124 | |
Alexandre Courbot | 0e9a5ed | 2014-12-02 23:15:05 +0900 | [diff] [blame] | 125 | if (!gpio_is_valid(gpio)) |
| 126 | WARN(1, "invalid GPIO %d\n", gpio); |
| 127 | |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 128 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 129 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 131 | |
| 132 | /** |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 133 | * gpiochip_get_desc - get the GPIO descriptor corresponding to the given |
| 134 | * hardware number for this chip |
| 135 | * @chip: GPIO chip |
| 136 | * @hwnum: hardware number of the GPIO for this chip |
| 137 | * |
| 138 | * Returns: |
| 139 | * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists |
| 140 | * in the given chip for the specified hardware number. |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 141 | */ |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 142 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
| 143 | u16 hwnum) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 144 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 145 | struct gpio_device *gdev = chip->gpiodev; |
| 146 | |
| 147 | if (hwnum >= gdev->ngpio) |
Alexandre Courbot | b7d0a28 | 2013-12-03 12:31:11 +0900 | [diff] [blame] | 148 | return ERR_PTR(-EINVAL); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 149 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 150 | return &gdev->descs[hwnum]; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 151 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 152 | |
| 153 | /** |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 154 | * desc_to_gpio - convert a GPIO descriptor to the integer namespace |
| 155 | * @desc: GPIO descriptor |
| 156 | * |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 157 | * This should disappear in the future but is needed since we still |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 158 | * use GPIO numbers for error messages and sysfs nodes. |
| 159 | * |
| 160 | * Returns: |
| 161 | * The global GPIO number for the GPIO specified by its descriptor. |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 162 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 163 | int desc_to_gpio(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 164 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 165 | return desc->gdev->base + (desc - &desc->gdev->descs[0]); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 166 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 168 | |
| 169 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 170 | /** |
| 171 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 172 | * @desc: descriptor to return the chip of |
| 173 | */ |
| 174 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 175 | { |
Vladimir Zapolskiy | dd3b9a4 | 2017-12-21 18:37:30 +0200 | [diff] [blame] | 176 | if (!desc || !desc->gdev) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 177 | return NULL; |
| 178 | return desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 179 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 181 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 182 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 183 | static int gpiochip_find_base(int ngpio) |
| 184 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 185 | struct gpio_device *gdev; |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 186 | int base = ARCH_NR_GPIOS - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 187 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 188 | list_for_each_entry_reverse(gdev, &gpio_devices, list) { |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 189 | /* found a free space? */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 190 | if (gdev->base + gdev->ngpio <= base) |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 191 | break; |
| 192 | else |
| 193 | /* nope, check the space right before the chip */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 194 | base = gdev->base - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 197 | if (gpio_is_valid(base)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 198 | pr_debug("%s: found new base at %d\n", __func__, base); |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 199 | return base; |
| 200 | } else { |
| 201 | pr_err("%s: cannot find free range\n", __func__); |
| 202 | return -ENOSPC; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 203 | } |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 206 | /** |
| 207 | * gpiod_get_direction - return the current direction of a GPIO |
| 208 | * @desc: GPIO to get the direction of |
| 209 | * |
Wolfram Sang | 94fc730 | 2018-01-09 12:35:53 +0100 | [diff] [blame] | 210 | * Returns 0 for output, 1 for input, or an error code in case of error. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 211 | * |
| 212 | * This function may sleep if gpiod_cansleep() is true. |
| 213 | */ |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 214 | int gpiod_get_direction(struct gpio_desc *desc) |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 215 | { |
Wolfram Sang | d0121b8 | 2018-07-11 18:33:19 +0200 | [diff] [blame] | 216 | struct gpio_chip *chip; |
| 217 | unsigned offset; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 218 | int ret; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 219 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 220 | chip = gpiod_to_chip(desc); |
| 221 | offset = gpio_chip_hwgpio(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 222 | |
| 223 | if (!chip->get_direction) |
Wolfram Sang | d0121b8 | 2018-07-11 18:33:19 +0200 | [diff] [blame] | 224 | return -ENOTSUPP; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 225 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 226 | ret = chip->get_direction(chip, offset); |
| 227 | if (ret > 0) { |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 228 | /* GPIOF_DIR_IN, or other positive */ |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 229 | ret = 1; |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 230 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 231 | } |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 232 | if (ret == 0) { |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 233 | /* GPIOF_DIR_OUT */ |
Alexandre Courbot | 8e53b0f | 2014-11-25 17:16:31 +0900 | [diff] [blame] | 234 | set_bit(FLAG_IS_OUT, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 235 | } |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 236 | return ret; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 237 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 238 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 239 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 240 | /* |
| 241 | * 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] | 242 | * by range(means [base, base + ngpio - 1]) order. |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 243 | * |
| 244 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 245 | * space. |
| 246 | */ |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 247 | static int gpiodev_add_to_list(struct gpio_device *gdev) |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 248 | { |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 249 | struct gpio_device *prev, *next; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 250 | |
| 251 | if (list_empty(&gpio_devices)) { |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 252 | /* initial entry in list */ |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 253 | list_add_tail(&gdev->list, &gpio_devices); |
Sudip Mukherjee | e28ecca | 2015-12-27 19:06:50 +0530 | [diff] [blame] | 254 | return 0; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 255 | } |
| 256 | |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 257 | next = list_entry(gpio_devices.next, struct gpio_device, list); |
| 258 | if (gdev->base + gdev->ngpio <= next->base) { |
| 259 | /* add before first entry */ |
| 260 | list_add(&gdev->list, &gpio_devices); |
Julien Grossholtz | 96098df | 2016-01-07 16:46:45 -0500 | [diff] [blame] | 261 | return 0; |
| 262 | } |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 263 | |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 264 | prev = list_entry(gpio_devices.prev, struct gpio_device, list); |
| 265 | if (prev->base + prev->ngpio <= gdev->base) { |
| 266 | /* add behind last entry */ |
| 267 | list_add_tail(&gdev->list, &gpio_devices); |
| 268 | return 0; |
| 269 | } |
Bamvor Jian Zhang | ef7c755 | 2015-11-16 13:02:46 +0800 | [diff] [blame] | 270 | |
Bamvor Jian Zhang | a961f9b | 2016-02-26 22:37:14 +0800 | [diff] [blame] | 271 | list_for_each_entry_safe(prev, next, &gpio_devices, list) { |
| 272 | /* at the end of the list */ |
| 273 | if (&next->list == &gpio_devices) |
| 274 | break; |
| 275 | |
| 276 | /* add between prev and next */ |
| 277 | if (prev->base + prev->ngpio <= gdev->base |
| 278 | && gdev->base + gdev->ngpio <= next->base) { |
| 279 | list_add(&gdev->list, &prev->list); |
| 280 | return 0; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); |
| 285 | return -EBUSY; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 286 | } |
| 287 | |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 288 | /* |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 289 | * Convert a GPIO name to its descriptor |
| 290 | */ |
| 291 | static struct gpio_desc *gpio_name_to_desc(const char * const name) |
| 292 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 293 | struct gpio_device *gdev; |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 294 | unsigned long flags; |
| 295 | |
| 296 | spin_lock_irqsave(&gpio_lock, flags); |
| 297 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 298 | list_for_each_entry(gdev, &gpio_devices, list) { |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 299 | int i; |
| 300 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 301 | for (i = 0; i != gdev->ngpio; ++i) { |
| 302 | struct gpio_desc *desc = &gdev->descs[i]; |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 303 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 304 | if (!desc->name || !name) |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 305 | continue; |
| 306 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 307 | if (!strcmp(desc->name, name)) { |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 308 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 309 | return desc; |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 315 | |
| 316 | return NULL; |
| 317 | } |
| 318 | |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 319 | /* |
| 320 | * Takes the names from gc->names and checks if they are all unique. If they |
| 321 | * are, they are assigned to their gpio descriptors. |
| 322 | * |
Bamvor Jian Zhang | ed37915 | 2015-11-14 16:43:20 +0800 | [diff] [blame] | 323 | * 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] | 324 | */ |
| 325 | static int gpiochip_set_desc_names(struct gpio_chip *gc) |
| 326 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 327 | struct gpio_device *gdev = gc->gpiodev; |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 328 | int i; |
| 329 | |
| 330 | if (!gc->names) |
| 331 | return 0; |
| 332 | |
| 333 | /* First check all names if they are unique */ |
| 334 | for (i = 0; i != gc->ngpio; ++i) { |
| 335 | struct gpio_desc *gpio; |
| 336 | |
| 337 | gpio = gpio_name_to_desc(gc->names[i]); |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 338 | if (gpio) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 339 | dev_warn(&gdev->dev, |
Linus Walleij | 34ffd85 | 2015-10-20 11:31:54 +0200 | [diff] [blame] | 340 | "Detected name collision for GPIO name '%s'\n", |
Linus Walleij | f881bab0 | 2015-09-23 16:20:43 -0700 | [diff] [blame] | 341 | gc->names[i]); |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Then add all names to the GPIO descriptors */ |
| 345 | for (i = 0; i != gc->ngpio; ++i) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 346 | gdev->descs[i].name = gc->names[i]; |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Stephen Boyd | e4371f6e | 2018-03-23 09:34:50 -0700 | [diff] [blame] | 351 | static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip) |
| 352 | { |
| 353 | unsigned long *p; |
| 354 | |
Masahiro Yamada | 7bdbd1ec | 2019-07-18 15:51:01 +0900 | [diff] [blame] | 355 | p = bitmap_alloc(chip->ngpio, GFP_KERNEL); |
Stephen Boyd | e4371f6e | 2018-03-23 09:34:50 -0700 | [diff] [blame] | 356 | if (!p) |
| 357 | return NULL; |
| 358 | |
| 359 | /* Assume by default all GPIOs are valid */ |
| 360 | bitmap_fill(p, chip->ngpio); |
| 361 | |
| 362 | return p; |
| 363 | } |
| 364 | |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 365 | static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 366 | { |
Linus Walleij | eb1e8bd | 2019-08-19 11:30:58 +0200 | [diff] [blame] | 367 | if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask)) |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 370 | gc->valid_mask = gpiochip_allocate_mask(gc); |
| 371 | if (!gc->valid_mask) |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 372 | return -ENOMEM; |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Linus Walleij | c9fc5af | 2019-08-19 10:49:04 +0200 | [diff] [blame] | 377 | static int gpiochip_init_valid_mask(struct gpio_chip *gc) |
Ricardo Ribalda Delgado | f8ec92a | 2018-10-05 08:52:58 +0200 | [diff] [blame] | 378 | { |
Linus Walleij | c9fc5af | 2019-08-19 10:49:04 +0200 | [diff] [blame] | 379 | if (gc->init_valid_mask) |
| 380 | return gc->init_valid_mask(gc, |
| 381 | gc->valid_mask, |
| 382 | gc->ngpio); |
Ricardo Ribalda Delgado | f8ec92a | 2018-10-05 08:52:58 +0200 | [diff] [blame] | 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 387 | static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip) |
| 388 | { |
Masahiro Yamada | 7bdbd1ec | 2019-07-18 15:51:01 +0900 | [diff] [blame] | 389 | bitmap_free(gpiochip->valid_mask); |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 390 | gpiochip->valid_mask = NULL; |
| 391 | } |
| 392 | |
| 393 | bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip, |
| 394 | unsigned int offset) |
| 395 | { |
| 396 | /* No mask means all valid */ |
| 397 | if (likely(!gpiochip->valid_mask)) |
| 398 | return true; |
| 399 | return test_bit(offset, gpiochip->valid_mask); |
| 400 | } |
| 401 | EXPORT_SYMBOL_GPL(gpiochip_line_is_valid); |
| 402 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 403 | /* |
| 404 | * GPIO line handle management |
| 405 | */ |
| 406 | |
| 407 | /** |
| 408 | * struct linehandle_state - contains the state of a userspace handle |
| 409 | * @gdev: the GPIO device the handle pertains to |
| 410 | * @label: consumer label used to tag descriptors |
| 411 | * @descs: the GPIO descriptors held by this handle |
| 412 | * @numdescs: the number of descriptors held in the descs array |
| 413 | */ |
| 414 | struct linehandle_state { |
| 415 | struct gpio_device *gdev; |
| 416 | const char *label; |
| 417 | struct gpio_desc *descs[GPIOHANDLES_MAX]; |
| 418 | u32 numdescs; |
| 419 | }; |
| 420 | |
Lars-Peter Clausen | e3e847c | 2016-10-18 16:54:05 +0200 | [diff] [blame] | 421 | #define GPIOHANDLE_REQUEST_VALID_FLAGS \ |
| 422 | (GPIOHANDLE_REQUEST_INPUT | \ |
| 423 | GPIOHANDLE_REQUEST_OUTPUT | \ |
| 424 | GPIOHANDLE_REQUEST_ACTIVE_LOW | \ |
Drew Fustini | 9225d51 | 2019-11-05 10:04:23 +0800 | [diff] [blame] | 425 | GPIOHANDLE_REQUEST_BIAS_PULL_UP | \ |
| 426 | GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \ |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 427 | GPIOHANDLE_REQUEST_BIAS_DISABLE | \ |
Lars-Peter Clausen | e3e847c | 2016-10-18 16:54:05 +0200 | [diff] [blame] | 428 | GPIOHANDLE_REQUEST_OPEN_DRAIN | \ |
| 429 | GPIOHANDLE_REQUEST_OPEN_SOURCE) |
| 430 | |
Kent Gibson | b043ed7 | 2019-11-05 10:04:28 +0800 | [diff] [blame^] | 431 | static int linehandle_validate_flags(u32 flags) |
| 432 | { |
| 433 | /* Return an error if an unknown flag is set */ |
| 434 | if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) |
| 435 | return -EINVAL; |
| 436 | |
| 437 | /* |
| 438 | * Do not allow both INPUT & OUTPUT flags to be set as they are |
| 439 | * contradictory. |
| 440 | */ |
| 441 | if ((flags & GPIOHANDLE_REQUEST_INPUT) && |
| 442 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | /* |
| 446 | * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If |
| 447 | * the hardware actually supports enabling both at the same time the |
| 448 | * electrical result would be disastrous. |
| 449 | */ |
| 450 | if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && |
| 451 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ |
| 455 | if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) && |
| 456 | ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
| 457 | (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | /* Bias flags only allowed for input or output mode. */ |
| 461 | if (!((flags & GPIOHANDLE_REQUEST_INPUT) || |
| 462 | (flags & GPIOHANDLE_REQUEST_OUTPUT)) && |
| 463 | ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) || |
| 464 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) || |
| 465 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN))) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | /* Only one bias flag can be set. */ |
| 469 | if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && |
| 470 | (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | |
| 471 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
| 472 | ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
| 473 | (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 479 | static long linehandle_ioctl(struct file *filep, unsigned int cmd, |
| 480 | unsigned long arg) |
| 481 | { |
| 482 | struct linehandle_state *lh = filep->private_data; |
| 483 | void __user *ip = (void __user *)arg; |
| 484 | struct gpiohandle_data ghd; |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 485 | DECLARE_BITMAP(vals, GPIOHANDLES_MAX); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 486 | int i; |
| 487 | |
| 488 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
Bartosz Golaszewski | 2b955b3 | 2018-07-16 10:34:24 +0200 | [diff] [blame] | 489 | /* NOTE: It's ok to read values of output lines. */ |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 490 | int ret = gpiod_get_array_value_complex(false, |
| 491 | true, |
| 492 | lh->numdescs, |
| 493 | lh->descs, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 494 | NULL, |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 495 | vals); |
| 496 | if (ret) |
| 497 | return ret; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 498 | |
Lars-Peter Clausen | 3eded5d | 2016-10-18 16:54:02 +0200 | [diff] [blame] | 499 | memset(&ghd, 0, sizeof(ghd)); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 500 | for (i = 0; i < lh->numdescs; i++) |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 501 | ghd.values[i] = test_bit(i, vals); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 502 | |
| 503 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 504 | return -EFAULT; |
| 505 | |
| 506 | return 0; |
| 507 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
Bartosz Golaszewski | e5332d5 | 2018-07-16 10:34:23 +0200 | [diff] [blame] | 508 | /* |
| 509 | * All line descriptors were created at once with the same |
| 510 | * flags so just check if the first one is really output. |
| 511 | */ |
| 512 | if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) |
| 513 | return -EPERM; |
| 514 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 515 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
| 516 | return -EFAULT; |
| 517 | |
| 518 | /* Clamp all values to [0,1] */ |
| 519 | for (i = 0; i < lh->numdescs; i++) |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 520 | __assign_bit(i, vals, ghd.values[i]); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 521 | |
| 522 | /* Reuse the array setting function */ |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 523 | return gpiod_set_array_value_complex(false, |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 524 | true, |
| 525 | lh->numdescs, |
| 526 | lh->descs, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 527 | NULL, |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 528 | vals); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 529 | } |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | #ifdef CONFIG_COMPAT |
| 534 | static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd, |
| 535 | unsigned long arg) |
| 536 | { |
| 537 | return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | static int linehandle_release(struct inode *inode, struct file *filep) |
| 542 | { |
| 543 | struct linehandle_state *lh = filep->private_data; |
| 544 | struct gpio_device *gdev = lh->gdev; |
| 545 | int i; |
| 546 | |
| 547 | for (i = 0; i < lh->numdescs; i++) |
| 548 | gpiod_free(lh->descs[i]); |
| 549 | kfree(lh->label); |
| 550 | kfree(lh); |
| 551 | put_device(&gdev->dev); |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static const struct file_operations linehandle_fileops = { |
| 556 | .release = linehandle_release, |
| 557 | .owner = THIS_MODULE, |
| 558 | .llseek = noop_llseek, |
| 559 | .unlocked_ioctl = linehandle_ioctl, |
| 560 | #ifdef CONFIG_COMPAT |
| 561 | .compat_ioctl = linehandle_ioctl_compat, |
| 562 | #endif |
| 563 | }; |
| 564 | |
| 565 | static int linehandle_create(struct gpio_device *gdev, void __user *ip) |
| 566 | { |
| 567 | struct gpiohandle_request handlereq; |
| 568 | struct linehandle_state *lh; |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 569 | struct file *file; |
Timur Tabi | ab3dbcf | 2018-03-29 13:29:12 -0500 | [diff] [blame] | 570 | int fd, i, count = 0, ret; |
Bartosz Golaszewski | 418ee8e | 2017-10-16 11:32:29 +0200 | [diff] [blame] | 571 | u32 lflags; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 572 | |
| 573 | if (copy_from_user(&handlereq, ip, sizeof(handlereq))) |
| 574 | return -EFAULT; |
| 575 | if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) |
| 576 | return -EINVAL; |
| 577 | |
Bartosz Golaszewski | 418ee8e | 2017-10-16 11:32:29 +0200 | [diff] [blame] | 578 | lflags = handlereq.flags; |
| 579 | |
Kent Gibson | b043ed7 | 2019-11-05 10:04:28 +0800 | [diff] [blame^] | 580 | ret = linehandle_validate_flags(lflags); |
| 581 | if (ret) |
| 582 | return ret; |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 583 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 584 | lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
| 585 | if (!lh) |
| 586 | return -ENOMEM; |
| 587 | lh->gdev = gdev; |
| 588 | get_device(&gdev->dev); |
| 589 | |
| 590 | /* Make sure this is terminated */ |
| 591 | handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; |
| 592 | if (strlen(handlereq.consumer_label)) { |
| 593 | lh->label = kstrdup(handlereq.consumer_label, |
| 594 | GFP_KERNEL); |
| 595 | if (!lh->label) { |
| 596 | ret = -ENOMEM; |
| 597 | goto out_free_lh; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* Request each GPIO */ |
| 602 | for (i = 0; i < handlereq.lines; i++) { |
| 603 | u32 offset = handlereq.lineoffsets[i]; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 604 | struct gpio_desc *desc; |
| 605 | |
Lars-Peter Clausen | e405f9f | 2016-10-18 16:54:01 +0200 | [diff] [blame] | 606 | if (offset >= gdev->ngpio) { |
| 607 | ret = -EINVAL; |
| 608 | goto out_free_descs; |
| 609 | } |
| 610 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 611 | desc = &gdev->descs[offset]; |
| 612 | ret = gpiod_request(desc, lh->label); |
| 613 | if (ret) |
| 614 | goto out_free_descs; |
| 615 | lh->descs[i] = desc; |
Ricardo Ribalda Delgado | 19a4fbf | 2018-09-13 15:37:04 +0200 | [diff] [blame] | 616 | count = i + 1; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 617 | |
| 618 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) |
| 619 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 620 | if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) |
| 621 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 622 | if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) |
| 623 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 624 | if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) |
| 625 | set_bit(FLAG_BIAS_DISABLE, &desc->flags); |
Drew Fustini | 9225d51 | 2019-11-05 10:04:23 +0800 | [diff] [blame] | 626 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) |
| 627 | set_bit(FLAG_PULL_DOWN, &desc->flags); |
| 628 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) |
| 629 | set_bit(FLAG_PULL_UP, &desc->flags); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 630 | |
Andrew Jeffery | e10f72b | 2017-11-30 14:25:24 +1030 | [diff] [blame] | 631 | ret = gpiod_set_transitory(desc, false); |
| 632 | if (ret < 0) |
| 633 | goto out_free_descs; |
| 634 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 635 | /* |
| 636 | * Lines have to be requested explicitly for input |
| 637 | * or output, else the line will be treated "as is". |
| 638 | */ |
| 639 | if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
| 640 | int val = !!handlereq.default_values[i]; |
| 641 | |
| 642 | ret = gpiod_direction_output(desc, val); |
| 643 | if (ret) |
| 644 | goto out_free_descs; |
| 645 | } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
| 646 | ret = gpiod_direction_input(desc); |
| 647 | if (ret) |
| 648 | goto out_free_descs; |
| 649 | } |
| 650 | dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", |
| 651 | offset); |
| 652 | } |
Linus Walleij | e2f608b | 2016-06-18 10:56:43 +0200 | [diff] [blame] | 653 | /* Let i point at the last handle */ |
| 654 | i--; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 655 | lh->numdescs = handlereq.lines; |
| 656 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 657 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 658 | if (fd < 0) { |
| 659 | ret = fd; |
| 660 | goto out_free_descs; |
| 661 | } |
| 662 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 663 | file = anon_inode_getfile("gpio-linehandle", |
| 664 | &linehandle_fileops, |
| 665 | lh, |
| 666 | O_RDONLY | O_CLOEXEC); |
| 667 | if (IS_ERR(file)) { |
| 668 | ret = PTR_ERR(file); |
| 669 | goto out_put_unused_fd; |
| 670 | } |
| 671 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 672 | handlereq.fd = fd; |
Linus Walleij | d932cd4 | 2016-07-04 13:13:04 +0200 | [diff] [blame] | 673 | if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 674 | /* |
| 675 | * fput() will trigger the release() callback, so do not go onto |
| 676 | * the regular error cleanup path here. |
| 677 | */ |
| 678 | fput(file); |
| 679 | put_unused_fd(fd); |
| 680 | return -EFAULT; |
Linus Walleij | d932cd4 | 2016-07-04 13:13:04 +0200 | [diff] [blame] | 681 | } |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 682 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 683 | fd_install(fd, file); |
| 684 | |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 685 | dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
| 686 | lh->numdescs); |
| 687 | |
| 688 | return 0; |
| 689 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 690 | out_put_unused_fd: |
| 691 | put_unused_fd(fd); |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 692 | out_free_descs: |
Timur Tabi | ab3dbcf | 2018-03-29 13:29:12 -0500 | [diff] [blame] | 693 | for (i = 0; i < count; i++) |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 694 | gpiod_free(lh->descs[i]); |
| 695 | kfree(lh->label); |
| 696 | out_free_lh: |
| 697 | kfree(lh); |
| 698 | put_device(&gdev->dev); |
| 699 | return ret; |
| 700 | } |
| 701 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 702 | /* |
| 703 | * GPIO line event management |
| 704 | */ |
| 705 | |
| 706 | /** |
| 707 | * struct lineevent_state - contains the state of a userspace event |
| 708 | * @gdev: the GPIO device the event pertains to |
| 709 | * @label: consumer label used to tag descriptors |
| 710 | * @desc: the GPIO descriptor held by this event |
| 711 | * @eflags: the event flags this line was requested with |
| 712 | * @irq: the interrupt that trigger in response to events on this GPIO |
| 713 | * @wait: wait queue that handles blocking reads of events |
| 714 | * @events: KFIFO for the GPIO events |
| 715 | * @read_lock: mutex lock to protect reads from colliding with adding |
| 716 | * new events to the FIFO |
Linus Walleij | d58f2bf | 2017-11-30 10:23:27 +0100 | [diff] [blame] | 717 | * @timestamp: cache for the timestamp storing it between hardirq |
| 718 | * and IRQ thread, used to bring the timestamp close to the actual |
| 719 | * event |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 720 | */ |
| 721 | struct lineevent_state { |
| 722 | struct gpio_device *gdev; |
| 723 | const char *label; |
| 724 | struct gpio_desc *desc; |
| 725 | u32 eflags; |
| 726 | int irq; |
| 727 | wait_queue_head_t wait; |
| 728 | DECLARE_KFIFO(events, struct gpioevent_data, 16); |
| 729 | struct mutex read_lock; |
Linus Walleij | d58f2bf | 2017-11-30 10:23:27 +0100 | [diff] [blame] | 730 | u64 timestamp; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 731 | }; |
| 732 | |
Lars-Peter Clausen | ac7dbb9 | 2016-10-18 16:54:06 +0200 | [diff] [blame] | 733 | #define GPIOEVENT_REQUEST_VALID_FLAGS \ |
| 734 | (GPIOEVENT_REQUEST_RISING_EDGE | \ |
| 735 | GPIOEVENT_REQUEST_FALLING_EDGE) |
| 736 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 737 | static __poll_t lineevent_poll(struct file *filep, |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 738 | struct poll_table_struct *wait) |
| 739 | { |
| 740 | struct lineevent_state *le = filep->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 741 | __poll_t events = 0; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 742 | |
| 743 | poll_wait(filep, &le->wait, wait); |
| 744 | |
| 745 | if (!kfifo_is_empty(&le->events)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 746 | events = EPOLLIN | EPOLLRDNORM; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 747 | |
| 748 | return events; |
| 749 | } |
| 750 | |
| 751 | |
| 752 | static ssize_t lineevent_read(struct file *filep, |
| 753 | char __user *buf, |
| 754 | size_t count, |
| 755 | loff_t *f_ps) |
| 756 | { |
| 757 | struct lineevent_state *le = filep->private_data; |
| 758 | unsigned int copied; |
| 759 | int ret; |
| 760 | |
| 761 | if (count < sizeof(struct gpioevent_data)) |
| 762 | return -EINVAL; |
| 763 | |
| 764 | do { |
| 765 | if (kfifo_is_empty(&le->events)) { |
| 766 | if (filep->f_flags & O_NONBLOCK) |
| 767 | return -EAGAIN; |
| 768 | |
| 769 | ret = wait_event_interruptible(le->wait, |
| 770 | !kfifo_is_empty(&le->events)); |
| 771 | if (ret) |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | if (mutex_lock_interruptible(&le->read_lock)) |
| 776 | return -ERESTARTSYS; |
| 777 | ret = kfifo_to_user(&le->events, buf, count, &copied); |
| 778 | mutex_unlock(&le->read_lock); |
| 779 | |
| 780 | if (ret) |
| 781 | return ret; |
| 782 | |
| 783 | /* |
| 784 | * If we couldn't read anything from the fifo (a different |
| 785 | * thread might have been faster) we either return -EAGAIN if |
| 786 | * the file descriptor is non-blocking, otherwise we go back to |
| 787 | * sleep and wait for more data to arrive. |
| 788 | */ |
| 789 | if (copied == 0 && (filep->f_flags & O_NONBLOCK)) |
| 790 | return -EAGAIN; |
| 791 | |
| 792 | } while (copied == 0); |
| 793 | |
| 794 | return copied; |
| 795 | } |
| 796 | |
| 797 | static int lineevent_release(struct inode *inode, struct file *filep) |
| 798 | { |
| 799 | struct lineevent_state *le = filep->private_data; |
| 800 | struct gpio_device *gdev = le->gdev; |
| 801 | |
| 802 | free_irq(le->irq, le); |
| 803 | gpiod_free(le->desc); |
| 804 | kfree(le->label); |
| 805 | kfree(le); |
| 806 | put_device(&gdev->dev); |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static long lineevent_ioctl(struct file *filep, unsigned int cmd, |
| 811 | unsigned long arg) |
| 812 | { |
| 813 | struct lineevent_state *le = filep->private_data; |
| 814 | void __user *ip = (void __user *)arg; |
| 815 | struct gpiohandle_data ghd; |
| 816 | |
| 817 | /* |
| 818 | * We can get the value for an event line but not set it, |
| 819 | * because it is input by definition. |
| 820 | */ |
| 821 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
| 822 | int val; |
| 823 | |
Lars-Peter Clausen | d82aa4a | 2016-10-18 16:54:04 +0200 | [diff] [blame] | 824 | memset(&ghd, 0, sizeof(ghd)); |
| 825 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 826 | val = gpiod_get_value_cansleep(le->desc); |
| 827 | if (val < 0) |
| 828 | return val; |
| 829 | ghd.values[0] = val; |
| 830 | |
| 831 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
| 832 | return -EFAULT; |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | |
| 839 | #ifdef CONFIG_COMPAT |
| 840 | static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd, |
| 841 | unsigned long arg) |
| 842 | { |
| 843 | return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); |
| 844 | } |
| 845 | #endif |
| 846 | |
| 847 | static const struct file_operations lineevent_fileops = { |
| 848 | .release = lineevent_release, |
| 849 | .read = lineevent_read, |
| 850 | .poll = lineevent_poll, |
| 851 | .owner = THIS_MODULE, |
| 852 | .llseek = noop_llseek, |
| 853 | .unlocked_ioctl = lineevent_ioctl, |
| 854 | #ifdef CONFIG_COMPAT |
| 855 | .compat_ioctl = lineevent_ioctl_compat, |
| 856 | #endif |
| 857 | }; |
| 858 | |
Ben Dooks | 33265b1 | 2016-06-17 16:03:13 +0100 | [diff] [blame] | 859 | static irqreturn_t lineevent_irq_thread(int irq, void *p) |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 860 | { |
| 861 | struct lineevent_state *le = p; |
| 862 | struct gpioevent_data ge; |
Uwe Kleine-König | fa38869 | 2018-08-20 08:32:53 +0200 | [diff] [blame] | 863 | int ret; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 864 | |
Linus Walleij | 24bd3ef | 2018-01-22 13:19:28 +0100 | [diff] [blame] | 865 | /* Do not leak kernel stack to userspace */ |
| 866 | memset(&ge, 0, sizeof(ge)); |
| 867 | |
Bartosz Golaszewski | 1033be5 | 2019-01-04 11:24:20 +0100 | [diff] [blame] | 868 | /* |
| 869 | * We may be running from a nested threaded interrupt in which case |
| 870 | * we didn't get the timestamp from lineevent_irq_handler(). |
| 871 | */ |
| 872 | if (!le->timestamp) |
| 873 | ge.timestamp = ktime_get_real_ns(); |
| 874 | else |
| 875 | ge.timestamp = le->timestamp; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 876 | |
Bartosz Golaszewski | ad537b8 | 2017-06-23 13:45:16 +0200 | [diff] [blame] | 877 | if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE |
| 878 | && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
Uwe Kleine-König | fa38869 | 2018-08-20 08:32:53 +0200 | [diff] [blame] | 879 | int level = gpiod_get_value_cansleep(le->desc); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 880 | if (level) |
| 881 | /* Emit low-to-high event */ |
| 882 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
| 883 | else |
| 884 | /* Emit high-to-low event */ |
| 885 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
Uwe Kleine-König | fa38869 | 2018-08-20 08:32:53 +0200 | [diff] [blame] | 886 | } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) { |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 887 | /* Emit low-to-high event */ |
| 888 | ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
Uwe Kleine-König | fa38869 | 2018-08-20 08:32:53 +0200 | [diff] [blame] | 889 | } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 890 | /* Emit high-to-low event */ |
| 891 | ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
Arnd Bergmann | bc0207a | 2016-06-16 11:02:41 +0200 | [diff] [blame] | 892 | } else { |
| 893 | return IRQ_NONE; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | ret = kfifo_put(&le->events, ge); |
Saiyam Doshi | 2efc6bf | 2019-09-07 23:09:10 +0530 | [diff] [blame] | 897 | if (ret) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 898 | wake_up_poll(&le->wait, EPOLLIN); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 899 | |
| 900 | return IRQ_HANDLED; |
| 901 | } |
| 902 | |
Linus Walleij | d58f2bf | 2017-11-30 10:23:27 +0100 | [diff] [blame] | 903 | static irqreturn_t lineevent_irq_handler(int irq, void *p) |
| 904 | { |
| 905 | struct lineevent_state *le = p; |
| 906 | |
| 907 | /* |
| 908 | * Just store the timestamp in hardirq context so we get it as |
| 909 | * close in time as possible to the actual event. |
| 910 | */ |
| 911 | le->timestamp = ktime_get_real_ns(); |
| 912 | |
| 913 | return IRQ_WAKE_THREAD; |
| 914 | } |
| 915 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 916 | static int lineevent_create(struct gpio_device *gdev, void __user *ip) |
| 917 | { |
| 918 | struct gpioevent_request eventreq; |
| 919 | struct lineevent_state *le; |
| 920 | struct gpio_desc *desc; |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 921 | struct file *file; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 922 | u32 offset; |
| 923 | u32 lflags; |
| 924 | u32 eflags; |
| 925 | int fd; |
| 926 | int ret; |
| 927 | int irqflags = 0; |
| 928 | |
| 929 | if (copy_from_user(&eventreq, ip, sizeof(eventreq))) |
| 930 | return -EFAULT; |
| 931 | |
Bartosz Golaszewski | bcc6d99 | 2019-09-16 11:46:23 +0200 | [diff] [blame] | 932 | offset = eventreq.lineoffset; |
| 933 | lflags = eventreq.handleflags; |
| 934 | eflags = eventreq.eventflags; |
| 935 | |
| 936 | if (offset >= gdev->ngpio) |
| 937 | return -EINVAL; |
| 938 | |
| 939 | /* Return an error if a unknown flag is set */ |
| 940 | if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || |
| 941 | (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) |
| 942 | return -EINVAL; |
| 943 | |
| 944 | /* This is just wrong: we don't look for events on output lines */ |
| 945 | if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || |
| 946 | (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
| 947 | (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
| 948 | return -EINVAL; |
| 949 | |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 950 | /* Only one bias flag can be set. */ |
| 951 | if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) && |
| 952 | (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | |
| 953 | GPIOHANDLE_REQUEST_BIAS_PULL_UP))) || |
| 954 | ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) && |
| 955 | (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP))) |
| 956 | return -EINVAL; |
| 957 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 958 | le = kzalloc(sizeof(*le), GFP_KERNEL); |
| 959 | if (!le) |
| 960 | return -ENOMEM; |
| 961 | le->gdev = gdev; |
| 962 | get_device(&gdev->dev); |
| 963 | |
| 964 | /* Make sure this is terminated */ |
| 965 | eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; |
| 966 | if (strlen(eventreq.consumer_label)) { |
| 967 | le->label = kstrdup(eventreq.consumer_label, |
| 968 | GFP_KERNEL); |
| 969 | if (!le->label) { |
| 970 | ret = -ENOMEM; |
| 971 | goto out_free_le; |
| 972 | } |
| 973 | } |
| 974 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 975 | desc = &gdev->descs[offset]; |
| 976 | ret = gpiod_request(desc, le->label); |
| 977 | if (ret) |
Uwe Kleine-König | f001cc3 | 2018-04-16 13:17:53 +0200 | [diff] [blame] | 978 | goto out_free_label; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 979 | le->desc = desc; |
| 980 | le->eflags = eflags; |
| 981 | |
| 982 | if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) |
| 983 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 984 | if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) |
| 985 | set_bit(FLAG_BIAS_DISABLE, &desc->flags); |
Kent Gibson | 7b479a8 | 2019-11-05 10:04:24 +0800 | [diff] [blame] | 986 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) |
| 987 | set_bit(FLAG_PULL_DOWN, &desc->flags); |
| 988 | if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) |
| 989 | set_bit(FLAG_PULL_UP, &desc->flags); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 990 | |
| 991 | ret = gpiod_direction_input(desc); |
| 992 | if (ret) |
| 993 | goto out_free_desc; |
| 994 | |
| 995 | le->irq = gpiod_to_irq(desc); |
| 996 | if (le->irq <= 0) { |
| 997 | ret = -ENODEV; |
| 998 | goto out_free_desc; |
| 999 | } |
| 1000 | |
| 1001 | if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) |
Michael Wu | 223ecaf | 2019-07-08 13:23:08 +0800 | [diff] [blame] | 1002 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 1003 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1004 | if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) |
Michael Wu | 223ecaf | 2019-07-08 13:23:08 +0800 | [diff] [blame] | 1005 | irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 1006 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1007 | irqflags |= IRQF_ONESHOT; |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1008 | |
| 1009 | INIT_KFIFO(le->events); |
| 1010 | init_waitqueue_head(&le->wait); |
| 1011 | mutex_init(&le->read_lock); |
| 1012 | |
| 1013 | /* Request a thread to read the events */ |
| 1014 | ret = request_threaded_irq(le->irq, |
Linus Walleij | d58f2bf | 2017-11-30 10:23:27 +0100 | [diff] [blame] | 1015 | lineevent_irq_handler, |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1016 | lineevent_irq_thread, |
| 1017 | irqflags, |
| 1018 | le->label, |
| 1019 | le); |
| 1020 | if (ret) |
| 1021 | goto out_free_desc; |
| 1022 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 1023 | fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1024 | if (fd < 0) { |
| 1025 | ret = fd; |
| 1026 | goto out_free_irq; |
| 1027 | } |
| 1028 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 1029 | file = anon_inode_getfile("gpio-event", |
| 1030 | &lineevent_fileops, |
| 1031 | le, |
| 1032 | O_RDONLY | O_CLOEXEC); |
| 1033 | if (IS_ERR(file)) { |
| 1034 | ret = PTR_ERR(file); |
| 1035 | goto out_put_unused_fd; |
| 1036 | } |
| 1037 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1038 | eventreq.fd = fd; |
Linus Walleij | d932cd4 | 2016-07-04 13:13:04 +0200 | [diff] [blame] | 1039 | if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 1040 | /* |
| 1041 | * fput() will trigger the release() callback, so do not go onto |
| 1042 | * the regular error cleanup path here. |
| 1043 | */ |
| 1044 | fput(file); |
| 1045 | put_unused_fd(fd); |
| 1046 | return -EFAULT; |
Linus Walleij | d932cd4 | 2016-07-04 13:13:04 +0200 | [diff] [blame] | 1047 | } |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1048 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 1049 | fd_install(fd, file); |
| 1050 | |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1051 | return 0; |
| 1052 | |
Lars-Peter Clausen | 953b956 | 2016-10-24 13:59:15 +0200 | [diff] [blame] | 1053 | out_put_unused_fd: |
| 1054 | put_unused_fd(fd); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1055 | out_free_irq: |
| 1056 | free_irq(le->irq, le); |
| 1057 | out_free_desc: |
| 1058 | gpiod_free(le->desc); |
| 1059 | out_free_label: |
| 1060 | kfree(le->label); |
| 1061 | out_free_le: |
| 1062 | kfree(le); |
| 1063 | put_device(&gdev->dev); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 1067 | /* |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1068 | * gpio_ioctl() - ioctl handler for the GPIO chardev |
| 1069 | */ |
| 1070 | static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1071 | { |
| 1072 | struct gpio_device *gdev = filp->private_data; |
| 1073 | struct gpio_chip *chip = gdev->chip; |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 1074 | void __user *ip = (void __user *)arg; |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1075 | |
| 1076 | /* We fail any subsequent ioctl():s when the chip is gone */ |
| 1077 | if (!chip) |
| 1078 | return -ENODEV; |
| 1079 | |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1080 | /* Fill in the struct and pass to userspace */ |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1081 | if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1082 | struct gpiochip_info chipinfo; |
| 1083 | |
Lars-Peter Clausen | 0f4bbb2 | 2016-10-18 16:54:00 +0200 | [diff] [blame] | 1084 | memset(&chipinfo, 0, sizeof(chipinfo)); |
| 1085 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1086 | strncpy(chipinfo.name, dev_name(&gdev->dev), |
| 1087 | sizeof(chipinfo.name)); |
| 1088 | chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1089 | strncpy(chipinfo.label, gdev->label, |
| 1090 | sizeof(chipinfo.label)); |
| 1091 | chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1092 | chipinfo.lines = gdev->ngpio; |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1093 | if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
| 1094 | return -EFAULT; |
| 1095 | return 0; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1096 | } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
| 1097 | struct gpioline_info lineinfo; |
| 1098 | struct gpio_desc *desc; |
| 1099 | |
| 1100 | if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
| 1101 | return -EFAULT; |
Lars-Peter Clausen | 1f1cc45 | 2016-10-18 16:53:59 +0200 | [diff] [blame] | 1102 | if (lineinfo.line_offset >= gdev->ngpio) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1103 | return -EINVAL; |
| 1104 | |
| 1105 | desc = &gdev->descs[lineinfo.line_offset]; |
| 1106 | if (desc->name) { |
| 1107 | strncpy(lineinfo.name, desc->name, |
| 1108 | sizeof(lineinfo.name)); |
| 1109 | lineinfo.name[sizeof(lineinfo.name)-1] = '\0'; |
| 1110 | } else { |
| 1111 | lineinfo.name[0] = '\0'; |
| 1112 | } |
| 1113 | if (desc->label) { |
Linus Walleij | 214338e | 2016-02-25 21:01:48 +0100 | [diff] [blame] | 1114 | strncpy(lineinfo.consumer, desc->label, |
| 1115 | sizeof(lineinfo.consumer)); |
| 1116 | lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0'; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1117 | } else { |
Linus Walleij | 214338e | 2016-02-25 21:01:48 +0100 | [diff] [blame] | 1118 | lineinfo.consumer[0] = '\0'; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Userspace only need to know that the kernel is using |
| 1123 | * this GPIO so it can't use it. |
| 1124 | */ |
| 1125 | lineinfo.flags = 0; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 1126 | if (test_bit(FLAG_REQUESTED, &desc->flags) || |
| 1127 | test_bit(FLAG_IS_HOGGED, &desc->flags) || |
| 1128 | test_bit(FLAG_USED_AS_IRQ, &desc->flags) || |
| 1129 | test_bit(FLAG_EXPORT, &desc->flags) || |
Stefan Wahren | 472a61e | 2019-08-14 14:00:35 +0300 | [diff] [blame] | 1130 | test_bit(FLAG_SYSFS, &desc->flags) || |
| 1131 | !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1132 | lineinfo.flags |= GPIOLINE_FLAG_KERNEL; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 1133 | if (test_bit(FLAG_IS_OUT, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1134 | lineinfo.flags |= GPIOLINE_FLAG_IS_OUT; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 1135 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1136 | lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW; |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 1137 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
Bartosz Golaszewski | 2c60e6b | 2019-08-06 13:41:51 +0200 | [diff] [blame] | 1138 | lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN | |
| 1139 | GPIOLINE_FLAG_IS_OUT); |
Linus Walleij | 9d8cc89 | 2016-02-22 13:44:53 +0100 | [diff] [blame] | 1140 | if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
Bartosz Golaszewski | 2c60e6b | 2019-08-06 13:41:51 +0200 | [diff] [blame] | 1141 | lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE | |
| 1142 | GPIOLINE_FLAG_IS_OUT); |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 1143 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) |
| 1144 | lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE; |
Drew Fustini | 9225d51 | 2019-11-05 10:04:23 +0800 | [diff] [blame] | 1145 | if (test_bit(FLAG_PULL_DOWN, &desc->flags)) |
| 1146 | lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN; |
| 1147 | if (test_bit(FLAG_PULL_UP, &desc->flags)) |
| 1148 | lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 1149 | |
| 1150 | if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) |
| 1151 | return -EFAULT; |
| 1152 | return 0; |
Linus Walleij | d7c51b4 | 2016-04-26 10:35:29 +0200 | [diff] [blame] | 1153 | } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
| 1154 | return linehandle_create(gdev, ip); |
Linus Walleij | 61f922d | 2016-06-02 11:30:15 +0200 | [diff] [blame] | 1155 | } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
| 1156 | return lineevent_create(gdev, ip); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1157 | } |
| 1158 | return -EINVAL; |
| 1159 | } |
| 1160 | |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 1161 | #ifdef CONFIG_COMPAT |
| 1162 | static long gpio_ioctl_compat(struct file *filp, unsigned int cmd, |
| 1163 | unsigned long arg) |
| 1164 | { |
| 1165 | return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
| 1166 | } |
| 1167 | #endif |
| 1168 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1169 | /** |
| 1170 | * gpio_chrdev_open() - open the chardev for ioctl operations |
| 1171 | * @inode: inode for this chardev |
| 1172 | * @filp: file struct for storing private data |
| 1173 | * Returns 0 on success |
| 1174 | */ |
| 1175 | static int gpio_chrdev_open(struct inode *inode, struct file *filp) |
| 1176 | { |
| 1177 | struct gpio_device *gdev = container_of(inode->i_cdev, |
| 1178 | struct gpio_device, chrdev); |
| 1179 | |
| 1180 | /* Fail on open if the backing gpiochip is gone */ |
Stephen Boyd | fb50574 | 2017-01-09 11:47:44 -0800 | [diff] [blame] | 1181 | if (!gdev->chip) |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1182 | return -ENODEV; |
| 1183 | get_device(&gdev->dev); |
| 1184 | filp->private_data = gdev; |
Lars-Peter Clausen | f4e81c5 | 2016-11-30 13:05:21 +0100 | [diff] [blame] | 1185 | |
| 1186 | return nonseekable_open(inode, filp); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * gpio_chrdev_release() - close chardev after ioctl operations |
| 1191 | * @inode: inode for this chardev |
| 1192 | * @filp: file struct for storing private data |
| 1193 | * Returns 0 on success |
| 1194 | */ |
| 1195 | static int gpio_chrdev_release(struct inode *inode, struct file *filp) |
| 1196 | { |
| 1197 | struct gpio_device *gdev = container_of(inode->i_cdev, |
| 1198 | struct gpio_device, chrdev); |
| 1199 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1200 | put_device(&gdev->dev); |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | static const struct file_operations gpio_fileops = { |
| 1206 | .release = gpio_chrdev_release, |
| 1207 | .open = gpio_chrdev_open, |
| 1208 | .owner = THIS_MODULE, |
Lars-Peter Clausen | f4e81c5 | 2016-11-30 13:05:21 +0100 | [diff] [blame] | 1209 | .llseek = no_llseek, |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1210 | .unlocked_ioctl = gpio_ioctl, |
Linus Walleij | 8b92e17 | 2016-05-27 14:24:04 +0200 | [diff] [blame] | 1211 | #ifdef CONFIG_COMPAT |
| 1212 | .compat_ioctl = gpio_ioctl_compat, |
| 1213 | #endif |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1214 | }; |
| 1215 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1216 | static void gpiodevice_release(struct device *dev) |
| 1217 | { |
| 1218 | struct gpio_device *gdev = dev_get_drvdata(dev); |
| 1219 | |
| 1220 | list_del(&gdev->list); |
| 1221 | ida_simple_remove(&gpio_ida, gdev->id); |
Bartosz Golaszewski | fcf273e5 | 2017-12-14 15:29:20 +0100 | [diff] [blame] | 1222 | kfree_const(gdev->label); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1223 | kfree(gdev->descs); |
Linus Walleij | 9efd9e6 | 2016-02-09 14:27:42 +0100 | [diff] [blame] | 1224 | kfree(gdev); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1227 | static int gpiochip_setup_dev(struct gpio_device *gdev) |
| 1228 | { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1229 | int ret; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1230 | |
| 1231 | cdev_init(&gdev->chrdev, &gpio_fileops); |
| 1232 | gdev->chrdev.owner = THIS_MODULE; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1233 | gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id); |
Logan Gunthorpe | 111379d | 2017-03-17 12:48:12 -0600 | [diff] [blame] | 1234 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1235 | ret = cdev_device_add(&gdev->chrdev, &gdev->dev); |
| 1236 | if (ret) |
| 1237 | return ret; |
Logan Gunthorpe | 111379d | 2017-03-17 12:48:12 -0600 | [diff] [blame] | 1238 | |
| 1239 | chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", |
| 1240 | MAJOR(gpio_devt), gdev->id); |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1241 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1242 | ret = gpiochip_sysfs_register(gdev); |
| 1243 | if (ret) |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1244 | goto err_remove_device; |
| 1245 | |
| 1246 | /* From this point, the .release() function cleans up gpio_device */ |
| 1247 | gdev->dev.release = gpiodevice_release; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1248 | pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n", |
| 1249 | __func__, gdev->base, gdev->base + gdev->ngpio - 1, |
| 1250 | dev_name(&gdev->dev), gdev->chip->label ? : "generic"); |
| 1251 | |
| 1252 | return 0; |
| 1253 | |
| 1254 | err_remove_device: |
Logan Gunthorpe | 111379d | 2017-03-17 12:48:12 -0600 | [diff] [blame] | 1255 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1256 | return ret; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Bartosz Golaszewski | a411e81 | 2018-04-10 22:30:28 +0200 | [diff] [blame] | 1259 | static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog) |
| 1260 | { |
| 1261 | struct gpio_desc *desc; |
| 1262 | int rv; |
| 1263 | |
| 1264 | desc = gpiochip_get_desc(chip, hog->chip_hwnum); |
| 1265 | if (IS_ERR(desc)) { |
| 1266 | pr_err("%s: unable to get GPIO desc: %ld\n", |
| 1267 | __func__, PTR_ERR(desc)); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
Dan Carpenter | ba3efdf | 2018-05-01 10:36:39 +0300 | [diff] [blame] | 1271 | if (test_bit(FLAG_IS_HOGGED, &desc->flags)) |
Bartosz Golaszewski | a411e81 | 2018-04-10 22:30:28 +0200 | [diff] [blame] | 1272 | return; |
| 1273 | |
| 1274 | rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); |
| 1275 | if (rv) |
| 1276 | pr_err("%s: unable to hog GPIO line (%s:%u): %d\n", |
| 1277 | __func__, chip->label, hog->chip_hwnum, rv); |
| 1278 | } |
| 1279 | |
| 1280 | static void machine_gpiochip_add(struct gpio_chip *chip) |
| 1281 | { |
| 1282 | struct gpiod_hog *hog; |
| 1283 | |
| 1284 | mutex_lock(&gpio_machine_hogs_mutex); |
| 1285 | |
| 1286 | list_for_each_entry(hog, &gpio_machine_hogs, list) { |
| 1287 | if (!strcmp(chip->label, hog->chip_label)) |
| 1288 | gpiochip_machine_hog(chip, hog); |
| 1289 | } |
| 1290 | |
| 1291 | mutex_unlock(&gpio_machine_hogs_mutex); |
| 1292 | } |
| 1293 | |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1294 | static void gpiochip_setup_devs(void) |
| 1295 | { |
| 1296 | struct gpio_device *gdev; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1297 | int ret; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1298 | |
| 1299 | list_for_each_entry(gdev, &gpio_devices, list) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1300 | ret = gpiochip_setup_dev(gdev); |
| 1301 | if (ret) |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1302 | pr_err("%s: Failed to initialize gpio device (%d)\n", |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1303 | dev_name(&gdev->dev), ret); |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
Thierry Reding | 959bc7b | 2017-11-07 19:15:59 +0100 | [diff] [blame] | 1307 | int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 1308 | struct lock_class_key *lock_key, |
| 1309 | struct lock_class_key *request_key) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1310 | { |
| 1311 | unsigned long flags; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1312 | int ret = 0; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1313 | unsigned i; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1314 | int base = chip->base; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1315 | struct gpio_device *gdev; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1316 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1317 | /* |
| 1318 | * First: allocate and populate the internal stat container, and |
| 1319 | * set up the struct device. |
| 1320 | */ |
Josh Cartwright | 969f07b | 2016-02-17 16:44:15 -0600 | [diff] [blame] | 1321 | gdev = kzalloc(sizeof(*gdev), GFP_KERNEL); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1322 | if (!gdev) |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1323 | return -ENOMEM; |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1324 | gdev->dev.bus = &gpio_bus_type; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1325 | gdev->chip = chip; |
| 1326 | chip->gpiodev = gdev; |
| 1327 | if (chip->parent) { |
| 1328 | gdev->dev.parent = chip->parent; |
| 1329 | gdev->dev.of_node = chip->parent->of_node; |
Thierry Reding | acc6e33 | 2016-07-05 14:11:14 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1332 | #ifdef CONFIG_OF_GPIO |
| 1333 | /* If the gpiochip has an assigned OF node this takes precedence */ |
Thierry Reding | acc6e33 | 2016-07-05 14:11:14 +0200 | [diff] [blame] | 1334 | if (chip->of_node) |
| 1335 | gdev->dev.of_node = chip->of_node; |
Biju Das | 6ff0497 | 2018-08-06 10:48:01 +0100 | [diff] [blame] | 1336 | else |
| 1337 | chip->of_node = gdev->dev.of_node; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1338 | #endif |
Thierry Reding | acc6e33 | 2016-07-05 14:11:14 +0200 | [diff] [blame] | 1339 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1340 | gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); |
| 1341 | if (gdev->id < 0) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1342 | ret = gdev->id; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1343 | goto err_free_gdev; |
| 1344 | } |
| 1345 | dev_set_name(&gdev->dev, "gpiochip%d", gdev->id); |
| 1346 | device_initialize(&gdev->dev); |
| 1347 | dev_set_drvdata(&gdev->dev, gdev); |
| 1348 | if (chip->parent && chip->parent->driver) |
| 1349 | gdev->owner = chip->parent->driver->owner; |
| 1350 | else if (chip->owner) |
| 1351 | /* TODO: remove chip->owner */ |
| 1352 | gdev->owner = chip->owner; |
| 1353 | else |
| 1354 | gdev->owner = THIS_MODULE; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1355 | |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1356 | gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1357 | if (!gdev->descs) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1358 | ret = -ENOMEM; |
Vladimir Zapolskiy | a05a140 | 2018-11-02 15:39:43 +0200 | [diff] [blame] | 1359 | goto err_free_ida; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Bamvor Jian Zhang | 5ed41cc | 2015-11-16 13:02:47 +0800 | [diff] [blame] | 1362 | if (chip->ngpio == 0) { |
| 1363 | chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1364 | ret = -EINVAL; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1365 | goto err_free_descs; |
Bamvor Jian Zhang | 5ed41cc | 2015-11-16 13:02:47 +0800 | [diff] [blame] | 1366 | } |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1367 | |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 1368 | if (chip->ngpio > FASTPATH_NGPIO) |
| 1369 | chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n", |
| 1370 | chip->ngpio, FASTPATH_NGPIO); |
| 1371 | |
Bartosz Golaszewski | fcf273e5 | 2017-12-14 15:29:20 +0100 | [diff] [blame] | 1372 | gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL); |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1373 | if (!gdev->label) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1374 | ret = -ENOMEM; |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1375 | goto err_free_descs; |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 1376 | } |
| 1377 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1378 | gdev->ngpio = chip->ngpio; |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1379 | gdev->data = data; |
Bamvor Jian Zhang | 5ed41cc | 2015-11-16 13:02:47 +0800 | [diff] [blame] | 1380 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1381 | spin_lock_irqsave(&gpio_lock, flags); |
| 1382 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1383 | /* |
| 1384 | * TODO: this allocates a Linux GPIO number base in the global |
| 1385 | * GPIO numberspace for this chip. In the long run we want to |
| 1386 | * get *rid* of this numberspace and use only descriptors, but |
| 1387 | * it may be a pipe dream. It will not happen before we get rid |
| 1388 | * of the sysfs interface anyways. |
| 1389 | */ |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1390 | if (base < 0) { |
| 1391 | base = gpiochip_find_base(chip->ngpio); |
| 1392 | if (base < 0) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1393 | ret = base; |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1394 | spin_unlock_irqrestore(&gpio_lock, flags); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1395 | goto err_free_label; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1396 | } |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1397 | /* |
| 1398 | * TODO: it should not be necessary to reflect the assigned |
| 1399 | * base outside of the GPIO subsystem. Go over drivers and |
| 1400 | * see if anyone makes use of this, else drop this and assign |
| 1401 | * a poison instead. |
| 1402 | */ |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1403 | chip->base = base; |
| 1404 | } |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1405 | gdev->base = base; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1406 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1407 | ret = gpiodev_add_to_list(gdev); |
| 1408 | if (ret) { |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 1409 | spin_unlock_irqrestore(&gpio_lock, flags); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1410 | goto err_free_label; |
Johan Hovold | 05aa520 | 2015-01-12 17:12:26 +0100 | [diff] [blame] | 1411 | } |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1412 | |
Linus Walleij | 545ebd9 | 2016-05-30 17:11:59 +0200 | [diff] [blame] | 1413 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1414 | |
Ricardo Ribalda Delgado | 767cd17 | 2018-10-12 08:11:36 +0200 | [diff] [blame] | 1415 | for (i = 0; i < chip->ngpio; i++) |
| 1416 | gdev->descs[i].gdev = gdev; |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1417 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1418 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 1419 | INIT_LIST_HEAD(&gdev->pin_ranges); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1420 | #endif |
| 1421 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1422 | ret = gpiochip_set_desc_names(chip); |
| 1423 | if (ret) |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 1424 | goto err_remove_from_list; |
| 1425 | |
Linus Walleij | 151a410 | 2019-09-05 11:40:54 +0200 | [diff] [blame] | 1426 | ret = gpiochip_alloc_valid_mask(chip); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1427 | if (ret) |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1428 | goto err_remove_from_list; |
| 1429 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1430 | ret = of_gpiochip_add(chip); |
| 1431 | if (ret) |
Linus Walleij | 48057ed | 2019-08-20 10:05:27 +0200 | [diff] [blame] | 1432 | goto err_free_gpiochip_mask; |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 1433 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1434 | ret = gpiochip_init_valid_mask(chip); |
| 1435 | if (ret) |
Geert Uytterhoeven | 3577989 | 2019-04-24 15:59:33 +0200 | [diff] [blame] | 1436 | goto err_remove_of_chip; |
Ricardo Ribalda Delgado | f8ec92a | 2018-10-05 08:52:58 +0200 | [diff] [blame] | 1437 | |
Ricardo Ribalda Delgado | 3edfb7b | 2018-10-05 08:53:00 +0200 | [diff] [blame] | 1438 | for (i = 0; i < chip->ngpio; i++) { |
| 1439 | struct gpio_desc *desc = &gdev->descs[i]; |
| 1440 | |
Chris Packham | d95da99 | 2019-07-08 08:35:58 +1200 | [diff] [blame] | 1441 | if (chip->get_direction && gpiochip_line_is_valid(chip, i)) { |
| 1442 | if (!chip->get_direction(chip, i)) |
| 1443 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1444 | else |
| 1445 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 1446 | } else { |
| 1447 | if (!chip->direction_input) |
| 1448 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1449 | else |
| 1450 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 1451 | } |
Ricardo Ribalda Delgado | 3edfb7b | 2018-10-05 08:53:00 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Mika Westerberg | 664e3e5 | 2014-01-08 12:40:54 +0200 | [diff] [blame] | 1454 | acpi_gpiochip_add(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1455 | |
Bartosz Golaszewski | a411e81 | 2018-04-10 22:30:28 +0200 | [diff] [blame] | 1456 | machine_gpiochip_add(chip); |
| 1457 | |
Linus Walleij | 504369c | 2019-10-30 13:29:14 +0100 | [diff] [blame] | 1458 | ret = gpiochip_irqchip_init_valid_mask(chip); |
Andy Shevchenko | 9411e3a | 2019-10-09 17:34:44 +0300 | [diff] [blame] | 1459 | if (ret) |
| 1460 | goto err_remove_acpi_chip; |
| 1461 | |
Linus Walleij | 504369c | 2019-10-30 13:29:14 +0100 | [diff] [blame] | 1462 | ret = gpiochip_irqchip_init_hw(chip); |
Linus Walleij | fbdf8d4 | 2019-09-06 12:05:35 +0200 | [diff] [blame] | 1463 | if (ret) |
Linus Walleij | 48057ed | 2019-08-20 10:05:27 +0200 | [diff] [blame] | 1464 | goto err_remove_acpi_chip; |
| 1465 | |
Linus Walleij | fbdf8d4 | 2019-09-06 12:05:35 +0200 | [diff] [blame] | 1466 | ret = gpiochip_add_irqchip(chip, lock_key, request_key); |
| 1467 | if (ret) |
Linus Walleij | 48057ed | 2019-08-20 10:05:27 +0200 | [diff] [blame] | 1468 | goto err_remove_irqchip_mask; |
| 1469 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1470 | /* |
| 1471 | * By first adding the chardev, and then adding the device, |
| 1472 | * we get a device node entry in sysfs under |
| 1473 | * /sys/bus/gpio/devices/gpiochipN/dev that can be used for |
| 1474 | * coldplug of device nodes and other udev business. |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1475 | * We can do this only if gpiolib has been initialized. |
| 1476 | * Otherwise, defer until later. |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 1477 | */ |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1478 | if (gpiolib_initialized) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1479 | ret = gpiochip_setup_dev(gdev); |
| 1480 | if (ret) |
Linus Walleij | 48057ed | 2019-08-20 10:05:27 +0200 | [diff] [blame] | 1481 | goto err_remove_irqchip; |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 1482 | } |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1483 | return 0; |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1484 | |
Linus Walleij | 48057ed | 2019-08-20 10:05:27 +0200 | [diff] [blame] | 1485 | err_remove_irqchip: |
| 1486 | gpiochip_irqchip_remove(chip); |
| 1487 | err_remove_irqchip_mask: |
| 1488 | gpiochip_irqchip_free_valid_mask(chip); |
Geert Uytterhoeven | 3577989 | 2019-04-24 15:59:33 +0200 | [diff] [blame] | 1489 | err_remove_acpi_chip: |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1490 | acpi_gpiochip_remove(chip); |
Geert Uytterhoeven | 3577989 | 2019-04-24 15:59:33 +0200 | [diff] [blame] | 1491 | err_remove_of_chip: |
Johan Hovold | 6d86750 | 2015-05-04 17:23:25 +0200 | [diff] [blame] | 1492 | gpiochip_free_hogs(chip); |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1493 | of_gpiochip_remove(chip); |
Geert Uytterhoeven | 3577989 | 2019-04-24 15:59:33 +0200 | [diff] [blame] | 1494 | err_free_gpiochip_mask: |
Andy Shevchenko | 2f4133b | 2019-11-05 20:06:54 +0200 | [diff] [blame] | 1495 | gpiochip_remove_pin_ranges(chip); |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 1496 | gpiochip_free_valid_mask(chip); |
Markus Pargmann | 5f3ca73 | 2015-08-14 16:11:00 +0200 | [diff] [blame] | 1497 | err_remove_from_list: |
Johan Hovold | 225fce8 | 2015-01-12 17:12:25 +0100 | [diff] [blame] | 1498 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1499 | list_del(&gdev->list); |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1500 | spin_unlock_irqrestore(&gpio_lock, flags); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1501 | err_free_label: |
Bartosz Golaszewski | fcf273e5 | 2017-12-14 15:29:20 +0100 | [diff] [blame] | 1502 | kfree_const(gdev->label); |
Guenter Roeck | 476e2fc | 2016-03-31 08:11:29 -0700 | [diff] [blame] | 1503 | err_free_descs: |
| 1504 | kfree(gdev->descs); |
Vladimir Zapolskiy | a05a140 | 2018-11-02 15:39:43 +0200 | [diff] [blame] | 1505 | err_free_ida: |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1506 | ida_simple_remove(&gpio_ida, gdev->id); |
Vladimir Zapolskiy | a05a140 | 2018-11-02 15:39:43 +0200 | [diff] [blame] | 1507 | err_free_gdev: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1508 | /* failures here can mean systems won't boot... */ |
Marcel Ziswiler | 1777fc9 | 2018-07-20 09:54:49 +0200 | [diff] [blame] | 1509 | pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1510 | gdev->base, gdev->base + gdev->ngpio - 1, |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1511 | chip->label ? : "generic", ret); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1512 | kfree(gdev); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 1513 | return ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1514 | } |
Thierry Reding | 959bc7b | 2017-11-07 19:15:59 +0100 | [diff] [blame] | 1515 | EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1516 | |
| 1517 | /** |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1518 | * gpiochip_get_data() - get per-subdriver data for the chip |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 1519 | * @chip: GPIO chip |
| 1520 | * |
| 1521 | * Returns: |
| 1522 | * The per-subdriver data for the chip. |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1523 | */ |
| 1524 | void *gpiochip_get_data(struct gpio_chip *chip) |
| 1525 | { |
| 1526 | return chip->gpiodev->data; |
| 1527 | } |
| 1528 | EXPORT_SYMBOL_GPL(gpiochip_get_data); |
| 1529 | |
| 1530 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1531 | * gpiochip_remove() - unregister a gpio_chip |
| 1532 | * @chip: the chip to unregister |
| 1533 | * |
| 1534 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 1535 | */ |
abdoulaye berthe | e1db170 | 2014-07-05 18:28:50 +0200 | [diff] [blame] | 1536 | void gpiochip_remove(struct gpio_chip *chip) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1537 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1538 | struct gpio_device *gdev = chip->gpiodev; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1539 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1540 | unsigned long flags; |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1541 | unsigned i; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1542 | bool requested = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1543 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1544 | /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ |
Linus Walleij | afbc4f3 | 2016-02-09 13:21:06 +0100 | [diff] [blame] | 1545 | gpiochip_sysfs_unregister(gdev); |
Geert Uytterhoeven | 5018ada | 2016-12-19 18:29:23 +0100 | [diff] [blame] | 1546 | gpiochip_free_hogs(chip); |
Bamvor Jian Zhang | bd203bd | 2016-02-20 13:13:19 +0800 | [diff] [blame] | 1547 | /* Numb the device, cancelling all outstanding operations */ |
| 1548 | gdev->chip = NULL; |
Johan Hovold | 00acc3d | 2015-01-12 17:12:27 +0100 | [diff] [blame] | 1549 | gpiochip_irqchip_remove(chip); |
Mika Westerberg | 6072b9d | 2014-03-10 14:54:53 +0200 | [diff] [blame] | 1550 | acpi_gpiochip_remove(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1551 | of_gpiochip_remove(chip); |
Andy Shevchenko | 2f4133b | 2019-11-05 20:06:54 +0200 | [diff] [blame] | 1552 | gpiochip_remove_pin_ranges(chip); |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 1553 | gpiochip_free_valid_mask(chip); |
Linus Walleij | 43c54ec | 2016-02-11 11:37:48 +0100 | [diff] [blame] | 1554 | /* |
| 1555 | * We accept no more calls into the driver from this point, so |
| 1556 | * NULL the driver data pointer |
| 1557 | */ |
| 1558 | gdev->data = NULL; |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1559 | |
Johan Hovold | 6798aca | 2015-01-12 17:12:28 +0100 | [diff] [blame] | 1560 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1561 | for (i = 0; i < gdev->ngpio; i++) { |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 1562 | desc = &gdev->descs[i]; |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1563 | if (test_bit(FLAG_REQUESTED, &desc->flags)) |
| 1564 | requested = true; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1565 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1566 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 14e85c0 | 2014-11-19 16:51:27 +0900 | [diff] [blame] | 1567 | |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1568 | if (requested) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 1569 | dev_crit(&gdev->dev, |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1570 | "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
Johan Hovold | fab28b8 | 2015-05-04 17:10:27 +0200 | [diff] [blame] | 1571 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1572 | /* |
| 1573 | * The gpiochip side puts its use of the device to rest here: |
| 1574 | * if there are no userspace clients, the chardev and device will |
| 1575 | * be removed, else it will be dangling until the last user is |
| 1576 | * gone. |
| 1577 | */ |
Logan Gunthorpe | 111379d | 2017-03-17 12:48:12 -0600 | [diff] [blame] | 1578 | cdev_device_del(&gdev->chrdev, &gdev->dev); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1579 | put_device(&gdev->dev); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1580 | } |
| 1581 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 1582 | |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1583 | static void devm_gpio_chip_release(struct device *dev, void *res) |
| 1584 | { |
| 1585 | struct gpio_chip *chip = *(struct gpio_chip **)res; |
| 1586 | |
| 1587 | gpiochip_remove(chip); |
| 1588 | } |
| 1589 | |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1590 | /** |
Jonathan Neuschäfer | 689fd02 | 2017-12-21 17:56:34 +0100 | [diff] [blame] | 1591 | * devm_gpiochip_add_data() - Resource manager gpiochip_add_data() |
Uwe Kleine-König | 3925b90f | 2018-10-08 11:34:03 +0200 | [diff] [blame] | 1592 | * @dev: pointer to the device that gpio_chip belongs to. |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1593 | * @chip: the chip to register, with chip->base initialized |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 1594 | * @data: driver-private data associated with this chip |
| 1595 | * |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1596 | * Context: potentially before irqs will work |
| 1597 | * |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1598 | * The gpio chip automatically be released when the device is unbound. |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 1599 | * |
| 1600 | * Returns: |
| 1601 | * A negative errno if the chip can't be registered, such as because the |
| 1602 | * chip->base is invalid or already associated with a different chip. |
| 1603 | * Otherwise it returns zero as a success code. |
Laxman Dewangan | 0cf3292 | 2016-02-15 16:32:09 +0530 | [diff] [blame] | 1604 | */ |
| 1605 | int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip, |
| 1606 | void *data) |
| 1607 | { |
| 1608 | struct gpio_chip **ptr; |
| 1609 | int ret; |
| 1610 | |
| 1611 | ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr), |
| 1612 | GFP_KERNEL); |
| 1613 | if (!ptr) |
| 1614 | return -ENOMEM; |
| 1615 | |
| 1616 | ret = gpiochip_add_data(chip, data); |
| 1617 | if (ret < 0) { |
| 1618 | devres_free(ptr); |
| 1619 | return ret; |
| 1620 | } |
| 1621 | |
| 1622 | *ptr = chip; |
| 1623 | devres_add(dev, ptr); |
| 1624 | |
| 1625 | return 0; |
| 1626 | } |
| 1627 | EXPORT_SYMBOL_GPL(devm_gpiochip_add_data); |
| 1628 | |
| 1629 | /** |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1630 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 1631 | * @data: data to pass to match function |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 1632 | * @match: Callback function to check gpio_chip |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1633 | * |
| 1634 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 1635 | * determined by a user supplied @match callback. The callback should return |
| 1636 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 1637 | * non-zero, this function will return to the caller and not iterate over any |
| 1638 | * more gpio_chips. |
| 1639 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 1640 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 1641 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 1642 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1643 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1644 | struct gpio_device *gdev; |
Masahiro Yamada | acf06ff | 2016-08-12 01:21:58 +0900 | [diff] [blame] | 1645 | struct gpio_chip *chip = NULL; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1646 | unsigned long flags; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1647 | |
| 1648 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1649 | list_for_each_entry(gdev, &gpio_devices, list) |
Masahiro Yamada | acf06ff | 2016-08-12 01:21:58 +0900 | [diff] [blame] | 1650 | if (gdev->chip && match(gdev->chip, data)) { |
| 1651 | chip = gdev->chip; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1652 | break; |
Masahiro Yamada | acf06ff | 2016-08-12 01:21:58 +0900 | [diff] [blame] | 1653 | } |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 1654 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1655 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1656 | |
| 1657 | return chip; |
| 1658 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 1659 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1660 | |
Alexandre Courbot | 79697ef | 2013-11-16 21:39:32 +0900 | [diff] [blame] | 1661 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
| 1662 | { |
| 1663 | const char *name = data; |
| 1664 | |
| 1665 | return !strcmp(chip->label, name); |
| 1666 | } |
| 1667 | |
| 1668 | static struct gpio_chip *find_chip_by_name(const char *name) |
| 1669 | { |
| 1670 | return gpiochip_find((void *)name, gpiochip_match_name); |
| 1671 | } |
| 1672 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1673 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 1674 | |
| 1675 | /* |
| 1676 | * The following is irqchip helper code for gpiochips. |
| 1677 | */ |
| 1678 | |
Andy Shevchenko | 9411e3a | 2019-10-09 17:34:44 +0300 | [diff] [blame] | 1679 | static int gpiochip_irqchip_init_hw(struct gpio_chip *gc) |
| 1680 | { |
| 1681 | struct gpio_irq_chip *girq = &gc->irq; |
| 1682 | |
| 1683 | if (!girq->init_hw) |
| 1684 | return 0; |
| 1685 | |
| 1686 | return girq->init_hw(gc); |
| 1687 | } |
| 1688 | |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 1689 | static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc) |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1690 | { |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 1691 | struct gpio_irq_chip *girq = &gc->irq; |
| 1692 | |
| 1693 | if (!girq->init_valid_mask) |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1694 | return 0; |
| 1695 | |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 1696 | girq->valid_mask = gpiochip_allocate_mask(gc); |
| 1697 | if (!girq->valid_mask) |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1698 | return -ENOMEM; |
| 1699 | |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 1700 | girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); |
| 1701 | |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1702 | return 0; |
| 1703 | } |
| 1704 | |
| 1705 | static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
| 1706 | { |
Masahiro Yamada | 7bdbd1ec | 2019-07-18 15:51:01 +0900 | [diff] [blame] | 1707 | bitmap_free(gpiochip->irq.valid_mask); |
Thierry Reding | dc7b038 | 2017-11-07 19:15:52 +0100 | [diff] [blame] | 1708 | gpiochip->irq.valid_mask = NULL; |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1709 | } |
| 1710 | |
Stephen Boyd | 64ff2c8 | 2018-01-09 17:58:46 -0800 | [diff] [blame] | 1711 | bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, |
| 1712 | unsigned int offset) |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1713 | { |
Stephen Boyd | 726cb3b | 2018-03-23 09:34:52 -0700 | [diff] [blame] | 1714 | if (!gpiochip_line_is_valid(gpiochip, offset)) |
| 1715 | return false; |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1716 | /* No mask means all valid */ |
Thierry Reding | dc7b038 | 2017-11-07 19:15:52 +0100 | [diff] [blame] | 1717 | if (likely(!gpiochip->irq.valid_mask)) |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1718 | return true; |
Thierry Reding | dc7b038 | 2017-11-07 19:15:52 +0100 | [diff] [blame] | 1719 | return test_bit(offset, gpiochip->irq.valid_mask); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1720 | } |
Stephen Boyd | 64ff2c8 | 2018-01-09 17:58:46 -0800 | [diff] [blame] | 1721 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 1722 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1723 | /** |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1724 | * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip |
Linus Walleij | 4892d3a | 2019-06-14 10:12:26 +0200 | [diff] [blame] | 1725 | * @gc: the gpiochip to set the irqchip chain to |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1726 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 1727 | * chained irqchip |
| 1728 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1729 | * coming out of the gpiochip. If the interrupt is nested rather than |
| 1730 | * cascaded, pass NULL in this handler argument |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1731 | */ |
Linus Walleij | 4892d3a | 2019-06-14 10:12:26 +0200 | [diff] [blame] | 1732 | static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc, |
Thierry Reding | 6f79309 | 2017-04-03 18:05:21 +0200 | [diff] [blame] | 1733 | unsigned int parent_irq, |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1734 | irq_flow_handler_t parent_handler) |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1735 | { |
Linus Walleij | 4892d3a | 2019-06-14 10:12:26 +0200 | [diff] [blame] | 1736 | struct gpio_irq_chip *girq = &gc->irq; |
| 1737 | struct device *dev = &gc->gpiodev->dev; |
| 1738 | |
| 1739 | if (!girq->domain) { |
| 1740 | chip_err(gc, "called %s before setting up irqchip\n", |
Linus Walleij | 83141a7 | 2014-09-26 13:50:12 +0200 | [diff] [blame] | 1741 | __func__); |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 1742 | return; |
| 1743 | } |
| 1744 | |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1745 | if (parent_handler) { |
Linus Walleij | 4892d3a | 2019-06-14 10:12:26 +0200 | [diff] [blame] | 1746 | if (gc->can_sleep) { |
| 1747 | chip_err(gc, |
Andy Shevchenko | b191171 | 2018-07-03 03:39:03 +0300 | [diff] [blame] | 1748 | "you cannot have chained interrupts on a chip that may sleep\n"); |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1749 | return; |
| 1750 | } |
Linus Walleij | 4892d3a | 2019-06-14 10:12:26 +0200 | [diff] [blame] | 1751 | girq->parents = devm_kcalloc(dev, 1, |
| 1752 | sizeof(*girq->parents), |
| 1753 | GFP_KERNEL); |
| 1754 | if (!girq->parents) { |
| 1755 | chip_err(gc, "out of memory allocating parent IRQ\n"); |
| 1756 | return; |
| 1757 | } |
| 1758 | girq->parents[0] = parent_irq; |
| 1759 | girq->num_parents = 1; |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1760 | /* |
| 1761 | * The parent irqchip is already using the chip_data for this |
| 1762 | * irqchip, so our callbacks simply use the handler_data. |
| 1763 | */ |
Thomas Gleixner | f7f8775 | 2015-06-21 21:10:48 +0200 | [diff] [blame] | 1764 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
Linus Walleij | 4892d3a | 2019-06-14 10:12:26 +0200 | [diff] [blame] | 1765 | gc); |
Linus Walleij | 3f97d5fc | 2014-09-26 14:19:52 +0200 | [diff] [blame] | 1766 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1767 | } |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1768 | |
| 1769 | /** |
| 1770 | * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip |
| 1771 | * @gpiochip: the gpiochip to set the irqchip chain to |
| 1772 | * @irqchip: the irqchip to chain to the gpiochip |
| 1773 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 1774 | * chained irqchip |
| 1775 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
Stephen Boyd | 40f5ff4 | 2018-10-08 09:32:16 -0700 | [diff] [blame] | 1776 | * coming out of the gpiochip. |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1777 | */ |
| 1778 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 1779 | struct irq_chip *irqchip, |
Thierry Reding | 6f79309 | 2017-04-03 18:05:21 +0200 | [diff] [blame] | 1780 | unsigned int parent_irq, |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1781 | irq_flow_handler_t parent_handler) |
| 1782 | { |
Thierry Reding | 60ed54c | 2017-11-07 19:15:57 +0100 | [diff] [blame] | 1783 | if (gpiochip->irq.threaded) { |
| 1784 | chip_err(gpiochip, "tried to chain a threaded gpiochip\n"); |
| 1785 | return; |
| 1786 | } |
| 1787 | |
Stephen Boyd | 3c1f6b2 | 2018-10-08 09:32:15 -0700 | [diff] [blame] | 1788 | gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler); |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1789 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 1790 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
| 1791 | |
| 1792 | /** |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1793 | * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip |
| 1794 | * @gpiochip: the gpiochip to set the irqchip nested handler to |
| 1795 | * @irqchip: the irqchip to nest to the gpiochip |
| 1796 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 1797 | * nested irqchip |
| 1798 | */ |
| 1799 | void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip, |
| 1800 | struct irq_chip *irqchip, |
Thierry Reding | 6f79309 | 2017-04-03 18:05:21 +0200 | [diff] [blame] | 1801 | unsigned int parent_irq) |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1802 | { |
Stephen Boyd | 3c1f6b2 | 2018-10-08 09:32:15 -0700 | [diff] [blame] | 1803 | gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL); |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 1804 | } |
| 1805 | EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip); |
| 1806 | |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 1807 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
| 1808 | |
| 1809 | /** |
| 1810 | * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip |
| 1811 | * to a gpiochip |
| 1812 | * @gc: the gpiochip to set the irqchip hierarchical handler to |
| 1813 | * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt |
| 1814 | * will then percolate up to the parent |
| 1815 | */ |
| 1816 | static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc, |
| 1817 | struct irq_chip *irqchip) |
| 1818 | { |
| 1819 | /* DT will deal with mapping each IRQ as we go along */ |
| 1820 | if (is_of_node(gc->irq.fwnode)) |
| 1821 | return; |
| 1822 | |
| 1823 | /* |
| 1824 | * This is for legacy and boardfile "irqchip" fwnodes: allocate |
| 1825 | * irqs upfront instead of dynamically since we don't have the |
| 1826 | * dynamic type of allocation that hardware description languages |
| 1827 | * provide. Once all GPIO drivers using board files are gone from |
| 1828 | * the kernel we can delete this code, but for a transitional period |
| 1829 | * it is necessary to keep this around. |
| 1830 | */ |
| 1831 | if (is_fwnode_irqchip(gc->irq.fwnode)) { |
| 1832 | int i; |
| 1833 | int ret; |
| 1834 | |
| 1835 | for (i = 0; i < gc->ngpio; i++) { |
| 1836 | struct irq_fwspec fwspec; |
| 1837 | unsigned int parent_hwirq; |
| 1838 | unsigned int parent_type; |
| 1839 | struct gpio_irq_chip *girq = &gc->irq; |
| 1840 | |
| 1841 | /* |
| 1842 | * We call the child to parent translation function |
| 1843 | * only to check if the child IRQ is valid or not. |
| 1844 | * Just pick the rising edge type here as that is what |
| 1845 | * we likely need to support. |
| 1846 | */ |
| 1847 | ret = girq->child_to_parent_hwirq(gc, i, |
| 1848 | IRQ_TYPE_EDGE_RISING, |
| 1849 | &parent_hwirq, |
| 1850 | &parent_type); |
| 1851 | if (ret) { |
| 1852 | chip_err(gc, "skip set-up on hwirq %d\n", |
| 1853 | i); |
| 1854 | continue; |
| 1855 | } |
| 1856 | |
| 1857 | fwspec.fwnode = gc->irq.fwnode; |
| 1858 | /* This is the hwirq for the GPIO line side of things */ |
| 1859 | fwspec.param[0] = girq->child_offset_to_irq(gc, i); |
| 1860 | /* Just pick something */ |
| 1861 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
| 1862 | fwspec.param_count = 2; |
| 1863 | ret = __irq_domain_alloc_irqs(gc->irq.domain, |
| 1864 | /* just pick something */ |
| 1865 | -1, |
| 1866 | 1, |
| 1867 | NUMA_NO_NODE, |
| 1868 | &fwspec, |
| 1869 | false, |
| 1870 | NULL); |
| 1871 | if (ret < 0) { |
| 1872 | chip_err(gc, |
| 1873 | "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", |
| 1874 | i, parent_hwirq, |
| 1875 | ret); |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__); |
| 1881 | |
| 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d, |
| 1886 | struct irq_fwspec *fwspec, |
| 1887 | unsigned long *hwirq, |
| 1888 | unsigned int *type) |
| 1889 | { |
| 1890 | /* We support standard DT translation */ |
| 1891 | if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { |
| 1892 | return irq_domain_translate_twocell(d, fwspec, hwirq, type); |
| 1893 | } |
| 1894 | |
| 1895 | /* This is for board files and others not using DT */ |
| 1896 | if (is_fwnode_irqchip(fwspec->fwnode)) { |
| 1897 | int ret; |
| 1898 | |
| 1899 | ret = irq_domain_translate_twocell(d, fwspec, hwirq, type); |
| 1900 | if (ret) |
| 1901 | return ret; |
| 1902 | WARN_ON(*type == IRQ_TYPE_NONE); |
| 1903 | return 0; |
| 1904 | } |
| 1905 | return -EINVAL; |
| 1906 | } |
| 1907 | |
| 1908 | static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d, |
| 1909 | unsigned int irq, |
| 1910 | unsigned int nr_irqs, |
| 1911 | void *data) |
| 1912 | { |
| 1913 | struct gpio_chip *gc = d->host_data; |
| 1914 | irq_hw_number_t hwirq; |
| 1915 | unsigned int type = IRQ_TYPE_NONE; |
| 1916 | struct irq_fwspec *fwspec = data; |
| 1917 | struct irq_fwspec parent_fwspec; |
| 1918 | unsigned int parent_hwirq; |
| 1919 | unsigned int parent_type; |
| 1920 | struct gpio_irq_chip *girq = &gc->irq; |
| 1921 | int ret; |
| 1922 | |
| 1923 | /* |
| 1924 | * The nr_irqs parameter is always one except for PCI multi-MSI |
| 1925 | * so this should not happen. |
| 1926 | */ |
| 1927 | WARN_ON(nr_irqs != 1); |
| 1928 | |
| 1929 | ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); |
| 1930 | if (ret) |
| 1931 | return ret; |
| 1932 | |
| 1933 | chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); |
| 1934 | |
| 1935 | ret = girq->child_to_parent_hwirq(gc, hwirq, type, |
| 1936 | &parent_hwirq, &parent_type); |
| 1937 | if (ret) { |
| 1938 | chip_err(gc, "can't look up hwirq %lu\n", hwirq); |
| 1939 | return ret; |
| 1940 | } |
| 1941 | chip_info(gc, "found parent hwirq %u\n", parent_hwirq); |
| 1942 | |
| 1943 | /* |
| 1944 | * We set handle_bad_irq because the .set_type() should |
| 1945 | * always be invoked and set the right type of handler. |
| 1946 | */ |
| 1947 | irq_domain_set_info(d, |
| 1948 | irq, |
| 1949 | hwirq, |
| 1950 | gc->irq.chip, |
| 1951 | gc, |
| 1952 | girq->handler, |
| 1953 | NULL, NULL); |
| 1954 | irq_set_probe(irq); |
| 1955 | |
| 1956 | /* |
| 1957 | * Create a IRQ fwspec to send up to the parent irqdomain: |
| 1958 | * specify the hwirq we address on the parent and tie it |
| 1959 | * all together up the chain. |
| 1960 | */ |
| 1961 | parent_fwspec.fwnode = d->parent->fwnode; |
| 1962 | /* This parent only handles asserted level IRQs */ |
| 1963 | girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq, |
| 1964 | parent_type); |
| 1965 | chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n", |
| 1966 | irq, parent_hwirq); |
| 1967 | ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec); |
| 1968 | if (ret) |
| 1969 | chip_err(gc, |
| 1970 | "failed to allocate parent hwirq %d for hwirq %lu\n", |
| 1971 | parent_hwirq, hwirq); |
| 1972 | |
| 1973 | return ret; |
| 1974 | } |
| 1975 | |
| 1976 | static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip, |
| 1977 | unsigned int offset) |
| 1978 | { |
| 1979 | return offset; |
| 1980 | } |
| 1981 | |
| 1982 | static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops) |
| 1983 | { |
| 1984 | ops->activate = gpiochip_irq_domain_activate; |
| 1985 | ops->deactivate = gpiochip_irq_domain_deactivate; |
| 1986 | ops->alloc = gpiochip_hierarchy_irq_domain_alloc; |
| 1987 | ops->free = irq_domain_free_irqs_common; |
| 1988 | |
| 1989 | /* |
| 1990 | * We only allow overriding the translate() function for |
| 1991 | * hierarchical chips, and this should only be done if the user |
| 1992 | * really need something other than 1:1 translation. |
| 1993 | */ |
| 1994 | if (!ops->translate) |
| 1995 | ops->translate = gpiochip_hierarchy_irq_domain_translate; |
| 1996 | } |
| 1997 | |
| 1998 | static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) |
| 1999 | { |
| 2000 | if (!gc->irq.child_to_parent_hwirq || |
| 2001 | !gc->irq.fwnode) { |
| 2002 | chip_err(gc, "missing irqdomain vital data\n"); |
| 2003 | return -EINVAL; |
| 2004 | } |
| 2005 | |
| 2006 | if (!gc->irq.child_offset_to_irq) |
| 2007 | gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; |
| 2008 | |
| 2009 | if (!gc->irq.populate_parent_fwspec) |
| 2010 | gc->irq.populate_parent_fwspec = |
| 2011 | gpiochip_populate_parent_fwspec_twocell; |
| 2012 | |
| 2013 | gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); |
| 2014 | |
| 2015 | gc->irq.domain = irq_domain_create_hierarchy( |
| 2016 | gc->irq.parent_domain, |
| 2017 | 0, |
| 2018 | gc->ngpio, |
| 2019 | gc->irq.fwnode, |
| 2020 | &gc->irq.child_irq_domain_ops, |
| 2021 | gc); |
| 2022 | |
| 2023 | if (!gc->irq.domain) |
| 2024 | return -ENOMEM; |
| 2025 | |
| 2026 | gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); |
| 2027 | |
| 2028 | return 0; |
| 2029 | } |
| 2030 | |
| 2031 | static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) |
| 2032 | { |
| 2033 | return !!gc->irq.parent_domain; |
| 2034 | } |
| 2035 | |
| 2036 | void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip, |
| 2037 | struct irq_fwspec *fwspec, |
| 2038 | unsigned int parent_hwirq, |
| 2039 | unsigned int parent_type) |
| 2040 | { |
| 2041 | fwspec->param_count = 2; |
| 2042 | fwspec->param[0] = parent_hwirq; |
| 2043 | fwspec->param[1] = parent_type; |
| 2044 | } |
| 2045 | EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell); |
| 2046 | |
| 2047 | void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip, |
| 2048 | struct irq_fwspec *fwspec, |
| 2049 | unsigned int parent_hwirq, |
| 2050 | unsigned int parent_type) |
| 2051 | { |
| 2052 | fwspec->param_count = 4; |
| 2053 | fwspec->param[0] = 0; |
| 2054 | fwspec->param[1] = parent_hwirq; |
| 2055 | fwspec->param[2] = 0; |
| 2056 | fwspec->param[3] = parent_type; |
| 2057 | } |
| 2058 | EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell); |
| 2059 | |
| 2060 | #else |
| 2061 | |
| 2062 | static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) |
| 2063 | { |
| 2064 | return -EINVAL; |
| 2065 | } |
| 2066 | |
| 2067 | static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) |
| 2068 | { |
| 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ |
| 2073 | |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 2074 | /** |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2075 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip |
| 2076 | * @d: the irqdomain used by this irqchip |
| 2077 | * @irq: the global irq number used by this GPIO irqchip irq |
| 2078 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip |
| 2079 | * |
| 2080 | * This function will set up the mapping for a certain IRQ line on a |
| 2081 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip |
| 2082 | * stored inside the gpiochip. |
| 2083 | */ |
Thierry Reding | 1b95b4e | 2017-11-07 19:15:55 +0100 | [diff] [blame] | 2084 | int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
| 2085 | irq_hw_number_t hwirq) |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2086 | { |
| 2087 | struct gpio_chip *chip = d->host_data; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2088 | int ret = 0; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2089 | |
Grygorii Strashko | dc749a0 | 2017-07-21 11:49:00 -0500 | [diff] [blame] | 2090 | if (!gpiochip_irqchip_irq_valid(chip, hwirq)) |
| 2091 | return -ENXIO; |
| 2092 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2093 | irq_set_chip_data(irq, chip); |
Grygorii Strashko | a0a8bcf | 2015-08-17 15:35:23 +0300 | [diff] [blame] | 2094 | /* |
| 2095 | * This lock class tells lockdep that GPIO irqs are in a different |
| 2096 | * category than their parents, so it won't report false recursion. |
| 2097 | */ |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2098 | irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key); |
Thierry Reding | c7a0aa5 | 2017-11-07 19:15:48 +0100 | [diff] [blame] | 2099 | irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler); |
Linus Walleij | d245b3f | 2016-11-24 10:57:25 +0100 | [diff] [blame] | 2100 | /* Chips that use nested thread handlers have them marked */ |
Thierry Reding | 60ed54c | 2017-11-07 19:15:57 +0100 | [diff] [blame] | 2101 | if (chip->irq.threaded) |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 2102 | irq_set_nested_thread(irq, 1); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2103 | irq_set_noprobe(irq); |
Rob Herring | 23393d4 | 2015-07-27 15:55:16 -0500 | [diff] [blame] | 2104 | |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2105 | if (chip->irq.num_parents == 1) |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2106 | ret = irq_set_parent(irq, chip->irq.parents[0]); |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2107 | else if (chip->irq.map) |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2108 | ret = irq_set_parent(irq, chip->irq.map[hwirq]); |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2109 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2110 | if (ret < 0) |
| 2111 | return ret; |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2112 | |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 2113 | /* |
| 2114 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 2115 | * is passed as default type. |
| 2116 | */ |
Thierry Reding | 3634eeb | 2017-11-07 19:15:49 +0100 | [diff] [blame] | 2117 | if (chip->irq.default_type != IRQ_TYPE_NONE) |
| 2118 | irq_set_irq_type(irq, chip->irq.default_type); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2119 | |
| 2120 | return 0; |
| 2121 | } |
Thierry Reding | 1b95b4e | 2017-11-07 19:15:55 +0100 | [diff] [blame] | 2122 | EXPORT_SYMBOL_GPL(gpiochip_irq_map); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2123 | |
Thierry Reding | 1b95b4e | 2017-11-07 19:15:55 +0100 | [diff] [blame] | 2124 | void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2125 | { |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 2126 | struct gpio_chip *chip = d->host_data; |
| 2127 | |
Thierry Reding | 60ed54c | 2017-11-07 19:15:57 +0100 | [diff] [blame] | 2128 | if (chip->irq.threaded) |
Linus Walleij | 1c8732b | 2014-04-09 13:34:39 +0200 | [diff] [blame] | 2129 | irq_set_nested_thread(irq, 0); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2130 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 2131 | irq_set_chip_data(irq, NULL); |
| 2132 | } |
Thierry Reding | 1b95b4e | 2017-11-07 19:15:55 +0100 | [diff] [blame] | 2133 | EXPORT_SYMBOL_GPL(gpiochip_irq_unmap); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2134 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2135 | static const struct irq_domain_ops gpiochip_domain_ops = { |
| 2136 | .map = gpiochip_irq_map, |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2137 | .unmap = gpiochip_irq_unmap, |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2138 | /* Virtually all GPIO irqchips are twocell:ed */ |
| 2139 | .xlate = irq_domain_xlate_twocell, |
| 2140 | }; |
| 2141 | |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 2142 | /* |
| 2143 | * TODO: move these activate/deactivate in under the hierarchicial |
| 2144 | * irqchip implementation as static once SPMI and SSBI (all external |
| 2145 | * users) are phased over. |
| 2146 | */ |
Brian Masney | ef74f70 | 2019-01-19 15:42:42 -0500 | [diff] [blame] | 2147 | /** |
| 2148 | * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ |
| 2149 | * @domain: The IRQ domain used by this IRQ chip |
| 2150 | * @data: Outermost irq_data associated with the IRQ |
| 2151 | * @reserve: If set, only reserve an interrupt vector instead of assigning one |
| 2152 | * |
| 2153 | * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be |
| 2154 | * used as the activate function for the &struct irq_domain_ops. The host_data |
| 2155 | * for the IRQ domain must be the &struct gpio_chip. |
| 2156 | */ |
| 2157 | int gpiochip_irq_domain_activate(struct irq_domain *domain, |
| 2158 | struct irq_data *data, bool reserve) |
| 2159 | { |
| 2160 | struct gpio_chip *chip = domain->host_data; |
| 2161 | |
| 2162 | return gpiochip_lock_as_irq(chip, data->hwirq); |
| 2163 | } |
| 2164 | EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate); |
| 2165 | |
| 2166 | /** |
| 2167 | * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ |
| 2168 | * @domain: The IRQ domain used by this IRQ chip |
| 2169 | * @data: Outermost irq_data associated with the IRQ |
| 2170 | * |
| 2171 | * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to |
| 2172 | * be used as the deactivate function for the &struct irq_domain_ops. The |
| 2173 | * host_data for the IRQ domain must be the &struct gpio_chip. |
| 2174 | */ |
| 2175 | void gpiochip_irq_domain_deactivate(struct irq_domain *domain, |
| 2176 | struct irq_data *data) |
| 2177 | { |
| 2178 | struct gpio_chip *chip = domain->host_data; |
| 2179 | |
| 2180 | return gpiochip_unlock_as_irq(chip, data->hwirq); |
| 2181 | } |
| 2182 | EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate); |
| 2183 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2184 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
| 2185 | { |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 2186 | struct irq_domain *domain = chip->irq.domain; |
| 2187 | |
Grygorii Strashko | dc749a0 | 2017-07-21 11:49:00 -0500 | [diff] [blame] | 2188 | if (!gpiochip_irqchip_irq_valid(chip, offset)) |
| 2189 | return -ENXIO; |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2190 | |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 2191 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
| 2192 | if (irq_domain_is_hierarchy(domain)) { |
| 2193 | struct irq_fwspec spec; |
| 2194 | |
| 2195 | spec.fwnode = domain->fwnode; |
| 2196 | spec.param_count = 2; |
| 2197 | spec.param[0] = chip->irq.child_offset_to_irq(chip, offset); |
| 2198 | spec.param[1] = IRQ_TYPE_NONE; |
| 2199 | |
| 2200 | return irq_create_fwspec_mapping(&spec); |
| 2201 | } |
| 2202 | #endif |
| 2203 | |
| 2204 | return irq_create_mapping(domain, offset); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
Hans Verkuil | 4e6b823 | 2018-09-08 11:23:14 +0200 | [diff] [blame] | 2207 | static int gpiochip_irq_reqres(struct irq_data *d) |
| 2208 | { |
| 2209 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 2210 | |
| 2211 | return gpiochip_reqres_irq(chip, d->hwirq); |
| 2212 | } |
| 2213 | |
| 2214 | static void gpiochip_irq_relres(struct irq_data *d) |
| 2215 | { |
| 2216 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 2217 | |
| 2218 | gpiochip_relres_irq(chip, d->hwirq); |
| 2219 | } |
| 2220 | |
Hans Verkuil | 461c1a7 | 2018-09-08 11:23:17 +0200 | [diff] [blame] | 2221 | static void gpiochip_irq_enable(struct irq_data *d) |
| 2222 | { |
| 2223 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 2224 | |
| 2225 | gpiochip_enable_irq(chip, d->hwirq); |
| 2226 | if (chip->irq.irq_enable) |
| 2227 | chip->irq.irq_enable(d); |
| 2228 | else |
| 2229 | chip->irq.chip->irq_unmask(d); |
| 2230 | } |
| 2231 | |
| 2232 | static void gpiochip_irq_disable(struct irq_data *d) |
| 2233 | { |
| 2234 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 2235 | |
| 2236 | if (chip->irq.irq_disable) |
| 2237 | chip->irq.irq_disable(d); |
| 2238 | else |
| 2239 | chip->irq.chip->irq_mask(d); |
| 2240 | gpiochip_disable_irq(chip, d->hwirq); |
| 2241 | } |
| 2242 | |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2243 | static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip) |
| 2244 | { |
| 2245 | struct irq_chip *irqchip = gpiochip->irq.chip; |
| 2246 | |
| 2247 | if (!irqchip->irq_request_resources && |
| 2248 | !irqchip->irq_release_resources) { |
| 2249 | irqchip->irq_request_resources = gpiochip_irq_reqres; |
| 2250 | irqchip->irq_release_resources = gpiochip_irq_relres; |
| 2251 | } |
Hans Verkuil | 461c1a7 | 2018-09-08 11:23:17 +0200 | [diff] [blame] | 2252 | if (WARN_ON(gpiochip->irq.irq_enable)) |
| 2253 | return; |
Hans Verkuil | 171948e | 2018-09-14 10:36:39 +0200 | [diff] [blame] | 2254 | /* Check if the irqchip already has this hook... */ |
| 2255 | if (irqchip->irq_enable == gpiochip_irq_enable) { |
| 2256 | /* |
| 2257 | * ...and if so, give a gentle warning that this is bad |
| 2258 | * practice. |
| 2259 | */ |
| 2260 | chip_info(gpiochip, |
| 2261 | "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n"); |
| 2262 | return; |
| 2263 | } |
Hans Verkuil | 461c1a7 | 2018-09-08 11:23:17 +0200 | [diff] [blame] | 2264 | gpiochip->irq.irq_enable = irqchip->irq_enable; |
| 2265 | gpiochip->irq.irq_disable = irqchip->irq_disable; |
| 2266 | irqchip->irq_enable = gpiochip_irq_enable; |
| 2267 | irqchip->irq_disable = gpiochip_irq_disable; |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2270 | /** |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2271 | * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip |
| 2272 | * @gpiochip: the GPIO chip to add the IRQ chip to |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2273 | * @lock_key: lockdep class for IRQ lock |
| 2274 | * @request_key: lockdep class for IRQ request |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2275 | */ |
Thierry Reding | 959bc7b | 2017-11-07 19:15:59 +0100 | [diff] [blame] | 2276 | static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2277 | struct lock_class_key *lock_key, |
| 2278 | struct lock_class_key *request_key) |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2279 | { |
| 2280 | struct irq_chip *irqchip = gpiochip->irq.chip; |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 2281 | const struct irq_domain_ops *ops = NULL; |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2282 | struct device_node *np; |
| 2283 | unsigned int type; |
| 2284 | unsigned int i; |
| 2285 | |
| 2286 | if (!irqchip) |
| 2287 | return 0; |
| 2288 | |
| 2289 | if (gpiochip->irq.parent_handler && gpiochip->can_sleep) { |
Andy Shevchenko | b191171 | 2018-07-03 03:39:03 +0300 | [diff] [blame] | 2290 | chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n"); |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2291 | return -EINVAL; |
| 2292 | } |
| 2293 | |
| 2294 | np = gpiochip->gpiodev->dev.of_node; |
| 2295 | type = gpiochip->irq.default_type; |
| 2296 | |
| 2297 | /* |
| 2298 | * Specifying a default trigger is a terrible idea if DT or ACPI is |
| 2299 | * used to configure the interrupts, as you may end up with |
| 2300 | * conflicting triggers. Tell the user, and reset to NONE. |
| 2301 | */ |
| 2302 | if (WARN(np && type != IRQ_TYPE_NONE, |
| 2303 | "%s: Ignoring %u default trigger\n", np->full_name, type)) |
| 2304 | type = IRQ_TYPE_NONE; |
| 2305 | |
| 2306 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
| 2307 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), |
| 2308 | "Ignoring %u default trigger\n", type); |
| 2309 | type = IRQ_TYPE_NONE; |
| 2310 | } |
| 2311 | |
| 2312 | gpiochip->to_irq = gpiochip_to_irq; |
| 2313 | gpiochip->irq.default_type = type; |
Thierry Reding | 959bc7b | 2017-11-07 19:15:59 +0100 | [diff] [blame] | 2314 | gpiochip->irq.lock_key = lock_key; |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2315 | gpiochip->irq.request_key = request_key; |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2316 | |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 2317 | /* If a parent irqdomain is provided, let's build a hierarchy */ |
| 2318 | if (gpiochip_hierarchy_is_hierarchical(gpiochip)) { |
| 2319 | int ret = gpiochip_hierarchy_add_domain(gpiochip); |
| 2320 | if (ret) |
| 2321 | return ret; |
| 2322 | } else { |
| 2323 | /* Some drivers provide custom irqdomain ops */ |
| 2324 | if (gpiochip->irq.domain_ops) |
| 2325 | ops = gpiochip->irq.domain_ops; |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2326 | |
Linus Walleij | fdd61a0 | 2019-08-08 14:32:37 +0200 | [diff] [blame] | 2327 | if (!ops) |
| 2328 | ops = &gpiochip_domain_ops; |
| 2329 | gpiochip->irq.domain = irq_domain_add_simple(np, |
| 2330 | gpiochip->ngpio, |
| 2331 | gpiochip->irq.first, |
| 2332 | ops, gpiochip); |
| 2333 | if (!gpiochip->irq.domain) |
| 2334 | return -EINVAL; |
| 2335 | } |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2336 | |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2337 | if (gpiochip->irq.parent_handler) { |
| 2338 | void *data = gpiochip->irq.parent_handler_data ?: gpiochip; |
| 2339 | |
| 2340 | for (i = 0; i < gpiochip->irq.num_parents; i++) { |
| 2341 | /* |
| 2342 | * The parent IRQ chip is already using the chip_data |
| 2343 | * for this IRQ chip, so our callbacks simply use the |
| 2344 | * handler_data. |
| 2345 | */ |
| 2346 | irq_set_chained_handler_and_data(gpiochip->irq.parents[i], |
| 2347 | gpiochip->irq.parent_handler, |
| 2348 | data); |
| 2349 | } |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2352 | gpiochip_set_irq_hooks(gpiochip); |
| 2353 | |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2354 | acpi_gpiochip_request_interrupts(gpiochip); |
| 2355 | |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
| 2359 | /** |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2360 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip |
| 2361 | * @gpiochip: the gpiochip to remove the irqchip from |
| 2362 | * |
| 2363 | * This is called only from gpiochip_remove() |
| 2364 | */ |
| 2365 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) |
| 2366 | { |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2367 | struct irq_chip *irqchip = gpiochip->irq.chip; |
Thierry Reding | 39e5f09 | 2017-11-07 19:15:50 +0100 | [diff] [blame] | 2368 | unsigned int offset; |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2369 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 2370 | acpi_gpiochip_free_interrupts(gpiochip); |
| 2371 | |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2372 | if (irqchip && gpiochip->irq.parent_handler) { |
Thierry Reding | 39e5f09 | 2017-11-07 19:15:50 +0100 | [diff] [blame] | 2373 | struct gpio_irq_chip *irq = &gpiochip->irq; |
| 2374 | unsigned int i; |
| 2375 | |
| 2376 | for (i = 0; i < irq->num_parents; i++) |
| 2377 | irq_set_chained_handler_and_data(irq->parents[i], |
| 2378 | NULL, NULL); |
Dmitry Eremin-Solenikov | 25e4fe9 | 2015-05-12 20:12:23 +0300 | [diff] [blame] | 2379 | } |
| 2380 | |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2381 | /* Remove all IRQ mappings and delete the domain */ |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 2382 | if (gpiochip->irq.domain) { |
Thierry Reding | 39e5f09 | 2017-11-07 19:15:50 +0100 | [diff] [blame] | 2383 | unsigned int irq; |
| 2384 | |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 2385 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
| 2386 | if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
| 2387 | continue; |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 2388 | |
| 2389 | irq = irq_find_mapping(gpiochip->irq.domain, offset); |
| 2390 | irq_dispose_mapping(irq); |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 2391 | } |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 2392 | |
| 2393 | irq_domain_remove(gpiochip->irq.domain); |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2394 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2395 | |
Hans Verkuil | 461c1a7 | 2018-09-08 11:23:17 +0200 | [diff] [blame] | 2396 | if (irqchip) { |
| 2397 | if (irqchip->irq_request_resources == gpiochip_irq_reqres) { |
| 2398 | irqchip->irq_request_resources = NULL; |
| 2399 | irqchip->irq_release_resources = NULL; |
| 2400 | } |
| 2401 | if (irqchip->irq_enable == gpiochip_irq_enable) { |
| 2402 | irqchip->irq_enable = gpiochip->irq.irq_enable; |
| 2403 | irqchip->irq_disable = gpiochip->irq.irq_disable; |
| 2404 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2405 | } |
Hans Verkuil | 461c1a7 | 2018-09-08 11:23:17 +0200 | [diff] [blame] | 2406 | gpiochip->irq.irq_enable = NULL; |
| 2407 | gpiochip->irq.irq_disable = NULL; |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2408 | gpiochip->irq.chip = NULL; |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 2409 | |
| 2410 | gpiochip_irqchip_free_valid_mask(gpiochip); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | /** |
Linus Walleij | 739e6f5 | 2017-01-11 13:37:07 +0100 | [diff] [blame] | 2414 | * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2415 | * @gpiochip: the gpiochip to add the irqchip to |
| 2416 | * @irqchip: the irqchip to add to the gpiochip |
| 2417 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 2418 | * allocate gpiochip irqs from |
| 2419 | * @handler: the irq handler to use (often a predefined irq core function) |
Linus Walleij | 1333b90 | 2014-04-23 16:45:12 +0200 | [diff] [blame] | 2420 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 2421 | * to have the core avoid setting up any default type in the hardware. |
Thierry Reding | 60ed54c | 2017-11-07 19:15:57 +0100 | [diff] [blame] | 2422 | * @threaded: whether this irqchip uses a nested thread handler |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2423 | * @lock_key: lockdep class for IRQ lock |
| 2424 | * @request_key: lockdep class for IRQ request |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2425 | * |
| 2426 | * This function closely associates a certain irqchip with a certain |
| 2427 | * gpiochip, providing an irq domain to translate the local IRQs to |
| 2428 | * global irqs in the gpiolib core, and making sure that the gpiochip |
| 2429 | * is passed as chip data to all related functions. Driver callbacks |
Linus Walleij | 09dd5f9 | 2015-12-07 15:31:58 +0100 | [diff] [blame] | 2430 | * 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] | 2431 | * from the gpiochip passed as chip data. An irqdomain will be stored |
| 2432 | * in the gpiochip that shall be used by the driver to handle IRQ number |
| 2433 | * translation. The gpiochip will need to be initialized and registered |
| 2434 | * before calling this function. |
| 2435 | * |
Linus Walleij | c3626fd | 2014-03-28 20:42:01 +0100 | [diff] [blame] | 2436 | * This function will handle two cell:ed simple IRQs and assumes all |
| 2437 | * the pins on the gpiochip can generate a unique IRQ. Everything else |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2438 | * need to be open coded. |
| 2439 | */ |
Linus Walleij | 739e6f5 | 2017-01-11 13:37:07 +0100 | [diff] [blame] | 2440 | int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, |
| 2441 | struct irq_chip *irqchip, |
| 2442 | unsigned int first_irq, |
| 2443 | irq_flow_handler_t handler, |
| 2444 | unsigned int type, |
Thierry Reding | 60ed54c | 2017-11-07 19:15:57 +0100 | [diff] [blame] | 2445 | bool threaded, |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2446 | struct lock_class_key *lock_key, |
| 2447 | struct lock_class_key *request_key) |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2448 | { |
| 2449 | struct device_node *of_node; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2450 | |
| 2451 | if (!gpiochip || !irqchip) |
| 2452 | return -EINVAL; |
| 2453 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 2454 | if (!gpiochip->parent) { |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2455 | pr_err("missing gpiochip .dev parent pointer\n"); |
| 2456 | return -EINVAL; |
| 2457 | } |
Thierry Reding | 60ed54c | 2017-11-07 19:15:57 +0100 | [diff] [blame] | 2458 | gpiochip->irq.threaded = threaded; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 2459 | of_node = gpiochip->parent->of_node; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2460 | #ifdef CONFIG_OF_GPIO |
| 2461 | /* |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 2462 | * If the gpiochip has an assigned OF node this takes precedence |
Bamvor Jian Zhang | c88402c | 2015-11-18 17:07:07 +0800 | [diff] [blame] | 2463 | * FIXME: get rid of this and use gpiochip->parent->of_node |
| 2464 | * everywhere |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2465 | */ |
| 2466 | if (gpiochip->of_node) |
| 2467 | of_node = gpiochip->of_node; |
| 2468 | #endif |
Marc Zyngier | 332e99d | 2016-09-07 09:12:11 +0100 | [diff] [blame] | 2469 | /* |
Mika Westerberg | 0a1e005 | 2016-09-12 14:29:51 +0300 | [diff] [blame] | 2470 | * 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] | 2471 | * used to configure the interrupts, as you may end-up with |
| 2472 | * conflicting triggers. Tell the user, and reset to NONE. |
| 2473 | */ |
| 2474 | if (WARN(of_node && type != IRQ_TYPE_NONE, |
Rob Herring | 7eb6ce2 | 2017-07-18 16:43:03 -0500 | [diff] [blame] | 2475 | "%pOF: Ignoring %d default trigger\n", of_node, type)) |
Marc Zyngier | 332e99d | 2016-09-07 09:12:11 +0100 | [diff] [blame] | 2476 | type = IRQ_TYPE_NONE; |
Mika Westerberg | 0a1e005 | 2016-09-12 14:29:51 +0300 | [diff] [blame] | 2477 | if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
| 2478 | acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), |
| 2479 | "Ignoring %d default trigger\n", type); |
| 2480 | type = IRQ_TYPE_NONE; |
| 2481 | } |
Marc Zyngier | 332e99d | 2016-09-07 09:12:11 +0100 | [diff] [blame] | 2482 | |
Thierry Reding | da80ff8 | 2017-11-07 19:15:46 +0100 | [diff] [blame] | 2483 | gpiochip->irq.chip = irqchip; |
Thierry Reding | c7a0aa5 | 2017-11-07 19:15:48 +0100 | [diff] [blame] | 2484 | gpiochip->irq.handler = handler; |
Thierry Reding | 3634eeb | 2017-11-07 19:15:49 +0100 | [diff] [blame] | 2485 | gpiochip->irq.default_type = type; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2486 | gpiochip->to_irq = gpiochip_to_irq; |
Thierry Reding | ca9df05 | 2017-11-07 19:15:53 +0100 | [diff] [blame] | 2487 | gpiochip->irq.lock_key = lock_key; |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2488 | gpiochip->irq.request_key = request_key; |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 2489 | gpiochip->irq.domain = irq_domain_add_simple(of_node, |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2490 | gpiochip->ngpio, first_irq, |
| 2491 | &gpiochip_domain_ops, gpiochip); |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 2492 | if (!gpiochip->irq.domain) { |
Thierry Reding | da80ff8 | 2017-11-07 19:15:46 +0100 | [diff] [blame] | 2493 | gpiochip->irq.chip = NULL; |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2494 | return -EINVAL; |
| 2495 | } |
Rabin Vincent | 8b67a1f | 2015-07-31 14:48:56 +0200 | [diff] [blame] | 2496 | |
Hans Verkuil | ca620f2 | 2018-09-08 11:23:15 +0200 | [diff] [blame] | 2497 | gpiochip_set_irq_hooks(gpiochip); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2498 | |
Mika Westerberg | afa82fa | 2014-07-25 09:54:48 +0300 | [diff] [blame] | 2499 | acpi_gpiochip_request_interrupts(gpiochip); |
| 2500 | |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2501 | return 0; |
| 2502 | } |
Linus Walleij | 739e6f5 | 2017-01-11 13:37:07 +0100 | [diff] [blame] | 2503 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2504 | |
| 2505 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
| 2506 | |
Thierry Reding | 959bc7b | 2017-11-07 19:15:59 +0100 | [diff] [blame] | 2507 | static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
Andrew Lunn | 39c3fd5 | 2017-12-02 18:11:04 +0100 | [diff] [blame] | 2508 | struct lock_class_key *lock_key, |
| 2509 | struct lock_class_key *request_key) |
Thierry Reding | e0d8972 | 2017-11-07 19:15:54 +0100 | [diff] [blame] | 2510 | { |
| 2511 | return 0; |
| 2512 | } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2513 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
Andy Shevchenko | 9411e3a | 2019-10-09 17:34:44 +0300 | [diff] [blame] | 2514 | |
| 2515 | static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip) |
| 2516 | { |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
Mika Westerberg | 79b804c | 2016-09-20 15:15:21 +0300 | [diff] [blame] | 2520 | static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
| 2521 | { |
| 2522 | return 0; |
| 2523 | } |
| 2524 | static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
| 2525 | { } |
Linus Walleij | 1425052 | 2014-03-25 10:40:18 +0100 | [diff] [blame] | 2526 | |
| 2527 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
| 2528 | |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 2529 | /** |
| 2530 | * gpiochip_generic_request() - request the gpio function for a pin |
| 2531 | * @chip: the gpiochip owning the GPIO |
| 2532 | * @offset: the offset of the GPIO to request for GPIO function |
| 2533 | */ |
| 2534 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) |
| 2535 | { |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 2536 | return pinctrl_gpio_request(chip->gpiodev->base + offset); |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 2537 | } |
| 2538 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); |
| 2539 | |
| 2540 | /** |
| 2541 | * gpiochip_generic_free() - free the gpio function from a pin |
| 2542 | * @chip: the gpiochip to request the gpio function for |
| 2543 | * @offset: the offset of the GPIO to free from GPIO function |
| 2544 | */ |
| 2545 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) |
| 2546 | { |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 2547 | pinctrl_gpio_free(chip->gpiodev->base + offset); |
Jonas Gorski | c771c2f | 2015-10-11 17:34:15 +0200 | [diff] [blame] | 2548 | } |
| 2549 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); |
| 2550 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 2551 | /** |
| 2552 | * gpiochip_generic_config() - apply configuration for a pin |
| 2553 | * @chip: the gpiochip owning the GPIO |
| 2554 | * @offset: the offset of the GPIO to apply the configuration |
| 2555 | * @config: the configuration to be applied |
| 2556 | */ |
| 2557 | int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset, |
| 2558 | unsigned long config) |
| 2559 | { |
| 2560 | return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config); |
| 2561 | } |
| 2562 | EXPORT_SYMBOL_GPL(gpiochip_generic_config); |
| 2563 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2564 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 2565 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2566 | /** |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2567 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 2568 | * @chip: the gpiochip to add the range for |
Tomeu Vizoso | d32651f | 2015-06-17 15:42:11 +0200 | [diff] [blame] | 2569 | * @pctldev: the pin controller to map to |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2570 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 2571 | * @pin_group: name of the pin group inside the pin controller |
Christian Lamparter | 973c171 | 2018-05-21 22:57:39 +0200 | [diff] [blame] | 2572 | * |
| 2573 | * Calling this function directly from a DeviceTree-supported |
| 2574 | * pinctrl driver is DEPRECATED. Please see Section 2.1 of |
| 2575 | * Documentation/devicetree/bindings/gpio/gpio.txt on how to |
| 2576 | * bind pinctrl and gpio drivers via the "gpio-ranges" property. |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2577 | */ |
| 2578 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 2579 | struct pinctrl_dev *pctldev, |
| 2580 | unsigned int gpio_offset, const char *pin_group) |
| 2581 | { |
| 2582 | struct gpio_pin_range *pin_range; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2583 | struct gpio_device *gdev = chip->gpiodev; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2584 | int ret; |
| 2585 | |
| 2586 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 2587 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 2588 | chip_err(chip, "failed to allocate pin ranges\n"); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2589 | return -ENOMEM; |
| 2590 | } |
| 2591 | |
| 2592 | /* Use local offset as range ID */ |
| 2593 | pin_range->range.id = gpio_offset; |
| 2594 | pin_range->range.gc = chip; |
| 2595 | pin_range->range.name = chip->label; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2596 | pin_range->range.base = gdev->base + gpio_offset; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2597 | pin_range->pctldev = pctldev; |
| 2598 | |
| 2599 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 2600 | &pin_range->range.pins, |
| 2601 | &pin_range->range.npins); |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 2602 | if (ret < 0) { |
| 2603 | kfree(pin_range); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2604 | return ret; |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 2605 | } |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2606 | |
| 2607 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 2608 | |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 2609 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 2610 | gpio_offset, gpio_offset + pin_range->range.npins - 1, |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2611 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 2612 | |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 2613 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 2614 | |
| 2615 | return 0; |
| 2616 | } |
| 2617 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 2618 | |
| 2619 | /** |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2620 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 2621 | * @chip: the gpiochip to add the range for |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 2622 | * @pinctl_name: the dev_name() of the pin controller to map to |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 2623 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 2624 | * @pin_offset: the start offset in the pin controller number space |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2625 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 2626 | * pin controller) to accumulate in this range |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 2627 | * |
| 2628 | * Returns: |
| 2629 | * 0 on success, or a negative error-code on failure. |
Christian Lamparter | 973c171 | 2018-05-21 22:57:39 +0200 | [diff] [blame] | 2630 | * |
| 2631 | * Calling this function directly from a DeviceTree-supported |
| 2632 | * pinctrl driver is DEPRECATED. Please see Section 2.1 of |
| 2633 | * Documentation/devicetree/bindings/gpio/gpio.txt on how to |
| 2634 | * bind pinctrl and gpio drivers via the "gpio-ranges" property. |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2635 | */ |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 2636 | 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] | 2637 | unsigned int gpio_offset, unsigned int pin_offset, |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2638 | unsigned int npins) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2639 | { |
| 2640 | struct gpio_pin_range *pin_range; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2641 | struct gpio_device *gdev = chip->gpiodev; |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 2642 | int ret; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2643 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2644 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2645 | if (!pin_range) { |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 2646 | chip_err(chip, "failed to allocate pin ranges\n"); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 2647 | return -ENOMEM; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2648 | } |
| 2649 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2650 | /* Use local offset as range ID */ |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 2651 | pin_range->range.id = gpio_offset; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2652 | pin_range->range.gc = chip; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2653 | pin_range->range.name = chip->label; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2654 | pin_range->range.base = gdev->base + gpio_offset; |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 2655 | pin_range->range.pin_base = pin_offset; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2656 | pin_range->range.npins = npins; |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 2657 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2658 | &pin_range->range); |
Linus Walleij | 8f23ca1 | 2012-11-20 14:56:25 +0100 | [diff] [blame] | 2659 | if (IS_ERR(pin_range->pctldev)) { |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 2660 | ret = PTR_ERR(pin_range->pctldev); |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 2661 | chip_err(chip, "could not create pin range\n"); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2662 | kfree(pin_range); |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 2663 | return ret; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2664 | } |
Andy Shevchenko | 1a2a99c | 2013-12-05 11:26:24 +0200 | [diff] [blame] | 2665 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 2666 | gpio_offset, gpio_offset + npins - 1, |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 2667 | pinctl_name, |
| 2668 | pin_offset, pin_offset + npins - 1); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2669 | |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 2670 | list_add_tail(&pin_range->node, &gdev->pin_ranges); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 2671 | |
| 2672 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2673 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 2674 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2675 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2676 | /** |
| 2677 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 2678 | * @chip: the chip to remove all the mappings for |
| 2679 | */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2680 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 2681 | { |
| 2682 | struct gpio_pin_range *pin_range, *tmp; |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 2683 | struct gpio_device *gdev = chip->gpiodev; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2684 | |
Linus Walleij | 20ec3e3 | 2016-02-11 11:03:06 +0100 | [diff] [blame] | 2685 | list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2686 | list_del(&pin_range->node); |
| 2687 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 2688 | &pin_range->range); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 2689 | kfree(pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2690 | } |
| 2691 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 2692 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 2693 | |
| 2694 | #endif /* CONFIG_PINCTRL */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 2695 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2696 | /* These "optional" allocation calls help prevent drivers from stomping |
| 2697 | * on each other, and help provide better diagnostics in debugfs. |
| 2698 | * They're called even less than the "set direction" calls. |
| 2699 | */ |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 2700 | static int gpiod_request_commit(struct gpio_desc *desc, const char *label) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2701 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2702 | struct gpio_chip *chip = desc->gdev->chip; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2703 | int ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2704 | unsigned long flags; |
Biju Das | 3789f5a | 2018-08-07 08:15:18 +0100 | [diff] [blame] | 2705 | unsigned offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2706 | |
Muchun Song | 18534df | 2018-11-01 21:12:50 +0800 | [diff] [blame] | 2707 | if (label) { |
| 2708 | label = kstrdup_const(label, GFP_KERNEL); |
| 2709 | if (!label) |
| 2710 | return -ENOMEM; |
| 2711 | } |
| 2712 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2713 | spin_lock_irqsave(&gpio_lock, flags); |
| 2714 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2715 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2716 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2717 | */ |
| 2718 | |
| 2719 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 2720 | desc_set_label(desc, label ? : "?"); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2721 | ret = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 2722 | } else { |
Muchun Song | 18534df | 2018-11-01 21:12:50 +0800 | [diff] [blame] | 2723 | kfree_const(label); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2724 | ret = -EBUSY; |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 2725 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | if (chip->request) { |
| 2729 | /* chip->request may sleep */ |
| 2730 | spin_unlock_irqrestore(&gpio_lock, flags); |
Biju Das | 3789f5a | 2018-08-07 08:15:18 +0100 | [diff] [blame] | 2731 | offset = gpio_chip_hwgpio(desc); |
| 2732 | if (gpiochip_line_is_valid(chip, offset)) |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2733 | ret = chip->request(chip, offset); |
Biju Das | 3789f5a | 2018-08-07 08:15:18 +0100 | [diff] [blame] | 2734 | else |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2735 | ret = -EINVAL; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2736 | spin_lock_irqsave(&gpio_lock, flags); |
| 2737 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2738 | if (ret < 0) { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2739 | desc_set_label(desc, NULL); |
Muchun Song | 18534df | 2018-11-01 21:12:50 +0800 | [diff] [blame] | 2740 | kfree_const(label); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2741 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 2742 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2743 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 2744 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 2745 | if (chip->get_direction) { |
| 2746 | /* chip->get_direction may sleep */ |
| 2747 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2748 | gpiod_get_direction(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 2749 | spin_lock_irqsave(&gpio_lock, flags); |
| 2750 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2751 | done: |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2752 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2753 | return ret; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2754 | } |
| 2755 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2756 | /* |
| 2757 | * This descriptor validation needs to be inserted verbatim into each |
| 2758 | * function taking a descriptor, so we need to use a preprocessor |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 2759 | * macro to avoid endless duplication. If the desc is NULL it is an |
| 2760 | * optional GPIO and calls should just bail out. |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2761 | */ |
Rasmus Villemoes | a746a23 | 2017-12-21 01:27:04 +0100 | [diff] [blame] | 2762 | static int validate_desc(const struct gpio_desc *desc, const char *func) |
| 2763 | { |
| 2764 | if (!desc) |
| 2765 | return 0; |
| 2766 | if (IS_ERR(desc)) { |
| 2767 | pr_warn("%s: invalid GPIO (errorpointer)\n", func); |
| 2768 | return PTR_ERR(desc); |
| 2769 | } |
| 2770 | if (!desc->gdev) { |
| 2771 | pr_warn("%s: invalid GPIO (no device)\n", func); |
| 2772 | return -EINVAL; |
| 2773 | } |
| 2774 | if (!desc->gdev->chip) { |
| 2775 | dev_warn(&desc->gdev->dev, |
| 2776 | "%s: backing chip is gone\n", func); |
| 2777 | return 0; |
| 2778 | } |
| 2779 | return 1; |
| 2780 | } |
| 2781 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2782 | #define VALIDATE_DESC(desc) do { \ |
Rasmus Villemoes | a746a23 | 2017-12-21 01:27:04 +0100 | [diff] [blame] | 2783 | int __valid = validate_desc(desc, __func__); \ |
| 2784 | if (__valid <= 0) \ |
| 2785 | return __valid; \ |
| 2786 | } while (0) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2787 | |
| 2788 | #define VALIDATE_DESC_VOID(desc) do { \ |
Rasmus Villemoes | a746a23 | 2017-12-21 01:27:04 +0100 | [diff] [blame] | 2789 | int __valid = validate_desc(desc, __func__); \ |
| 2790 | if (__valid <= 0) \ |
Linus Walleij | 54d7719 | 2016-05-30 16:48:39 +0200 | [diff] [blame] | 2791 | return; \ |
Rasmus Villemoes | a746a23 | 2017-12-21 01:27:04 +0100 | [diff] [blame] | 2792 | } while (0) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2793 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 2794 | int gpiod_request(struct gpio_desc *desc, const char *label) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2795 | { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2796 | int ret = -EPROBE_DEFER; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2797 | struct gpio_device *gdev; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2798 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2799 | VALIDATE_DESC(desc); |
| 2800 | gdev = desc->gdev; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2801 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2802 | if (try_module_get(gdev->owner)) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2803 | ret = gpiod_request_commit(desc, label); |
| 2804 | if (ret < 0) |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2805 | module_put(gdev->owner); |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 2806 | else |
| 2807 | get_device(&gdev->dev); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2808 | } |
| 2809 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2810 | if (ret) |
| 2811 | gpiod_dbg(desc, "%s: status %d\n", __func__, ret); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2812 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2813 | return ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2814 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2815 | |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 2816 | static bool gpiod_free_commit(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2817 | { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2818 | bool ret = false; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2819 | unsigned long flags; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2820 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2821 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 2822 | might_sleep(); |
| 2823 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2824 | gpiod_unexport(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 2825 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2826 | spin_lock_irqsave(&gpio_lock, flags); |
| 2827 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2828 | chip = desc->gdev->chip; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2829 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 2830 | if (chip->free) { |
| 2831 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 2832 | might_sleep_if(chip->can_sleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2833 | chip->free(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2834 | spin_lock_irqsave(&gpio_lock, flags); |
| 2835 | } |
Muchun Song | 18534df | 2018-11-01 21:12:50 +0800 | [diff] [blame] | 2836 | kfree_const(desc->label); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2837 | desc_set_label(desc, NULL); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 2838 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 2839 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2840 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2841 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Drew Fustini | 9225d51 | 2019-11-05 10:04:23 +0800 | [diff] [blame] | 2842 | clear_bit(FLAG_PULL_UP, &desc->flags); |
| 2843 | clear_bit(FLAG_PULL_DOWN, &desc->flags); |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 2844 | clear_bit(FLAG_BIAS_DISABLE, &desc->flags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 2845 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2846 | ret = true; |
| 2847 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2848 | |
| 2849 | spin_unlock_irqrestore(&gpio_lock, flags); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2850 | return ret; |
| 2851 | } |
| 2852 | |
Alexandre Courbot | 0eb4c6c | 2014-07-01 14:45:15 +0900 | [diff] [blame] | 2853 | void gpiod_free(struct gpio_desc *desc) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2854 | { |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 2855 | if (desc && desc->gdev && gpiod_free_commit(desc)) { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2856 | module_put(desc->gdev->owner); |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 2857 | put_device(&desc->gdev->dev); |
| 2858 | } else { |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2859 | WARN_ON(extra_checks); |
Linus Walleij | 33a68e8 | 2016-02-11 10:28:44 +0100 | [diff] [blame] | 2860 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2861 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2862 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2863 | /** |
| 2864 | * gpiochip_is_requested - return string iff signal was requested |
| 2865 | * @chip: controller managing the signal |
| 2866 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 2867 | * |
| 2868 | * Returns NULL if the GPIO is not currently requested, else a string. |
Alexandre Courbot | 9c8318f | 2014-07-01 14:45:14 +0900 | [diff] [blame] | 2869 | * The string returned is the label passed to gpio_request(); if none has been |
| 2870 | * passed it is a meaningless, non-NULL constant. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2871 | * |
| 2872 | * This function is for use by GPIO controller drivers. The label can |
| 2873 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 2874 | * can help avoid accidentally multiplexing it to another controller. |
| 2875 | */ |
| 2876 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 2877 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2878 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2879 | |
Dirk Behme | 48b5953 | 2015-08-18 18:02:32 +0200 | [diff] [blame] | 2880 | if (offset >= chip->ngpio) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2881 | return NULL; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2882 | |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 2883 | desc = &chip->gpiodev->descs[offset]; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2884 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2885 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2886 | return NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2887 | return desc->label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2888 | } |
| 2889 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 2890 | |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2891 | /** |
| 2892 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 2893 | * @chip: GPIO chip |
| 2894 | * @hwnum: hardware number of the GPIO for which to request the descriptor |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2895 | * @label: label for the GPIO |
Linus Walleij | 5923ea6 | 2019-04-26 14:40:18 +0200 | [diff] [blame] | 2896 | * @lflags: lookup flags for this GPIO or 0 if default, this can be used to |
| 2897 | * specify things like line inversion semantics with the machine flags |
| 2898 | * such as GPIO_OUT_LOW |
| 2899 | * @dflags: descriptor request flags for this GPIO or 0 if default, this |
| 2900 | * can be used to specify consumer semantics such as open drain |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2901 | * |
| 2902 | * Function allows GPIO chip drivers to request and use their own GPIO |
| 2903 | * descriptors via gpiolib API. Difference to gpiod_request() is that this |
| 2904 | * function will not increase reference count of the GPIO chip module. This |
| 2905 | * allows the GPIO chip module to be unloaded as needed (we assume that the |
| 2906 | * GPIO chip driver handles freeing the GPIOs it has requested). |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 2907 | * |
| 2908 | * Returns: |
| 2909 | * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error |
| 2910 | * code on failure. |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2911 | */ |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2912 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
Linus Walleij | 21abf10 | 2018-09-04 13:31:45 +0200 | [diff] [blame] | 2913 | const char *label, |
Linus Walleij | 5923ea6 | 2019-04-26 14:40:18 +0200 | [diff] [blame] | 2914 | enum gpio_lookup_flags lflags, |
| 2915 | enum gpiod_flags dflags) |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2916 | { |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2917 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2918 | int ret; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2919 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2920 | if (IS_ERR(desc)) { |
| 2921 | chip_err(chip, "failed to get GPIO descriptor\n"); |
| 2922 | return desc; |
| 2923 | } |
| 2924 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2925 | ret = gpiod_request_commit(desc, label); |
| 2926 | if (ret < 0) |
| 2927 | return ERR_PTR(ret); |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2928 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2929 | ret = gpiod_configure_flags(desc, label, lflags, dflags); |
| 2930 | if (ret) { |
Linus Walleij | 21abf10 | 2018-09-04 13:31:45 +0200 | [diff] [blame] | 2931 | chip_err(chip, "setup of own GPIO %s failed\n", label); |
| 2932 | gpiod_free_commit(desc); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 2933 | return ERR_PTR(ret); |
Linus Walleij | 21abf10 | 2018-09-04 13:31:45 +0200 | [diff] [blame] | 2934 | } |
| 2935 | |
Alexandre Courbot | abdc08a | 2014-08-19 10:06:09 -0700 | [diff] [blame] | 2936 | return desc; |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2937 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 2938 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2939 | |
| 2940 | /** |
| 2941 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver |
| 2942 | * @desc: GPIO descriptor to free |
| 2943 | * |
| 2944 | * Function frees the given GPIO requested previously with |
| 2945 | * gpiochip_request_own_desc(). |
| 2946 | */ |
| 2947 | void gpiochip_free_own_desc(struct gpio_desc *desc) |
| 2948 | { |
| 2949 | if (desc) |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 2950 | gpiod_free_commit(desc); |
Mika Westerberg | 77c2d79 | 2014-03-10 14:54:50 +0200 | [diff] [blame] | 2951 | } |
Guenter Roeck | f7d4ad9 | 2014-07-22 08:01:01 -0700 | [diff] [blame] | 2952 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2953 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 2954 | /* |
| 2955 | * Drivers MUST set GPIO direction before making get/set calls. In |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2956 | * some cases this is done in early boot, before IRQs are enabled. |
| 2957 | * |
| 2958 | * As a rule these aren't called more than once (except for drivers |
| 2959 | * using the open-drain emulation idiom) so these are natural places |
| 2960 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 2961 | * rely on gpio_request() having been called beforehand. |
| 2962 | */ |
| 2963 | |
Thomas Petazzoni | 7147978 | 2019-02-07 17:28:56 +0100 | [diff] [blame] | 2964 | static int gpio_set_config(struct gpio_chip *gc, unsigned offset, |
| 2965 | enum pin_config_param mode) |
| 2966 | { |
Maxime Ripard | 542f361 | 2019-03-14 20:32:50 +0100 | [diff] [blame] | 2967 | unsigned long config; |
| 2968 | unsigned arg; |
Thomas Petazzoni | 7147978 | 2019-02-07 17:28:56 +0100 | [diff] [blame] | 2969 | |
Maxime Ripard | 542f361 | 2019-03-14 20:32:50 +0100 | [diff] [blame] | 2970 | switch (mode) { |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 2971 | case PIN_CONFIG_BIAS_DISABLE: |
Maxime Ripard | 542f361 | 2019-03-14 20:32:50 +0100 | [diff] [blame] | 2972 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 2973 | case PIN_CONFIG_BIAS_PULL_UP: |
| 2974 | arg = 1; |
| 2975 | break; |
| 2976 | |
| 2977 | default: |
| 2978 | arg = 0; |
| 2979 | } |
| 2980 | |
| 2981 | config = PIN_CONF_PACKED(mode, arg); |
Thomas Petazzoni | 7147978 | 2019-02-07 17:28:56 +0100 | [diff] [blame] | 2982 | return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; |
| 2983 | } |
| 2984 | |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 2985 | static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc) |
| 2986 | { |
| 2987 | int bias = 0; |
| 2988 | int ret = 0; |
| 2989 | |
| 2990 | if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) |
| 2991 | bias = PIN_CONFIG_BIAS_DISABLE; |
| 2992 | else if (test_bit(FLAG_PULL_UP, &desc->flags)) |
| 2993 | bias = PIN_CONFIG_BIAS_PULL_UP; |
| 2994 | else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) |
| 2995 | bias = PIN_CONFIG_BIAS_PULL_DOWN; |
| 2996 | |
| 2997 | if (bias) { |
| 2998 | ret = gpio_set_config(chip, gpio_chip_hwgpio(desc), bias); |
| 2999 | if (ret != -ENOTSUPP) |
| 3000 | return ret; |
| 3001 | } |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3005 | /** |
| 3006 | * gpiod_direction_input - set the GPIO direction to input |
| 3007 | * @desc: GPIO to set to input |
| 3008 | * |
| 3009 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 3010 | * be called safely on it. |
| 3011 | * |
| 3012 | * Return 0 in case of success, else an error code. |
| 3013 | */ |
| 3014 | int gpiod_direction_input(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3015 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3016 | struct gpio_chip *chip; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3017 | int ret = 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3018 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3019 | VALIDATE_DESC(desc); |
| 3020 | chip = desc->gdev->chip; |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 3021 | |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3022 | /* |
| 3023 | * It is legal to have no .get() and .direction_input() specified if |
| 3024 | * the chip is output-only, but you can't specify .direction_input() |
| 3025 | * and not support the .get() operation, that doesn't make sense. |
| 3026 | */ |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3027 | if (!chip->get && chip->direction_input) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 3028 | gpiod_warn(desc, |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3029 | "%s: missing get() but have direction_input()\n", |
| 3030 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 3031 | return -EIO; |
| 3032 | } |
| 3033 | |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3034 | /* |
| 3035 | * If we have a .direction_input() callback, things are simple, |
| 3036 | * just call it. Else we are some input-only chip so try to check the |
| 3037 | * direction (if .get_direction() is supported) else we silently |
| 3038 | * assume we are in input mode after this. |
| 3039 | */ |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3040 | if (chip->direction_input) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3041 | ret = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3042 | } else if (chip->get_direction && |
| 3043 | (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) { |
| 3044 | gpiod_warn(desc, |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3045 | "%s: missing direction_input() operation and line is output\n", |
| 3046 | __func__); |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3047 | return -EIO; |
| 3048 | } |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 3049 | if (ret == 0) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3050 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Kent Gibson | 2148ad7 | 2019-11-05 10:04:25 +0800 | [diff] [blame] | 3051 | ret = gpio_set_bias(chip, desc); |
| 3052 | } |
Thomas Petazzoni | d449991 | 2019-02-07 17:28:58 +0100 | [diff] [blame] | 3053 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3054 | trace_gpio_direction(desc_to_gpio(desc), 1, ret); |
Alexandre Courbot | d82da79 | 2014-07-22 16:17:43 +0900 | [diff] [blame] | 3055 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3056 | return ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3057 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3058 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3059 | |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3060 | 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] | 3061 | { |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 3062 | struct gpio_chip *gc = desc->gdev->chip; |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 3063 | int val = !!value; |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3064 | int ret = 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3065 | |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3066 | /* |
| 3067 | * It's OK not to specify .direction_output() if the gpiochip is |
| 3068 | * output-only, but if there is then not even a .set() operation it |
| 3069 | * is pretty tricky to drive the output line. |
| 3070 | */ |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3071 | if (!gc->set && !gc->direction_output) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 3072 | gpiod_warn(desc, |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3073 | "%s: missing set() and direction_output() operations\n", |
| 3074 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 3075 | return -EIO; |
| 3076 | } |
| 3077 | |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3078 | if (gc->direction_output) { |
| 3079 | ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); |
| 3080 | } else { |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3081 | /* Check that we are in output mode if we can */ |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3082 | if (gc->get_direction && |
| 3083 | gc->get_direction(gc, gpio_chip_hwgpio(desc))) { |
| 3084 | gpiod_warn(desc, |
| 3085 | "%s: missing direction_output() operation\n", |
| 3086 | __func__); |
| 3087 | return -EIO; |
| 3088 | } |
Linus Walleij | e48d194 | 2018-09-25 09:54:14 +0200 | [diff] [blame] | 3089 | /* |
| 3090 | * If we can't actively set the direction, we are some |
| 3091 | * output-only chip, so just drive the output as desired. |
| 3092 | */ |
Ricardo Ribalda Delgado | ae9847f | 2018-09-21 12:36:03 +0200 | [diff] [blame] | 3093 | gc->set(gc, gpio_chip_hwgpio(desc), val); |
| 3094 | } |
| 3095 | |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 3096 | if (!ret) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3097 | set_bit(FLAG_IS_OUT, &desc->flags); |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 3098 | trace_gpio_value(desc_to_gpio(desc), 0, val); |
Linus Walleij | c663e5f | 2016-03-22 10:51:16 +0100 | [diff] [blame] | 3099 | trace_gpio_direction(desc_to_gpio(desc), 0, ret); |
| 3100 | return ret; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3101 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 3102 | |
| 3103 | /** |
| 3104 | * gpiod_direction_output_raw - set the GPIO direction to output |
| 3105 | * @desc: GPIO to set to output |
| 3106 | * @value: initial output value of the GPIO |
| 3107 | * |
| 3108 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 3109 | * be called safely on it. The initial value of the output must be specified |
| 3110 | * as raw value on the physical line without regard for the ACTIVE_LOW status. |
| 3111 | * |
| 3112 | * Return 0 in case of success, else an error code. |
| 3113 | */ |
| 3114 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 3115 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3116 | VALIDATE_DESC(desc); |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3117 | return gpiod_direction_output_raw_commit(desc, value); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 3118 | } |
| 3119 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); |
| 3120 | |
| 3121 | /** |
Rahul Bedarkar | 90df4fe | 2014-02-08 11:55:25 +0530 | [diff] [blame] | 3122 | * gpiod_direction_output - set the GPIO direction to output |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 3123 | * @desc: GPIO to set to output |
| 3124 | * @value: initial output value of the GPIO |
| 3125 | * |
| 3126 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 3127 | * be called safely on it. The initial value of the output must be specified |
| 3128 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 3129 | * account. |
| 3130 | * |
| 3131 | * Return 0 in case of success, else an error code. |
| 3132 | */ |
| 3133 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 3134 | { |
Vladimir Zapolskiy | 30322bc | 2017-12-21 18:37:24 +0200 | [diff] [blame] | 3135 | struct gpio_chip *gc; |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3136 | int ret; |
| 3137 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3138 | VALIDATE_DESC(desc); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 3139 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 3140 | value = !value; |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 3141 | else |
| 3142 | value = !!value; |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3143 | |
Hans Verkuil | 4e9439d | 2018-09-08 11:23:16 +0200 | [diff] [blame] | 3144 | /* GPIOs used for enabled IRQs shall not be set as output */ |
| 3145 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && |
| 3146 | test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3147 | gpiod_err(desc, |
| 3148 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 3149 | __func__); |
| 3150 | return -EIO; |
| 3151 | } |
| 3152 | |
Vladimir Zapolskiy | 30322bc | 2017-12-21 18:37:24 +0200 | [diff] [blame] | 3153 | gc = desc->gdev->chip; |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3154 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
| 3155 | /* First see if we can enable open drain in hardware */ |
Thomas Petazzoni | 7147978 | 2019-02-07 17:28:56 +0100 | [diff] [blame] | 3156 | ret = gpio_set_config(gc, gpio_chip_hwgpio(desc), |
| 3157 | PIN_CONFIG_DRIVE_OPEN_DRAIN); |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3158 | if (!ret) |
| 3159 | goto set_output_value; |
| 3160 | /* Emulate open drain by not actively driving the line high */ |
Bartosz Golaszewski | e735244 | 2019-10-01 11:44:53 +0200 | [diff] [blame] | 3161 | if (value) { |
| 3162 | ret = gpiod_direction_input(desc); |
| 3163 | goto set_output_flag; |
| 3164 | } |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3165 | } |
| 3166 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
Thomas Petazzoni | 7147978 | 2019-02-07 17:28:56 +0100 | [diff] [blame] | 3167 | ret = gpio_set_config(gc, gpio_chip_hwgpio(desc), |
| 3168 | PIN_CONFIG_DRIVE_OPEN_SOURCE); |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3169 | if (!ret) |
| 3170 | goto set_output_value; |
| 3171 | /* Emulate open source by not actively driving the line low */ |
Bartosz Golaszewski | e735244 | 2019-10-01 11:44:53 +0200 | [diff] [blame] | 3172 | if (!value) { |
| 3173 | ret = gpiod_direction_input(desc); |
| 3174 | goto set_output_flag; |
| 3175 | } |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3176 | } else { |
Thomas Petazzoni | 7147978 | 2019-02-07 17:28:56 +0100 | [diff] [blame] | 3177 | gpio_set_config(gc, gpio_chip_hwgpio(desc), |
| 3178 | PIN_CONFIG_DRIVE_PUSH_PULL); |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | set_output_value: |
Kent Gibson | 2821ae5 | 2019-11-05 10:04:26 +0800 | [diff] [blame] | 3182 | ret = gpio_set_bias(gc, desc); |
| 3183 | if (ret) |
| 3184 | return ret; |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3185 | return gpiod_direction_output_raw_commit(desc, value); |
Bartosz Golaszewski | e735244 | 2019-10-01 11:44:53 +0200 | [diff] [blame] | 3186 | |
| 3187 | set_output_flag: |
| 3188 | /* |
| 3189 | * When emulating open-source or open-drain functionalities by not |
| 3190 | * actively driving the line (setting mode to input) we still need to |
| 3191 | * set the IS_OUT flag or otherwise we won't be able to set the line |
| 3192 | * value anymore. |
| 3193 | */ |
| 3194 | if (ret == 0) |
| 3195 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 3196 | return ret; |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame] | 3197 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3198 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3199 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 3200 | /** |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 3201 | * gpiod_set_debounce - sets @debounce time for a GPIO |
| 3202 | * @desc: descriptor of the GPIO for which to set debounce time |
| 3203 | * @debounce: debounce time in microseconds |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 3204 | * |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 3205 | * Returns: |
| 3206 | * 0 on success, %-ENOTSUPP if the controller doesn't support setting the |
| 3207 | * debounce time. |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 3208 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3209 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 3210 | { |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 3211 | struct gpio_chip *chip; |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 3212 | unsigned long config; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 3213 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3214 | VALIDATE_DESC(desc); |
| 3215 | chip = desc->gdev->chip; |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 3216 | if (!chip->set || !chip->set_config) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 3217 | gpiod_dbg(desc, |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 3218 | "%s: missing set() or set_config() operations\n", |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 3219 | __func__); |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 3220 | return -ENOTSUPP; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 3223 | config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); |
Andrew Jeffery | fa59dd23 | 2019-03-26 15:19:54 +1030 | [diff] [blame] | 3224 | return chip->set_config(chip, gpio_chip_hwgpio(desc), config); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 3225 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3226 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3227 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3228 | /** |
Andrew Jeffery | e10f72b | 2017-11-30 14:25:24 +1030 | [diff] [blame] | 3229 | * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset |
| 3230 | * @desc: descriptor of the GPIO for which to configure persistence |
| 3231 | * @transitory: True to lose state on suspend or reset, false for persistence |
| 3232 | * |
| 3233 | * Returns: |
| 3234 | * 0 on success, otherwise a negative error code. |
| 3235 | */ |
| 3236 | int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) |
| 3237 | { |
| 3238 | struct gpio_chip *chip; |
| 3239 | unsigned long packed; |
| 3240 | int gpio; |
| 3241 | int rc; |
| 3242 | |
Vladimir Zapolskiy | 156dd39 | 2017-12-21 18:37:35 +0200 | [diff] [blame] | 3243 | VALIDATE_DESC(desc); |
Andrew Jeffery | e10f72b | 2017-11-30 14:25:24 +1030 | [diff] [blame] | 3244 | /* |
| 3245 | * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for |
| 3246 | * persistence state. |
| 3247 | */ |
| 3248 | if (transitory) |
| 3249 | set_bit(FLAG_TRANSITORY, &desc->flags); |
| 3250 | else |
| 3251 | clear_bit(FLAG_TRANSITORY, &desc->flags); |
| 3252 | |
| 3253 | /* If the driver supports it, set the persistence state now */ |
| 3254 | chip = desc->gdev->chip; |
| 3255 | if (!chip->set_config) |
| 3256 | return 0; |
| 3257 | |
| 3258 | packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE, |
| 3259 | !transitory); |
| 3260 | gpio = gpio_chip_hwgpio(desc); |
Andrew Jeffery | fa59dd23 | 2019-03-26 15:19:54 +1030 | [diff] [blame] | 3261 | rc = chip->set_config(chip, gpio, packed); |
Andrew Jeffery | e10f72b | 2017-11-30 14:25:24 +1030 | [diff] [blame] | 3262 | if (rc == -ENOTSUPP) { |
| 3263 | dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", |
| 3264 | gpio); |
| 3265 | return 0; |
| 3266 | } |
| 3267 | |
| 3268 | return rc; |
| 3269 | } |
| 3270 | EXPORT_SYMBOL_GPL(gpiod_set_transitory); |
| 3271 | |
| 3272 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3273 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 3274 | * @desc: the gpio descriptor to test |
| 3275 | * |
| 3276 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 3277 | */ |
| 3278 | int gpiod_is_active_low(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3279 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3280 | VALIDATE_DESC(desc); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3281 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3282 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3283 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3284 | |
| 3285 | /* I/O calls are only valid after configuration completed; the relevant |
| 3286 | * "is this a valid GPIO" error checks should already have been done. |
| 3287 | * |
| 3288 | * "Get" operations are often inlinable as reading a pin value register, |
| 3289 | * and masking the relevant bit in that register. |
| 3290 | * |
| 3291 | * When "set" operations are inlinable, they involve writing that mask to |
| 3292 | * one register to set a low value, or a different register to set it high. |
| 3293 | * Otherwise locking is needed, so there may be little value to inlining. |
| 3294 | * |
| 3295 | *------------------------------------------------------------------------ |
| 3296 | * |
| 3297 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 3298 | * have requested the GPIO. That can include implicit requesting by |
| 3299 | * a direction setting call. Marking a gpio as requested locks its chip |
| 3300 | * in memory, guaranteeing that these table lookups need no more locking |
| 3301 | * and that gpiochip_remove() will fail. |
| 3302 | * |
| 3303 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 3304 | * that the GPIO was actually requested. |
| 3305 | */ |
| 3306 | |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3307 | static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3308 | { |
| 3309 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3310 | int offset; |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 3311 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3312 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3313 | chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3314 | offset = gpio_chip_hwgpio(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 3315 | value = chip->get ? chip->get(chip, offset) : -EIO; |
Linus Walleij | 723a630 | 2015-12-21 23:10:12 +0100 | [diff] [blame] | 3316 | value = value < 0 ? value : !!value; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3317 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 3318 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3319 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3320 | |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3321 | static int gpio_chip_get_multiple(struct gpio_chip *chip, |
| 3322 | unsigned long *mask, unsigned long *bits) |
| 3323 | { |
| 3324 | if (chip->get_multiple) { |
| 3325 | return chip->get_multiple(chip, mask, bits); |
| 3326 | } else if (chip->get) { |
| 3327 | int i, value; |
| 3328 | |
| 3329 | for_each_set_bit(i, mask, chip->ngpio) { |
| 3330 | value = chip->get(chip, i); |
| 3331 | if (value < 0) |
| 3332 | return value; |
| 3333 | __assign_bit(i, bits, value); |
| 3334 | } |
| 3335 | return 0; |
| 3336 | } |
| 3337 | return -EIO; |
| 3338 | } |
| 3339 | |
| 3340 | int gpiod_get_array_value_complex(bool raw, bool can_sleep, |
| 3341 | unsigned int array_size, |
| 3342 | struct gpio_desc **desc_array, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3343 | struct gpio_array *array_info, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3344 | unsigned long *value_bitmap) |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3345 | { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3346 | int ret, i = 0; |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3347 | |
| 3348 | /* |
| 3349 | * Validate array_info against desc_array and its size. |
| 3350 | * It should immediately follow desc_array if both |
| 3351 | * have been obtained from the same gpiod_get_array() call. |
| 3352 | */ |
| 3353 | if (array_info && array_info->desc == desc_array && |
| 3354 | array_size <= array_info->size && |
| 3355 | (void *)array_info == desc_array + array_info->size) { |
| 3356 | if (!can_sleep) |
| 3357 | WARN_ON(array_info->chip->can_sleep); |
| 3358 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3359 | ret = gpio_chip_get_multiple(array_info->chip, |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3360 | array_info->get_mask, |
| 3361 | value_bitmap); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3362 | if (ret) |
| 3363 | return ret; |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3364 | |
| 3365 | if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) |
| 3366 | bitmap_xor(value_bitmap, value_bitmap, |
| 3367 | array_info->invert_mask, array_size); |
| 3368 | |
| 3369 | if (bitmap_full(array_info->get_mask, array_size)) |
| 3370 | return 0; |
| 3371 | |
| 3372 | i = find_first_zero_bit(array_info->get_mask, array_size); |
| 3373 | } else { |
| 3374 | array_info = NULL; |
| 3375 | } |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3376 | |
| 3377 | while (i < array_size) { |
| 3378 | struct gpio_chip *chip = desc_array[i]->gdev->chip; |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3379 | unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; |
| 3380 | unsigned long *mask, *bits; |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3381 | int first, j, ret; |
| 3382 | |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3383 | if (likely(chip->ngpio <= FASTPATH_NGPIO)) { |
| 3384 | mask = fastpath; |
| 3385 | } else { |
| 3386 | mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio), |
| 3387 | sizeof(*mask), |
| 3388 | can_sleep ? GFP_KERNEL : GFP_ATOMIC); |
| 3389 | if (!mask) |
| 3390 | return -ENOMEM; |
| 3391 | } |
| 3392 | |
| 3393 | bits = mask + BITS_TO_LONGS(chip->ngpio); |
| 3394 | bitmap_zero(mask, chip->ngpio); |
| 3395 | |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3396 | if (!can_sleep) |
| 3397 | WARN_ON(chip->can_sleep); |
| 3398 | |
| 3399 | /* collect all inputs belonging to the same chip */ |
| 3400 | first = i; |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3401 | do { |
| 3402 | const struct gpio_desc *desc = desc_array[i]; |
| 3403 | int hwgpio = gpio_chip_hwgpio(desc); |
| 3404 | |
| 3405 | __set_bit(hwgpio, mask); |
| 3406 | i++; |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3407 | |
| 3408 | if (array_info) |
Janusz Krzysztofik | 35ae7f9 | 2018-09-24 01:53:35 +0200 | [diff] [blame] | 3409 | i = find_next_zero_bit(array_info->get_mask, |
| 3410 | array_size, i); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3411 | } while ((i < array_size) && |
| 3412 | (desc_array[i]->gdev->chip == chip)); |
| 3413 | |
| 3414 | ret = gpio_chip_get_multiple(chip, mask, bits); |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3415 | if (ret) { |
| 3416 | if (mask != fastpath) |
| 3417 | kfree(mask); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3418 | return ret; |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3419 | } |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3420 | |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3421 | for (j = first; j < i; ) { |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3422 | const struct gpio_desc *desc = desc_array[j]; |
| 3423 | int hwgpio = gpio_chip_hwgpio(desc); |
| 3424 | int value = test_bit(hwgpio, bits); |
| 3425 | |
| 3426 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 3427 | value = !value; |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3428 | __assign_bit(j, value_bitmap, value); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3429 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Janusz Krzysztofik | 799d5eb | 2018-09-29 14:20:22 +0200 | [diff] [blame] | 3430 | j++; |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3431 | |
| 3432 | if (array_info) |
Janusz Krzysztofik | 35ae7f9 | 2018-09-24 01:53:35 +0200 | [diff] [blame] | 3433 | j = find_next_zero_bit(array_info->get_mask, i, |
| 3434 | j); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3435 | } |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3436 | |
| 3437 | if (mask != fastpath) |
| 3438 | kfree(mask); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3439 | } |
| 3440 | return 0; |
| 3441 | } |
| 3442 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3443 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3444 | * gpiod_get_raw_value() - return a gpio's raw value |
| 3445 | * @desc: gpio whose value will be returned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3446 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3447 | * 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] | 3448 | * its ACTIVE_LOW status, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3449 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3450 | * This function can be called from contexts where we cannot sleep, and will |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3451 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3452 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3453 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3454 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3455 | VALIDATE_DESC(desc); |
Geert Uytterhoeven | 3285170f | 2019-07-01 16:27:38 +0200 | [diff] [blame] | 3456 | /* Should be using gpiod_get_raw_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3457 | WARN_ON(desc->gdev->chip->can_sleep); |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3458 | return gpiod_get_raw_value_commit(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3459 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3460 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3461 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3462 | /** |
| 3463 | * gpiod_get_value() - return a gpio's value |
| 3464 | * @desc: gpio whose value will be returned |
| 3465 | * |
| 3466 | * 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] | 3467 | * account, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3468 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3469 | * This function can be called from contexts where we cannot sleep, and will |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3470 | * complain if the GPIO chip functions potentially sleep. |
| 3471 | */ |
| 3472 | int gpiod_get_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3473 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3474 | int value; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3475 | |
| 3476 | VALIDATE_DESC(desc); |
Geert Uytterhoeven | 3285170f | 2019-07-01 16:27:38 +0200 | [diff] [blame] | 3477 | /* Should be using gpiod_get_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3478 | WARN_ON(desc->gdev->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3479 | |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3480 | value = gpiod_get_raw_value_commit(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 3481 | if (value < 0) |
| 3482 | return value; |
| 3483 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3484 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 3485 | value = !value; |
| 3486 | |
| 3487 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3488 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3489 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3490 | |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3491 | /** |
| 3492 | * gpiod_get_raw_array_value() - read raw values from an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3493 | * @array_size: number of elements in the descriptor array / value bitmap |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3494 | * @desc_array: array of GPIO descriptors whose values will be read |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3495 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3496 | * @value_bitmap: bitmap to store the read values |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3497 | * |
| 3498 | * Read the raw values of the GPIOs, i.e. the values of the physical lines |
| 3499 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, |
| 3500 | * else an error code. |
| 3501 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3502 | * This function can be called from contexts where we cannot sleep, |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3503 | * and it will complain if the GPIO chip functions potentially sleep. |
| 3504 | */ |
| 3505 | int gpiod_get_raw_array_value(unsigned int array_size, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3506 | struct gpio_desc **desc_array, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3507 | struct gpio_array *array_info, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3508 | unsigned long *value_bitmap) |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3509 | { |
| 3510 | if (!desc_array) |
| 3511 | return -EINVAL; |
| 3512 | return gpiod_get_array_value_complex(true, false, array_size, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3513 | desc_array, array_info, |
| 3514 | value_bitmap); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3515 | } |
| 3516 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); |
| 3517 | |
| 3518 | /** |
| 3519 | * gpiod_get_array_value() - read values from an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3520 | * @array_size: number of elements in the descriptor array / value bitmap |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3521 | * @desc_array: array of GPIO descriptors whose values will be read |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3522 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3523 | * @value_bitmap: bitmap to store the read values |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3524 | * |
| 3525 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 3526 | * into account. Return 0 in case of success, else an error code. |
| 3527 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3528 | * This function can be called from contexts where we cannot sleep, |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3529 | * and it will complain if the GPIO chip functions potentially sleep. |
| 3530 | */ |
| 3531 | int gpiod_get_array_value(unsigned int array_size, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3532 | struct gpio_desc **desc_array, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3533 | struct gpio_array *array_info, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3534 | unsigned long *value_bitmap) |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3535 | { |
| 3536 | if (!desc_array) |
| 3537 | return -EINVAL; |
| 3538 | return gpiod_get_array_value_complex(false, false, array_size, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3539 | desc_array, array_info, |
| 3540 | value_bitmap); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 3541 | } |
| 3542 | EXPORT_SYMBOL_GPL(gpiod_get_array_value); |
| 3543 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 3544 | /* |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3545 | * 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] | 3546 | * @desc: gpio descriptor whose state need to be set. |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 3547 | * @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] | 3548 | */ |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3549 | 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] | 3550 | { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3551 | int ret = 0; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3552 | struct gpio_chip *chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3553 | int offset = gpio_chip_hwgpio(desc); |
| 3554 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 3555 | if (value) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3556 | ret = chip->direction_input(chip, offset); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 3557 | } else { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3558 | ret = chip->direction_output(chip, offset, 0); |
| 3559 | if (!ret) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3560 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 3561 | } |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3562 | trace_gpio_direction(desc_to_gpio(desc), value, ret); |
| 3563 | if (ret < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 3564 | gpiod_err(desc, |
| 3565 | "%s: Error in set_value for open drain err %d\n", |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3566 | __func__, ret); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 3567 | } |
| 3568 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 3569 | /* |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3570 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 3571 | * @desc: gpio descriptor whose state need to be set. |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 3572 | * @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] | 3573 | */ |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3574 | 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] | 3575 | { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3576 | int ret = 0; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3577 | struct gpio_chip *chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3578 | int offset = gpio_chip_hwgpio(desc); |
| 3579 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 3580 | if (value) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3581 | ret = chip->direction_output(chip, offset, 1); |
| 3582 | if (!ret) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3583 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 3584 | } else { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3585 | ret = chip->direction_input(chip, offset); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 3586 | } |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3587 | trace_gpio_direction(desc_to_gpio(desc), !value, ret); |
| 3588 | if (ret < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 3589 | gpiod_err(desc, |
| 3590 | "%s: Error in set_value for open source err %d\n", |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 3591 | __func__, ret); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 3592 | } |
| 3593 | |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3594 | 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] | 3595 | { |
| 3596 | struct gpio_chip *chip; |
| 3597 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3598 | chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3599 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3600 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3601 | } |
| 3602 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3603 | /* |
| 3604 | * set multiple outputs on the same chip; |
| 3605 | * use the chip's set_multiple function if available; |
| 3606 | * otherwise set the outputs sequentially; |
| 3607 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word |
| 3608 | * defines which outputs are to be changed |
| 3609 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word |
| 3610 | * defines the values the outputs specified by mask are to be set to |
| 3611 | */ |
| 3612 | static void gpio_chip_set_multiple(struct gpio_chip *chip, |
| 3613 | unsigned long *mask, unsigned long *bits) |
| 3614 | { |
| 3615 | if (chip->set_multiple) { |
| 3616 | chip->set_multiple(chip, mask, bits); |
| 3617 | } else { |
Andy Shevchenko | 5e4e6fb | 2017-01-03 19:01:17 +0200 | [diff] [blame] | 3618 | unsigned int i; |
| 3619 | |
| 3620 | /* set outputs if the corresponding mask bit is set */ |
| 3621 | for_each_set_bit(i, mask, chip->ngpio) |
| 3622 | chip->set(chip, i, test_bit(i, bits)); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3623 | } |
| 3624 | } |
| 3625 | |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3626 | int gpiod_set_array_value_complex(bool raw, bool can_sleep, |
Geert Uytterhoeven | 3c94066 | 2018-09-27 13:38:10 +0200 | [diff] [blame] | 3627 | unsigned int array_size, |
| 3628 | struct gpio_desc **desc_array, |
| 3629 | struct gpio_array *array_info, |
| 3630 | unsigned long *value_bitmap) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3631 | { |
| 3632 | int i = 0; |
| 3633 | |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3634 | /* |
| 3635 | * Validate array_info against desc_array and its size. |
| 3636 | * It should immediately follow desc_array if both |
| 3637 | * have been obtained from the same gpiod_get_array() call. |
| 3638 | */ |
| 3639 | if (array_info && array_info->desc == desc_array && |
| 3640 | array_size <= array_info->size && |
| 3641 | (void *)array_info == desc_array + array_info->size) { |
| 3642 | if (!can_sleep) |
| 3643 | WARN_ON(array_info->chip->can_sleep); |
| 3644 | |
| 3645 | if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) |
| 3646 | bitmap_xor(value_bitmap, value_bitmap, |
| 3647 | array_info->invert_mask, array_size); |
| 3648 | |
| 3649 | gpio_chip_set_multiple(array_info->chip, array_info->set_mask, |
| 3650 | value_bitmap); |
| 3651 | |
| 3652 | if (bitmap_full(array_info->set_mask, array_size)) |
| 3653 | return 0; |
| 3654 | |
| 3655 | i = find_first_zero_bit(array_info->set_mask, array_size); |
| 3656 | } else { |
| 3657 | array_info = NULL; |
| 3658 | } |
| 3659 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3660 | while (i < array_size) { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3661 | struct gpio_chip *chip = desc_array[i]->gdev->chip; |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3662 | unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; |
| 3663 | unsigned long *mask, *bits; |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3664 | int count = 0; |
| 3665 | |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3666 | if (likely(chip->ngpio <= FASTPATH_NGPIO)) { |
| 3667 | mask = fastpath; |
| 3668 | } else { |
| 3669 | mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio), |
| 3670 | sizeof(*mask), |
| 3671 | can_sleep ? GFP_KERNEL : GFP_ATOMIC); |
| 3672 | if (!mask) |
| 3673 | return -ENOMEM; |
| 3674 | } |
| 3675 | |
| 3676 | bits = mask + BITS_TO_LONGS(chip->ngpio); |
| 3677 | bitmap_zero(mask, chip->ngpio); |
| 3678 | |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 3679 | if (!can_sleep) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3680 | WARN_ON(chip->can_sleep); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 3681 | |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3682 | do { |
| 3683 | struct gpio_desc *desc = desc_array[i]; |
| 3684 | int hwgpio = gpio_chip_hwgpio(desc); |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3685 | int value = test_bit(i, value_bitmap); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3686 | |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3687 | /* |
| 3688 | * Pins applicable for fast input but not for |
| 3689 | * fast output processing may have been already |
| 3690 | * inverted inside the fast path, skip them. |
| 3691 | */ |
| 3692 | if (!raw && !(array_info && |
| 3693 | test_bit(i, array_info->invert_mask)) && |
| 3694 | test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3695 | value = !value; |
| 3696 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 3697 | /* |
| 3698 | * collect all normal outputs belonging to the same chip |
| 3699 | * open drain and open source outputs are set individually |
| 3700 | */ |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3701 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3702 | gpio_set_open_drain_value_commit(desc, value); |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3703 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3704 | gpio_set_open_source_value_commit(desc, value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3705 | } else { |
| 3706 | __set_bit(hwgpio, mask); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 3707 | if (value) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3708 | __set_bit(hwgpio, bits); |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 3709 | else |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3710 | __clear_bit(hwgpio, bits); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3711 | count++; |
| 3712 | } |
| 3713 | i++; |
Janusz Krzysztofik | b17566a | 2018-09-05 23:50:08 +0200 | [diff] [blame] | 3714 | |
| 3715 | if (array_info) |
Janusz Krzysztofik | 35ae7f9 | 2018-09-24 01:53:35 +0200 | [diff] [blame] | 3716 | i = find_next_zero_bit(array_info->set_mask, |
| 3717 | array_size, i); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3718 | } while ((i < array_size) && |
| 3719 | (desc_array[i]->gdev->chip == chip)); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3720 | /* push collected bits to outputs */ |
Daniel Lockyer | 38e003f | 2015-06-10 14:26:27 +0100 | [diff] [blame] | 3721 | if (count != 0) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3722 | gpio_chip_set_multiple(chip, mask, bits); |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3723 | |
| 3724 | if (mask != fastpath) |
| 3725 | kfree(mask); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3726 | } |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3727 | return 0; |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3728 | } |
| 3729 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3730 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3731 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 3732 | * @desc: gpio whose value will be assigned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3733 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3734 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3735 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 3736 | * regard for its ACTIVE_LOW status. |
| 3737 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3738 | * This function can be called from contexts where we cannot sleep, and will |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3739 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3740 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3741 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3742 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3743 | VALIDATE_DESC_VOID(desc); |
Geert Uytterhoeven | 3285170f | 2019-07-01 16:27:38 +0200 | [diff] [blame] | 3744 | /* Should be using gpiod_set_raw_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3745 | WARN_ON(desc->gdev->chip->can_sleep); |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 3746 | gpiod_set_raw_value_commit(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3747 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3748 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3749 | |
| 3750 | /** |
Geert Uytterhoeven | 1e77fc8 | 2018-01-09 19:08:21 +0100 | [diff] [blame] | 3751 | * gpiod_set_value_nocheck() - set a GPIO line value without checking |
| 3752 | * @desc: the descriptor to set the value on |
| 3753 | * @value: value to set |
| 3754 | * |
| 3755 | * This sets the value of a GPIO line backing a descriptor, applying |
| 3756 | * different semantic quirks like active low and open drain/source |
| 3757 | * handling. |
| 3758 | */ |
| 3759 | static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value) |
| 3760 | { |
| 3761 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 3762 | value = !value; |
| 3763 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 3764 | gpio_set_open_drain_value_commit(desc, value); |
| 3765 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 3766 | gpio_set_open_source_value_commit(desc, value); |
| 3767 | else |
| 3768 | gpiod_set_raw_value_commit(desc, value); |
| 3769 | } |
| 3770 | |
| 3771 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3772 | * gpiod_set_value() - assign a gpio's value |
| 3773 | * @desc: gpio whose value will be assigned |
| 3774 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3775 | * |
Linus Walleij | 02e4798 | 2017-09-26 21:20:23 +0200 | [diff] [blame] | 3776 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, |
| 3777 | * OPEN_DRAIN and OPEN_SOURCE flags into account. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3778 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3779 | * This function can be called from contexts where we cannot sleep, and will |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3780 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3781 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3782 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 3783 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3784 | VALIDATE_DESC_VOID(desc); |
Geert Uytterhoeven | 3285170f | 2019-07-01 16:27:38 +0200 | [diff] [blame] | 3785 | /* Should be using gpiod_set_value_cansleep() */ |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3786 | WARN_ON(desc->gdev->chip->can_sleep); |
Geert Uytterhoeven | 1e77fc8 | 2018-01-09 19:08:21 +0100 | [diff] [blame] | 3787 | gpiod_set_value_nocheck(desc, value); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3788 | } |
| 3789 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 3790 | |
| 3791 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 3792 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3793 | * @array_size: number of elements in the descriptor array / value bitmap |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3794 | * @desc_array: array of GPIO descriptors whose values will be assigned |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3795 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3796 | * @value_bitmap: bitmap of values to assign |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3797 | * |
| 3798 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 3799 | * without regard for their ACTIVE_LOW status. |
| 3800 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3801 | * This function can be called from contexts where we cannot sleep, and will |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3802 | * complain if the GPIO chip functions potentially sleep. |
| 3803 | */ |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3804 | int gpiod_set_raw_array_value(unsigned int array_size, |
Geert Uytterhoeven | 3c94066 | 2018-09-27 13:38:10 +0200 | [diff] [blame] | 3805 | struct gpio_desc **desc_array, |
| 3806 | struct gpio_array *array_info, |
| 3807 | unsigned long *value_bitmap) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3808 | { |
| 3809 | if (!desc_array) |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 3810 | return -EINVAL; |
| 3811 | return gpiod_set_array_value_complex(true, false, array_size, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3812 | desc_array, array_info, value_bitmap); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3813 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 3814 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3815 | |
| 3816 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 3817 | * gpiod_set_array_value() - assign values to an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3818 | * @array_size: number of elements in the descriptor array / value bitmap |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3819 | * @desc_array: array of GPIO descriptors whose values will be assigned |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 3820 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 3821 | * @value_bitmap: bitmap of values to assign |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3822 | * |
| 3823 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 3824 | * into account. |
| 3825 | * |
Geert Uytterhoeven | 827a9b8 | 2019-07-01 16:28:09 +0200 | [diff] [blame] | 3826 | * This function can be called from contexts where we cannot sleep, and will |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3827 | * complain if the GPIO chip functions potentially sleep. |
| 3828 | */ |
Geert Uytterhoeven | cf9af0d | 2018-09-27 13:38:09 +0200 | [diff] [blame] | 3829 | int gpiod_set_array_value(unsigned int array_size, |
| 3830 | struct gpio_desc **desc_array, |
| 3831 | struct gpio_array *array_info, |
| 3832 | unsigned long *value_bitmap) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3833 | { |
| 3834 | if (!desc_array) |
Geert Uytterhoeven | cf9af0d | 2018-09-27 13:38:09 +0200 | [diff] [blame] | 3835 | return -EINVAL; |
| 3836 | return gpiod_set_array_value_complex(false, false, array_size, |
| 3837 | desc_array, array_info, |
| 3838 | value_bitmap); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3839 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 3840 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 3841 | |
| 3842 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3843 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 3844 | * @desc: gpio to check |
| 3845 | * |
| 3846 | */ |
| 3847 | int gpiod_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3848 | { |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3849 | VALIDATE_DESC(desc); |
| 3850 | return desc->gdev->chip->can_sleep; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3851 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3852 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3853 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 3854 | /** |
Linus Walleij | 90b39402e | 2018-06-01 13:21:27 +0200 | [diff] [blame] | 3855 | * gpiod_set_consumer_name() - set the consumer name for the descriptor |
| 3856 | * @desc: gpio to set the consumer name on |
| 3857 | * @name: the new consumer name |
| 3858 | */ |
Muchun Song | 18534df | 2018-11-01 21:12:50 +0800 | [diff] [blame] | 3859 | int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) |
Linus Walleij | 90b39402e | 2018-06-01 13:21:27 +0200 | [diff] [blame] | 3860 | { |
Muchun Song | 18534df | 2018-11-01 21:12:50 +0800 | [diff] [blame] | 3861 | VALIDATE_DESC(desc); |
| 3862 | if (name) { |
| 3863 | name = kstrdup_const(name, GFP_KERNEL); |
| 3864 | if (!name) |
| 3865 | return -ENOMEM; |
| 3866 | } |
| 3867 | |
| 3868 | kfree_const(desc->label); |
| 3869 | desc_set_label(desc, name); |
| 3870 | |
| 3871 | return 0; |
Linus Walleij | 90b39402e | 2018-06-01 13:21:27 +0200 | [diff] [blame] | 3872 | } |
| 3873 | EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); |
| 3874 | |
| 3875 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3876 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 3877 | * @desc: gpio whose IRQ will be returned (already requested) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 3878 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3879 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 3880 | * error. |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 3881 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3882 | int gpiod_to_irq(const struct gpio_desc *desc) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 3883 | { |
Linus Walleij | 4c37ce8 | 2016-05-02 13:13:10 +0200 | [diff] [blame] | 3884 | struct gpio_chip *chip; |
| 3885 | int offset; |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 3886 | |
Linus Walleij | 79bb71b | 2016-06-15 22:57:38 +0200 | [diff] [blame] | 3887 | /* |
| 3888 | * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics |
| 3889 | * requires this function to not return zero on an invalid descriptor |
| 3890 | * but rather a negative error number. |
| 3891 | */ |
Linus Walleij | bfbbe44 | 2016-06-16 11:55:55 +0200 | [diff] [blame] | 3892 | if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) |
Linus Walleij | 79bb71b | 2016-06-15 22:57:38 +0200 | [diff] [blame] | 3893 | return -EINVAL; |
| 3894 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 3895 | chip = desc->gdev->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3896 | offset = gpio_chip_hwgpio(desc); |
Linus Walleij | 4c37ce8 | 2016-05-02 13:13:10 +0200 | [diff] [blame] | 3897 | if (chip->to_irq) { |
| 3898 | int retirq = chip->to_irq(chip, offset); |
| 3899 | |
| 3900 | /* Zero means NO_IRQ */ |
| 3901 | if (!retirq) |
| 3902 | return -ENXIO; |
| 3903 | |
| 3904 | return retirq; |
| 3905 | } |
| 3906 | return -ENXIO; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3907 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 3908 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 3909 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3910 | /** |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 3911 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 3912 | * @chip: the chip the GPIO to lock belongs to |
| 3913 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3914 | * |
| 3915 | * This is used directly by GPIO drivers that want to lock down |
Linus Walleij | f438acd | 2014-03-07 10:12:49 +0800 | [diff] [blame] | 3916 | * a certain GPIO line to be used for IRQs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3917 | */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 3918 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 3919 | { |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 3920 | struct gpio_desc *desc; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3921 | |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 3922 | desc = gpiochip_get_desc(chip, offset); |
| 3923 | if (IS_ERR(desc)) |
| 3924 | return PTR_ERR(desc); |
| 3925 | |
Linus Walleij | 60f8339 | 2016-11-12 15:01:09 +0100 | [diff] [blame] | 3926 | /* |
| 3927 | * If it's fast: flush the direction setting if something changed |
| 3928 | * behind our back |
| 3929 | */ |
| 3930 | if (!chip->can_sleep && chip->get_direction) { |
Andy Shevchenko | 8095679 | 2018-07-09 21:47:21 +0300 | [diff] [blame] | 3931 | int dir = gpiod_get_direction(desc); |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 3932 | |
Andy Shevchenko | 36b3127 | 2018-07-03 03:38:31 +0300 | [diff] [blame] | 3933 | if (dir < 0) { |
| 3934 | chip_err(chip, "%s: cannot get GPIO direction\n", |
| 3935 | __func__); |
| 3936 | return dir; |
| 3937 | } |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 3941 | chip_err(chip, |
Andy Shevchenko | b191171 | 2018-07-03 03:39:03 +0300 | [diff] [blame] | 3942 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 3943 | __func__); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3944 | return -EIO; |
| 3945 | } |
| 3946 | |
Linus Walleij | 9c10280 | 2016-05-25 10:56:03 +0200 | [diff] [blame] | 3947 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
Hans Verkuil | 4e9439d | 2018-09-08 11:23:16 +0200 | [diff] [blame] | 3948 | set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 3949 | |
| 3950 | /* |
| 3951 | * If the consumer has not set up a label (such as when the |
| 3952 | * IRQ is referenced from .to_irq()) we set up a label here |
| 3953 | * so it is clear this is used as an interrupt. |
| 3954 | */ |
| 3955 | if (!desc->label) |
| 3956 | desc_set_label(desc, "interrupt"); |
| 3957 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3958 | return 0; |
| 3959 | } |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 3960 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3961 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3962 | /** |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 3963 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
Alexandre Courbot | d74be6d | 2014-07-22 16:17:42 +0900 | [diff] [blame] | 3964 | * @chip: the chip the GPIO to lock belongs to |
| 3965 | * @offset: the offset of the GPIO to lock as IRQ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3966 | * |
| 3967 | * This is used directly by GPIO drivers that want to indicate |
| 3968 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 3969 | */ |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 3970 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3971 | { |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 3972 | struct gpio_desc *desc; |
| 3973 | |
| 3974 | desc = gpiochip_get_desc(chip, offset); |
| 3975 | if (IS_ERR(desc)) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3976 | return; |
| 3977 | |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 3978 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
Hans Verkuil | 4e9439d | 2018-09-08 11:23:16 +0200 | [diff] [blame] | 3979 | clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
Linus Walleij | 3940c34 | 2016-11-14 00:09:07 +0100 | [diff] [blame] | 3980 | |
| 3981 | /* If we only had this marking, erase it */ |
| 3982 | if (desc->label && !strcmp(desc->label, "interrupt")) |
| 3983 | desc_set_label(desc, NULL); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3984 | } |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 3985 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 3986 | |
Hans Verkuil | 4e9439d | 2018-09-08 11:23:16 +0200 | [diff] [blame] | 3987 | void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset) |
| 3988 | { |
| 3989 | struct gpio_desc *desc = gpiochip_get_desc(chip, offset); |
| 3990 | |
| 3991 | if (!IS_ERR(desc) && |
| 3992 | !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) |
| 3993 | clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
| 3994 | } |
| 3995 | EXPORT_SYMBOL_GPL(gpiochip_disable_irq); |
| 3996 | |
| 3997 | void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset) |
| 3998 | { |
| 3999 | struct gpio_desc *desc = gpiochip_get_desc(chip, offset); |
| 4000 | |
| 4001 | if (!IS_ERR(desc) && |
| 4002 | !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { |
| 4003 | WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags)); |
| 4004 | set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
| 4005 | } |
| 4006 | } |
| 4007 | EXPORT_SYMBOL_GPL(gpiochip_enable_irq); |
| 4008 | |
Linus Walleij | 6cee382 | 2016-02-11 20:16:45 +0100 | [diff] [blame] | 4009 | bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset) |
| 4010 | { |
| 4011 | if (offset >= chip->ngpio) |
| 4012 | return false; |
| 4013 | |
| 4014 | return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags); |
| 4015 | } |
| 4016 | EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); |
| 4017 | |
Hans Verkuil | 4e6b823 | 2018-09-08 11:23:14 +0200 | [diff] [blame] | 4018 | int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset) |
| 4019 | { |
| 4020 | int ret; |
| 4021 | |
| 4022 | if (!try_module_get(chip->gpiodev->owner)) |
| 4023 | return -ENODEV; |
| 4024 | |
| 4025 | ret = gpiochip_lock_as_irq(chip, offset); |
| 4026 | if (ret) { |
| 4027 | chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset); |
| 4028 | module_put(chip->gpiodev->owner); |
| 4029 | return ret; |
| 4030 | } |
| 4031 | return 0; |
| 4032 | } |
| 4033 | EXPORT_SYMBOL_GPL(gpiochip_reqres_irq); |
| 4034 | |
| 4035 | void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset) |
| 4036 | { |
| 4037 | gpiochip_unlock_as_irq(chip, offset); |
| 4038 | module_put(chip->gpiodev->owner); |
| 4039 | } |
| 4040 | EXPORT_SYMBOL_GPL(gpiochip_relres_irq); |
| 4041 | |
Linus Walleij | 143b65d | 2016-02-16 15:41:42 +0100 | [diff] [blame] | 4042 | bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset) |
| 4043 | { |
| 4044 | if (offset >= chip->ngpio) |
| 4045 | return false; |
| 4046 | |
| 4047 | return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags); |
| 4048 | } |
| 4049 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); |
| 4050 | |
| 4051 | bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset) |
| 4052 | { |
| 4053 | if (offset >= chip->ngpio) |
| 4054 | return false; |
| 4055 | |
| 4056 | return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags); |
| 4057 | } |
| 4058 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); |
| 4059 | |
Charles Keepax | 05f479b | 2017-05-23 15:47:29 +0100 | [diff] [blame] | 4060 | bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset) |
| 4061 | { |
| 4062 | if (offset >= chip->ngpio) |
| 4063 | return false; |
| 4064 | |
Andrew Jeffery | e10f72b | 2017-11-30 14:25:24 +1030 | [diff] [blame] | 4065 | return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags); |
Charles Keepax | 05f479b | 2017-05-23 15:47:29 +0100 | [diff] [blame] | 4066 | } |
| 4067 | EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); |
| 4068 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4069 | /** |
| 4070 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 4071 | * @desc: gpio whose value will be returned |
| 4072 | * |
| 4073 | * 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] | 4074 | * its ACTIVE_LOW status, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4075 | * |
| 4076 | * This function is to be called from contexts that can sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4077 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4078 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4079 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4080 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 4081 | VALIDATE_DESC(desc); |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 4082 | return gpiod_get_raw_value_commit(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4083 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4084 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4085 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4086 | /** |
| 4087 | * gpiod_get_value_cansleep() - return a gpio's value |
| 4088 | * @desc: gpio whose value will be returned |
| 4089 | * |
| 4090 | * 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] | 4091 | * account, or negative errno on failure. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4092 | * |
| 4093 | * This function is to be called from contexts that can sleep. |
| 4094 | */ |
| 4095 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4096 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4097 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4098 | |
| 4099 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 4100 | VALIDATE_DESC(desc); |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 4101 | value = gpiod_get_raw_value_commit(desc); |
Bjorn Andersson | e20538b | 2015-08-28 09:44:18 -0700 | [diff] [blame] | 4102 | if (value < 0) |
| 4103 | return value; |
| 4104 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4105 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 4106 | value = !value; |
| 4107 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4108 | return value; |
| 4109 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4110 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4111 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4112 | /** |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4113 | * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4114 | * @array_size: number of elements in the descriptor array / value bitmap |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4115 | * @desc_array: array of GPIO descriptors whose values will be read |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4116 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4117 | * @value_bitmap: bitmap to store the read values |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4118 | * |
| 4119 | * Read the raw values of the GPIOs, i.e. the values of the physical lines |
| 4120 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, |
| 4121 | * else an error code. |
| 4122 | * |
| 4123 | * This function is to be called from contexts that can sleep. |
| 4124 | */ |
| 4125 | int gpiod_get_raw_array_value_cansleep(unsigned int array_size, |
| 4126 | struct gpio_desc **desc_array, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4127 | struct gpio_array *array_info, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4128 | unsigned long *value_bitmap) |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4129 | { |
| 4130 | might_sleep_if(extra_checks); |
| 4131 | if (!desc_array) |
| 4132 | return -EINVAL; |
| 4133 | return gpiod_get_array_value_complex(true, true, array_size, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4134 | desc_array, array_info, |
| 4135 | value_bitmap); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4136 | } |
| 4137 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); |
| 4138 | |
| 4139 | /** |
| 4140 | * gpiod_get_array_value_cansleep() - read values from an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4141 | * @array_size: number of elements in the descriptor array / value bitmap |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4142 | * @desc_array: array of GPIO descriptors whose values will be read |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4143 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4144 | * @value_bitmap: bitmap to store the read values |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4145 | * |
| 4146 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 4147 | * into account. Return 0 in case of success, else an error code. |
| 4148 | * |
| 4149 | * This function is to be called from contexts that can sleep. |
| 4150 | */ |
| 4151 | int gpiod_get_array_value_cansleep(unsigned int array_size, |
| 4152 | struct gpio_desc **desc_array, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4153 | struct gpio_array *array_info, |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4154 | unsigned long *value_bitmap) |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4155 | { |
| 4156 | might_sleep_if(extra_checks); |
| 4157 | if (!desc_array) |
| 4158 | return -EINVAL; |
| 4159 | return gpiod_get_array_value_complex(false, true, array_size, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4160 | desc_array, array_info, |
| 4161 | value_bitmap); |
Lukas Wunner | eec1d56 | 2017-10-12 12:40:10 +0200 | [diff] [blame] | 4162 | } |
| 4163 | EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); |
| 4164 | |
| 4165 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4166 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 4167 | * @desc: gpio whose value will be assigned |
| 4168 | * @value: value to assign |
| 4169 | * |
| 4170 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 4171 | * regard for its ACTIVE_LOW status. |
| 4172 | * |
| 4173 | * This function is to be called from contexts that can sleep. |
| 4174 | */ |
| 4175 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4176 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4177 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 4178 | VALIDATE_DESC_VOID(desc); |
Linus Walleij | fac9d88 | 2017-09-26 20:58:28 +0200 | [diff] [blame] | 4179 | gpiod_set_raw_value_commit(desc, value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4180 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4181 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4182 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4183 | /** |
| 4184 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 4185 | * @desc: gpio whose value will be assigned |
| 4186 | * @value: value to assign |
| 4187 | * |
| 4188 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 4189 | * account |
| 4190 | * |
| 4191 | * This function is to be called from contexts that can sleep. |
| 4192 | */ |
| 4193 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 4194 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4195 | might_sleep_if(extra_checks); |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 4196 | VALIDATE_DESC_VOID(desc); |
Geert Uytterhoeven | 1e77fc8 | 2018-01-09 19:08:21 +0100 | [diff] [blame] | 4197 | gpiod_set_value_nocheck(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4198 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 4199 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4200 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4201 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 4202 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4203 | * @array_size: number of elements in the descriptor array / value bitmap |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4204 | * @desc_array: array of GPIO descriptors whose values will be assigned |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4205 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4206 | * @value_bitmap: bitmap of values to assign |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4207 | * |
| 4208 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 4209 | * without regard for their ACTIVE_LOW status. |
| 4210 | * |
| 4211 | * This function is to be called from contexts that can sleep. |
| 4212 | */ |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 4213 | int gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
Geert Uytterhoeven | 3c94066 | 2018-09-27 13:38:10 +0200 | [diff] [blame] | 4214 | struct gpio_desc **desc_array, |
| 4215 | struct gpio_array *array_info, |
| 4216 | unsigned long *value_bitmap) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4217 | { |
| 4218 | might_sleep_if(extra_checks); |
| 4219 | if (!desc_array) |
Laura Abbott | 3027743 | 2018-05-21 10:57:07 -0700 | [diff] [blame] | 4220 | return -EINVAL; |
| 4221 | return gpiod_set_array_value_complex(true, true, array_size, desc_array, |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4222 | array_info, value_bitmap); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4223 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 4224 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4225 | |
| 4226 | /** |
Dmitry Torokhov | 3946d18 | 2017-08-14 21:59:55 -0700 | [diff] [blame] | 4227 | * gpiod_add_lookup_tables() - register GPIO device consumers |
| 4228 | * @tables: list of tables of consumers to register |
| 4229 | * @n: number of tables in the list |
| 4230 | */ |
| 4231 | void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) |
| 4232 | { |
| 4233 | unsigned int i; |
| 4234 | |
| 4235 | mutex_lock(&gpio_lookup_lock); |
| 4236 | |
| 4237 | for (i = 0; i < n; i++) |
| 4238 | list_add_tail(&tables[i]->list, &gpio_lookup_list); |
| 4239 | |
| 4240 | mutex_unlock(&gpio_lookup_lock); |
| 4241 | } |
| 4242 | |
| 4243 | /** |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 4244 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4245 | * @array_size: number of elements in the descriptor array / value bitmap |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4246 | * @desc_array: array of GPIO descriptors whose values will be assigned |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 4247 | * @array_info: information on applicability of fast bitmap processing path |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 4248 | * @value_bitmap: bitmap of values to assign |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4249 | * |
| 4250 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 4251 | * into account. |
| 4252 | * |
| 4253 | * This function is to be called from contexts that can sleep. |
| 4254 | */ |
Geert Uytterhoeven | cf9af0d | 2018-09-27 13:38:09 +0200 | [diff] [blame] | 4255 | int gpiod_set_array_value_cansleep(unsigned int array_size, |
| 4256 | struct gpio_desc **desc_array, |
| 4257 | struct gpio_array *array_info, |
| 4258 | unsigned long *value_bitmap) |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4259 | { |
| 4260 | might_sleep_if(extra_checks); |
| 4261 | if (!desc_array) |
Geert Uytterhoeven | cf9af0d | 2018-09-27 13:38:09 +0200 | [diff] [blame] | 4262 | return -EINVAL; |
| 4263 | return gpiod_set_array_value_complex(false, true, array_size, |
| 4264 | desc_array, array_info, |
| 4265 | value_bitmap); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4266 | } |
Rojhalat Ibrahim | 3fff99b | 2015-05-13 11:04:56 +0200 | [diff] [blame] | 4267 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
Rojhalat Ibrahim | 5f42424 | 2014-11-04 17:12:06 +0100 | [diff] [blame] | 4268 | |
| 4269 | /** |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4270 | * gpiod_add_lookup_table() - register GPIO device consumers |
| 4271 | * @table: table of consumers to register |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4272 | */ |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4273 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4274 | { |
| 4275 | mutex_lock(&gpio_lookup_lock); |
| 4276 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4277 | list_add_tail(&table->list, &gpio_lookup_list); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4278 | |
| 4279 | mutex_unlock(&gpio_lookup_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4280 | } |
Anatolij Gustschin | 226b224 | 2017-04-20 23:23:20 +0200 | [diff] [blame] | 4281 | EXPORT_SYMBOL_GPL(gpiod_add_lookup_table); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4282 | |
Shobhit Kumar | be9015a | 2015-06-26 14:32:04 +0530 | [diff] [blame] | 4283 | /** |
| 4284 | * gpiod_remove_lookup_table() - unregister GPIO device consumers |
| 4285 | * @table: table of consumers to unregister |
| 4286 | */ |
| 4287 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) |
| 4288 | { |
| 4289 | mutex_lock(&gpio_lookup_lock); |
| 4290 | |
| 4291 | list_del(&table->list); |
| 4292 | |
| 4293 | mutex_unlock(&gpio_lookup_lock); |
| 4294 | } |
Anatolij Gustschin | 226b224 | 2017-04-20 23:23:20 +0200 | [diff] [blame] | 4295 | EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table); |
Shobhit Kumar | be9015a | 2015-06-26 14:32:04 +0530 | [diff] [blame] | 4296 | |
Bartosz Golaszewski | a411e81 | 2018-04-10 22:30:28 +0200 | [diff] [blame] | 4297 | /** |
| 4298 | * gpiod_add_hogs() - register a set of GPIO hogs from machine code |
| 4299 | * @hogs: table of gpio hog entries with a zeroed sentinel at the end |
| 4300 | */ |
| 4301 | void gpiod_add_hogs(struct gpiod_hog *hogs) |
| 4302 | { |
| 4303 | struct gpio_chip *chip; |
| 4304 | struct gpiod_hog *hog; |
| 4305 | |
| 4306 | mutex_lock(&gpio_machine_hogs_mutex); |
| 4307 | |
| 4308 | for (hog = &hogs[0]; hog->chip_label; hog++) { |
| 4309 | list_add_tail(&hog->list, &gpio_machine_hogs); |
| 4310 | |
| 4311 | /* |
| 4312 | * The chip may have been registered earlier, so check if it |
| 4313 | * exists and, if so, try to hog the line now. |
| 4314 | */ |
| 4315 | chip = find_chip_by_name(hog->chip_label); |
| 4316 | if (chip) |
| 4317 | gpiochip_machine_hog(chip, hog); |
| 4318 | } |
| 4319 | |
| 4320 | mutex_unlock(&gpio_machine_hogs_mutex); |
| 4321 | } |
| 4322 | EXPORT_SYMBOL_GPL(gpiod_add_hogs); |
| 4323 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4324 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
| 4325 | { |
| 4326 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 4327 | struct gpiod_lookup_table *table; |
| 4328 | |
| 4329 | mutex_lock(&gpio_lookup_lock); |
| 4330 | |
| 4331 | list_for_each_entry(table, &gpio_lookup_list, list) { |
| 4332 | if (table->dev_id && dev_id) { |
| 4333 | /* |
| 4334 | * Valid strings on both ends, must be identical to have |
| 4335 | * a match |
| 4336 | */ |
| 4337 | if (!strcmp(table->dev_id, dev_id)) |
| 4338 | goto found; |
| 4339 | } else { |
| 4340 | /* |
| 4341 | * One of the pointers is NULL, so both must be to have |
| 4342 | * a match |
| 4343 | */ |
| 4344 | if (dev_id == table->dev_id) |
| 4345 | goto found; |
| 4346 | } |
| 4347 | } |
| 4348 | table = NULL; |
| 4349 | |
| 4350 | found: |
| 4351 | mutex_unlock(&gpio_lookup_lock); |
| 4352 | return table; |
| 4353 | } |
| 4354 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4355 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
Andy Shevchenko | fed7026 | 2019-04-10 18:39:16 +0300 | [diff] [blame] | 4356 | unsigned int idx, unsigned long *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4357 | { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 4358 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4359 | struct gpiod_lookup_table *table; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4360 | struct gpiod_lookup *p; |
| 4361 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4362 | table = gpiod_find_lookup_table(dev); |
| 4363 | if (!table) |
| 4364 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4365 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4366 | for (p = &table->table[0]; p->chip_label; p++) { |
| 4367 | struct gpio_chip *chip; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4368 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4369 | /* idx must always match exactly */ |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4370 | if (p->idx != idx) |
| 4371 | continue; |
| 4372 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4373 | /* If the lookup entry has a con_id, require exact match */ |
| 4374 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) |
| 4375 | continue; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4376 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4377 | chip = find_chip_by_name(p->chip_label); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4378 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4379 | if (!chip) { |
Janusz Krzysztofik | 8853daf | 2018-07-04 00:18:19 +0200 | [diff] [blame] | 4380 | /* |
| 4381 | * As the lookup table indicates a chip with |
| 4382 | * p->chip_label should exist, assume it may |
| 4383 | * still appear later and let the interested |
| 4384 | * consumer be probed again or let the Deferred |
| 4385 | * Probe infrastructure handle the error. |
| 4386 | */ |
| 4387 | dev_warn(dev, "cannot find GPIO chip %s, deferring\n", |
| 4388 | p->chip_label); |
| 4389 | return ERR_PTR(-EPROBE_DEFER); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4390 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4391 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4392 | if (chip->ngpio <= p->chip_hwnum) { |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 4393 | dev_err(dev, |
| 4394 | "requested GPIO %d is out of range [0..%d] for chip %s\n", |
| 4395 | idx, chip->ngpio, chip->label); |
| 4396 | return ERR_PTR(-EINVAL); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4397 | } |
| 4398 | |
Alexandre Courbot | bb1e88c | 2014-02-09 17:43:54 +0900 | [diff] [blame] | 4399 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4400 | *flags = p->flags; |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 4401 | |
| 4402 | return desc; |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4403 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4404 | |
| 4405 | return desc; |
| 4406 | } |
| 4407 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4408 | static int platform_gpio_count(struct device *dev, const char *con_id) |
| 4409 | { |
| 4410 | struct gpiod_lookup_table *table; |
| 4411 | struct gpiod_lookup *p; |
| 4412 | unsigned int count = 0; |
| 4413 | |
| 4414 | table = gpiod_find_lookup_table(dev); |
| 4415 | if (!table) |
| 4416 | return -ENOENT; |
| 4417 | |
| 4418 | for (p = &table->table[0]; p->chip_label; p++) { |
| 4419 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || |
| 4420 | (!con_id && !p->con_id)) |
| 4421 | count++; |
| 4422 | } |
| 4423 | if (!count) |
| 4424 | return -ENOENT; |
| 4425 | |
| 4426 | return count; |
| 4427 | } |
| 4428 | |
| 4429 | /** |
Dmitry Torokhov | 13949fa | 2019-09-12 20:22:39 -0700 | [diff] [blame] | 4430 | * fwnode_gpiod_get_index - obtain a GPIO from firmware node |
| 4431 | * @fwnode: handle of the firmware node |
| 4432 | * @con_id: function within the GPIO consumer |
| 4433 | * @index: index of the GPIO to obtain for the consumer |
| 4434 | * @flags: GPIO initialization flags |
| 4435 | * @label: label to attach to the requested GPIO |
| 4436 | * |
| 4437 | * This function can be used for drivers that get their configuration |
| 4438 | * from opaque firmware. |
| 4439 | * |
| 4440 | * The function properly finds the corresponding GPIO using whatever is the |
| 4441 | * underlying firmware interface and then makes sure that the GPIO |
| 4442 | * descriptor is requested before it is returned to the caller. |
| 4443 | * |
| 4444 | * Returns: |
| 4445 | * On successful request the GPIO pin is configured in accordance with |
| 4446 | * provided @flags. |
| 4447 | * |
| 4448 | * In case of error an ERR_PTR() is returned. |
| 4449 | */ |
| 4450 | struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, |
| 4451 | const char *con_id, int index, |
| 4452 | enum gpiod_flags flags, |
| 4453 | const char *label) |
| 4454 | { |
| 4455 | struct gpio_desc *desc; |
| 4456 | char prop_name[32]; /* 32 is max size of property name */ |
| 4457 | unsigned int i; |
| 4458 | |
| 4459 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 4460 | if (con_id) |
| 4461 | snprintf(prop_name, sizeof(prop_name), "%s-%s", |
| 4462 | con_id, gpio_suffixes[i]); |
| 4463 | else |
| 4464 | snprintf(prop_name, sizeof(prop_name), "%s", |
| 4465 | gpio_suffixes[i]); |
| 4466 | |
| 4467 | desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags, |
| 4468 | label); |
| 4469 | if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) |
| 4470 | break; |
| 4471 | } |
| 4472 | |
| 4473 | return desc; |
| 4474 | } |
| 4475 | EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); |
| 4476 | |
| 4477 | /** |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4478 | * gpiod_count - return the number of GPIOs associated with a device / function |
| 4479 | * or -ENOENT if no GPIO has been assigned to the requested function |
| 4480 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 4481 | * @con_id: function within the GPIO consumer |
| 4482 | */ |
| 4483 | int gpiod_count(struct device *dev, const char *con_id) |
| 4484 | { |
| 4485 | int count = -ENOENT; |
| 4486 | |
| 4487 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
Linus Walleij | f626d6d | 2019-07-17 09:10:01 +0200 | [diff] [blame] | 4488 | count = of_gpio_get_count(dev, con_id); |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4489 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) |
| 4490 | count = acpi_gpio_count(dev, con_id); |
| 4491 | |
| 4492 | if (count < 0) |
| 4493 | count = platform_gpio_count(dev, con_id); |
| 4494 | |
| 4495 | return count; |
| 4496 | } |
| 4497 | EXPORT_SYMBOL_GPL(gpiod_count); |
| 4498 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4499 | /** |
Thierry Reding | 0879162 | 2014-04-25 16:54:22 +0200 | [diff] [blame] | 4500 | * gpiod_get - obtain a GPIO for a given GPIO function |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 4501 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4502 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4503 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4504 | * |
| 4505 | * Return the GPIO descriptor corresponding to the function con_id of device |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 4506 | * 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] | 4507 | * 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] | 4508 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4509 | 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] | 4510 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4511 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4512 | return gpiod_get_index(dev, con_id, 0, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4513 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4514 | EXPORT_SYMBOL_GPL(gpiod_get); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4515 | |
| 4516 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4517 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function |
| 4518 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 4519 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4520 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4521 | * |
| 4522 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to |
| 4523 | * the requested function it will return NULL. This is convenient for drivers |
| 4524 | * that need to handle optional GPIOs. |
| 4525 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4526 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4527 | const char *con_id, |
| 4528 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4529 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4530 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4531 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4532 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4533 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4534 | |
| 4535 | /** |
| 4536 | * gpiod_configure_flags - helper function to configure a given GPIO |
| 4537 | * @desc: gpio whose value will be assigned |
| 4538 | * @con_id: function within the GPIO consumer |
Andy Shevchenko | fed7026 | 2019-04-10 18:39:16 +0300 | [diff] [blame] | 4539 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
| 4540 | * of_find_gpio() or of_get_gpio_hog() |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4541 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 4542 | * |
| 4543 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the |
| 4544 | * requested function and/or index, or another IS_ERR() code if an error |
| 4545 | * occurred while trying to acquire the GPIO. |
| 4546 | */ |
Andy Shevchenko | c29fd9e | 2017-05-23 20:03:16 +0300 | [diff] [blame] | 4547 | int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 4548 | unsigned long lflags, enum gpiod_flags dflags) |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4549 | { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4550 | int ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4551 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 4552 | if (lflags & GPIO_ACTIVE_LOW) |
| 4553 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Linus Walleij | f926dfc | 2017-09-10 19:26:22 +0200 | [diff] [blame] | 4554 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 4555 | if (lflags & GPIO_OPEN_DRAIN) |
| 4556 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Linus Walleij | f926dfc | 2017-09-10 19:26:22 +0200 | [diff] [blame] | 4557 | else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) { |
| 4558 | /* |
| 4559 | * This enforces open drain mode from the consumer side. |
| 4560 | * This is necessary for some busses like I2C, but the lookup |
| 4561 | * should *REALLY* have specified them as open drain in the |
| 4562 | * first place, so print a little warning here. |
| 4563 | */ |
| 4564 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 4565 | gpiod_warn(desc, |
| 4566 | "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); |
| 4567 | } |
| 4568 | |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 4569 | if (lflags & GPIO_OPEN_SOURCE) |
| 4570 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Andrew Jeffery | e10f72b | 2017-11-30 14:25:24 +1030 | [diff] [blame] | 4571 | |
Thomas Petazzoni | d449991 | 2019-02-07 17:28:58 +0100 | [diff] [blame] | 4572 | if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) { |
| 4573 | gpiod_err(desc, |
| 4574 | "both pull-up and pull-down enabled, invalid configuration\n"); |
| 4575 | return -EINVAL; |
| 4576 | } |
| 4577 | |
| 4578 | if (lflags & GPIO_PULL_UP) |
| 4579 | set_bit(FLAG_PULL_UP, &desc->flags); |
| 4580 | else if (lflags & GPIO_PULL_DOWN) |
| 4581 | set_bit(FLAG_PULL_DOWN, &desc->flags); |
| 4582 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4583 | ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); |
| 4584 | if (ret < 0) |
| 4585 | return ret; |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 4586 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4587 | /* No particular flag request, return here... */ |
| 4588 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { |
| 4589 | pr_debug("no flags found for %s\n", con_id); |
| 4590 | return 0; |
| 4591 | } |
| 4592 | |
| 4593 | /* Process flags */ |
| 4594 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4595 | ret = gpiod_direction_output(desc, |
Linus Walleij | ad17731 | 2016-11-13 23:02:44 +0100 | [diff] [blame] | 4596 | !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4597 | else |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4598 | ret = gpiod_direction_input(desc); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4599 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4600 | return ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4601 | } |
| 4602 | |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4603 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4604 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
Andy Shevchenko | fdd6a5f | 2013-12-05 11:26:26 +0200 | [diff] [blame] | 4605 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4606 | * @con_id: function within the GPIO consumer |
| 4607 | * @idx: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4608 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4609 | * |
| 4610 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 4611 | * defined one for functions that define several GPIOs. |
| 4612 | * |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 4613 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
| 4614 | * 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] | 4615 | * occurred while trying to acquire the GPIO. |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4616 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4617 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4618 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4619 | unsigned int idx, |
| 4620 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4621 | { |
Andy Shevchenko | 2d6c06f | 2019-04-10 18:39:17 +0300 | [diff] [blame] | 4622 | unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 4623 | struct gpio_desc *desc = NULL; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4624 | int ret; |
Linus Walleij | 7d18f0a | 2018-01-16 08:29:50 +0100 | [diff] [blame] | 4625 | /* Maybe we have a device name, maybe not */ |
| 4626 | const char *devname = dev ? dev_name(dev) : "?"; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4627 | |
| 4628 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 4629 | |
Rafael J. Wysocki | 4d8440b | 2015-03-10 23:08:57 +0100 | [diff] [blame] | 4630 | if (dev) { |
| 4631 | /* Using device tree? */ |
| 4632 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 4633 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 4634 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); |
| 4635 | } else if (ACPI_COMPANION(dev)) { |
| 4636 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
Andy Shevchenko | a31f5c3 | 2017-05-23 20:03:23 +0300 | [diff] [blame] | 4637 | desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags); |
Rafael J. Wysocki | 4d8440b | 2015-03-10 23:08:57 +0100 | [diff] [blame] | 4638 | } |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /* |
| 4642 | * Either we are not using DT or ACPI, or their lookup did not return |
| 4643 | * a result. In that case, use platform lookup as a fallback. |
| 4644 | */ |
Alexandre Courbot | 2a3cf6a | 2013-12-11 11:32:28 +0900 | [diff] [blame] | 4645 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
Alexander Shiyan | 43a8785 | 2014-09-19 11:39:25 +0400 | [diff] [blame] | 4646 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4647 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | if (IS_ERR(desc)) { |
Wang Dongsheng | 9d5a1f2 | 2018-02-27 00:12:13 -0800 | [diff] [blame] | 4651 | dev_dbg(dev, "No GPIO consumer %s found\n", con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4652 | return desc; |
| 4653 | } |
| 4654 | |
Linus Walleij | 7d18f0a | 2018-01-16 08:29:50 +0100 | [diff] [blame] | 4655 | /* |
| 4656 | * If a connection label was passed use that, else attempt to use |
| 4657 | * the device name as label |
| 4658 | */ |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4659 | ret = gpiod_request(desc, con_id ? con_id : devname); |
| 4660 | if (ret < 0) { |
| 4661 | if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { |
Linus Walleij | b0ce7b29 | 2018-10-12 14:54:12 +0200 | [diff] [blame] | 4662 | /* |
| 4663 | * This happens when there are several consumers for |
| 4664 | * the same GPIO line: we just return here without |
| 4665 | * further initialization. It is a bit if a hack. |
| 4666 | * This is necessary to support fixed regulators. |
| 4667 | * |
| 4668 | * FIXME: Make this more sane and safe. |
| 4669 | */ |
| 4670 | dev_info(dev, "nonexclusive access to GPIO for %s\n", |
| 4671 | con_id ? con_id : devname); |
| 4672 | return desc; |
| 4673 | } else { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4674 | return ERR_PTR(ret); |
Linus Walleij | b0ce7b29 | 2018-10-12 14:54:12 +0200 | [diff] [blame] | 4675 | } |
| 4676 | } |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4677 | |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4678 | ret = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
| 4679 | if (ret < 0) { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4680 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
| 4681 | gpiod_put(desc); |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4682 | return ERR_PTR(ret); |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4683 | } |
| 4684 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4685 | return desc; |
| 4686 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4687 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4688 | |
| 4689 | /** |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4690 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node |
| 4691 | * @fwnode: handle of the firmware node |
| 4692 | * @propname: name of the firmware property representing the GPIO |
Linus Walleij | 6392cca | 2017-12-29 02:07:54 +0100 | [diff] [blame] | 4693 | * @index: index of the GPIO to obtain for the consumer |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 4694 | * @dflags: GPIO initialization flags |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 4695 | * @label: label to attach to the requested GPIO |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4696 | * |
| 4697 | * This function can be used for drivers that get their configuration |
Linus Walleij | 6392cca | 2017-12-29 02:07:54 +0100 | [diff] [blame] | 4698 | * from opaque firmware. |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4699 | * |
Linus Walleij | 6392cca | 2017-12-29 02:07:54 +0100 | [diff] [blame] | 4700 | * The function properly finds the corresponding GPIO using whatever is the |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4701 | * underlying firmware interface and then makes sure that the GPIO |
| 4702 | * descriptor is requested before it is returned to the caller. |
| 4703 | * |
Thierry Reding | 950d55f5 | 2017-07-24 16:57:22 +0200 | [diff] [blame] | 4704 | * Returns: |
Andy Shevchenko | ff21378 | 2017-02-28 17:03:12 +0200 | [diff] [blame] | 4705 | * On successful request the GPIO pin is configured in accordance with |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 4706 | * provided @dflags. |
| 4707 | * |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4708 | * In case of error an ERR_PTR() is returned. |
| 4709 | */ |
| 4710 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 4711 | const char *propname, int index, |
Alexander Stein | b2987d7 | 2017-01-12 17:39:24 +0100 | [diff] [blame] | 4712 | enum gpiod_flags dflags, |
| 4713 | const char *label) |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4714 | { |
Andy Shevchenko | 2d6c06f | 2019-04-10 18:39:17 +0300 | [diff] [blame] | 4715 | unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4716 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4717 | int ret; |
| 4718 | |
| 4719 | if (!fwnode) |
| 4720 | return ERR_PTR(-EINVAL); |
| 4721 | |
| 4722 | if (is_of_node(fwnode)) { |
Linus Walleij | 6392cca | 2017-12-29 02:07:54 +0100 | [diff] [blame] | 4723 | desc = gpiod_get_from_of_node(to_of_node(fwnode), |
| 4724 | propname, index, |
| 4725 | dflags, |
| 4726 | label); |
| 4727 | return desc; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4728 | } else if (is_acpi_node(fwnode)) { |
| 4729 | struct acpi_gpio_info info; |
| 4730 | |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 4731 | desc = acpi_node_get_gpiod(fwnode, propname, index, &info); |
Linus Walleij | 6392cca | 2017-12-29 02:07:54 +0100 | [diff] [blame] | 4732 | if (IS_ERR(desc)) |
| 4733 | return desc; |
| 4734 | |
| 4735 | acpi_gpio_update_gpiod_flags(&dflags, &info); |
Andy Shevchenko | 606be34 | 2019-04-10 18:39:20 +0300 | [diff] [blame] | 4736 | acpi_gpio_update_gpiod_lookup_flags(&lflags, &info); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4737 | } |
| 4738 | |
Linus Walleij | 6392cca | 2017-12-29 02:07:54 +0100 | [diff] [blame] | 4739 | /* Currently only ACPI takes this path */ |
Alexander Stein | b2987d7 | 2017-01-12 17:39:24 +0100 | [diff] [blame] | 4740 | ret = gpiod_request(desc, label); |
Johan Hovold | 85b03b3 | 2016-07-03 18:32:05 +0200 | [diff] [blame] | 4741 | if (ret) |
| 4742 | return ERR_PTR(ret); |
| 4743 | |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 4744 | ret = gpiod_configure_flags(desc, propname, lflags, dflags); |
| 4745 | if (ret < 0) { |
| 4746 | gpiod_put(desc); |
| 4747 | return ERR_PTR(ret); |
Laurent Pinchart | 90b665f | 2015-10-13 00:20:21 +0300 | [diff] [blame] | 4748 | } |
| 4749 | |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 4750 | return desc; |
| 4751 | } |
| 4752 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); |
| 4753 | |
| 4754 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4755 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO |
| 4756 | * function |
| 4757 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 4758 | * @con_id: function within the GPIO consumer |
| 4759 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4760 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4761 | * |
| 4762 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the |
| 4763 | * specified index was assigned to the requested function it will return NULL. |
| 4764 | * This is convenient for drivers that need to handle optional GPIOs. |
| 4765 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4766 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4767 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4768 | unsigned int index, |
| 4769 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4770 | { |
| 4771 | struct gpio_desc *desc; |
| 4772 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 4773 | desc = gpiod_get_index(dev, con_id, index, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4774 | if (IS_ERR(desc)) { |
| 4775 | if (PTR_ERR(desc) == -ENOENT) |
| 4776 | return NULL; |
| 4777 | } |
| 4778 | |
| 4779 | return desc; |
| 4780 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 4781 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 4782 | |
| 4783 | /** |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4784 | * gpiod_hog - Hog the specified GPIO desc given the provided flags |
| 4785 | * @desc: gpio whose value will be assigned |
| 4786 | * @name: gpio line name |
Andy Shevchenko | fed7026 | 2019-04-10 18:39:16 +0300 | [diff] [blame] | 4787 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
| 4788 | * of_find_gpio() or of_get_gpio_hog() |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4789 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 4790 | */ |
| 4791 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
| 4792 | unsigned long lflags, enum gpiod_flags dflags) |
| 4793 | { |
| 4794 | struct gpio_chip *chip; |
| 4795 | struct gpio_desc *local_desc; |
| 4796 | int hwnum; |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4797 | int ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4798 | |
| 4799 | chip = gpiod_to_chip(desc); |
| 4800 | hwnum = gpio_chip_hwgpio(desc); |
| 4801 | |
Linus Walleij | 5923ea6 | 2019-04-26 14:40:18 +0200 | [diff] [blame] | 4802 | local_desc = gpiochip_request_own_desc(chip, hwnum, name, |
| 4803 | lflags, dflags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4804 | if (IS_ERR(local_desc)) { |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4805 | ret = PTR_ERR(local_desc); |
Laxman Dewangan | c31a571 | 2016-03-11 19:13:21 +0530 | [diff] [blame] | 4806 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", |
Linus Walleij | d377f56 | 2019-07-16 11:11:45 +0200 | [diff] [blame] | 4807 | name, chip->label, hwnum, ret); |
| 4808 | return ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4809 | } |
| 4810 | |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4811 | /* Mark GPIO as hogged so it can be identified and removed later */ |
| 4812 | set_bit(FLAG_IS_HOGGED, &desc->flags); |
| 4813 | |
| 4814 | pr_info("GPIO line %d (%s) hogged as %s%s\n", |
| 4815 | desc_to_gpio(desc), name, |
| 4816 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
| 4817 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? |
| 4818 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); |
| 4819 | |
| 4820 | return 0; |
| 4821 | } |
| 4822 | |
| 4823 | /** |
| 4824 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog |
| 4825 | * @chip: gpio chip to act on |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4826 | */ |
| 4827 | static void gpiochip_free_hogs(struct gpio_chip *chip) |
| 4828 | { |
| 4829 | int id; |
| 4830 | |
| 4831 | for (id = 0; id < chip->ngpio; id++) { |
Linus Walleij | 1c3cdb1 | 2016-02-09 13:51:59 +0100 | [diff] [blame] | 4832 | if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags)) |
| 4833 | gpiochip_free_own_desc(&chip->gpiodev->descs[id]); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | /** |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4838 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function |
| 4839 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 4840 | * @con_id: function within the GPIO consumer |
| 4841 | * @flags: optional GPIO initialization flags |
| 4842 | * |
| 4843 | * This function acquires all the GPIOs defined under a given function. |
| 4844 | * |
| 4845 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if |
| 4846 | * no GPIO has been assigned to the requested function, or another IS_ERR() |
| 4847 | * code if an error occurred while trying to acquire the GPIOs. |
| 4848 | */ |
| 4849 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, |
| 4850 | const char *con_id, |
| 4851 | enum gpiod_flags flags) |
| 4852 | { |
| 4853 | struct gpio_desc *desc; |
| 4854 | struct gpio_descs *descs; |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4855 | struct gpio_array *array_info = NULL; |
| 4856 | struct gpio_chip *chip; |
| 4857 | int count, bitmap_size; |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4858 | |
| 4859 | count = gpiod_count(dev, con_id); |
| 4860 | if (count < 0) |
| 4861 | return ERR_PTR(count); |
| 4862 | |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 4863 | descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL); |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4864 | if (!descs) |
| 4865 | return ERR_PTR(-ENOMEM); |
| 4866 | |
| 4867 | for (descs->ndescs = 0; descs->ndescs < count; ) { |
| 4868 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); |
| 4869 | if (IS_ERR(desc)) { |
| 4870 | gpiod_put_array(descs); |
| 4871 | return ERR_CAST(desc); |
| 4872 | } |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4873 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4874 | descs->desc[descs->ndescs] = desc; |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4875 | |
| 4876 | chip = gpiod_to_chip(desc); |
| 4877 | /* |
Janusz Krzysztofik | c4c958a | 2018-09-24 01:53:36 +0200 | [diff] [blame] | 4878 | * If pin hardware number of array member 0 is also 0, select |
| 4879 | * its chip as a candidate for fast bitmap processing path. |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4880 | */ |
Janusz Krzysztofik | c4c958a | 2018-09-24 01:53:36 +0200 | [diff] [blame] | 4881 | if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4882 | struct gpio_descs *array; |
| 4883 | |
| 4884 | bitmap_size = BITS_TO_LONGS(chip->ngpio > count ? |
| 4885 | chip->ngpio : count); |
| 4886 | |
| 4887 | array = kzalloc(struct_size(descs, desc, count) + |
| 4888 | struct_size(array_info, invert_mask, |
| 4889 | 3 * bitmap_size), GFP_KERNEL); |
| 4890 | if (!array) { |
| 4891 | gpiod_put_array(descs); |
| 4892 | return ERR_PTR(-ENOMEM); |
| 4893 | } |
| 4894 | |
| 4895 | memcpy(array, descs, |
| 4896 | struct_size(descs, desc, descs->ndescs + 1)); |
| 4897 | kfree(descs); |
| 4898 | |
| 4899 | descs = array; |
| 4900 | array_info = (void *)(descs->desc + count); |
| 4901 | array_info->get_mask = array_info->invert_mask + |
| 4902 | bitmap_size; |
| 4903 | array_info->set_mask = array_info->get_mask + |
| 4904 | bitmap_size; |
| 4905 | |
| 4906 | array_info->desc = descs->desc; |
| 4907 | array_info->size = count; |
| 4908 | array_info->chip = chip; |
| 4909 | bitmap_set(array_info->get_mask, descs->ndescs, |
| 4910 | count - descs->ndescs); |
| 4911 | bitmap_set(array_info->set_mask, descs->ndescs, |
| 4912 | count - descs->ndescs); |
| 4913 | descs->info = array_info; |
| 4914 | } |
Janusz Krzysztofik | c4c958a | 2018-09-24 01:53:36 +0200 | [diff] [blame] | 4915 | /* Unmark array members which don't belong to the 'fast' chip */ |
| 4916 | if (array_info && array_info->chip != chip) { |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4917 | __clear_bit(descs->ndescs, array_info->get_mask); |
| 4918 | __clear_bit(descs->ndescs, array_info->set_mask); |
Janusz Krzysztofik | c4c958a | 2018-09-24 01:53:36 +0200 | [diff] [blame] | 4919 | } |
| 4920 | /* |
| 4921 | * Detect array members which belong to the 'fast' chip |
| 4922 | * but their pins are not in hardware order. |
| 4923 | */ |
| 4924 | else if (array_info && |
| 4925 | gpio_chip_hwgpio(desc) != descs->ndescs) { |
| 4926 | /* |
| 4927 | * Don't use fast path if all array members processed so |
| 4928 | * far belong to the same chip as this one but its pin |
| 4929 | * hardware number is different from its array index. |
| 4930 | */ |
| 4931 | if (bitmap_full(array_info->get_mask, descs->ndescs)) { |
| 4932 | array_info = NULL; |
| 4933 | } else { |
| 4934 | __clear_bit(descs->ndescs, |
| 4935 | array_info->get_mask); |
| 4936 | __clear_bit(descs->ndescs, |
| 4937 | array_info->set_mask); |
| 4938 | } |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4939 | } else if (array_info) { |
| 4940 | /* Exclude open drain or open source from fast output */ |
| 4941 | if (gpiochip_line_is_open_drain(chip, descs->ndescs) || |
| 4942 | gpiochip_line_is_open_source(chip, descs->ndescs)) |
| 4943 | __clear_bit(descs->ndescs, |
| 4944 | array_info->set_mask); |
| 4945 | /* Identify 'fast' pins which require invertion */ |
| 4946 | if (gpiod_is_active_low(desc)) |
| 4947 | __set_bit(descs->ndescs, |
| 4948 | array_info->invert_mask); |
| 4949 | } |
| 4950 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4951 | descs->ndescs++; |
| 4952 | } |
Janusz Krzysztofik | bf9346f | 2018-09-05 23:50:06 +0200 | [diff] [blame] | 4953 | if (array_info) |
| 4954 | dev_dbg(dev, |
| 4955 | "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", |
| 4956 | array_info->chip->label, array_info->size, |
| 4957 | *array_info->get_mask, *array_info->set_mask, |
| 4958 | *array_info->invert_mask); |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 4959 | return descs; |
| 4960 | } |
| 4961 | EXPORT_SYMBOL_GPL(gpiod_get_array); |
| 4962 | |
| 4963 | /** |
| 4964 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO |
| 4965 | * function |
| 4966 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 4967 | * @con_id: function within the GPIO consumer |
| 4968 | * @flags: optional GPIO initialization flags |
| 4969 | * |
| 4970 | * This is equivalent to gpiod_get_array(), except that when no GPIO was |
| 4971 | * assigned to the requested function it will return NULL. |
| 4972 | */ |
| 4973 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, |
| 4974 | const char *con_id, |
| 4975 | enum gpiod_flags flags) |
| 4976 | { |
| 4977 | struct gpio_descs *descs; |
| 4978 | |
| 4979 | descs = gpiod_get_array(dev, con_id, flags); |
| 4980 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
| 4981 | return NULL; |
| 4982 | |
| 4983 | return descs; |
| 4984 | } |
| 4985 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); |
| 4986 | |
| 4987 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4988 | * gpiod_put - dispose of a GPIO descriptor |
| 4989 | * @desc: GPIO descriptor to dispose of |
| 4990 | * |
| 4991 | * No descriptor can be used after gpiod_put() has been called on it. |
| 4992 | */ |
| 4993 | void gpiod_put(struct gpio_desc *desc) |
| 4994 | { |
Andy Shevchenko | 1d7765b | 2019-03-26 17:21:14 +0200 | [diff] [blame] | 4995 | if (desc) |
| 4996 | gpiod_free(desc); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 4997 | } |
| 4998 | EXPORT_SYMBOL_GPL(gpiod_put); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4999 | |
Rojhalat Ibrahim | 6685852 | 2015-02-11 17:27:58 +0100 | [diff] [blame] | 5000 | /** |
| 5001 | * gpiod_put_array - dispose of multiple GPIO descriptors |
| 5002 | * @descs: struct gpio_descs containing an array of descriptors |
| 5003 | */ |
| 5004 | void gpiod_put_array(struct gpio_descs *descs) |
| 5005 | { |
| 5006 | unsigned int i; |
| 5007 | |
| 5008 | for (i = 0; i < descs->ndescs; i++) |
| 5009 | gpiod_put(descs->desc[i]); |
| 5010 | |
| 5011 | kfree(descs); |
| 5012 | } |
| 5013 | EXPORT_SYMBOL_GPL(gpiod_put_array); |
| 5014 | |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 5015 | static int __init gpiolib_dev_init(void) |
| 5016 | { |
| 5017 | int ret; |
| 5018 | |
| 5019 | /* Register GPIO sysfs bus */ |
Andy Shevchenko | b191171 | 2018-07-03 03:39:03 +0300 | [diff] [blame] | 5020 | ret = bus_register(&gpio_bus_type); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 5021 | if (ret < 0) { |
| 5022 | pr_err("gpiolib: could not register GPIO bus type\n"); |
| 5023 | return ret; |
| 5024 | } |
| 5025 | |
| 5026 | ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip"); |
| 5027 | if (ret < 0) { |
| 5028 | pr_err("gpiolib: failed to allocate char dev region\n"); |
| 5029 | bus_unregister(&gpio_bus_type); |
Guenter Roeck | 159f3cd | 2016-03-31 08:11:30 -0700 | [diff] [blame] | 5030 | } else { |
| 5031 | gpiolib_initialized = true; |
| 5032 | gpiochip_setup_devs(); |
Linus Walleij | 3c702e9 | 2015-10-21 15:29:53 +0200 | [diff] [blame] | 5033 | } |
| 5034 | return ret; |
| 5035 | } |
| 5036 | core_initcall(gpiolib_dev_init); |
| 5037 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5038 | #ifdef CONFIG_DEBUG_FS |
| 5039 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 5040 | 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] | 5041 | { |
| 5042 | unsigned i; |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 5043 | struct gpio_chip *chip = gdev->chip; |
| 5044 | unsigned gpio = gdev->base; |
| 5045 | struct gpio_desc *gdesc = &gdev->descs[0]; |
Linus Walleij | 90fd227 | 2018-10-01 23:06:17 +0200 | [diff] [blame] | 5046 | bool is_out; |
| 5047 | bool is_irq; |
| 5048 | bool active_low; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5049 | |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 5050 | for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame] | 5051 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
| 5052 | if (gdesc->name) { |
| 5053 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", |
| 5054 | gpio, gdesc->name); |
| 5055 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5056 | continue; |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame] | 5057 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5058 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 5059 | gpiod_get_direction(gdesc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5060 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 5061 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
Linus Walleij | 90fd227 | 2018-10-01 23:06:17 +0200 | [diff] [blame] | 5062 | active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags); |
| 5063 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s", |
Markus Pargmann | ced433e | 2015-08-14 16:11:02 +0200 | [diff] [blame] | 5064 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5065 | is_out ? "out" : "in ", |
Andy Shevchenko | 1c22a25 | 2018-07-12 20:36:42 +0300 | [diff] [blame] | 5066 | chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ", |
Linus Walleij | 90fd227 | 2018-10-01 23:06:17 +0200 | [diff] [blame] | 5067 | is_irq ? "IRQ " : "", |
| 5068 | active_low ? "ACTIVE LOW" : ""); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5069 | seq_printf(s, "\n"); |
| 5070 | } |
| 5071 | } |
| 5072 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5073 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5074 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5075 | unsigned long flags; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5076 | struct gpio_device *gdev = NULL; |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 5077 | loff_t index = *pos; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5078 | |
| 5079 | s->private = ""; |
| 5080 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5081 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5082 | list_for_each_entry(gdev, &gpio_devices, list) |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5083 | if (index-- == 0) { |
| 5084 | spin_unlock_irqrestore(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5085 | return gdev; |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5086 | } |
| 5087 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 5088 | |
| 5089 | return NULL; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 5093 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5094 | unsigned long flags; |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5095 | struct gpio_device *gdev = v; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5096 | void *ret = NULL; |
| 5097 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5098 | spin_lock_irqsave(&gpio_lock, flags); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5099 | if (list_is_last(&gdev->list, &gpio_devices)) |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 5100 | ret = NULL; |
| 5101 | else |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5102 | ret = list_entry(gdev->list.next, struct gpio_device, list); |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 5103 | spin_unlock_irqrestore(&gpio_lock, flags); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5104 | |
| 5105 | s->private = "\n"; |
| 5106 | ++*pos; |
| 5107 | |
| 5108 | return ret; |
| 5109 | } |
| 5110 | |
| 5111 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 5112 | { |
| 5113 | } |
| 5114 | |
| 5115 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 5116 | { |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5117 | struct gpio_device *gdev = v; |
| 5118 | struct gpio_chip *chip = gdev->chip; |
| 5119 | struct device *parent; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5120 | |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5121 | if (!chip) { |
| 5122 | seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, |
| 5123 | dev_name(&gdev->dev)); |
| 5124 | return 0; |
| 5125 | } |
| 5126 | |
| 5127 | seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, |
| 5128 | dev_name(&gdev->dev), |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 5129 | gdev->base, gdev->base + gdev->ngpio - 1); |
Linus Walleij | ff2b135 | 2015-10-20 11:10:38 +0200 | [diff] [blame] | 5130 | parent = chip->parent; |
| 5131 | if (parent) |
| 5132 | seq_printf(s, ", parent: %s/%s", |
| 5133 | parent->bus ? parent->bus->name : "no-bus", |
| 5134 | dev_name(parent)); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5135 | if (chip->label) |
| 5136 | seq_printf(s, ", %s", chip->label); |
| 5137 | if (chip->can_sleep) |
| 5138 | seq_printf(s, ", can sleep"); |
| 5139 | seq_printf(s, ":\n"); |
| 5140 | |
| 5141 | if (chip->dbg_show) |
| 5142 | chip->dbg_show(s, chip); |
| 5143 | else |
Linus Walleij | fdeb8e1 | 2016-02-10 10:57:36 +0100 | [diff] [blame] | 5144 | gpiolib_dbg_show(s, gdev); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5145 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5146 | return 0; |
| 5147 | } |
| 5148 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5149 | static const struct seq_operations gpiolib_seq_ops = { |
| 5150 | .start = gpiolib_seq_start, |
| 5151 | .next = gpiolib_seq_next, |
| 5152 | .stop = gpiolib_seq_stop, |
| 5153 | .show = gpiolib_seq_show, |
| 5154 | }; |
| 5155 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5156 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 5157 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5158 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5159 | } |
| 5160 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5161 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5162 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5163 | .open = gpiolib_open, |
| 5164 | .read = seq_read, |
| 5165 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 5166 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5167 | }; |
| 5168 | |
| 5169 | static int __init gpiolib_debugfs_init(void) |
| 5170 | { |
| 5171 | /* /sys/kernel/debug/gpio */ |
Greg Kroah-Hartman | acc68b0 | 2019-06-18 17:50:45 +0200 | [diff] [blame] | 5172 | debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL, |
| 5173 | &gpiolib_operations); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 5174 | return 0; |
| 5175 | } |
| 5176 | subsys_initcall(gpiolib_debugfs_init); |
| 5177 | |
| 5178 | #endif /* DEBUG_FS */ |