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