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