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