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