David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/irq.h> |
| 4 | #include <linux/spinlock.h> |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 5 | #include <linux/device.h> |
| 6 | #include <linux/err.h> |
| 7 | #include <linux/debugfs.h> |
| 8 | #include <linux/seq_file.h> |
| 9 | #include <linux/gpio.h> |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 10 | |
| 11 | |
| 12 | /* Optional implementation infrastructure for GPIO interfaces. |
| 13 | * |
| 14 | * Platforms may want to use this if they tend to use very many GPIOs |
| 15 | * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. |
| 16 | * |
| 17 | * When kernel footprint or instruction count is an issue, simpler |
| 18 | * implementations may be preferred. The GPIO programming interface |
| 19 | * allows for inlining speed-critical get/set operations for common |
| 20 | * cases, so that access to SOC-integrated GPIOs can sometimes cost |
| 21 | * only an instruction or two per bit. |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | /* When debugging, extend minimal trust to callers and platform code. |
| 26 | * Also emit diagnostic messages that may help initial bringup, when |
| 27 | * board setup or driver bugs are most common. |
| 28 | * |
| 29 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 30 | */ |
| 31 | #ifdef DEBUG |
| 32 | #define extra_checks 1 |
| 33 | #else |
| 34 | #define extra_checks 0 |
| 35 | #endif |
| 36 | |
| 37 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 38 | * While any GPIO is requested, its gpio_chip is not removable; |
| 39 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 40 | */ |
| 41 | static DEFINE_SPINLOCK(gpio_lock); |
| 42 | |
| 43 | struct gpio_desc { |
| 44 | struct gpio_chip *chip; |
| 45 | unsigned long flags; |
| 46 | /* flag symbols are bit numbers */ |
| 47 | #define FLAG_REQUESTED 0 |
| 48 | #define FLAG_IS_OUT 1 |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 49 | #define FLAG_RESERVED 2 |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 50 | #define FLAG_EXPORT 3 /* protected by sysfs_lock */ |
| 51 | #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 52 | |
| 53 | #ifdef CONFIG_DEBUG_FS |
| 54 | const char *label; |
| 55 | #endif |
| 56 | }; |
| 57 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; |
| 58 | |
| 59 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 60 | { |
| 61 | #ifdef CONFIG_DEBUG_FS |
| 62 | d->label = label; |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
| 67 | * when setting direction, and otherwise illegal. Until board setup code |
| 68 | * and drivers use explicit requests everywhere (which won't happen when |
| 69 | * those calls have no teeth) we can't avoid autorequesting. This nag |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 70 | * message should motivate switching to explicit requests... so should |
| 71 | * the weaker cleanup after faults, compared to gpio_request(). |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 72 | */ |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 73 | static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 74 | { |
| 75 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 76 | struct gpio_chip *chip = desc->chip; |
| 77 | int gpio = chip->base + offset; |
| 78 | |
| 79 | if (!try_module_get(chip->owner)) { |
| 80 | pr_err("GPIO-%d: module can't be gotten \n", gpio); |
| 81 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 82 | /* lose */ |
| 83 | return -EIO; |
| 84 | } |
| 85 | pr_warning("GPIO-%d autorequested\n", gpio); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 86 | desc_set_label(desc, "[auto]"); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 87 | /* caller must chip->request() w/o spinlock */ |
| 88 | if (chip->request) |
| 89 | return 1; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 90 | } |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 91 | return 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* caller holds gpio_lock *OR* gpio is marked as requested */ |
| 95 | static inline struct gpio_chip *gpio_to_chip(unsigned gpio) |
| 96 | { |
| 97 | return gpio_desc[gpio].chip; |
| 98 | } |
| 99 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 100 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 101 | static int gpiochip_find_base(int ngpio) |
| 102 | { |
| 103 | int i; |
| 104 | int spare = 0; |
| 105 | int base = -ENOSPC; |
| 106 | |
| 107 | for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) { |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 108 | struct gpio_desc *desc = &gpio_desc[i]; |
| 109 | struct gpio_chip *chip = desc->chip; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 110 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 111 | if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 112 | spare++; |
| 113 | if (spare == ngpio) { |
| 114 | base = i; |
| 115 | break; |
| 116 | } |
| 117 | } else { |
| 118 | spare = 0; |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 119 | if (chip) |
| 120 | i -= chip->ngpio - 1; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | if (gpio_is_valid(base)) |
| 125 | pr_debug("%s: found new base at %d\n", __func__, base); |
| 126 | return base; |
| 127 | } |
| 128 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 129 | /** |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 130 | * gpiochip_reserve() - reserve range of gpios to use with platform code only |
| 131 | * @start: starting gpio number |
| 132 | * @ngpio: number of gpios to reserve |
| 133 | * Context: platform init, potentially before irqs or kmalloc will work |
| 134 | * |
| 135 | * Returns a negative errno if any gpio within the range is already reserved |
| 136 | * or registered, else returns zero as a success code. Use this function |
| 137 | * to mark a range of gpios as unavailable for dynamic gpio number allocation, |
| 138 | * for example because its driver support is not yet loaded. |
| 139 | */ |
| 140 | int __init gpiochip_reserve(int start, int ngpio) |
| 141 | { |
| 142 | int ret = 0; |
| 143 | unsigned long flags; |
| 144 | int i; |
| 145 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 146 | if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1)) |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | |
| 149 | spin_lock_irqsave(&gpio_lock, flags); |
| 150 | |
| 151 | for (i = start; i < start + ngpio; i++) { |
| 152 | struct gpio_desc *desc = &gpio_desc[i]; |
| 153 | |
| 154 | if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) { |
| 155 | ret = -EBUSY; |
| 156 | goto err; |
| 157 | } |
| 158 | |
| 159 | set_bit(FLAG_RESERVED, &desc->flags); |
| 160 | } |
| 161 | |
| 162 | pr_debug("%s: reserved gpios from %d to %d\n", |
| 163 | __func__, start, start + ngpio - 1); |
| 164 | err: |
| 165 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 170 | #ifdef CONFIG_GPIO_SYSFS |
| 171 | |
| 172 | /* lock protects against unexport_gpio() being called while |
| 173 | * sysfs files are active. |
| 174 | */ |
| 175 | static DEFINE_MUTEX(sysfs_lock); |
| 176 | |
| 177 | /* |
| 178 | * /sys/class/gpio/gpioN... only for GPIOs that are exported |
| 179 | * /direction |
| 180 | * * MAY BE OMITTED if kernel won't allow direction changes |
| 181 | * * is read/write as "in" or "out" |
| 182 | * * may also be written as "high" or "low", initializing |
| 183 | * output value as specified ("out" implies "low") |
| 184 | * /value |
| 185 | * * always readable, subject to hardware behavior |
| 186 | * * may be writable, as zero/nonzero |
| 187 | * |
| 188 | * REVISIT there will likely be an attribute for configuring async |
| 189 | * notifications, e.g. to specify polling interval or IRQ trigger type |
| 190 | * that would for example trigger a poll() on the "value". |
| 191 | */ |
| 192 | |
| 193 | static ssize_t gpio_direction_show(struct device *dev, |
| 194 | struct device_attribute *attr, char *buf) |
| 195 | { |
| 196 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 197 | ssize_t status; |
| 198 | |
| 199 | mutex_lock(&sysfs_lock); |
| 200 | |
| 201 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 202 | status = -EIO; |
| 203 | else |
| 204 | status = sprintf(buf, "%s\n", |
| 205 | test_bit(FLAG_IS_OUT, &desc->flags) |
| 206 | ? "out" : "in"); |
| 207 | |
| 208 | mutex_unlock(&sysfs_lock); |
| 209 | return status; |
| 210 | } |
| 211 | |
| 212 | static ssize_t gpio_direction_store(struct device *dev, |
| 213 | struct device_attribute *attr, const char *buf, size_t size) |
| 214 | { |
| 215 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 216 | unsigned gpio = desc - gpio_desc; |
| 217 | ssize_t status; |
| 218 | |
| 219 | mutex_lock(&sysfs_lock); |
| 220 | |
| 221 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 222 | status = -EIO; |
| 223 | else if (sysfs_streq(buf, "high")) |
| 224 | status = gpio_direction_output(gpio, 1); |
| 225 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
| 226 | status = gpio_direction_output(gpio, 0); |
| 227 | else if (sysfs_streq(buf, "in")) |
| 228 | status = gpio_direction_input(gpio); |
| 229 | else |
| 230 | status = -EINVAL; |
| 231 | |
| 232 | mutex_unlock(&sysfs_lock); |
| 233 | return status ? : size; |
| 234 | } |
| 235 | |
| 236 | static const DEVICE_ATTR(direction, 0644, |
| 237 | gpio_direction_show, gpio_direction_store); |
| 238 | |
| 239 | static ssize_t gpio_value_show(struct device *dev, |
| 240 | struct device_attribute *attr, char *buf) |
| 241 | { |
| 242 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 243 | unsigned gpio = desc - gpio_desc; |
| 244 | ssize_t status; |
| 245 | |
| 246 | mutex_lock(&sysfs_lock); |
| 247 | |
| 248 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 249 | status = -EIO; |
| 250 | else |
Steven A. Falco | e3274e9 | 2008-10-18 20:27:48 -0700 | [diff] [blame] | 251 | status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio)); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 252 | |
| 253 | mutex_unlock(&sysfs_lock); |
| 254 | return status; |
| 255 | } |
| 256 | |
| 257 | static ssize_t gpio_value_store(struct device *dev, |
| 258 | struct device_attribute *attr, const char *buf, size_t size) |
| 259 | { |
| 260 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
| 261 | unsigned gpio = desc - gpio_desc; |
| 262 | ssize_t status; |
| 263 | |
| 264 | mutex_lock(&sysfs_lock); |
| 265 | |
| 266 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
| 267 | status = -EIO; |
| 268 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) |
| 269 | status = -EPERM; |
| 270 | else { |
| 271 | long value; |
| 272 | |
| 273 | status = strict_strtol(buf, 0, &value); |
| 274 | if (status == 0) { |
| 275 | gpio_set_value_cansleep(gpio, value != 0); |
| 276 | status = size; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | mutex_unlock(&sysfs_lock); |
| 281 | return status; |
| 282 | } |
| 283 | |
| 284 | static /*const*/ DEVICE_ATTR(value, 0644, |
| 285 | gpio_value_show, gpio_value_store); |
| 286 | |
| 287 | static const struct attribute *gpio_attrs[] = { |
| 288 | &dev_attr_direction.attr, |
| 289 | &dev_attr_value.attr, |
| 290 | NULL, |
| 291 | }; |
| 292 | |
| 293 | static const struct attribute_group gpio_attr_group = { |
| 294 | .attrs = (struct attribute **) gpio_attrs, |
| 295 | }; |
| 296 | |
| 297 | /* |
| 298 | * /sys/class/gpio/gpiochipN/ |
| 299 | * /base ... matching gpio_chip.base (N) |
| 300 | * /label ... matching gpio_chip.label |
| 301 | * /ngpio ... matching gpio_chip.ngpio |
| 302 | */ |
| 303 | |
| 304 | static ssize_t chip_base_show(struct device *dev, |
| 305 | struct device_attribute *attr, char *buf) |
| 306 | { |
| 307 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 308 | |
| 309 | return sprintf(buf, "%d\n", chip->base); |
| 310 | } |
| 311 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); |
| 312 | |
| 313 | static ssize_t chip_label_show(struct device *dev, |
| 314 | struct device_attribute *attr, char *buf) |
| 315 | { |
| 316 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 317 | |
| 318 | return sprintf(buf, "%s\n", chip->label ? : ""); |
| 319 | } |
| 320 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); |
| 321 | |
| 322 | static ssize_t chip_ngpio_show(struct device *dev, |
| 323 | struct device_attribute *attr, char *buf) |
| 324 | { |
| 325 | const struct gpio_chip *chip = dev_get_drvdata(dev); |
| 326 | |
| 327 | return sprintf(buf, "%u\n", chip->ngpio); |
| 328 | } |
| 329 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); |
| 330 | |
| 331 | static const struct attribute *gpiochip_attrs[] = { |
| 332 | &dev_attr_base.attr, |
| 333 | &dev_attr_label.attr, |
| 334 | &dev_attr_ngpio.attr, |
| 335 | NULL, |
| 336 | }; |
| 337 | |
| 338 | static const struct attribute_group gpiochip_attr_group = { |
| 339 | .attrs = (struct attribute **) gpiochip_attrs, |
| 340 | }; |
| 341 | |
| 342 | /* |
| 343 | * /sys/class/gpio/export ... write-only |
| 344 | * integer N ... number of GPIO to export (full access) |
| 345 | * /sys/class/gpio/unexport ... write-only |
| 346 | * integer N ... number of GPIO to unexport |
| 347 | */ |
| 348 | static ssize_t export_store(struct class *class, const char *buf, size_t len) |
| 349 | { |
| 350 | long gpio; |
| 351 | int status; |
| 352 | |
| 353 | status = strict_strtol(buf, 0, &gpio); |
| 354 | if (status < 0) |
| 355 | goto done; |
| 356 | |
| 357 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 358 | * request and export were done by on behalf of userspace, so |
| 359 | * they may be undone on its behalf too. |
| 360 | */ |
| 361 | |
| 362 | status = gpio_request(gpio, "sysfs"); |
| 363 | if (status < 0) |
| 364 | goto done; |
| 365 | |
| 366 | status = gpio_export(gpio, true); |
| 367 | if (status < 0) |
| 368 | gpio_free(gpio); |
| 369 | else |
| 370 | set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags); |
| 371 | |
| 372 | done: |
| 373 | if (status) |
| 374 | pr_debug("%s: status %d\n", __func__, status); |
| 375 | return status ? : len; |
| 376 | } |
| 377 | |
| 378 | static ssize_t unexport_store(struct class *class, const char *buf, size_t len) |
| 379 | { |
| 380 | long gpio; |
| 381 | int status; |
| 382 | |
| 383 | status = strict_strtol(buf, 0, &gpio); |
| 384 | if (status < 0) |
| 385 | goto done; |
| 386 | |
| 387 | status = -EINVAL; |
| 388 | |
| 389 | /* reject bogus commands (gpio_unexport ignores them) */ |
| 390 | if (!gpio_is_valid(gpio)) |
| 391 | goto done; |
| 392 | |
| 393 | /* No extra locking here; FLAG_SYSFS just signifies that the |
| 394 | * request and export were done by on behalf of userspace, so |
| 395 | * they may be undone on its behalf too. |
| 396 | */ |
| 397 | if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) { |
| 398 | status = 0; |
| 399 | gpio_free(gpio); |
| 400 | } |
| 401 | done: |
| 402 | if (status) |
| 403 | pr_debug("%s: status %d\n", __func__, status); |
| 404 | return status ? : len; |
| 405 | } |
| 406 | |
| 407 | static struct class_attribute gpio_class_attrs[] = { |
| 408 | __ATTR(export, 0200, NULL, export_store), |
| 409 | __ATTR(unexport, 0200, NULL, unexport_store), |
| 410 | __ATTR_NULL, |
| 411 | }; |
| 412 | |
| 413 | static struct class gpio_class = { |
| 414 | .name = "gpio", |
| 415 | .owner = THIS_MODULE, |
| 416 | |
| 417 | .class_attrs = gpio_class_attrs, |
| 418 | }; |
| 419 | |
| 420 | |
| 421 | /** |
| 422 | * gpio_export - export a GPIO through sysfs |
| 423 | * @gpio: gpio to make available, already requested |
| 424 | * @direction_may_change: true if userspace may change gpio direction |
| 425 | * Context: arch_initcall or later |
| 426 | * |
| 427 | * When drivers want to make a GPIO accessible to userspace after they |
| 428 | * have requested it -- perhaps while debugging, or as part of their |
| 429 | * public interface -- they may use this routine. If the GPIO can |
| 430 | * change direction (some can't) and the caller allows it, userspace |
| 431 | * will see "direction" sysfs attribute which may be used to change |
| 432 | * the gpio's direction. A "value" attribute will always be provided. |
| 433 | * |
| 434 | * Returns zero on success, else an error. |
| 435 | */ |
| 436 | int gpio_export(unsigned gpio, bool direction_may_change) |
| 437 | { |
| 438 | unsigned long flags; |
| 439 | struct gpio_desc *desc; |
| 440 | int status = -EINVAL; |
| 441 | |
| 442 | /* can't export until sysfs is available ... */ |
| 443 | if (!gpio_class.p) { |
| 444 | pr_debug("%s: called too early!\n", __func__); |
| 445 | return -ENOENT; |
| 446 | } |
| 447 | |
| 448 | if (!gpio_is_valid(gpio)) |
| 449 | goto done; |
| 450 | |
| 451 | mutex_lock(&sysfs_lock); |
| 452 | |
| 453 | spin_lock_irqsave(&gpio_lock, flags); |
| 454 | desc = &gpio_desc[gpio]; |
| 455 | if (test_bit(FLAG_REQUESTED, &desc->flags) |
| 456 | && !test_bit(FLAG_EXPORT, &desc->flags)) { |
| 457 | status = 0; |
| 458 | if (!desc->chip->direction_input |
| 459 | || !desc->chip->direction_output) |
| 460 | direction_may_change = false; |
| 461 | } |
| 462 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 463 | |
| 464 | if (status == 0) { |
| 465 | struct device *dev; |
| 466 | |
| 467 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
| 468 | desc, "gpio%d", gpio); |
| 469 | if (dev) { |
| 470 | if (direction_may_change) |
| 471 | status = sysfs_create_group(&dev->kobj, |
| 472 | &gpio_attr_group); |
| 473 | else |
| 474 | status = device_create_file(dev, |
| 475 | &dev_attr_value); |
| 476 | if (status != 0) |
| 477 | device_unregister(dev); |
| 478 | } else |
| 479 | status = -ENODEV; |
| 480 | if (status == 0) |
| 481 | set_bit(FLAG_EXPORT, &desc->flags); |
| 482 | } |
| 483 | |
| 484 | mutex_unlock(&sysfs_lock); |
| 485 | |
| 486 | done: |
| 487 | if (status) |
| 488 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); |
| 489 | |
| 490 | return status; |
| 491 | } |
| 492 | EXPORT_SYMBOL_GPL(gpio_export); |
| 493 | |
| 494 | static int match_export(struct device *dev, void *data) |
| 495 | { |
| 496 | return dev_get_drvdata(dev) == data; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * gpio_unexport - reverse effect of gpio_export() |
| 501 | * @gpio: gpio to make unavailable |
| 502 | * |
| 503 | * This is implicit on gpio_free(). |
| 504 | */ |
| 505 | void gpio_unexport(unsigned gpio) |
| 506 | { |
| 507 | struct gpio_desc *desc; |
| 508 | int status = -EINVAL; |
| 509 | |
| 510 | if (!gpio_is_valid(gpio)) |
| 511 | goto done; |
| 512 | |
| 513 | mutex_lock(&sysfs_lock); |
| 514 | |
| 515 | desc = &gpio_desc[gpio]; |
| 516 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
| 517 | struct device *dev = NULL; |
| 518 | |
| 519 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
| 520 | if (dev) { |
| 521 | clear_bit(FLAG_EXPORT, &desc->flags); |
| 522 | put_device(dev); |
| 523 | device_unregister(dev); |
| 524 | status = 0; |
| 525 | } else |
| 526 | status = -ENODEV; |
| 527 | } |
| 528 | |
| 529 | mutex_unlock(&sysfs_lock); |
| 530 | done: |
| 531 | if (status) |
| 532 | pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); |
| 533 | } |
| 534 | EXPORT_SYMBOL_GPL(gpio_unexport); |
| 535 | |
| 536 | static int gpiochip_export(struct gpio_chip *chip) |
| 537 | { |
| 538 | int status; |
| 539 | struct device *dev; |
| 540 | |
| 541 | /* Many systems register gpio chips for SOC support very early, |
| 542 | * before driver model support is available. In those cases we |
| 543 | * export this later, in gpiolib_sysfs_init() ... here we just |
| 544 | * verify that _some_ field of gpio_class got initialized. |
| 545 | */ |
| 546 | if (!gpio_class.p) |
| 547 | return 0; |
| 548 | |
| 549 | /* use chip->base for the ID; it's already known to be unique */ |
| 550 | mutex_lock(&sysfs_lock); |
| 551 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, |
| 552 | "gpiochip%d", chip->base); |
| 553 | if (dev) { |
| 554 | status = sysfs_create_group(&dev->kobj, |
| 555 | &gpiochip_attr_group); |
| 556 | } else |
| 557 | status = -ENODEV; |
| 558 | chip->exported = (status == 0); |
| 559 | mutex_unlock(&sysfs_lock); |
| 560 | |
| 561 | if (status) { |
| 562 | unsigned long flags; |
| 563 | unsigned gpio; |
| 564 | |
| 565 | spin_lock_irqsave(&gpio_lock, flags); |
| 566 | gpio = chip->base; |
| 567 | while (gpio_desc[gpio].chip == chip) |
| 568 | gpio_desc[gpio++].chip = NULL; |
| 569 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 570 | |
| 571 | pr_debug("%s: chip %s status %d\n", __func__, |
| 572 | chip->label, status); |
| 573 | } |
| 574 | |
| 575 | return status; |
| 576 | } |
| 577 | |
| 578 | static void gpiochip_unexport(struct gpio_chip *chip) |
| 579 | { |
| 580 | int status; |
| 581 | struct device *dev; |
| 582 | |
| 583 | mutex_lock(&sysfs_lock); |
| 584 | dev = class_find_device(&gpio_class, NULL, chip, match_export); |
| 585 | if (dev) { |
| 586 | put_device(dev); |
| 587 | device_unregister(dev); |
| 588 | chip->exported = 0; |
| 589 | status = 0; |
| 590 | } else |
| 591 | status = -ENODEV; |
| 592 | mutex_unlock(&sysfs_lock); |
| 593 | |
| 594 | if (status) |
| 595 | pr_debug("%s: chip %s status %d\n", __func__, |
| 596 | chip->label, status); |
| 597 | } |
| 598 | |
| 599 | static int __init gpiolib_sysfs_init(void) |
| 600 | { |
| 601 | int status; |
| 602 | unsigned long flags; |
| 603 | unsigned gpio; |
| 604 | |
| 605 | status = class_register(&gpio_class); |
| 606 | if (status < 0) |
| 607 | return status; |
| 608 | |
| 609 | /* Scan and register the gpio_chips which registered very |
| 610 | * early (e.g. before the class_register above was called). |
| 611 | * |
| 612 | * We run before arch_initcall() so chip->dev nodes can have |
| 613 | * registered, and so arch_initcall() can always gpio_export(). |
| 614 | */ |
| 615 | spin_lock_irqsave(&gpio_lock, flags); |
| 616 | for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) { |
| 617 | struct gpio_chip *chip; |
| 618 | |
| 619 | chip = gpio_desc[gpio].chip; |
| 620 | if (!chip || chip->exported) |
| 621 | continue; |
| 622 | |
| 623 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 624 | status = gpiochip_export(chip); |
| 625 | spin_lock_irqsave(&gpio_lock, flags); |
| 626 | } |
| 627 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 628 | |
| 629 | |
| 630 | return status; |
| 631 | } |
| 632 | postcore_initcall(gpiolib_sysfs_init); |
| 633 | |
| 634 | #else |
| 635 | static inline int gpiochip_export(struct gpio_chip *chip) |
| 636 | { |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static inline void gpiochip_unexport(struct gpio_chip *chip) |
| 641 | { |
| 642 | } |
| 643 | |
| 644 | #endif /* CONFIG_GPIO_SYSFS */ |
| 645 | |
Anton Vorontsov | 169b6a7 | 2008-04-28 02:14:47 -0700 | [diff] [blame] | 646 | /** |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 647 | * gpiochip_add() - register a gpio_chip |
| 648 | * @chip: the chip to register, with chip->base initialized |
| 649 | * Context: potentially before irqs or kmalloc will work |
| 650 | * |
| 651 | * Returns a negative errno if the chip can't be registered, such as |
| 652 | * because the chip->base is invalid or already associated with a |
| 653 | * different chip. Otherwise it returns zero as a success code. |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 654 | * |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 655 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 656 | * can be freely used, the chip->dev device must be registered before |
| 657 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 658 | * for GPIOs will fail rudely. |
| 659 | * |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 660 | * If chip->base is negative, this requests dynamic assignment of |
| 661 | * a range of valid GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 662 | */ |
| 663 | int gpiochip_add(struct gpio_chip *chip) |
| 664 | { |
| 665 | unsigned long flags; |
| 666 | int status = 0; |
| 667 | unsigned id; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 668 | int base = chip->base; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 669 | |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 670 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 671 | && base >= 0) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 672 | status = -EINVAL; |
| 673 | goto fail; |
| 674 | } |
| 675 | |
| 676 | spin_lock_irqsave(&gpio_lock, flags); |
| 677 | |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 678 | if (base < 0) { |
| 679 | base = gpiochip_find_base(chip->ngpio); |
| 680 | if (base < 0) { |
| 681 | status = base; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 682 | goto unlock; |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 683 | } |
| 684 | chip->base = base; |
| 685 | } |
| 686 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 687 | /* these GPIO numbers must not be managed by another gpio_chip */ |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 688 | for (id = base; id < base + chip->ngpio; id++) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 689 | if (gpio_desc[id].chip != NULL) { |
| 690 | status = -EBUSY; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | if (status == 0) { |
Anton Vorontsov | 8d0aab2 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 695 | for (id = base; id < base + chip->ngpio; id++) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 696 | gpio_desc[id].chip = chip; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 697 | |
| 698 | /* REVISIT: most hardware initializes GPIOs as |
| 699 | * inputs (often with pullups enabled) so power |
| 700 | * usage is minimized. Linux code should set the |
| 701 | * gpio direction first thing; but until it does, |
| 702 | * we may expose the wrong direction in sysfs. |
| 703 | */ |
| 704 | gpio_desc[id].flags = !chip->direction_input |
| 705 | ? (1 << FLAG_IS_OUT) |
| 706 | : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 710 | unlock: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 711 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 712 | if (status == 0) |
| 713 | status = gpiochip_export(chip); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 714 | fail: |
| 715 | /* failures here can mean systems won't boot... */ |
| 716 | if (status) |
| 717 | pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n", |
Trent Piepho | bff5fda | 2008-05-23 13:04:44 -0700 | [diff] [blame] | 718 | chip->base, chip->base + chip->ngpio - 1, |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 719 | chip->label ? : "generic"); |
| 720 | return status; |
| 721 | } |
| 722 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 723 | |
| 724 | /** |
| 725 | * gpiochip_remove() - unregister a gpio_chip |
| 726 | * @chip: the chip to unregister |
| 727 | * |
| 728 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 729 | */ |
| 730 | int gpiochip_remove(struct gpio_chip *chip) |
| 731 | { |
| 732 | unsigned long flags; |
| 733 | int status = 0; |
| 734 | unsigned id; |
| 735 | |
| 736 | spin_lock_irqsave(&gpio_lock, flags); |
| 737 | |
| 738 | for (id = chip->base; id < chip->base + chip->ngpio; id++) { |
| 739 | if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) { |
| 740 | status = -EBUSY; |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | if (status == 0) { |
| 745 | for (id = chip->base; id < chip->base + chip->ngpio; id++) |
| 746 | gpio_desc[id].chip = NULL; |
| 747 | } |
| 748 | |
| 749 | spin_unlock_irqrestore(&gpio_lock, flags); |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 750 | |
| 751 | if (status == 0) |
| 752 | gpiochip_unexport(chip); |
| 753 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 754 | return status; |
| 755 | } |
| 756 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 757 | |
| 758 | |
| 759 | /* These "optional" allocation calls help prevent drivers from stomping |
| 760 | * on each other, and help provide better diagnostics in debugfs. |
| 761 | * They're called even less than the "set direction" calls. |
| 762 | */ |
| 763 | int gpio_request(unsigned gpio, const char *label) |
| 764 | { |
| 765 | struct gpio_desc *desc; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 766 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 767 | int status = -EINVAL; |
| 768 | unsigned long flags; |
| 769 | |
| 770 | spin_lock_irqsave(&gpio_lock, flags); |
| 771 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 772 | if (!gpio_is_valid(gpio)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 773 | goto done; |
| 774 | desc = &gpio_desc[gpio]; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 775 | chip = desc->chip; |
| 776 | if (chip == NULL) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 777 | goto done; |
| 778 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 779 | if (!try_module_get(chip->owner)) |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 780 | goto done; |
| 781 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 782 | /* NOTE: gpio_request() can be called in early boot, |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 783 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 784 | */ |
| 785 | |
| 786 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 787 | desc_set_label(desc, label ? : "?"); |
| 788 | status = 0; |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 789 | } else { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 790 | status = -EBUSY; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 791 | module_put(chip->owner); |
| 792 | } |
| 793 | |
| 794 | if (chip->request) { |
| 795 | /* chip->request may sleep */ |
| 796 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 797 | status = chip->request(chip, gpio - chip->base); |
| 798 | spin_lock_irqsave(&gpio_lock, flags); |
| 799 | |
| 800 | if (status < 0) { |
| 801 | desc_set_label(desc, NULL); |
| 802 | module_put(chip->owner); |
| 803 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 804 | } |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 805 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 806 | |
| 807 | done: |
| 808 | if (status) |
| 809 | pr_debug("gpio_request: gpio-%d (%s) status %d\n", |
| 810 | gpio, label ? : "?", status); |
| 811 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 812 | return status; |
| 813 | } |
| 814 | EXPORT_SYMBOL_GPL(gpio_request); |
| 815 | |
| 816 | void gpio_free(unsigned gpio) |
| 817 | { |
| 818 | unsigned long flags; |
| 819 | struct gpio_desc *desc; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 820 | struct gpio_chip *chip; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 821 | |
Uwe Kleine-König | 3d599d1 | 2008-10-15 22:03:12 -0700 | [diff] [blame] | 822 | might_sleep(); |
| 823 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 824 | if (!gpio_is_valid(gpio)) { |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 825 | WARN_ON(extra_checks); |
| 826 | return; |
| 827 | } |
| 828 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 829 | gpio_unexport(gpio); |
| 830 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 831 | spin_lock_irqsave(&gpio_lock, flags); |
| 832 | |
| 833 | desc = &gpio_desc[gpio]; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 834 | chip = desc->chip; |
| 835 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 836 | if (chip->free) { |
| 837 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 838 | might_sleep_if(extra_checks && chip->can_sleep); |
| 839 | chip->free(chip, gpio - chip->base); |
| 840 | spin_lock_irqsave(&gpio_lock, flags); |
| 841 | } |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 842 | desc_set_label(desc, NULL); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 843 | module_put(desc->chip->owner); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 844 | clear_bit(FLAG_REQUESTED, &desc->flags); |
Guennadi Liakhovetski | 438d890 | 2008-04-28 02:14:44 -0700 | [diff] [blame] | 845 | } else |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 846 | WARN_ON(extra_checks); |
| 847 | |
| 848 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 849 | } |
| 850 | EXPORT_SYMBOL_GPL(gpio_free); |
| 851 | |
| 852 | |
| 853 | /** |
| 854 | * gpiochip_is_requested - return string iff signal was requested |
| 855 | * @chip: controller managing the signal |
| 856 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 857 | * |
| 858 | * Returns NULL if the GPIO is not currently requested, else a string. |
| 859 | * If debugfs support is enabled, the string returned is the label passed |
| 860 | * to gpio_request(); otherwise it is a meaningless constant. |
| 861 | * |
| 862 | * This function is for use by GPIO controller drivers. The label can |
| 863 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 864 | * can help avoid accidentally multiplexing it to another controller. |
| 865 | */ |
| 866 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 867 | { |
| 868 | unsigned gpio = chip->base + offset; |
| 869 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 870 | if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 871 | return NULL; |
| 872 | if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) |
| 873 | return NULL; |
| 874 | #ifdef CONFIG_DEBUG_FS |
| 875 | return gpio_desc[gpio].label; |
| 876 | #else |
| 877 | return "?"; |
| 878 | #endif |
| 879 | } |
| 880 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 881 | |
| 882 | |
| 883 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 884 | * some cases this is done in early boot, before IRQs are enabled. |
| 885 | * |
| 886 | * As a rule these aren't called more than once (except for drivers |
| 887 | * using the open-drain emulation idiom) so these are natural places |
| 888 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 889 | * rely on gpio_request() having been called beforehand. |
| 890 | */ |
| 891 | |
| 892 | int gpio_direction_input(unsigned gpio) |
| 893 | { |
| 894 | unsigned long flags; |
| 895 | struct gpio_chip *chip; |
| 896 | struct gpio_desc *desc = &gpio_desc[gpio]; |
| 897 | int status = -EINVAL; |
| 898 | |
| 899 | spin_lock_irqsave(&gpio_lock, flags); |
| 900 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 901 | if (!gpio_is_valid(gpio)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 902 | goto fail; |
| 903 | chip = desc->chip; |
| 904 | if (!chip || !chip->get || !chip->direction_input) |
| 905 | goto fail; |
| 906 | gpio -= chip->base; |
| 907 | if (gpio >= chip->ngpio) |
| 908 | goto fail; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 909 | status = gpio_ensure_requested(desc, gpio); |
| 910 | if (status < 0) |
| 911 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 912 | |
| 913 | /* now we know the gpio is valid and chip won't vanish */ |
| 914 | |
| 915 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 916 | |
| 917 | might_sleep_if(extra_checks && chip->can_sleep); |
| 918 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 919 | if (status) { |
| 920 | status = chip->request(chip, gpio); |
| 921 | if (status < 0) { |
| 922 | pr_debug("GPIO-%d: chip request fail, %d\n", |
| 923 | chip->base + gpio, status); |
| 924 | /* and it's not available to anyone else ... |
| 925 | * gpio_request() is the fully clean solution. |
| 926 | */ |
| 927 | goto lose; |
| 928 | } |
| 929 | } |
| 930 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 931 | status = chip->direction_input(chip, gpio); |
| 932 | if (status == 0) |
| 933 | clear_bit(FLAG_IS_OUT, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 934 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 935 | return status; |
| 936 | fail: |
| 937 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 938 | if (status) |
| 939 | pr_debug("%s: gpio-%d status %d\n", |
Harvey Harrison | 145980a | 2008-04-30 00:54:57 -0700 | [diff] [blame] | 940 | __func__, gpio, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 941 | return status; |
| 942 | } |
| 943 | EXPORT_SYMBOL_GPL(gpio_direction_input); |
| 944 | |
| 945 | int gpio_direction_output(unsigned gpio, int value) |
| 946 | { |
| 947 | unsigned long flags; |
| 948 | struct gpio_chip *chip; |
| 949 | struct gpio_desc *desc = &gpio_desc[gpio]; |
| 950 | int status = -EINVAL; |
| 951 | |
| 952 | spin_lock_irqsave(&gpio_lock, flags); |
| 953 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 954 | if (!gpio_is_valid(gpio)) |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 955 | goto fail; |
| 956 | chip = desc->chip; |
| 957 | if (!chip || !chip->set || !chip->direction_output) |
| 958 | goto fail; |
| 959 | gpio -= chip->base; |
| 960 | if (gpio >= chip->ngpio) |
| 961 | goto fail; |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 962 | status = gpio_ensure_requested(desc, gpio); |
| 963 | if (status < 0) |
| 964 | goto fail; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 965 | |
| 966 | /* now we know the gpio is valid and chip won't vanish */ |
| 967 | |
| 968 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 969 | |
| 970 | might_sleep_if(extra_checks && chip->can_sleep); |
| 971 | |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 972 | if (status) { |
| 973 | status = chip->request(chip, gpio); |
| 974 | if (status < 0) { |
| 975 | pr_debug("GPIO-%d: chip request fail, %d\n", |
| 976 | chip->base + gpio, status); |
| 977 | /* and it's not available to anyone else ... |
| 978 | * gpio_request() is the fully clean solution. |
| 979 | */ |
| 980 | goto lose; |
| 981 | } |
| 982 | } |
| 983 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 984 | status = chip->direction_output(chip, gpio, value); |
| 985 | if (status == 0) |
| 986 | set_bit(FLAG_IS_OUT, &desc->flags); |
David Brownell | 35e8bb5 | 2008-10-15 22:03:16 -0700 | [diff] [blame] | 987 | lose: |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 988 | return status; |
| 989 | fail: |
| 990 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 991 | if (status) |
| 992 | pr_debug("%s: gpio-%d status %d\n", |
Harvey Harrison | 145980a | 2008-04-30 00:54:57 -0700 | [diff] [blame] | 993 | __func__, gpio, status); |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 994 | return status; |
| 995 | } |
| 996 | EXPORT_SYMBOL_GPL(gpio_direction_output); |
| 997 | |
| 998 | |
| 999 | /* I/O calls are only valid after configuration completed; the relevant |
| 1000 | * "is this a valid GPIO" error checks should already have been done. |
| 1001 | * |
| 1002 | * "Get" operations are often inlinable as reading a pin value register, |
| 1003 | * and masking the relevant bit in that register. |
| 1004 | * |
| 1005 | * When "set" operations are inlinable, they involve writing that mask to |
| 1006 | * one register to set a low value, or a different register to set it high. |
| 1007 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1008 | * |
| 1009 | *------------------------------------------------------------------------ |
| 1010 | * |
| 1011 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1012 | * have requested the GPIO. That can include implicit requesting by |
| 1013 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1014 | * in memory, guaranteeing that these table lookups need no more locking |
| 1015 | * and that gpiochip_remove() will fail. |
| 1016 | * |
| 1017 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1018 | * that the GPIO was actually requested. |
| 1019 | */ |
| 1020 | |
| 1021 | /** |
| 1022 | * __gpio_get_value() - return a gpio's value |
| 1023 | * @gpio: gpio whose value will be returned |
| 1024 | * Context: any |
| 1025 | * |
| 1026 | * This is used directly or indirectly to implement gpio_get_value(). |
| 1027 | * It returns the zero or nonzero value provided by the associated |
| 1028 | * gpio_chip.get() method; or zero if no such method is provided. |
| 1029 | */ |
| 1030 | int __gpio_get_value(unsigned gpio) |
| 1031 | { |
| 1032 | struct gpio_chip *chip; |
| 1033 | |
| 1034 | chip = gpio_to_chip(gpio); |
| 1035 | WARN_ON(extra_checks && chip->can_sleep); |
| 1036 | return chip->get ? chip->get(chip, gpio - chip->base) : 0; |
| 1037 | } |
| 1038 | EXPORT_SYMBOL_GPL(__gpio_get_value); |
| 1039 | |
| 1040 | /** |
| 1041 | * __gpio_set_value() - assign a gpio's value |
| 1042 | * @gpio: gpio whose value will be assigned |
| 1043 | * @value: value to assign |
| 1044 | * Context: any |
| 1045 | * |
| 1046 | * This is used directly or indirectly to implement gpio_set_value(). |
| 1047 | * It invokes the associated gpio_chip.set() method. |
| 1048 | */ |
| 1049 | void __gpio_set_value(unsigned gpio, int value) |
| 1050 | { |
| 1051 | struct gpio_chip *chip; |
| 1052 | |
| 1053 | chip = gpio_to_chip(gpio); |
| 1054 | WARN_ON(extra_checks && chip->can_sleep); |
| 1055 | chip->set(chip, gpio - chip->base, value); |
| 1056 | } |
| 1057 | EXPORT_SYMBOL_GPL(__gpio_set_value); |
| 1058 | |
| 1059 | /** |
| 1060 | * __gpio_cansleep() - report whether gpio value access will sleep |
| 1061 | * @gpio: gpio in question |
| 1062 | * Context: any |
| 1063 | * |
| 1064 | * This is used directly or indirectly to implement gpio_cansleep(). It |
| 1065 | * returns nonzero if access reading or writing the GPIO value can sleep. |
| 1066 | */ |
| 1067 | int __gpio_cansleep(unsigned gpio) |
| 1068 | { |
| 1069 | struct gpio_chip *chip; |
| 1070 | |
| 1071 | /* only call this on GPIOs that are valid! */ |
| 1072 | chip = gpio_to_chip(gpio); |
| 1073 | |
| 1074 | return chip->can_sleep; |
| 1075 | } |
| 1076 | EXPORT_SYMBOL_GPL(__gpio_cansleep); |
| 1077 | |
David Brownell | 0f6d504 | 2008-10-15 22:03:14 -0700 | [diff] [blame] | 1078 | /** |
| 1079 | * __gpio_to_irq() - return the IRQ corresponding to a GPIO |
| 1080 | * @gpio: gpio whose IRQ will be returned (already requested) |
| 1081 | * Context: any |
| 1082 | * |
| 1083 | * This is used directly or indirectly to implement gpio_to_irq(). |
| 1084 | * It returns the number of the IRQ signaled by this (input) GPIO, |
| 1085 | * or a negative errno. |
| 1086 | */ |
| 1087 | int __gpio_to_irq(unsigned gpio) |
| 1088 | { |
| 1089 | struct gpio_chip *chip; |
| 1090 | |
| 1091 | chip = gpio_to_chip(gpio); |
| 1092 | return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO; |
| 1093 | } |
| 1094 | EXPORT_SYMBOL_GPL(__gpio_to_irq); |
| 1095 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1096 | |
| 1097 | |
| 1098 | /* There's no value in making it easy to inline GPIO calls that may sleep. |
| 1099 | * Common examples include ones connected to I2C or SPI chips. |
| 1100 | */ |
| 1101 | |
| 1102 | int gpio_get_value_cansleep(unsigned gpio) |
| 1103 | { |
| 1104 | struct gpio_chip *chip; |
| 1105 | |
| 1106 | might_sleep_if(extra_checks); |
| 1107 | chip = gpio_to_chip(gpio); |
David Brownell | 978ccaa | 2008-10-18 20:27:49 -0700 | [diff] [blame^] | 1108 | return chip->get ? chip->get(chip, gpio - chip->base) : 0; |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1109 | } |
| 1110 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); |
| 1111 | |
| 1112 | void gpio_set_value_cansleep(unsigned gpio, int value) |
| 1113 | { |
| 1114 | struct gpio_chip *chip; |
| 1115 | |
| 1116 | might_sleep_if(extra_checks); |
| 1117 | chip = gpio_to_chip(gpio); |
| 1118 | chip->set(chip, gpio - chip->base, value); |
| 1119 | } |
| 1120 | EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); |
| 1121 | |
| 1122 | |
| 1123 | #ifdef CONFIG_DEBUG_FS |
| 1124 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1125 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 1126 | { |
| 1127 | unsigned i; |
| 1128 | unsigned gpio = chip->base; |
| 1129 | struct gpio_desc *gdesc = &gpio_desc[gpio]; |
| 1130 | int is_out; |
| 1131 | |
| 1132 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
| 1133 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) |
| 1134 | continue; |
| 1135 | |
| 1136 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
| 1137 | seq_printf(s, " gpio-%-3d (%-12s) %s %s", |
| 1138 | gpio, gdesc->label, |
| 1139 | is_out ? "out" : "in ", |
| 1140 | chip->get |
| 1141 | ? (chip->get(chip, i) ? "hi" : "lo") |
| 1142 | : "? "); |
| 1143 | |
| 1144 | if (!is_out) { |
| 1145 | int irq = gpio_to_irq(gpio); |
| 1146 | struct irq_desc *desc = irq_desc + irq; |
| 1147 | |
| 1148 | /* This races with request_irq(), set_irq_type(), |
| 1149 | * and set_irq_wake() ... but those are "rare". |
| 1150 | * |
| 1151 | * More significantly, trigger type flags aren't |
| 1152 | * currently maintained by genirq. |
| 1153 | */ |
| 1154 | if (irq >= 0 && desc->action) { |
| 1155 | char *trigger; |
| 1156 | |
| 1157 | switch (desc->status & IRQ_TYPE_SENSE_MASK) { |
| 1158 | case IRQ_TYPE_NONE: |
| 1159 | trigger = "(default)"; |
| 1160 | break; |
| 1161 | case IRQ_TYPE_EDGE_FALLING: |
| 1162 | trigger = "edge-falling"; |
| 1163 | break; |
| 1164 | case IRQ_TYPE_EDGE_RISING: |
| 1165 | trigger = "edge-rising"; |
| 1166 | break; |
| 1167 | case IRQ_TYPE_EDGE_BOTH: |
| 1168 | trigger = "edge-both"; |
| 1169 | break; |
| 1170 | case IRQ_TYPE_LEVEL_HIGH: |
| 1171 | trigger = "level-high"; |
| 1172 | break; |
| 1173 | case IRQ_TYPE_LEVEL_LOW: |
| 1174 | trigger = "level-low"; |
| 1175 | break; |
| 1176 | default: |
| 1177 | trigger = "?trigger?"; |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | seq_printf(s, " irq-%d %s%s", |
| 1182 | irq, trigger, |
| 1183 | (desc->status & IRQ_WAKEUP) |
| 1184 | ? " wakeup" : ""); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | seq_printf(s, "\n"); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | static int gpiolib_show(struct seq_file *s, void *unused) |
| 1193 | { |
| 1194 | struct gpio_chip *chip = NULL; |
| 1195 | unsigned gpio; |
| 1196 | int started = 0; |
| 1197 | |
| 1198 | /* REVISIT this isn't locked against gpio_chip removal ... */ |
| 1199 | |
Guennadi Liakhovetski | e6de180 | 2008-04-28 02:14:46 -0700 | [diff] [blame] | 1200 | for (gpio = 0; gpio_is_valid(gpio); gpio++) { |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1201 | struct device *dev; |
| 1202 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1203 | if (chip == gpio_desc[gpio].chip) |
| 1204 | continue; |
| 1205 | chip = gpio_desc[gpio].chip; |
| 1206 | if (!chip) |
| 1207 | continue; |
| 1208 | |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1209 | seq_printf(s, "%sGPIOs %d-%d", |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1210 | started ? "\n" : "", |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 1211 | chip->base, chip->base + chip->ngpio - 1); |
| 1212 | dev = chip->dev; |
| 1213 | if (dev) |
| 1214 | seq_printf(s, ", %s/%s", |
| 1215 | dev->bus ? dev->bus->name : "no-bus", |
| 1216 | dev->bus_id); |
| 1217 | if (chip->label) |
| 1218 | seq_printf(s, ", %s", chip->label); |
| 1219 | if (chip->can_sleep) |
| 1220 | seq_printf(s, ", can sleep"); |
| 1221 | seq_printf(s, ":\n"); |
| 1222 | |
David Brownell | d2876d0 | 2008-02-04 22:28:20 -0800 | [diff] [blame] | 1223 | started = 1; |
| 1224 | if (chip->dbg_show) |
| 1225 | chip->dbg_show(s, chip); |
| 1226 | else |
| 1227 | gpiolib_dbg_show(s, chip); |
| 1228 | } |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 1233 | { |
| 1234 | return single_open(file, gpiolib_show, NULL); |
| 1235 | } |
| 1236 | |
| 1237 | static struct file_operations gpiolib_operations = { |
| 1238 | .open = gpiolib_open, |
| 1239 | .read = seq_read, |
| 1240 | .llseek = seq_lseek, |
| 1241 | .release = single_release, |
| 1242 | }; |
| 1243 | |
| 1244 | static int __init gpiolib_debugfs_init(void) |
| 1245 | { |
| 1246 | /* /sys/kernel/debug/gpio */ |
| 1247 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 1248 | NULL, NULL, &gpiolib_operations); |
| 1249 | return 0; |
| 1250 | } |
| 1251 | subsys_initcall(gpiolib_debugfs_init); |
| 1252 | |
| 1253 | #endif /* DEBUG_FS */ |