1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/gpio.h>
19 /* Optional implementation infrastructure for GPIO interfaces.
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 #define extra_checks 1
41 #define extra_checks 0
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 static DEFINE_SPINLOCK(gpio_lock);
51 struct gpio_chip *chip;
53 /* flag symbols are bit numbers */
54 #define FLAG_REQUESTED 0
56 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
57 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60 #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
64 #define ID_SHIFT 16 /* add new flags before this one */
66 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
69 #ifdef CONFIG_DEBUG_FS
73 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
75 static LIST_HEAD(gpio_chips);
77 #ifdef CONFIG_GPIO_SYSFS
78 static DEFINE_IDR(dirent_idr);
81 static inline void desc_set_label(struct gpio_desc *d, const char *label)
83 #ifdef CONFIG_DEBUG_FS
88 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
89 * when setting direction, and otherwise illegal. Until board setup code
90 * and drivers use explicit requests everywhere (which won't happen when
91 * those calls have no teeth) we can't avoid autorequesting. This nag
92 * message should motivate switching to explicit requests... so should
93 * the weaker cleanup after faults, compared to gpio_request().
95 * NOTE: the autorequest mechanism is going away; at this point it's
96 * only "legal" in the sense that (old) code using it won't break yet,
97 * but instead only triggers a WARN() stack dump.
99 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
101 const struct gpio_chip *chip = desc->chip;
102 const int gpio = chip->base + offset;
104 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
105 "autorequest GPIO-%d\n", gpio)) {
106 if (!try_module_get(chip->owner)) {
107 pr_err("GPIO-%d: module can't be gotten \n", gpio);
108 clear_bit(FLAG_REQUESTED, &desc->flags);
112 desc_set_label(desc, "[auto]");
113 /* caller must chip->request() w/o spinlock */
120 /* caller holds gpio_lock *OR* gpio is marked as requested */
121 struct gpio_chip *gpio_to_chip(unsigned gpio)
123 return gpio_desc[gpio].chip;
126 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
127 static int gpiochip_find_base(int ngpio)
133 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
134 struct gpio_desc *desc = &gpio_desc[i];
135 struct gpio_chip *chip = desc->chip;
139 if (spare == ngpio) {
146 i -= chip->ngpio - 1;
150 if (gpio_is_valid(base))
151 pr_debug("%s: found new base at %d\n", __func__, base);
155 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
156 static int gpio_get_direction(unsigned gpio)
158 struct gpio_chip *chip;
159 struct gpio_desc *desc = &gpio_desc[gpio];
160 int status = -EINVAL;
162 chip = gpio_to_chip(gpio);
165 if (!chip->get_direction)
168 status = chip->get_direction(chip, gpio);
170 /* GPIOF_DIR_IN, or other positive */
172 clear_bit(FLAG_IS_OUT, &desc->flags);
176 set_bit(FLAG_IS_OUT, &desc->flags);
181 #ifdef CONFIG_GPIO_SYSFS
183 /* lock protects against unexport_gpio() being called while
184 * sysfs files are active.
186 static DEFINE_MUTEX(sysfs_lock);
189 * /sys/class/gpio/gpioN... only for GPIOs that are exported
191 * * MAY BE OMITTED if kernel won't allow direction changes
192 * * is read/write as "in" or "out"
193 * * may also be written as "high" or "low", initializing
194 * output value as specified ("out" implies "low")
196 * * always readable, subject to hardware behavior
197 * * may be writable, as zero/nonzero
199 * * configures behavior of poll(2) on /value
200 * * available only if pin can generate IRQs on input
201 * * is read/write as "none", "falling", "rising", or "both"
203 * * configures polarity of /value
204 * * is read/write as zero/nonzero
205 * * also affects existing and subsequent "falling" and "rising"
206 * /edge configuration
209 static ssize_t gpio_direction_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 const struct gpio_desc *desc = dev_get_drvdata(dev);
213 unsigned gpio = desc - gpio_desc;
216 mutex_lock(&sysfs_lock);
218 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
221 gpio_get_direction(gpio);
222 status = sprintf(buf, "%s\n",
223 test_bit(FLAG_IS_OUT, &desc->flags)
227 mutex_unlock(&sysfs_lock);
231 static ssize_t gpio_direction_store(struct device *dev,
232 struct device_attribute *attr, const char *buf, size_t size)
234 const struct gpio_desc *desc = dev_get_drvdata(dev);
235 unsigned gpio = desc - gpio_desc;
238 mutex_lock(&sysfs_lock);
240 if (!test_bit(FLAG_EXPORT, &desc->flags))
242 else if (sysfs_streq(buf, "high"))
243 status = gpio_direction_output(gpio, 1);
244 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
245 status = gpio_direction_output(gpio, 0);
246 else if (sysfs_streq(buf, "in"))
247 status = gpio_direction_input(gpio);
251 mutex_unlock(&sysfs_lock);
252 return status ? : size;
255 static /* const */ DEVICE_ATTR(direction, 0644,
256 gpio_direction_show, gpio_direction_store);
258 static ssize_t gpio_value_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 const struct gpio_desc *desc = dev_get_drvdata(dev);
262 unsigned gpio = desc - gpio_desc;
265 mutex_lock(&sysfs_lock);
267 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
272 value = !!gpio_get_value_cansleep(gpio);
273 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
276 status = sprintf(buf, "%d\n", value);
279 mutex_unlock(&sysfs_lock);
283 static ssize_t gpio_value_store(struct device *dev,
284 struct device_attribute *attr, const char *buf, size_t size)
286 const struct gpio_desc *desc = dev_get_drvdata(dev);
287 unsigned gpio = desc - gpio_desc;
290 mutex_lock(&sysfs_lock);
292 if (!test_bit(FLAG_EXPORT, &desc->flags))
294 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
299 status = strict_strtol(buf, 0, &value);
301 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
303 gpio_set_value_cansleep(gpio, value != 0);
308 mutex_unlock(&sysfs_lock);
312 static const DEVICE_ATTR(value, 0644,
313 gpio_value_show, gpio_value_store);
315 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
317 struct sysfs_dirent *value_sd = priv;
319 sysfs_notify_dirent(value_sd);
323 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
324 unsigned long gpio_flags)
326 struct sysfs_dirent *value_sd;
327 unsigned long irq_flags;
330 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
333 irq = gpio_to_irq(desc - gpio_desc);
337 id = desc->flags >> ID_SHIFT;
338 value_sd = idr_find(&dirent_idr, id);
340 free_irq(irq, value_sd);
342 desc->flags &= ~GPIO_TRIGGER_MASK;
349 irq_flags = IRQF_SHARED;
350 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
351 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
352 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
353 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
354 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
355 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
358 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
366 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
367 ret = idr_get_new_above(&dirent_idr, value_sd,
369 } while (ret == -EAGAIN);
374 desc->flags &= GPIO_FLAGS_MASK;
375 desc->flags |= (unsigned long)id << ID_SHIFT;
377 if (desc->flags >> ID_SHIFT != id) {
383 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
384 "gpiolib", value_sd);
388 desc->flags |= gpio_flags;
392 idr_remove(&dirent_idr, id);
393 desc->flags &= GPIO_FLAGS_MASK;
401 static const struct {
404 } trigger_types[] = {
406 { "falling", BIT(FLAG_TRIG_FALL) },
407 { "rising", BIT(FLAG_TRIG_RISE) },
408 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
411 static ssize_t gpio_edge_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 const struct gpio_desc *desc = dev_get_drvdata(dev);
417 mutex_lock(&sysfs_lock);
419 if (!test_bit(FLAG_EXPORT, &desc->flags))
425 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
426 if ((desc->flags & GPIO_TRIGGER_MASK)
427 == trigger_types[i].flags) {
428 status = sprintf(buf, "%s\n",
429 trigger_types[i].name);
434 mutex_unlock(&sysfs_lock);
438 static ssize_t gpio_edge_store(struct device *dev,
439 struct device_attribute *attr, const char *buf, size_t size)
441 struct gpio_desc *desc = dev_get_drvdata(dev);
445 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
446 if (sysfs_streq(trigger_types[i].name, buf))
451 mutex_lock(&sysfs_lock);
453 if (!test_bit(FLAG_EXPORT, &desc->flags))
456 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
461 mutex_unlock(&sysfs_lock);
466 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
468 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
473 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
477 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
479 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
481 /* reconfigure poll(2) support if enabled on one edge only */
482 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
483 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
484 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
486 gpio_setup_irq(desc, dev, 0);
487 status = gpio_setup_irq(desc, dev, trigger_flags);
493 static ssize_t gpio_active_low_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
496 const struct gpio_desc *desc = dev_get_drvdata(dev);
499 mutex_lock(&sysfs_lock);
501 if (!test_bit(FLAG_EXPORT, &desc->flags))
504 status = sprintf(buf, "%d\n",
505 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
507 mutex_unlock(&sysfs_lock);
512 static ssize_t gpio_active_low_store(struct device *dev,
513 struct device_attribute *attr, const char *buf, size_t size)
515 struct gpio_desc *desc = dev_get_drvdata(dev);
518 mutex_lock(&sysfs_lock);
520 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
525 status = strict_strtol(buf, 0, &value);
527 status = sysfs_set_active_low(desc, dev, value != 0);
530 mutex_unlock(&sysfs_lock);
532 return status ? : size;
535 static const DEVICE_ATTR(active_low, 0644,
536 gpio_active_low_show, gpio_active_low_store);
538 static const struct attribute *gpio_attrs[] = {
539 &dev_attr_value.attr,
540 &dev_attr_active_low.attr,
544 static const struct attribute_group gpio_attr_group = {
545 .attrs = (struct attribute **) gpio_attrs,
549 * /sys/class/gpio/gpiochipN/
550 * /base ... matching gpio_chip.base (N)
551 * /label ... matching gpio_chip.label
552 * /ngpio ... matching gpio_chip.ngpio
555 static ssize_t chip_base_show(struct device *dev,
556 struct device_attribute *attr, char *buf)
558 const struct gpio_chip *chip = dev_get_drvdata(dev);
560 return sprintf(buf, "%d\n", chip->base);
562 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
564 static ssize_t chip_label_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
567 const struct gpio_chip *chip = dev_get_drvdata(dev);
569 return sprintf(buf, "%s\n", chip->label ? : "");
571 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
573 static ssize_t chip_ngpio_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
576 const struct gpio_chip *chip = dev_get_drvdata(dev);
578 return sprintf(buf, "%u\n", chip->ngpio);
580 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
582 static const struct attribute *gpiochip_attrs[] = {
584 &dev_attr_label.attr,
585 &dev_attr_ngpio.attr,
589 static const struct attribute_group gpiochip_attr_group = {
590 .attrs = (struct attribute **) gpiochip_attrs,
594 * /sys/class/gpio/export ... write-only
595 * integer N ... number of GPIO to export (full access)
596 * /sys/class/gpio/unexport ... write-only
597 * integer N ... number of GPIO to unexport
599 static ssize_t export_store(struct class *class,
600 struct class_attribute *attr,
601 const char *buf, size_t len)
606 status = strict_strtol(buf, 0, &gpio);
610 /* No extra locking here; FLAG_SYSFS just signifies that the
611 * request and export were done by on behalf of userspace, so
612 * they may be undone on its behalf too.
615 status = gpio_request(gpio, "sysfs");
617 if (status == -EPROBE_DEFER)
621 status = gpio_export(gpio, true);
625 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
629 pr_debug("%s: status %d\n", __func__, status);
630 return status ? : len;
633 static ssize_t unexport_store(struct class *class,
634 struct class_attribute *attr,
635 const char *buf, size_t len)
640 status = strict_strtol(buf, 0, &gpio);
646 /* reject bogus commands (gpio_unexport ignores them) */
647 if (!gpio_is_valid(gpio))
650 /* No extra locking here; FLAG_SYSFS just signifies that the
651 * request and export were done by on behalf of userspace, so
652 * they may be undone on its behalf too.
654 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
660 pr_debug("%s: status %d\n", __func__, status);
661 return status ? : len;
664 static struct class_attribute gpio_class_attrs[] = {
665 __ATTR(export, 0200, NULL, export_store),
666 __ATTR(unexport, 0200, NULL, unexport_store),
670 static struct class gpio_class = {
672 .owner = THIS_MODULE,
674 .class_attrs = gpio_class_attrs,
679 * gpio_export - export a GPIO through sysfs
680 * @gpio: gpio to make available, already requested
681 * @direction_may_change: true if userspace may change gpio direction
682 * Context: arch_initcall or later
684 * When drivers want to make a GPIO accessible to userspace after they
685 * have requested it -- perhaps while debugging, or as part of their
686 * public interface -- they may use this routine. If the GPIO can
687 * change direction (some can't) and the caller allows it, userspace
688 * will see "direction" sysfs attribute which may be used to change
689 * the gpio's direction. A "value" attribute will always be provided.
691 * Returns zero on success, else an error.
693 int gpio_export(unsigned gpio, bool direction_may_change)
696 struct gpio_desc *desc;
698 const char *ioname = NULL;
701 /* can't export until sysfs is available ... */
703 pr_debug("%s: called too early!\n", __func__);
707 if (!gpio_is_valid(gpio)) {
708 pr_debug("%s: gpio %d is not valid\n", __func__, gpio);
712 mutex_lock(&sysfs_lock);
714 spin_lock_irqsave(&gpio_lock, flags);
715 desc = &gpio_desc[gpio];
716 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
717 test_bit(FLAG_EXPORT, &desc->flags)) {
718 spin_unlock_irqrestore(&gpio_lock, flags);
719 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
721 test_bit(FLAG_REQUESTED, &desc->flags),
722 test_bit(FLAG_EXPORT, &desc->flags));
727 if (!desc->chip->direction_input || !desc->chip->direction_output)
728 direction_may_change = false;
729 spin_unlock_irqrestore(&gpio_lock, flags);
731 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
732 ioname = desc->chip->names[gpio - desc->chip->base];
734 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
735 desc, ioname ? ioname : "gpio%u", gpio);
737 status = PTR_ERR(dev);
741 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
743 goto fail_unregister_device;
745 if (direction_may_change) {
746 status = device_create_file(dev, &dev_attr_direction);
748 goto fail_unregister_device;
751 if (gpio_to_irq(gpio) >= 0 && (direction_may_change ||
752 !test_bit(FLAG_IS_OUT, &desc->flags))) {
753 status = device_create_file(dev, &dev_attr_edge);
755 goto fail_unregister_device;
758 set_bit(FLAG_EXPORT, &desc->flags);
759 mutex_unlock(&sysfs_lock);
762 fail_unregister_device:
763 device_unregister(dev);
765 mutex_unlock(&sysfs_lock);
766 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
769 EXPORT_SYMBOL_GPL(gpio_export);
771 static int match_export(struct device *dev, void *data)
773 return dev_get_drvdata(dev) == data;
777 * gpio_export_link - create a sysfs link to an exported GPIO node
778 * @dev: device under which to create symlink
779 * @name: name of the symlink
780 * @gpio: gpio to create symlink to, already exported
782 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
783 * node. Caller is responsible for unlinking.
785 * Returns zero on success, else an error.
787 int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
789 struct gpio_desc *desc;
790 int status = -EINVAL;
792 if (!gpio_is_valid(gpio))
795 mutex_lock(&sysfs_lock);
797 desc = &gpio_desc[gpio];
799 if (test_bit(FLAG_EXPORT, &desc->flags)) {
802 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
804 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
811 mutex_unlock(&sysfs_lock);
815 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
819 EXPORT_SYMBOL_GPL(gpio_export_link);
823 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
824 * @gpio: gpio to change
825 * @value: non-zero to use active low, i.e. inverted values
827 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
828 * The GPIO does not have to be exported yet. If poll(2) support has
829 * been enabled for either rising or falling edge, it will be
830 * reconfigured to follow the new polarity.
832 * Returns zero on success, else an error.
834 int gpio_sysfs_set_active_low(unsigned gpio, int value)
836 struct gpio_desc *desc;
837 struct device *dev = NULL;
838 int status = -EINVAL;
840 if (!gpio_is_valid(gpio))
843 mutex_lock(&sysfs_lock);
845 desc = &gpio_desc[gpio];
847 if (test_bit(FLAG_EXPORT, &desc->flags)) {
848 dev = class_find_device(&gpio_class, NULL, desc, match_export);
855 status = sysfs_set_active_low(desc, dev, value);
858 mutex_unlock(&sysfs_lock);
862 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
866 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
869 * gpio_unexport - reverse effect of gpio_export()
870 * @gpio: gpio to make unavailable
872 * This is implicit on gpio_free().
874 void gpio_unexport(unsigned gpio)
876 struct gpio_desc *desc;
878 struct device *dev = NULL;
880 if (!gpio_is_valid(gpio)) {
885 mutex_lock(&sysfs_lock);
887 desc = &gpio_desc[gpio];
889 if (test_bit(FLAG_EXPORT, &desc->flags)) {
891 dev = class_find_device(&gpio_class, NULL, desc, match_export);
893 gpio_setup_irq(desc, dev, 0);
894 clear_bit(FLAG_EXPORT, &desc->flags);
899 mutex_unlock(&sysfs_lock);
901 device_unregister(dev);
906 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
908 EXPORT_SYMBOL_GPL(gpio_unexport);
910 static int gpiochip_export(struct gpio_chip *chip)
915 /* Many systems register gpio chips for SOC support very early,
916 * before driver model support is available. In those cases we
917 * export this later, in gpiolib_sysfs_init() ... here we just
918 * verify that _some_ field of gpio_class got initialized.
923 /* use chip->base for the ID; it's already known to be unique */
924 mutex_lock(&sysfs_lock);
925 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
926 "gpiochip%d", chip->base);
928 status = sysfs_create_group(&dev->kobj,
929 &gpiochip_attr_group);
931 status = PTR_ERR(dev);
932 chip->exported = (status == 0);
933 mutex_unlock(&sysfs_lock);
939 spin_lock_irqsave(&gpio_lock, flags);
941 while (gpio_desc[gpio].chip == chip)
942 gpio_desc[gpio++].chip = NULL;
943 spin_unlock_irqrestore(&gpio_lock, flags);
945 pr_debug("%s: chip %s status %d\n", __func__,
946 chip->label, status);
952 static void gpiochip_unexport(struct gpio_chip *chip)
957 mutex_lock(&sysfs_lock);
958 dev = class_find_device(&gpio_class, NULL, chip, match_export);
961 device_unregister(dev);
966 mutex_unlock(&sysfs_lock);
969 pr_debug("%s: chip %s status %d\n", __func__,
970 chip->label, status);
973 static int __init gpiolib_sysfs_init(void)
977 struct gpio_chip *chip;
979 status = class_register(&gpio_class);
983 /* Scan and register the gpio_chips which registered very
984 * early (e.g. before the class_register above was called).
986 * We run before arch_initcall() so chip->dev nodes can have
987 * registered, and so arch_initcall() can always gpio_export().
989 spin_lock_irqsave(&gpio_lock, flags);
990 list_for_each_entry(chip, &gpio_chips, list) {
991 if (!chip || chip->exported)
994 spin_unlock_irqrestore(&gpio_lock, flags);
995 status = gpiochip_export(chip);
996 spin_lock_irqsave(&gpio_lock, flags);
998 spin_unlock_irqrestore(&gpio_lock, flags);
1003 postcore_initcall(gpiolib_sysfs_init);
1006 static inline int gpiochip_export(struct gpio_chip *chip)
1011 static inline void gpiochip_unexport(struct gpio_chip *chip)
1015 #endif /* CONFIG_GPIO_SYSFS */
1018 * Add a new chip to the global chips list, keeping the list of chips sorted
1021 * Return -EBUSY if the new chip overlaps with some other chip's integer
1024 static int gpiochip_add_to_list(struct gpio_chip *chip)
1026 struct list_head *pos = &gpio_chips;
1027 struct gpio_chip *_chip;
1030 /* find where to insert our chip */
1031 list_for_each(pos, &gpio_chips) {
1032 _chip = list_entry(pos, struct gpio_chip, list);
1033 /* shall we insert before _chip? */
1034 if (_chip->base >= chip->base + chip->ngpio)
1038 /* are we stepping on the chip right before? */
1039 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1040 _chip = list_entry(pos->prev, struct gpio_chip, list);
1041 if (_chip->base + _chip->ngpio > chip->base) {
1043 "GPIO integer space overlap, cannot add chip\n");
1049 list_add_tail(&chip->list, pos);
1055 * gpiochip_add() - register a gpio_chip
1056 * @chip: the chip to register, with chip->base initialized
1057 * Context: potentially before irqs or kmalloc will work
1059 * Returns a negative errno if the chip can't be registered, such as
1060 * because the chip->base is invalid or already associated with a
1061 * different chip. Otherwise it returns zero as a success code.
1063 * When gpiochip_add() is called very early during boot, so that GPIOs
1064 * can be freely used, the chip->dev device must be registered before
1065 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1066 * for GPIOs will fail rudely.
1068 * If chip->base is negative, this requests dynamic assignment of
1069 * a range of valid GPIOs.
1071 int gpiochip_add(struct gpio_chip *chip)
1073 unsigned long flags;
1076 int base = chip->base;
1078 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1084 spin_lock_irqsave(&gpio_lock, flags);
1087 base = gpiochip_find_base(chip->ngpio);
1095 status = gpiochip_add_to_list(chip);
1098 for (id = base; id < base + chip->ngpio; id++) {
1099 gpio_desc[id].chip = chip;
1101 /* REVISIT: most hardware initializes GPIOs as
1102 * inputs (often with pullups enabled) so power
1103 * usage is minimized. Linux code should set the
1104 * gpio direction first thing; but until it does,
1105 * and in case chip->get_direction is not set,
1106 * we may expose the wrong direction in sysfs.
1108 gpio_desc[id].flags = !chip->direction_input
1109 ? (1 << FLAG_IS_OUT)
1114 #ifdef CONFIG_PINCTRL
1115 INIT_LIST_HEAD(&chip->pin_ranges);
1118 of_gpiochip_add(chip);
1121 spin_unlock_irqrestore(&gpio_lock, flags);
1126 status = gpiochip_export(chip);
1130 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1131 chip->base, chip->base + chip->ngpio - 1,
1132 chip->label ? : "generic");
1136 /* failures here can mean systems won't boot... */
1137 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1138 chip->base, chip->base + chip->ngpio - 1,
1139 chip->label ? : "generic");
1142 EXPORT_SYMBOL_GPL(gpiochip_add);
1145 * gpiochip_remove() - unregister a gpio_chip
1146 * @chip: the chip to unregister
1148 * A gpio_chip with any GPIOs still requested may not be removed.
1150 int gpiochip_remove(struct gpio_chip *chip)
1152 unsigned long flags;
1156 spin_lock_irqsave(&gpio_lock, flags);
1158 gpiochip_remove_pin_ranges(chip);
1159 of_gpiochip_remove(chip);
1161 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1162 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1168 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1169 gpio_desc[id].chip = NULL;
1171 list_del(&chip->list);
1174 spin_unlock_irqrestore(&gpio_lock, flags);
1177 gpiochip_unexport(chip);
1181 EXPORT_SYMBOL_GPL(gpiochip_remove);
1184 * gpiochip_find() - iterator for locating a specific gpio_chip
1185 * @data: data to pass to match function
1186 * @callback: Callback function to check gpio_chip
1188 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1189 * determined by a user supplied @match callback. The callback should return
1190 * 0 if the device doesn't match and non-zero if it does. If the callback is
1191 * non-zero, this function will return to the caller and not iterate over any
1194 struct gpio_chip *gpiochip_find(void *data,
1195 int (*match)(struct gpio_chip *chip,
1198 struct gpio_chip *chip = NULL;
1199 unsigned long flags;
1202 spin_lock_irqsave(&gpio_lock, flags);
1203 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1204 if (!gpio_desc[i].chip)
1207 if (match(gpio_desc[i].chip, data)) {
1208 chip = gpio_desc[i].chip;
1212 spin_unlock_irqrestore(&gpio_lock, flags);
1216 EXPORT_SYMBOL_GPL(gpiochip_find);
1218 #ifdef CONFIG_PINCTRL
1221 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1222 * @chip: the gpiochip to add the range for
1223 * @pinctrl_name: the dev_name() of the pin controller to map to
1224 * @gpio_offset: the start offset in the current gpio_chip number space
1225 * @pin_offset: the start offset in the pin controller number space
1226 * @npins: the number of pins from the offset of each pin space (GPIO and
1227 * pin controller) to accumulate in this range
1229 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1230 unsigned int gpio_offset, unsigned int pin_offset,
1233 struct gpio_pin_range *pin_range;
1236 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1238 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1243 /* Use local offset as range ID */
1244 pin_range->range.id = gpio_offset;
1245 pin_range->range.gc = chip;
1246 pin_range->range.name = chip->label;
1247 pin_range->range.base = chip->base + gpio_offset;
1248 pin_range->range.pin_base = pin_offset;
1249 pin_range->range.npins = npins;
1250 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1252 if (IS_ERR(pin_range->pctldev)) {
1253 ret = PTR_ERR(pin_range->pctldev);
1254 pr_err("%s: GPIO chip: could not create pin range\n",
1259 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1260 chip->label, gpio_offset, gpio_offset + npins - 1,
1262 pin_offset, pin_offset + npins - 1);
1264 list_add_tail(&pin_range->node, &chip->pin_ranges);
1268 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1271 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1272 * @chip: the chip to remove all the mappings for
1274 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1276 struct gpio_pin_range *pin_range, *tmp;
1278 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1279 list_del(&pin_range->node);
1280 pinctrl_remove_gpio_range(pin_range->pctldev,
1285 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1287 #endif /* CONFIG_PINCTRL */
1289 /* These "optional" allocation calls help prevent drivers from stomping
1290 * on each other, and help provide better diagnostics in debugfs.
1291 * They're called even less than the "set direction" calls.
1293 int gpio_request(unsigned gpio, const char *label)
1295 struct gpio_desc *desc;
1296 struct gpio_chip *chip;
1297 int status = -EPROBE_DEFER;
1298 unsigned long flags;
1300 spin_lock_irqsave(&gpio_lock, flags);
1302 if (!gpio_is_valid(gpio)) {
1306 desc = &gpio_desc[gpio];
1311 if (!try_module_get(chip->owner))
1314 /* NOTE: gpio_request() can be called in early boot,
1315 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1318 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1319 desc_set_label(desc, label ? : "?");
1323 module_put(chip->owner);
1327 if (chip->request) {
1328 /* chip->request may sleep */
1329 spin_unlock_irqrestore(&gpio_lock, flags);
1330 status = chip->request(chip, gpio - chip->base);
1331 spin_lock_irqsave(&gpio_lock, flags);
1334 desc_set_label(desc, NULL);
1335 module_put(chip->owner);
1336 clear_bit(FLAG_REQUESTED, &desc->flags);
1340 if (chip->get_direction) {
1341 /* chip->get_direction may sleep */
1342 spin_unlock_irqrestore(&gpio_lock, flags);
1343 gpio_get_direction(gpio);
1344 spin_lock_irqsave(&gpio_lock, flags);
1348 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1349 gpio, label ? : "?", status);
1350 spin_unlock_irqrestore(&gpio_lock, flags);
1353 EXPORT_SYMBOL_GPL(gpio_request);
1355 void gpio_free(unsigned gpio)
1357 unsigned long flags;
1358 struct gpio_desc *desc;
1359 struct gpio_chip *chip;
1363 if (!gpio_is_valid(gpio)) {
1364 WARN_ON(extra_checks);
1368 gpio_unexport(gpio);
1370 spin_lock_irqsave(&gpio_lock, flags);
1372 desc = &gpio_desc[gpio];
1374 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1376 spin_unlock_irqrestore(&gpio_lock, flags);
1377 might_sleep_if(chip->can_sleep);
1378 chip->free(chip, gpio - chip->base);
1379 spin_lock_irqsave(&gpio_lock, flags);
1381 desc_set_label(desc, NULL);
1382 module_put(desc->chip->owner);
1383 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1384 clear_bit(FLAG_REQUESTED, &desc->flags);
1385 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1386 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1388 WARN_ON(extra_checks);
1390 spin_unlock_irqrestore(&gpio_lock, flags);
1392 EXPORT_SYMBOL_GPL(gpio_free);
1395 * gpio_request_one - request a single GPIO with initial configuration
1396 * @gpio: the GPIO number
1397 * @flags: GPIO configuration as specified by GPIOF_*
1398 * @label: a literal description string of this GPIO
1400 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1404 err = gpio_request(gpio, label);
1408 if (flags & GPIOF_OPEN_DRAIN)
1409 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1411 if (flags & GPIOF_OPEN_SOURCE)
1412 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1414 if (flags & GPIOF_DIR_IN)
1415 err = gpio_direction_input(gpio);
1417 err = gpio_direction_output(gpio,
1418 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1423 if (flags & GPIOF_EXPORT) {
1424 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1435 EXPORT_SYMBOL_GPL(gpio_request_one);
1438 * gpio_request_array - request multiple GPIOs in a single call
1439 * @array: array of the 'struct gpio'
1440 * @num: how many GPIOs in the array
1442 int gpio_request_array(const struct gpio *array, size_t num)
1446 for (i = 0; i < num; i++, array++) {
1447 err = gpio_request_one(array->gpio, array->flags, array->label);
1455 gpio_free((--array)->gpio);
1458 EXPORT_SYMBOL_GPL(gpio_request_array);
1461 * gpio_free_array - release multiple GPIOs in a single call
1462 * @array: array of the 'struct gpio'
1463 * @num: how many GPIOs in the array
1465 void gpio_free_array(const struct gpio *array, size_t num)
1468 gpio_free((array++)->gpio);
1470 EXPORT_SYMBOL_GPL(gpio_free_array);
1473 * gpiochip_is_requested - return string iff signal was requested
1474 * @chip: controller managing the signal
1475 * @offset: of signal within controller's 0..(ngpio - 1) range
1477 * Returns NULL if the GPIO is not currently requested, else a string.
1478 * If debugfs support is enabled, the string returned is the label passed
1479 * to gpio_request(); otherwise it is a meaningless constant.
1481 * This function is for use by GPIO controller drivers. The label can
1482 * help with diagnostics, and knowing that the signal is used as a GPIO
1483 * can help avoid accidentally multiplexing it to another controller.
1485 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1487 unsigned gpio = chip->base + offset;
1489 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1491 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1493 #ifdef CONFIG_DEBUG_FS
1494 return gpio_desc[gpio].label;
1499 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1502 /* Drivers MUST set GPIO direction before making get/set calls. In
1503 * some cases this is done in early boot, before IRQs are enabled.
1505 * As a rule these aren't called more than once (except for drivers
1506 * using the open-drain emulation idiom) so these are natural places
1507 * to accumulate extra debugging checks. Note that we can't (yet)
1508 * rely on gpio_request() having been called beforehand.
1511 int gpio_direction_input(unsigned gpio)
1513 unsigned long flags;
1514 struct gpio_chip *chip;
1515 struct gpio_desc *desc = &gpio_desc[gpio];
1516 int status = -EINVAL;
1518 spin_lock_irqsave(&gpio_lock, flags);
1520 if (!gpio_is_valid(gpio))
1523 if (!chip || !chip->get || !chip->direction_input)
1526 if (gpio >= chip->ngpio)
1528 status = gpio_ensure_requested(desc, gpio);
1532 /* now we know the gpio is valid and chip won't vanish */
1534 spin_unlock_irqrestore(&gpio_lock, flags);
1536 might_sleep_if(chip->can_sleep);
1539 status = chip->request(chip, gpio);
1541 pr_debug("GPIO-%d: chip request fail, %d\n",
1542 chip->base + gpio, status);
1543 /* and it's not available to anyone else ...
1544 * gpio_request() is the fully clean solution.
1550 status = chip->direction_input(chip, gpio);
1552 clear_bit(FLAG_IS_OUT, &desc->flags);
1554 trace_gpio_direction(chip->base + gpio, 1, status);
1558 spin_unlock_irqrestore(&gpio_lock, flags);
1560 pr_debug("%s: gpio-%d status %d\n",
1561 __func__, gpio, status);
1564 EXPORT_SYMBOL_GPL(gpio_direction_input);
1566 int gpio_direction_output(unsigned gpio, int value)
1568 unsigned long flags;
1569 struct gpio_chip *chip;
1570 struct gpio_desc *desc = &gpio_desc[gpio];
1571 int status = -EINVAL;
1573 /* Open drain pin should not be driven to 1 */
1574 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1575 return gpio_direction_input(gpio);
1577 /* Open source pin should not be driven to 0 */
1578 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1579 return gpio_direction_input(gpio);
1581 spin_lock_irqsave(&gpio_lock, flags);
1583 if (!gpio_is_valid(gpio))
1586 if (!chip || !chip->set || !chip->direction_output)
1589 if (gpio >= chip->ngpio)
1591 status = gpio_ensure_requested(desc, gpio);
1595 /* now we know the gpio is valid and chip won't vanish */
1597 spin_unlock_irqrestore(&gpio_lock, flags);
1599 might_sleep_if(chip->can_sleep);
1602 status = chip->request(chip, gpio);
1604 pr_debug("GPIO-%d: chip request fail, %d\n",
1605 chip->base + gpio, status);
1606 /* and it's not available to anyone else ...
1607 * gpio_request() is the fully clean solution.
1613 status = chip->direction_output(chip, gpio, value);
1615 set_bit(FLAG_IS_OUT, &desc->flags);
1616 trace_gpio_value(chip->base + gpio, 0, value);
1617 trace_gpio_direction(chip->base + gpio, 0, status);
1621 spin_unlock_irqrestore(&gpio_lock, flags);
1623 pr_debug("%s: gpio-%d status %d\n",
1624 __func__, gpio, status);
1627 EXPORT_SYMBOL_GPL(gpio_direction_output);
1630 * gpio_set_debounce - sets @debounce time for a @gpio
1631 * @gpio: the gpio to set debounce time
1632 * @debounce: debounce time is microseconds
1634 int gpio_set_debounce(unsigned gpio, unsigned debounce)
1636 unsigned long flags;
1637 struct gpio_chip *chip;
1638 struct gpio_desc *desc = &gpio_desc[gpio];
1639 int status = -EINVAL;
1641 spin_lock_irqsave(&gpio_lock, flags);
1643 if (!gpio_is_valid(gpio))
1646 if (!chip || !chip->set || !chip->set_debounce)
1649 if (gpio >= chip->ngpio)
1651 status = gpio_ensure_requested(desc, gpio);
1655 /* now we know the gpio is valid and chip won't vanish */
1657 spin_unlock_irqrestore(&gpio_lock, flags);
1659 might_sleep_if(chip->can_sleep);
1661 return chip->set_debounce(chip, gpio, debounce);
1664 spin_unlock_irqrestore(&gpio_lock, flags);
1666 pr_debug("%s: gpio-%d status %d\n",
1667 __func__, gpio, status);
1671 EXPORT_SYMBOL_GPL(gpio_set_debounce);
1673 /* I/O calls are only valid after configuration completed; the relevant
1674 * "is this a valid GPIO" error checks should already have been done.
1676 * "Get" operations are often inlinable as reading a pin value register,
1677 * and masking the relevant bit in that register.
1679 * When "set" operations are inlinable, they involve writing that mask to
1680 * one register to set a low value, or a different register to set it high.
1681 * Otherwise locking is needed, so there may be little value to inlining.
1683 *------------------------------------------------------------------------
1685 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1686 * have requested the GPIO. That can include implicit requesting by
1687 * a direction setting call. Marking a gpio as requested locks its chip
1688 * in memory, guaranteeing that these table lookups need no more locking
1689 * and that gpiochip_remove() will fail.
1691 * REVISIT when debugging, consider adding some instrumentation to ensure
1692 * that the GPIO was actually requested.
1696 * __gpio_get_value() - return a gpio's value
1697 * @gpio: gpio whose value will be returned
1700 * This is used directly or indirectly to implement gpio_get_value().
1701 * It returns the zero or nonzero value provided by the associated
1702 * gpio_chip.get() method; or zero if no such method is provided.
1704 int __gpio_get_value(unsigned gpio)
1706 struct gpio_chip *chip;
1709 chip = gpio_to_chip(gpio);
1710 /* Should be using gpio_get_value_cansleep() */
1711 WARN_ON(chip->can_sleep);
1712 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1713 trace_gpio_value(gpio, 1, value);
1716 EXPORT_SYMBOL_GPL(__gpio_get_value);
1719 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1720 * @gpio: Gpio whose state need to be set.
1722 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1724 static void _gpio_set_open_drain_value(unsigned gpio,
1725 struct gpio_chip *chip, int value)
1729 err = chip->direction_input(chip, gpio - chip->base);
1731 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1733 err = chip->direction_output(chip, gpio - chip->base, 0);
1735 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1737 trace_gpio_direction(gpio, value, err);
1739 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1740 __func__, gpio, err);
1744 * _gpio_set_open_source() - Set the open source gpio's value.
1745 * @gpio: Gpio whose state need to be set.
1747 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1749 static void _gpio_set_open_source_value(unsigned gpio,
1750 struct gpio_chip *chip, int value)
1754 err = chip->direction_output(chip, gpio - chip->base, 1);
1756 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1758 err = chip->direction_input(chip, gpio - chip->base);
1760 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1762 trace_gpio_direction(gpio, !value, err);
1764 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1765 __func__, gpio, err);
1770 * __gpio_set_value() - assign a gpio's value
1771 * @gpio: gpio whose value will be assigned
1772 * @value: value to assign
1775 * This is used directly or indirectly to implement gpio_set_value().
1776 * It invokes the associated gpio_chip.set() method.
1778 void __gpio_set_value(unsigned gpio, int value)
1780 struct gpio_chip *chip;
1782 chip = gpio_to_chip(gpio);
1783 /* Should be using gpio_set_value_cansleep() */
1784 WARN_ON(chip->can_sleep);
1785 trace_gpio_value(gpio, 0, value);
1786 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1787 _gpio_set_open_drain_value(gpio, chip, value);
1788 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1789 _gpio_set_open_source_value(gpio, chip, value);
1791 chip->set(chip, gpio - chip->base, value);
1793 EXPORT_SYMBOL_GPL(__gpio_set_value);
1796 * __gpio_cansleep() - report whether gpio value access will sleep
1797 * @gpio: gpio in question
1800 * This is used directly or indirectly to implement gpio_cansleep(). It
1801 * returns nonzero if access reading or writing the GPIO value can sleep.
1803 int __gpio_cansleep(unsigned gpio)
1805 struct gpio_chip *chip;
1807 /* only call this on GPIOs that are valid! */
1808 chip = gpio_to_chip(gpio);
1810 return chip->can_sleep;
1812 EXPORT_SYMBOL_GPL(__gpio_cansleep);
1815 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1816 * @gpio: gpio whose IRQ will be returned (already requested)
1819 * This is used directly or indirectly to implement gpio_to_irq().
1820 * It returns the number of the IRQ signaled by this (input) GPIO,
1821 * or a negative errno.
1823 int __gpio_to_irq(unsigned gpio)
1825 struct gpio_chip *chip;
1827 chip = gpio_to_chip(gpio);
1828 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1830 EXPORT_SYMBOL_GPL(__gpio_to_irq);
1834 /* There's no value in making it easy to inline GPIO calls that may sleep.
1835 * Common examples include ones connected to I2C or SPI chips.
1838 int gpio_get_value_cansleep(unsigned gpio)
1840 struct gpio_chip *chip;
1843 might_sleep_if(extra_checks);
1844 chip = gpio_to_chip(gpio);
1845 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1846 trace_gpio_value(gpio, 1, value);
1849 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1851 void gpio_set_value_cansleep(unsigned gpio, int value)
1853 struct gpio_chip *chip;
1855 might_sleep_if(extra_checks);
1856 chip = gpio_to_chip(gpio);
1857 trace_gpio_value(gpio, 0, value);
1858 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1859 _gpio_set_open_drain_value(gpio, chip, value);
1860 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1861 _gpio_set_open_source_value(gpio, chip, value);
1863 chip->set(chip, gpio - chip->base, value);
1865 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1868 #ifdef CONFIG_DEBUG_FS
1870 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1873 unsigned gpio = chip->base;
1874 struct gpio_desc *gdesc = &gpio_desc[gpio];
1877 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1878 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1881 gpio_get_direction(gpio);
1882 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1883 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1885 is_out ? "out" : "in ",
1887 ? (chip->get(chip, i) ? "hi" : "lo")
1889 seq_printf(s, "\n");
1893 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
1895 struct gpio_chip *chip = NULL;
1900 /* REVISIT this isn't locked against gpio_chip removal ... */
1902 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1903 if (gpio_desc[gpio].chip == chip)
1906 chip = gpio_desc[gpio].chip;
1910 if (index++ >= *pos) {
1921 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1923 struct gpio_chip *chip = v;
1927 /* skip GPIOs provided by the current chip */
1928 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1929 chip = gpio_desc[gpio].chip;
1942 static void gpiolib_seq_stop(struct seq_file *s, void *v)
1946 static int gpiolib_seq_show(struct seq_file *s, void *v)
1948 struct gpio_chip *chip = v;
1951 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1952 chip->base, chip->base + chip->ngpio - 1);
1955 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1958 seq_printf(s, ", %s", chip->label);
1959 if (chip->can_sleep)
1960 seq_printf(s, ", can sleep");
1961 seq_printf(s, ":\n");
1964 chip->dbg_show(s, chip);
1966 gpiolib_dbg_show(s, chip);
1971 static const struct seq_operations gpiolib_seq_ops = {
1972 .start = gpiolib_seq_start,
1973 .next = gpiolib_seq_next,
1974 .stop = gpiolib_seq_stop,
1975 .show = gpiolib_seq_show,
1978 static int gpiolib_open(struct inode *inode, struct file *file)
1980 return seq_open(file, &gpiolib_seq_ops);
1983 static const struct file_operations gpiolib_operations = {
1984 .owner = THIS_MODULE,
1985 .open = gpiolib_open,
1987 .llseek = seq_lseek,
1988 .release = seq_release,
1991 static int __init gpiolib_debugfs_init(void)
1993 /* /sys/kernel/debug/gpio */
1994 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1995 NULL, NULL, &gpiolib_operations);
1998 subsys_initcall(gpiolib_debugfs_init);
2000 #endif /* DEBUG_FS */