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