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