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