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