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