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