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, "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;
419 sysfs_put(pdesc->value_sd);
421 idr_remove(&pdesc_idr, id);
422 desc->flags &= GPIO_FLAGS_MASK;
429 static const struct {
432 } trigger_types[] = {
434 { "falling", BIT(FLAG_TRIG_FALL) },
435 { "rising", BIT(FLAG_TRIG_RISE) },
436 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
439 static ssize_t gpio_edge_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
442 const struct gpio_desc *desc = dev_get_drvdata(dev);
445 mutex_lock(&sysfs_lock);
447 if (!test_bit(FLAG_EXPORT, &desc->flags))
453 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
454 if ((desc->flags & GPIO_TRIGGER_MASK)
455 == trigger_types[i].flags) {
456 status = sprintf(buf, "%s\n",
457 trigger_types[i].name);
462 mutex_unlock(&sysfs_lock);
466 static ssize_t gpio_edge_store(struct device *dev,
467 struct device_attribute *attr, const char *buf, size_t size)
469 struct gpio_desc *desc = dev_get_drvdata(dev);
473 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
474 if (sysfs_streq(trigger_types[i].name, buf))
479 mutex_lock(&sysfs_lock);
481 if (!test_bit(FLAG_EXPORT, &desc->flags))
484 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
489 mutex_unlock(&sysfs_lock);
494 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
496 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
501 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
505 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
507 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
509 /* reconfigure poll(2) support if enabled on one edge only */
510 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
511 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
512 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
514 gpio_setup_irq(desc, dev, 0);
515 status = gpio_setup_irq(desc, dev, trigger_flags);
521 static ssize_t gpio_active_low_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
524 const struct gpio_desc *desc = dev_get_drvdata(dev);
527 mutex_lock(&sysfs_lock);
529 if (!test_bit(FLAG_EXPORT, &desc->flags))
532 status = sprintf(buf, "%d\n",
533 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
535 mutex_unlock(&sysfs_lock);
540 static ssize_t gpio_active_low_store(struct device *dev,
541 struct device_attribute *attr, const char *buf, size_t size)
543 struct gpio_desc *desc = dev_get_drvdata(dev);
546 mutex_lock(&sysfs_lock);
548 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
553 status = strict_strtol(buf, 0, &value);
555 status = sysfs_set_active_low(desc, dev, value != 0);
558 mutex_unlock(&sysfs_lock);
560 return status ? : size;
563 static const DEVICE_ATTR(active_low, 0644,
564 gpio_active_low_show, gpio_active_low_store);
566 static const struct attribute *gpio_attrs[] = {
567 &dev_attr_value.attr,
568 &dev_attr_active_low.attr,
572 static const struct attribute_group gpio_attr_group = {
573 .attrs = (struct attribute **) gpio_attrs,
577 * /sys/class/gpio/gpiochipN/
578 * /base ... matching gpio_chip.base (N)
579 * /label ... matching gpio_chip.label
580 * /ngpio ... matching gpio_chip.ngpio
583 static ssize_t chip_base_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 const struct gpio_chip *chip = dev_get_drvdata(dev);
588 return sprintf(buf, "%d\n", chip->base);
590 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
592 static ssize_t chip_label_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 const struct gpio_chip *chip = dev_get_drvdata(dev);
597 return sprintf(buf, "%s\n", chip->label ? : "");
599 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
601 static ssize_t chip_ngpio_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 const struct gpio_chip *chip = dev_get_drvdata(dev);
606 return sprintf(buf, "%u\n", chip->ngpio);
608 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
610 static const struct attribute *gpiochip_attrs[] = {
612 &dev_attr_label.attr,
613 &dev_attr_ngpio.attr,
617 static const struct attribute_group gpiochip_attr_group = {
618 .attrs = (struct attribute **) gpiochip_attrs,
622 * /sys/class/gpio/export ... write-only
623 * integer N ... number of GPIO to export (full access)
624 * /sys/class/gpio/unexport ... write-only
625 * integer N ... number of GPIO to unexport
627 static ssize_t export_store(struct class *class,
628 struct class_attribute *attr,
629 const char *buf, size_t len)
634 status = strict_strtol(buf, 0, &gpio);
638 /* No extra locking here; FLAG_SYSFS just signifies that the
639 * request and export were done by on behalf of userspace, so
640 * they may be undone on its behalf too.
643 status = gpio_request(gpio, "sysfs");
647 status = gpio_export(gpio, true);
651 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
655 pr_debug("%s: status %d\n", __func__, status);
656 return status ? : len;
659 static ssize_t unexport_store(struct class *class,
660 struct class_attribute *attr,
661 const char *buf, size_t len)
666 status = strict_strtol(buf, 0, &gpio);
672 /* reject bogus commands (gpio_unexport ignores them) */
673 if (!gpio_is_valid(gpio))
676 /* No extra locking here; FLAG_SYSFS just signifies that the
677 * request and export were done by on behalf of userspace, so
678 * they may be undone on its behalf too.
680 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
686 pr_debug("%s: status %d\n", __func__, status);
687 return status ? : len;
690 static struct class_attribute gpio_class_attrs[] = {
691 __ATTR(export, 0200, NULL, export_store),
692 __ATTR(unexport, 0200, NULL, unexport_store),
696 static struct class gpio_class = {
698 .owner = THIS_MODULE,
700 .class_attrs = gpio_class_attrs,
705 * gpio_export - export a GPIO through sysfs
706 * @gpio: gpio to make available, already requested
707 * @direction_may_change: true if userspace may change gpio direction
708 * Context: arch_initcall or later
710 * When drivers want to make a GPIO accessible to userspace after they
711 * have requested it -- perhaps while debugging, or as part of their
712 * public interface -- they may use this routine. If the GPIO can
713 * change direction (some can't) and the caller allows it, userspace
714 * will see "direction" sysfs attribute which may be used to change
715 * the gpio's direction. A "value" attribute will always be provided.
717 * Returns zero on success, else an error.
719 int gpio_export(unsigned gpio, bool direction_may_change)
722 struct gpio_desc *desc;
723 int status = -EINVAL;
726 /* can't export until sysfs is available ... */
728 pr_debug("%s: called too early!\n", __func__);
732 if (!gpio_is_valid(gpio))
735 mutex_lock(&sysfs_lock);
737 spin_lock_irqsave(&gpio_lock, flags);
738 desc = &gpio_desc[gpio];
739 if (test_bit(FLAG_REQUESTED, &desc->flags)
740 && !test_bit(FLAG_EXPORT, &desc->flags)) {
742 if (!desc->chip->direction_input
743 || !desc->chip->direction_output)
744 direction_may_change = false;
746 spin_unlock_irqrestore(&gpio_lock, flags);
748 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
749 ioname = desc->chip->names[gpio - desc->chip->base];
754 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
755 desc, ioname ? ioname : "gpio%d", gpio);
757 status = sysfs_create_group(&dev->kobj,
760 if (!status && direction_may_change)
761 status = device_create_file(dev,
762 &dev_attr_direction);
764 if (!status && gpio_to_irq(gpio) >= 0
765 && (direction_may_change
766 || !test_bit(FLAG_IS_OUT,
768 status = device_create_file(dev,
772 device_unregister(dev);
774 status = PTR_ERR(dev);
776 set_bit(FLAG_EXPORT, &desc->flags);
779 mutex_unlock(&sysfs_lock);
783 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
787 EXPORT_SYMBOL_GPL(gpio_export);
789 static int match_export(struct device *dev, void *data)
791 return dev_get_drvdata(dev) == data;
795 * gpio_export_link - create a sysfs link to an exported GPIO node
796 * @dev: device under which to create symlink
797 * @name: name of the symlink
798 * @gpio: gpio to create symlink to, already exported
800 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
801 * node. Caller is responsible for unlinking.
803 * Returns zero on success, else an error.
805 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
807 struct gpio_desc *desc;
808 int status = -EINVAL;
810 if (!gpio_is_valid(gpio))
813 mutex_lock(&sysfs_lock);
815 desc = &gpio_desc[gpio];
817 if (test_bit(FLAG_EXPORT, &desc->flags)) {
820 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
822 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
829 mutex_unlock(&sysfs_lock);
833 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
837 EXPORT_SYMBOL_GPL(gpio_export_link);
841 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
842 * @gpio: gpio to change
843 * @value: non-zero to use active low, i.e. inverted values
845 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
846 * The GPIO does not have to be exported yet. If poll(2) support has
847 * been enabled for either rising or falling edge, it will be
848 * reconfigured to follow the new polarity.
850 * Returns zero on success, else an error.
852 int gpio_sysfs_set_active_low(unsigned gpio, int value)
854 struct gpio_desc *desc;
855 struct device *dev = NULL;
856 int status = -EINVAL;
858 if (!gpio_is_valid(gpio))
861 mutex_lock(&sysfs_lock);
863 desc = &gpio_desc[gpio];
865 if (test_bit(FLAG_EXPORT, &desc->flags)) {
866 dev = class_find_device(&gpio_class, NULL, desc, match_export);
873 status = sysfs_set_active_low(desc, dev, value);
876 mutex_unlock(&sysfs_lock);
880 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
884 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
887 * gpio_unexport - reverse effect of gpio_export()
888 * @gpio: gpio to make unavailable
890 * This is implicit on gpio_free().
892 void gpio_unexport(unsigned gpio)
894 struct gpio_desc *desc;
895 int status = -EINVAL;
897 if (!gpio_is_valid(gpio))
900 mutex_lock(&sysfs_lock);
902 desc = &gpio_desc[gpio];
904 if (test_bit(FLAG_EXPORT, &desc->flags)) {
905 struct device *dev = NULL;
907 dev = class_find_device(&gpio_class, NULL, desc, match_export);
909 gpio_setup_irq(desc, dev, 0);
910 clear_bit(FLAG_EXPORT, &desc->flags);
912 device_unregister(dev);
918 mutex_unlock(&sysfs_lock);
921 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
923 EXPORT_SYMBOL_GPL(gpio_unexport);
925 static int gpiochip_export(struct gpio_chip *chip)
930 /* Many systems register gpio chips for SOC support very early,
931 * before driver model support is available. In those cases we
932 * export this later, in gpiolib_sysfs_init() ... here we just
933 * verify that _some_ field of gpio_class got initialized.
938 /* use chip->base for the ID; it's already known to be unique */
939 mutex_lock(&sysfs_lock);
940 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
941 "gpiochip%d", chip->base);
943 status = sysfs_create_group(&dev->kobj,
944 &gpiochip_attr_group);
946 status = PTR_ERR(dev);
947 chip->exported = (status == 0);
948 mutex_unlock(&sysfs_lock);
954 spin_lock_irqsave(&gpio_lock, flags);
956 while (gpio_desc[gpio].chip == chip)
957 gpio_desc[gpio++].chip = NULL;
958 spin_unlock_irqrestore(&gpio_lock, flags);
960 pr_debug("%s: chip %s status %d\n", __func__,
961 chip->label, status);
967 static void gpiochip_unexport(struct gpio_chip *chip)
972 mutex_lock(&sysfs_lock);
973 dev = class_find_device(&gpio_class, NULL, chip, match_export);
976 device_unregister(dev);
981 mutex_unlock(&sysfs_lock);
984 pr_debug("%s: chip %s status %d\n", __func__,
985 chip->label, status);
988 static int __init gpiolib_sysfs_init(void)
994 idr_init(&pdesc_idr);
996 status = class_register(&gpio_class);
1000 /* Scan and register the gpio_chips which registered very
1001 * early (e.g. before the class_register above was called).
1003 * We run before arch_initcall() so chip->dev nodes can have
1004 * registered, and so arch_initcall() can always gpio_export().
1006 spin_lock_irqsave(&gpio_lock, flags);
1007 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
1008 struct gpio_chip *chip;
1010 chip = gpio_desc[gpio].chip;
1011 if (!chip || chip->exported)
1014 spin_unlock_irqrestore(&gpio_lock, flags);
1015 status = gpiochip_export(chip);
1016 spin_lock_irqsave(&gpio_lock, flags);
1018 spin_unlock_irqrestore(&gpio_lock, flags);
1023 postcore_initcall(gpiolib_sysfs_init);
1026 static inline int gpiochip_export(struct gpio_chip *chip)
1031 static inline void gpiochip_unexport(struct gpio_chip *chip)
1035 #endif /* CONFIG_GPIO_SYSFS */
1038 * gpiochip_add() - register a gpio_chip
1039 * @chip: the chip to register, with chip->base initialized
1040 * Context: potentially before irqs or kmalloc will work
1042 * Returns a negative errno if the chip can't be registered, such as
1043 * because the chip->base is invalid or already associated with a
1044 * different chip. Otherwise it returns zero as a success code.
1046 * When gpiochip_add() is called very early during boot, so that GPIOs
1047 * can be freely used, the chip->dev device must be registered before
1048 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1049 * for GPIOs will fail rudely.
1051 * If chip->base is negative, this requests dynamic assignment of
1052 * a range of valid GPIOs.
1054 int gpiochip_add(struct gpio_chip *chip)
1056 unsigned long flags;
1059 int base = chip->base;
1061 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1067 spin_lock_irqsave(&gpio_lock, flags);
1070 base = gpiochip_find_base(chip->ngpio);
1078 /* these GPIO numbers must not be managed by another gpio_chip */
1079 for (id = base; id < base + chip->ngpio; id++) {
1080 if (gpio_desc[id].chip != NULL) {
1086 for (id = base; id < base + chip->ngpio; id++) {
1087 gpio_desc[id].chip = chip;
1089 /* REVISIT: most hardware initializes GPIOs as
1090 * inputs (often with pullups enabled) so power
1091 * usage is minimized. Linux code should set the
1092 * gpio direction first thing; but until it does,
1093 * we may expose the wrong direction in sysfs.
1095 gpio_desc[id].flags = !chip->direction_input
1096 ? (1 << FLAG_IS_OUT)
1102 spin_unlock_irqrestore(&gpio_lock, flags);
1104 status = gpiochip_export(chip);
1106 /* failures here can mean systems won't boot... */
1108 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
1109 chip->base, chip->base + chip->ngpio - 1,
1110 chip->label ? : "generic");
1113 EXPORT_SYMBOL_GPL(gpiochip_add);
1116 * gpiochip_remove() - unregister a gpio_chip
1117 * @chip: the chip to unregister
1119 * A gpio_chip with any GPIOs still requested may not be removed.
1121 int gpiochip_remove(struct gpio_chip *chip)
1123 unsigned long flags;
1127 spin_lock_irqsave(&gpio_lock, flags);
1129 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1130 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1136 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1137 gpio_desc[id].chip = NULL;
1140 spin_unlock_irqrestore(&gpio_lock, flags);
1143 gpiochip_unexport(chip);
1147 EXPORT_SYMBOL_GPL(gpiochip_remove);
1150 /* These "optional" allocation calls help prevent drivers from stomping
1151 * on each other, and help provide better diagnostics in debugfs.
1152 * They're called even less than the "set direction" calls.
1154 int gpio_request(unsigned gpio, const char *label)
1156 struct gpio_desc *desc;
1157 struct gpio_chip *chip;
1158 int status = -EINVAL;
1159 unsigned long flags;
1161 spin_lock_irqsave(&gpio_lock, flags);
1163 if (!gpio_is_valid(gpio))
1165 desc = &gpio_desc[gpio];
1170 if (!try_module_get(chip->owner))
1173 /* NOTE: gpio_request() can be called in early boot,
1174 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1177 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1178 desc_set_label(desc, label ? : "?");
1182 module_put(chip->owner);
1186 if (chip->request) {
1187 /* chip->request may sleep */
1188 spin_unlock_irqrestore(&gpio_lock, flags);
1189 status = chip->request(chip, gpio - chip->base);
1190 spin_lock_irqsave(&gpio_lock, flags);
1193 desc_set_label(desc, NULL);
1194 module_put(chip->owner);
1195 clear_bit(FLAG_REQUESTED, &desc->flags);
1201 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1202 gpio, label ? : "?", status);
1203 spin_unlock_irqrestore(&gpio_lock, flags);
1206 EXPORT_SYMBOL_GPL(gpio_request);
1208 void gpio_free(unsigned gpio)
1210 unsigned long flags;
1211 struct gpio_desc *desc;
1212 struct gpio_chip *chip;
1216 if (!gpio_is_valid(gpio)) {
1217 WARN_ON(extra_checks);
1221 gpio_unexport(gpio);
1223 spin_lock_irqsave(&gpio_lock, flags);
1225 desc = &gpio_desc[gpio];
1227 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1229 spin_unlock_irqrestore(&gpio_lock, flags);
1230 might_sleep_if(extra_checks && chip->can_sleep);
1231 chip->free(chip, gpio - chip->base);
1232 spin_lock_irqsave(&gpio_lock, flags);
1234 desc_set_label(desc, NULL);
1235 module_put(desc->chip->owner);
1236 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1237 clear_bit(FLAG_REQUESTED, &desc->flags);
1239 WARN_ON(extra_checks);
1241 spin_unlock_irqrestore(&gpio_lock, flags);
1243 EXPORT_SYMBOL_GPL(gpio_free);
1246 * gpio_request_one - request a single GPIO with initial configuration
1247 * @gpio: the GPIO number
1248 * @flags: GPIO configuration as specified by GPIOF_*
1249 * @label: a literal description string of this GPIO
1251 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1255 err = gpio_request(gpio, label);
1259 if (flags & GPIOF_DIR_IN)
1260 err = gpio_direction_input(gpio);
1262 err = gpio_direction_output(gpio,
1263 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1267 EXPORT_SYMBOL_GPL(gpio_request_one);
1270 * gpio_request_array - request multiple GPIOs in a single call
1271 * @array: array of the 'struct gpio'
1272 * @num: how many GPIOs in the array
1274 int gpio_request_array(struct gpio *array, size_t num)
1278 for (i = 0; i < num; i++, array++) {
1279 err = gpio_request_one(array->gpio, array->flags, array->label);
1287 gpio_free((--array)->gpio);
1290 EXPORT_SYMBOL_GPL(gpio_request_array);
1293 * gpio_free_array - release multiple GPIOs in a single call
1294 * @array: array of the 'struct gpio'
1295 * @num: how many GPIOs in the array
1297 void gpio_free_array(struct gpio *array, size_t num)
1300 gpio_free((array++)->gpio);
1302 EXPORT_SYMBOL_GPL(gpio_free_array);
1305 * gpiochip_is_requested - return string iff signal was requested
1306 * @chip: controller managing the signal
1307 * @offset: of signal within controller's 0..(ngpio - 1) range
1309 * Returns NULL if the GPIO is not currently requested, else a string.
1310 * If debugfs support is enabled, the string returned is the label passed
1311 * to gpio_request(); otherwise it is a meaningless constant.
1313 * This function is for use by GPIO controller drivers. The label can
1314 * help with diagnostics, and knowing that the signal is used as a GPIO
1315 * can help avoid accidentally multiplexing it to another controller.
1317 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1319 unsigned gpio = chip->base + offset;
1321 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1323 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1325 #ifdef CONFIG_DEBUG_FS
1326 return gpio_desc[gpio].label;
1331 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1334 /* Drivers MUST set GPIO direction before making get/set calls. In
1335 * some cases this is done in early boot, before IRQs are enabled.
1337 * As a rule these aren't called more than once (except for drivers
1338 * using the open-drain emulation idiom) so these are natural places
1339 * to accumulate extra debugging checks. Note that we can't (yet)
1340 * rely on gpio_request() having been called beforehand.
1343 int gpio_direction_input(unsigned gpio)
1345 unsigned long flags;
1346 struct gpio_chip *chip;
1347 struct gpio_desc *desc = &gpio_desc[gpio];
1348 int status = -EINVAL;
1350 spin_lock_irqsave(&gpio_lock, flags);
1352 if (!gpio_is_valid(gpio))
1355 if (!chip || !chip->get || !chip->direction_input)
1358 if (gpio >= chip->ngpio)
1360 status = gpio_ensure_requested(desc, gpio);
1364 /* now we know the gpio is valid and chip won't vanish */
1366 spin_unlock_irqrestore(&gpio_lock, flags);
1368 might_sleep_if(extra_checks && chip->can_sleep);
1371 status = chip->request(chip, gpio);
1373 pr_debug("GPIO-%d: chip request fail, %d\n",
1374 chip->base + gpio, status);
1375 /* and it's not available to anyone else ...
1376 * gpio_request() is the fully clean solution.
1382 status = chip->direction_input(chip, gpio);
1384 clear_bit(FLAG_IS_OUT, &desc->flags);
1388 spin_unlock_irqrestore(&gpio_lock, flags);
1390 pr_debug("%s: gpio-%d status %d\n",
1391 __func__, gpio, status);
1394 EXPORT_SYMBOL_GPL(gpio_direction_input);
1396 int gpio_direction_output(unsigned gpio, int value)
1398 unsigned long flags;
1399 struct gpio_chip *chip;
1400 struct gpio_desc *desc = &gpio_desc[gpio];
1401 int status = -EINVAL;
1403 spin_lock_irqsave(&gpio_lock, flags);
1405 if (!gpio_is_valid(gpio))
1408 if (!chip || !chip->set || !chip->direction_output)
1411 if (gpio >= chip->ngpio)
1413 status = gpio_ensure_requested(desc, gpio);
1417 /* now we know the gpio is valid and chip won't vanish */
1419 spin_unlock_irqrestore(&gpio_lock, flags);
1421 might_sleep_if(extra_checks && chip->can_sleep);
1424 status = chip->request(chip, gpio);
1426 pr_debug("GPIO-%d: chip request fail, %d\n",
1427 chip->base + gpio, status);
1428 /* and it's not available to anyone else ...
1429 * gpio_request() is the fully clean solution.
1435 status = chip->direction_output(chip, gpio, value);
1437 set_bit(FLAG_IS_OUT, &desc->flags);
1441 spin_unlock_irqrestore(&gpio_lock, flags);
1443 pr_debug("%s: gpio-%d status %d\n",
1444 __func__, gpio, status);
1447 EXPORT_SYMBOL_GPL(gpio_direction_output);
1450 /* I/O calls are only valid after configuration completed; the relevant
1451 * "is this a valid GPIO" error checks should already have been done.
1453 * "Get" operations are often inlinable as reading a pin value register,
1454 * and masking the relevant bit in that register.
1456 * When "set" operations are inlinable, they involve writing that mask to
1457 * one register to set a low value, or a different register to set it high.
1458 * Otherwise locking is needed, so there may be little value to inlining.
1460 *------------------------------------------------------------------------
1462 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1463 * have requested the GPIO. That can include implicit requesting by
1464 * a direction setting call. Marking a gpio as requested locks its chip
1465 * in memory, guaranteeing that these table lookups need no more locking
1466 * and that gpiochip_remove() will fail.
1468 * REVISIT when debugging, consider adding some instrumentation to ensure
1469 * that the GPIO was actually requested.
1473 * __gpio_get_value() - return a gpio's value
1474 * @gpio: gpio whose value will be returned
1477 * This is used directly or indirectly to implement gpio_get_value().
1478 * It returns the zero or nonzero value provided by the associated
1479 * gpio_chip.get() method; or zero if no such method is provided.
1481 int __gpio_get_value(unsigned gpio)
1483 struct gpio_chip *chip;
1485 chip = gpio_to_chip(gpio);
1486 WARN_ON(extra_checks && chip->can_sleep);
1487 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1489 EXPORT_SYMBOL_GPL(__gpio_get_value);
1492 * __gpio_set_value() - assign a gpio's value
1493 * @gpio: gpio whose value will be assigned
1494 * @value: value to assign
1497 * This is used directly or indirectly to implement gpio_set_value().
1498 * It invokes the associated gpio_chip.set() method.
1500 void __gpio_set_value(unsigned gpio, int value)
1502 struct gpio_chip *chip;
1504 chip = gpio_to_chip(gpio);
1505 WARN_ON(extra_checks && chip->can_sleep);
1506 chip->set(chip, gpio - chip->base, value);
1508 EXPORT_SYMBOL_GPL(__gpio_set_value);
1511 * __gpio_cansleep() - report whether gpio value access will sleep
1512 * @gpio: gpio in question
1515 * This is used directly or indirectly to implement gpio_cansleep(). It
1516 * returns nonzero if access reading or writing the GPIO value can sleep.
1518 int __gpio_cansleep(unsigned gpio)
1520 struct gpio_chip *chip;
1522 /* only call this on GPIOs that are valid! */
1523 chip = gpio_to_chip(gpio);
1525 return chip->can_sleep;
1527 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1530 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1531 * @gpio: gpio whose IRQ will be returned (already requested)
1534 * This is used directly or indirectly to implement gpio_to_irq().
1535 * It returns the number of the IRQ signaled by this (input) GPIO,
1536 * or a negative errno.
1538 int __gpio_to_irq(unsigned gpio)
1540 struct gpio_chip *chip;
1542 chip = gpio_to_chip(gpio);
1543 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1545 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1549 /* There's no value in making it easy to inline GPIO calls that may sleep.
1550 * Common examples include ones connected to I2C or SPI chips.
1553 int gpio_get_value_cansleep(unsigned gpio)
1555 struct gpio_chip *chip;
1557 might_sleep_if(extra_checks);
1558 chip = gpio_to_chip(gpio);
1559 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1561 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1563 void gpio_set_value_cansleep(unsigned gpio, int value)
1565 struct gpio_chip *chip;
1567 might_sleep_if(extra_checks);
1568 chip = gpio_to_chip(gpio);
1569 chip->set(chip, gpio - chip->base, value);
1571 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1574 #ifdef CONFIG_DEBUG_FS
1576 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1579 unsigned gpio = chip->base;
1580 struct gpio_desc *gdesc = &gpio_desc[gpio];
1583 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1584 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1587 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1588 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1590 is_out ? "out" : "in ",
1592 ? (chip->get(chip, i) ? "hi" : "lo")
1596 int irq = gpio_to_irq(gpio);
1597 struct irq_desc *desc = irq_to_desc(irq);
1599 /* This races with request_irq(), set_irq_type(),
1600 * and set_irq_wake() ... but those are "rare".
1602 * More significantly, trigger type flags aren't
1603 * currently maintained by genirq.
1605 if (irq >= 0 && desc->action) {
1608 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
1610 trigger = "(default)";
1612 case IRQ_TYPE_EDGE_FALLING:
1613 trigger = "edge-falling";
1615 case IRQ_TYPE_EDGE_RISING:
1616 trigger = "edge-rising";
1618 case IRQ_TYPE_EDGE_BOTH:
1619 trigger = "edge-both";
1621 case IRQ_TYPE_LEVEL_HIGH:
1622 trigger = "level-high";
1624 case IRQ_TYPE_LEVEL_LOW:
1625 trigger = "level-low";
1628 trigger = "?trigger?";
1632 seq_printf(s, " irq-%d %s%s",
1634 (desc->status & IRQ_WAKEUP)
1639 seq_printf(s, "\n");
1643 static int gpiolib_show(struct seq_file *s, void *unused)
1645 struct gpio_chip *chip = NULL;
1649 /* REVISIT this isn't locked against gpio_chip removal ... */
1651 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1654 if (chip == gpio_desc[gpio].chip)
1656 chip = gpio_desc[gpio].chip;
1660 seq_printf(s, "%sGPIOs %d-%d",
1661 started ? "\n" : "",
1662 chip->base, chip->base + chip->ngpio - 1);
1665 seq_printf(s, ", %s/%s",
1666 dev->bus ? dev->bus->name : "no-bus",
1669 seq_printf(s, ", %s", chip->label);
1670 if (chip->can_sleep)
1671 seq_printf(s, ", can sleep");
1672 seq_printf(s, ":\n");
1676 chip->dbg_show(s, chip);
1678 gpiolib_dbg_show(s, chip);
1683 static int gpiolib_open(struct inode *inode, struct file *file)
1685 return single_open(file, gpiolib_show, NULL);
1688 static const struct file_operations gpiolib_operations = {
1689 .open = gpiolib_open,
1691 .llseek = seq_lseek,
1692 .release = single_release,
1695 static int __init gpiolib_debugfs_init(void)
1697 /* /sys/kernel/debug/gpio */
1698 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1699 NULL, NULL, &gpiolib_operations);
1702 subsys_initcall(gpiolib_debugfs_init);
1704 #endif /* DEBUG_FS */