David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 3 | #include <linux/interrupt.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 4 | #include <linux/irq.h> |
| 5 | #include <linux/spinlock.h> |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 6 | #include <linux/list.h> |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/gpio.h> |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 12 | #include <linux/of_gpio.h> |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 13 | #include <linux/acpi_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> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 18 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 19 | #define CREATE_TRACE_POINTS |
| 20 | #include <trace/events/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 21 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 22 | /* Implementation infrastructure for GPIO interfaces. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 23 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 24 | * The GPIO programming interface allows for inlining speed-critical |
| 25 | * get/set operations for common cases, so that access to SOC-integrated |
| 26 | * GPIOs can sometimes cost only an instruction or two per bit. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | |
| 30 | /* When debugging, extend minimal trust to callers and platform code. |
| 31 | * Also emit diagnostic messages that may help initial bringup, when |
| 32 | * board setup or driver bugs are most common. |
| 33 | * |
| 34 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 35 | */ |
| 36 | #ifdef DEBUG |
| 37 | #define extra_checks 1 |
| 38 | #else |
| 39 | #define extra_checks 0 |
| 40 | #endif |
| 41 | |
| 42 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 43 | * While any GPIO is requested, its gpio_chip is not removable; |
| 44 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 45 | */ |
| 46 | static DEFINE_SPINLOCK(gpio_lock); |
| 47 | |
| 48 | struct gpio_desc { |
| 49 | struct gpio_chip *chip; |
| 50 | unsigned long flags; |
| 51 | /* flag symbols are bit numbers */ |
| 52 | #define FLAG_REQUESTED 0 |
| 53 | #define FLAG_IS_OUT 1 |
Alexandre Courbot | 710b40e | 2013-02-02 23:44:06 +0900 | [diff] [blame] | 54 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ |
| 55 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ |
| 56 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ |
| 57 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 58 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
Alexandre Courbot | 710b40e | 2013-02-02 23:44:06 +0900 | [diff] [blame] | 59 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
| 60 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 61 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 62 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 63 | #define ID_SHIFT 16 /* add new flags before this one */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 64 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 65 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 66 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 67 | |
| 68 | #ifdef CONFIG_DEBUG_FS |
| 69 | const char *label; |
| 70 | #endif |
| 71 | }; |
| 72 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
| 73 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 74 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
| 75 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 76 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 77 | static LIST_HEAD(gpio_lookup_list); |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 78 | static LIST_HEAD(gpio_chips); |
| 79 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_GPIO_SYSFS |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 81 | static DEFINE_IDR(dirent_idr); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 82 | #endif |
| 83 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 84 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
| 85 | static void gpiod_free(struct gpio_desc *desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 86 | |
Mark Brown | 7b17b59 | 2013-09-09 10:33:50 +0100 | [diff] [blame] | 87 | #ifdef CONFIG_DEBUG_FS |
| 88 | #define gpiod_emerg(desc, fmt, ...) \ |
| 89 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 90 | ##__VA_ARGS__) |
| 91 | #define gpiod_crit(desc, fmt, ...) \ |
| 92 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 93 | ##__VA_ARGS__) |
| 94 | #define gpiod_err(desc, fmt, ...) \ |
| 95 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 96 | ##__VA_ARGS__) |
| 97 | #define gpiod_warn(desc, fmt, ...) \ |
| 98 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 99 | ##__VA_ARGS__) |
| 100 | #define gpiod_info(desc, fmt, ...) \ |
| 101 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 102 | ##__VA_ARGS__) |
| 103 | #define gpiod_dbg(desc, fmt, ...) \ |
| 104 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ |
| 105 | ##__VA_ARGS__) |
| 106 | #else |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 107 | #define gpiod_emerg(desc, fmt, ...) \ |
| 108 | pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 109 | #define gpiod_crit(desc, fmt, ...) \ |
| 110 | pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 111 | #define gpiod_err(desc, fmt, ...) \ |
| 112 | pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 113 | #define gpiod_warn(desc, fmt, ...) \ |
| 114 | pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 115 | #define gpiod_info(desc, fmt, ...) \ |
| 116 | pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
| 117 | #define gpiod_dbg(desc, fmt, ...) \ |
| 118 | pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
Mark Brown | 7b17b59 | 2013-09-09 10:33:50 +0100 | [diff] [blame] | 119 | #endif |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 120 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 121 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 122 | { |
| 123 | #ifdef CONFIG_DEBUG_FS |
| 124 | d->label = label; |
| 125 | #endif |
| 126 | } |
| 127 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 128 | /* |
| 129 | * Return the GPIO number of the passed descriptor relative to its chip |
| 130 | */ |
| 131 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) |
| 132 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 133 | return desc - &desc->chip->desc[0]; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Convert a GPIO number to its descriptor |
| 138 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 139 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 140 | { |
| 141 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) |
| 142 | return NULL; |
| 143 | else |
| 144 | return &gpio_desc[gpio]; |
| 145 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 147 | |
| 148 | /** |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 149 | * Convert an offset on a certain chip to a corresponding descriptor |
| 150 | */ |
| 151 | static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, |
| 152 | unsigned int offset) |
| 153 | { |
| 154 | unsigned int gpio = chip->base + offset; |
| 155 | |
| 156 | return gpio_to_desc(gpio); |
| 157 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Convert a GPIO descriptor to the integer namespace. |
| 161 | * This should disappear in the future but is needed since we still |
| 162 | * use GPIO numbers for error messages and sysfs nodes |
| 163 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 164 | int desc_to_gpio(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 165 | { |
Alexandre Courbot | 8c0fca8 | 2013-10-04 10:59:57 -0700 | [diff] [blame] | 166 | return desc - &gpio_desc[0]; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 167 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 169 | |
| 170 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 171 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
| 172 | * when setting direction, and otherwise illegal. Until board setup code |
| 173 | * and drivers use explicit requests everywhere (which won't happen when |
| 174 | * those calls have no teeth) we can't avoid autorequesting. This nag |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 175 | * message should motivate switching to explicit requests... so should |
| 176 | * the weaker cleanup after faults, compared to gpio_request(). |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 177 | * |
| 178 | * NOTE: the autorequest mechanism is going away; at this point it's |
| 179 | * only "legal" in the sense that (old) code using it won't break yet, |
| 180 | * but instead only triggers a WARN() stack dump. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 181 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 182 | static int gpio_ensure_requested(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 183 | { |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 184 | const struct gpio_chip *chip = desc->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 185 | const int gpio = desc_to_gpio(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 186 | |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 187 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
| 188 | "autorequest GPIO-%d\n", gpio)) { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 189 | if (!try_module_get(chip->owner)) { |
| 190 | pr_err("GPIO-%d: module can't be gotten \n", gpio); |
| 191 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 192 | /* lose */ |
| 193 | return -EIO; |
| 194 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 195 | desc_set_label(desc, "[auto]"); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 196 | /* caller must chip->request() w/o spinlock */ |
| 197 | if (chip->request) |
| 198 | return 1; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 199 | } |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 200 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 203 | /** |
| 204 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 205 | * @desc: descriptor to return the chip of |
| 206 | */ |
| 207 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 208 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 209 | return desc ? desc->chip : NULL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 210 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 211 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 212 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 213 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 214 | static int gpiochip_find_base(int ngpio) |
| 215 | { |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 216 | struct gpio_chip *chip; |
| 217 | int base = ARCH_NR_GPIOS - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 218 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 219 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
| 220 | /* found a free space? */ |
| 221 | if (chip->base + chip->ngpio <= base) |
| 222 | break; |
| 223 | else |
| 224 | /* nope, check the space right before the chip */ |
| 225 | base = chip->base - ngpio; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 228 | if (gpio_is_valid(base)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 229 | pr_debug("%s: found new base at %d\n", __func__, base); |
Alexandre Courbot | 83cabe3 | 2013-02-03 01:29:28 +0900 | [diff] [blame] | 230 | return base; |
| 231 | } else { |
| 232 | pr_err("%s: cannot find free range\n", __func__); |
| 233 | return -ENOSPC; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 234 | } |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 237 | /** |
| 238 | * gpiod_get_direction - return the current direction of a GPIO |
| 239 | * @desc: GPIO to get the direction of |
| 240 | * |
| 241 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. |
| 242 | * |
| 243 | * This function may sleep if gpiod_cansleep() is true. |
| 244 | */ |
| 245 | int gpiod_get_direction(const struct gpio_desc *desc) |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 246 | { |
| 247 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 248 | unsigned offset; |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 249 | int status = -EINVAL; |
| 250 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 251 | chip = gpiod_to_chip(desc); |
| 252 | offset = gpio_chip_hwgpio(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 253 | |
| 254 | if (!chip->get_direction) |
| 255 | return status; |
| 256 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 257 | status = chip->get_direction(chip, offset); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 258 | if (status > 0) { |
| 259 | /* GPIOF_DIR_IN, or other positive */ |
| 260 | status = 1; |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 261 | /* FLAG_IS_OUT is just a cache of the result of get_direction(), |
| 262 | * so it does not affect constness per se */ |
| 263 | clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 264 | } |
| 265 | if (status == 0) { |
| 266 | /* GPIOF_DIR_OUT */ |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 267 | set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 268 | } |
| 269 | return status; |
| 270 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 271 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 272 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 273 | #ifdef CONFIG_GPIO_SYSFS |
| 274 | |
| 275 | /* lock protects against unexport_gpio() being called while |
| 276 | * sysfs files are active. |
| 277 | */ |
| 278 | static DEFINE_MUTEX(sysfs_lock); |
| 279 | |
| 280 | /* |
| 281 | * /sys/class/gpio/gpioN... only for GPIOs that are exported |
| 282 | * /direction |
| 283 | * * MAY BE OMITTED if kernel won't allow direction changes |
| 284 | * * is read/write as "in" or "out" |
| 285 | * * may also be written as "high" or "low", initializing |
| 286 | * output value as specified ("out" implies "low") |
| 287 | * /value |
| 288 | * * always readable, subject to hardware behavior |
| 289 | * * may be writable, as zero/nonzero |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 290 | * /edge |
| 291 | * * configures behavior of poll(2) on /value |
| 292 | * * available only if pin can generate IRQs on input |
| 293 | * * is read/write as "none", "falling", "rising", or "both" |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 294 | * /active_low |
| 295 | * * configures polarity of /value |
| 296 | * * is read/write as zero/nonzero |
| 297 | * * also affects existing and subsequent "falling" and "rising" |
| 298 | * /edge configuration |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 299 | */ |
| 300 | |
| 301 | static ssize_t gpio_direction_show(struct device *dev, |
| 302 | struct device_attribute *attr, char *buf) |
| 303 | { |
Alexandre Courbot | def6343 | 2013-02-15 14:46:15 +0900 | [diff] [blame] | 304 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 305 | ssize_t status; |
| 306 | |
| 307 | mutex_lock(&sysfs_lock); |
| 308 | |
Alexandre Courbot | 476171ce | 2013-02-04 17:42:04 +0900 | [diff] [blame] | 309 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 310 | status = -EIO; |
Alexandre Courbot | 476171ce | 2013-02-04 17:42:04 +0900 | [diff] [blame] | 311 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 312 | gpiod_get_direction(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 313 | status = sprintf(buf, "%s\n", |
| 314 | test_bit(FLAG_IS_OUT, &desc->flags) |
| 315 | ? "out" : "in"); |
Alexandre Courbot | 476171ce | 2013-02-04 17:42:04 +0900 | [diff] [blame] | 316 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 317 | |
| 318 | mutex_unlock(&sysfs_lock); |
| 319 | return status; |
| 320 | } |
| 321 | |
| 322 | static ssize_t gpio_direction_store(struct device *dev, |
| 323 | struct device_attribute *attr, const char *buf, size_t size) |
| 324 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 325 | struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 326 | ssize_t status; |
| 327 | |
| 328 | mutex_lock(&sysfs_lock); |
| 329 | |
| 330 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 331 | status = -EIO; |
| 332 | else if (sysfs_streq(buf, "high")) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 333 | status = gpiod_direction_output(desc, 1); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 334 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 335 | status = gpiod_direction_output(desc, 0); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 336 | else if (sysfs_streq(buf, "in")) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 337 | status = gpiod_direction_input(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 338 | else |
| 339 | status = -EINVAL; |
| 340 | |
| 341 | mutex_unlock(&sysfs_lock); |
| 342 | return status ? : size; |
| 343 | } |
| 344 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 345 | static /* const */ DEVICE_ATTR(direction, 0644, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 346 | gpio_direction_show, gpio_direction_store); |
| 347 | |
| 348 | static ssize_t gpio_value_show(struct device *dev, |
| 349 | struct device_attribute *attr, char *buf) |
| 350 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 351 | struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 352 | ssize_t status; |
| 353 | |
| 354 | mutex_lock(&sysfs_lock); |
| 355 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 356 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 357 | status = -EIO; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 358 | else |
| 359 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 360 | |
| 361 | mutex_unlock(&sysfs_lock); |
| 362 | return status; |
| 363 | } |
| 364 | |
| 365 | static ssize_t gpio_value_store(struct device *dev, |
| 366 | struct device_attribute *attr, const char *buf, size_t size) |
| 367 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 368 | struct gpio_desc *desc = dev_get_drvdata(dev); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 369 | ssize_t status; |
| 370 | |
| 371 | mutex_lock(&sysfs_lock); |
| 372 | |
| 373 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 374 | status = -EIO; |
| 375 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) |
| 376 | status = -EPERM; |
| 377 | else { |
| 378 | long value; |
| 379 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 380 | status = kstrtol(buf, 0, &value); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 381 | if (status == 0) { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 382 | gpiod_set_value_cansleep(desc, value); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 383 | status = size; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | mutex_unlock(&sysfs_lock); |
| 388 | return status; |
| 389 | } |
| 390 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 391 | static const DEVICE_ATTR(value, 0644, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 392 | gpio_value_show, gpio_value_store); |
| 393 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 394 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
| 395 | { |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 396 | struct sysfs_dirent *value_sd = priv; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 397 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 398 | sysfs_notify_dirent(value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 399 | return IRQ_HANDLED; |
| 400 | } |
| 401 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 402 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
| 403 | unsigned long gpio_flags) |
| 404 | { |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 405 | struct sysfs_dirent *value_sd; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 406 | unsigned long irq_flags; |
| 407 | int ret, irq, id; |
| 408 | |
| 409 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) |
| 410 | return 0; |
| 411 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 412 | irq = gpiod_to_irq(desc); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 413 | if (irq < 0) |
| 414 | return -EIO; |
| 415 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 416 | id = desc->flags >> ID_SHIFT; |
| 417 | value_sd = idr_find(&dirent_idr, id); |
| 418 | if (value_sd) |
| 419 | free_irq(irq, value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 420 | |
| 421 | desc->flags &= ~GPIO_TRIGGER_MASK; |
| 422 | |
| 423 | if (!gpio_flags) { |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 424 | gpiod_unlock_as_irq(desc); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 425 | ret = 0; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 426 | goto free_id; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | irq_flags = IRQF_SHARED; |
| 430 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 431 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 432 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 433 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 434 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 435 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 436 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 437 | if (!value_sd) { |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 438 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 439 | if (!value_sd) { |
| 440 | ret = -ENODEV; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 441 | goto err_out; |
| 442 | } |
| 443 | |
Tejun Heo | 62f516b | 2013-02-27 17:04:06 -0800 | [diff] [blame] | 444 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); |
| 445 | if (ret < 0) |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 446 | goto free_sd; |
Tejun Heo | 62f516b | 2013-02-27 17:04:06 -0800 | [diff] [blame] | 447 | id = ret; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 448 | |
| 449 | desc->flags &= GPIO_FLAGS_MASK; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 450 | desc->flags |= (unsigned long)id << ID_SHIFT; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 451 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 452 | if (desc->flags >> ID_SHIFT != id) { |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 453 | ret = -ERANGE; |
| 454 | goto free_id; |
| 455 | } |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Daniel Gl?ckner | 364fadb3 | 2010-08-10 18:02:26 -0700 | [diff] [blame] | 458 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 459 | "gpiolib", value_sd); |
Daniel Gl?ckner | 364fadb3 | 2010-08-10 18:02:26 -0700 | [diff] [blame] | 460 | if (ret < 0) |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 461 | goto free_id; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 462 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 463 | ret = gpiod_lock_as_irq(desc); |
| 464 | if (ret < 0) { |
| 465 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); |
| 466 | goto free_id; |
| 467 | } |
| 468 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 469 | desc->flags |= gpio_flags; |
| 470 | return 0; |
| 471 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 472 | free_id: |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 473 | idr_remove(&dirent_idr, id); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 474 | desc->flags &= GPIO_FLAGS_MASK; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 475 | free_sd: |
| 476 | if (value_sd) |
| 477 | sysfs_put(value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 478 | err_out: |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static const struct { |
| 483 | const char *name; |
| 484 | unsigned long flags; |
| 485 | } trigger_types[] = { |
| 486 | { "none", 0 }, |
| 487 | { "falling", BIT(FLAG_TRIG_FALL) }, |
| 488 | { "rising", BIT(FLAG_TRIG_RISE) }, |
| 489 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, |
| 490 | }; |
| 491 | |
| 492 | static ssize_t gpio_edge_show(struct device *dev, |
| 493 | struct device_attribute *attr, char *buf) |
| 494 | { |
| 495 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 496 | ssize_t status; |
| 497 | |
| 498 | mutex_lock(&sysfs_lock); |
| 499 | |
| 500 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 501 | status = -EIO; |
| 502 | else { |
| 503 | int i; |
| 504 | |
| 505 | status = 0; |
| 506 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 507 | if ((desc->flags & GPIO_TRIGGER_MASK) |
| 508 | == trigger_types[i].flags) { |
| 509 | status = sprintf(buf, "%s\n", |
| 510 | trigger_types[i].name); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | mutex_unlock(&sysfs_lock); |
| 516 | return status; |
| 517 | } |
| 518 | |
| 519 | static ssize_t gpio_edge_store(struct device *dev, |
| 520 | struct device_attribute *attr, const char *buf, size_t size) |
| 521 | { |
| 522 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 523 | ssize_t status; |
| 524 | int i; |
| 525 | |
| 526 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 527 | if (sysfs_streq(trigger_types[i].name, buf)) |
| 528 | goto found; |
| 529 | return -EINVAL; |
| 530 | |
| 531 | found: |
| 532 | mutex_lock(&sysfs_lock); |
| 533 | |
| 534 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 535 | status = -EIO; |
| 536 | else { |
| 537 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); |
| 538 | if (!status) |
| 539 | status = size; |
| 540 | } |
| 541 | |
| 542 | mutex_unlock(&sysfs_lock); |
| 543 | |
| 544 | return status; |
| 545 | } |
| 546 | |
| 547 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); |
| 548 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 549 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
| 550 | int value) |
| 551 | { |
| 552 | int status = 0; |
| 553 | |
| 554 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) |
| 555 | return 0; |
| 556 | |
| 557 | if (value) |
| 558 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 559 | else |
| 560 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 561 | |
| 562 | /* reconfigure poll(2) support if enabled on one edge only */ |
| 563 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ |
| 564 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { |
| 565 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; |
| 566 | |
| 567 | gpio_setup_irq(desc, dev, 0); |
| 568 | status = gpio_setup_irq(desc, dev, trigger_flags); |
| 569 | } |
| 570 | |
| 571 | return status; |
| 572 | } |
| 573 | |
| 574 | static ssize_t gpio_active_low_show(struct device *dev, |
| 575 | struct device_attribute *attr, char *buf) |
| 576 | { |
| 577 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 578 | ssize_t status; |
| 579 | |
| 580 | mutex_lock(&sysfs_lock); |
| 581 | |
| 582 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 583 | status = -EIO; |
| 584 | else |
| 585 | status = sprintf(buf, "%d\n", |
| 586 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); |
| 587 | |
| 588 | mutex_unlock(&sysfs_lock); |
| 589 | |
| 590 | return status; |
| 591 | } |
| 592 | |
| 593 | static ssize_t gpio_active_low_store(struct device *dev, |
| 594 | struct device_attribute *attr, const char *buf, size_t size) |
| 595 | { |
| 596 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 597 | ssize_t status; |
| 598 | |
| 599 | mutex_lock(&sysfs_lock); |
| 600 | |
| 601 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
| 602 | status = -EIO; |
| 603 | } else { |
| 604 | long value; |
| 605 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 606 | status = kstrtol(buf, 0, &value); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 607 | if (status == 0) |
| 608 | status = sysfs_set_active_low(desc, dev, value != 0); |
| 609 | } |
| 610 | |
| 611 | mutex_unlock(&sysfs_lock); |
| 612 | |
| 613 | return status ? : size; |
| 614 | } |
| 615 | |
| 616 | static const DEVICE_ATTR(active_low, 0644, |
| 617 | gpio_active_low_show, gpio_active_low_store); |
| 618 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 619 | static const struct attribute *gpio_attrs[] = { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 620 | &dev_attr_value.attr, |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 621 | &dev_attr_active_low.attr, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 622 | NULL, |
| 623 | }; |
| 624 | |
| 625 | static const struct attribute_group gpio_attr_group = { |
| 626 | .attrs = (struct attribute **) gpio_attrs, |
| 627 | }; |
| 628 | |
| 629 | /* |
| 630 | * /sys/class/gpio/gpiochipN/ |
| 631 | * /base ... matching gpio_chip.base (N) |
| 632 | * /label ... matching gpio_chip.label |
| 633 | * /ngpio ... matching gpio_chip.ngpio |
| 634 | */ |
| 635 | |
| 636 | static ssize_t chip_base_show(struct device *dev, |
| 637 | struct device_attribute *attr, char *buf) |
| 638 | { |
| 639 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 640 | |
| 641 | return sprintf(buf, "%d\n", chip->base); |
| 642 | } |
| 643 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); |
| 644 | |
| 645 | static ssize_t chip_label_show(struct device *dev, |
| 646 | struct device_attribute *attr, char *buf) |
| 647 | { |
| 648 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 649 | |
| 650 | return sprintf(buf, "%s\n", chip->label ? : ""); |
| 651 | } |
| 652 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); |
| 653 | |
| 654 | static ssize_t chip_ngpio_show(struct device *dev, |
| 655 | struct device_attribute *attr, char *buf) |
| 656 | { |
| 657 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 658 | |
| 659 | return sprintf(buf, "%u\n", chip->ngpio); |
| 660 | } |
| 661 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
| 662 | |
| 663 | static const struct attribute *gpiochip_attrs[] = { |
| 664 | &dev_attr_base.attr, |
| 665 | &dev_attr_label.attr, |
| 666 | &dev_attr_ngpio.attr, |
| 667 | NULL, |
| 668 | }; |
| 669 | |
| 670 | static const struct attribute_group gpiochip_attr_group = { |
| 671 | .attrs = (struct attribute **) gpiochip_attrs, |
| 672 | }; |
| 673 | |
| 674 | /* |
| 675 | * /sys/class/gpio/export ... write-only |
| 676 | * integer N ... number of GPIO to export (full access) |
| 677 | * /sys/class/gpio/unexport ... write-only |
| 678 | * integer N ... number of GPIO to unexport |
| 679 | */ |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 680 | static ssize_t export_store(struct class *class, |
| 681 | struct class_attribute *attr, |
| 682 | const char *buf, size_t len) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 683 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 684 | long gpio; |
| 685 | struct gpio_desc *desc; |
| 686 | int status; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 687 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 688 | status = kstrtol(buf, 0, &gpio); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 689 | if (status < 0) |
| 690 | goto done; |
| 691 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 692 | desc = gpio_to_desc(gpio); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 693 | /* reject invalid GPIOs */ |
| 694 | if (!desc) { |
| 695 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); |
| 696 | return -EINVAL; |
| 697 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 698 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 699 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 700 | * request and export were done by on behalf of userspace, so |
| 701 | * they may be undone on its behalf too. |
| 702 | */ |
| 703 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 704 | status = gpiod_request(desc, "sysfs"); |
Mathias Nyman | ad2fab3 | 2012-10-25 14:03:03 +0300 | [diff] [blame] | 705 | if (status < 0) { |
| 706 | if (status == -EPROBE_DEFER) |
| 707 | status = -ENODEV; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 708 | goto done; |
Mathias Nyman | ad2fab3 | 2012-10-25 14:03:03 +0300 | [diff] [blame] | 709 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 710 | status = gpiod_export(desc, true); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 711 | if (status < 0) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 712 | gpiod_free(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 713 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 714 | set_bit(FLAG_SYSFS, &desc->flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 715 | |
| 716 | done: |
| 717 | if (status) |
| 718 | pr_debug("%s: status %d\n", __func__, status); |
| 719 | return status ? : len; |
| 720 | } |
| 721 | |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 722 | static ssize_t unexport_store(struct class *class, |
| 723 | struct class_attribute *attr, |
| 724 | const char *buf, size_t len) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 725 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 726 | long gpio; |
| 727 | struct gpio_desc *desc; |
| 728 | int status; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 729 | |
Jingoo Han | a3d88c9 | 2013-07-19 16:12:50 +0900 | [diff] [blame] | 730 | status = kstrtol(buf, 0, &gpio); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 731 | if (status < 0) |
| 732 | goto done; |
| 733 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 734 | desc = gpio_to_desc(gpio); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 735 | /* reject bogus commands (gpio_unexport ignores them) */ |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 736 | if (!desc) { |
| 737 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); |
| 738 | return -EINVAL; |
| 739 | } |
| 740 | |
| 741 | status = -EINVAL; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 742 | |
| 743 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 744 | * request and export were done by on behalf of userspace, so |
| 745 | * they may be undone on its behalf too. |
| 746 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 747 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 748 | status = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 749 | gpiod_free(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 750 | } |
| 751 | done: |
| 752 | if (status) |
| 753 | pr_debug("%s: status %d\n", __func__, status); |
| 754 | return status ? : len; |
| 755 | } |
| 756 | |
| 757 | static struct class_attribute gpio_class_attrs[] = { |
| 758 | __ATTR(export, 0200, NULL, export_store), |
| 759 | __ATTR(unexport, 0200, NULL, unexport_store), |
| 760 | __ATTR_NULL, |
| 761 | }; |
| 762 | |
| 763 | static struct class gpio_class = { |
| 764 | .name = "gpio", |
| 765 | .owner = THIS_MODULE, |
| 766 | |
| 767 | .class_attrs = gpio_class_attrs, |
| 768 | }; |
| 769 | |
| 770 | |
| 771 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 772 | * gpiod_export - export a GPIO through sysfs |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 773 | * @gpio: gpio to make available, already requested |
| 774 | * @direction_may_change: true if userspace may change gpio direction |
| 775 | * Context: arch_initcall or later |
| 776 | * |
| 777 | * When drivers want to make a GPIO accessible to userspace after they |
| 778 | * have requested it -- perhaps while debugging, or as part of their |
| 779 | * public interface -- they may use this routine. If the GPIO can |
| 780 | * change direction (some can't) and the caller allows it, userspace |
| 781 | * will see "direction" sysfs attribute which may be used to change |
| 782 | * the gpio's direction. A "value" attribute will always be provided. |
| 783 | * |
| 784 | * Returns zero on success, else an error. |
| 785 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 786 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 787 | { |
| 788 | unsigned long flags; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 789 | int status; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 790 | const char *ioname = NULL; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 791 | struct device *dev; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 792 | int offset; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 793 | |
| 794 | /* can't export until sysfs is available ... */ |
| 795 | if (!gpio_class.p) { |
| 796 | pr_debug("%s: called too early!\n", __func__); |
| 797 | return -ENOENT; |
| 798 | } |
| 799 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 800 | if (!desc) { |
| 801 | pr_debug("%s: invalid gpio descriptor\n", __func__); |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 802 | return -EINVAL; |
| 803 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 804 | |
| 805 | mutex_lock(&sysfs_lock); |
| 806 | |
| 807 | spin_lock_irqsave(&gpio_lock, flags); |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 808 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
| 809 | test_bit(FLAG_EXPORT, &desc->flags)) { |
| 810 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 811 | pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 812 | __func__, desc_to_gpio(desc), |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 813 | test_bit(FLAG_REQUESTED, &desc->flags), |
| 814 | test_bit(FLAG_EXPORT, &desc->flags)); |
Dan Carpenter | 529f2ad | 2012-10-26 09:59:43 +0300 | [diff] [blame] | 815 | status = -EPERM; |
| 816 | goto fail_unlock; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 817 | } |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 818 | |
| 819 | if (!desc->chip->direction_input || !desc->chip->direction_output) |
| 820 | direction_may_change = false; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 821 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 822 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 823 | offset = gpio_chip_hwgpio(desc); |
| 824 | if (desc->chip->names && desc->chip->names[offset]) |
| 825 | ioname = desc->chip->names[offset]; |
Daniel Silverstone | 926b663 | 2009-04-02 16:57:05 -0700 | [diff] [blame] | 826 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 827 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 828 | desc, ioname ? ioname : "gpio%u", |
| 829 | desc_to_gpio(desc)); |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 830 | if (IS_ERR(dev)) { |
| 831 | status = PTR_ERR(dev); |
| 832 | goto fail_unlock; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 835 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 836 | if (status) |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 837 | goto fail_unregister_device; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 838 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 839 | if (direction_may_change) { |
| 840 | status = device_create_file(dev, &dev_attr_direction); |
| 841 | if (status) |
| 842 | goto fail_unregister_device; |
| 843 | } |
| 844 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 845 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 846 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
| 847 | status = device_create_file(dev, &dev_attr_edge); |
| 848 | if (status) |
| 849 | goto fail_unregister_device; |
| 850 | } |
| 851 | |
| 852 | set_bit(FLAG_EXPORT, &desc->flags); |
| 853 | mutex_unlock(&sysfs_lock); |
| 854 | return 0; |
| 855 | |
| 856 | fail_unregister_device: |
| 857 | device_unregister(dev); |
| 858 | fail_unlock: |
| 859 | mutex_unlock(&sysfs_lock); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 860 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 861 | status); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 862 | return status; |
| 863 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 864 | EXPORT_SYMBOL_GPL(gpiod_export); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 865 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 866 | static int match_export(struct device *dev, const void *data) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 867 | { |
| 868 | return dev_get_drvdata(dev) == data; |
| 869 | } |
| 870 | |
| 871 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 872 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 873 | * @dev: device under which to create symlink |
| 874 | * @name: name of the symlink |
| 875 | * @gpio: gpio to create symlink to, already exported |
| 876 | * |
| 877 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN |
| 878 | * node. Caller is responsible for unlinking. |
| 879 | * |
| 880 | * Returns zero on success, else an error. |
| 881 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 882 | int gpiod_export_link(struct device *dev, const char *name, |
| 883 | struct gpio_desc *desc) |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 884 | { |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 885 | int status = -EINVAL; |
| 886 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 887 | if (!desc) { |
| 888 | pr_warn("%s: invalid GPIO\n", __func__); |
| 889 | return -EINVAL; |
| 890 | } |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 891 | |
| 892 | mutex_lock(&sysfs_lock); |
| 893 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 894 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 895 | struct device *tdev; |
| 896 | |
| 897 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 898 | if (tdev != NULL) { |
| 899 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, |
| 900 | name); |
| 901 | } else { |
| 902 | status = -ENODEV; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | mutex_unlock(&sysfs_lock); |
| 907 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 908 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 909 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 910 | status); |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 911 | |
| 912 | return status; |
| 913 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 914 | EXPORT_SYMBOL_GPL(gpiod_export_link); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 915 | |
| 916 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 917 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 918 | * @gpio: gpio to change |
| 919 | * @value: non-zero to use active low, i.e. inverted values |
| 920 | * |
| 921 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. |
| 922 | * The GPIO does not have to be exported yet. If poll(2) support has |
| 923 | * been enabled for either rising or falling edge, it will be |
| 924 | * reconfigured to follow the new polarity. |
| 925 | * |
| 926 | * Returns zero on success, else an error. |
| 927 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 928 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 929 | { |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 930 | struct device *dev = NULL; |
| 931 | int status = -EINVAL; |
| 932 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 933 | if (!desc) { |
| 934 | pr_warn("%s: invalid GPIO\n", __func__); |
| 935 | return -EINVAL; |
| 936 | } |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 937 | |
| 938 | mutex_lock(&sysfs_lock); |
| 939 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 940 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 941 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 942 | if (dev == NULL) { |
| 943 | status = -ENODEV; |
| 944 | goto unlock; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | status = sysfs_set_active_low(desc, dev, value); |
| 949 | |
| 950 | unlock: |
| 951 | mutex_unlock(&sysfs_lock); |
| 952 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 953 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 954 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 955 | status); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 956 | |
| 957 | return status; |
| 958 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 959 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 960 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 961 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 962 | * gpiod_unexport - reverse effect of gpio_export() |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 963 | * @gpio: gpio to make unavailable |
| 964 | * |
| 965 | * This is implicit on gpio_free(). |
| 966 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 967 | void gpiod_unexport(struct gpio_desc *desc) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 968 | { |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 969 | int status = 0; |
Ming Lei | 864533c | 2012-02-13 22:53:20 +0800 | [diff] [blame] | 970 | struct device *dev = NULL; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 971 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 972 | if (!desc) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 973 | pr_warn("%s: invalid GPIO\n", __func__); |
| 974 | return; |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 975 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 976 | |
| 977 | mutex_lock(&sysfs_lock); |
| 978 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 979 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 980 | |
| 981 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 982 | if (dev) { |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 983 | gpio_setup_irq(desc, dev, 0); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 984 | clear_bit(FLAG_EXPORT, &desc->flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 985 | } else |
| 986 | status = -ENODEV; |
| 987 | } |
| 988 | |
| 989 | mutex_unlock(&sysfs_lock); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 990 | |
Ming Lei | 864533c | 2012-02-13 22:53:20 +0800 | [diff] [blame] | 991 | if (dev) { |
| 992 | device_unregister(dev); |
| 993 | put_device(dev); |
| 994 | } |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 995 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 996 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 997 | pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), |
| 998 | status); |
| 999 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1000 | EXPORT_SYMBOL_GPL(gpiod_unexport); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1001 | |
| 1002 | static int gpiochip_export(struct gpio_chip *chip) |
| 1003 | { |
| 1004 | int status; |
| 1005 | struct device *dev; |
| 1006 | |
| 1007 | /* Many systems register gpio chips for SOC support very early, |
| 1008 | * before driver model support is available. In those cases we |
| 1009 | * export this later, in gpiolib_sysfs_init() ... here we just |
| 1010 | * verify that _some_ field of gpio_class got initialized. |
| 1011 | */ |
| 1012 | if (!gpio_class.p) |
| 1013 | return 0; |
| 1014 | |
| 1015 | /* use chip->base for the ID; it's already known to be unique */ |
| 1016 | mutex_lock(&sysfs_lock); |
| 1017 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, |
| 1018 | "gpiochip%d", chip->base); |
Sergei Shtylyov | d62668e | 2009-11-11 14:26:50 -0800 | [diff] [blame] | 1019 | if (!IS_ERR(dev)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1020 | status = sysfs_create_group(&dev->kobj, |
| 1021 | &gpiochip_attr_group); |
| 1022 | } else |
Sergei Shtylyov | d62668e | 2009-11-11 14:26:50 -0800 | [diff] [blame] | 1023 | status = PTR_ERR(dev); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1024 | chip->exported = (status == 0); |
| 1025 | mutex_unlock(&sysfs_lock); |
| 1026 | |
| 1027 | if (status) { |
| 1028 | unsigned long flags; |
| 1029 | unsigned gpio; |
| 1030 | |
| 1031 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1032 | gpio = 0; |
| 1033 | while (gpio < chip->ngpio) |
| 1034 | chip->desc[gpio++].chip = NULL; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1035 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1036 | |
| 1037 | pr_debug("%s: chip %s status %d\n", __func__, |
| 1038 | chip->label, status); |
| 1039 | } |
| 1040 | |
| 1041 | return status; |
| 1042 | } |
| 1043 | |
| 1044 | static void gpiochip_unexport(struct gpio_chip *chip) |
| 1045 | { |
| 1046 | int status; |
| 1047 | struct device *dev; |
| 1048 | |
| 1049 | mutex_lock(&sysfs_lock); |
| 1050 | dev = class_find_device(&gpio_class, NULL, chip, match_export); |
| 1051 | if (dev) { |
| 1052 | put_device(dev); |
| 1053 | device_unregister(dev); |
| 1054 | chip->exported = 0; |
| 1055 | status = 0; |
| 1056 | } else |
| 1057 | status = -ENODEV; |
| 1058 | mutex_unlock(&sysfs_lock); |
| 1059 | |
| 1060 | if (status) |
| 1061 | pr_debug("%s: chip %s status %d\n", __func__, |
| 1062 | chip->label, status); |
| 1063 | } |
| 1064 | |
| 1065 | static int __init gpiolib_sysfs_init(void) |
| 1066 | { |
| 1067 | int status; |
| 1068 | unsigned long flags; |
Alexandre Courbot | 65493e3 | 2013-02-03 01:29:25 +0900 | [diff] [blame] | 1069 | struct gpio_chip *chip; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1070 | |
| 1071 | status = class_register(&gpio_class); |
| 1072 | if (status < 0) |
| 1073 | return status; |
| 1074 | |
| 1075 | /* Scan and register the gpio_chips which registered very |
| 1076 | * early (e.g. before the class_register above was called). |
| 1077 | * |
| 1078 | * We run before arch_initcall() so chip->dev nodes can have |
| 1079 | * registered, and so arch_initcall() can always gpio_export(). |
| 1080 | */ |
| 1081 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 65493e3 | 2013-02-03 01:29:25 +0900 | [diff] [blame] | 1082 | list_for_each_entry(chip, &gpio_chips, list) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1083 | if (!chip || chip->exported) |
| 1084 | continue; |
| 1085 | |
| 1086 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1087 | status = gpiochip_export(chip); |
| 1088 | spin_lock_irqsave(&gpio_lock, flags); |
| 1089 | } |
| 1090 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1091 | |
| 1092 | |
| 1093 | return status; |
| 1094 | } |
| 1095 | postcore_initcall(gpiolib_sysfs_init); |
| 1096 | |
| 1097 | #else |
| 1098 | static inline int gpiochip_export(struct gpio_chip *chip) |
| 1099 | { |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | static inline void gpiochip_unexport(struct gpio_chip *chip) |
| 1104 | { |
| 1105 | } |
| 1106 | |
| 1107 | #endif /* CONFIG_GPIO_SYSFS */ |
| 1108 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1109 | /* |
| 1110 | * Add a new chip to the global chips list, keeping the list of chips sorted |
| 1111 | * by base order. |
| 1112 | * |
| 1113 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 1114 | * space. |
| 1115 | */ |
| 1116 | static int gpiochip_add_to_list(struct gpio_chip *chip) |
| 1117 | { |
| 1118 | struct list_head *pos = &gpio_chips; |
| 1119 | struct gpio_chip *_chip; |
| 1120 | int err = 0; |
| 1121 | |
| 1122 | /* find where to insert our chip */ |
| 1123 | list_for_each(pos, &gpio_chips) { |
| 1124 | _chip = list_entry(pos, struct gpio_chip, list); |
| 1125 | /* shall we insert before _chip? */ |
| 1126 | if (_chip->base >= chip->base + chip->ngpio) |
| 1127 | break; |
| 1128 | } |
| 1129 | |
| 1130 | /* are we stepping on the chip right before? */ |
| 1131 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { |
| 1132 | _chip = list_entry(pos->prev, struct gpio_chip, list); |
| 1133 | if (_chip->base + _chip->ngpio > chip->base) { |
| 1134 | dev_err(chip->dev, |
| 1135 | "GPIO integer space overlap, cannot add chip\n"); |
| 1136 | err = -EBUSY; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | if (!err) |
| 1141 | list_add_tail(&chip->list, pos); |
| 1142 | |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 1146 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1147 | * gpiochip_add() - register a gpio_chip |
| 1148 | * @chip: the chip to register, with chip->base initialized |
| 1149 | * Context: potentially before irqs or kmalloc will work |
| 1150 | * |
| 1151 | * Returns a negative errno if the chip can't be registered, such as |
| 1152 | * because the chip->base is invalid or already associated with a |
| 1153 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1154 | * |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1155 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 1156 | * can be freely used, the chip->dev device must be registered before |
| 1157 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 1158 | * for GPIOs will fail rudely. |
| 1159 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1160 | * If chip->base is negative, this requests dynamic assignment of |
| 1161 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1162 | */ |
| 1163 | int gpiochip_add(struct gpio_chip *chip) |
| 1164 | { |
| 1165 | unsigned long flags; |
| 1166 | int status = 0; |
| 1167 | unsigned id; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1168 | int base = chip->base; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1169 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 1170 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1171 | && base >= 0) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1172 | status = -EINVAL; |
| 1173 | goto fail; |
| 1174 | } |
| 1175 | |
| 1176 | spin_lock_irqsave(&gpio_lock, flags); |
| 1177 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1178 | if (base < 0) { |
| 1179 | base = gpiochip_find_base(chip->ngpio); |
| 1180 | if (base < 0) { |
| 1181 | status = base; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1182 | goto unlock; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1183 | } |
| 1184 | chip->base = base; |
| 1185 | } |
| 1186 | |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1187 | status = gpiochip_add_to_list(chip); |
| 1188 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1189 | if (status == 0) { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1190 | chip->desc = &gpio_desc[chip->base]; |
| 1191 | |
| 1192 | for (id = 0; id < chip->ngpio; id++) { |
| 1193 | struct gpio_desc *desc = &chip->desc[id]; |
| 1194 | desc->chip = chip; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1195 | |
| 1196 | /* REVISIT: most hardware initializes GPIOs as |
| 1197 | * inputs (often with pullups enabled) so power |
| 1198 | * usage is minimized. Linux code should set the |
| 1199 | * gpio direction first thing; but until it does, |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1200 | * and in case chip->get_direction is not set, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1201 | * we may expose the wrong direction in sysfs. |
| 1202 | */ |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1203 | desc->flags = !chip->direction_input |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1204 | ? (1 << FLAG_IS_OUT) |
| 1205 | : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1209 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1210 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1211 | #ifdef CONFIG_PINCTRL |
| 1212 | INIT_LIST_HEAD(&chip->pin_ranges); |
| 1213 | #endif |
| 1214 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1215 | of_gpiochip_add(chip); |
| 1216 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1217 | if (status) |
| 1218 | goto fail; |
| 1219 | |
| 1220 | status = gpiochip_export(chip); |
| 1221 | if (status) |
| 1222 | goto fail; |
| 1223 | |
H Hartley Sweeten | ee1c1e7 | 2012-05-11 16:58:33 -0700 | [diff] [blame] | 1224 | pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n", |
Grant Likely | 64842aa | 2011-11-06 11:36:18 -0700 | [diff] [blame] | 1225 | chip->base, chip->base + chip->ngpio - 1, |
| 1226 | chip->label ? : "generic"); |
| 1227 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1228 | return 0; |
Zhangfei Gao | 3bae481 | 2013-06-09 11:08:32 +0800 | [diff] [blame] | 1229 | |
| 1230 | unlock: |
| 1231 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1232 | fail: |
| 1233 | /* failures here can mean systems won't boot... */ |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1234 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", |
| 1235 | chip->base, chip->base + chip->ngpio - 1, |
| 1236 | chip->label ? : "generic"); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1237 | return status; |
| 1238 | } |
| 1239 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 1240 | |
| 1241 | /** |
| 1242 | * gpiochip_remove() - unregister a gpio_chip |
| 1243 | * @chip: the chip to unregister |
| 1244 | * |
| 1245 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 1246 | */ |
| 1247 | int gpiochip_remove(struct gpio_chip *chip) |
| 1248 | { |
| 1249 | unsigned long flags; |
| 1250 | int status = 0; |
| 1251 | unsigned id; |
| 1252 | |
| 1253 | spin_lock_irqsave(&gpio_lock, flags); |
| 1254 | |
Linus Walleij | 9ef0d6f | 2012-11-06 15:15:44 +0100 | [diff] [blame] | 1255 | gpiochip_remove_pin_ranges(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1256 | of_gpiochip_remove(chip); |
| 1257 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1258 | for (id = 0; id < chip->ngpio; id++) { |
| 1259 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1260 | status = -EBUSY; |
| 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | if (status == 0) { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1265 | for (id = 0; id < chip->ngpio; id++) |
| 1266 | chip->desc[id].chip = NULL; |
Alexandre Courbot | 1a989d0 | 2013-02-03 01:29:24 +0900 | [diff] [blame] | 1267 | |
| 1268 | list_del(&chip->list); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1272 | |
| 1273 | if (status == 0) |
| 1274 | gpiochip_unexport(chip); |
| 1275 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1276 | return status; |
| 1277 | } |
| 1278 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 1279 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1280 | /** |
| 1281 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 1282 | * @data: data to pass to match function |
| 1283 | * @callback: Callback function to check gpio_chip |
| 1284 | * |
| 1285 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 1286 | * determined by a user supplied @match callback. The callback should return |
| 1287 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 1288 | * non-zero, this function will return to the caller and not iterate over any |
| 1289 | * more gpio_chips. |
| 1290 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 1291 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 1292 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 1293 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1294 | { |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 1295 | struct gpio_chip *chip; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1296 | unsigned long flags; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1297 | |
| 1298 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 1299 | list_for_each_entry(chip, &gpio_chips, list) |
| 1300 | if (match(chip, data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1301 | break; |
Alexandre Courbot | 125eef9 | 2013-02-03 01:29:26 +0900 | [diff] [blame] | 1302 | |
| 1303 | /* No match? */ |
| 1304 | if (&chip->list == &gpio_chips) |
| 1305 | chip = NULL; |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1306 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1307 | |
| 1308 | return chip; |
| 1309 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 1310 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1311 | |
Alexandre Courbot | 79697ef | 2013-11-16 21:39:32 +0900 | [diff] [blame] | 1312 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
| 1313 | { |
| 1314 | const char *name = data; |
| 1315 | |
| 1316 | return !strcmp(chip->label, name); |
| 1317 | } |
| 1318 | |
| 1319 | static struct gpio_chip *find_chip_by_name(const char *name) |
| 1320 | { |
| 1321 | return gpiochip_find((void *)name, gpiochip_match_name); |
| 1322 | } |
| 1323 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1324 | #ifdef CONFIG_PINCTRL |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1325 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1326 | /** |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1327 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 1328 | * @chip: the gpiochip to add the range for |
| 1329 | * @pinctrl: the dev_name() of the pin controller to map to |
| 1330 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 1331 | * @pin_group: name of the pin group inside the pin controller |
| 1332 | */ |
| 1333 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 1334 | struct pinctrl_dev *pctldev, |
| 1335 | unsigned int gpio_offset, const char *pin_group) |
| 1336 | { |
| 1337 | struct gpio_pin_range *pin_range; |
| 1338 | int ret; |
| 1339 | |
| 1340 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 1341 | if (!pin_range) { |
| 1342 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", |
| 1343 | chip->label); |
| 1344 | return -ENOMEM; |
| 1345 | } |
| 1346 | |
| 1347 | /* Use local offset as range ID */ |
| 1348 | pin_range->range.id = gpio_offset; |
| 1349 | pin_range->range.gc = chip; |
| 1350 | pin_range->range.name = chip->label; |
| 1351 | pin_range->range.base = chip->base + gpio_offset; |
| 1352 | pin_range->pctldev = pctldev; |
| 1353 | |
| 1354 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 1355 | &pin_range->range.pins, |
| 1356 | &pin_range->range.npins); |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 1357 | if (ret < 0) { |
| 1358 | kfree(pin_range); |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1359 | return ret; |
Michal Nazarewicz | 61c6375 | 2013-11-13 21:20:39 +0100 | [diff] [blame] | 1360 | } |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 1361 | |
| 1362 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 1363 | |
| 1364 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 1365 | chip->label, gpio_offset, |
| 1366 | gpio_offset + pin_range->range.npins - 1, |
| 1367 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 1368 | |
| 1369 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
| 1370 | |
| 1371 | return 0; |
| 1372 | } |
| 1373 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 1374 | |
| 1375 | /** |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1376 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 1377 | * @chip: the gpiochip to add the range for |
| 1378 | * @pinctrl_name: the dev_name() of the pin controller to map to |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1379 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 1380 | * @pin_offset: the start offset in the pin controller number space |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1381 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 1382 | * pin controller) to accumulate in this range |
| 1383 | */ |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1384 | 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] | 1385 | unsigned int gpio_offset, unsigned int pin_offset, |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1386 | unsigned int npins) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1387 | { |
| 1388 | struct gpio_pin_range *pin_range; |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1389 | int ret; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1390 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1391 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1392 | if (!pin_range) { |
| 1393 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", |
| 1394 | chip->label); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1395 | return -ENOMEM; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1396 | } |
| 1397 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1398 | /* Use local offset as range ID */ |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1399 | pin_range->range.id = gpio_offset; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1400 | pin_range->range.gc = chip; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1401 | pin_range->range.name = chip->label; |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1402 | pin_range->range.base = chip->base + gpio_offset; |
| 1403 | pin_range->range.pin_base = pin_offset; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1404 | pin_range->range.npins = npins; |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 1405 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1406 | &pin_range->range); |
Linus Walleij | 8f23ca1 | 2012-11-20 14:56:25 +0100 | [diff] [blame] | 1407 | if (IS_ERR(pin_range->pctldev)) { |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1408 | ret = PTR_ERR(pin_range->pctldev); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1409 | pr_err("%s: GPIO chip: could not create pin range\n", |
| 1410 | chip->label); |
| 1411 | kfree(pin_range); |
Axel Lin | b4d4b1f | 2012-11-21 14:33:56 +0800 | [diff] [blame] | 1412 | return ret; |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1413 | } |
Linus Walleij | 316511c | 2012-11-21 08:48:09 +0100 | [diff] [blame] | 1414 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 1415 | chip->label, gpio_offset, gpio_offset + npins - 1, |
| 1416 | pinctl_name, |
| 1417 | pin_offset, pin_offset + npins - 1); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1418 | |
| 1419 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 1420 | |
| 1421 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1422 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1423 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1424 | |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1425 | /** |
| 1426 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 1427 | * @chip: the chip to remove all the mappings for |
| 1428 | */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1429 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 1430 | { |
| 1431 | struct gpio_pin_range *pin_range, *tmp; |
| 1432 | |
| 1433 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
| 1434 | list_del(&pin_range->node); |
| 1435 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 1436 | &pin_range->range); |
Linus Walleij | 3f0f867 | 2012-11-20 12:40:15 +0100 | [diff] [blame] | 1437 | kfree(pin_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1438 | } |
| 1439 | } |
Linus Walleij | 165adc9 | 2012-11-06 14:49:39 +0100 | [diff] [blame] | 1440 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 1441 | |
| 1442 | #endif /* CONFIG_PINCTRL */ |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 1443 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1444 | /* These "optional" allocation calls help prevent drivers from stomping |
| 1445 | * on each other, and help provide better diagnostics in debugfs. |
| 1446 | * They're called even less than the "set direction" calls. |
| 1447 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1448 | static int gpiod_request(struct gpio_desc *desc, const char *label) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1449 | { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1450 | struct gpio_chip *chip; |
Mark Brown | e935457 | 2012-07-09 12:22:56 +0100 | [diff] [blame] | 1451 | int status = -EPROBE_DEFER; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1452 | unsigned long flags; |
| 1453 | |
Alexandre Courbot | 0204df4 | 2013-10-04 10:59:58 -0700 | [diff] [blame] | 1454 | if (!desc) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1455 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1456 | return -EINVAL; |
| 1457 | } |
| 1458 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1459 | spin_lock_irqsave(&gpio_lock, flags); |
| 1460 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1461 | chip = desc->chip; |
Alexandre Courbot | 0204df4 | 2013-10-04 10:59:58 -0700 | [diff] [blame] | 1462 | if (chip == NULL) |
| 1463 | goto done; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1464 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1465 | if (!try_module_get(chip->owner)) |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1466 | goto done; |
| 1467 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1468 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1469 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1470 | */ |
| 1471 | |
| 1472 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 1473 | desc_set_label(desc, label ? : "?"); |
| 1474 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1475 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1476 | status = -EBUSY; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1477 | module_put(chip->owner); |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 1478 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | if (chip->request) { |
| 1482 | /* chip->request may sleep */ |
| 1483 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1484 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1485 | spin_lock_irqsave(&gpio_lock, flags); |
| 1486 | |
| 1487 | if (status < 0) { |
| 1488 | desc_set_label(desc, NULL); |
| 1489 | module_put(chip->owner); |
| 1490 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1491 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1492 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1493 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1494 | if (chip->get_direction) { |
| 1495 | /* chip->get_direction may sleep */ |
| 1496 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1497 | gpiod_get_direction(desc); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame] | 1498 | spin_lock_irqsave(&gpio_lock, flags); |
| 1499 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1500 | done: |
| 1501 | if (status) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1502 | pr_debug("_gpio_request: gpio-%d (%s) status %d\n", |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1503 | desc_to_gpio(desc), label ? : "?", status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1504 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1505 | return status; |
| 1506 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1507 | |
| 1508 | int gpio_request(unsigned gpio, const char *label) |
| 1509 | { |
| 1510 | return gpiod_request(gpio_to_desc(gpio), label); |
| 1511 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1512 | EXPORT_SYMBOL_GPL(gpio_request); |
| 1513 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1514 | static void gpiod_free(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1515 | { |
| 1516 | unsigned long flags; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1517 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1518 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 1519 | might_sleep(); |
| 1520 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1521 | if (!desc) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1522 | WARN_ON(extra_checks); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1526 | gpiod_unexport(desc); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1527 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1528 | spin_lock_irqsave(&gpio_lock, flags); |
| 1529 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1530 | chip = desc->chip; |
| 1531 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 1532 | if (chip->free) { |
| 1533 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1534 | might_sleep_if(chip->can_sleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1535 | chip->free(chip, gpio_chip_hwgpio(desc)); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1536 | spin_lock_irqsave(&gpio_lock, flags); |
| 1537 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1538 | desc_set_label(desc, NULL); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1539 | module_put(desc->chip->owner); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 1540 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1541 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1542 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1543 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1544 | } else |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1545 | WARN_ON(extra_checks); |
| 1546 | |
| 1547 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1548 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1549 | |
| 1550 | void gpio_free(unsigned gpio) |
| 1551 | { |
| 1552 | gpiod_free(gpio_to_desc(gpio)); |
| 1553 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1554 | EXPORT_SYMBOL_GPL(gpio_free); |
| 1555 | |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1556 | /** |
| 1557 | * gpio_request_one - request a single GPIO with initial configuration |
| 1558 | * @gpio: the GPIO number |
| 1559 | * @flags: GPIO configuration as specified by GPIOF_* |
| 1560 | * @label: a literal description string of this GPIO |
| 1561 | */ |
| 1562 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) |
| 1563 | { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1564 | struct gpio_desc *desc; |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1565 | int err; |
| 1566 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1567 | desc = gpio_to_desc(gpio); |
| 1568 | |
| 1569 | err = gpiod_request(desc, label); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1570 | if (err) |
| 1571 | return err; |
| 1572 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1573 | if (flags & GPIOF_OPEN_DRAIN) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1574 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1575 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1576 | if (flags & GPIOF_OPEN_SOURCE) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1577 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1578 | |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1579 | if (flags & GPIOF_DIR_IN) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1580 | err = gpiod_direction_input(desc); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1581 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1582 | err = gpiod_direction_output(desc, |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1583 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
| 1584 | |
Aaro Koskinen | e254811 | 2010-12-21 17:24:22 -0800 | [diff] [blame] | 1585 | if (err) |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1586 | goto free_gpio; |
Aaro Koskinen | e254811 | 2010-12-21 17:24:22 -0800 | [diff] [blame] | 1587 | |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1588 | if (flags & GPIOF_EXPORT) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1589 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1590 | if (err) |
| 1591 | goto free_gpio; |
| 1592 | } |
| 1593 | |
| 1594 | return 0; |
| 1595 | |
| 1596 | free_gpio: |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1597 | gpiod_free(desc); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1598 | return err; |
| 1599 | } |
| 1600 | EXPORT_SYMBOL_GPL(gpio_request_one); |
| 1601 | |
| 1602 | /** |
| 1603 | * gpio_request_array - request multiple GPIOs in a single call |
| 1604 | * @array: array of the 'struct gpio' |
| 1605 | * @num: how many GPIOs in the array |
| 1606 | */ |
Lars-Peter Clausen | 7c29597 | 2011-05-25 16:20:31 -0700 | [diff] [blame] | 1607 | int gpio_request_array(const struct gpio *array, size_t num) |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1608 | { |
| 1609 | int i, err; |
| 1610 | |
| 1611 | for (i = 0; i < num; i++, array++) { |
| 1612 | err = gpio_request_one(array->gpio, array->flags, array->label); |
| 1613 | if (err) |
| 1614 | goto err_free; |
| 1615 | } |
| 1616 | return 0; |
| 1617 | |
| 1618 | err_free: |
| 1619 | while (i--) |
| 1620 | gpio_free((--array)->gpio); |
| 1621 | return err; |
| 1622 | } |
| 1623 | EXPORT_SYMBOL_GPL(gpio_request_array); |
| 1624 | |
| 1625 | /** |
| 1626 | * gpio_free_array - release multiple GPIOs in a single call |
| 1627 | * @array: array of the 'struct gpio' |
| 1628 | * @num: how many GPIOs in the array |
| 1629 | */ |
Lars-Peter Clausen | 7c29597 | 2011-05-25 16:20:31 -0700 | [diff] [blame] | 1630 | void gpio_free_array(const struct gpio *array, size_t num) |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1631 | { |
| 1632 | while (num--) |
| 1633 | gpio_free((array++)->gpio); |
| 1634 | } |
| 1635 | EXPORT_SYMBOL_GPL(gpio_free_array); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1636 | |
| 1637 | /** |
| 1638 | * gpiochip_is_requested - return string iff signal was requested |
| 1639 | * @chip: controller managing the signal |
| 1640 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 1641 | * |
| 1642 | * Returns NULL if the GPIO is not currently requested, else a string. |
| 1643 | * If debugfs support is enabled, the string returned is the label passed |
| 1644 | * to gpio_request(); otherwise it is a meaningless constant. |
| 1645 | * |
| 1646 | * This function is for use by GPIO controller drivers. The label can |
| 1647 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 1648 | * can help avoid accidentally multiplexing it to another controller. |
| 1649 | */ |
| 1650 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 1651 | { |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1652 | struct gpio_desc *desc; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1653 | |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1654 | if (!GPIO_OFFSET_VALID(chip, offset)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1655 | return NULL; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 1656 | |
| 1657 | desc = &chip->desc[offset]; |
| 1658 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1659 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1660 | return NULL; |
| 1661 | #ifdef CONFIG_DEBUG_FS |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1662 | return desc->label; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1663 | #else |
| 1664 | return "?"; |
| 1665 | #endif |
| 1666 | } |
| 1667 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 1668 | |
| 1669 | |
| 1670 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 1671 | * some cases this is done in early boot, before IRQs are enabled. |
| 1672 | * |
| 1673 | * As a rule these aren't called more than once (except for drivers |
| 1674 | * using the open-drain emulation idiom) so these are natural places |
| 1675 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 1676 | * rely on gpio_request() having been called beforehand. |
| 1677 | */ |
| 1678 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1679 | /** |
| 1680 | * gpiod_direction_input - set the GPIO direction to input |
| 1681 | * @desc: GPIO to set to input |
| 1682 | * |
| 1683 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 1684 | * be called safely on it. |
| 1685 | * |
| 1686 | * Return 0 in case of success, else an error code. |
| 1687 | */ |
| 1688 | int gpiod_direction_input(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1689 | { |
| 1690 | unsigned long flags; |
| 1691 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1692 | int status = -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1693 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1694 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1695 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1696 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1697 | return -EINVAL; |
| 1698 | } |
| 1699 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1700 | chip = desc->chip; |
| 1701 | if (!chip->get || !chip->direction_input) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1702 | gpiod_warn(desc, |
| 1703 | "%s: missing get() or direction_input() operations\n", |
| 1704 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1705 | return -EIO; |
| 1706 | } |
| 1707 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1708 | spin_lock_irqsave(&gpio_lock, flags); |
| 1709 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1710 | status = gpio_ensure_requested(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1711 | if (status < 0) |
| 1712 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1713 | |
| 1714 | /* now we know the gpio is valid and chip won't vanish */ |
| 1715 | |
| 1716 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1717 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1718 | might_sleep_if(chip->can_sleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1719 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1720 | offset = gpio_chip_hwgpio(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1721 | if (status) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1722 | status = chip->request(chip, offset); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1723 | if (status < 0) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1724 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1725 | /* and it's not available to anyone else ... |
| 1726 | * gpio_request() is the fully clean solution. |
| 1727 | */ |
| 1728 | goto lose; |
| 1729 | } |
| 1730 | } |
| 1731 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1732 | status = chip->direction_input(chip, offset); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1733 | if (status == 0) |
| 1734 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1735 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1736 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1737 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1738 | return status; |
| 1739 | fail: |
| 1740 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1741 | if (status) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1742 | gpiod_dbg(desc, "%s status %d\n", __func__, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1743 | return status; |
| 1744 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1745 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1746 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1747 | /** |
| 1748 | * gpiod_direction_output - set the GPIO direction to input |
| 1749 | * @desc: GPIO to set to output |
| 1750 | * @value: initial output value of the GPIO |
| 1751 | * |
| 1752 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1753 | * be called safely on it. The initial value of the output must be specified. |
| 1754 | * |
| 1755 | * Return 0 in case of success, else an error code. |
| 1756 | */ |
| 1757 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1758 | { |
| 1759 | unsigned long flags; |
| 1760 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1761 | int status = -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1762 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1763 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1764 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1765 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1766 | return -EINVAL; |
| 1767 | } |
| 1768 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 1769 | /* GPIOs used for IRQs shall not be set as output */ |
| 1770 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
| 1771 | gpiod_err(desc, |
| 1772 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 1773 | __func__); |
| 1774 | return -EIO; |
| 1775 | } |
| 1776 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1777 | /* Open drain pin should not be driven to 1 */ |
| 1778 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1779 | return gpiod_direction_input(desc); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1780 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1781 | /* Open source pin should not be driven to 0 */ |
| 1782 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1783 | return gpiod_direction_input(desc); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1784 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1785 | chip = desc->chip; |
| 1786 | if (!chip->set || !chip->direction_output) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1787 | gpiod_warn(desc, |
| 1788 | "%s: missing set() or direction_output() operations\n", |
| 1789 | __func__); |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1790 | return -EIO; |
| 1791 | } |
| 1792 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1793 | spin_lock_irqsave(&gpio_lock, flags); |
| 1794 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1795 | status = gpio_ensure_requested(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1796 | if (status < 0) |
| 1797 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1798 | |
| 1799 | /* now we know the gpio is valid and chip won't vanish */ |
| 1800 | |
| 1801 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1802 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1803 | might_sleep_if(chip->can_sleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1804 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1805 | offset = gpio_chip_hwgpio(desc); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1806 | if (status) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1807 | status = chip->request(chip, offset); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1808 | if (status < 0) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1809 | gpiod_dbg(desc, "chip request fail, %d\n", status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1810 | /* and it's not available to anyone else ... |
| 1811 | * gpio_request() is the fully clean solution. |
| 1812 | */ |
| 1813 | goto lose; |
| 1814 | } |
| 1815 | } |
| 1816 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1817 | status = chip->direction_output(chip, offset, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1818 | if (status == 0) |
| 1819 | set_bit(FLAG_IS_OUT, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1820 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1821 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1822 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1823 | return status; |
| 1824 | fail: |
| 1825 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1826 | if (status) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1827 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1828 | return status; |
| 1829 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1830 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1831 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1832 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1833 | * gpiod_set_debounce - sets @debounce time for a @gpio |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1834 | * @gpio: the gpio to set debounce time |
| 1835 | * @debounce: debounce time is microseconds |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1836 | * |
| 1837 | * returns -ENOTSUPP if the controller does not support setting |
| 1838 | * debounce. |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1839 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1840 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1841 | { |
| 1842 | unsigned long flags; |
| 1843 | struct gpio_chip *chip; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1844 | int status = -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1845 | int offset; |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1846 | |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1847 | if (!desc || !desc->chip) { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1848 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1849 | return -EINVAL; |
| 1850 | } |
| 1851 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1852 | chip = desc->chip; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1853 | if (!chip->set || !chip->set_debounce) { |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1854 | gpiod_dbg(desc, |
| 1855 | "%s: missing set() or set_debounce() operations\n", |
| 1856 | __func__); |
Linus Walleij | 65d8765 | 2013-09-04 14:17:08 +0200 | [diff] [blame] | 1857 | return -ENOTSUPP; |
Linus Walleij | be1a4b1 | 2013-08-30 09:41:45 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1861 | |
| 1862 | status = gpio_ensure_requested(desc); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1863 | if (status < 0) |
| 1864 | goto fail; |
| 1865 | |
| 1866 | /* now we know the gpio is valid and chip won't vanish */ |
| 1867 | |
| 1868 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1869 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1870 | might_sleep_if(chip->can_sleep); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1871 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1872 | offset = gpio_chip_hwgpio(desc); |
| 1873 | return chip->set_debounce(chip, offset, debounce); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1874 | |
| 1875 | fail: |
| 1876 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 1877 | if (status) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1878 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1879 | |
| 1880 | return status; |
| 1881 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1882 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1883 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1884 | /** |
| 1885 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 1886 | * @desc: the gpio descriptor to test |
| 1887 | * |
| 1888 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 1889 | */ |
| 1890 | int gpiod_is_active_low(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1891 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1892 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1893 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1894 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1895 | |
| 1896 | /* I/O calls are only valid after configuration completed; the relevant |
| 1897 | * "is this a valid GPIO" error checks should already have been done. |
| 1898 | * |
| 1899 | * "Get" operations are often inlinable as reading a pin value register, |
| 1900 | * and masking the relevant bit in that register. |
| 1901 | * |
| 1902 | * When "set" operations are inlinable, they involve writing that mask to |
| 1903 | * one register to set a low value, or a different register to set it high. |
| 1904 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1905 | * |
| 1906 | *------------------------------------------------------------------------ |
| 1907 | * |
| 1908 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1909 | * have requested the GPIO. That can include implicit requesting by |
| 1910 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1911 | * in memory, guaranteeing that these table lookups need no more locking |
| 1912 | * and that gpiochip_remove() will fail. |
| 1913 | * |
| 1914 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1915 | * that the GPIO was actually requested. |
| 1916 | */ |
| 1917 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1918 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1919 | { |
| 1920 | struct gpio_chip *chip; |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1921 | int value; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1922 | int offset; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1923 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1924 | chip = desc->chip; |
| 1925 | offset = gpio_chip_hwgpio(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1926 | value = chip->get ? chip->get(chip, offset) : 0; |
| 1927 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1928 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1929 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1930 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1931 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1932 | * gpiod_get_raw_value() - return a gpio's raw value |
| 1933 | * @desc: gpio whose value will be returned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1934 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1935 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 1936 | * its ACTIVE_LOW status. |
| 1937 | * |
| 1938 | * This function should be called from contexts where we cannot sleep, and will |
| 1939 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1940 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1941 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1942 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1943 | if (!desc) |
| 1944 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1945 | /* Should be using gpio_get_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 1946 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1947 | return _gpiod_get_raw_value(desc); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1948 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1949 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1950 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1951 | /** |
| 1952 | * gpiod_get_value() - return a gpio's value |
| 1953 | * @desc: gpio whose value will be returned |
| 1954 | * |
| 1955 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 1956 | * account. |
| 1957 | * |
| 1958 | * This function should be called from contexts where we cannot sleep, and will |
| 1959 | * complain if the GPIO chip functions potentially sleep. |
| 1960 | */ |
| 1961 | int gpiod_get_value(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1962 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1963 | int value; |
| 1964 | if (!desc) |
| 1965 | return 0; |
| 1966 | /* Should be using gpio_get_value_cansleep() */ |
| 1967 | WARN_ON(desc->chip->can_sleep); |
| 1968 | |
| 1969 | value = _gpiod_get_raw_value(desc); |
| 1970 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1971 | value = !value; |
| 1972 | |
| 1973 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1974 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1975 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1976 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1977 | /* |
| 1978 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1979 | * @desc: gpio descriptor whose state need to be set. |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1980 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1981 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1982 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1983 | { |
| 1984 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1985 | struct gpio_chip *chip = desc->chip; |
| 1986 | int offset = gpio_chip_hwgpio(desc); |
| 1987 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1988 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1989 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1990 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1991 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1992 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1993 | err = chip->direction_output(chip, offset, 0); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1994 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1995 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1996 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 1997 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1998 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 1999 | gpiod_err(desc, |
| 2000 | "%s: Error in set_value for open drain err %d\n", |
| 2001 | __func__, err); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2002 | } |
| 2003 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2004 | /* |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2005 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 2006 | * @desc: gpio descriptor whose state need to be set. |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2007 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 2008 | */ |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2009 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2010 | { |
| 2011 | int err = 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2012 | struct gpio_chip *chip = desc->chip; |
| 2013 | int offset = gpio_chip_hwgpio(desc); |
| 2014 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2015 | if (value) { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2016 | err = chip->direction_output(chip, offset, 1); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2017 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2018 | set_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2019 | } else { |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2020 | err = chip->direction_input(chip, offset); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2021 | if (!err) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2022 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2023 | } |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2024 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2025 | if (err < 0) |
Mark Brown | 6424de5 | 2013-09-09 10:33:49 +0100 | [diff] [blame] | 2026 | gpiod_err(desc, |
| 2027 | "%s: Error in set_value for open source err %d\n", |
| 2028 | __func__, err); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 2029 | } |
| 2030 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2031 | static void _gpiod_set_raw_value(struct gpio_desc *desc, int value) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2032 | { |
| 2033 | struct gpio_chip *chip; |
| 2034 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2035 | chip = desc->chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2036 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 2037 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 2038 | _gpio_set_open_drain_value(desc, value); |
| 2039 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 2040 | _gpio_set_open_source_value(desc, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 2041 | else |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2042 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
| 2043 | } |
| 2044 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2045 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2046 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 2047 | * @desc: gpio whose value will be assigned |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2048 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2049 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2050 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2051 | * regard for its ACTIVE_LOW status. |
| 2052 | * |
| 2053 | * This function should be called from contexts where we cannot sleep, and will |
| 2054 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2055 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2056 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2057 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2058 | if (!desc) |
| 2059 | return; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2060 | /* Should be using gpio_set_value_cansleep() */ |
Alexandre Courbot | d8e0ac0 | 2013-09-04 20:29:25 +0900 | [diff] [blame] | 2061 | WARN_ON(desc->chip->can_sleep); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2062 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2063 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2064 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2065 | |
| 2066 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2067 | * gpiod_set_value() - assign a gpio's value |
| 2068 | * @desc: gpio whose value will be assigned |
| 2069 | * @value: value to assign |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2070 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2071 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2072 | * account |
| 2073 | * |
| 2074 | * This function should be called from contexts where we cannot sleep, and will |
| 2075 | * complain if the GPIO chip functions potentially sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2076 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2077 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 2078 | { |
| 2079 | if (!desc) |
| 2080 | return; |
| 2081 | /* Should be using gpio_set_value_cansleep() */ |
| 2082 | WARN_ON(desc->chip->can_sleep); |
| 2083 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2084 | value = !value; |
| 2085 | _gpiod_set_raw_value(desc, value); |
| 2086 | } |
| 2087 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 2088 | |
| 2089 | /** |
| 2090 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 2091 | * @desc: gpio to check |
| 2092 | * |
| 2093 | */ |
| 2094 | int gpiod_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2095 | { |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2096 | if (!desc) |
| 2097 | return 0; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2098 | return desc->chip->can_sleep; |
| 2099 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2100 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2101 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2102 | /** |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2103 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 2104 | * @desc: gpio whose IRQ will be returned (already requested) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2105 | * |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2106 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 2107 | * error. |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2108 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2109 | int gpiod_to_irq(const struct gpio_desc *desc) |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2110 | { |
| 2111 | struct gpio_chip *chip; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2112 | int offset; |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 2113 | |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2114 | if (!desc) |
| 2115 | return -EINVAL; |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2116 | chip = desc->chip; |
| 2117 | offset = gpio_chip_hwgpio(desc); |
| 2118 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
| 2119 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2120 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2121 | |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2122 | /** |
| 2123 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ |
| 2124 | * @gpio: the GPIO line to lock as used for IRQ |
| 2125 | * |
| 2126 | * This is used directly by GPIO drivers that want to lock down |
| 2127 | * a certain GPIO line to be used as IRQs, for example in the |
| 2128 | * .to_irq() callback of their gpio_chip, or in the .irq_enable() |
| 2129 | * of its irq_chip implementation if the GPIO is known from that |
| 2130 | * code. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2131 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2132 | int gpiod_lock_as_irq(struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2133 | { |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2134 | if (!desc) |
| 2135 | return -EINVAL; |
| 2136 | |
| 2137 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
| 2138 | gpiod_err(desc, |
| 2139 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 2140 | __func__); |
| 2141 | return -EIO; |
| 2142 | } |
| 2143 | |
| 2144 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2145 | return 0; |
| 2146 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2147 | EXPORT_SYMBOL_GPL(gpiod_lock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2148 | |
| 2149 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 2150 | { |
| 2151 | return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset)); |
| 2152 | } |
| 2153 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); |
| 2154 | |
| 2155 | /** |
| 2156 | * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ |
| 2157 | * @gpio: the GPIO line to unlock from IRQ usage |
| 2158 | * |
| 2159 | * This is used directly by GPIO drivers that want to indicate |
| 2160 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 2161 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2162 | void gpiod_unlock_as_irq(struct gpio_desc *desc) |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2163 | { |
| 2164 | if (!desc) |
| 2165 | return; |
| 2166 | |
| 2167 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
| 2168 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2169 | EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2170 | |
| 2171 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 2172 | { |
| 2173 | return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset)); |
| 2174 | } |
| 2175 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2176 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2177 | /** |
| 2178 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 2179 | * @desc: gpio whose value will be returned |
| 2180 | * |
| 2181 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 2182 | * its ACTIVE_LOW status. |
| 2183 | * |
| 2184 | * This function is to be called from contexts that can sleep. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2185 | */ |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2186 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2187 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2188 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2189 | if (!desc) |
| 2190 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2191 | return _gpiod_get_raw_value(desc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2192 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2193 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2194 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2195 | /** |
| 2196 | * gpiod_get_value_cansleep() - return a gpio's value |
| 2197 | * @desc: gpio whose value will be returned |
| 2198 | * |
| 2199 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 2200 | * account. |
| 2201 | * |
| 2202 | * This function is to be called from contexts that can sleep. |
| 2203 | */ |
| 2204 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2205 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2206 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2207 | |
| 2208 | might_sleep_if(extra_checks); |
| 2209 | if (!desc) |
| 2210 | return 0; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2211 | |
| 2212 | value = _gpiod_get_raw_value(desc); |
| 2213 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2214 | value = !value; |
| 2215 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2216 | return value; |
| 2217 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2218 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2219 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2220 | /** |
| 2221 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 2222 | * @desc: gpio whose value will be assigned |
| 2223 | * @value: value to assign |
| 2224 | * |
| 2225 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 2226 | * regard for its ACTIVE_LOW status. |
| 2227 | * |
| 2228 | * This function is to be called from contexts that can sleep. |
| 2229 | */ |
| 2230 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2231 | { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2232 | might_sleep_if(extra_checks); |
Alexandre Courbot | bcabdef | 2013-02-15 14:46:14 +0900 | [diff] [blame] | 2233 | if (!desc) |
| 2234 | return; |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2235 | _gpiod_set_raw_value(desc, value); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2236 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2237 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2238 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2239 | /** |
| 2240 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 2241 | * @desc: gpio whose value will be assigned |
| 2242 | * @value: value to assign |
| 2243 | * |
| 2244 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 2245 | * account |
| 2246 | * |
| 2247 | * This function is to be called from contexts that can sleep. |
| 2248 | */ |
| 2249 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2250 | { |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2251 | might_sleep_if(extra_checks); |
| 2252 | if (!desc) |
| 2253 | return; |
| 2254 | |
| 2255 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 2256 | value = !value; |
| 2257 | _gpiod_set_raw_value(desc, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2258 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 2259 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2260 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2261 | /** |
| 2262 | * gpiod_add_table() - register GPIO device consumers |
| 2263 | * @table: array of consumers to register |
| 2264 | * @num: number of consumers in table |
| 2265 | */ |
| 2266 | void gpiod_add_table(struct gpiod_lookup *table, size_t size) |
| 2267 | { |
| 2268 | mutex_lock(&gpio_lookup_lock); |
| 2269 | |
| 2270 | while (size--) { |
| 2271 | list_add_tail(&table->list, &gpio_lookup_list); |
| 2272 | table++; |
| 2273 | } |
| 2274 | |
| 2275 | mutex_unlock(&gpio_lookup_lock); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2276 | } |
| 2277 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2278 | #ifdef CONFIG_OF |
| 2279 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2280 | unsigned int idx, |
| 2281 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2282 | { |
| 2283 | char prop_name[32]; /* 32 is max size of property name */ |
| 2284 | enum of_gpio_flags of_flags; |
| 2285 | struct gpio_desc *desc; |
| 2286 | |
| 2287 | if (con_id) |
| 2288 | snprintf(prop_name, 32, "%s-gpios", con_id); |
| 2289 | else |
| 2290 | snprintf(prop_name, 32, "gpios"); |
| 2291 | |
| 2292 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
| 2293 | &of_flags); |
| 2294 | |
| 2295 | if (IS_ERR(desc)) |
| 2296 | return desc; |
| 2297 | |
| 2298 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2299 | *flags |= GPIO_ACTIVE_LOW; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2300 | |
| 2301 | return desc; |
| 2302 | } |
| 2303 | #else |
| 2304 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
Alexandre Courbot | 209e64f | 2013-11-19 10:37:29 +0900 | [diff] [blame] | 2305 | unsigned int idx, |
| 2306 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2307 | { |
| 2308 | return ERR_PTR(-ENODEV); |
| 2309 | } |
| 2310 | #endif |
| 2311 | |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2312 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2313 | unsigned int idx, |
| 2314 | enum gpio_lookup_flags *flags) |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2315 | { |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 2316 | struct acpi_gpio_info info; |
| 2317 | struct gpio_desc *desc; |
| 2318 | |
| 2319 | desc = acpi_get_gpiod_by_index(dev, idx, &info); |
| 2320 | if (IS_ERR(desc)) |
| 2321 | return desc; |
| 2322 | |
| 2323 | if (info.gpioint && info.active_low) |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2324 | *flags |= GPIO_ACTIVE_LOW; |
Mika Westerberg | e01f440 | 2013-10-10 11:01:10 +0300 | [diff] [blame] | 2325 | |
| 2326 | return desc; |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2327 | } |
| 2328 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2329 | 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] | 2330 | unsigned int idx, |
| 2331 | enum gpio_lookup_flags *flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2332 | { |
| 2333 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 2334 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
| 2335 | unsigned int match, best = 0; |
| 2336 | struct gpiod_lookup *p; |
| 2337 | |
| 2338 | mutex_lock(&gpio_lookup_lock); |
| 2339 | |
| 2340 | list_for_each_entry(p, &gpio_lookup_list, list) { |
| 2341 | match = 0; |
| 2342 | |
| 2343 | if (p->dev_id) { |
| 2344 | if (!dev_id || strcmp(p->dev_id, dev_id)) |
| 2345 | continue; |
| 2346 | |
| 2347 | match += 2; |
| 2348 | } |
| 2349 | |
| 2350 | if (p->con_id) { |
| 2351 | if (!con_id || strcmp(p->con_id, con_id)) |
| 2352 | continue; |
| 2353 | |
| 2354 | match += 1; |
| 2355 | } |
| 2356 | |
| 2357 | if (p->idx != idx) |
| 2358 | continue; |
| 2359 | |
| 2360 | if (match > best) { |
| 2361 | struct gpio_chip *chip; |
| 2362 | |
| 2363 | chip = find_chip_by_name(p->chip_label); |
| 2364 | |
| 2365 | if (!chip) { |
| 2366 | dev_warn(dev, "cannot find GPIO chip %s\n", |
| 2367 | p->chip_label); |
| 2368 | continue; |
| 2369 | } |
| 2370 | |
Alexandre Courbot | 7cc67b9 | 2013-11-23 14:55:52 +0900 | [diff] [blame] | 2371 | if (chip->ngpio <= p->chip_hwnum) { |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2372 | dev_warn(dev, "GPIO chip %s has %d GPIOs\n", |
| 2373 | chip->label, chip->ngpio); |
| 2374 | continue; |
| 2375 | } |
| 2376 | |
| 2377 | desc = gpio_to_desc(chip->base + p->chip_hwnum); |
| 2378 | *flags = p->flags; |
| 2379 | |
| 2380 | if (match != 3) |
| 2381 | best = match; |
| 2382 | else |
| 2383 | break; |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | mutex_unlock(&gpio_lookup_lock); |
| 2388 | |
| 2389 | return desc; |
| 2390 | } |
| 2391 | |
| 2392 | /** |
| 2393 | * gpio_get - obtain a GPIO for a given GPIO function |
| 2394 | * @dev: GPIO consumer |
| 2395 | * @con_id: function within the GPIO consumer |
| 2396 | * |
| 2397 | * Return the GPIO descriptor corresponding to the function con_id of device |
| 2398 | * dev, or an IS_ERR() condition if an error occured. |
| 2399 | */ |
| 2400 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) |
| 2401 | { |
| 2402 | return gpiod_get_index(dev, con_id, 0); |
| 2403 | } |
| 2404 | EXPORT_SYMBOL_GPL(gpiod_get); |
| 2405 | |
| 2406 | /** |
| 2407 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
| 2408 | * @dev: GPIO consumer |
| 2409 | * @con_id: function within the GPIO consumer |
| 2410 | * @idx: index of the GPIO to obtain in the consumer |
| 2411 | * |
| 2412 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 2413 | * defined one for functions that define several GPIOs. |
| 2414 | * |
| 2415 | * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error. |
| 2416 | */ |
| 2417 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 2418 | const char *con_id, |
| 2419 | unsigned int idx) |
| 2420 | { |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 2421 | struct gpio_desc *desc = NULL; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2422 | int status; |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2423 | enum gpio_lookup_flags flags = 0; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2424 | |
| 2425 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 2426 | |
| 2427 | /* Using device tree? */ |
| 2428 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { |
| 2429 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 2430 | desc = of_find_gpio(dev, con_id, idx, &flags); |
Mika Westerberg | 81f59e9 | 2013-10-10 11:01:09 +0300 | [diff] [blame] | 2431 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { |
| 2432 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
| 2433 | desc = acpi_find_gpio(dev, con_id, idx, &flags); |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Either we are not using DT or ACPI, or their lookup did not return |
| 2438 | * a result. In that case, use platform lookup as a fallback. |
| 2439 | */ |
| 2440 | if (!desc || IS_ERR(desc)) { |
| 2441 | struct gpio_desc *pdesc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2442 | dev_dbg(dev, "using lookup tables for GPIO lookup"); |
Alexandre Courbot | 35c5d7f | 2013-11-23 19:34:50 +0900 | [diff] [blame] | 2443 | pdesc = gpiod_find(dev, con_id, idx, &flags); |
| 2444 | /* If used as fallback, do not replace the previous error */ |
| 2445 | if (!IS_ERR(pdesc) || !desc) |
| 2446 | desc = pdesc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | if (IS_ERR(desc)) { |
Heikki Krogerus | 351cfe0 | 2013-11-29 15:47:34 +0200 | [diff] [blame] | 2450 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2451 | return desc; |
| 2452 | } |
| 2453 | |
| 2454 | status = gpiod_request(desc, con_id); |
| 2455 | |
| 2456 | if (status < 0) |
| 2457 | return ERR_PTR(status); |
| 2458 | |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2459 | if (flags & GPIO_ACTIVE_LOW) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2460 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
Alexandre Courbot | 53e7cac | 2013-11-16 21:44:52 +0900 | [diff] [blame] | 2461 | if (flags & GPIO_OPEN_DRAIN) |
| 2462 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 2463 | if (flags & GPIO_OPEN_SOURCE) |
| 2464 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 2465 | |
| 2466 | return desc; |
| 2467 | } |
| 2468 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
| 2469 | |
| 2470 | /** |
| 2471 | * gpiod_put - dispose of a GPIO descriptor |
| 2472 | * @desc: GPIO descriptor to dispose of |
| 2473 | * |
| 2474 | * No descriptor can be used after gpiod_put() has been called on it. |
| 2475 | */ |
| 2476 | void gpiod_put(struct gpio_desc *desc) |
| 2477 | { |
| 2478 | gpiod_free(desc); |
| 2479 | } |
| 2480 | EXPORT_SYMBOL_GPL(gpiod_put); |
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 | #ifdef CONFIG_DEBUG_FS |
| 2483 | |
| 2484 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 2485 | { |
| 2486 | unsigned i; |
| 2487 | unsigned gpio = chip->base; |
Alexandre Courbot | 6c0b4e6 | 2013-02-03 01:29:30 +0900 | [diff] [blame] | 2488 | struct gpio_desc *gdesc = &chip->desc[0]; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2489 | int is_out; |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2490 | int is_irq; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2491 | |
| 2492 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
| 2493 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
| 2494 | continue; |
| 2495 | |
Alexandre Courbot | 372e722 | 2013-02-03 01:29:29 +0900 | [diff] [blame] | 2496 | gpiod_get_direction(gdesc); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2497 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2498 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
| 2499 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2500 | gpio, gdesc->label, |
| 2501 | is_out ? "out" : "in ", |
| 2502 | chip->get |
| 2503 | ? (chip->get(chip, i) ? "hi" : "lo") |
Linus Walleij | d468bf9 | 2013-09-24 11:54:38 +0200 | [diff] [blame] | 2504 | : "? ", |
| 2505 | is_irq ? "IRQ" : " "); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2506 | seq_printf(s, "\n"); |
| 2507 | } |
| 2508 | } |
| 2509 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2510 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2511 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2512 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2513 | struct gpio_chip *chip = NULL; |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2514 | loff_t index = *pos; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2515 | |
| 2516 | s->private = ""; |
| 2517 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2518 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2519 | list_for_each_entry(chip, &gpio_chips, list) |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2520 | if (index-- == 0) { |
| 2521 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2522 | return chip; |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2523 | } |
| 2524 | spin_unlock_irqrestore(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2525 | |
| 2526 | return NULL; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2530 | { |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2531 | unsigned long flags; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2532 | struct gpio_chip *chip = v; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2533 | void *ret = NULL; |
| 2534 | |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2535 | spin_lock_irqsave(&gpio_lock, flags); |
Alexandre Courbot | cb1650d | 2013-02-03 01:29:27 +0900 | [diff] [blame] | 2536 | if (list_is_last(&chip->list, &gpio_chips)) |
| 2537 | ret = NULL; |
| 2538 | else |
| 2539 | ret = list_entry(chip->list.next, struct gpio_chip, list); |
Grant Likely | 362432a | 2013-02-09 09:41:49 +0000 | [diff] [blame] | 2540 | spin_unlock_irqrestore(&gpio_lock, flags); |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2541 | |
| 2542 | s->private = "\n"; |
| 2543 | ++*pos; |
| 2544 | |
| 2545 | return ret; |
| 2546 | } |
| 2547 | |
| 2548 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 2549 | { |
| 2550 | } |
| 2551 | |
| 2552 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 2553 | { |
| 2554 | struct gpio_chip *chip = v; |
| 2555 | struct device *dev; |
| 2556 | |
| 2557 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
| 2558 | chip->base, chip->base + chip->ngpio - 1); |
| 2559 | dev = chip->dev; |
| 2560 | if (dev) |
| 2561 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
| 2562 | dev_name(dev)); |
| 2563 | if (chip->label) |
| 2564 | seq_printf(s, ", %s", chip->label); |
| 2565 | if (chip->can_sleep) |
| 2566 | seq_printf(s, ", can sleep"); |
| 2567 | seq_printf(s, ":\n"); |
| 2568 | |
| 2569 | if (chip->dbg_show) |
| 2570 | chip->dbg_show(s, chip); |
| 2571 | else |
| 2572 | gpiolib_dbg_show(s, chip); |
| 2573 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2574 | return 0; |
| 2575 | } |
| 2576 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2577 | static const struct seq_operations gpiolib_seq_ops = { |
| 2578 | .start = gpiolib_seq_start, |
| 2579 | .next = gpiolib_seq_next, |
| 2580 | .stop = gpiolib_seq_stop, |
| 2581 | .show = gpiolib_seq_show, |
| 2582 | }; |
| 2583 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2584 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 2585 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2586 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2587 | } |
| 2588 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2589 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2590 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2591 | .open = gpiolib_open, |
| 2592 | .read = seq_read, |
| 2593 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 2594 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 2595 | }; |
| 2596 | |
| 2597 | static int __init gpiolib_debugfs_init(void) |
| 2598 | { |
| 2599 | /* /sys/kernel/debug/gpio */ |
| 2600 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 2601 | NULL, NULL, &gpiolib_operations); |
| 2602 | return 0; |
| 2603 | } |
| 2604 | subsys_initcall(gpiolib_debugfs_init); |
| 2605 | |
| 2606 | #endif /* DEBUG_FS */ |