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