1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/device.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/gpio.h>
11 #include <linux/idr.h>
12 #include <linux/slab.h>
15 /* Optional implementation infrastructure for GPIO interfaces.
17 * Platforms may want to use this if they tend to use very many GPIOs
18 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
20 * When kernel footprint or instruction count is an issue, simpler
21 * implementations may be preferred. The GPIO programming interface
22 * allows for inlining speed-critical get/set operations for common
23 * cases, so that access to SOC-integrated GPIOs can sometimes cost
24 * only an instruction or two per bit.
28 /* When debugging, extend minimal trust to callers and platform code.
29 * Also emit diagnostic messages that may help initial bringup, when
30 * board setup or driver bugs are most common.
32 * Otherwise, minimize overhead in what may be bitbanging codepaths.
35 #define extra_checks 1
37 #define extra_checks 0
40 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
41 * While any GPIO is requested, its gpio_chip is not removable;
42 * each GPIO's "requested" flag serves as a lock and refcount.
44 static DEFINE_SPINLOCK(gpio_lock);
47 struct gpio_chip *chip;
49 /* flag symbols are bit numbers */
50 #define FLAG_REQUESTED 0
52 #define FLAG_RESERVED 2
53 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
54 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
55 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
56 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
57 #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
59 #define PDESC_ID_SHIFT 16 /* add new flags before this one */
61 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
62 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
64 #ifdef CONFIG_DEBUG_FS
68 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
70 #ifdef CONFIG_GPIO_SYSFS
72 struct work_struct work;
73 struct sysfs_dirent *value_sd;
76 static struct idr pdesc_idr;
79 static inline void desc_set_label(struct gpio_desc *d, const char *label)
81 #ifdef CONFIG_DEBUG_FS
86 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
87 * when setting direction, and otherwise illegal. Until board setup code
88 * and drivers use explicit requests everywhere (which won't happen when
89 * those calls have no teeth) we can't avoid autorequesting. This nag
90 * message should motivate switching to explicit requests... so should
91 * the weaker cleanup after faults, compared to gpio_request().
93 * NOTE: the autorequest mechanism is going away; at this point it's
94 * only "legal" in the sense that (old) code using it won't break yet,
95 * but instead only triggers a WARN() stack dump.
97 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
99 const struct gpio_chip *chip = desc->chip;
100 const int gpio = chip->base + offset;
102 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
103 "autorequest GPIO-%d\n", gpio)) {
104 if (!try_module_get(chip->owner)) {
105 pr_err("GPIO-%d: module can't be gotten \n", gpio);
106 clear_bit(FLAG_REQUESTED, &desc->flags);
110 desc_set_label(desc, "[auto]");
111 /* caller must chip->request() w/o spinlock */
118 /* caller holds gpio_lock *OR* gpio is marked as requested */
119 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
121 return gpio_desc[gpio].chip;
124 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
125 static int gpiochip_find_base(int ngpio)
131 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
132 struct gpio_desc *desc = &gpio_desc[i];
133 struct gpio_chip *chip = desc->chip;
135 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
137 if (spare == ngpio) {
144 i -= chip->ngpio - 1;
148 if (gpio_is_valid(base))
149 pr_debug("%s: found new base at %d\n", __func__, base);
154 * gpiochip_reserve() - reserve range of gpios to use with platform code only
155 * @start: starting gpio number
156 * @ngpio: number of gpios to reserve
157 * Context: platform init, potentially before irqs or kmalloc will work
159 * Returns a negative errno if any gpio within the range is already reserved
160 * or registered, else returns zero as a success code. Use this function
161 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
162 * for example because its driver support is not yet loaded.
164 int __init gpiochip_reserve(int start, int ngpio)
170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
173 spin_lock_irqsave(&gpio_lock, flags);
175 for (i = start; i < start + ngpio; i++) {
176 struct gpio_desc *desc = &gpio_desc[i];
178 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
183 set_bit(FLAG_RESERVED, &desc->flags);
186 pr_debug("%s: reserved gpios from %d to %d\n",
187 __func__, start, start + ngpio - 1);
189 spin_unlock_irqrestore(&gpio_lock, flags);
194 #ifdef CONFIG_GPIO_SYSFS
196 /* lock protects against unexport_gpio() being called while
197 * sysfs files are active.
199 static DEFINE_MUTEX(sysfs_lock);
202 * /sys/class/gpio/gpioN... only for GPIOs that are exported
204 * * MAY BE OMITTED if kernel won't allow direction changes
205 * * is read/write as "in" or "out"
206 * * may also be written as "high" or "low", initializing
207 * output value as specified ("out" implies "low")
209 * * always readable, subject to hardware behavior
210 * * may be writable, as zero/nonzero
212 * * configures behavior of poll(2) on /value
213 * * available only if pin can generate IRQs on input
214 * * is read/write as "none", "falling", "rising", or "both"
216 * * configures polarity of /value
217 * * is read/write as zero/nonzero
218 * * also affects existing and subsequent "falling" and "rising"
219 * /edge configuration
222 static ssize_t gpio_direction_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 const struct gpio_desc *desc = dev_get_drvdata(dev);
228 mutex_lock(&sysfs_lock);
230 if (!test_bit(FLAG_EXPORT, &desc->flags))
233 status = sprintf(buf, "%s\n",
234 test_bit(FLAG_IS_OUT, &desc->flags)
237 mutex_unlock(&sysfs_lock);
241 static ssize_t gpio_direction_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t size)
244 const struct gpio_desc *desc = dev_get_drvdata(dev);
245 unsigned gpio = desc - gpio_desc;
248 mutex_lock(&sysfs_lock);
250 if (!test_bit(FLAG_EXPORT, &desc->flags))
252 else if (sysfs_streq(buf, "high"))
253 status = gpio_direction_output(gpio, 1);
254 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
255 status = gpio_direction_output(gpio, 0);
256 else if (sysfs_streq(buf, "in"))
257 status = gpio_direction_input(gpio);
261 mutex_unlock(&sysfs_lock);
262 return status ? : size;
265 static /* const */ DEVICE_ATTR(direction, 0644,
266 gpio_direction_show, gpio_direction_store);
268 static ssize_t gpio_value_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 const struct gpio_desc *desc = dev_get_drvdata(dev);
272 unsigned gpio = desc - gpio_desc;
275 mutex_lock(&sysfs_lock);
277 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
282 value = !!gpio_get_value_cansleep(gpio);
283 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
286 status = sprintf(buf, "%d\n", value);
289 mutex_unlock(&sysfs_lock);
293 static ssize_t gpio_value_store(struct device *dev,
294 struct device_attribute *attr, const char *buf, size_t size)
296 const struct gpio_desc *desc = dev_get_drvdata(dev);
297 unsigned gpio = desc - gpio_desc;
300 mutex_lock(&sysfs_lock);
302 if (!test_bit(FLAG_EXPORT, &desc->flags))
304 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
309 status = strict_strtol(buf, 0, &value);
311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
313 gpio_set_value_cansleep(gpio, value != 0);
318 mutex_unlock(&sysfs_lock);
322 static const DEVICE_ATTR(value, 0644,
323 gpio_value_show, gpio_value_store);
325 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
327 struct work_struct *work = priv;
333 static void gpio_notify_sysfs(struct work_struct *work)
335 struct poll_desc *pdesc;
337 pdesc = container_of(work, struct poll_desc, work);
338 sysfs_notify_dirent(pdesc->value_sd);
341 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
342 unsigned long gpio_flags)
344 struct poll_desc *pdesc;
345 unsigned long irq_flags;
348 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
351 irq = gpio_to_irq(desc - gpio_desc);
355 id = desc->flags >> PDESC_ID_SHIFT;
356 pdesc = idr_find(&pdesc_idr, id);
358 free_irq(irq, &pdesc->work);
359 cancel_work_sync(&pdesc->work);
362 desc->flags &= ~GPIO_TRIGGER_MASK;
369 irq_flags = IRQF_SHARED;
370 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
371 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
372 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
373 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
374 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
375 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
378 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL);
386 if (idr_pre_get(&pdesc_idr, GFP_KERNEL))
387 ret = idr_get_new_above(&pdesc_idr,
389 } while (ret == -EAGAIN);
394 desc->flags &= GPIO_FLAGS_MASK;
395 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT;
397 if (desc->flags >> PDESC_ID_SHIFT != id) {
402 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
403 if (!pdesc->value_sd) {
407 INIT_WORK(&pdesc->work, gpio_notify_sysfs);
410 ret = request_irq(irq, gpio_sysfs_irq, irq_flags,
411 "gpiolib", &pdesc->work);
415 desc->flags |= gpio_flags;
420 sysfs_put(pdesc->value_sd);
422 idr_remove(&pdesc_idr, id);
423 desc->flags &= GPIO_FLAGS_MASK;
430 static const struct {
433 } trigger_types[] = {
435 { "falling", BIT(FLAG_TRIG_FALL) },
436 { "rising", BIT(FLAG_TRIG_RISE) },
437 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
440 static ssize_t gpio_edge_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 const struct gpio_desc *desc = dev_get_drvdata(dev);
446 mutex_lock(&sysfs_lock);
448 if (!test_bit(FLAG_EXPORT, &desc->flags))
454 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
455 if ((desc->flags & GPIO_TRIGGER_MASK)
456 == trigger_types[i].flags) {
457 status = sprintf(buf, "%s\n",
458 trigger_types[i].name);
463 mutex_unlock(&sysfs_lock);
467 static ssize_t gpio_edge_store(struct device *dev,
468 struct device_attribute *attr, const char *buf, size_t size)
470 struct gpio_desc *desc = dev_get_drvdata(dev);
474 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
475 if (sysfs_streq(trigger_types[i].name, buf))
480 mutex_lock(&sysfs_lock);
482 if (!test_bit(FLAG_EXPORT, &desc->flags))
485 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
490 mutex_unlock(&sysfs_lock);
495 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
497 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
502 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
506 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
508 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
510 /* reconfigure poll(2) support if enabled on one edge only */
511 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
512 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
513 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
515 gpio_setup_irq(desc, dev, 0);
516 status = gpio_setup_irq(desc, dev, trigger_flags);
522 static ssize_t gpio_active_low_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
525 const struct gpio_desc *desc = dev_get_drvdata(dev);
528 mutex_lock(&sysfs_lock);
530 if (!test_bit(FLAG_EXPORT, &desc->flags))
533 status = sprintf(buf, "%d\n",
534 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
536 mutex_unlock(&sysfs_lock);
541 static ssize_t gpio_active_low_store(struct device *dev,
542 struct device_attribute *attr, const char *buf, size_t size)
544 struct gpio_desc *desc = dev_get_drvdata(dev);
547 mutex_lock(&sysfs_lock);
549 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
554 status = strict_strtol(buf, 0, &value);
556 status = sysfs_set_active_low(desc, dev, value != 0);
559 mutex_unlock(&sysfs_lock);
561 return status ? : size;
564 static const DEVICE_ATTR(active_low, 0644,
565 gpio_active_low_show, gpio_active_low_store);
567 static const struct attribute *gpio_attrs[] = {
568 &dev_attr_value.attr,
569 &dev_attr_active_low.attr,
573 static const struct attribute_group gpio_attr_group = {
574 .attrs = (struct attribute **) gpio_attrs,
578 * /sys/class/gpio/gpiochipN/
579 * /base ... matching gpio_chip.base (N)
580 * /label ... matching gpio_chip.label
581 * /ngpio ... matching gpio_chip.ngpio
584 static ssize_t chip_base_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
587 const struct gpio_chip *chip = dev_get_drvdata(dev);
589 return sprintf(buf, "%d\n", chip->base);
591 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
593 static ssize_t chip_label_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 const struct gpio_chip *chip = dev_get_drvdata(dev);
598 return sprintf(buf, "%s\n", chip->label ? : "");
600 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
602 static ssize_t chip_ngpio_show(struct device *dev,
603 struct device_attribute *attr, char *buf)
605 const struct gpio_chip *chip = dev_get_drvdata(dev);
607 return sprintf(buf, "%u\n", chip->ngpio);
609 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
611 static const struct attribute *gpiochip_attrs[] = {
613 &dev_attr_label.attr,
614 &dev_attr_ngpio.attr,
618 static const struct attribute_group gpiochip_attr_group = {
619 .attrs = (struct attribute **) gpiochip_attrs,
623 * /sys/class/gpio/export ... write-only
624 * integer N ... number of GPIO to export (full access)
625 * /sys/class/gpio/unexport ... write-only
626 * integer N ... number of GPIO to unexport
628 static ssize_t export_store(struct class *class,
629 struct class_attribute *attr,
630 const char *buf, size_t len)
635 status = strict_strtol(buf, 0, &gpio);
639 /* No extra locking here; FLAG_SYSFS just signifies that the
640 * request and export were done by on behalf of userspace, so
641 * they may be undone on its behalf too.
644 status = gpio_request(gpio, "sysfs");
648 status = gpio_export(gpio, true);
652 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
656 pr_debug("%s: status %d\n", __func__, status);
657 return status ? : len;
660 static ssize_t unexport_store(struct class *class,
661 struct class_attribute *attr,
662 const char *buf, size_t len)
667 status = strict_strtol(buf, 0, &gpio);
673 /* reject bogus commands (gpio_unexport ignores them) */
674 if (!gpio_is_valid(gpio))
677 /* No extra locking here; FLAG_SYSFS just signifies that the
678 * request and export were done by on behalf of userspace, so
679 * they may be undone on its behalf too.
681 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
687 pr_debug("%s: status %d\n", __func__, status);
688 return status ? : len;
691 static struct class_attribute gpio_class_attrs[] = {
692 __ATTR(export, 0200, NULL, export_store),
693 __ATTR(unexport, 0200, NULL, unexport_store),
697 static struct class gpio_class = {
699 .owner = THIS_MODULE,
701 .class_attrs = gpio_class_attrs,
706 * gpio_export - export a GPIO through sysfs
707 * @gpio: gpio to make available, already requested
708 * @direction_may_change: true if userspace may change gpio direction
709 * Context: arch_initcall or later
711 * When drivers want to make a GPIO accessible to userspace after they
712 * have requested it -- perhaps while debugging, or as part of their
713 * public interface -- they may use this routine. If the GPIO can
714 * change direction (some can't) and the caller allows it, userspace
715 * will see "direction" sysfs attribute which may be used to change
716 * the gpio's direction. A "value" attribute will always be provided.
718 * Returns zero on success, else an error.
720 int gpio_export(unsigned gpio, bool direction_may_change)
723 struct gpio_desc *desc;
724 int status = -EINVAL;
725 const char *ioname = NULL;
727 /* can't export until sysfs is available ... */
729 pr_debug("%s: called too early!\n", __func__);
733 if (!gpio_is_valid(gpio))
736 mutex_lock(&sysfs_lock);
738 spin_lock_irqsave(&gpio_lock, flags);
739 desc = &gpio_desc[gpio];
740 if (test_bit(FLAG_REQUESTED, &desc->flags)
741 && !test_bit(FLAG_EXPORT, &desc->flags)) {
743 if (!desc->chip->direction_input
744 || !desc->chip->direction_output)
745 direction_may_change = false;
747 spin_unlock_irqrestore(&gpio_lock, flags);
749 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
750 ioname = desc->chip->names[gpio - desc->chip->base];
755 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
756 desc, ioname ? ioname : "gpio%u", gpio);
758 status = sysfs_create_group(&dev->kobj,
761 if (!status && direction_may_change)
762 status = device_create_file(dev,
763 &dev_attr_direction);
765 if (!status && gpio_to_irq(gpio) >= 0
766 && (direction_may_change
767 || !test_bit(FLAG_IS_OUT,
769 status = device_create_file(dev,
773 device_unregister(dev);
775 status = PTR_ERR(dev);
777 set_bit(FLAG_EXPORT, &desc->flags);
780 mutex_unlock(&sysfs_lock);
784 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
788 EXPORT_SYMBOL_GPL(gpio_export);
790 static int match_export(struct device *dev, void *data)
792 return dev_get_drvdata(dev) == data;
796 * gpio_export_link - create a sysfs link to an exported GPIO node
797 * @dev: device under which to create symlink
798 * @name: name of the symlink
799 * @gpio: gpio to create symlink to, already exported
801 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
802 * node. Caller is responsible for unlinking.
804 * Returns zero on success, else an error.
806 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
808 struct gpio_desc *desc;
809 int status = -EINVAL;
811 if (!gpio_is_valid(gpio))
814 mutex_lock(&sysfs_lock);
816 desc = &gpio_desc[gpio];
818 if (test_bit(FLAG_EXPORT, &desc->flags)) {
821 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
823 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
830 mutex_unlock(&sysfs_lock);
834 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
838 EXPORT_SYMBOL_GPL(gpio_export_link);
842 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
843 * @gpio: gpio to change
844 * @value: non-zero to use active low, i.e. inverted values
846 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
847 * The GPIO does not have to be exported yet. If poll(2) support has
848 * been enabled for either rising or falling edge, it will be
849 * reconfigured to follow the new polarity.
851 * Returns zero on success, else an error.
853 int gpio_sysfs_set_active_low(unsigned gpio, int value)
855 struct gpio_desc *desc;
856 struct device *dev = NULL;
857 int status = -EINVAL;
859 if (!gpio_is_valid(gpio))
862 mutex_lock(&sysfs_lock);
864 desc = &gpio_desc[gpio];
866 if (test_bit(FLAG_EXPORT, &desc->flags)) {
867 dev = class_find_device(&gpio_class, NULL, desc, match_export);
874 status = sysfs_set_active_low(desc, dev, value);
877 mutex_unlock(&sysfs_lock);
881 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
885 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
888 * gpio_unexport - reverse effect of gpio_export()
889 * @gpio: gpio to make unavailable
891 * This is implicit on gpio_free().
893 void gpio_unexport(unsigned gpio)
895 struct gpio_desc *desc;
896 int status = -EINVAL;
898 if (!gpio_is_valid(gpio))
901 mutex_lock(&sysfs_lock);
903 desc = &gpio_desc[gpio];
905 if (test_bit(FLAG_EXPORT, &desc->flags)) {
906 struct device *dev = NULL;
908 dev = class_find_device(&gpio_class, NULL, desc, match_export);
910 gpio_setup_irq(desc, dev, 0);
911 clear_bit(FLAG_EXPORT, &desc->flags);
913 device_unregister(dev);
919 mutex_unlock(&sysfs_lock);
922 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
924 EXPORT_SYMBOL_GPL(gpio_unexport);
926 static int gpiochip_export(struct gpio_chip *chip)
931 /* Many systems register gpio chips for SOC support very early,
932 * before driver model support is available. In those cases we
933 * export this later, in gpiolib_sysfs_init() ... here we just
934 * verify that _some_ field of gpio_class got initialized.
939 /* use chip->base for the ID; it's already known to be unique */
940 mutex_lock(&sysfs_lock);
941 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
942 "gpiochip%d", chip->base);
944 status = sysfs_create_group(&dev->kobj,
945 &gpiochip_attr_group);
947 status = PTR_ERR(dev);
948 chip->exported = (status == 0);
949 mutex_unlock(&sysfs_lock);
955 spin_lock_irqsave(&gpio_lock, flags);
957 while (gpio_desc[gpio].chip == chip)
958 gpio_desc[gpio++].chip = NULL;
959 spin_unlock_irqrestore(&gpio_lock, flags);
961 pr_debug("%s: chip %s status %d\n", __func__,
962 chip->label, status);
968 static void gpiochip_unexport(struct gpio_chip *chip)
973 mutex_lock(&sysfs_lock);
974 dev = class_find_device(&gpio_class, NULL, chip, match_export);
977 device_unregister(dev);
982 mutex_unlock(&sysfs_lock);
985 pr_debug("%s: chip %s status %d\n", __func__,
986 chip->label, status);
989 static int __init gpiolib_sysfs_init(void)
995 idr_init(&pdesc_idr);
997 status = class_register(&gpio_class);
1001 /* Scan and register the gpio_chips which registered very
1002 * early (e.g. before the class_register above was called).
1004 * We run before arch_initcall() so chip->dev nodes can have
1005 * registered, and so arch_initcall() can always gpio_export().
1007 spin_lock_irqsave(&gpio_lock, flags);
1008 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1009 struct gpio_chip *chip;
1011 chip = gpio_desc[gpio].chip;
1012 if (!chip || chip->exported)
1015 spin_unlock_irqrestore(&gpio_lock, flags);
1016 status = gpiochip_export(chip);
1017 spin_lock_irqsave(&gpio_lock, flags);
1019 spin_unlock_irqrestore(&gpio_lock, flags);
1024 postcore_initcall(gpiolib_sysfs_init);
1027 static inline int gpiochip_export(struct gpio_chip *chip)
1032 static inline void gpiochip_unexport(struct gpio_chip *chip)
1036 #endif /* CONFIG_GPIO_SYSFS */
1039 * gpiochip_add() - register a gpio_chip
1040 * @chip: the chip to register, with chip->base initialized
1041 * Context: potentially before irqs or kmalloc will work
1043 * Returns a negative errno if the chip can't be registered, such as
1044 * because the chip->base is invalid or already associated with a
1045 * different chip. Otherwise it returns zero as a success code.
1047 * When gpiochip_add() is called very early during boot, so that GPIOs
1048 * can be freely used, the chip->dev device must be registered before
1049 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1050 * for GPIOs will fail rudely.
1052 * If chip->base is negative, this requests dynamic assignment of
1053 * a range of valid GPIOs.
1055 int gpiochip_add(struct gpio_chip *chip)
1057 unsigned long flags;
1060 int base = chip->base;
1062 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1068 spin_lock_irqsave(&gpio_lock, flags);
1071 base = gpiochip_find_base(chip->ngpio);
1079 /* these GPIO numbers must not be managed by another gpio_chip */
1080 for (id = base; id < base + chip->ngpio; id++) {
1081 if (gpio_desc[id].chip != NULL) {
1087 for (id = base; id < base + chip->ngpio; id++) {
1088 gpio_desc[id].chip = chip;
1090 /* REVISIT: most hardware initializes GPIOs as
1091 * inputs (often with pullups enabled) so power
1092 * usage is minimized. Linux code should set the
1093 * gpio direction first thing; but until it does,
1094 * we may expose the wrong direction in sysfs.
1096 gpio_desc[id].flags = !chip->direction_input
1097 ? (1 << FLAG_IS_OUT)
1103 spin_unlock_irqrestore(&gpio_lock, flags);
1108 status = gpiochip_export(chip);
1114 /* failures here can mean systems won't boot... */
1115 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1116 chip->base, chip->base + chip->ngpio - 1,
1117 chip->label ? : "generic");
1120 EXPORT_SYMBOL_GPL(gpiochip_add);
1123 * gpiochip_remove() - unregister a gpio_chip
1124 * @chip: the chip to unregister
1126 * A gpio_chip with any GPIOs still requested may not be removed.
1128 int gpiochip_remove(struct gpio_chip *chip)
1130 unsigned long flags;
1134 spin_lock_irqsave(&gpio_lock, flags);
1136 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1137 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1143 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1144 gpio_desc[id].chip = NULL;
1147 spin_unlock_irqrestore(&gpio_lock, flags);
1150 gpiochip_unexport(chip);
1154 EXPORT_SYMBOL_GPL(gpiochip_remove);
1157 /* These "optional" allocation calls help prevent drivers from stomping
1158 * on each other, and help provide better diagnostics in debugfs.
1159 * They're called even less than the "set direction" calls.
1161 int gpio_request(unsigned gpio, const char *label)
1163 struct gpio_desc *desc;
1164 struct gpio_chip *chip;
1165 int status = -EINVAL;
1166 unsigned long flags;
1168 spin_lock_irqsave(&gpio_lock, flags);
1170 if (!gpio_is_valid(gpio))
1172 desc = &gpio_desc[gpio];
1177 if (!try_module_get(chip->owner))
1180 /* NOTE: gpio_request() can be called in early boot,
1181 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1184 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1185 desc_set_label(desc, label ? : "?");
1189 module_put(chip->owner);
1193 if (chip->request) {
1194 /* chip->request may sleep */
1195 spin_unlock_irqrestore(&gpio_lock, flags);
1196 status = chip->request(chip, gpio - chip->base);
1197 spin_lock_irqsave(&gpio_lock, flags);
1200 desc_set_label(desc, NULL);
1201 module_put(chip->owner);
1202 clear_bit(FLAG_REQUESTED, &desc->flags);
1208 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1209 gpio, label ? : "?", status);
1210 spin_unlock_irqrestore(&gpio_lock, flags);
1213 EXPORT_SYMBOL_GPL(gpio_request);
1215 void gpio_free(unsigned gpio)
1217 unsigned long flags;
1218 struct gpio_desc *desc;
1219 struct gpio_chip *chip;
1223 if (!gpio_is_valid(gpio)) {
1224 WARN_ON(extra_checks);
1228 gpio_unexport(gpio);
1230 spin_lock_irqsave(&gpio_lock, flags);
1232 desc = &gpio_desc[gpio];
1234 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1236 spin_unlock_irqrestore(&gpio_lock, flags);
1237 might_sleep_if(extra_checks && chip->can_sleep);
1238 chip->free(chip, gpio - chip->base);
1239 spin_lock_irqsave(&gpio_lock, flags);
1241 desc_set_label(desc, NULL);
1242 module_put(desc->chip->owner);
1243 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1244 clear_bit(FLAG_REQUESTED, &desc->flags);
1246 WARN_ON(extra_checks);
1248 spin_unlock_irqrestore(&gpio_lock, flags);
1250 EXPORT_SYMBOL_GPL(gpio_free);
1253 * gpio_request_one - request a single GPIO with initial configuration
1254 * @gpio: the GPIO number
1255 * @flags: GPIO configuration as specified by GPIOF_*
1256 * @label: a literal description string of this GPIO
1258 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1262 err = gpio_request(gpio, label);
1266 if (flags & GPIOF_DIR_IN)
1267 err = gpio_direction_input(gpio);
1269 err = gpio_direction_output(gpio,
1270 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1274 EXPORT_SYMBOL_GPL(gpio_request_one);
1277 * gpio_request_array - request multiple GPIOs in a single call
1278 * @array: array of the 'struct gpio'
1279 * @num: how many GPIOs in the array
1281 int gpio_request_array(struct gpio *array, size_t num)
1285 for (i = 0; i < num; i++, array++) {
1286 err = gpio_request_one(array->gpio, array->flags, array->label);
1294 gpio_free((--array)->gpio);
1297 EXPORT_SYMBOL_GPL(gpio_request_array);
1300 * gpio_free_array - release multiple GPIOs in a single call
1301 * @array: array of the 'struct gpio'
1302 * @num: how many GPIOs in the array
1304 void gpio_free_array(struct gpio *array, size_t num)
1307 gpio_free((array++)->gpio);
1309 EXPORT_SYMBOL_GPL(gpio_free_array);
1312 * gpiochip_is_requested - return string iff signal was requested
1313 * @chip: controller managing the signal
1314 * @offset: of signal within controller's 0..(ngpio - 1) range
1316 * Returns NULL if the GPIO is not currently requested, else a string.
1317 * If debugfs support is enabled, the string returned is the label passed
1318 * to gpio_request(); otherwise it is a meaningless constant.
1320 * This function is for use by GPIO controller drivers. The label can
1321 * help with diagnostics, and knowing that the signal is used as a GPIO
1322 * can help avoid accidentally multiplexing it to another controller.
1324 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1326 unsigned gpio = chip->base + offset;
1328 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1330 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1332 #ifdef CONFIG_DEBUG_FS
1333 return gpio_desc[gpio].label;
1338 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1341 /* Drivers MUST set GPIO direction before making get/set calls. In
1342 * some cases this is done in early boot, before IRQs are enabled.
1344 * As a rule these aren't called more than once (except for drivers
1345 * using the open-drain emulation idiom) so these are natural places
1346 * to accumulate extra debugging checks. Note that we can't (yet)
1347 * rely on gpio_request() having been called beforehand.
1350 int gpio_direction_input(unsigned gpio)
1352 unsigned long flags;
1353 struct gpio_chip *chip;
1354 struct gpio_desc *desc = &gpio_desc[gpio];
1355 int status = -EINVAL;
1357 spin_lock_irqsave(&gpio_lock, flags);
1359 if (!gpio_is_valid(gpio))
1362 if (!chip || !chip->get || !chip->direction_input)
1365 if (gpio >= chip->ngpio)
1367 status = gpio_ensure_requested(desc, gpio);
1371 /* now we know the gpio is valid and chip won't vanish */
1373 spin_unlock_irqrestore(&gpio_lock, flags);
1375 might_sleep_if(extra_checks && chip->can_sleep);
1378 status = chip->request(chip, gpio);
1380 pr_debug("GPIO-%d: chip request fail, %d\n",
1381 chip->base + gpio, status);
1382 /* and it's not available to anyone else ...
1383 * gpio_request() is the fully clean solution.
1389 status = chip->direction_input(chip, gpio);
1391 clear_bit(FLAG_IS_OUT, &desc->flags);
1395 spin_unlock_irqrestore(&gpio_lock, flags);
1397 pr_debug("%s: gpio-%d status %d\n",
1398 __func__, gpio, status);
1401 EXPORT_SYMBOL_GPL(gpio_direction_input);
1403 int gpio_direction_output(unsigned gpio, int value)
1405 unsigned long flags;
1406 struct gpio_chip *chip;
1407 struct gpio_desc *desc = &gpio_desc[gpio];
1408 int status = -EINVAL;
1410 spin_lock_irqsave(&gpio_lock, flags);
1412 if (!gpio_is_valid(gpio))
1415 if (!chip || !chip->set || !chip->direction_output)
1418 if (gpio >= chip->ngpio)
1420 status = gpio_ensure_requested(desc, gpio);
1424 /* now we know the gpio is valid and chip won't vanish */
1426 spin_unlock_irqrestore(&gpio_lock, flags);
1428 might_sleep_if(extra_checks && chip->can_sleep);
1431 status = chip->request(chip, gpio);
1433 pr_debug("GPIO-%d: chip request fail, %d\n",
1434 chip->base + gpio, status);
1435 /* and it's not available to anyone else ...
1436 * gpio_request() is the fully clean solution.
1442 status = chip->direction_output(chip, gpio, value);
1444 set_bit(FLAG_IS_OUT, &desc->flags);
1448 spin_unlock_irqrestore(&gpio_lock, flags);
1450 pr_debug("%s: gpio-%d status %d\n",
1451 __func__, gpio, status);
1454 EXPORT_SYMBOL_GPL(gpio_direction_output);
1457 * gpio_set_debounce - sets @debounce time for a @gpio
1458 * @gpio: the gpio to set debounce time
1459 * @debounce: debounce time is microseconds
1461 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1463 unsigned long flags;
1464 struct gpio_chip *chip;
1465 struct gpio_desc *desc = &gpio_desc[gpio];
1466 int status = -EINVAL;
1468 spin_lock_irqsave(&gpio_lock, flags);
1470 if (!gpio_is_valid(gpio))
1473 if (!chip || !chip->set || !chip->set_debounce)
1476 if (gpio >= chip->ngpio)
1478 status = gpio_ensure_requested(desc, gpio);
1482 /* now we know the gpio is valid and chip won't vanish */
1484 spin_unlock_irqrestore(&gpio_lock, flags);
1486 might_sleep_if(extra_checks && chip->can_sleep);
1488 return chip->set_debounce(chip, gpio, debounce);
1491 spin_unlock_irqrestore(&gpio_lock, flags);
1493 pr_debug("%s: gpio-%d status %d\n",
1494 __func__, gpio, status);
1498 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1500 /* I/O calls are only valid after configuration completed; the relevant
1501 * "is this a valid GPIO" error checks should already have been done.
1503 * "Get" operations are often inlinable as reading a pin value register,
1504 * and masking the relevant bit in that register.
1506 * When "set" operations are inlinable, they involve writing that mask to
1507 * one register to set a low value, or a different register to set it high.
1508 * Otherwise locking is needed, so there may be little value to inlining.
1510 *------------------------------------------------------------------------
1512 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1513 * have requested the GPIO. That can include implicit requesting by
1514 * a direction setting call. Marking a gpio as requested locks its chip
1515 * in memory, guaranteeing that these table lookups need no more locking
1516 * and that gpiochip_remove() will fail.
1518 * REVISIT when debugging, consider adding some instrumentation to ensure
1519 * that the GPIO was actually requested.
1523 * __gpio_get_value() - return a gpio's value
1524 * @gpio: gpio whose value will be returned
1527 * This is used directly or indirectly to implement gpio_get_value().
1528 * It returns the zero or nonzero value provided by the associated
1529 * gpio_chip.get() method; or zero if no such method is provided.
1531 int __gpio_get_value(unsigned gpio)
1533 struct gpio_chip *chip;
1535 chip = gpio_to_chip(gpio);
1536 WARN_ON(extra_checks && chip->can_sleep);
1537 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1539 EXPORT_SYMBOL_GPL(__gpio_get_value);
1542 * __gpio_set_value() - assign a gpio's value
1543 * @gpio: gpio whose value will be assigned
1544 * @value: value to assign
1547 * This is used directly or indirectly to implement gpio_set_value().
1548 * It invokes the associated gpio_chip.set() method.
1550 void __gpio_set_value(unsigned gpio, int value)
1552 struct gpio_chip *chip;
1554 chip = gpio_to_chip(gpio);
1555 WARN_ON(extra_checks && chip->can_sleep);
1556 chip->set(chip, gpio - chip->base, value);
1558 EXPORT_SYMBOL_GPL(__gpio_set_value);
1561 * __gpio_cansleep() - report whether gpio value access will sleep
1562 * @gpio: gpio in question
1565 * This is used directly or indirectly to implement gpio_cansleep(). It
1566 * returns nonzero if access reading or writing the GPIO value can sleep.
1568 int __gpio_cansleep(unsigned gpio)
1570 struct gpio_chip *chip;
1572 /* only call this on GPIOs that are valid! */
1573 chip = gpio_to_chip(gpio);
1575 return chip->can_sleep;
1577 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1580 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1581 * @gpio: gpio whose IRQ will be returned (already requested)
1584 * This is used directly or indirectly to implement gpio_to_irq().
1585 * It returns the number of the IRQ signaled by this (input) GPIO,
1586 * or a negative errno.
1588 int __gpio_to_irq(unsigned gpio)
1590 struct gpio_chip *chip;
1592 chip = gpio_to_chip(gpio);
1593 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1595 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1599 /* There's no value in making it easy to inline GPIO calls that may sleep.
1600 * Common examples include ones connected to I2C or SPI chips.
1603 int gpio_get_value_cansleep(unsigned gpio)
1605 struct gpio_chip *chip;
1607 might_sleep_if(extra_checks);
1608 chip = gpio_to_chip(gpio);
1609 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1611 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1613 void gpio_set_value_cansleep(unsigned gpio, int value)
1615 struct gpio_chip *chip;
1617 might_sleep_if(extra_checks);
1618 chip = gpio_to_chip(gpio);
1619 chip->set(chip, gpio - chip->base, value);
1621 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1624 #ifdef CONFIG_DEBUG_FS
1626 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1629 unsigned gpio = chip->base;
1630 struct gpio_desc *gdesc = &gpio_desc[gpio];
1633 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1634 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1637 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1638 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1640 is_out ? "out" : "in ",
1642 ? (chip->get(chip, i) ? "hi" : "lo")
1646 int irq = gpio_to_irq(gpio);
1647 struct irq_desc *desc = irq_to_desc(irq);
1649 /* This races with request_irq(), set_irq_type(),
1650 * and set_irq_wake() ... but those are "rare".
1652 * More significantly, trigger type flags aren't
1653 * currently maintained by genirq.
1655 if (irq >= 0 && desc->action) {
1658 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1660 trigger = "(default)";
1662 case IRQ_TYPE_EDGE_FALLING:
1663 trigger = "edge-falling";
1665 case IRQ_TYPE_EDGE_RISING:
1666 trigger = "edge-rising";
1668 case IRQ_TYPE_EDGE_BOTH:
1669 trigger = "edge-both";
1671 case IRQ_TYPE_LEVEL_HIGH:
1672 trigger = "level-high";
1674 case IRQ_TYPE_LEVEL_LOW:
1675 trigger = "level-low";
1678 trigger = "?trigger?";
1682 seq_printf(s, " irq-%d %s%s",
1684 (desc->status & IRQ_WAKEUP)
1689 seq_printf(s, "\n");
1693 static int gpiolib_show(struct seq_file *s, void *unused)
1695 struct gpio_chip *chip = NULL;
1699 /* REVISIT this isn't locked against gpio_chip removal ... */
1701 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1704 if (chip == gpio_desc[gpio].chip)
1706 chip = gpio_desc[gpio].chip;
1710 seq_printf(s, "%sGPIOs %d-%d",
1711 started ? "\n" : "",
1712 chip->base, chip->base + chip->ngpio - 1);
1715 seq_printf(s, ", %s/%s",
1716 dev->bus ? dev->bus->name : "no-bus",
1719 seq_printf(s, ", %s", chip->label);
1720 if (chip->can_sleep)
1721 seq_printf(s, ", can sleep");
1722 seq_printf(s, ":\n");
1726 chip->dbg_show(s, chip);
1728 gpiolib_dbg_show(s, chip);
1733 static int gpiolib_open(struct inode *inode, struct file *file)
1735 return single_open(file, gpiolib_show, NULL);
1738 static const struct file_operations gpiolib_operations = {
1739 .open = gpiolib_open,
1741 .llseek = seq_lseek,
1742 .release = single_release,
1745 static int __init gpiolib_debugfs_init(void)
1747 /* /sys/kernel/debug/gpio */
1748 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1749 NULL, NULL, &gpiolib_operations);
1752 subsys_initcall(gpiolib_debugfs_init);
1754 #endif /* DEBUG_FS */