pcmcia: drivers/pcmcia/pcmcia_resource.c: Remove unnecessary semicolons
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / gpiolib.c
blob51a8d4103be53beb3dd90bd1328d89935cf6573c
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/irq.h>
4 #include <linux/spinlock.h>
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>
12 /* Optional implementation infrastructure for GPIO interfaces.
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.
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.
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.
29 * Otherwise, minimize overhead in what may be bitbanging codepaths.
31 #ifdef DEBUG
32 #define extra_checks 1
33 #else
34 #define extra_checks 0
35 #endif
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.
41 static DEFINE_SPINLOCK(gpio_lock);
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
49 #define FLAG_RESERVED 2
50 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
51 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
53 #ifdef CONFIG_DEBUG_FS
54 const char *label;
55 #endif
57 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
59 static inline void desc_set_label(struct gpio_desc *d, const char *label)
61 #ifdef CONFIG_DEBUG_FS
62 d->label = label;
63 #endif
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
70 * message should motivate switching to explicit requests... so should
71 * the weaker cleanup after faults, compared to gpio_request().
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.
77 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
79 const struct gpio_chip *chip = desc->chip;
80 const int gpio = chip->base + offset;
82 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
83 "autorequest GPIO-%d\n", gpio)) {
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;
90 desc_set_label(desc, "[auto]");
91 /* caller must chip->request() w/o spinlock */
92 if (chip->request)
93 return 1;
95 return 0;
98 /* caller holds gpio_lock *OR* gpio is marked as requested */
99 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
101 return gpio_desc[gpio].chip;
104 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
105 static int gpiochip_find_base(int ngpio)
107 int i;
108 int spare = 0;
109 int base = -ENOSPC;
111 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
112 struct gpio_desc *desc = &gpio_desc[i];
113 struct gpio_chip *chip = desc->chip;
115 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
116 spare++;
117 if (spare == ngpio) {
118 base = i;
119 break;
121 } else {
122 spare = 0;
123 if (chip)
124 i -= chip->ngpio - 1;
128 if (gpio_is_valid(base))
129 pr_debug("%s: found new base at %d\n", __func__, base);
130 return base;
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
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.
144 int __init gpiochip_reserve(int start, int ngpio)
146 int ret = 0;
147 unsigned long flags;
148 int i;
150 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
151 return -EINVAL;
153 spin_lock_irqsave(&gpio_lock, flags);
155 for (i = start; i < start + ngpio; i++) {
156 struct gpio_desc *desc = &gpio_desc[i];
158 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
159 ret = -EBUSY;
160 goto err;
163 set_bit(FLAG_RESERVED, &desc->flags);
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);
171 return ret;
174 #ifdef CONFIG_GPIO_SYSFS
176 /* lock protects against unexport_gpio() being called while
177 * sysfs files are active.
179 static DEFINE_MUTEX(sysfs_lock);
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
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".
197 static ssize_t gpio_direction_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 const struct gpio_desc *desc = dev_get_drvdata(dev);
201 ssize_t status;
203 mutex_lock(&sysfs_lock);
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");
212 mutex_unlock(&sysfs_lock);
213 return status;
216 static ssize_t gpio_direction_store(struct device *dev,
217 struct device_attribute *attr, const char *buf, size_t size)
219 const struct gpio_desc *desc = dev_get_drvdata(dev);
220 unsigned gpio = desc - gpio_desc;
221 ssize_t status;
223 mutex_lock(&sysfs_lock);
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;
236 mutex_unlock(&sysfs_lock);
237 return status ? : size;
240 static const DEVICE_ATTR(direction, 0644,
241 gpio_direction_show, gpio_direction_store);
243 static ssize_t gpio_value_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 const struct gpio_desc *desc = dev_get_drvdata(dev);
247 unsigned gpio = desc - gpio_desc;
248 ssize_t status;
250 mutex_lock(&sysfs_lock);
252 if (!test_bit(FLAG_EXPORT, &desc->flags))
253 status = -EIO;
254 else
255 status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
257 mutex_unlock(&sysfs_lock);
258 return status;
261 static ssize_t gpio_value_store(struct device *dev,
262 struct device_attribute *attr, const char *buf, size_t size)
264 const struct gpio_desc *desc = dev_get_drvdata(dev);
265 unsigned gpio = desc - gpio_desc;
266 ssize_t status;
268 mutex_lock(&sysfs_lock);
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;
277 status = strict_strtol(buf, 0, &value);
278 if (status == 0) {
279 gpio_set_value_cansleep(gpio, value != 0);
280 status = size;
284 mutex_unlock(&sysfs_lock);
285 return status;
288 static /*const*/ DEVICE_ATTR(value, 0644,
289 gpio_value_show, gpio_value_store);
291 static const struct attribute *gpio_attrs[] = {
292 &dev_attr_direction.attr,
293 &dev_attr_value.attr,
294 NULL,
297 static const struct attribute_group gpio_attr_group = {
298 .attrs = (struct attribute **) gpio_attrs,
302 * /sys/class/gpio/gpiochipN/
303 * /base ... matching gpio_chip.base (N)
304 * /label ... matching gpio_chip.label
305 * /ngpio ... matching gpio_chip.ngpio
308 static ssize_t chip_base_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 const struct gpio_chip *chip = dev_get_drvdata(dev);
313 return sprintf(buf, "%d\n", chip->base);
315 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
317 static ssize_t chip_label_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 const struct gpio_chip *chip = dev_get_drvdata(dev);
322 return sprintf(buf, "%s\n", chip->label ? : "");
324 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
326 static ssize_t chip_ngpio_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 const struct gpio_chip *chip = dev_get_drvdata(dev);
331 return sprintf(buf, "%u\n", chip->ngpio);
333 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
335 static const struct attribute *gpiochip_attrs[] = {
336 &dev_attr_base.attr,
337 &dev_attr_label.attr,
338 &dev_attr_ngpio.attr,
339 NULL,
342 static const struct attribute_group gpiochip_attr_group = {
343 .attrs = (struct attribute **) gpiochip_attrs,
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
352 static ssize_t export_store(struct class *class, const char *buf, size_t len)
354 long gpio;
355 int status;
357 status = strict_strtol(buf, 0, &gpio);
358 if (status < 0)
359 goto done;
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.
366 status = gpio_request(gpio, "sysfs");
367 if (status < 0)
368 goto done;
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);
376 done:
377 if (status)
378 pr_debug("%s: status %d\n", __func__, status);
379 return status ? : len;
382 static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
384 long gpio;
385 int status;
387 status = strict_strtol(buf, 0, &gpio);
388 if (status < 0)
389 goto done;
391 status = -EINVAL;
393 /* reject bogus commands (gpio_unexport ignores them) */
394 if (!gpio_is_valid(gpio))
395 goto done;
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.
401 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
402 status = 0;
403 gpio_free(gpio);
405 done:
406 if (status)
407 pr_debug("%s: status %d\n", __func__, status);
408 return status ? : len;
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,
417 static struct class gpio_class = {
418 .name = "gpio",
419 .owner = THIS_MODULE,
421 .class_attrs = gpio_class_attrs,
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
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.
438 * Returns zero on success, else an error.
440 int gpio_export(unsigned gpio, bool direction_may_change)
442 unsigned long flags;
443 struct gpio_desc *desc;
444 int status = -EINVAL;
445 char *ioname = NULL;
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;
453 if (!gpio_is_valid(gpio))
454 goto done;
456 mutex_lock(&sysfs_lock);
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;
467 spin_unlock_irqrestore(&gpio_lock, flags);
469 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
470 ioname = desc->chip->names[gpio - desc->chip->base];
472 if (status == 0) {
473 struct device *dev;
475 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
476 desc, ioname ? ioname : "gpio%d", gpio);
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);
492 mutex_unlock(&sysfs_lock);
494 done:
495 if (status)
496 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
498 return status;
500 EXPORT_SYMBOL_GPL(gpio_export);
502 static int match_export(struct device *dev, void *data)
504 return dev_get_drvdata(dev) == data;
508 * gpio_unexport - reverse effect of gpio_export()
509 * @gpio: gpio to make unavailable
511 * This is implicit on gpio_free().
513 void gpio_unexport(unsigned gpio)
515 struct gpio_desc *desc;
516 int status = -EINVAL;
518 if (!gpio_is_valid(gpio))
519 goto done;
521 mutex_lock(&sysfs_lock);
523 desc = &gpio_desc[gpio];
525 if (test_bit(FLAG_EXPORT, &desc->flags)) {
526 struct device *dev = NULL;
528 dev = class_find_device(&gpio_class, NULL, desc, match_export);
529 if (dev) {
530 clear_bit(FLAG_EXPORT, &desc->flags);
531 put_device(dev);
532 device_unregister(dev);
533 status = 0;
534 } else
535 status = -ENODEV;
538 mutex_unlock(&sysfs_lock);
539 done:
540 if (status)
541 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
543 EXPORT_SYMBOL_GPL(gpio_unexport);
545 static int gpiochip_export(struct gpio_chip *chip)
547 int status;
548 struct device *dev;
550 /* Many systems register gpio chips for SOC support very early,
551 * before driver model support is available. In those cases we
552 * export this later, in gpiolib_sysfs_init() ... here we just
553 * verify that _some_ field of gpio_class got initialized.
555 if (!gpio_class.p)
556 return 0;
558 /* use chip->base for the ID; it's already known to be unique */
559 mutex_lock(&sysfs_lock);
560 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
561 "gpiochip%d", chip->base);
562 if (dev) {
563 status = sysfs_create_group(&dev->kobj,
564 &gpiochip_attr_group);
565 } else
566 status = -ENODEV;
567 chip->exported = (status == 0);
568 mutex_unlock(&sysfs_lock);
570 if (status) {
571 unsigned long flags;
572 unsigned gpio;
574 spin_lock_irqsave(&gpio_lock, flags);
575 gpio = chip->base;
576 while (gpio_desc[gpio].chip == chip)
577 gpio_desc[gpio++].chip = NULL;
578 spin_unlock_irqrestore(&gpio_lock, flags);
580 pr_debug("%s: chip %s status %d\n", __func__,
581 chip->label, status);
584 return status;
587 static void gpiochip_unexport(struct gpio_chip *chip)
589 int status;
590 struct device *dev;
592 mutex_lock(&sysfs_lock);
593 dev = class_find_device(&gpio_class, NULL, chip, match_export);
594 if (dev) {
595 put_device(dev);
596 device_unregister(dev);
597 chip->exported = 0;
598 status = 0;
599 } else
600 status = -ENODEV;
601 mutex_unlock(&sysfs_lock);
603 if (status)
604 pr_debug("%s: chip %s status %d\n", __func__,
605 chip->label, status);
608 static int __init gpiolib_sysfs_init(void)
610 int status;
611 unsigned long flags;
612 unsigned gpio;
614 status = class_register(&gpio_class);
615 if (status < 0)
616 return status;
618 /* Scan and register the gpio_chips which registered very
619 * early (e.g. before the class_register above was called).
621 * We run before arch_initcall() so chip->dev nodes can have
622 * registered, and so arch_initcall() can always gpio_export().
624 spin_lock_irqsave(&gpio_lock, flags);
625 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
626 struct gpio_chip *chip;
628 chip = gpio_desc[gpio].chip;
629 if (!chip || chip->exported)
630 continue;
632 spin_unlock_irqrestore(&gpio_lock, flags);
633 status = gpiochip_export(chip);
634 spin_lock_irqsave(&gpio_lock, flags);
636 spin_unlock_irqrestore(&gpio_lock, flags);
639 return status;
641 postcore_initcall(gpiolib_sysfs_init);
643 #else
644 static inline int gpiochip_export(struct gpio_chip *chip)
646 return 0;
649 static inline void gpiochip_unexport(struct gpio_chip *chip)
653 #endif /* CONFIG_GPIO_SYSFS */
656 * gpiochip_add() - register a gpio_chip
657 * @chip: the chip to register, with chip->base initialized
658 * Context: potentially before irqs or kmalloc will work
660 * Returns a negative errno if the chip can't be registered, such as
661 * because the chip->base is invalid or already associated with a
662 * different chip. Otherwise it returns zero as a success code.
664 * When gpiochip_add() is called very early during boot, so that GPIOs
665 * can be freely used, the chip->dev device must be registered before
666 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
667 * for GPIOs will fail rudely.
669 * If chip->base is negative, this requests dynamic assignment of
670 * a range of valid GPIOs.
672 int gpiochip_add(struct gpio_chip *chip)
674 unsigned long flags;
675 int status = 0;
676 unsigned id;
677 int base = chip->base;
679 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
680 && base >= 0) {
681 status = -EINVAL;
682 goto fail;
685 spin_lock_irqsave(&gpio_lock, flags);
687 if (base < 0) {
688 base = gpiochip_find_base(chip->ngpio);
689 if (base < 0) {
690 status = base;
691 goto unlock;
693 chip->base = base;
696 /* these GPIO numbers must not be managed by another gpio_chip */
697 for (id = base; id < base + chip->ngpio; id++) {
698 if (gpio_desc[id].chip != NULL) {
699 status = -EBUSY;
700 break;
703 if (status == 0) {
704 for (id = base; id < base + chip->ngpio; id++) {
705 gpio_desc[id].chip = chip;
707 /* REVISIT: most hardware initializes GPIOs as
708 * inputs (often with pullups enabled) so power
709 * usage is minimized. Linux code should set the
710 * gpio direction first thing; but until it does,
711 * we may expose the wrong direction in sysfs.
713 gpio_desc[id].flags = !chip->direction_input
714 ? (1 << FLAG_IS_OUT)
715 : 0;
719 unlock:
720 spin_unlock_irqrestore(&gpio_lock, flags);
721 if (status == 0)
722 status = gpiochip_export(chip);
723 fail:
724 /* failures here can mean systems won't boot... */
725 if (status)
726 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
727 chip->base, chip->base + chip->ngpio - 1,
728 chip->label ? : "generic");
729 return status;
731 EXPORT_SYMBOL_GPL(gpiochip_add);
734 * gpiochip_remove() - unregister a gpio_chip
735 * @chip: the chip to unregister
737 * A gpio_chip with any GPIOs still requested may not be removed.
739 int gpiochip_remove(struct gpio_chip *chip)
741 unsigned long flags;
742 int status = 0;
743 unsigned id;
745 spin_lock_irqsave(&gpio_lock, flags);
747 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
748 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
749 status = -EBUSY;
750 break;
753 if (status == 0) {
754 for (id = chip->base; id < chip->base + chip->ngpio; id++)
755 gpio_desc[id].chip = NULL;
758 spin_unlock_irqrestore(&gpio_lock, flags);
760 if (status == 0)
761 gpiochip_unexport(chip);
763 return status;
765 EXPORT_SYMBOL_GPL(gpiochip_remove);
768 /* These "optional" allocation calls help prevent drivers from stomping
769 * on each other, and help provide better diagnostics in debugfs.
770 * They're called even less than the "set direction" calls.
772 int gpio_request(unsigned gpio, const char *label)
774 struct gpio_desc *desc;
775 struct gpio_chip *chip;
776 int status = -EINVAL;
777 unsigned long flags;
779 spin_lock_irqsave(&gpio_lock, flags);
781 if (!gpio_is_valid(gpio))
782 goto done;
783 desc = &gpio_desc[gpio];
784 chip = desc->chip;
785 if (chip == NULL)
786 goto done;
788 if (!try_module_get(chip->owner))
789 goto done;
791 /* NOTE: gpio_request() can be called in early boot,
792 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
795 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
796 desc_set_label(desc, label ? : "?");
797 status = 0;
798 } else {
799 status = -EBUSY;
800 module_put(chip->owner);
801 goto done;
804 if (chip->request) {
805 /* chip->request may sleep */
806 spin_unlock_irqrestore(&gpio_lock, flags);
807 status = chip->request(chip, gpio - chip->base);
808 spin_lock_irqsave(&gpio_lock, flags);
810 if (status < 0) {
811 desc_set_label(desc, NULL);
812 module_put(chip->owner);
813 clear_bit(FLAG_REQUESTED, &desc->flags);
817 done:
818 if (status)
819 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
820 gpio, label ? : "?", status);
821 spin_unlock_irqrestore(&gpio_lock, flags);
822 return status;
824 EXPORT_SYMBOL_GPL(gpio_request);
826 void gpio_free(unsigned gpio)
828 unsigned long flags;
829 struct gpio_desc *desc;
830 struct gpio_chip *chip;
832 might_sleep();
834 if (!gpio_is_valid(gpio)) {
835 WARN_ON(extra_checks);
836 return;
839 gpio_unexport(gpio);
841 spin_lock_irqsave(&gpio_lock, flags);
843 desc = &gpio_desc[gpio];
844 chip = desc->chip;
845 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
846 if (chip->free) {
847 spin_unlock_irqrestore(&gpio_lock, flags);
848 might_sleep_if(extra_checks && chip->can_sleep);
849 chip->free(chip, gpio - chip->base);
850 spin_lock_irqsave(&gpio_lock, flags);
852 desc_set_label(desc, NULL);
853 module_put(desc->chip->owner);
854 clear_bit(FLAG_REQUESTED, &desc->flags);
855 } else
856 WARN_ON(extra_checks);
858 spin_unlock_irqrestore(&gpio_lock, flags);
860 EXPORT_SYMBOL_GPL(gpio_free);
864 * gpiochip_is_requested - return string iff signal was requested
865 * @chip: controller managing the signal
866 * @offset: of signal within controller's 0..(ngpio - 1) range
868 * Returns NULL if the GPIO is not currently requested, else a string.
869 * If debugfs support is enabled, the string returned is the label passed
870 * to gpio_request(); otherwise it is a meaningless constant.
872 * This function is for use by GPIO controller drivers. The label can
873 * help with diagnostics, and knowing that the signal is used as a GPIO
874 * can help avoid accidentally multiplexing it to another controller.
876 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
878 unsigned gpio = chip->base + offset;
880 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
881 return NULL;
882 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
883 return NULL;
884 #ifdef CONFIG_DEBUG_FS
885 return gpio_desc[gpio].label;
886 #else
887 return "?";
888 #endif
890 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
893 /* Drivers MUST set GPIO direction before making get/set calls. In
894 * some cases this is done in early boot, before IRQs are enabled.
896 * As a rule these aren't called more than once (except for drivers
897 * using the open-drain emulation idiom) so these are natural places
898 * to accumulate extra debugging checks. Note that we can't (yet)
899 * rely on gpio_request() having been called beforehand.
902 int gpio_direction_input(unsigned gpio)
904 unsigned long flags;
905 struct gpio_chip *chip;
906 struct gpio_desc *desc = &gpio_desc[gpio];
907 int status = -EINVAL;
909 spin_lock_irqsave(&gpio_lock, flags);
911 if (!gpio_is_valid(gpio))
912 goto fail;
913 chip = desc->chip;
914 if (!chip || !chip->get || !chip->direction_input)
915 goto fail;
916 gpio -= chip->base;
917 if (gpio >= chip->ngpio)
918 goto fail;
919 status = gpio_ensure_requested(desc, gpio);
920 if (status < 0)
921 goto fail;
923 /* now we know the gpio is valid and chip won't vanish */
925 spin_unlock_irqrestore(&gpio_lock, flags);
927 might_sleep_if(extra_checks && chip->can_sleep);
929 if (status) {
930 status = chip->request(chip, gpio);
931 if (status < 0) {
932 pr_debug("GPIO-%d: chip request fail, %d\n",
933 chip->base + gpio, status);
934 /* and it's not available to anyone else ...
935 * gpio_request() is the fully clean solution.
937 goto lose;
941 status = chip->direction_input(chip, gpio);
942 if (status == 0)
943 clear_bit(FLAG_IS_OUT, &desc->flags);
944 lose:
945 return status;
946 fail:
947 spin_unlock_irqrestore(&gpio_lock, flags);
948 if (status)
949 pr_debug("%s: gpio-%d status %d\n",
950 __func__, gpio, status);
951 return status;
953 EXPORT_SYMBOL_GPL(gpio_direction_input);
955 int gpio_direction_output(unsigned gpio, int value)
957 unsigned long flags;
958 struct gpio_chip *chip;
959 struct gpio_desc *desc = &gpio_desc[gpio];
960 int status = -EINVAL;
962 spin_lock_irqsave(&gpio_lock, flags);
964 if (!gpio_is_valid(gpio))
965 goto fail;
966 chip = desc->chip;
967 if (!chip || !chip->set || !chip->direction_output)
968 goto fail;
969 gpio -= chip->base;
970 if (gpio >= chip->ngpio)
971 goto fail;
972 status = gpio_ensure_requested(desc, gpio);
973 if (status < 0)
974 goto fail;
976 /* now we know the gpio is valid and chip won't vanish */
978 spin_unlock_irqrestore(&gpio_lock, flags);
980 might_sleep_if(extra_checks && chip->can_sleep);
982 if (status) {
983 status = chip->request(chip, gpio);
984 if (status < 0) {
985 pr_debug("GPIO-%d: chip request fail, %d\n",
986 chip->base + gpio, status);
987 /* and it's not available to anyone else ...
988 * gpio_request() is the fully clean solution.
990 goto lose;
994 status = chip->direction_output(chip, gpio, value);
995 if (status == 0)
996 set_bit(FLAG_IS_OUT, &desc->flags);
997 lose:
998 return status;
999 fail:
1000 spin_unlock_irqrestore(&gpio_lock, flags);
1001 if (status)
1002 pr_debug("%s: gpio-%d status %d\n",
1003 __func__, gpio, status);
1004 return status;
1006 EXPORT_SYMBOL_GPL(gpio_direction_output);
1009 /* I/O calls are only valid after configuration completed; the relevant
1010 * "is this a valid GPIO" error checks should already have been done.
1012 * "Get" operations are often inlinable as reading a pin value register,
1013 * and masking the relevant bit in that register.
1015 * When "set" operations are inlinable, they involve writing that mask to
1016 * one register to set a low value, or a different register to set it high.
1017 * Otherwise locking is needed, so there may be little value to inlining.
1019 *------------------------------------------------------------------------
1021 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1022 * have requested the GPIO. That can include implicit requesting by
1023 * a direction setting call. Marking a gpio as requested locks its chip
1024 * in memory, guaranteeing that these table lookups need no more locking
1025 * and that gpiochip_remove() will fail.
1027 * REVISIT when debugging, consider adding some instrumentation to ensure
1028 * that the GPIO was actually requested.
1032 * __gpio_get_value() - return a gpio's value
1033 * @gpio: gpio whose value will be returned
1034 * Context: any
1036 * This is used directly or indirectly to implement gpio_get_value().
1037 * It returns the zero or nonzero value provided by the associated
1038 * gpio_chip.get() method; or zero if no such method is provided.
1040 int __gpio_get_value(unsigned gpio)
1042 struct gpio_chip *chip;
1044 chip = gpio_to_chip(gpio);
1045 WARN_ON(extra_checks && chip->can_sleep);
1046 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1048 EXPORT_SYMBOL_GPL(__gpio_get_value);
1051 * __gpio_set_value() - assign a gpio's value
1052 * @gpio: gpio whose value will be assigned
1053 * @value: value to assign
1054 * Context: any
1056 * This is used directly or indirectly to implement gpio_set_value().
1057 * It invokes the associated gpio_chip.set() method.
1059 void __gpio_set_value(unsigned gpio, int value)
1061 struct gpio_chip *chip;
1063 chip = gpio_to_chip(gpio);
1064 WARN_ON(extra_checks && chip->can_sleep);
1065 chip->set(chip, gpio - chip->base, value);
1067 EXPORT_SYMBOL_GPL(__gpio_set_value);
1070 * __gpio_cansleep() - report whether gpio value access will sleep
1071 * @gpio: gpio in question
1072 * Context: any
1074 * This is used directly or indirectly to implement gpio_cansleep(). It
1075 * returns nonzero if access reading or writing the GPIO value can sleep.
1077 int __gpio_cansleep(unsigned gpio)
1079 struct gpio_chip *chip;
1081 /* only call this on GPIOs that are valid! */
1082 chip = gpio_to_chip(gpio);
1084 return chip->can_sleep;
1086 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1089 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1090 * @gpio: gpio whose IRQ will be returned (already requested)
1091 * Context: any
1093 * This is used directly or indirectly to implement gpio_to_irq().
1094 * It returns the number of the IRQ signaled by this (input) GPIO,
1095 * or a negative errno.
1097 int __gpio_to_irq(unsigned gpio)
1099 struct gpio_chip *chip;
1101 chip = gpio_to_chip(gpio);
1102 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1104 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1108 /* There's no value in making it easy to inline GPIO calls that may sleep.
1109 * Common examples include ones connected to I2C or SPI chips.
1112 int gpio_get_value_cansleep(unsigned gpio)
1114 struct gpio_chip *chip;
1116 might_sleep_if(extra_checks);
1117 chip = gpio_to_chip(gpio);
1118 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1120 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1122 void gpio_set_value_cansleep(unsigned gpio, int value)
1124 struct gpio_chip *chip;
1126 might_sleep_if(extra_checks);
1127 chip = gpio_to_chip(gpio);
1128 chip->set(chip, gpio - chip->base, value);
1130 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1133 #ifdef CONFIG_DEBUG_FS
1135 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1137 unsigned i;
1138 unsigned gpio = chip->base;
1139 struct gpio_desc *gdesc = &gpio_desc[gpio];
1140 int is_out;
1142 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1143 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1144 continue;
1146 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1147 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1148 gpio, gdesc->label,
1149 is_out ? "out" : "in ",
1150 chip->get
1151 ? (chip->get(chip, i) ? "hi" : "lo")
1152 : "? ");
1154 if (!is_out) {
1155 int irq = gpio_to_irq(gpio);
1156 struct irq_desc *desc = irq_to_desc(irq);
1158 /* This races with request_irq(), set_irq_type(),
1159 * and set_irq_wake() ... but those are "rare".
1161 * More significantly, trigger type flags aren't
1162 * currently maintained by genirq.
1164 if (irq >= 0 && desc->action) {
1165 char *trigger;
1167 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1168 case IRQ_TYPE_NONE:
1169 trigger = "(default)";
1170 break;
1171 case IRQ_TYPE_EDGE_FALLING:
1172 trigger = "edge-falling";
1173 break;
1174 case IRQ_TYPE_EDGE_RISING:
1175 trigger = "edge-rising";
1176 break;
1177 case IRQ_TYPE_EDGE_BOTH:
1178 trigger = "edge-both";
1179 break;
1180 case IRQ_TYPE_LEVEL_HIGH:
1181 trigger = "level-high";
1182 break;
1183 case IRQ_TYPE_LEVEL_LOW:
1184 trigger = "level-low";
1185 break;
1186 default:
1187 trigger = "?trigger?";
1188 break;
1191 seq_printf(s, " irq-%d %s%s",
1192 irq, trigger,
1193 (desc->status & IRQ_WAKEUP)
1194 ? " wakeup" : "");
1198 seq_printf(s, "\n");
1202 static int gpiolib_show(struct seq_file *s, void *unused)
1204 struct gpio_chip *chip = NULL;
1205 unsigned gpio;
1206 int started = 0;
1208 /* REVISIT this isn't locked against gpio_chip removal ... */
1210 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1211 struct device *dev;
1213 if (chip == gpio_desc[gpio].chip)
1214 continue;
1215 chip = gpio_desc[gpio].chip;
1216 if (!chip)
1217 continue;
1219 seq_printf(s, "%sGPIOs %d-%d",
1220 started ? "\n" : "",
1221 chip->base, chip->base + chip->ngpio - 1);
1222 dev = chip->dev;
1223 if (dev)
1224 seq_printf(s, ", %s/%s",
1225 dev->bus ? dev->bus->name : "no-bus",
1226 dev_name(dev));
1227 if (chip->label)
1228 seq_printf(s, ", %s", chip->label);
1229 if (chip->can_sleep)
1230 seq_printf(s, ", can sleep");
1231 seq_printf(s, ":\n");
1233 started = 1;
1234 if (chip->dbg_show)
1235 chip->dbg_show(s, chip);
1236 else
1237 gpiolib_dbg_show(s, chip);
1239 return 0;
1242 static int gpiolib_open(struct inode *inode, struct file *file)
1244 return single_open(file, gpiolib_show, NULL);
1247 static struct file_operations gpiolib_operations = {
1248 .open = gpiolib_open,
1249 .read = seq_read,
1250 .llseek = seq_lseek,
1251 .release = single_release,
1254 static int __init gpiolib_debugfs_init(void)
1256 /* /sys/kernel/debug/gpio */
1257 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1258 NULL, NULL, &gpiolib_operations);
1259 return 0;
1261 subsys_initcall(gpiolib_debugfs_init);
1263 #endif /* DEBUG_FS */