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> |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 6 | #include <linux/device.h> |
| 7 | #include <linux/err.h> |
| 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/seq_file.h> |
| 10 | #include <linux/gpio.h> |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 11 | #include <linux/of_gpio.h> |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 12 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 14 | |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 15 | #define CREATE_TRACE_POINTS |
| 16 | #include <trace/events/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 17 | |
| 18 | /* Optional implementation infrastructure for GPIO interfaces. |
| 19 | * |
| 20 | * Platforms may want to use this if they tend to use very many GPIOs |
| 21 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. |
| 22 | * |
| 23 | * When kernel footprint or instruction count is an issue, simpler |
| 24 | * implementations may be preferred. The GPIO programming interface |
| 25 | * allows for inlining speed-critical get/set operations for common |
| 26 | * cases, so that access to SOC-integrated GPIOs can sometimes cost |
| 27 | * only an instruction or two per bit. |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | /* When debugging, extend minimal trust to callers and platform code. |
| 32 | * Also emit diagnostic messages that may help initial bringup, when |
| 33 | * board setup or driver bugs are most common. |
| 34 | * |
| 35 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 36 | */ |
| 37 | #ifdef DEBUG |
| 38 | #define extra_checks 1 |
| 39 | #else |
| 40 | #define extra_checks 0 |
| 41 | #endif |
| 42 | |
| 43 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 44 | * While any GPIO is requested, its gpio_chip is not removable; |
| 45 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 46 | */ |
| 47 | static DEFINE_SPINLOCK(gpio_lock); |
| 48 | |
| 49 | struct gpio_desc { |
| 50 | struct gpio_chip *chip; |
| 51 | unsigned long flags; |
| 52 | /* flag symbols are bit numbers */ |
| 53 | #define FLAG_REQUESTED 0 |
| 54 | #define FLAG_IS_OUT 1 |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 55 | #define FLAG_RESERVED 2 |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 56 | #define FLAG_EXPORT 3 /* protected by sysfs_lock */ |
| 57 | #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 58 | #define FLAG_TRIG_FALL 5 /* trigger on falling edge */ |
| 59 | #define FLAG_TRIG_RISE 6 /* trigger on rising edge */ |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 60 | #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */ |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 61 | #define FLAG_OPEN_DRAIN 8 /* Gpio is open drain type */ |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 62 | #define FLAG_OPEN_SOURCE 9 /* Gpio is open source type */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 63 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 64 | #define ID_SHIFT 16 /* add new flags before this one */ |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 65 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 66 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 67 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 68 | |
| 69 | #ifdef CONFIG_DEBUG_FS |
| 70 | const char *label; |
| 71 | #endif |
| 72 | }; |
| 73 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
| 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 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 79 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 80 | { |
| 81 | #ifdef CONFIG_DEBUG_FS |
| 82 | d->label = label; |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
| 87 | * when setting direction, and otherwise illegal. Until board setup code |
| 88 | * and drivers use explicit requests everywhere (which won't happen when |
| 89 | * those calls have no teeth) we can't avoid autorequesting. This nag |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 90 | * message should motivate switching to explicit requests... so should |
| 91 | * the weaker cleanup after faults, compared to gpio_request(). |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 92 | * |
| 93 | * NOTE: the autorequest mechanism is going away; at this point it's |
| 94 | * only "legal" in the sense that (old) code using it won't break yet, |
| 95 | * but instead only triggers a WARN() stack dump. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 96 | */ |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 97 | static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 98 | { |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 99 | const struct gpio_chip *chip = desc->chip; |
| 100 | const int gpio = chip->base + offset; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 101 | |
David Brownell | 8a0cecf | 2009-04-02 16:57:06 -0700 | [diff] [blame] | 102 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
| 103 | "autorequest GPIO-%d\n", gpio)) { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 104 | if (!try_module_get(chip->owner)) { |
| 105 | pr_err("GPIO-%d: module can't be gotten \n", gpio); |
| 106 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 107 | /* lose */ |
| 108 | return -EIO; |
| 109 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 110 | desc_set_label(desc, "[auto]"); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 111 | /* caller must chip->request() w/o spinlock */ |
| 112 | if (chip->request) |
| 113 | return 1; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 114 | } |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 115 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* caller holds gpio_lock *OR* gpio is marked as requested */ |
Grant Likely | 1a2d397 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 119 | struct gpio_chip *gpio_to_chip(unsigned gpio) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 120 | { |
| 121 | return gpio_desc[gpio].chip; |
| 122 | } |
| 123 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 124 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 125 | static int gpiochip_find_base(int ngpio) |
| 126 | { |
| 127 | int i; |
| 128 | int spare = 0; |
| 129 | int base = -ENOSPC; |
| 130 | |
| 131 | for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) { |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 132 | struct gpio_desc *desc = &gpio_desc[i]; |
| 133 | struct gpio_chip *chip = desc->chip; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 134 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 135 | if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 136 | spare++; |
| 137 | if (spare == ngpio) { |
| 138 | base = i; |
| 139 | break; |
| 140 | } |
| 141 | } else { |
| 142 | spare = 0; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 143 | if (chip) |
| 144 | i -= chip->ngpio - 1; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | if (gpio_is_valid(base)) |
| 149 | pr_debug("%s: found new base at %d\n", __func__, base); |
| 150 | return base; |
| 151 | } |
| 152 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 153 | /** |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 154 | * gpiochip_reserve() - reserve range of gpios to use with platform code only |
| 155 | * @start: starting gpio number |
| 156 | * @ngpio: number of gpios to reserve |
| 157 | * Context: platform init, potentially before irqs or kmalloc will work |
| 158 | * |
| 159 | * Returns a negative errno if any gpio within the range is already reserved |
| 160 | * or registered, else returns zero as a success code. Use this function |
| 161 | * to mark a range of gpios as unavailable for dynamic gpio number allocation, |
| 162 | * for example because its driver support is not yet loaded. |
| 163 | */ |
| 164 | int __init gpiochip_reserve(int start, int ngpio) |
| 165 | { |
| 166 | int ret = 0; |
| 167 | unsigned long flags; |
| 168 | int i; |
| 169 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 170 | if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1)) |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 171 | return -EINVAL; |
| 172 | |
| 173 | spin_lock_irqsave(&gpio_lock, flags); |
| 174 | |
| 175 | for (i = start; i < start + ngpio; i++) { |
| 176 | struct gpio_desc *desc = &gpio_desc[i]; |
| 177 | |
| 178 | if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) { |
| 179 | ret = -EBUSY; |
| 180 | goto err; |
| 181 | } |
| 182 | |
| 183 | set_bit(FLAG_RESERVED, &desc->flags); |
| 184 | } |
| 185 | |
| 186 | pr_debug("%s: reserved gpios from %d to %d\n", |
| 187 | __func__, start, start + ngpio - 1); |
| 188 | err: |
| 189 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 194 | /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ |
| 195 | static int gpio_get_direction(unsigned gpio) |
| 196 | { |
| 197 | struct gpio_chip *chip; |
| 198 | struct gpio_desc *desc = &gpio_desc[gpio]; |
| 199 | int status = -EINVAL; |
| 200 | |
| 201 | chip = gpio_to_chip(gpio); |
| 202 | gpio -= chip->base; |
| 203 | |
| 204 | if (!chip->get_direction) |
| 205 | return status; |
| 206 | |
| 207 | status = chip->get_direction(chip, gpio); |
| 208 | if (status > 0) { |
| 209 | /* GPIOF_DIR_IN, or other positive */ |
| 210 | status = 1; |
| 211 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 212 | } |
| 213 | if (status == 0) { |
| 214 | /* GPIOF_DIR_OUT */ |
| 215 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 216 | } |
| 217 | return status; |
| 218 | } |
| 219 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 220 | #ifdef CONFIG_GPIO_SYSFS |
| 221 | |
| 222 | /* lock protects against unexport_gpio() being called while |
| 223 | * sysfs files are active. |
| 224 | */ |
| 225 | static DEFINE_MUTEX(sysfs_lock); |
| 226 | |
| 227 | /* |
| 228 | * /sys/class/gpio/gpioN... only for GPIOs that are exported |
| 229 | * /direction |
| 230 | * * MAY BE OMITTED if kernel won't allow direction changes |
| 231 | * * is read/write as "in" or "out" |
| 232 | * * may also be written as "high" or "low", initializing |
| 233 | * output value as specified ("out" implies "low") |
| 234 | * /value |
| 235 | * * always readable, subject to hardware behavior |
| 236 | * * may be writable, as zero/nonzero |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 237 | * /edge |
| 238 | * * configures behavior of poll(2) on /value |
| 239 | * * available only if pin can generate IRQs on input |
| 240 | * * is read/write as "none", "falling", "rising", or "both" |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 241 | * /active_low |
| 242 | * * configures polarity of /value |
| 243 | * * is read/write as zero/nonzero |
| 244 | * * also affects existing and subsequent "falling" and "rising" |
| 245 | * /edge configuration |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 246 | */ |
| 247 | |
| 248 | static ssize_t gpio_direction_show(struct device *dev, |
| 249 | struct device_attribute *attr, char *buf) |
| 250 | { |
| 251 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 252 | unsigned gpio = desc - gpio_desc; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 253 | ssize_t status; |
| 254 | |
| 255 | mutex_lock(&sysfs_lock); |
| 256 | |
| 257 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 258 | status = -EIO; |
| 259 | else |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 260 | gpio_get_direction(gpio); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 261 | status = sprintf(buf, "%s\n", |
| 262 | test_bit(FLAG_IS_OUT, &desc->flags) |
| 263 | ? "out" : "in"); |
| 264 | |
| 265 | mutex_unlock(&sysfs_lock); |
| 266 | return status; |
| 267 | } |
| 268 | |
| 269 | static ssize_t gpio_direction_store(struct device *dev, |
| 270 | struct device_attribute *attr, const char *buf, size_t size) |
| 271 | { |
| 272 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 273 | unsigned gpio = desc - gpio_desc; |
| 274 | ssize_t status; |
| 275 | |
| 276 | mutex_lock(&sysfs_lock); |
| 277 | |
| 278 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 279 | status = -EIO; |
| 280 | else if (sysfs_streq(buf, "high")) |
| 281 | status = gpio_direction_output(gpio, 1); |
| 282 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
| 283 | status = gpio_direction_output(gpio, 0); |
| 284 | else if (sysfs_streq(buf, "in")) |
| 285 | status = gpio_direction_input(gpio); |
| 286 | else |
| 287 | status = -EINVAL; |
| 288 | |
| 289 | mutex_unlock(&sysfs_lock); |
| 290 | return status ? : size; |
| 291 | } |
| 292 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 293 | static /* const */ DEVICE_ATTR(direction, 0644, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 294 | gpio_direction_show, gpio_direction_store); |
| 295 | |
| 296 | static ssize_t gpio_value_show(struct device *dev, |
| 297 | struct device_attribute *attr, char *buf) |
| 298 | { |
| 299 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 300 | unsigned gpio = desc - gpio_desc; |
| 301 | ssize_t status; |
| 302 | |
| 303 | mutex_lock(&sysfs_lock); |
| 304 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 305 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 306 | status = -EIO; |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 307 | } else { |
| 308 | int value; |
| 309 | |
| 310 | value = !!gpio_get_value_cansleep(gpio); |
| 311 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 312 | value = !value; |
| 313 | |
| 314 | status = sprintf(buf, "%d\n", value); |
| 315 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 316 | |
| 317 | mutex_unlock(&sysfs_lock); |
| 318 | return status; |
| 319 | } |
| 320 | |
| 321 | static ssize_t gpio_value_store(struct device *dev, |
| 322 | struct device_attribute *attr, const char *buf, size_t size) |
| 323 | { |
| 324 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 325 | unsigned gpio = desc - gpio_desc; |
| 326 | ssize_t status; |
| 327 | |
| 328 | mutex_lock(&sysfs_lock); |
| 329 | |
| 330 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 331 | status = -EIO; |
| 332 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) |
| 333 | status = -EPERM; |
| 334 | else { |
| 335 | long value; |
| 336 | |
| 337 | status = strict_strtol(buf, 0, &value); |
| 338 | if (status == 0) { |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 339 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 340 | value = !value; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 341 | gpio_set_value_cansleep(gpio, value != 0); |
| 342 | status = size; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | mutex_unlock(&sysfs_lock); |
| 347 | return status; |
| 348 | } |
| 349 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 350 | static const DEVICE_ATTR(value, 0644, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 351 | gpio_value_show, gpio_value_store); |
| 352 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 353 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
| 354 | { |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 355 | struct sysfs_dirent *value_sd = priv; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 356 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 357 | sysfs_notify_dirent(value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 358 | return IRQ_HANDLED; |
| 359 | } |
| 360 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 361 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
| 362 | unsigned long gpio_flags) |
| 363 | { |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 364 | struct sysfs_dirent *value_sd; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 365 | unsigned long irq_flags; |
| 366 | int ret, irq, id; |
| 367 | |
| 368 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) |
| 369 | return 0; |
| 370 | |
| 371 | irq = gpio_to_irq(desc - gpio_desc); |
| 372 | if (irq < 0) |
| 373 | return -EIO; |
| 374 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 375 | id = desc->flags >> ID_SHIFT; |
| 376 | value_sd = idr_find(&dirent_idr, id); |
| 377 | if (value_sd) |
| 378 | free_irq(irq, value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 379 | |
| 380 | desc->flags &= ~GPIO_TRIGGER_MASK; |
| 381 | |
| 382 | if (!gpio_flags) { |
| 383 | ret = 0; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 384 | goto free_id; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | irq_flags = IRQF_SHARED; |
| 388 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 389 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 390 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 391 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 392 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
| 393 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 394 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 395 | if (!value_sd) { |
| 396 | value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value"); |
| 397 | if (!value_sd) { |
| 398 | ret = -ENODEV; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 399 | goto err_out; |
| 400 | } |
| 401 | |
| 402 | do { |
| 403 | ret = -ENOMEM; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 404 | if (idr_pre_get(&dirent_idr, GFP_KERNEL)) |
| 405 | ret = idr_get_new_above(&dirent_idr, value_sd, |
| 406 | 1, &id); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 407 | } while (ret == -EAGAIN); |
| 408 | |
| 409 | if (ret) |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 410 | goto free_sd; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 411 | |
| 412 | desc->flags &= GPIO_FLAGS_MASK; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 413 | desc->flags |= (unsigned long)id << ID_SHIFT; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 414 | |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 415 | if (desc->flags >> ID_SHIFT != id) { |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 416 | ret = -ERANGE; |
| 417 | goto free_id; |
| 418 | } |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Daniel Gl?ckner | 364fadb3 | 2010-08-10 18:02:26 -0700 | [diff] [blame] | 421 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 422 | "gpiolib", value_sd); |
Daniel Gl?ckner | 364fadb3 | 2010-08-10 18:02:26 -0700 | [diff] [blame] | 423 | if (ret < 0) |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 424 | goto free_id; |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 425 | |
| 426 | desc->flags |= gpio_flags; |
| 427 | return 0; |
| 428 | |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 429 | free_id: |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 430 | idr_remove(&dirent_idr, id); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 431 | desc->flags &= GPIO_FLAGS_MASK; |
Daniel Gl?ckner | 5ba1821 | 2010-08-10 18:02:25 -0700 | [diff] [blame] | 432 | free_sd: |
| 433 | if (value_sd) |
| 434 | sysfs_put(value_sd); |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 435 | err_out: |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static const struct { |
| 440 | const char *name; |
| 441 | unsigned long flags; |
| 442 | } trigger_types[] = { |
| 443 | { "none", 0 }, |
| 444 | { "falling", BIT(FLAG_TRIG_FALL) }, |
| 445 | { "rising", BIT(FLAG_TRIG_RISE) }, |
| 446 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, |
| 447 | }; |
| 448 | |
| 449 | static ssize_t gpio_edge_show(struct device *dev, |
| 450 | struct device_attribute *attr, char *buf) |
| 451 | { |
| 452 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 453 | ssize_t status; |
| 454 | |
| 455 | mutex_lock(&sysfs_lock); |
| 456 | |
| 457 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 458 | status = -EIO; |
| 459 | else { |
| 460 | int i; |
| 461 | |
| 462 | status = 0; |
| 463 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 464 | if ((desc->flags & GPIO_TRIGGER_MASK) |
| 465 | == trigger_types[i].flags) { |
| 466 | status = sprintf(buf, "%s\n", |
| 467 | trigger_types[i].name); |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | mutex_unlock(&sysfs_lock); |
| 473 | return status; |
| 474 | } |
| 475 | |
| 476 | static ssize_t gpio_edge_store(struct device *dev, |
| 477 | struct device_attribute *attr, const char *buf, size_t size) |
| 478 | { |
| 479 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 480 | ssize_t status; |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) |
| 484 | if (sysfs_streq(trigger_types[i].name, buf)) |
| 485 | goto found; |
| 486 | return -EINVAL; |
| 487 | |
| 488 | found: |
| 489 | mutex_lock(&sysfs_lock); |
| 490 | |
| 491 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 492 | status = -EIO; |
| 493 | else { |
| 494 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); |
| 495 | if (!status) |
| 496 | status = size; |
| 497 | } |
| 498 | |
| 499 | mutex_unlock(&sysfs_lock); |
| 500 | |
| 501 | return status; |
| 502 | } |
| 503 | |
| 504 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); |
| 505 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 506 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
| 507 | int value) |
| 508 | { |
| 509 | int status = 0; |
| 510 | |
| 511 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) |
| 512 | return 0; |
| 513 | |
| 514 | if (value) |
| 515 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 516 | else |
| 517 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 518 | |
| 519 | /* reconfigure poll(2) support if enabled on one edge only */ |
| 520 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ |
| 521 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { |
| 522 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; |
| 523 | |
| 524 | gpio_setup_irq(desc, dev, 0); |
| 525 | status = gpio_setup_irq(desc, dev, trigger_flags); |
| 526 | } |
| 527 | |
| 528 | return status; |
| 529 | } |
| 530 | |
| 531 | static ssize_t gpio_active_low_show(struct device *dev, |
| 532 | struct device_attribute *attr, char *buf) |
| 533 | { |
| 534 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 535 | ssize_t status; |
| 536 | |
| 537 | mutex_lock(&sysfs_lock); |
| 538 | |
| 539 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 540 | status = -EIO; |
| 541 | else |
| 542 | status = sprintf(buf, "%d\n", |
| 543 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); |
| 544 | |
| 545 | mutex_unlock(&sysfs_lock); |
| 546 | |
| 547 | return status; |
| 548 | } |
| 549 | |
| 550 | static ssize_t gpio_active_low_store(struct device *dev, |
| 551 | struct device_attribute *attr, const char *buf, size_t size) |
| 552 | { |
| 553 | struct gpio_desc *desc = dev_get_drvdata(dev); |
| 554 | ssize_t status; |
| 555 | |
| 556 | mutex_lock(&sysfs_lock); |
| 557 | |
| 558 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
| 559 | status = -EIO; |
| 560 | } else { |
| 561 | long value; |
| 562 | |
| 563 | status = strict_strtol(buf, 0, &value); |
| 564 | if (status == 0) |
| 565 | status = sysfs_set_active_low(desc, dev, value != 0); |
| 566 | } |
| 567 | |
| 568 | mutex_unlock(&sysfs_lock); |
| 569 | |
| 570 | return status ? : size; |
| 571 | } |
| 572 | |
| 573 | static const DEVICE_ATTR(active_low, 0644, |
| 574 | gpio_active_low_show, gpio_active_low_store); |
| 575 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 576 | static const struct attribute *gpio_attrs[] = { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 577 | &dev_attr_value.attr, |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 578 | &dev_attr_active_low.attr, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 579 | NULL, |
| 580 | }; |
| 581 | |
| 582 | static const struct attribute_group gpio_attr_group = { |
| 583 | .attrs = (struct attribute **) gpio_attrs, |
| 584 | }; |
| 585 | |
| 586 | /* |
| 587 | * /sys/class/gpio/gpiochipN/ |
| 588 | * /base ... matching gpio_chip.base (N) |
| 589 | * /label ... matching gpio_chip.label |
| 590 | * /ngpio ... matching gpio_chip.ngpio |
| 591 | */ |
| 592 | |
| 593 | static ssize_t chip_base_show(struct device *dev, |
| 594 | struct device_attribute *attr, char *buf) |
| 595 | { |
| 596 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 597 | |
| 598 | return sprintf(buf, "%d\n", chip->base); |
| 599 | } |
| 600 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); |
| 601 | |
| 602 | static ssize_t chip_label_show(struct device *dev, |
| 603 | struct device_attribute *attr, char *buf) |
| 604 | { |
| 605 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 606 | |
| 607 | return sprintf(buf, "%s\n", chip->label ? : ""); |
| 608 | } |
| 609 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); |
| 610 | |
| 611 | static ssize_t chip_ngpio_show(struct device *dev, |
| 612 | struct device_attribute *attr, char *buf) |
| 613 | { |
| 614 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 615 | |
| 616 | return sprintf(buf, "%u\n", chip->ngpio); |
| 617 | } |
| 618 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
| 619 | |
| 620 | static const struct attribute *gpiochip_attrs[] = { |
| 621 | &dev_attr_base.attr, |
| 622 | &dev_attr_label.attr, |
| 623 | &dev_attr_ngpio.attr, |
| 624 | NULL, |
| 625 | }; |
| 626 | |
| 627 | static const struct attribute_group gpiochip_attr_group = { |
| 628 | .attrs = (struct attribute **) gpiochip_attrs, |
| 629 | }; |
| 630 | |
| 631 | /* |
| 632 | * /sys/class/gpio/export ... write-only |
| 633 | * integer N ... number of GPIO to export (full access) |
| 634 | * /sys/class/gpio/unexport ... write-only |
| 635 | * integer N ... number of GPIO to unexport |
| 636 | */ |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 637 | static ssize_t export_store(struct class *class, |
| 638 | struct class_attribute *attr, |
| 639 | const char *buf, size_t len) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 640 | { |
| 641 | long gpio; |
| 642 | int status; |
| 643 | |
| 644 | status = strict_strtol(buf, 0, &gpio); |
| 645 | if (status < 0) |
| 646 | goto done; |
| 647 | |
| 648 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 649 | * request and export were done by on behalf of userspace, so |
| 650 | * they may be undone on its behalf too. |
| 651 | */ |
| 652 | |
| 653 | status = gpio_request(gpio, "sysfs"); |
| 654 | if (status < 0) |
| 655 | goto done; |
| 656 | |
| 657 | status = gpio_export(gpio, true); |
| 658 | if (status < 0) |
| 659 | gpio_free(gpio); |
| 660 | else |
| 661 | set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags); |
| 662 | |
| 663 | done: |
| 664 | if (status) |
| 665 | pr_debug("%s: status %d\n", __func__, status); |
| 666 | return status ? : len; |
| 667 | } |
| 668 | |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 669 | static ssize_t unexport_store(struct class *class, |
| 670 | struct class_attribute *attr, |
| 671 | const char *buf, size_t len) |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 672 | { |
| 673 | long gpio; |
| 674 | int status; |
| 675 | |
| 676 | status = strict_strtol(buf, 0, &gpio); |
| 677 | if (status < 0) |
| 678 | goto done; |
| 679 | |
| 680 | status = -EINVAL; |
| 681 | |
| 682 | /* reject bogus commands (gpio_unexport ignores them) */ |
| 683 | if (!gpio_is_valid(gpio)) |
| 684 | goto done; |
| 685 | |
| 686 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 687 | * request and export were done by on behalf of userspace, so |
| 688 | * they may be undone on its behalf too. |
| 689 | */ |
| 690 | if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) { |
| 691 | status = 0; |
| 692 | gpio_free(gpio); |
| 693 | } |
| 694 | done: |
| 695 | if (status) |
| 696 | pr_debug("%s: status %d\n", __func__, status); |
| 697 | return status ? : len; |
| 698 | } |
| 699 | |
| 700 | static struct class_attribute gpio_class_attrs[] = { |
| 701 | __ATTR(export, 0200, NULL, export_store), |
| 702 | __ATTR(unexport, 0200, NULL, unexport_store), |
| 703 | __ATTR_NULL, |
| 704 | }; |
| 705 | |
| 706 | static struct class gpio_class = { |
| 707 | .name = "gpio", |
| 708 | .owner = THIS_MODULE, |
| 709 | |
| 710 | .class_attrs = gpio_class_attrs, |
| 711 | }; |
| 712 | |
| 713 | |
| 714 | /** |
| 715 | * gpio_export - export a GPIO through sysfs |
| 716 | * @gpio: gpio to make available, already requested |
| 717 | * @direction_may_change: true if userspace may change gpio direction |
| 718 | * Context: arch_initcall or later |
| 719 | * |
| 720 | * When drivers want to make a GPIO accessible to userspace after they |
| 721 | * have requested it -- perhaps while debugging, or as part of their |
| 722 | * public interface -- they may use this routine. If the GPIO can |
| 723 | * change direction (some can't) and the caller allows it, userspace |
| 724 | * will see "direction" sysfs attribute which may be used to change |
| 725 | * the gpio's direction. A "value" attribute will always be provided. |
| 726 | * |
| 727 | * Returns zero on success, else an error. |
| 728 | */ |
| 729 | int gpio_export(unsigned gpio, bool direction_may_change) |
| 730 | { |
| 731 | unsigned long flags; |
| 732 | struct gpio_desc *desc; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 733 | int status; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 734 | const char *ioname = NULL; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 735 | struct device *dev; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 736 | |
| 737 | /* can't export until sysfs is available ... */ |
| 738 | if (!gpio_class.p) { |
| 739 | pr_debug("%s: called too early!\n", __func__); |
| 740 | return -ENOENT; |
| 741 | } |
| 742 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 743 | if (!gpio_is_valid(gpio)) { |
| 744 | pr_debug("%s: gpio %d is not valid\n", __func__, gpio); |
| 745 | return -EINVAL; |
| 746 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 747 | |
| 748 | mutex_lock(&sysfs_lock); |
| 749 | |
| 750 | spin_lock_irqsave(&gpio_lock, flags); |
| 751 | desc = &gpio_desc[gpio]; |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 752 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
| 753 | test_bit(FLAG_EXPORT, &desc->flags)) { |
| 754 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 755 | pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", |
| 756 | __func__, gpio, |
| 757 | test_bit(FLAG_REQUESTED, &desc->flags), |
| 758 | test_bit(FLAG_EXPORT, &desc->flags)); |
| 759 | return -EPERM; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 760 | } |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 761 | |
| 762 | if (!desc->chip->direction_input || !desc->chip->direction_output) |
| 763 | direction_may_change = false; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 764 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 765 | |
Daniel Silverstone | 926b663 | 2009-04-02 16:57:05 -0700 | [diff] [blame] | 766 | if (desc->chip->names && desc->chip->names[gpio - desc->chip->base]) |
| 767 | ioname = desc->chip->names[gpio - desc->chip->base]; |
| 768 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 769 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
| 770 | desc, ioname ? ioname : "gpio%u", gpio); |
| 771 | if (IS_ERR(dev)) { |
| 772 | status = PTR_ERR(dev); |
| 773 | goto fail_unlock; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 776 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 777 | if (status) |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 778 | goto fail_unregister_device; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 779 | |
Ryan Mallon | fc4e251 | 2012-10-22 11:39:12 +1100 | [diff] [blame] | 780 | if (direction_may_change) { |
| 781 | status = device_create_file(dev, &dev_attr_direction); |
| 782 | if (status) |
| 783 | goto fail_unregister_device; |
| 784 | } |
| 785 | |
| 786 | if (gpio_to_irq(gpio) >= 0 && (direction_may_change || |
| 787 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
| 788 | status = device_create_file(dev, &dev_attr_edge); |
| 789 | if (status) |
| 790 | goto fail_unregister_device; |
| 791 | } |
| 792 | |
| 793 | set_bit(FLAG_EXPORT, &desc->flags); |
| 794 | mutex_unlock(&sysfs_lock); |
| 795 | return 0; |
| 796 | |
| 797 | fail_unregister_device: |
| 798 | device_unregister(dev); |
| 799 | fail_unlock: |
| 800 | mutex_unlock(&sysfs_lock); |
| 801 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 802 | return status; |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(gpio_export); |
| 805 | |
| 806 | static int match_export(struct device *dev, void *data) |
| 807 | { |
| 808 | return dev_get_drvdata(dev) == data; |
| 809 | } |
| 810 | |
| 811 | /** |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 812 | * gpio_export_link - create a sysfs link to an exported GPIO node |
| 813 | * @dev: device under which to create symlink |
| 814 | * @name: name of the symlink |
| 815 | * @gpio: gpio to create symlink to, already exported |
| 816 | * |
| 817 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN |
| 818 | * node. Caller is responsible for unlinking. |
| 819 | * |
| 820 | * Returns zero on success, else an error. |
| 821 | */ |
| 822 | int gpio_export_link(struct device *dev, const char *name, unsigned gpio) |
| 823 | { |
| 824 | struct gpio_desc *desc; |
| 825 | int status = -EINVAL; |
| 826 | |
| 827 | if (!gpio_is_valid(gpio)) |
| 828 | goto done; |
| 829 | |
| 830 | mutex_lock(&sysfs_lock); |
| 831 | |
| 832 | desc = &gpio_desc[gpio]; |
| 833 | |
| 834 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 835 | struct device *tdev; |
| 836 | |
| 837 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 838 | if (tdev != NULL) { |
| 839 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, |
| 840 | name); |
| 841 | } else { |
| 842 | status = -ENODEV; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | mutex_unlock(&sysfs_lock); |
| 847 | |
| 848 | done: |
| 849 | if (status) |
| 850 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); |
| 851 | |
| 852 | return status; |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(gpio_export_link); |
| 855 | |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 856 | |
| 857 | /** |
| 858 | * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value |
| 859 | * @gpio: gpio to change |
| 860 | * @value: non-zero to use active low, i.e. inverted values |
| 861 | * |
| 862 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. |
| 863 | * The GPIO does not have to be exported yet. If poll(2) support has |
| 864 | * been enabled for either rising or falling edge, it will be |
| 865 | * reconfigured to follow the new polarity. |
| 866 | * |
| 867 | * Returns zero on success, else an error. |
| 868 | */ |
| 869 | int gpio_sysfs_set_active_low(unsigned gpio, int value) |
| 870 | { |
| 871 | struct gpio_desc *desc; |
| 872 | struct device *dev = NULL; |
| 873 | int status = -EINVAL; |
| 874 | |
| 875 | if (!gpio_is_valid(gpio)) |
| 876 | goto done; |
| 877 | |
| 878 | mutex_lock(&sysfs_lock); |
| 879 | |
| 880 | desc = &gpio_desc[gpio]; |
| 881 | |
| 882 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 883 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 884 | if (dev == NULL) { |
| 885 | status = -ENODEV; |
| 886 | goto unlock; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | status = sysfs_set_active_low(desc, dev, value); |
| 891 | |
| 892 | unlock: |
| 893 | mutex_unlock(&sysfs_lock); |
| 894 | |
| 895 | done: |
| 896 | if (status) |
| 897 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); |
| 898 | |
| 899 | return status; |
| 900 | } |
| 901 | EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); |
| 902 | |
Jani Nikula | a4177ee | 2009-09-22 16:46:33 -0700 | [diff] [blame] | 903 | /** |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 904 | * gpio_unexport - reverse effect of gpio_export() |
| 905 | * @gpio: gpio to make unavailable |
| 906 | * |
| 907 | * This is implicit on gpio_free(). |
| 908 | */ |
| 909 | void gpio_unexport(unsigned gpio) |
| 910 | { |
| 911 | struct gpio_desc *desc; |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 912 | int status = 0; |
Ming Lei | 864533c | 2012-02-13 22:53:20 +0800 | [diff] [blame] | 913 | struct device *dev = NULL; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 914 | |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 915 | if (!gpio_is_valid(gpio)) { |
| 916 | status = -EINVAL; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 917 | goto done; |
Jon Povey | 6a99ad4 | 2010-07-27 13:18:06 -0700 | [diff] [blame] | 918 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 919 | |
| 920 | mutex_lock(&sysfs_lock); |
| 921 | |
| 922 | desc = &gpio_desc[gpio]; |
Daniel Silverstone | 926b663 | 2009-04-02 16:57:05 -0700 | [diff] [blame] | 923 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 924 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 925 | |
| 926 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 927 | if (dev) { |
Daniel Glöckner | ff77c35 | 2009-09-22 16:46:38 -0700 | [diff] [blame] | 928 | gpio_setup_irq(desc, dev, 0); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 929 | clear_bit(FLAG_EXPORT, &desc->flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 930 | } else |
| 931 | status = -ENODEV; |
| 932 | } |
| 933 | |
| 934 | mutex_unlock(&sysfs_lock); |
Ming Lei | 864533c | 2012-02-13 22:53:20 +0800 | [diff] [blame] | 935 | if (dev) { |
| 936 | device_unregister(dev); |
| 937 | put_device(dev); |
| 938 | } |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 939 | done: |
| 940 | if (status) |
| 941 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); |
| 942 | } |
| 943 | EXPORT_SYMBOL_GPL(gpio_unexport); |
| 944 | |
| 945 | static int gpiochip_export(struct gpio_chip *chip) |
| 946 | { |
| 947 | int status; |
| 948 | struct device *dev; |
| 949 | |
| 950 | /* Many systems register gpio chips for SOC support very early, |
| 951 | * before driver model support is available. In those cases we |
| 952 | * export this later, in gpiolib_sysfs_init() ... here we just |
| 953 | * verify that _some_ field of gpio_class got initialized. |
| 954 | */ |
| 955 | if (!gpio_class.p) |
| 956 | return 0; |
| 957 | |
| 958 | /* use chip->base for the ID; it's already known to be unique */ |
| 959 | mutex_lock(&sysfs_lock); |
| 960 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, |
| 961 | "gpiochip%d", chip->base); |
Sergei Shtylyov | d62668e | 2009-11-11 14:26:50 -0800 | [diff] [blame] | 962 | if (!IS_ERR(dev)) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 963 | status = sysfs_create_group(&dev->kobj, |
| 964 | &gpiochip_attr_group); |
| 965 | } else |
Sergei Shtylyov | d62668e | 2009-11-11 14:26:50 -0800 | [diff] [blame] | 966 | status = PTR_ERR(dev); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 967 | chip->exported = (status == 0); |
| 968 | mutex_unlock(&sysfs_lock); |
| 969 | |
| 970 | if (status) { |
| 971 | unsigned long flags; |
| 972 | unsigned gpio; |
| 973 | |
| 974 | spin_lock_irqsave(&gpio_lock, flags); |
| 975 | gpio = chip->base; |
| 976 | while (gpio_desc[gpio].chip == chip) |
| 977 | gpio_desc[gpio++].chip = NULL; |
| 978 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 979 | |
| 980 | pr_debug("%s: chip %s status %d\n", __func__, |
| 981 | chip->label, status); |
| 982 | } |
| 983 | |
| 984 | return status; |
| 985 | } |
| 986 | |
| 987 | static void gpiochip_unexport(struct gpio_chip *chip) |
| 988 | { |
| 989 | int status; |
| 990 | struct device *dev; |
| 991 | |
| 992 | mutex_lock(&sysfs_lock); |
| 993 | dev = class_find_device(&gpio_class, NULL, chip, match_export); |
| 994 | if (dev) { |
| 995 | put_device(dev); |
| 996 | device_unregister(dev); |
| 997 | chip->exported = 0; |
| 998 | status = 0; |
| 999 | } else |
| 1000 | status = -ENODEV; |
| 1001 | mutex_unlock(&sysfs_lock); |
| 1002 | |
| 1003 | if (status) |
| 1004 | pr_debug("%s: chip %s status %d\n", __func__, |
| 1005 | chip->label, status); |
| 1006 | } |
| 1007 | |
| 1008 | static int __init gpiolib_sysfs_init(void) |
| 1009 | { |
| 1010 | int status; |
| 1011 | unsigned long flags; |
| 1012 | unsigned gpio; |
| 1013 | |
| 1014 | status = class_register(&gpio_class); |
| 1015 | if (status < 0) |
| 1016 | return status; |
| 1017 | |
| 1018 | /* Scan and register the gpio_chips which registered very |
| 1019 | * early (e.g. before the class_register above was called). |
| 1020 | * |
| 1021 | * We run before arch_initcall() so chip->dev nodes can have |
| 1022 | * registered, and so arch_initcall() can always gpio_export(). |
| 1023 | */ |
| 1024 | spin_lock_irqsave(&gpio_lock, flags); |
| 1025 | for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) { |
| 1026 | struct gpio_chip *chip; |
| 1027 | |
| 1028 | chip = gpio_desc[gpio].chip; |
| 1029 | if (!chip || chip->exported) |
| 1030 | continue; |
| 1031 | |
| 1032 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1033 | status = gpiochip_export(chip); |
| 1034 | spin_lock_irqsave(&gpio_lock, flags); |
| 1035 | } |
| 1036 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1037 | |
| 1038 | |
| 1039 | return status; |
| 1040 | } |
| 1041 | postcore_initcall(gpiolib_sysfs_init); |
| 1042 | |
| 1043 | #else |
| 1044 | static inline int gpiochip_export(struct gpio_chip *chip) |
| 1045 | { |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static inline void gpiochip_unexport(struct gpio_chip *chip) |
| 1050 | { |
| 1051 | } |
| 1052 | |
| 1053 | #endif /* CONFIG_GPIO_SYSFS */ |
| 1054 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 1055 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1056 | * gpiochip_add() - register a gpio_chip |
| 1057 | * @chip: the chip to register, with chip->base initialized |
| 1058 | * Context: potentially before irqs or kmalloc will work |
| 1059 | * |
| 1060 | * Returns a negative errno if the chip can't be registered, such as |
| 1061 | * because the chip->base is invalid or already associated with a |
| 1062 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1063 | * |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1064 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 1065 | * can be freely used, the chip->dev device must be registered before |
| 1066 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 1067 | * for GPIOs will fail rudely. |
| 1068 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1069 | * If chip->base is negative, this requests dynamic assignment of |
| 1070 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1071 | */ |
| 1072 | int gpiochip_add(struct gpio_chip *chip) |
| 1073 | { |
| 1074 | unsigned long flags; |
| 1075 | int status = 0; |
| 1076 | unsigned id; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1077 | int base = chip->base; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1078 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 1079 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1080 | && base >= 0) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1081 | status = -EINVAL; |
| 1082 | goto fail; |
| 1083 | } |
| 1084 | |
| 1085 | spin_lock_irqsave(&gpio_lock, flags); |
| 1086 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1087 | if (base < 0) { |
| 1088 | base = gpiochip_find_base(chip->ngpio); |
| 1089 | if (base < 0) { |
| 1090 | status = base; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1091 | goto unlock; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1092 | } |
| 1093 | chip->base = base; |
| 1094 | } |
| 1095 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1096 | /* these GPIO numbers must not be managed by another gpio_chip */ |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1097 | for (id = base; id < base + chip->ngpio; id++) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1098 | if (gpio_desc[id].chip != NULL) { |
| 1099 | status = -EBUSY; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | if (status == 0) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1104 | for (id = base; id < base + chip->ngpio; id++) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1105 | gpio_desc[id].chip = chip; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1106 | |
| 1107 | /* REVISIT: most hardware initializes GPIOs as |
| 1108 | * inputs (often with pullups enabled) so power |
| 1109 | * usage is minimized. Linux code should set the |
| 1110 | * gpio direction first thing; but until it does, |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 1111 | * and in case chip->get_direction is not set, |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1112 | * we may expose the wrong direction in sysfs. |
| 1113 | */ |
| 1114 | gpio_desc[id].flags = !chip->direction_input |
| 1115 | ? (1 << FLAG_IS_OUT) |
| 1116 | : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1120 | of_gpiochip_add(chip); |
| 1121 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1122 | unlock: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1123 | spin_unlock_irqrestore(&gpio_lock, flags); |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1124 | |
| 1125 | if (status) |
| 1126 | goto fail; |
| 1127 | |
| 1128 | status = gpiochip_export(chip); |
| 1129 | if (status) |
| 1130 | goto fail; |
| 1131 | |
H Hartley Sweeten | ee1c1e7 | 2012-05-11 16:58:33 -0700 | [diff] [blame] | 1132 | 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] | 1133 | chip->base, chip->base + chip->ngpio - 1, |
| 1134 | chip->label ? : "generic"); |
| 1135 | |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1136 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1137 | fail: |
| 1138 | /* failures here can mean systems won't boot... */ |
Anton Vorontsov | cedb188 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 1139 | pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", |
| 1140 | chip->base, chip->base + chip->ngpio - 1, |
| 1141 | chip->label ? : "generic"); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1142 | return status; |
| 1143 | } |
| 1144 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 1145 | |
| 1146 | /** |
| 1147 | * gpiochip_remove() - unregister a gpio_chip |
| 1148 | * @chip: the chip to unregister |
| 1149 | * |
| 1150 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 1151 | */ |
| 1152 | int gpiochip_remove(struct gpio_chip *chip) |
| 1153 | { |
| 1154 | unsigned long flags; |
| 1155 | int status = 0; |
| 1156 | unsigned id; |
| 1157 | |
| 1158 | spin_lock_irqsave(&gpio_lock, flags); |
| 1159 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 1160 | of_gpiochip_remove(chip); |
| 1161 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1162 | for (id = chip->base; id < chip->base + chip->ngpio; id++) { |
| 1163 | if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) { |
| 1164 | status = -EBUSY; |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | if (status == 0) { |
| 1169 | for (id = chip->base; id < chip->base + chip->ngpio; id++) |
| 1170 | gpio_desc[id].chip = NULL; |
| 1171 | } |
| 1172 | |
| 1173 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1174 | |
| 1175 | if (status == 0) |
| 1176 | gpiochip_unexport(chip); |
| 1177 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1178 | return status; |
| 1179 | } |
| 1180 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 1181 | |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1182 | /** |
| 1183 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 1184 | * @data: data to pass to match function |
| 1185 | * @callback: Callback function to check gpio_chip |
| 1186 | * |
| 1187 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 1188 | * determined by a user supplied @match callback. The callback should return |
| 1189 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 1190 | * non-zero, this function will return to the caller and not iterate over any |
| 1191 | * more gpio_chips. |
| 1192 | */ |
Grant Likely | 07ce8ec | 2012-05-18 23:01:05 -0600 | [diff] [blame] | 1193 | struct gpio_chip *gpiochip_find(void *data, |
Grant Likely | 6e2cf65 | 2012-03-02 15:56:03 -0700 | [diff] [blame] | 1194 | int (*match)(struct gpio_chip *chip, |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 1195 | void *data)) |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 1196 | { |
| 1197 | struct gpio_chip *chip = NULL; |
| 1198 | unsigned long flags; |
| 1199 | int i; |
| 1200 | |
| 1201 | spin_lock_irqsave(&gpio_lock, flags); |
| 1202 | for (i = 0; i < ARCH_NR_GPIOS; i++) { |
| 1203 | if (!gpio_desc[i].chip) |
| 1204 | continue; |
| 1205 | |
| 1206 | if (match(gpio_desc[i].chip, data)) { |
| 1207 | chip = gpio_desc[i].chip; |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
| 1211 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1212 | |
| 1213 | return chip; |
| 1214 | } |
Jean Delvare | 8fa0c9b | 2011-05-20 00:40:18 -0600 | [diff] [blame] | 1215 | EXPORT_SYMBOL_GPL(gpiochip_find); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1216 | |
| 1217 | /* These "optional" allocation calls help prevent drivers from stomping |
| 1218 | * on each other, and help provide better diagnostics in debugfs. |
| 1219 | * They're called even less than the "set direction" calls. |
| 1220 | */ |
| 1221 | int gpio_request(unsigned gpio, const char *label) |
| 1222 | { |
| 1223 | struct gpio_desc *desc; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1224 | struct gpio_chip *chip; |
Mark Brown | e935457 | 2012-07-09 12:22:56 +0100 | [diff] [blame] | 1225 | int status = -EPROBE_DEFER; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1226 | unsigned long flags; |
| 1227 | |
| 1228 | spin_lock_irqsave(&gpio_lock, flags); |
| 1229 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1230 | if (!gpio_is_valid(gpio)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1231 | goto done; |
| 1232 | desc = &gpio_desc[gpio]; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1233 | chip = desc->chip; |
| 1234 | if (chip == NULL) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1235 | goto done; |
| 1236 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1237 | if (!try_module_get(chip->owner)) |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1238 | goto done; |
| 1239 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1240 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1241 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1242 | */ |
| 1243 | |
| 1244 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 1245 | desc_set_label(desc, label ? : "?"); |
| 1246 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1247 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1248 | status = -EBUSY; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1249 | module_put(chip->owner); |
Magnus Damm | 7460db5 | 2009-01-29 14:25:12 -0800 | [diff] [blame] | 1250 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | if (chip->request) { |
| 1254 | /* chip->request may sleep */ |
| 1255 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1256 | status = chip->request(chip, gpio - chip->base); |
| 1257 | spin_lock_irqsave(&gpio_lock, flags); |
| 1258 | |
| 1259 | if (status < 0) { |
| 1260 | desc_set_label(desc, NULL); |
| 1261 | module_put(chip->owner); |
| 1262 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 1263 | goto done; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1264 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1265 | } |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 1266 | if (chip->get_direction) { |
| 1267 | /* chip->get_direction may sleep */ |
| 1268 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1269 | gpio_get_direction(gpio); |
| 1270 | spin_lock_irqsave(&gpio_lock, flags); |
| 1271 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1272 | done: |
| 1273 | if (status) |
| 1274 | pr_debug("gpio_request: gpio-%d (%s) status %d\n", |
| 1275 | gpio, label ? : "?", status); |
| 1276 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1277 | return status; |
| 1278 | } |
| 1279 | EXPORT_SYMBOL_GPL(gpio_request); |
| 1280 | |
| 1281 | void gpio_free(unsigned gpio) |
| 1282 | { |
| 1283 | unsigned long flags; |
| 1284 | struct gpio_desc *desc; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1285 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1286 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 1287 | might_sleep(); |
| 1288 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1289 | if (!gpio_is_valid(gpio)) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1290 | WARN_ON(extra_checks); |
| 1291 | return; |
| 1292 | } |
| 1293 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1294 | gpio_unexport(gpio); |
| 1295 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1296 | spin_lock_irqsave(&gpio_lock, flags); |
| 1297 | |
| 1298 | desc = &gpio_desc[gpio]; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1299 | chip = desc->chip; |
| 1300 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 1301 | if (chip->free) { |
| 1302 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1303 | might_sleep_if(chip->can_sleep); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1304 | chip->free(chip, gpio - chip->base); |
| 1305 | spin_lock_irqsave(&gpio_lock, flags); |
| 1306 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1307 | desc_set_label(desc, NULL); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1308 | module_put(desc->chip->owner); |
Jani Nikula | 0769746 | 2009-12-15 16:46:20 -0800 | [diff] [blame] | 1309 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1310 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1311 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1312 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 1313 | } else |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1314 | WARN_ON(extra_checks); |
| 1315 | |
| 1316 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1317 | } |
| 1318 | EXPORT_SYMBOL_GPL(gpio_free); |
| 1319 | |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1320 | /** |
| 1321 | * gpio_request_one - request a single GPIO with initial configuration |
| 1322 | * @gpio: the GPIO number |
| 1323 | * @flags: GPIO configuration as specified by GPIOF_* |
| 1324 | * @label: a literal description string of this GPIO |
| 1325 | */ |
| 1326 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) |
| 1327 | { |
| 1328 | int err; |
| 1329 | |
| 1330 | err = gpio_request(gpio, label); |
| 1331 | if (err) |
| 1332 | return err; |
| 1333 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1334 | if (flags & GPIOF_OPEN_DRAIN) |
| 1335 | set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags); |
| 1336 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1337 | if (flags & GPIOF_OPEN_SOURCE) |
| 1338 | set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags); |
| 1339 | |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1340 | if (flags & GPIOF_DIR_IN) |
| 1341 | err = gpio_direction_input(gpio); |
| 1342 | else |
| 1343 | err = gpio_direction_output(gpio, |
| 1344 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
| 1345 | |
Aaro Koskinen | e254811 | 2010-12-21 17:24:22 -0800 | [diff] [blame] | 1346 | if (err) |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1347 | goto free_gpio; |
Aaro Koskinen | e254811 | 2010-12-21 17:24:22 -0800 | [diff] [blame] | 1348 | |
Wolfram Sang | fc3a1f0 | 2011-12-13 18:34:01 +0100 | [diff] [blame] | 1349 | if (flags & GPIOF_EXPORT) { |
| 1350 | err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE); |
| 1351 | if (err) |
| 1352 | goto free_gpio; |
| 1353 | } |
| 1354 | |
| 1355 | return 0; |
| 1356 | |
| 1357 | free_gpio: |
| 1358 | gpio_free(gpio); |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1359 | return err; |
| 1360 | } |
| 1361 | EXPORT_SYMBOL_GPL(gpio_request_one); |
| 1362 | |
| 1363 | /** |
| 1364 | * gpio_request_array - request multiple GPIOs in a single call |
| 1365 | * @array: array of the 'struct gpio' |
| 1366 | * @num: how many GPIOs in the array |
| 1367 | */ |
Lars-Peter Clausen | 7c29597 | 2011-05-25 16:20:31 -0700 | [diff] [blame] | 1368 | int gpio_request_array(const struct gpio *array, size_t num) |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1369 | { |
| 1370 | int i, err; |
| 1371 | |
| 1372 | for (i = 0; i < num; i++, array++) { |
| 1373 | err = gpio_request_one(array->gpio, array->flags, array->label); |
| 1374 | if (err) |
| 1375 | goto err_free; |
| 1376 | } |
| 1377 | return 0; |
| 1378 | |
| 1379 | err_free: |
| 1380 | while (i--) |
| 1381 | gpio_free((--array)->gpio); |
| 1382 | return err; |
| 1383 | } |
| 1384 | EXPORT_SYMBOL_GPL(gpio_request_array); |
| 1385 | |
| 1386 | /** |
| 1387 | * gpio_free_array - release multiple GPIOs in a single call |
| 1388 | * @array: array of the 'struct gpio' |
| 1389 | * @num: how many GPIOs in the array |
| 1390 | */ |
Lars-Peter Clausen | 7c29597 | 2011-05-25 16:20:31 -0700 | [diff] [blame] | 1391 | void gpio_free_array(const struct gpio *array, size_t num) |
Eric Miao | 3e45f1d | 2010-03-05 13:44:35 -0800 | [diff] [blame] | 1392 | { |
| 1393 | while (num--) |
| 1394 | gpio_free((array++)->gpio); |
| 1395 | } |
| 1396 | EXPORT_SYMBOL_GPL(gpio_free_array); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1397 | |
| 1398 | /** |
| 1399 | * gpiochip_is_requested - return string iff signal was requested |
| 1400 | * @chip: controller managing the signal |
| 1401 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 1402 | * |
| 1403 | * Returns NULL if the GPIO is not currently requested, else a string. |
| 1404 | * If debugfs support is enabled, the string returned is the label passed |
| 1405 | * to gpio_request(); otherwise it is a meaningless constant. |
| 1406 | * |
| 1407 | * This function is for use by GPIO controller drivers. The label can |
| 1408 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 1409 | * can help avoid accidentally multiplexing it to another controller. |
| 1410 | */ |
| 1411 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 1412 | { |
| 1413 | unsigned gpio = chip->base + offset; |
| 1414 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1415 | if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1416 | return NULL; |
| 1417 | if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) |
| 1418 | return NULL; |
| 1419 | #ifdef CONFIG_DEBUG_FS |
| 1420 | return gpio_desc[gpio].label; |
| 1421 | #else |
| 1422 | return "?"; |
| 1423 | #endif |
| 1424 | } |
| 1425 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 1426 | |
| 1427 | |
| 1428 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 1429 | * some cases this is done in early boot, before IRQs are enabled. |
| 1430 | * |
| 1431 | * As a rule these aren't called more than once (except for drivers |
| 1432 | * using the open-drain emulation idiom) so these are natural places |
| 1433 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 1434 | * rely on gpio_request() having been called beforehand. |
| 1435 | */ |
| 1436 | |
| 1437 | int gpio_direction_input(unsigned gpio) |
| 1438 | { |
| 1439 | unsigned long flags; |
| 1440 | struct gpio_chip *chip; |
| 1441 | struct gpio_desc *desc = &gpio_desc[gpio]; |
| 1442 | int status = -EINVAL; |
| 1443 | |
| 1444 | spin_lock_irqsave(&gpio_lock, flags); |
| 1445 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1446 | if (!gpio_is_valid(gpio)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1447 | goto fail; |
| 1448 | chip = desc->chip; |
| 1449 | if (!chip || !chip->get || !chip->direction_input) |
| 1450 | goto fail; |
| 1451 | gpio -= chip->base; |
| 1452 | if (gpio >= chip->ngpio) |
| 1453 | goto fail; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1454 | status = gpio_ensure_requested(desc, gpio); |
| 1455 | if (status < 0) |
| 1456 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1457 | |
| 1458 | /* now we know the gpio is valid and chip won't vanish */ |
| 1459 | |
| 1460 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1461 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1462 | might_sleep_if(chip->can_sleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1463 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1464 | if (status) { |
| 1465 | status = chip->request(chip, gpio); |
| 1466 | if (status < 0) { |
| 1467 | pr_debug("GPIO-%d: chip request fail, %d\n", |
| 1468 | chip->base + gpio, status); |
| 1469 | /* and it's not available to anyone else ... |
| 1470 | * gpio_request() is the fully clean solution. |
| 1471 | */ |
| 1472 | goto lose; |
| 1473 | } |
| 1474 | } |
| 1475 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1476 | status = chip->direction_input(chip, gpio); |
| 1477 | if (status == 0) |
| 1478 | clear_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1479 | |
| 1480 | trace_gpio_direction(chip->base + gpio, 1, status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1481 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1482 | return status; |
| 1483 | fail: |
| 1484 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1485 | if (status) |
| 1486 | pr_debug("%s: gpio-%d status %d\n", |
Harvey Harrison | 145980a | 2008-04-30 00:54:57 -0700 | [diff] [blame] | 1487 | __func__, gpio, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1488 | return status; |
| 1489 | } |
| 1490 | EXPORT_SYMBOL_GPL(gpio_direction_input); |
| 1491 | |
| 1492 | int gpio_direction_output(unsigned gpio, int value) |
| 1493 | { |
| 1494 | unsigned long flags; |
| 1495 | struct gpio_chip *chip; |
| 1496 | struct gpio_desc *desc = &gpio_desc[gpio]; |
| 1497 | int status = -EINVAL; |
| 1498 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1499 | /* Open drain pin should not be driven to 1 */ |
| 1500 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 1501 | return gpio_direction_input(gpio); |
| 1502 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1503 | /* Open source pin should not be driven to 0 */ |
| 1504 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 1505 | return gpio_direction_input(gpio); |
| 1506 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1507 | spin_lock_irqsave(&gpio_lock, flags); |
| 1508 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1509 | if (!gpio_is_valid(gpio)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1510 | goto fail; |
| 1511 | chip = desc->chip; |
| 1512 | if (!chip || !chip->set || !chip->direction_output) |
| 1513 | goto fail; |
| 1514 | gpio -= chip->base; |
| 1515 | if (gpio >= chip->ngpio) |
| 1516 | goto fail; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1517 | status = gpio_ensure_requested(desc, gpio); |
| 1518 | if (status < 0) |
| 1519 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1520 | |
| 1521 | /* now we know the gpio is valid and chip won't vanish */ |
| 1522 | |
| 1523 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1524 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1525 | might_sleep_if(chip->can_sleep); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1526 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1527 | if (status) { |
| 1528 | status = chip->request(chip, gpio); |
| 1529 | if (status < 0) { |
| 1530 | pr_debug("GPIO-%d: chip request fail, %d\n", |
| 1531 | chip->base + gpio, status); |
| 1532 | /* and it's not available to anyone else ... |
| 1533 | * gpio_request() is the fully clean solution. |
| 1534 | */ |
| 1535 | goto lose; |
| 1536 | } |
| 1537 | } |
| 1538 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1539 | status = chip->direction_output(chip, gpio, value); |
| 1540 | if (status == 0) |
| 1541 | set_bit(FLAG_IS_OUT, &desc->flags); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1542 | trace_gpio_value(chip->base + gpio, 0, value); |
| 1543 | trace_gpio_direction(chip->base + gpio, 0, status); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 1544 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1545 | return status; |
| 1546 | fail: |
| 1547 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1548 | if (status) |
| 1549 | pr_debug("%s: gpio-%d status %d\n", |
Harvey Harrison | 145980a | 2008-04-30 00:54:57 -0700 | [diff] [blame] | 1550 | __func__, gpio, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1551 | return status; |
| 1552 | } |
| 1553 | EXPORT_SYMBOL_GPL(gpio_direction_output); |
| 1554 | |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1555 | /** |
| 1556 | * gpio_set_debounce - sets @debounce time for a @gpio |
| 1557 | * @gpio: the gpio to set debounce time |
| 1558 | * @debounce: debounce time is microseconds |
| 1559 | */ |
| 1560 | int gpio_set_debounce(unsigned gpio, unsigned debounce) |
| 1561 | { |
| 1562 | unsigned long flags; |
| 1563 | struct gpio_chip *chip; |
| 1564 | struct gpio_desc *desc = &gpio_desc[gpio]; |
| 1565 | int status = -EINVAL; |
| 1566 | |
| 1567 | spin_lock_irqsave(&gpio_lock, flags); |
| 1568 | |
| 1569 | if (!gpio_is_valid(gpio)) |
| 1570 | goto fail; |
| 1571 | chip = desc->chip; |
| 1572 | if (!chip || !chip->set || !chip->set_debounce) |
| 1573 | goto fail; |
| 1574 | gpio -= chip->base; |
| 1575 | if (gpio >= chip->ngpio) |
| 1576 | goto fail; |
| 1577 | status = gpio_ensure_requested(desc, gpio); |
| 1578 | if (status < 0) |
| 1579 | goto fail; |
| 1580 | |
| 1581 | /* now we know the gpio is valid and chip won't vanish */ |
| 1582 | |
| 1583 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1584 | |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1585 | might_sleep_if(chip->can_sleep); |
Felipe Balbi | c4b5be9 | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1586 | |
| 1587 | return chip->set_debounce(chip, gpio, debounce); |
| 1588 | |
| 1589 | fail: |
| 1590 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 1591 | if (status) |
| 1592 | pr_debug("%s: gpio-%d status %d\n", |
| 1593 | __func__, gpio, status); |
| 1594 | |
| 1595 | return status; |
| 1596 | } |
| 1597 | EXPORT_SYMBOL_GPL(gpio_set_debounce); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1598 | |
| 1599 | /* I/O calls are only valid after configuration completed; the relevant |
| 1600 | * "is this a valid GPIO" error checks should already have been done. |
| 1601 | * |
| 1602 | * "Get" operations are often inlinable as reading a pin value register, |
| 1603 | * and masking the relevant bit in that register. |
| 1604 | * |
| 1605 | * When "set" operations are inlinable, they involve writing that mask to |
| 1606 | * one register to set a low value, or a different register to set it high. |
| 1607 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1608 | * |
| 1609 | *------------------------------------------------------------------------ |
| 1610 | * |
| 1611 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1612 | * have requested the GPIO. That can include implicit requesting by |
| 1613 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1614 | * in memory, guaranteeing that these table lookups need no more locking |
| 1615 | * and that gpiochip_remove() will fail. |
| 1616 | * |
| 1617 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1618 | * that the GPIO was actually requested. |
| 1619 | */ |
| 1620 | |
| 1621 | /** |
| 1622 | * __gpio_get_value() - return a gpio's value |
| 1623 | * @gpio: gpio whose value will be returned |
| 1624 | * Context: any |
| 1625 | * |
| 1626 | * This is used directly or indirectly to implement gpio_get_value(). |
| 1627 | * It returns the zero or nonzero value provided by the associated |
| 1628 | * gpio_chip.get() method; or zero if no such method is provided. |
| 1629 | */ |
| 1630 | int __gpio_get_value(unsigned gpio) |
| 1631 | { |
| 1632 | struct gpio_chip *chip; |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1633 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1634 | |
| 1635 | chip = gpio_to_chip(gpio); |
Mark Brown | e4e449e | 2012-02-17 10:46:00 -0800 | [diff] [blame] | 1636 | /* Should be using gpio_get_value_cansleep() */ |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1637 | WARN_ON(chip->can_sleep); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1638 | value = chip->get ? chip->get(chip, gpio - chip->base) : 0; |
| 1639 | trace_gpio_value(gpio, 1, value); |
| 1640 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1641 | } |
| 1642 | EXPORT_SYMBOL_GPL(__gpio_get_value); |
| 1643 | |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1644 | /* |
| 1645 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
| 1646 | * @gpio: Gpio whose state need to be set. |
| 1647 | * @chip: Gpio chip. |
| 1648 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1649 | */ |
| 1650 | static void _gpio_set_open_drain_value(unsigned gpio, |
| 1651 | struct gpio_chip *chip, int value) |
| 1652 | { |
| 1653 | int err = 0; |
| 1654 | if (value) { |
| 1655 | err = chip->direction_input(chip, gpio - chip->base); |
| 1656 | if (!err) |
| 1657 | clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); |
| 1658 | } else { |
| 1659 | err = chip->direction_output(chip, gpio - chip->base, 0); |
| 1660 | if (!err) |
| 1661 | set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); |
| 1662 | } |
| 1663 | trace_gpio_direction(gpio, value, err); |
| 1664 | if (err < 0) |
| 1665 | pr_err("%s: Error in set_value for open drain gpio%d err %d\n", |
| 1666 | __func__, gpio, err); |
| 1667 | } |
| 1668 | |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1669 | /* |
| 1670 | * _gpio_set_open_source() - Set the open source gpio's value. |
| 1671 | * @gpio: Gpio whose state need to be set. |
| 1672 | * @chip: Gpio chip. |
| 1673 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
| 1674 | */ |
| 1675 | static void _gpio_set_open_source_value(unsigned gpio, |
| 1676 | struct gpio_chip *chip, int value) |
| 1677 | { |
| 1678 | int err = 0; |
| 1679 | if (value) { |
| 1680 | err = chip->direction_output(chip, gpio - chip->base, 1); |
| 1681 | if (!err) |
| 1682 | set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); |
| 1683 | } else { |
| 1684 | err = chip->direction_input(chip, gpio - chip->base); |
| 1685 | if (!err) |
| 1686 | clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); |
| 1687 | } |
| 1688 | trace_gpio_direction(gpio, !value, err); |
| 1689 | if (err < 0) |
| 1690 | pr_err("%s: Error in set_value for open source gpio%d err %d\n", |
| 1691 | __func__, gpio, err); |
| 1692 | } |
| 1693 | |
| 1694 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1695 | /** |
| 1696 | * __gpio_set_value() - assign a gpio's value |
| 1697 | * @gpio: gpio whose value will be assigned |
| 1698 | * @value: value to assign |
| 1699 | * Context: any |
| 1700 | * |
| 1701 | * This is used directly or indirectly to implement gpio_set_value(). |
| 1702 | * It invokes the associated gpio_chip.set() method. |
| 1703 | */ |
| 1704 | void __gpio_set_value(unsigned gpio, int value) |
| 1705 | { |
| 1706 | struct gpio_chip *chip; |
| 1707 | |
| 1708 | chip = gpio_to_chip(gpio); |
Mark Brown | e4e449e | 2012-02-17 10:46:00 -0800 | [diff] [blame] | 1709 | /* Should be using gpio_set_value_cansleep() */ |
David Brownell | 9c4ba94 | 2010-08-10 18:02:24 -0700 | [diff] [blame] | 1710 | WARN_ON(chip->can_sleep); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1711 | trace_gpio_value(gpio, 0, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1712 | if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) |
| 1713 | _gpio_set_open_drain_value(gpio, chip, value); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1714 | else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) |
| 1715 | _gpio_set_open_source_value(gpio, chip, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1716 | else |
| 1717 | chip->set(chip, gpio - chip->base, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1718 | } |
| 1719 | EXPORT_SYMBOL_GPL(__gpio_set_value); |
| 1720 | |
| 1721 | /** |
| 1722 | * __gpio_cansleep() - report whether gpio value access will sleep |
| 1723 | * @gpio: gpio in question |
| 1724 | * Context: any |
| 1725 | * |
| 1726 | * This is used directly or indirectly to implement gpio_cansleep(). It |
| 1727 | * returns nonzero if access reading or writing the GPIO value can sleep. |
| 1728 | */ |
| 1729 | int __gpio_cansleep(unsigned gpio) |
| 1730 | { |
| 1731 | struct gpio_chip *chip; |
| 1732 | |
| 1733 | /* only call this on GPIOs that are valid! */ |
| 1734 | chip = gpio_to_chip(gpio); |
| 1735 | |
| 1736 | return chip->can_sleep; |
| 1737 | } |
| 1738 | EXPORT_SYMBOL_GPL(__gpio_cansleep); |
| 1739 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1740 | /** |
| 1741 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO |
| 1742 | * @gpio: gpio whose IRQ will be returned (already requested) |
| 1743 | * Context: any |
| 1744 | * |
| 1745 | * This is used directly or indirectly to implement gpio_to_irq(). |
| 1746 | * It returns the number of the IRQ signaled by this (input) GPIO, |
| 1747 | * or a negative errno. |
| 1748 | */ |
| 1749 | int __gpio_to_irq(unsigned gpio) |
| 1750 | { |
| 1751 | struct gpio_chip *chip; |
| 1752 | |
| 1753 | chip = gpio_to_chip(gpio); |
| 1754 | return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO; |
| 1755 | } |
| 1756 | EXPORT_SYMBOL_GPL(__gpio_to_irq); |
| 1757 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1758 | |
| 1759 | |
| 1760 | /* There's no value in making it easy to inline GPIO calls that may sleep. |
| 1761 | * Common examples include ones connected to I2C or SPI chips. |
| 1762 | */ |
| 1763 | |
| 1764 | int gpio_get_value_cansleep(unsigned gpio) |
| 1765 | { |
| 1766 | struct gpio_chip *chip; |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1767 | int value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1768 | |
| 1769 | might_sleep_if(extra_checks); |
| 1770 | chip = gpio_to_chip(gpio); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1771 | value = chip->get ? chip->get(chip, gpio - chip->base) : 0; |
| 1772 | trace_gpio_value(gpio, 1, value); |
| 1773 | return value; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1774 | } |
| 1775 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); |
| 1776 | |
| 1777 | void gpio_set_value_cansleep(unsigned gpio, int value) |
| 1778 | { |
| 1779 | struct gpio_chip *chip; |
| 1780 | |
| 1781 | might_sleep_if(extra_checks); |
| 1782 | chip = gpio_to_chip(gpio); |
Uwe Kleine-König | 3f397c21 | 2011-05-20 00:40:19 -0600 | [diff] [blame] | 1783 | trace_gpio_value(gpio, 0, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1784 | if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) |
| 1785 | _gpio_set_open_drain_value(gpio, chip, value); |
Laxman Dewangan | 25553ff | 2012-02-17 20:26:22 +0530 | [diff] [blame] | 1786 | else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) |
| 1787 | _gpio_set_open_source_value(gpio, chip, value); |
Laxman Dewangan | aca5ce1 | 2012-02-17 20:26:21 +0530 | [diff] [blame] | 1788 | else |
| 1789 | chip->set(chip, gpio - chip->base, value); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1790 | } |
| 1791 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); |
| 1792 | |
| 1793 | |
| 1794 | #ifdef CONFIG_DEBUG_FS |
| 1795 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1796 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 1797 | { |
| 1798 | unsigned i; |
| 1799 | unsigned gpio = chip->base; |
| 1800 | struct gpio_desc *gdesc = &gpio_desc[gpio]; |
| 1801 | int is_out; |
| 1802 | |
| 1803 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
| 1804 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
| 1805 | continue; |
| 1806 | |
Mathias Nyman | 80b0a60 | 2012-10-24 17:25:27 +0300 | [diff] [blame^] | 1807 | gpio_get_direction(gpio); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1808 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
Jarkko Nikula | 6e8ba72 | 2008-11-19 15:36:17 -0800 | [diff] [blame] | 1809 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1810 | gpio, gdesc->label, |
| 1811 | is_out ? "out" : "in ", |
| 1812 | chip->get |
| 1813 | ? (chip->get(chip, i) ? "hi" : "lo") |
| 1814 | : "? "); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1815 | seq_printf(s, "\n"); |
| 1816 | } |
| 1817 | } |
| 1818 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1819 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1820 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1821 | struct gpio_chip *chip = NULL; |
| 1822 | unsigned int gpio; |
| 1823 | void *ret = NULL; |
| 1824 | loff_t index = 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1825 | |
| 1826 | /* REVISIT this isn't locked against gpio_chip removal ... */ |
| 1827 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1828 | for (gpio = 0; gpio_is_valid(gpio); gpio++) { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1829 | if (gpio_desc[gpio].chip == chip) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1830 | continue; |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1831 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1832 | chip = gpio_desc[gpio].chip; |
| 1833 | if (!chip) |
| 1834 | continue; |
| 1835 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1836 | if (index++ >= *pos) { |
| 1837 | ret = chip; |
| 1838 | break; |
| 1839 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1840 | } |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1841 | |
| 1842 | s->private = ""; |
| 1843 | |
| 1844 | return ret; |
| 1845 | } |
| 1846 | |
| 1847 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1848 | { |
| 1849 | struct gpio_chip *chip = v; |
| 1850 | unsigned int gpio; |
| 1851 | void *ret = NULL; |
| 1852 | |
| 1853 | /* skip GPIOs provided by the current chip */ |
| 1854 | for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) { |
| 1855 | chip = gpio_desc[gpio].chip; |
| 1856 | if (chip) { |
| 1857 | ret = chip; |
| 1858 | break; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | s->private = "\n"; |
| 1863 | ++*pos; |
| 1864 | |
| 1865 | return ret; |
| 1866 | } |
| 1867 | |
| 1868 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 1869 | { |
| 1870 | } |
| 1871 | |
| 1872 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 1873 | { |
| 1874 | struct gpio_chip *chip = v; |
| 1875 | struct device *dev; |
| 1876 | |
| 1877 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
| 1878 | chip->base, chip->base + chip->ngpio - 1); |
| 1879 | dev = chip->dev; |
| 1880 | if (dev) |
| 1881 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
| 1882 | dev_name(dev)); |
| 1883 | if (chip->label) |
| 1884 | seq_printf(s, ", %s", chip->label); |
| 1885 | if (chip->can_sleep) |
| 1886 | seq_printf(s, ", can sleep"); |
| 1887 | seq_printf(s, ":\n"); |
| 1888 | |
| 1889 | if (chip->dbg_show) |
| 1890 | chip->dbg_show(s, chip); |
| 1891 | else |
| 1892 | gpiolib_dbg_show(s, chip); |
| 1893 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1894 | return 0; |
| 1895 | } |
| 1896 | |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1897 | static const struct seq_operations gpiolib_seq_ops = { |
| 1898 | .start = gpiolib_seq_start, |
| 1899 | .next = gpiolib_seq_next, |
| 1900 | .stop = gpiolib_seq_stop, |
| 1901 | .show = gpiolib_seq_show, |
| 1902 | }; |
| 1903 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1904 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 1905 | { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1906 | return seq_open(file, &gpiolib_seq_ops); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1909 | static const struct file_operations gpiolib_operations = { |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1910 | .owner = THIS_MODULE, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1911 | .open = gpiolib_open, |
| 1912 | .read = seq_read, |
| 1913 | .llseek = seq_lseek, |
Thierry Reding | f9c4a31 | 2012-04-12 13:26:01 +0200 | [diff] [blame] | 1914 | .release = seq_release, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1915 | }; |
| 1916 | |
| 1917 | static int __init gpiolib_debugfs_init(void) |
| 1918 | { |
| 1919 | /* /sys/kernel/debug/gpio */ |
| 1920 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 1921 | NULL, NULL, &gpiolib_operations); |
| 1922 | return 0; |
| 1923 | } |
| 1924 | subsys_initcall(gpiolib_debugfs_init); |
| 1925 | |
| 1926 | #endif /* DEBUG_FS */ |