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