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