[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / gpio / gpiolib.c
blob662ed923d9eb0be22a49c0a1db2ae5c14e11d49d
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
4 #include <linux/irq.h>
5 #include <linux/spinlock.h>
6 #include <linux/device.h>
7 #include <linux/err.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/gpio.h>
11 #include <linux/idr.h>
14 /* Optional implementation infrastructure for GPIO interfaces.
16 * Platforms may want to use this if they tend to use very many GPIOs
17 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
19 * When kernel footprint or instruction count is an issue, simpler
20 * implementations may be preferred. The GPIO programming interface
21 * allows for inlining speed-critical get/set operations for common
22 * cases, so that access to SOC-integrated GPIOs can sometimes cost
23 * only an instruction or two per bit.
27 /* When debugging, extend minimal trust to callers and platform code.
28 * Also emit diagnostic messages that may help initial bringup, when
29 * board setup or driver bugs are most common.
31 * Otherwise, minimize overhead in what may be bitbanging codepaths.
33 #ifdef DEBUG
34 #define extra_checks 1
35 #else
36 #define extra_checks 0
37 #endif
39 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
40 * While any GPIO is requested, its gpio_chip is not removable;
41 * each GPIO's "requested" flag serves as a lock and refcount.
43 static DEFINE_SPINLOCK(gpio_lock);
45 struct gpio_desc {
46 struct gpio_chip *chip;
47 unsigned long flags;
48 /* flag symbols are bit numbers */
49 #define FLAG_REQUESTED 0
50 #define FLAG_IS_OUT 1
51 #define FLAG_RESERVED 2
52 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
53 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
54 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
55 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
57 #define PDESC_ID_SHIFT 16 /* add new flags before this one */
59 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
60 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
62 #ifdef CONFIG_DEBUG_FS
63 const char *label;
64 #endif
66 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
68 #ifdef CONFIG_GPIO_SYSFS
69 struct poll_desc {
70 struct work_struct work;
71 struct sysfs_dirent *value_sd;
74 static struct idr pdesc_idr;
75 #endif
77 static inline void desc_set_label(struct gpio_desc *d, const char *label)
79 #ifdef CONFIG_DEBUG_FS
80 d->label = label;
81 #endif
84 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
85 * when setting direction, and otherwise illegal. Until board setup code
86 * and drivers use explicit requests everywhere (which won't happen when
87 * those calls have no teeth) we can't avoid autorequesting. This nag
88 * message should motivate switching to explicit requests... so should
89 * the weaker cleanup after faults, compared to gpio_request().
91 * NOTE: the autorequest mechanism is going away; at this point it's
92 * only "legal" in the sense that (old) code using it won't break yet,
93 * but instead only triggers a WARN() stack dump.
95 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
97 const struct gpio_chip *chip = desc->chip;
98 const int gpio = chip->base + offset;
100 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
101 "autorequest GPIO-%d\n", gpio)) {
102 if (!try_module_get(chip->owner)) {
103 pr_err("GPIO-%d: module can't be gotten \n", gpio);
104 clear_bit(FLAG_REQUESTED, &desc->flags);
105 /* lose */
106 return -EIO;
108 desc_set_label(desc, "[auto]");
109 /* caller must chip->request() w/o spinlock */
110 if (chip->request)
111 return 1;
113 return 0;
116 /* caller holds gpio_lock *OR* gpio is marked as requested */
117 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
119 return gpio_desc[gpio].chip;
122 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
123 static int gpiochip_find_base(int ngpio)
125 int i;
126 int spare = 0;
127 int base = -ENOSPC;
129 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
130 struct gpio_desc *desc = &gpio_desc[i];
131 struct gpio_chip *chip = desc->chip;
133 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
134 spare++;
135 if (spare == ngpio) {
136 base = i;
137 break;
139 } else {
140 spare = 0;
141 if (chip)
142 i -= chip->ngpio - 1;
146 if (gpio_is_valid(base))
147 pr_debug("%s: found new base at %d\n", __func__, base);
148 return base;
152 * gpiochip_reserve() - reserve range of gpios to use with platform code only
153 * @start: starting gpio number
154 * @ngpio: number of gpios to reserve
155 * Context: platform init, potentially before irqs or kmalloc will work
157 * Returns a negative errno if any gpio within the range is already reserved
158 * or registered, else returns zero as a success code. Use this function
159 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
160 * for example because its driver support is not yet loaded.
162 int __init gpiochip_reserve(int start, int ngpio)
164 int ret = 0;
165 unsigned long flags;
166 int i;
168 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
169 return -EINVAL;
171 spin_lock_irqsave(&gpio_lock, flags);
173 for (i = start; i < start + ngpio; i++) {
174 struct gpio_desc *desc = &gpio_desc[i];
176 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
177 ret = -EBUSY;
178 goto err;
181 set_bit(FLAG_RESERVED, &desc->flags);
184 pr_debug("%s: reserved gpios from %d to %d\n",
185 __func__, start, start + ngpio - 1);
186 err:
187 spin_unlock_irqrestore(&gpio_lock, flags);
189 return ret;
192 #ifdef CONFIG_GPIO_SYSFS
194 /* lock protects against unexport_gpio() being called while
195 * sysfs files are active.
197 static DEFINE_MUTEX(sysfs_lock);
200 * /sys/class/gpio/gpioN... only for GPIOs that are exported
201 * /direction
202 * * MAY BE OMITTED if kernel won't allow direction changes
203 * * is read/write as "in" or "out"
204 * * may also be written as "high" or "low", initializing
205 * output value as specified ("out" implies "low")
206 * /value
207 * * always readable, subject to hardware behavior
208 * * may be writable, as zero/nonzero
209 * /edge
210 * * configures behavior of poll(2) on /value
211 * * available only if pin can generate IRQs on input
212 * * is read/write as "none", "falling", "rising", or "both"
215 static ssize_t gpio_direction_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
218 const struct gpio_desc *desc = dev_get_drvdata(dev);
219 ssize_t status;
221 mutex_lock(&sysfs_lock);
223 if (!test_bit(FLAG_EXPORT, &desc->flags))
224 status = -EIO;
225 else
226 status = sprintf(buf, "%s\n",
227 test_bit(FLAG_IS_OUT, &desc->flags)
228 ? "out" : "in");
230 mutex_unlock(&sysfs_lock);
231 return status;
234 static ssize_t gpio_direction_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t size)
237 const struct gpio_desc *desc = dev_get_drvdata(dev);
238 unsigned gpio = desc - gpio_desc;
239 ssize_t status;
241 mutex_lock(&sysfs_lock);
243 if (!test_bit(FLAG_EXPORT, &desc->flags))
244 status = -EIO;
245 else if (sysfs_streq(buf, "high"))
246 status = gpio_direction_output(gpio, 1);
247 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
248 status = gpio_direction_output(gpio, 0);
249 else if (sysfs_streq(buf, "in"))
250 status = gpio_direction_input(gpio);
251 else
252 status = -EINVAL;
254 mutex_unlock(&sysfs_lock);
255 return status ? : size;
258 static const DEVICE_ATTR(direction, 0644,
259 gpio_direction_show, gpio_direction_store);
261 static ssize_t gpio_value_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
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
273 status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
275 mutex_unlock(&sysfs_lock);
276 return status;
279 static ssize_t gpio_value_store(struct device *dev,
280 struct device_attribute *attr, const char *buf, size_t size)
282 const struct gpio_desc *desc = dev_get_drvdata(dev);
283 unsigned gpio = desc - gpio_desc;
284 ssize_t status;
286 mutex_lock(&sysfs_lock);
288 if (!test_bit(FLAG_EXPORT, &desc->flags))
289 status = -EIO;
290 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
291 status = -EPERM;
292 else {
293 long value;
295 status = strict_strtol(buf, 0, &value);
296 if (status == 0) {
297 gpio_set_value_cansleep(gpio, value != 0);
298 status = size;
302 mutex_unlock(&sysfs_lock);
303 return status;
306 static /*const*/ DEVICE_ATTR(value, 0644,
307 gpio_value_show, gpio_value_store);
309 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
311 struct work_struct *work = priv;
313 schedule_work(work);
314 return IRQ_HANDLED;
317 static void gpio_notify_sysfs(struct work_struct *work)
319 struct poll_desc *pdesc;
321 pdesc = container_of(work, struct poll_desc, work);
322 sysfs_notify_dirent(pdesc->value_sd);
325 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
326 unsigned long gpio_flags)
328 struct poll_desc *pdesc;
329 unsigned long irq_flags;
330 int ret, irq, id;
332 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
333 return 0;
335 irq = gpio_to_irq(desc - gpio_desc);
336 if (irq < 0)
337 return -EIO;
339 id = desc->flags >> PDESC_ID_SHIFT;
340 pdesc = idr_find(&pdesc_idr, id);
341 if (pdesc) {
342 free_irq(irq, &pdesc->work);
343 cancel_work_sync(&pdesc->work);
346 desc->flags &= ~GPIO_TRIGGER_MASK;
348 if (!gpio_flags) {
349 ret = 0;
350 goto free_sd;
353 irq_flags = IRQF_SHARED;
354 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
355 irq_flags |= IRQF_TRIGGER_FALLING;
356 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
357 irq_flags |= IRQF_TRIGGER_RISING;
359 if (!pdesc) {
360 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
361 if (!pdesc) {
362 ret = -ENOMEM;
363 goto err_out;
366 do {
367 ret = -ENOMEM;
368 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
369 ret = idr_get_new_above(&pdesc_idr,
370 pdesc, 1, &id);
371 } while (ret == -EAGAIN);
373 if (ret)
374 goto free_mem;
376 desc->flags &= GPIO_FLAGS_MASK;
377 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
379 if (desc->flags >> PDESC_ID_SHIFT != id) {
380 ret = -ERANGE;
381 goto free_id;
384 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
385 if (!pdesc->value_sd) {
386 ret = -ENODEV;
387 goto free_id;
389 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
392 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
393 "gpiolib", &pdesc->work);
394 if (ret)
395 goto free_sd;
397 desc->flags |= gpio_flags;
398 return 0;
400 free_sd:
401 sysfs_put(pdesc->value_sd);
402 free_id:
403 idr_remove(&pdesc_idr, id);
404 desc->flags &= GPIO_FLAGS_MASK;
405 free_mem:
406 kfree(pdesc);
407 err_out:
408 return ret;
411 static const struct {
412 const char *name;
413 unsigned long flags;
414 } trigger_types[] = {
415 { "none", 0 },
416 { "falling", BIT(FLAG_TRIG_FALL) },
417 { "rising", BIT(FLAG_TRIG_RISE) },
418 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
421 static ssize_t gpio_edge_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
424 const struct gpio_desc *desc = dev_get_drvdata(dev);
425 ssize_t status;
427 mutex_lock(&sysfs_lock);
429 if (!test_bit(FLAG_EXPORT, &desc->flags))
430 status = -EIO;
431 else {
432 int i;
434 status = 0;
435 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
436 if ((desc->flags & GPIO_TRIGGER_MASK)
437 == trigger_types[i].flags) {
438 status = sprintf(buf, "%s\n",
439 trigger_types[i].name);
440 break;
444 mutex_unlock(&sysfs_lock);
445 return status;
448 static ssize_t gpio_edge_store(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t size)
451 struct gpio_desc *desc = dev_get_drvdata(dev);
452 ssize_t status;
453 int i;
455 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
456 if (sysfs_streq(trigger_types[i].name, buf))
457 goto found;
458 return -EINVAL;
460 found:
461 mutex_lock(&sysfs_lock);
463 if (!test_bit(FLAG_EXPORT, &desc->flags))
464 status = -EIO;
465 else {
466 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
467 if (!status)
468 status = size;
471 mutex_unlock(&sysfs_lock);
473 return status;
476 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
478 static const struct attribute *gpio_attrs[] = {
479 &dev_attr_direction.attr,
480 &dev_attr_value.attr,
481 NULL,
484 static const struct attribute_group gpio_attr_group = {
485 .attrs = (struct attribute **) gpio_attrs,
489 * /sys/class/gpio/gpiochipN/
490 * /base ... matching gpio_chip.base (N)
491 * /label ... matching gpio_chip.label
492 * /ngpio ... matching gpio_chip.ngpio
495 static ssize_t chip_base_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 const struct gpio_chip *chip = dev_get_drvdata(dev);
500 return sprintf(buf, "%d\n", chip->base);
502 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
504 static ssize_t chip_label_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
507 const struct gpio_chip *chip = dev_get_drvdata(dev);
509 return sprintf(buf, "%s\n", chip->label ? : "");
511 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
513 static ssize_t chip_ngpio_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 const struct gpio_chip *chip = dev_get_drvdata(dev);
518 return sprintf(buf, "%u\n", chip->ngpio);
520 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
522 static const struct attribute *gpiochip_attrs[] = {
523 &dev_attr_base.attr,
524 &dev_attr_label.attr,
525 &dev_attr_ngpio.attr,
526 NULL,
529 static const struct attribute_group gpiochip_attr_group = {
530 .attrs = (struct attribute **) gpiochip_attrs,
534 * /sys/class/gpio/export ... write-only
535 * integer N ... number of GPIO to export (full access)
536 * /sys/class/gpio/unexport ... write-only
537 * integer N ... number of GPIO to unexport
539 static ssize_t export_store(struct class *class, const char *buf, size_t len)
541 long gpio;
542 int status;
544 status = strict_strtol(buf, 0, &gpio);
545 if (status < 0)
546 goto done;
548 /* No extra locking here; FLAG_SYSFS just signifies that the
549 * request and export were done by on behalf of userspace, so
550 * they may be undone on its behalf too.
553 status = gpio_request(gpio, "sysfs");
554 if (status < 0)
555 goto done;
557 status = gpio_export(gpio, true);
558 if (status < 0)
559 gpio_free(gpio);
560 else
561 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
563 done:
564 if (status)
565 pr_debug("%s: status %d\n", __func__, status);
566 return status ? : len;
569 static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
571 long gpio;
572 int status;
574 status = strict_strtol(buf, 0, &gpio);
575 if (status < 0)
576 goto done;
578 status = -EINVAL;
580 /* reject bogus commands (gpio_unexport ignores them) */
581 if (!gpio_is_valid(gpio))
582 goto done;
584 /* No extra locking here; FLAG_SYSFS just signifies that the
585 * request and export were done by on behalf of userspace, so
586 * they may be undone on its behalf too.
588 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
589 status = 0;
590 gpio_free(gpio);
592 done:
593 if (status)
594 pr_debug("%s: status %d\n", __func__, status);
595 return status ? : len;
598 static struct class_attribute gpio_class_attrs[] = {
599 __ATTR(export, 0200, NULL, export_store),
600 __ATTR(unexport, 0200, NULL, unexport_store),
601 __ATTR_NULL,
604 static struct class gpio_class = {
605 .name = "gpio",
606 .owner = THIS_MODULE,
608 .class_attrs = gpio_class_attrs,
613 * gpio_export - export a GPIO through sysfs
614 * @gpio: gpio to make available, already requested
615 * @direction_may_change: true if userspace may change gpio direction
616 * Context: arch_initcall or later
618 * When drivers want to make a GPIO accessible to userspace after they
619 * have requested it -- perhaps while debugging, or as part of their
620 * public interface -- they may use this routine. If the GPIO can
621 * change direction (some can't) and the caller allows it, userspace
622 * will see "direction" sysfs attribute which may be used to change
623 * the gpio's direction. A "value" attribute will always be provided.
625 * Returns zero on success, else an error.
627 int gpio_export(unsigned gpio, bool direction_may_change)
629 unsigned long flags;
630 struct gpio_desc *desc;
631 int status = -EINVAL;
632 char *ioname = NULL;
634 /* can't export until sysfs is available ... */
635 if (!gpio_class.p) {
636 pr_debug("%s: called too early!\n", __func__);
637 return -ENOENT;
640 if (!gpio_is_valid(gpio))
641 goto done;
643 mutex_lock(&sysfs_lock);
645 spin_lock_irqsave(&gpio_lock, flags);
646 desc = &gpio_desc[gpio];
647 if (test_bit(FLAG_REQUESTED, &desc->flags)
648 && !test_bit(FLAG_EXPORT, &desc->flags)) {
649 status = 0;
650 if (!desc->chip->direction_input
651 || !desc->chip->direction_output)
652 direction_may_change = false;
654 spin_unlock_irqrestore(&gpio_lock, flags);
656 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
657 ioname = desc->chip->names[gpio - desc->chip->base];
659 if (status == 0) {
660 struct device *dev;
662 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
663 desc, ioname ? ioname : "gpio%d", gpio);
664 if (dev) {
665 if (direction_may_change)
666 status = sysfs_create_group(&dev->kobj,
667 &gpio_attr_group);
668 else
669 status = device_create_file(dev,
670 &dev_attr_value);
672 if (!status && gpio_to_irq(gpio) >= 0
673 && (direction_may_change
674 || !test_bit(FLAG_IS_OUT,
675 &desc->flags)))
676 status = device_create_file(dev,
677 &dev_attr_edge);
679 if (status != 0)
680 device_unregister(dev);
681 } else
682 status = -ENODEV;
683 if (status == 0)
684 set_bit(FLAG_EXPORT, &desc->flags);
687 mutex_unlock(&sysfs_lock);
689 done:
690 if (status)
691 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
693 return status;
695 EXPORT_SYMBOL_GPL(gpio_export);
697 static int match_export(struct device *dev, void *data)
699 return dev_get_drvdata(dev) == data;
703 * gpio_export_link - create a sysfs link to an exported GPIO node
704 * @dev: device under which to create symlink
705 * @name: name of the symlink
706 * @gpio: gpio to create symlink to, already exported
708 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
709 * node. Caller is responsible for unlinking.
711 * Returns zero on success, else an error.
713 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
715 struct gpio_desc *desc;
716 int status = -EINVAL;
718 if (!gpio_is_valid(gpio))
719 goto done;
721 mutex_lock(&sysfs_lock);
723 desc = &gpio_desc[gpio];
725 if (test_bit(FLAG_EXPORT, &desc->flags)) {
726 struct device *tdev;
728 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
729 if (tdev != NULL) {
730 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
731 name);
732 } else {
733 status = -ENODEV;
737 mutex_unlock(&sysfs_lock);
739 done:
740 if (status)
741 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
743 return status;
745 EXPORT_SYMBOL_GPL(gpio_export_link);
748 * gpio_unexport - reverse effect of gpio_export()
749 * @gpio: gpio to make unavailable
751 * This is implicit on gpio_free().
753 void gpio_unexport(unsigned gpio)
755 struct gpio_desc *desc;
756 int status = -EINVAL;
758 if (!gpio_is_valid(gpio))
759 goto done;
761 mutex_lock(&sysfs_lock);
763 desc = &gpio_desc[gpio];
765 if (test_bit(FLAG_EXPORT, &desc->flags)) {
766 struct device *dev = NULL;
768 dev = class_find_device(&gpio_class, NULL, desc, match_export);
769 if (dev) {
770 gpio_setup_irq(desc, dev, 0);
771 clear_bit(FLAG_EXPORT, &desc->flags);
772 put_device(dev);
773 device_unregister(dev);
774 status = 0;
775 } else
776 status = -ENODEV;
779 mutex_unlock(&sysfs_lock);
780 done:
781 if (status)
782 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
784 EXPORT_SYMBOL_GPL(gpio_unexport);
786 static int gpiochip_export(struct gpio_chip *chip)
788 int status;
789 struct device *dev;
791 /* Many systems register gpio chips for SOC support very early,
792 * before driver model support is available. In those cases we
793 * export this later, in gpiolib_sysfs_init() ... here we just
794 * verify that _some_ field of gpio_class got initialized.
796 if (!gpio_class.p)
797 return 0;
799 /* use chip->base for the ID; it's already known to be unique */
800 mutex_lock(&sysfs_lock);
801 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
802 "gpiochip%d", chip->base);
803 if (dev) {
804 status = sysfs_create_group(&dev->kobj,
805 &gpiochip_attr_group);
806 } else
807 status = -ENODEV;
808 chip->exported = (status == 0);
809 mutex_unlock(&sysfs_lock);
811 if (status) {
812 unsigned long flags;
813 unsigned gpio;
815 spin_lock_irqsave(&gpio_lock, flags);
816 gpio = chip->base;
817 while (gpio_desc[gpio].chip == chip)
818 gpio_desc[gpio++].chip = NULL;
819 spin_unlock_irqrestore(&gpio_lock, flags);
821 pr_debug("%s: chip %s status %d\n", __func__,
822 chip->label, status);
825 return status;
828 static void gpiochip_unexport(struct gpio_chip *chip)
830 int status;
831 struct device *dev;
833 mutex_lock(&sysfs_lock);
834 dev = class_find_device(&gpio_class, NULL, chip, match_export);
835 if (dev) {
836 put_device(dev);
837 device_unregister(dev);
838 chip->exported = 0;
839 status = 0;
840 } else
841 status = -ENODEV;
842 mutex_unlock(&sysfs_lock);
844 if (status)
845 pr_debug("%s: chip %s status %d\n", __func__,
846 chip->label, status);
849 static int __init gpiolib_sysfs_init(void)
851 int status;
852 unsigned long flags;
853 unsigned gpio;
855 idr_init(&pdesc_idr);
857 status = class_register(&gpio_class);
858 if (status < 0)
859 return status;
861 /* Scan and register the gpio_chips which registered very
862 * early (e.g. before the class_register above was called).
864 * We run before arch_initcall() so chip->dev nodes can have
865 * registered, and so arch_initcall() can always gpio_export().
867 spin_lock_irqsave(&gpio_lock, flags);
868 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
869 struct gpio_chip *chip;
871 chip = gpio_desc[gpio].chip;
872 if (!chip || chip->exported)
873 continue;
875 spin_unlock_irqrestore(&gpio_lock, flags);
876 status = gpiochip_export(chip);
877 spin_lock_irqsave(&gpio_lock, flags);
879 spin_unlock_irqrestore(&gpio_lock, flags);
882 return status;
884 postcore_initcall(gpiolib_sysfs_init);
886 #else
887 static inline int gpiochip_export(struct gpio_chip *chip)
889 return 0;
892 static inline void gpiochip_unexport(struct gpio_chip *chip)
896 #endif /* CONFIG_GPIO_SYSFS */
899 * gpiochip_add() - register a gpio_chip
900 * @chip: the chip to register, with chip->base initialized
901 * Context: potentially before irqs or kmalloc will work
903 * Returns a negative errno if the chip can't be registered, such as
904 * because the chip->base is invalid or already associated with a
905 * different chip. Otherwise it returns zero as a success code.
907 * When gpiochip_add() is called very early during boot, so that GPIOs
908 * can be freely used, the chip->dev device must be registered before
909 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
910 * for GPIOs will fail rudely.
912 * If chip->base is negative, this requests dynamic assignment of
913 * a range of valid GPIOs.
915 int gpiochip_add(struct gpio_chip *chip)
917 unsigned long flags;
918 int status = 0;
919 unsigned id;
920 int base = chip->base;
922 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
923 && base >= 0) {
924 status = -EINVAL;
925 goto fail;
928 spin_lock_irqsave(&gpio_lock, flags);
930 if (base < 0) {
931 base = gpiochip_find_base(chip->ngpio);
932 if (base < 0) {
933 status = base;
934 goto unlock;
936 chip->base = base;
939 /* these GPIO numbers must not be managed by another gpio_chip */
940 for (id = base; id < base + chip->ngpio; id++) {
941 if (gpio_desc[id].chip != NULL) {
942 status = -EBUSY;
943 break;
946 if (status == 0) {
947 for (id = base; id < base + chip->ngpio; id++) {
948 gpio_desc[id].chip = chip;
950 /* REVISIT: most hardware initializes GPIOs as
951 * inputs (often with pullups enabled) so power
952 * usage is minimized. Linux code should set the
953 * gpio direction first thing; but until it does,
954 * we may expose the wrong direction in sysfs.
956 gpio_desc[id].flags = !chip->direction_input
957 ? (1 << FLAG_IS_OUT)
958 : 0;
962 unlock:
963 spin_unlock_irqrestore(&gpio_lock, flags);
964 if (status == 0)
965 status = gpiochip_export(chip);
966 fail:
967 /* failures here can mean systems won't boot... */
968 if (status)
969 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
970 chip->base, chip->base + chip->ngpio - 1,
971 chip->label ? : "generic");
972 return status;
974 EXPORT_SYMBOL_GPL(gpiochip_add);
977 * gpiochip_remove() - unregister a gpio_chip
978 * @chip: the chip to unregister
980 * A gpio_chip with any GPIOs still requested may not be removed.
982 int gpiochip_remove(struct gpio_chip *chip)
984 unsigned long flags;
985 int status = 0;
986 unsigned id;
988 spin_lock_irqsave(&gpio_lock, flags);
990 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
991 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
992 status = -EBUSY;
993 break;
996 if (status == 0) {
997 for (id = chip->base; id < chip->base + chip->ngpio; id++)
998 gpio_desc[id].chip = NULL;
1001 spin_unlock_irqrestore(&gpio_lock, flags);
1003 if (status == 0)
1004 gpiochip_unexport(chip);
1006 return status;
1008 EXPORT_SYMBOL_GPL(gpiochip_remove);
1011 /* These "optional" allocation calls help prevent drivers from stomping
1012 * on each other, and help provide better diagnostics in debugfs.
1013 * They're called even less than the "set direction" calls.
1015 int gpio_request(unsigned gpio, const char *label)
1017 struct gpio_desc *desc;
1018 struct gpio_chip *chip;
1019 int status = -EINVAL;
1020 unsigned long flags;
1022 spin_lock_irqsave(&gpio_lock, flags);
1024 if (!gpio_is_valid(gpio))
1025 goto done;
1026 desc = &gpio_desc[gpio];
1027 chip = desc->chip;
1028 if (chip == NULL)
1029 goto done;
1031 if (!try_module_get(chip->owner))
1032 goto done;
1034 /* NOTE: gpio_request() can be called in early boot,
1035 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1038 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1039 desc_set_label(desc, label ? : "?");
1040 status = 0;
1041 } else {
1042 status = -EBUSY;
1043 module_put(chip->owner);
1044 goto done;
1047 if (chip->request) {
1048 /* chip->request may sleep */
1049 spin_unlock_irqrestore(&gpio_lock, flags);
1050 status = chip->request(chip, gpio - chip->base);
1051 spin_lock_irqsave(&gpio_lock, flags);
1053 if (status < 0) {
1054 desc_set_label(desc, NULL);
1055 module_put(chip->owner);
1056 clear_bit(FLAG_REQUESTED, &desc->flags);
1060 done:
1061 if (status)
1062 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1063 gpio, label ? : "?", status);
1064 spin_unlock_irqrestore(&gpio_lock, flags);
1065 return status;
1067 EXPORT_SYMBOL_GPL(gpio_request);
1069 void gpio_free(unsigned gpio)
1071 unsigned long flags;
1072 struct gpio_desc *desc;
1073 struct gpio_chip *chip;
1075 might_sleep();
1077 if (!gpio_is_valid(gpio)) {
1078 WARN_ON(extra_checks);
1079 return;
1082 gpio_unexport(gpio);
1084 spin_lock_irqsave(&gpio_lock, flags);
1086 desc = &gpio_desc[gpio];
1087 chip = desc->chip;
1088 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1089 if (chip->free) {
1090 spin_unlock_irqrestore(&gpio_lock, flags);
1091 might_sleep_if(extra_checks && chip->can_sleep);
1092 chip->free(chip, gpio - chip->base);
1093 spin_lock_irqsave(&gpio_lock, flags);
1095 desc_set_label(desc, NULL);
1096 module_put(desc->chip->owner);
1097 clear_bit(FLAG_REQUESTED, &desc->flags);
1098 } else
1099 WARN_ON(extra_checks);
1101 spin_unlock_irqrestore(&gpio_lock, flags);
1103 EXPORT_SYMBOL_GPL(gpio_free);
1107 * gpiochip_is_requested - return string iff signal was requested
1108 * @chip: controller managing the signal
1109 * @offset: of signal within controller's 0..(ngpio - 1) range
1111 * Returns NULL if the GPIO is not currently requested, else a string.
1112 * If debugfs support is enabled, the string returned is the label passed
1113 * to gpio_request(); otherwise it is a meaningless constant.
1115 * This function is for use by GPIO controller drivers. The label can
1116 * help with diagnostics, and knowing that the signal is used as a GPIO
1117 * can help avoid accidentally multiplexing it to another controller.
1119 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1121 unsigned gpio = chip->base + offset;
1123 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1124 return NULL;
1125 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1126 return NULL;
1127 #ifdef CONFIG_DEBUG_FS
1128 return gpio_desc[gpio].label;
1129 #else
1130 return "?";
1131 #endif
1133 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1136 /* Drivers MUST set GPIO direction before making get/set calls. In
1137 * some cases this is done in early boot, before IRQs are enabled.
1139 * As a rule these aren't called more than once (except for drivers
1140 * using the open-drain emulation idiom) so these are natural places
1141 * to accumulate extra debugging checks. Note that we can't (yet)
1142 * rely on gpio_request() having been called beforehand.
1145 int gpio_direction_input(unsigned gpio)
1147 unsigned long flags;
1148 struct gpio_chip *chip;
1149 struct gpio_desc *desc = &gpio_desc[gpio];
1150 int status = -EINVAL;
1152 spin_lock_irqsave(&gpio_lock, flags);
1154 if (!gpio_is_valid(gpio))
1155 goto fail;
1156 chip = desc->chip;
1157 if (!chip || !chip->get || !chip->direction_input)
1158 goto fail;
1159 gpio -= chip->base;
1160 if (gpio >= chip->ngpio)
1161 goto fail;
1162 status = gpio_ensure_requested(desc, gpio);
1163 if (status < 0)
1164 goto fail;
1166 /* now we know the gpio is valid and chip won't vanish */
1168 spin_unlock_irqrestore(&gpio_lock, flags);
1170 might_sleep_if(extra_checks && chip->can_sleep);
1172 if (status) {
1173 status = chip->request(chip, gpio);
1174 if (status < 0) {
1175 pr_debug("GPIO-%d: chip request fail, %d\n",
1176 chip->base + gpio, status);
1177 /* and it's not available to anyone else ...
1178 * gpio_request() is the fully clean solution.
1180 goto lose;
1184 status = chip->direction_input(chip, gpio);
1185 if (status == 0)
1186 clear_bit(FLAG_IS_OUT, &desc->flags);
1187 lose:
1188 return status;
1189 fail:
1190 spin_unlock_irqrestore(&gpio_lock, flags);
1191 if (status)
1192 pr_debug("%s: gpio-%d status %d\n",
1193 __func__, gpio, status);
1194 return status;
1196 EXPORT_SYMBOL_GPL(gpio_direction_input);
1198 int gpio_direction_output(unsigned gpio, int value)
1200 unsigned long flags;
1201 struct gpio_chip *chip;
1202 struct gpio_desc *desc = &gpio_desc[gpio];
1203 int status = -EINVAL;
1205 spin_lock_irqsave(&gpio_lock, flags);
1207 if (!gpio_is_valid(gpio))
1208 goto fail;
1209 chip = desc->chip;
1210 if (!chip || !chip->set || !chip->direction_output)
1211 goto fail;
1212 gpio -= chip->base;
1213 if (gpio >= chip->ngpio)
1214 goto fail;
1215 status = gpio_ensure_requested(desc, gpio);
1216 if (status < 0)
1217 goto fail;
1219 /* now we know the gpio is valid and chip won't vanish */
1221 spin_unlock_irqrestore(&gpio_lock, flags);
1223 might_sleep_if(extra_checks && chip->can_sleep);
1225 if (status) {
1226 status = chip->request(chip, gpio);
1227 if (status < 0) {
1228 pr_debug("GPIO-%d: chip request fail, %d\n",
1229 chip->base + gpio, status);
1230 /* and it's not available to anyone else ...
1231 * gpio_request() is the fully clean solution.
1233 goto lose;
1237 status = chip->direction_output(chip, gpio, value);
1238 if (status == 0)
1239 set_bit(FLAG_IS_OUT, &desc->flags);
1240 lose:
1241 return status;
1242 fail:
1243 spin_unlock_irqrestore(&gpio_lock, flags);
1244 if (status)
1245 pr_debug("%s: gpio-%d status %d\n",
1246 __func__, gpio, status);
1247 return status;
1249 EXPORT_SYMBOL_GPL(gpio_direction_output);
1252 /* I/O calls are only valid after configuration completed; the relevant
1253 * "is this a valid GPIO" error checks should already have been done.
1255 * "Get" operations are often inlinable as reading a pin value register,
1256 * and masking the relevant bit in that register.
1258 * When "set" operations are inlinable, they involve writing that mask to
1259 * one register to set a low value, or a different register to set it high.
1260 * Otherwise locking is needed, so there may be little value to inlining.
1262 *------------------------------------------------------------------------
1264 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1265 * have requested the GPIO. That can include implicit requesting by
1266 * a direction setting call. Marking a gpio as requested locks its chip
1267 * in memory, guaranteeing that these table lookups need no more locking
1268 * and that gpiochip_remove() will fail.
1270 * REVISIT when debugging, consider adding some instrumentation to ensure
1271 * that the GPIO was actually requested.
1275 * __gpio_get_value() - return a gpio's value
1276 * @gpio: gpio whose value will be returned
1277 * Context: any
1279 * This is used directly or indirectly to implement gpio_get_value().
1280 * It returns the zero or nonzero value provided by the associated
1281 * gpio_chip.get() method; or zero if no such method is provided.
1283 int __gpio_get_value(unsigned gpio)
1285 struct gpio_chip *chip;
1287 chip = gpio_to_chip(gpio);
1288 WARN_ON(extra_checks && chip->can_sleep);
1289 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1291 EXPORT_SYMBOL_GPL(__gpio_get_value);
1294 * __gpio_set_value() - assign a gpio's value
1295 * @gpio: gpio whose value will be assigned
1296 * @value: value to assign
1297 * Context: any
1299 * This is used directly or indirectly to implement gpio_set_value().
1300 * It invokes the associated gpio_chip.set() method.
1302 void __gpio_set_value(unsigned gpio, int value)
1304 struct gpio_chip *chip;
1306 chip = gpio_to_chip(gpio);
1307 WARN_ON(extra_checks && chip->can_sleep);
1308 chip->set(chip, gpio - chip->base, value);
1310 EXPORT_SYMBOL_GPL(__gpio_set_value);
1313 * __gpio_cansleep() - report whether gpio value access will sleep
1314 * @gpio: gpio in question
1315 * Context: any
1317 * This is used directly or indirectly to implement gpio_cansleep(). It
1318 * returns nonzero if access reading or writing the GPIO value can sleep.
1320 int __gpio_cansleep(unsigned gpio)
1322 struct gpio_chip *chip;
1324 /* only call this on GPIOs that are valid! */
1325 chip = gpio_to_chip(gpio);
1327 return chip->can_sleep;
1329 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1332 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1333 * @gpio: gpio whose IRQ will be returned (already requested)
1334 * Context: any
1336 * This is used directly or indirectly to implement gpio_to_irq().
1337 * It returns the number of the IRQ signaled by this (input) GPIO,
1338 * or a negative errno.
1340 int __gpio_to_irq(unsigned gpio)
1342 struct gpio_chip *chip;
1344 chip = gpio_to_chip(gpio);
1345 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1347 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1351 /* There's no value in making it easy to inline GPIO calls that may sleep.
1352 * Common examples include ones connected to I2C or SPI chips.
1355 int gpio_get_value_cansleep(unsigned gpio)
1357 struct gpio_chip *chip;
1359 might_sleep_if(extra_checks);
1360 chip = gpio_to_chip(gpio);
1361 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1363 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1365 void gpio_set_value_cansleep(unsigned gpio, int value)
1367 struct gpio_chip *chip;
1369 might_sleep_if(extra_checks);
1370 chip = gpio_to_chip(gpio);
1371 chip->set(chip, gpio - chip->base, value);
1373 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1376 #ifdef CONFIG_DEBUG_FS
1378 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1380 unsigned i;
1381 unsigned gpio = chip->base;
1382 struct gpio_desc *gdesc = &gpio_desc[gpio];
1383 int is_out;
1385 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1386 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1387 continue;
1389 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1390 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1391 gpio, gdesc->label,
1392 is_out ? "out" : "in ",
1393 chip->get
1394 ? (chip->get(chip, i) ? "hi" : "lo")
1395 : "? ");
1397 if (!is_out) {
1398 int irq = gpio_to_irq(gpio);
1399 struct irq_desc *desc = irq_to_desc(irq);
1401 /* This races with request_irq(), set_irq_type(),
1402 * and set_irq_wake() ... but those are "rare".
1404 * More significantly, trigger type flags aren't
1405 * currently maintained by genirq.
1407 if (irq >= 0 && desc->action) {
1408 char *trigger;
1410 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1411 case IRQ_TYPE_NONE:
1412 trigger = "(default)";
1413 break;
1414 case IRQ_TYPE_EDGE_FALLING:
1415 trigger = "edge-falling";
1416 break;
1417 case IRQ_TYPE_EDGE_RISING:
1418 trigger = "edge-rising";
1419 break;
1420 case IRQ_TYPE_EDGE_BOTH:
1421 trigger = "edge-both";
1422 break;
1423 case IRQ_TYPE_LEVEL_HIGH:
1424 trigger = "level-high";
1425 break;
1426 case IRQ_TYPE_LEVEL_LOW:
1427 trigger = "level-low";
1428 break;
1429 default:
1430 trigger = "?trigger?";
1431 break;
1434 seq_printf(s, " irq-%d %s%s",
1435 irq, trigger,
1436 (desc->status & IRQ_WAKEUP)
1437 ? " wakeup" : "");
1441 seq_printf(s, "\n");
1445 static int gpiolib_show(struct seq_file *s, void *unused)
1447 struct gpio_chip *chip = NULL;
1448 unsigned gpio;
1449 int started = 0;
1451 /* REVISIT this isn't locked against gpio_chip removal ... */
1453 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1454 struct device *dev;
1456 if (chip == gpio_desc[gpio].chip)
1457 continue;
1458 chip = gpio_desc[gpio].chip;
1459 if (!chip)
1460 continue;
1462 seq_printf(s, "%sGPIOs %d-%d",
1463 started ? "\n" : "",
1464 chip->base, chip->base + chip->ngpio - 1);
1465 dev = chip->dev;
1466 if (dev)
1467 seq_printf(s, ", %s/%s",
1468 dev->bus ? dev->bus->name : "no-bus",
1469 dev_name(dev));
1470 if (chip->label)
1471 seq_printf(s, ", %s", chip->label);
1472 if (chip->can_sleep)
1473 seq_printf(s, ", can sleep");
1474 seq_printf(s, ":\n");
1476 started = 1;
1477 if (chip->dbg_show)
1478 chip->dbg_show(s, chip);
1479 else
1480 gpiolib_dbg_show(s, chip);
1482 return 0;
1485 static int gpiolib_open(struct inode *inode, struct file *file)
1487 return single_open(file, gpiolib_show, NULL);
1490 static const struct file_operations gpiolib_operations = {
1491 .open = gpiolib_open,
1492 .read = seq_read,
1493 .llseek = seq_lseek,
1494 .release = single_release,
1497 static int __init gpiolib_debugfs_init(void)
1499 /* /sys/kernel/debug/gpio */
1500 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1501 NULL, NULL, &gpiolib_operations);
1502 return 0;
1504 subsys_initcall(gpiolib_debugfs_init);
1506 #endif /* DEBUG_FS */