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