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/of_gpio.h>
12 #include <linux/idr.h>
13 #include <linux/slab.h>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/gpio.h>
18 /* Optional implementation infrastructure for GPIO interfaces.
20 * Platforms may want to use this if they tend to use very many GPIOs
21 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
23 * When kernel footprint or instruction count is an issue, simpler
24 * implementations may be preferred. The GPIO programming interface
25 * allows for inlining speed-critical get/set operations for common
26 * cases, so that access to SOC-integrated GPIOs can sometimes cost
27 * only an instruction or two per bit.
31 /* When debugging, extend minimal trust to callers and platform code.
32 * Also emit diagnostic messages that may help initial bringup, when
33 * board setup or driver bugs are most common.
35 * Otherwise, minimize overhead in what may be bitbanging codepaths.
38 #define extra_checks 1
40 #define extra_checks 0
43 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
44 * While any GPIO is requested, its gpio_chip is not removable;
45 * each GPIO's "requested" flag serves as a lock and refcount.
47 static DEFINE_SPINLOCK(gpio_lock);
50 struct gpio_chip *chip;
52 /* flag symbols are bit numbers */
53 #define FLAG_REQUESTED 0
55 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
56 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
57 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
58 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
59 #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
60 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
61 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
63 #define ID_SHIFT 16 /* add new flags before this one */
65 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
66 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
68 #ifdef CONFIG_DEBUG_FS
72 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74 #ifdef CONFIG_GPIO_SYSFS
75 static DEFINE_IDR(dirent_idr);
78 static inline void desc_set_label(struct gpio_desc *d, const char *label)
80 #ifdef CONFIG_DEBUG_FS
85 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
86 * when setting direction, and otherwise illegal. Until board setup code
87 * and drivers use explicit requests everywhere (which won't happen when
88 * those calls have no teeth) we can't avoid autorequesting. This nag
89 * message should motivate switching to explicit requests... so should
90 * the weaker cleanup after faults, compared to gpio_request().
92 * NOTE: the autorequest mechanism is going away; at this point it's
93 * only "legal" in the sense that (old) code using it won't break yet,
94 * but instead only triggers a WARN() stack dump.
96 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
98 const struct gpio_chip *chip = desc->chip;
99 const int gpio = chip->base + offset;
101 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
102 "autorequest GPIO-%d\n", gpio)) {
103 if (!try_module_get(chip->owner)) {
104 pr_err("GPIO-%d: module can't be gotten \n", gpio);
105 clear_bit(FLAG_REQUESTED, &desc->flags);
109 desc_set_label(desc, "[auto]");
110 /* caller must chip->request() w/o spinlock */
117 /* caller holds gpio_lock *OR* gpio is marked as requested */
118 struct gpio_chip *gpio_to_chip(unsigned gpio)
120 return gpio_desc[gpio].chip;
123 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
124 static int gpiochip_find_base(int ngpio)
130 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
131 struct gpio_desc *desc = &gpio_desc[i];
132 struct gpio_chip *chip = desc->chip;
136 if (spare == ngpio) {
143 i -= chip->ngpio - 1;
147 if (gpio_is_valid(base))
148 pr_debug("%s: found new base at %d\n", __func__, base);
152 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
153 static int gpio_get_direction(unsigned gpio)
155 struct gpio_chip *chip;
156 struct gpio_desc *desc = &gpio_desc[gpio];
157 int status = -EINVAL;
159 chip = gpio_to_chip(gpio);
162 if (!chip->get_direction)
165 status = chip->get_direction(chip, gpio);
167 /* GPIOF_DIR_IN, or other positive */
169 clear_bit(FLAG_IS_OUT, &desc->flags);
173 set_bit(FLAG_IS_OUT, &desc->flags);
178 #ifdef CONFIG_GPIO_SYSFS
180 /* lock protects against unexport_gpio() being called while
181 * sysfs files are active.
183 static DEFINE_MUTEX(sysfs_lock);
186 * /sys/class/gpio/gpioN... only for GPIOs that are exported
188 * * MAY BE OMITTED if kernel won't allow direction changes
189 * * is read/write as "in" or "out"
190 * * may also be written as "high" or "low", initializing
191 * output value as specified ("out" implies "low")
193 * * always readable, subject to hardware behavior
194 * * may be writable, as zero/nonzero
196 * * configures behavior of poll(2) on /value
197 * * available only if pin can generate IRQs on input
198 * * is read/write as "none", "falling", "rising", or "both"
200 * * configures polarity of /value
201 * * is read/write as zero/nonzero
202 * * also affects existing and subsequent "falling" and "rising"
203 * /edge configuration
206 static ssize_t gpio_direction_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 const struct gpio_desc *desc = dev_get_drvdata(dev);
210 unsigned gpio = desc - gpio_desc;
213 mutex_lock(&sysfs_lock);
215 if (!test_bit(FLAG_EXPORT, &desc->flags))
218 gpio_get_direction(gpio);
219 status = sprintf(buf, "%s\n",
220 test_bit(FLAG_IS_OUT, &desc->flags)
223 mutex_unlock(&sysfs_lock);
227 static ssize_t gpio_direction_store(struct device *dev,
228 struct device_attribute *attr, const char *buf, size_t size)
230 const struct gpio_desc *desc = dev_get_drvdata(dev);
231 unsigned gpio = desc - gpio_desc;
234 mutex_lock(&sysfs_lock);
236 if (!test_bit(FLAG_EXPORT, &desc->flags))
238 else if (sysfs_streq(buf, "high"))
239 status = gpio_direction_output(gpio, 1);
240 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
241 status = gpio_direction_output(gpio, 0);
242 else if (sysfs_streq(buf, "in"))
243 status = gpio_direction_input(gpio);
247 mutex_unlock(&sysfs_lock);
248 return status ? : size;
251 static /* const */ DEVICE_ATTR(direction, 0644,
252 gpio_direction_show, gpio_direction_store);
254 static ssize_t gpio_value_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 const struct gpio_desc *desc = dev_get_drvdata(dev);
258 unsigned gpio = desc - gpio_desc;
261 mutex_lock(&sysfs_lock);
263 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
268 value = !!gpio_get_value_cansleep(gpio);
269 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
272 status = sprintf(buf, "%d\n", value);
275 mutex_unlock(&sysfs_lock);
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;
286 mutex_lock(&sysfs_lock);
288 if (!test_bit(FLAG_EXPORT, &desc->flags))
290 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
295 status = strict_strtol(buf, 0, &value);
297 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
299 gpio_set_value_cansleep(gpio, value != 0);
304 mutex_unlock(&sysfs_lock);
308 static const DEVICE_ATTR(value, 0644,
309 gpio_value_show, gpio_value_store);
311 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
313 struct sysfs_dirent *value_sd = priv;
315 sysfs_notify_dirent(value_sd);
319 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
320 unsigned long gpio_flags)
322 struct sysfs_dirent *value_sd;
323 unsigned long irq_flags;
326 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
329 irq = gpio_to_irq(desc - gpio_desc);
333 id = desc->flags >> ID_SHIFT;
334 value_sd = idr_find(&dirent_idr, id);
336 free_irq(irq, value_sd);
338 desc->flags &= ~GPIO_TRIGGER_MASK;
345 irq_flags = IRQF_SHARED;
346 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
347 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
348 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
349 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
350 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
351 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
354 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
362 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
363 ret = idr_get_new_above(&dirent_idr, value_sd,
365 } while (ret == -EAGAIN);
370 desc->flags &= GPIO_FLAGS_MASK;
371 desc->flags |= (unsigned long)id << ID_SHIFT;
373 if (desc->flags >> ID_SHIFT != id) {
379 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
380 "gpiolib", value_sd);
384 desc->flags |= gpio_flags;
388 idr_remove(&dirent_idr, id);
389 desc->flags &= GPIO_FLAGS_MASK;
397 static const struct {
400 } trigger_types[] = {
402 { "falling", BIT(FLAG_TRIG_FALL) },
403 { "rising", BIT(FLAG_TRIG_RISE) },
404 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
407 static ssize_t gpio_edge_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 const struct gpio_desc *desc = dev_get_drvdata(dev);
413 mutex_lock(&sysfs_lock);
415 if (!test_bit(FLAG_EXPORT, &desc->flags))
421 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
422 if ((desc->flags & GPIO_TRIGGER_MASK)
423 == trigger_types[i].flags) {
424 status = sprintf(buf, "%s\n",
425 trigger_types[i].name);
430 mutex_unlock(&sysfs_lock);
434 static ssize_t gpio_edge_store(struct device *dev,
435 struct device_attribute *attr, const char *buf, size_t size)
437 struct gpio_desc *desc = dev_get_drvdata(dev);
441 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
442 if (sysfs_streq(trigger_types[i].name, buf))
447 mutex_lock(&sysfs_lock);
449 if (!test_bit(FLAG_EXPORT, &desc->flags))
452 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
457 mutex_unlock(&sysfs_lock);
462 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
464 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
469 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
473 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
475 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
477 /* reconfigure poll(2) support if enabled on one edge only */
478 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
479 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
480 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
482 gpio_setup_irq(desc, dev, 0);
483 status = gpio_setup_irq(desc, dev, trigger_flags);
489 static ssize_t gpio_active_low_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 const struct gpio_desc *desc = dev_get_drvdata(dev);
495 mutex_lock(&sysfs_lock);
497 if (!test_bit(FLAG_EXPORT, &desc->flags))
500 status = sprintf(buf, "%d\n",
501 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
503 mutex_unlock(&sysfs_lock);
508 static ssize_t gpio_active_low_store(struct device *dev,
509 struct device_attribute *attr, const char *buf, size_t size)
511 struct gpio_desc *desc = dev_get_drvdata(dev);
514 mutex_lock(&sysfs_lock);
516 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
521 status = strict_strtol(buf, 0, &value);
523 status = sysfs_set_active_low(desc, dev, value != 0);
526 mutex_unlock(&sysfs_lock);
528 return status ? : size;
531 static const DEVICE_ATTR(active_low, 0644,
532 gpio_active_low_show, gpio_active_low_store);
534 static const struct attribute *gpio_attrs[] = {
535 &dev_attr_value.attr,
536 &dev_attr_active_low.attr,
540 static const struct attribute_group gpio_attr_group = {
541 .attrs = (struct attribute **) gpio_attrs,
545 * /sys/class/gpio/gpiochipN/
546 * /base ... matching gpio_chip.base (N)
547 * /label ... matching gpio_chip.label
548 * /ngpio ... matching gpio_chip.ngpio
551 static ssize_t chip_base_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
554 const struct gpio_chip *chip = dev_get_drvdata(dev);
556 return sprintf(buf, "%d\n", chip->base);
558 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
560 static ssize_t chip_label_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 const struct gpio_chip *chip = dev_get_drvdata(dev);
565 return sprintf(buf, "%s\n", chip->label ? : "");
567 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
569 static ssize_t chip_ngpio_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
572 const struct gpio_chip *chip = dev_get_drvdata(dev);
574 return sprintf(buf, "%u\n", chip->ngpio);
576 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
578 static const struct attribute *gpiochip_attrs[] = {
580 &dev_attr_label.attr,
581 &dev_attr_ngpio.attr,
585 static const struct attribute_group gpiochip_attr_group = {
586 .attrs = (struct attribute **) gpiochip_attrs,
590 * /sys/class/gpio/export ... write-only
591 * integer N ... number of GPIO to export (full access)
592 * /sys/class/gpio/unexport ... write-only
593 * integer N ... number of GPIO to unexport
595 static ssize_t export_store(struct class *class,
596 struct class_attribute *attr,
597 const char *buf, size_t len)
602 status = strict_strtol(buf, 0, &gpio);
606 /* No extra locking here; FLAG_SYSFS just signifies that the
607 * request and export were done by on behalf of userspace, so
608 * they may be undone on its behalf too.
611 status = gpio_request(gpio, "sysfs");
613 if (status == -EPROBE_DEFER)
617 status = gpio_export(gpio, true);
621 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
625 pr_debug("%s: status %d\n", __func__, status);
626 return status ? : len;
629 static ssize_t unexport_store(struct class *class,
630 struct class_attribute *attr,
631 const char *buf, size_t len)
636 status = strict_strtol(buf, 0, &gpio);
642 /* reject bogus commands (gpio_unexport ignores them) */
643 if (!gpio_is_valid(gpio))
646 /* No extra locking here; FLAG_SYSFS just signifies that the
647 * request and export were done by on behalf of userspace, so
648 * they may be undone on its behalf too.
650 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
656 pr_debug("%s: status %d\n", __func__, status);
657 return status ? : len;
660 static struct class_attribute gpio_class_attrs[] = {
661 __ATTR(export, 0200, NULL, export_store),
662 __ATTR(unexport, 0200, NULL, unexport_store),
666 static struct class gpio_class = {
668 .owner = THIS_MODULE,
670 .class_attrs = gpio_class_attrs,
675 * gpio_export - export a GPIO through sysfs
676 * @gpio: gpio to make available, already requested
677 * @direction_may_change: true if userspace may change gpio direction
678 * Context: arch_initcall or later
680 * When drivers want to make a GPIO accessible to userspace after they
681 * have requested it -- perhaps while debugging, or as part of their
682 * public interface -- they may use this routine. If the GPIO can
683 * change direction (some can't) and the caller allows it, userspace
684 * will see "direction" sysfs attribute which may be used to change
685 * the gpio's direction. A "value" attribute will always be provided.
687 * Returns zero on success, else an error.
689 int gpio_export(unsigned gpio, bool direction_may_change)
692 struct gpio_desc *desc;
694 const char *ioname = NULL;
697 /* can't export until sysfs is available ... */
699 pr_debug("%s: called too early!\n", __func__);
703 if (!gpio_is_valid(gpio)) {
704 pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
708 mutex_lock(&sysfs_lock);
710 spin_lock_irqsave(&gpio_lock, flags);
711 desc = &gpio_desc[gpio];
712 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
713 test_bit(FLAG_EXPORT, &desc->flags)) {
714 spin_unlock_irqrestore(&gpio_lock, flags);
715 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
717 test_bit(FLAG_REQUESTED, &desc->flags),
718 test_bit(FLAG_EXPORT, &desc->flags));
723 if (!desc->chip->direction_input || !desc->chip->direction_output)
724 direction_may_change = false;
725 spin_unlock_irqrestore(&gpio_lock, flags);
727 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
728 ioname = desc->chip->names[gpio - desc->chip->base];
730 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
731 desc, ioname ? ioname : "gpio%u", gpio);
733 status = PTR_ERR(dev);
737 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
739 goto fail_unregister_device;
741 if (direction_may_change) {
742 status = device_create_file(dev, &dev_attr_direction);
744 goto fail_unregister_device;
747 if (gpio_to_irq(gpio) >= 0 && (direction_may_change ||
748 !test_bit(FLAG_IS_OUT, &desc->flags))) {
749 status = device_create_file(dev, &dev_attr_edge);
751 goto fail_unregister_device;
754 set_bit(FLAG_EXPORT, &desc->flags);
755 mutex_unlock(&sysfs_lock);
758 fail_unregister_device:
759 device_unregister(dev);
761 mutex_unlock(&sysfs_lock);
762 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
765 EXPORT_SYMBOL_GPL(gpio_export);
767 static int match_export(struct device *dev, void *data)
769 return dev_get_drvdata(dev) == data;
773 * gpio_export_link - create a sysfs link to an exported GPIO node
774 * @dev: device under which to create symlink
775 * @name: name of the symlink
776 * @gpio: gpio to create symlink to, already exported
778 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
779 * node. Caller is responsible for unlinking.
781 * Returns zero on success, else an error.
783 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
785 struct gpio_desc *desc;
786 int status = -EINVAL;
788 if (!gpio_is_valid(gpio))
791 mutex_lock(&sysfs_lock);
793 desc = &gpio_desc[gpio];
795 if (test_bit(FLAG_EXPORT, &desc->flags)) {
798 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
800 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
807 mutex_unlock(&sysfs_lock);
811 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
815 EXPORT_SYMBOL_GPL(gpio_export_link);
819 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
820 * @gpio: gpio to change
821 * @value: non-zero to use active low, i.e. inverted values
823 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
824 * The GPIO does not have to be exported yet. If poll(2) support has
825 * been enabled for either rising or falling edge, it will be
826 * reconfigured to follow the new polarity.
828 * Returns zero on success, else an error.
830 int gpio_sysfs_set_active_low(unsigned gpio, int value)
832 struct gpio_desc *desc;
833 struct device *dev = NULL;
834 int status = -EINVAL;
836 if (!gpio_is_valid(gpio))
839 mutex_lock(&sysfs_lock);
841 desc = &gpio_desc[gpio];
843 if (test_bit(FLAG_EXPORT, &desc->flags)) {
844 dev = class_find_device(&gpio_class, NULL, desc, match_export);
851 status = sysfs_set_active_low(desc, dev, value);
854 mutex_unlock(&sysfs_lock);
858 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
862 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
865 * gpio_unexport - reverse effect of gpio_export()
866 * @gpio: gpio to make unavailable
868 * This is implicit on gpio_free().
870 void gpio_unexport(unsigned gpio)
872 struct gpio_desc *desc;
874 struct device *dev = NULL;
876 if (!gpio_is_valid(gpio)) {
881 mutex_lock(&sysfs_lock);
883 desc = &gpio_desc[gpio];
885 if (test_bit(FLAG_EXPORT, &desc->flags)) {
887 dev = class_find_device(&gpio_class, NULL, desc, match_export);
889 gpio_setup_irq(desc, dev, 0);
890 clear_bit(FLAG_EXPORT, &desc->flags);
895 mutex_unlock(&sysfs_lock);
897 device_unregister(dev);
902 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
904 EXPORT_SYMBOL_GPL(gpio_unexport);
906 static int gpiochip_export(struct gpio_chip *chip)
911 /* Many systems register gpio chips for SOC support very early,
912 * before driver model support is available. In those cases we
913 * export this later, in gpiolib_sysfs_init() ... here we just
914 * verify that _some_ field of gpio_class got initialized.
919 /* use chip->base for the ID; it's already known to be unique */
920 mutex_lock(&sysfs_lock);
921 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
922 "gpiochip%d", chip->base);
924 status = sysfs_create_group(&dev->kobj,
925 &gpiochip_attr_group);
927 status = PTR_ERR(dev);
928 chip->exported = (status == 0);
929 mutex_unlock(&sysfs_lock);
935 spin_lock_irqsave(&gpio_lock, flags);
937 while (gpio_desc[gpio].chip == chip)
938 gpio_desc[gpio++].chip = NULL;
939 spin_unlock_irqrestore(&gpio_lock, flags);
941 pr_debug("%s: chip %s status %d\n", __func__,
942 chip->label, status);
948 static void gpiochip_unexport(struct gpio_chip *chip)
953 mutex_lock(&sysfs_lock);
954 dev = class_find_device(&gpio_class, NULL, chip, match_export);
957 device_unregister(dev);
962 mutex_unlock(&sysfs_lock);
965 pr_debug("%s: chip %s status %d\n", __func__,
966 chip->label, status);
969 static int __init gpiolib_sysfs_init(void)
975 status = class_register(&gpio_class);
979 /* Scan and register the gpio_chips which registered very
980 * early (e.g. before the class_register above was called).
982 * We run before arch_initcall() so chip->dev nodes can have
983 * registered, and so arch_initcall() can always gpio_export().
985 spin_lock_irqsave(&gpio_lock, flags);
986 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
987 struct gpio_chip *chip;
989 chip = gpio_desc[gpio].chip;
990 if (!chip || chip->exported)
993 spin_unlock_irqrestore(&gpio_lock, flags);
994 status = gpiochip_export(chip);
995 spin_lock_irqsave(&gpio_lock, flags);
997 spin_unlock_irqrestore(&gpio_lock, flags);
1002 postcore_initcall(gpiolib_sysfs_init);
1005 static inline int gpiochip_export(struct gpio_chip *chip)
1010 static inline void gpiochip_unexport(struct gpio_chip *chip)
1014 #endif /* CONFIG_GPIO_SYSFS */
1017 * gpiochip_add() - register a gpio_chip
1018 * @chip: the chip to register, with chip->base initialized
1019 * Context: potentially before irqs or kmalloc will work
1021 * Returns a negative errno if the chip can't be registered, such as
1022 * because the chip->base is invalid or already associated with a
1023 * different chip. Otherwise it returns zero as a success code.
1025 * When gpiochip_add() is called very early during boot, so that GPIOs
1026 * can be freely used, the chip->dev device must be registered before
1027 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1028 * for GPIOs will fail rudely.
1030 * If chip->base is negative, this requests dynamic assignment of
1031 * a range of valid GPIOs.
1033 int gpiochip_add(struct gpio_chip *chip)
1035 unsigned long flags;
1038 int base = chip->base;
1040 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1046 spin_lock_irqsave(&gpio_lock, flags);
1049 base = gpiochip_find_base(chip->ngpio);
1057 /* these GPIO numbers must not be managed by another gpio_chip */
1058 for (id = base; id < base + chip->ngpio; id++) {
1059 if (gpio_desc[id].chip != NULL) {
1065 for (id = base; id < base + chip->ngpio; id++) {
1066 gpio_desc[id].chip = chip;
1068 /* REVISIT: most hardware initializes GPIOs as
1069 * inputs (often with pullups enabled) so power
1070 * usage is minimized. Linux code should set the
1071 * gpio direction first thing; but until it does,
1072 * and in case chip->get_direction is not set,
1073 * we may expose the wrong direction in sysfs.
1075 gpio_desc[id].flags = !chip->direction_input
1076 ? (1 << FLAG_IS_OUT)
1081 #ifdef CONFIG_PINCTRL
1082 INIT_LIST_HEAD(&chip->pin_ranges);
1085 of_gpiochip_add(chip);
1088 spin_unlock_irqrestore(&gpio_lock, flags);
1093 status = gpiochip_export(chip);
1097 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1098 chip->base, chip->base + chip->ngpio - 1,
1099 chip->label ? : "generic");
1103 /* failures here can mean systems won't boot... */
1104 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1105 chip->base, chip->base + chip->ngpio - 1,
1106 chip->label ? : "generic");
1109 EXPORT_SYMBOL_GPL(gpiochip_add);
1112 * gpiochip_remove() - unregister a gpio_chip
1113 * @chip: the chip to unregister
1115 * A gpio_chip with any GPIOs still requested may not be removed.
1117 int gpiochip_remove(struct gpio_chip *chip)
1119 unsigned long flags;
1123 spin_lock_irqsave(&gpio_lock, flags);
1125 gpiochip_remove_pin_ranges(chip);
1126 of_gpiochip_remove(chip);
1128 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1129 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1135 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1136 gpio_desc[id].chip = NULL;
1139 spin_unlock_irqrestore(&gpio_lock, flags);
1142 gpiochip_unexport(chip);
1146 EXPORT_SYMBOL_GPL(gpiochip_remove);
1149 * gpiochip_find() - iterator for locating a specific gpio_chip
1150 * @data: data to pass to match function
1151 * @callback: Callback function to check gpio_chip
1153 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1154 * determined by a user supplied @match callback. The callback should return
1155 * 0 if the device doesn't match and non-zero if it does. If the callback is
1156 * non-zero, this function will return to the caller and not iterate over any
1159 struct gpio_chip *gpiochip_find(void *data,
1160 int (*match)(struct gpio_chip *chip,
1163 struct gpio_chip *chip = NULL;
1164 unsigned long flags;
1167 spin_lock_irqsave(&gpio_lock, flags);
1168 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1169 if (!gpio_desc[i].chip)
1172 if (match(gpio_desc[i].chip, data)) {
1173 chip = gpio_desc[i].chip;
1177 spin_unlock_irqrestore(&gpio_lock, flags);
1181 EXPORT_SYMBOL_GPL(gpiochip_find);
1183 #ifdef CONFIG_PINCTRL
1186 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1187 * @chip: the gpiochip to add the range for
1188 * @pinctrl_name: the dev_name() of the pin controller to map to
1189 * @gpio_offset: the start offset in the current gpio_chip number space
1190 * @pin_offset: the start offset in the pin controller number space
1191 * @npins: the number of pins from the offset of each pin space (GPIO and
1192 * pin controller) to accumulate in this range
1194 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1195 unsigned int gpio_offset, unsigned int pin_offset,
1198 struct gpio_pin_range *pin_range;
1201 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1203 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1208 /* Use local offset as range ID */
1209 pin_range->range.id = gpio_offset;
1210 pin_range->range.gc = chip;
1211 pin_range->range.name = chip->label;
1212 pin_range->range.base = chip->base + gpio_offset;
1213 pin_range->range.pin_base = pin_offset;
1214 pin_range->range.npins = npins;
1215 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1217 if (IS_ERR(pin_range->pctldev)) {
1218 ret = PTR_ERR(pin_range->pctldev);
1219 pr_err("%s: GPIO chip: could not create pin range\n",
1224 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1225 chip->label, gpio_offset, gpio_offset + npins - 1,
1227 pin_offset, pin_offset + npins - 1);
1229 list_add_tail(&pin_range->node, &chip->pin_ranges);
1233 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1236 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1237 * @chip: the chip to remove all the mappings for
1239 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1241 struct gpio_pin_range *pin_range, *tmp;
1243 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1244 list_del(&pin_range->node);
1245 pinctrl_remove_gpio_range(pin_range->pctldev,
1250 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1252 #endif /* CONFIG_PINCTRL */
1254 /* These "optional" allocation calls help prevent drivers from stomping
1255 * on each other, and help provide better diagnostics in debugfs.
1256 * They're called even less than the "set direction" calls.
1258 int gpio_request(unsigned gpio, const char *label)
1260 struct gpio_desc *desc;
1261 struct gpio_chip *chip;
1262 int status = -EPROBE_DEFER;
1263 unsigned long flags;
1265 spin_lock_irqsave(&gpio_lock, flags);
1267 if (!gpio_is_valid(gpio)) {
1271 desc = &gpio_desc[gpio];
1276 if (!try_module_get(chip->owner))
1279 /* NOTE: gpio_request() can be called in early boot,
1280 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1283 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1284 desc_set_label(desc, label ? : "?");
1288 module_put(chip->owner);
1292 if (chip->request) {
1293 /* chip->request may sleep */
1294 spin_unlock_irqrestore(&gpio_lock, flags);
1295 status = chip->request(chip, gpio - chip->base);
1296 spin_lock_irqsave(&gpio_lock, flags);
1299 desc_set_label(desc, NULL);
1300 module_put(chip->owner);
1301 clear_bit(FLAG_REQUESTED, &desc->flags);
1305 if (chip->get_direction) {
1306 /* chip->get_direction may sleep */
1307 spin_unlock_irqrestore(&gpio_lock, flags);
1308 gpio_get_direction(gpio);
1309 spin_lock_irqsave(&gpio_lock, flags);
1313 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1314 gpio, label ? : "?", status);
1315 spin_unlock_irqrestore(&gpio_lock, flags);
1318 EXPORT_SYMBOL_GPL(gpio_request);
1320 void gpio_free(unsigned gpio)
1322 unsigned long flags;
1323 struct gpio_desc *desc;
1324 struct gpio_chip *chip;
1328 if (!gpio_is_valid(gpio)) {
1329 WARN_ON(extra_checks);
1333 gpio_unexport(gpio);
1335 spin_lock_irqsave(&gpio_lock, flags);
1337 desc = &gpio_desc[gpio];
1339 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1341 spin_unlock_irqrestore(&gpio_lock, flags);
1342 might_sleep_if(chip->can_sleep);
1343 chip->free(chip, gpio - chip->base);
1344 spin_lock_irqsave(&gpio_lock, flags);
1346 desc_set_label(desc, NULL);
1347 module_put(desc->chip->owner);
1348 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1349 clear_bit(FLAG_REQUESTED, &desc->flags);
1350 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1351 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1353 WARN_ON(extra_checks);
1355 spin_unlock_irqrestore(&gpio_lock, flags);
1357 EXPORT_SYMBOL_GPL(gpio_free);
1360 * gpio_request_one - request a single GPIO with initial configuration
1361 * @gpio: the GPIO number
1362 * @flags: GPIO configuration as specified by GPIOF_*
1363 * @label: a literal description string of this GPIO
1365 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1369 err = gpio_request(gpio, label);
1373 if (flags & GPIOF_OPEN_DRAIN)
1374 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1376 if (flags & GPIOF_OPEN_SOURCE)
1377 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1379 if (flags & GPIOF_DIR_IN)
1380 err = gpio_direction_input(gpio);
1382 err = gpio_direction_output(gpio,
1383 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1388 if (flags & GPIOF_EXPORT) {
1389 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1400 EXPORT_SYMBOL_GPL(gpio_request_one);
1403 * gpio_request_array - request multiple GPIOs in a single call
1404 * @array: array of the 'struct gpio'
1405 * @num: how many GPIOs in the array
1407 int gpio_request_array(const struct gpio *array, size_t num)
1411 for (i = 0; i < num; i++, array++) {
1412 err = gpio_request_one(array->gpio, array->flags, array->label);
1420 gpio_free((--array)->gpio);
1423 EXPORT_SYMBOL_GPL(gpio_request_array);
1426 * gpio_free_array - release multiple GPIOs in a single call
1427 * @array: array of the 'struct gpio'
1428 * @num: how many GPIOs in the array
1430 void gpio_free_array(const struct gpio *array, size_t num)
1433 gpio_free((array++)->gpio);
1435 EXPORT_SYMBOL_GPL(gpio_free_array);
1438 * gpiochip_is_requested - return string iff signal was requested
1439 * @chip: controller managing the signal
1440 * @offset: of signal within controller's 0..(ngpio - 1) range
1442 * Returns NULL if the GPIO is not currently requested, else a string.
1443 * If debugfs support is enabled, the string returned is the label passed
1444 * to gpio_request(); otherwise it is a meaningless constant.
1446 * This function is for use by GPIO controller drivers. The label can
1447 * help with diagnostics, and knowing that the signal is used as a GPIO
1448 * can help avoid accidentally multiplexing it to another controller.
1450 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1452 unsigned gpio = chip->base + offset;
1454 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1456 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1458 #ifdef CONFIG_DEBUG_FS
1459 return gpio_desc[gpio].label;
1464 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1467 /* Drivers MUST set GPIO direction before making get/set calls. In
1468 * some cases this is done in early boot, before IRQs are enabled.
1470 * As a rule these aren't called more than once (except for drivers
1471 * using the open-drain emulation idiom) so these are natural places
1472 * to accumulate extra debugging checks. Note that we can't (yet)
1473 * rely on gpio_request() having been called beforehand.
1476 int gpio_direction_input(unsigned gpio)
1478 unsigned long flags;
1479 struct gpio_chip *chip;
1480 struct gpio_desc *desc = &gpio_desc[gpio];
1481 int status = -EINVAL;
1483 spin_lock_irqsave(&gpio_lock, flags);
1485 if (!gpio_is_valid(gpio))
1488 if (!chip || !chip->get || !chip->direction_input)
1491 if (gpio >= chip->ngpio)
1493 status = gpio_ensure_requested(desc, gpio);
1497 /* now we know the gpio is valid and chip won't vanish */
1499 spin_unlock_irqrestore(&gpio_lock, flags);
1501 might_sleep_if(chip->can_sleep);
1504 status = chip->request(chip, gpio);
1506 pr_debug("GPIO-%d: chip request fail, %d\n",
1507 chip->base + gpio, status);
1508 /* and it's not available to anyone else ...
1509 * gpio_request() is the fully clean solution.
1515 status = chip->direction_input(chip, gpio);
1517 clear_bit(FLAG_IS_OUT, &desc->flags);
1519 trace_gpio_direction(chip->base + gpio, 1, status);
1523 spin_unlock_irqrestore(&gpio_lock, flags);
1525 pr_debug("%s: gpio-%d status %d\n",
1526 __func__, gpio, status);
1529 EXPORT_SYMBOL_GPL(gpio_direction_input);
1531 int gpio_direction_output(unsigned gpio, int value)
1533 unsigned long flags;
1534 struct gpio_chip *chip;
1535 struct gpio_desc *desc = &gpio_desc[gpio];
1536 int status = -EINVAL;
1538 /* Open drain pin should not be driven to 1 */
1539 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1540 return gpio_direction_input(gpio);
1542 /* Open source pin should not be driven to 0 */
1543 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1544 return gpio_direction_input(gpio);
1546 spin_lock_irqsave(&gpio_lock, flags);
1548 if (!gpio_is_valid(gpio))
1551 if (!chip || !chip->set || !chip->direction_output)
1554 if (gpio >= chip->ngpio)
1556 status = gpio_ensure_requested(desc, gpio);
1560 /* now we know the gpio is valid and chip won't vanish */
1562 spin_unlock_irqrestore(&gpio_lock, flags);
1564 might_sleep_if(chip->can_sleep);
1567 status = chip->request(chip, gpio);
1569 pr_debug("GPIO-%d: chip request fail, %d\n",
1570 chip->base + gpio, status);
1571 /* and it's not available to anyone else ...
1572 * gpio_request() is the fully clean solution.
1578 status = chip->direction_output(chip, gpio, value);
1580 set_bit(FLAG_IS_OUT, &desc->flags);
1581 trace_gpio_value(chip->base + gpio, 0, value);
1582 trace_gpio_direction(chip->base + gpio, 0, status);
1586 spin_unlock_irqrestore(&gpio_lock, flags);
1588 pr_debug("%s: gpio-%d status %d\n",
1589 __func__, gpio, status);
1592 EXPORT_SYMBOL_GPL(gpio_direction_output);
1595 * gpio_set_debounce - sets @debounce time for a @gpio
1596 * @gpio: the gpio to set debounce time
1597 * @debounce: debounce time is microseconds
1599 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1601 unsigned long flags;
1602 struct gpio_chip *chip;
1603 struct gpio_desc *desc = &gpio_desc[gpio];
1604 int status = -EINVAL;
1606 spin_lock_irqsave(&gpio_lock, flags);
1608 if (!gpio_is_valid(gpio))
1611 if (!chip || !chip->set || !chip->set_debounce)
1614 if (gpio >= chip->ngpio)
1616 status = gpio_ensure_requested(desc, gpio);
1620 /* now we know the gpio is valid and chip won't vanish */
1622 spin_unlock_irqrestore(&gpio_lock, flags);
1624 might_sleep_if(chip->can_sleep);
1626 return chip->set_debounce(chip, gpio, debounce);
1629 spin_unlock_irqrestore(&gpio_lock, flags);
1631 pr_debug("%s: gpio-%d status %d\n",
1632 __func__, gpio, status);
1636 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1638 /* I/O calls are only valid after configuration completed; the relevant
1639 * "is this a valid GPIO" error checks should already have been done.
1641 * "Get" operations are often inlinable as reading a pin value register,
1642 * and masking the relevant bit in that register.
1644 * When "set" operations are inlinable, they involve writing that mask to
1645 * one register to set a low value, or a different register to set it high.
1646 * Otherwise locking is needed, so there may be little value to inlining.
1648 *------------------------------------------------------------------------
1650 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1651 * have requested the GPIO. That can include implicit requesting by
1652 * a direction setting call. Marking a gpio as requested locks its chip
1653 * in memory, guaranteeing that these table lookups need no more locking
1654 * and that gpiochip_remove() will fail.
1656 * REVISIT when debugging, consider adding some instrumentation to ensure
1657 * that the GPIO was actually requested.
1661 * __gpio_get_value() - return a gpio's value
1662 * @gpio: gpio whose value will be returned
1665 * This is used directly or indirectly to implement gpio_get_value().
1666 * It returns the zero or nonzero value provided by the associated
1667 * gpio_chip.get() method; or zero if no such method is provided.
1669 int __gpio_get_value(unsigned gpio)
1671 struct gpio_chip *chip;
1674 chip = gpio_to_chip(gpio);
1675 /* Should be using gpio_get_value_cansleep() */
1676 WARN_ON(chip->can_sleep);
1677 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1678 trace_gpio_value(gpio, 1, value);
1681 EXPORT_SYMBOL_GPL(__gpio_get_value);
1684 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1685 * @gpio: Gpio whose state need to be set.
1687 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1689 static void _gpio_set_open_drain_value(unsigned gpio,
1690 struct gpio_chip *chip, int value)
1694 err = chip->direction_input(chip, gpio - chip->base);
1696 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1698 err = chip->direction_output(chip, gpio - chip->base, 0);
1700 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1702 trace_gpio_direction(gpio, value, err);
1704 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1705 __func__, gpio, err);
1709 * _gpio_set_open_source() - Set the open source gpio's value.
1710 * @gpio: Gpio whose state need to be set.
1712 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1714 static void _gpio_set_open_source_value(unsigned gpio,
1715 struct gpio_chip *chip, int value)
1719 err = chip->direction_output(chip, gpio - chip->base, 1);
1721 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1723 err = chip->direction_input(chip, gpio - chip->base);
1725 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1727 trace_gpio_direction(gpio, !value, err);
1729 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1730 __func__, gpio, err);
1735 * __gpio_set_value() - assign a gpio's value
1736 * @gpio: gpio whose value will be assigned
1737 * @value: value to assign
1740 * This is used directly or indirectly to implement gpio_set_value().
1741 * It invokes the associated gpio_chip.set() method.
1743 void __gpio_set_value(unsigned gpio, int value)
1745 struct gpio_chip *chip;
1747 chip = gpio_to_chip(gpio);
1748 /* Should be using gpio_set_value_cansleep() */
1749 WARN_ON(chip->can_sleep);
1750 trace_gpio_value(gpio, 0, value);
1751 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1752 _gpio_set_open_drain_value(gpio, chip, value);
1753 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1754 _gpio_set_open_source_value(gpio, chip, value);
1756 chip->set(chip, gpio - chip->base, value);
1758 EXPORT_SYMBOL_GPL(__gpio_set_value);
1761 * __gpio_cansleep() - report whether gpio value access will sleep
1762 * @gpio: gpio in question
1765 * This is used directly or indirectly to implement gpio_cansleep(). It
1766 * returns nonzero if access reading or writing the GPIO value can sleep.
1768 int __gpio_cansleep(unsigned gpio)
1770 struct gpio_chip *chip;
1772 /* only call this on GPIOs that are valid! */
1773 chip = gpio_to_chip(gpio);
1775 return chip->can_sleep;
1777 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1780 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1781 * @gpio: gpio whose IRQ will be returned (already requested)
1784 * This is used directly or indirectly to implement gpio_to_irq().
1785 * It returns the number of the IRQ signaled by this (input) GPIO,
1786 * or a negative errno.
1788 int __gpio_to_irq(unsigned gpio)
1790 struct gpio_chip *chip;
1792 chip = gpio_to_chip(gpio);
1793 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1795 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1799 /* There's no value in making it easy to inline GPIO calls that may sleep.
1800 * Common examples include ones connected to I2C or SPI chips.
1803 int gpio_get_value_cansleep(unsigned gpio)
1805 struct gpio_chip *chip;
1808 might_sleep_if(extra_checks);
1809 chip = gpio_to_chip(gpio);
1810 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1811 trace_gpio_value(gpio, 1, value);
1814 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1816 void gpio_set_value_cansleep(unsigned gpio, int value)
1818 struct gpio_chip *chip;
1820 might_sleep_if(extra_checks);
1821 chip = gpio_to_chip(gpio);
1822 trace_gpio_value(gpio, 0, value);
1823 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1824 _gpio_set_open_drain_value(gpio, chip, value);
1825 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1826 _gpio_set_open_source_value(gpio, chip, value);
1828 chip->set(chip, gpio - chip->base, value);
1830 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1833 #ifdef CONFIG_DEBUG_FS
1835 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1838 unsigned gpio = chip->base;
1839 struct gpio_desc *gdesc = &gpio_desc[gpio];
1842 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1843 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1846 gpio_get_direction(gpio);
1847 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1848 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1850 is_out ? "out" : "in ",
1852 ? (chip->get(chip, i) ? "hi" : "lo")
1854 seq_printf(s, "\n");
1858 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
1860 struct gpio_chip *chip = NULL;
1865 /* REVISIT this isn't locked against gpio_chip removal ... */
1867 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1868 if (gpio_desc[gpio].chip == chip)
1871 chip = gpio_desc[gpio].chip;
1875 if (index++ >= *pos) {
1886 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1888 struct gpio_chip *chip = v;
1892 /* skip GPIOs provided by the current chip */
1893 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1894 chip = gpio_desc[gpio].chip;
1907 static void gpiolib_seq_stop(struct seq_file *s, void *v)
1911 static int gpiolib_seq_show(struct seq_file *s, void *v)
1913 struct gpio_chip *chip = v;
1916 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1917 chip->base, chip->base + chip->ngpio - 1);
1920 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1923 seq_printf(s, ", %s", chip->label);
1924 if (chip->can_sleep)
1925 seq_printf(s, ", can sleep");
1926 seq_printf(s, ":\n");
1929 chip->dbg_show(s, chip);
1931 gpiolib_dbg_show(s, chip);
1936 static const struct seq_operations gpiolib_seq_ops = {
1937 .start = gpiolib_seq_start,
1938 .next = gpiolib_seq_next,
1939 .stop = gpiolib_seq_stop,
1940 .show = gpiolib_seq_show,
1943 static int gpiolib_open(struct inode *inode, struct file *file)
1945 return seq_open(file, &gpiolib_seq_ops);
1948 static const struct file_operations gpiolib_operations = {
1949 .owner = THIS_MODULE,
1950 .open = gpiolib_open,
1952 .llseek = seq_lseek,
1953 .release = seq_release,
1956 static int __init gpiolib_debugfs_init(void)
1958 /* /sys/kernel/debug/gpio */
1959 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1960 NULL, NULL, &gpiolib_operations);
1963 subsys_initcall(gpiolib_debugfs_init);
1965 #endif /* DEBUG_FS */