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