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