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>
15 #include <linux/acpi.h>
16 #include <linux/gpio/driver.h>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/gpio.h>
23 /* Implementation infrastructure for GPIO interfaces.
25 * The GPIO programming interface allows for inlining speed-critical
26 * get/set operations for common cases, so that access to SOC-integrated
27 * GPIOs can sometimes cost 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 /* 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 */
62 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
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 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
77 static DEFINE_MUTEX(gpio_lookup_lock);
78 static LIST_HEAD(gpio_lookup_list);
79 static LIST_HEAD(gpio_chips);
81 #ifdef CONFIG_GPIO_SYSFS
82 static DEFINE_IDR(dirent_idr);
85 static int gpiod_request(struct gpio_desc *desc, const char *label);
86 static void gpiod_free(struct gpio_desc *desc);
88 /* With descriptor prefix */
90 #ifdef CONFIG_DEBUG_FS
91 #define gpiod_emerg(desc, fmt, ...) \
92 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
94 #define gpiod_crit(desc, fmt, ...) \
95 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
97 #define gpiod_err(desc, fmt, ...) \
98 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
100 #define gpiod_warn(desc, fmt, ...) \
101 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
103 #define gpiod_info(desc, fmt, ...) \
104 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
106 #define gpiod_dbg(desc, fmt, ...) \
107 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
110 #define gpiod_emerg(desc, fmt, ...) \
111 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
112 #define gpiod_crit(desc, fmt, ...) \
113 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
114 #define gpiod_err(desc, fmt, ...) \
115 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
116 #define gpiod_warn(desc, fmt, ...) \
117 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
118 #define gpiod_info(desc, fmt, ...) \
119 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
120 #define gpiod_dbg(desc, fmt, ...) \
121 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
124 /* With chip prefix */
126 #define chip_emerg(chip, fmt, ...) \
127 pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
128 #define chip_crit(chip, fmt, ...) \
129 pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
130 #define chip_err(chip, fmt, ...) \
131 pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
132 #define chip_warn(chip, fmt, ...) \
133 pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
134 #define chip_info(chip, fmt, ...) \
135 pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
136 #define chip_dbg(chip, fmt, ...) \
137 pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
139 static inline void desc_set_label(struct gpio_desc *d, const char *label)
141 #ifdef CONFIG_DEBUG_FS
147 * Return the GPIO number of the passed descriptor relative to its chip
149 static int gpio_chip_hwgpio(const struct gpio_desc *desc)
151 return desc - &desc->chip->desc[0];
155 * Convert a GPIO number to its descriptor
157 struct gpio_desc *gpio_to_desc(unsigned gpio)
159 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
162 return &gpio_desc[gpio];
164 EXPORT_SYMBOL_GPL(gpio_to_desc);
167 * Convert an offset on a certain chip to a corresponding descriptor
169 static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
172 if (offset >= chip->ngpio)
173 return ERR_PTR(-EINVAL);
175 return &chip->desc[offset];
179 * Convert a GPIO descriptor to the integer namespace.
180 * This should disappear in the future but is needed since we still
181 * use GPIO numbers for error messages and sysfs nodes
183 int desc_to_gpio(const struct gpio_desc *desc)
185 return desc - &gpio_desc[0];
187 EXPORT_SYMBOL_GPL(desc_to_gpio);
190 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
191 * when setting direction, and otherwise illegal. Until board setup code
192 * and drivers use explicit requests everywhere (which won't happen when
193 * those calls have no teeth) we can't avoid autorequesting. This nag
194 * message should motivate switching to explicit requests... so should
195 * the weaker cleanup after faults, compared to gpio_request().
197 * NOTE: the autorequest mechanism is going away; at this point it's
198 * only "legal" in the sense that (old) code using it won't break yet,
199 * but instead only triggers a WARN() stack dump.
201 static int gpio_ensure_requested(struct gpio_desc *desc)
203 const struct gpio_chip *chip = desc->chip;
204 const int gpio = desc_to_gpio(desc);
206 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
207 "autorequest GPIO-%d\n", gpio)) {
208 if (!try_module_get(chip->owner)) {
209 gpiod_err(desc, "%s: module can't be gotten\n",
211 clear_bit(FLAG_REQUESTED, &desc->flags);
215 desc_set_label(desc, "[auto]");
216 /* caller must chip->request() w/o spinlock */
224 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
225 * @desc: descriptor to return the chip of
227 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
229 return desc ? desc->chip : NULL;
231 EXPORT_SYMBOL_GPL(gpiod_to_chip);
233 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
234 static int gpiochip_find_base(int ngpio)
236 struct gpio_chip *chip;
237 int base = ARCH_NR_GPIOS - ngpio;
239 list_for_each_entry_reverse(chip, &gpio_chips, list) {
240 /* found a free space? */
241 if (chip->base + chip->ngpio <= base)
244 /* nope, check the space right before the chip */
245 base = chip->base - ngpio;
248 if (gpio_is_valid(base)) {
249 pr_debug("%s: found new base at %d\n", __func__, base);
252 pr_err("%s: cannot find free range\n", __func__);
258 * gpiod_get_direction - return the current direction of a GPIO
259 * @desc: GPIO to get the direction of
261 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
263 * This function may sleep if gpiod_cansleep() is true.
265 int gpiod_get_direction(const struct gpio_desc *desc)
267 struct gpio_chip *chip;
269 int status = -EINVAL;
271 chip = gpiod_to_chip(desc);
272 offset = gpio_chip_hwgpio(desc);
274 if (!chip->get_direction)
277 status = chip->get_direction(chip, offset);
279 /* GPIOF_DIR_IN, or other positive */
281 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
282 * so it does not affect constness per se */
283 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
287 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
291 EXPORT_SYMBOL_GPL(gpiod_get_direction);
293 #ifdef CONFIG_GPIO_SYSFS
295 /* lock protects against unexport_gpio() being called while
296 * sysfs files are active.
298 static DEFINE_MUTEX(sysfs_lock);
301 * /sys/class/gpio/gpioN... only for GPIOs that are exported
303 * * MAY BE OMITTED if kernel won't allow direction changes
304 * * is read/write as "in" or "out"
305 * * may also be written as "high" or "low", initializing
306 * output value as specified ("out" implies "low")
308 * * always readable, subject to hardware behavior
309 * * may be writable, as zero/nonzero
311 * * configures behavior of poll(2) on /value
312 * * available only if pin can generate IRQs on input
313 * * is read/write as "none", "falling", "rising", or "both"
315 * * configures polarity of /value
316 * * is read/write as zero/nonzero
317 * * also affects existing and subsequent "falling" and "rising"
318 * /edge configuration
321 static ssize_t gpio_direction_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 const struct gpio_desc *desc = dev_get_drvdata(dev);
327 mutex_lock(&sysfs_lock);
329 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
332 gpiod_get_direction(desc);
333 status = sprintf(buf, "%s\n",
334 test_bit(FLAG_IS_OUT, &desc->flags)
338 mutex_unlock(&sysfs_lock);
342 static ssize_t gpio_direction_store(struct device *dev,
343 struct device_attribute *attr, const char *buf, size_t size)
345 struct gpio_desc *desc = dev_get_drvdata(dev);
348 mutex_lock(&sysfs_lock);
350 if (!test_bit(FLAG_EXPORT, &desc->flags))
352 else if (sysfs_streq(buf, "high"))
353 status = gpiod_direction_output_raw(desc, 1);
354 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
355 status = gpiod_direction_output_raw(desc, 0);
356 else if (sysfs_streq(buf, "in"))
357 status = gpiod_direction_input(desc);
361 mutex_unlock(&sysfs_lock);
362 return status ? : size;
365 static /* const */ DEVICE_ATTR(direction, 0644,
366 gpio_direction_show, gpio_direction_store);
368 static ssize_t gpio_value_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct gpio_desc *desc = dev_get_drvdata(dev);
374 mutex_lock(&sysfs_lock);
376 if (!test_bit(FLAG_EXPORT, &desc->flags))
379 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
381 mutex_unlock(&sysfs_lock);
385 static ssize_t gpio_value_store(struct device *dev,
386 struct device_attribute *attr, const char *buf, size_t size)
388 struct gpio_desc *desc = dev_get_drvdata(dev);
391 mutex_lock(&sysfs_lock);
393 if (!test_bit(FLAG_EXPORT, &desc->flags))
395 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
400 status = kstrtol(buf, 0, &value);
402 gpiod_set_value_cansleep(desc, value);
407 mutex_unlock(&sysfs_lock);
411 static const DEVICE_ATTR(value, 0644,
412 gpio_value_show, gpio_value_store);
414 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
416 struct kernfs_node *value_sd = priv;
418 sysfs_notify_dirent(value_sd);
422 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
423 unsigned long gpio_flags)
425 struct kernfs_node *value_sd;
426 unsigned long irq_flags;
429 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
432 irq = gpiod_to_irq(desc);
436 id = desc->flags >> ID_SHIFT;
437 value_sd = idr_find(&dirent_idr, id);
439 free_irq(irq, value_sd);
441 desc->flags &= ~GPIO_TRIGGER_MASK;
444 gpiod_unlock_as_irq(desc);
449 irq_flags = IRQF_SHARED;
450 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
451 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
452 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
453 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
454 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
455 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
458 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
464 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
469 desc->flags &= GPIO_FLAGS_MASK;
470 desc->flags |= (unsigned long)id << ID_SHIFT;
472 if (desc->flags >> ID_SHIFT != id) {
478 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
479 "gpiolib", value_sd);
483 ret = gpiod_lock_as_irq(desc);
485 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
489 desc->flags |= gpio_flags;
493 idr_remove(&dirent_idr, id);
494 desc->flags &= GPIO_FLAGS_MASK;
502 static const struct {
505 } trigger_types[] = {
507 { "falling", BIT(FLAG_TRIG_FALL) },
508 { "rising", BIT(FLAG_TRIG_RISE) },
509 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
512 static ssize_t gpio_edge_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
515 const struct gpio_desc *desc = dev_get_drvdata(dev);
518 mutex_lock(&sysfs_lock);
520 if (!test_bit(FLAG_EXPORT, &desc->flags))
526 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
527 if ((desc->flags & GPIO_TRIGGER_MASK)
528 == trigger_types[i].flags) {
529 status = sprintf(buf, "%s\n",
530 trigger_types[i].name);
535 mutex_unlock(&sysfs_lock);
539 static ssize_t gpio_edge_store(struct device *dev,
540 struct device_attribute *attr, const char *buf, size_t size)
542 struct gpio_desc *desc = dev_get_drvdata(dev);
546 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
547 if (sysfs_streq(trigger_types[i].name, buf))
552 mutex_lock(&sysfs_lock);
554 if (!test_bit(FLAG_EXPORT, &desc->flags))
557 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
562 mutex_unlock(&sysfs_lock);
567 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
569 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
574 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
578 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
580 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
582 /* reconfigure poll(2) support if enabled on one edge only */
583 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
584 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
585 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
587 gpio_setup_irq(desc, dev, 0);
588 status = gpio_setup_irq(desc, dev, trigger_flags);
594 static ssize_t gpio_active_low_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 const struct gpio_desc *desc = dev_get_drvdata(dev);
600 mutex_lock(&sysfs_lock);
602 if (!test_bit(FLAG_EXPORT, &desc->flags))
605 status = sprintf(buf, "%d\n",
606 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
608 mutex_unlock(&sysfs_lock);
613 static ssize_t gpio_active_low_store(struct device *dev,
614 struct device_attribute *attr, const char *buf, size_t size)
616 struct gpio_desc *desc = dev_get_drvdata(dev);
619 mutex_lock(&sysfs_lock);
621 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
626 status = kstrtol(buf, 0, &value);
628 status = sysfs_set_active_low(desc, dev, value != 0);
631 mutex_unlock(&sysfs_lock);
633 return status ? : size;
636 static const DEVICE_ATTR(active_low, 0644,
637 gpio_active_low_show, gpio_active_low_store);
639 static const struct attribute *gpio_attrs[] = {
640 &dev_attr_value.attr,
641 &dev_attr_active_low.attr,
645 static const struct attribute_group gpio_attr_group = {
646 .attrs = (struct attribute **) gpio_attrs,
650 * /sys/class/gpio/gpiochipN/
651 * /base ... matching gpio_chip.base (N)
652 * /label ... matching gpio_chip.label
653 * /ngpio ... matching gpio_chip.ngpio
656 static ssize_t chip_base_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
659 const struct gpio_chip *chip = dev_get_drvdata(dev);
661 return sprintf(buf, "%d\n", chip->base);
663 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
665 static ssize_t chip_label_show(struct device *dev,
666 struct device_attribute *attr, char *buf)
668 const struct gpio_chip *chip = dev_get_drvdata(dev);
670 return sprintf(buf, "%s\n", chip->label ? : "");
672 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
674 static ssize_t chip_ngpio_show(struct device *dev,
675 struct device_attribute *attr, char *buf)
677 const struct gpio_chip *chip = dev_get_drvdata(dev);
679 return sprintf(buf, "%u\n", chip->ngpio);
681 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
683 static const struct attribute *gpiochip_attrs[] = {
685 &dev_attr_label.attr,
686 &dev_attr_ngpio.attr,
690 static const struct attribute_group gpiochip_attr_group = {
691 .attrs = (struct attribute **) gpiochip_attrs,
695 * /sys/class/gpio/export ... write-only
696 * integer N ... number of GPIO to export (full access)
697 * /sys/class/gpio/unexport ... write-only
698 * integer N ... number of GPIO to unexport
700 static ssize_t export_store(struct class *class,
701 struct class_attribute *attr,
702 const char *buf, size_t len)
705 struct gpio_desc *desc;
708 status = kstrtol(buf, 0, &gpio);
712 desc = gpio_to_desc(gpio);
713 /* reject invalid GPIOs */
715 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
719 /* No extra locking here; FLAG_SYSFS just signifies that the
720 * request and export were done by on behalf of userspace, so
721 * they may be undone on its behalf too.
724 status = gpiod_request(desc, "sysfs");
726 if (status == -EPROBE_DEFER)
730 status = gpiod_export(desc, true);
734 set_bit(FLAG_SYSFS, &desc->flags);
738 pr_debug("%s: status %d\n", __func__, status);
739 return status ? : len;
742 static ssize_t unexport_store(struct class *class,
743 struct class_attribute *attr,
744 const char *buf, size_t len)
747 struct gpio_desc *desc;
750 status = kstrtol(buf, 0, &gpio);
754 desc = gpio_to_desc(gpio);
755 /* reject bogus commands (gpio_unexport ignores them) */
757 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
763 /* No extra locking here; FLAG_SYSFS just signifies that the
764 * request and export were done by on behalf of userspace, so
765 * they may be undone on its behalf too.
767 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
773 pr_debug("%s: status %d\n", __func__, status);
774 return status ? : len;
777 static struct class_attribute gpio_class_attrs[] = {
778 __ATTR(export, 0200, NULL, export_store),
779 __ATTR(unexport, 0200, NULL, unexport_store),
783 static struct class gpio_class = {
785 .owner = THIS_MODULE,
787 .class_attrs = gpio_class_attrs,
792 * gpiod_export - export a GPIO through sysfs
793 * @gpio: gpio to make available, already requested
794 * @direction_may_change: true if userspace may change gpio direction
795 * Context: arch_initcall or later
797 * When drivers want to make a GPIO accessible to userspace after they
798 * have requested it -- perhaps while debugging, or as part of their
799 * public interface -- they may use this routine. If the GPIO can
800 * change direction (some can't) and the caller allows it, userspace
801 * will see "direction" sysfs attribute which may be used to change
802 * the gpio's direction. A "value" attribute will always be provided.
804 * Returns zero on success, else an error.
806 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
810 const char *ioname = NULL;
814 /* can't export until sysfs is available ... */
816 pr_debug("%s: called too early!\n", __func__);
821 pr_debug("%s: invalid gpio descriptor\n", __func__);
825 mutex_lock(&sysfs_lock);
827 spin_lock_irqsave(&gpio_lock, flags);
828 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
829 test_bit(FLAG_EXPORT, &desc->flags)) {
830 spin_unlock_irqrestore(&gpio_lock, flags);
831 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
833 test_bit(FLAG_REQUESTED, &desc->flags),
834 test_bit(FLAG_EXPORT, &desc->flags));
839 if (!desc->chip->direction_input || !desc->chip->direction_output)
840 direction_may_change = false;
841 spin_unlock_irqrestore(&gpio_lock, flags);
843 offset = gpio_chip_hwgpio(desc);
844 if (desc->chip->names && desc->chip->names[offset])
845 ioname = desc->chip->names[offset];
847 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
848 desc, ioname ? ioname : "gpio%u",
851 status = PTR_ERR(dev);
855 status = sysfs_create_group(&dev->kobj, &gpio_attr_group);
857 goto fail_unregister_device;
859 if (direction_may_change) {
860 status = device_create_file(dev, &dev_attr_direction);
862 goto fail_unregister_device;
865 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
866 !test_bit(FLAG_IS_OUT, &desc->flags))) {
867 status = device_create_file(dev, &dev_attr_edge);
869 goto fail_unregister_device;
872 set_bit(FLAG_EXPORT, &desc->flags);
873 mutex_unlock(&sysfs_lock);
876 fail_unregister_device:
877 device_unregister(dev);
879 mutex_unlock(&sysfs_lock);
880 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
883 EXPORT_SYMBOL_GPL(gpiod_export);
885 static int match_export(struct device *dev, const void *data)
887 return dev_get_drvdata(dev) == data;
891 * gpiod_export_link - create a sysfs link to an exported GPIO node
892 * @dev: device under which to create symlink
893 * @name: name of the symlink
894 * @gpio: gpio to create symlink to, already exported
896 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
897 * node. Caller is responsible for unlinking.
899 * Returns zero on success, else an error.
901 int gpiod_export_link(struct device *dev, const char *name,
902 struct gpio_desc *desc)
904 int status = -EINVAL;
907 pr_warn("%s: invalid GPIO\n", __func__);
911 mutex_lock(&sysfs_lock);
913 if (test_bit(FLAG_EXPORT, &desc->flags)) {
916 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
918 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
925 mutex_unlock(&sysfs_lock);
928 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
932 EXPORT_SYMBOL_GPL(gpiod_export_link);
935 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
936 * @gpio: gpio to change
937 * @value: non-zero to use active low, i.e. inverted values
939 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
940 * The GPIO does not have to be exported yet. If poll(2) support has
941 * been enabled for either rising or falling edge, it will be
942 * reconfigured to follow the new polarity.
944 * Returns zero on success, else an error.
946 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
948 struct device *dev = NULL;
949 int status = -EINVAL;
952 pr_warn("%s: invalid GPIO\n", __func__);
956 mutex_lock(&sysfs_lock);
958 if (test_bit(FLAG_EXPORT, &desc->flags)) {
959 dev = class_find_device(&gpio_class, NULL, desc, match_export);
966 status = sysfs_set_active_low(desc, dev, value);
969 mutex_unlock(&sysfs_lock);
972 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
976 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
979 * gpiod_unexport - reverse effect of gpio_export()
980 * @gpio: gpio to make unavailable
982 * This is implicit on gpio_free().
984 void gpiod_unexport(struct gpio_desc *desc)
987 struct device *dev = NULL;
990 pr_warn("%s: invalid GPIO\n", __func__);
994 mutex_lock(&sysfs_lock);
996 if (test_bit(FLAG_EXPORT, &desc->flags)) {
998 dev = class_find_device(&gpio_class, NULL, desc, match_export);
1000 gpio_setup_irq(desc, dev, 0);
1001 clear_bit(FLAG_EXPORT, &desc->flags);
1006 mutex_unlock(&sysfs_lock);
1009 device_unregister(dev);
1014 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1016 EXPORT_SYMBOL_GPL(gpiod_unexport);
1018 static int gpiochip_export(struct gpio_chip *chip)
1023 /* Many systems register gpio chips for SOC support very early,
1024 * before driver model support is available. In those cases we
1025 * export this later, in gpiolib_sysfs_init() ... here we just
1026 * verify that _some_ field of gpio_class got initialized.
1031 /* use chip->base for the ID; it's already known to be unique */
1032 mutex_lock(&sysfs_lock);
1033 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
1034 "gpiochip%d", chip->base);
1036 status = sysfs_create_group(&dev->kobj,
1037 &gpiochip_attr_group);
1039 status = PTR_ERR(dev);
1040 chip->exported = (status == 0);
1041 mutex_unlock(&sysfs_lock);
1044 unsigned long flags;
1047 spin_lock_irqsave(&gpio_lock, flags);
1049 while (gpio < chip->ngpio)
1050 chip->desc[gpio++].chip = NULL;
1051 spin_unlock_irqrestore(&gpio_lock, flags);
1053 chip_dbg(chip, "%s: status %d\n", __func__, status);
1059 static void gpiochip_unexport(struct gpio_chip *chip)
1064 mutex_lock(&sysfs_lock);
1065 dev = class_find_device(&gpio_class, NULL, chip, match_export);
1068 device_unregister(dev);
1069 chip->exported = false;
1073 mutex_unlock(&sysfs_lock);
1076 chip_dbg(chip, "%s: status %d\n", __func__, status);
1079 static int __init gpiolib_sysfs_init(void)
1082 unsigned long flags;
1083 struct gpio_chip *chip;
1085 status = class_register(&gpio_class);
1089 /* Scan and register the gpio_chips which registered very
1090 * early (e.g. before the class_register above was called).
1092 * We run before arch_initcall() so chip->dev nodes can have
1093 * registered, and so arch_initcall() can always gpio_export().
1095 spin_lock_irqsave(&gpio_lock, flags);
1096 list_for_each_entry(chip, &gpio_chips, list) {
1097 if (!chip || chip->exported)
1100 spin_unlock_irqrestore(&gpio_lock, flags);
1101 status = gpiochip_export(chip);
1102 spin_lock_irqsave(&gpio_lock, flags);
1104 spin_unlock_irqrestore(&gpio_lock, flags);
1109 postcore_initcall(gpiolib_sysfs_init);
1112 static inline int gpiochip_export(struct gpio_chip *chip)
1117 static inline void gpiochip_unexport(struct gpio_chip *chip)
1121 #endif /* CONFIG_GPIO_SYSFS */
1124 * Add a new chip to the global chips list, keeping the list of chips sorted
1127 * Return -EBUSY if the new chip overlaps with some other chip's integer
1130 static int gpiochip_add_to_list(struct gpio_chip *chip)
1132 struct list_head *pos = &gpio_chips;
1133 struct gpio_chip *_chip;
1136 /* find where to insert our chip */
1137 list_for_each(pos, &gpio_chips) {
1138 _chip = list_entry(pos, struct gpio_chip, list);
1139 /* shall we insert before _chip? */
1140 if (_chip->base >= chip->base + chip->ngpio)
1144 /* are we stepping on the chip right before? */
1145 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1146 _chip = list_entry(pos->prev, struct gpio_chip, list);
1147 if (_chip->base + _chip->ngpio > chip->base) {
1149 "GPIO integer space overlap, cannot add chip\n");
1155 list_add_tail(&chip->list, pos);
1161 * gpiochip_add() - register a gpio_chip
1162 * @chip: the chip to register, with chip->base initialized
1163 * Context: potentially before irqs or kmalloc will work
1165 * Returns a negative errno if the chip can't be registered, such as
1166 * because the chip->base is invalid or already associated with a
1167 * different chip. Otherwise it returns zero as a success code.
1169 * When gpiochip_add() is called very early during boot, so that GPIOs
1170 * can be freely used, the chip->dev device must be registered before
1171 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1172 * for GPIOs will fail rudely.
1174 * If chip->base is negative, this requests dynamic assignment of
1175 * a range of valid GPIOs.
1177 int gpiochip_add(struct gpio_chip *chip)
1179 unsigned long flags;
1182 int base = chip->base;
1184 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1190 spin_lock_irqsave(&gpio_lock, flags);
1193 base = gpiochip_find_base(chip->ngpio);
1201 status = gpiochip_add_to_list(chip);
1204 chip->desc = &gpio_desc[chip->base];
1206 for (id = 0; id < chip->ngpio; id++) {
1207 struct gpio_desc *desc = &chip->desc[id];
1210 /* REVISIT: most hardware initializes GPIOs as
1211 * inputs (often with pullups enabled) so power
1212 * usage is minimized. Linux code should set the
1213 * gpio direction first thing; but until it does,
1214 * and in case chip->get_direction is not set,
1215 * we may expose the wrong direction in sysfs.
1217 desc->flags = !chip->direction_input
1218 ? (1 << FLAG_IS_OUT)
1223 spin_unlock_irqrestore(&gpio_lock, flags);
1225 #ifdef CONFIG_PINCTRL
1226 INIT_LIST_HEAD(&chip->pin_ranges);
1229 of_gpiochip_add(chip);
1230 acpi_gpiochip_add(chip);
1235 status = gpiochip_export(chip);
1239 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
1240 chip->base, chip->base + chip->ngpio - 1,
1241 chip->label ? : "generic");
1246 spin_unlock_irqrestore(&gpio_lock, flags);
1248 /* failures here can mean systems won't boot... */
1249 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1250 chip->base, chip->base + chip->ngpio - 1,
1251 chip->label ? : "generic");
1254 EXPORT_SYMBOL_GPL(gpiochip_add);
1257 * gpiochip_remove() - unregister a gpio_chip
1258 * @chip: the chip to unregister
1260 * A gpio_chip with any GPIOs still requested may not be removed.
1262 int gpiochip_remove(struct gpio_chip *chip)
1264 unsigned long flags;
1268 spin_lock_irqsave(&gpio_lock, flags);
1270 gpiochip_remove_pin_ranges(chip);
1271 of_gpiochip_remove(chip);
1272 acpi_gpiochip_remove(chip);
1274 for (id = 0; id < chip->ngpio; id++) {
1275 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
1281 for (id = 0; id < chip->ngpio; id++)
1282 chip->desc[id].chip = NULL;
1284 list_del(&chip->list);
1287 spin_unlock_irqrestore(&gpio_lock, flags);
1290 gpiochip_unexport(chip);
1294 EXPORT_SYMBOL_GPL(gpiochip_remove);
1297 * gpiochip_find() - iterator for locating a specific gpio_chip
1298 * @data: data to pass to match function
1299 * @callback: Callback function to check gpio_chip
1301 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1302 * determined by a user supplied @match callback. The callback should return
1303 * 0 if the device doesn't match and non-zero if it does. If the callback is
1304 * non-zero, this function will return to the caller and not iterate over any
1307 struct gpio_chip *gpiochip_find(void *data,
1308 int (*match)(struct gpio_chip *chip,
1311 struct gpio_chip *chip;
1312 unsigned long flags;
1314 spin_lock_irqsave(&gpio_lock, flags);
1315 list_for_each_entry(chip, &gpio_chips, list)
1316 if (match(chip, data))
1320 if (&chip->list == &gpio_chips)
1322 spin_unlock_irqrestore(&gpio_lock, flags);
1326 EXPORT_SYMBOL_GPL(gpiochip_find);
1328 static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1330 const char *name = data;
1332 return !strcmp(chip->label, name);
1335 static struct gpio_chip *find_chip_by_name(const char *name)
1337 return gpiochip_find((void *)name, gpiochip_match_name);
1340 #ifdef CONFIG_PINCTRL
1343 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1344 * @chip: the gpiochip to add the range for
1345 * @pinctrl: the dev_name() of the pin controller to map to
1346 * @gpio_offset: the start offset in the current gpio_chip number space
1347 * @pin_group: name of the pin group inside the pin controller
1349 int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1350 struct pinctrl_dev *pctldev,
1351 unsigned int gpio_offset, const char *pin_group)
1353 struct gpio_pin_range *pin_range;
1356 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1358 chip_err(chip, "failed to allocate pin ranges\n");
1362 /* Use local offset as range ID */
1363 pin_range->range.id = gpio_offset;
1364 pin_range->range.gc = chip;
1365 pin_range->range.name = chip->label;
1366 pin_range->range.base = chip->base + gpio_offset;
1367 pin_range->pctldev = pctldev;
1369 ret = pinctrl_get_group_pins(pctldev, pin_group,
1370 &pin_range->range.pins,
1371 &pin_range->range.npins);
1377 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1379 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1380 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1381 pinctrl_dev_get_devname(pctldev), pin_group);
1383 list_add_tail(&pin_range->node, &chip->pin_ranges);
1387 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1390 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1391 * @chip: the gpiochip to add the range for
1392 * @pinctrl_name: the dev_name() of the pin controller to map to
1393 * @gpio_offset: the start offset in the current gpio_chip number space
1394 * @pin_offset: the start offset in the pin controller number space
1395 * @npins: the number of pins from the offset of each pin space (GPIO and
1396 * pin controller) to accumulate in this range
1398 int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1399 unsigned int gpio_offset, unsigned int pin_offset,
1402 struct gpio_pin_range *pin_range;
1405 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1407 chip_err(chip, "failed to allocate pin ranges\n");
1411 /* Use local offset as range ID */
1412 pin_range->range.id = gpio_offset;
1413 pin_range->range.gc = chip;
1414 pin_range->range.name = chip->label;
1415 pin_range->range.base = chip->base + gpio_offset;
1416 pin_range->range.pin_base = pin_offset;
1417 pin_range->range.npins = npins;
1418 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1420 if (IS_ERR(pin_range->pctldev)) {
1421 ret = PTR_ERR(pin_range->pctldev);
1422 chip_err(chip, "could not create pin range\n");
1426 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1427 gpio_offset, gpio_offset + npins - 1,
1429 pin_offset, pin_offset + npins - 1);
1431 list_add_tail(&pin_range->node, &chip->pin_ranges);
1435 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1438 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1439 * @chip: the chip to remove all the mappings for
1441 void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1443 struct gpio_pin_range *pin_range, *tmp;
1445 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
1446 list_del(&pin_range->node);
1447 pinctrl_remove_gpio_range(pin_range->pctldev,
1452 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1454 #endif /* CONFIG_PINCTRL */
1456 /* These "optional" allocation calls help prevent drivers from stomping
1457 * on each other, and help provide better diagnostics in debugfs.
1458 * They're called even less than the "set direction" calls.
1460 static int gpiod_request(struct gpio_desc *desc, const char *label)
1462 struct gpio_chip *chip;
1463 int status = -EPROBE_DEFER;
1464 unsigned long flags;
1467 pr_warn("%s: invalid GPIO\n", __func__);
1471 spin_lock_irqsave(&gpio_lock, flags);
1477 if (!try_module_get(chip->owner))
1480 /* NOTE: gpio_request() can be called in early boot,
1481 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1484 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1485 desc_set_label(desc, label ? : "?");
1489 module_put(chip->owner);
1493 if (chip->request) {
1494 /* chip->request may sleep */
1495 spin_unlock_irqrestore(&gpio_lock, flags);
1496 status = chip->request(chip, gpio_chip_hwgpio(desc));
1497 spin_lock_irqsave(&gpio_lock, flags);
1500 desc_set_label(desc, NULL);
1501 module_put(chip->owner);
1502 clear_bit(FLAG_REQUESTED, &desc->flags);
1506 if (chip->get_direction) {
1507 /* chip->get_direction may sleep */
1508 spin_unlock_irqrestore(&gpio_lock, flags);
1509 gpiod_get_direction(desc);
1510 spin_lock_irqsave(&gpio_lock, flags);
1514 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1515 spin_unlock_irqrestore(&gpio_lock, flags);
1519 int gpio_request(unsigned gpio, const char *label)
1521 return gpiod_request(gpio_to_desc(gpio), label);
1523 EXPORT_SYMBOL_GPL(gpio_request);
1525 static void gpiod_free(struct gpio_desc *desc)
1527 unsigned long flags;
1528 struct gpio_chip *chip;
1533 WARN_ON(extra_checks);
1537 gpiod_unexport(desc);
1539 spin_lock_irqsave(&gpio_lock, flags);
1542 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1544 spin_unlock_irqrestore(&gpio_lock, flags);
1545 might_sleep_if(chip->can_sleep);
1546 chip->free(chip, gpio_chip_hwgpio(desc));
1547 spin_lock_irqsave(&gpio_lock, flags);
1549 desc_set_label(desc, NULL);
1550 module_put(desc->chip->owner);
1551 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1552 clear_bit(FLAG_REQUESTED, &desc->flags);
1553 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1554 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1556 WARN_ON(extra_checks);
1558 spin_unlock_irqrestore(&gpio_lock, flags);
1561 void gpio_free(unsigned gpio)
1563 gpiod_free(gpio_to_desc(gpio));
1565 EXPORT_SYMBOL_GPL(gpio_free);
1568 * gpio_request_one - request a single GPIO with initial configuration
1569 * @gpio: the GPIO number
1570 * @flags: GPIO configuration as specified by GPIOF_*
1571 * @label: a literal description string of this GPIO
1573 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1575 struct gpio_desc *desc;
1578 desc = gpio_to_desc(gpio);
1580 err = gpiod_request(desc, label);
1584 if (flags & GPIOF_OPEN_DRAIN)
1585 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1587 if (flags & GPIOF_OPEN_SOURCE)
1588 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1590 if (flags & GPIOF_DIR_IN)
1591 err = gpiod_direction_input(desc);
1593 err = gpiod_direction_output_raw(desc,
1594 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1599 if (flags & GPIOF_EXPORT) {
1600 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
1611 EXPORT_SYMBOL_GPL(gpio_request_one);
1614 * gpio_request_array - request multiple GPIOs in a single call
1615 * @array: array of the 'struct gpio'
1616 * @num: how many GPIOs in the array
1618 int gpio_request_array(const struct gpio *array, size_t num)
1622 for (i = 0; i < num; i++, array++) {
1623 err = gpio_request_one(array->gpio, array->flags, array->label);
1631 gpio_free((--array)->gpio);
1634 EXPORT_SYMBOL_GPL(gpio_request_array);
1637 * gpio_free_array - release multiple GPIOs in a single call
1638 * @array: array of the 'struct gpio'
1639 * @num: how many GPIOs in the array
1641 void gpio_free_array(const struct gpio *array, size_t num)
1644 gpio_free((array++)->gpio);
1646 EXPORT_SYMBOL_GPL(gpio_free_array);
1649 * gpiochip_is_requested - return string iff signal was requested
1650 * @chip: controller managing the signal
1651 * @offset: of signal within controller's 0..(ngpio - 1) range
1653 * Returns NULL if the GPIO is not currently requested, else a string.
1654 * If debugfs support is enabled, the string returned is the label passed
1655 * to gpio_request(); otherwise it is a meaningless constant.
1657 * This function is for use by GPIO controller drivers. The label can
1658 * help with diagnostics, and knowing that the signal is used as a GPIO
1659 * can help avoid accidentally multiplexing it to another controller.
1661 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1663 struct gpio_desc *desc;
1665 if (!GPIO_OFFSET_VALID(chip, offset))
1668 desc = &chip->desc[offset];
1670 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1672 #ifdef CONFIG_DEBUG_FS
1678 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1681 /* Drivers MUST set GPIO direction before making get/set calls. In
1682 * some cases this is done in early boot, before IRQs are enabled.
1684 * As a rule these aren't called more than once (except for drivers
1685 * using the open-drain emulation idiom) so these are natural places
1686 * to accumulate extra debugging checks. Note that we can't (yet)
1687 * rely on gpio_request() having been called beforehand.
1691 * gpiod_direction_input - set the GPIO direction to input
1692 * @desc: GPIO to set to input
1694 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1695 * be called safely on it.
1697 * Return 0 in case of success, else an error code.
1699 int gpiod_direction_input(struct gpio_desc *desc)
1701 unsigned long flags;
1702 struct gpio_chip *chip;
1703 int status = -EINVAL;
1706 if (!desc || !desc->chip) {
1707 pr_warn("%s: invalid GPIO\n", __func__);
1712 if (!chip->get || !chip->direction_input) {
1714 "%s: missing get() or direction_input() operations\n",
1719 spin_lock_irqsave(&gpio_lock, flags);
1721 status = gpio_ensure_requested(desc);
1725 /* now we know the gpio is valid and chip won't vanish */
1727 spin_unlock_irqrestore(&gpio_lock, flags);
1729 might_sleep_if(chip->can_sleep);
1731 offset = gpio_chip_hwgpio(desc);
1733 status = chip->request(chip, offset);
1735 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1737 /* and it's not available to anyone else ...
1738 * gpio_request() is the fully clean solution.
1744 status = chip->direction_input(chip, offset);
1746 clear_bit(FLAG_IS_OUT, &desc->flags);
1748 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1752 spin_unlock_irqrestore(&gpio_lock, flags);
1754 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1757 EXPORT_SYMBOL_GPL(gpiod_direction_input);
1759 static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1761 unsigned long flags;
1762 struct gpio_chip *chip;
1763 int status = -EINVAL;
1766 /* GPIOs used for IRQs shall not be set as output */
1767 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1769 "%s: tried to set a GPIO tied to an IRQ as output\n",
1774 /* Open drain pin should not be driven to 1 */
1775 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1776 return gpiod_direction_input(desc);
1778 /* Open source pin should not be driven to 0 */
1779 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1780 return gpiod_direction_input(desc);
1783 if (!chip->set || !chip->direction_output) {
1785 "%s: missing set() or direction_output() operations\n",
1790 spin_lock_irqsave(&gpio_lock, flags);
1792 status = gpio_ensure_requested(desc);
1796 /* now we know the gpio is valid and chip won't vanish */
1798 spin_unlock_irqrestore(&gpio_lock, flags);
1800 might_sleep_if(chip->can_sleep);
1802 offset = gpio_chip_hwgpio(desc);
1804 status = chip->request(chip, offset);
1806 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1808 /* and it's not available to anyone else ...
1809 * gpio_request() is the fully clean solution.
1815 status = chip->direction_output(chip, offset, value);
1817 set_bit(FLAG_IS_OUT, &desc->flags);
1818 trace_gpio_value(desc_to_gpio(desc), 0, value);
1819 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1823 spin_unlock_irqrestore(&gpio_lock, flags);
1825 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
1830 * gpiod_direction_output_raw - set the GPIO direction to output
1831 * @desc: GPIO to set to output
1832 * @value: initial output value of the GPIO
1834 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1835 * be called safely on it. The initial value of the output must be specified
1836 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1838 * Return 0 in case of success, else an error code.
1840 int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1842 if (!desc || !desc->chip) {
1843 pr_warn("%s: invalid GPIO\n", __func__);
1846 return _gpiod_direction_output_raw(desc, value);
1848 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1851 * gpiod_direction_output - set the GPIO direction to output
1852 * @desc: GPIO to set to output
1853 * @value: initial output value of the GPIO
1855 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1856 * be called safely on it. The initial value of the output must be specified
1857 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1860 * Return 0 in case of success, else an error code.
1862 int gpiod_direction_output(struct gpio_desc *desc, int value)
1864 if (!desc || !desc->chip) {
1865 pr_warn("%s: invalid GPIO\n", __func__);
1868 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1870 return _gpiod_direction_output_raw(desc, value);
1872 EXPORT_SYMBOL_GPL(gpiod_direction_output);
1875 * gpiod_set_debounce - sets @debounce time for a @gpio
1876 * @gpio: the gpio to set debounce time
1877 * @debounce: debounce time is microseconds
1879 * returns -ENOTSUPP if the controller does not support setting
1882 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1884 unsigned long flags;
1885 struct gpio_chip *chip;
1886 int status = -EINVAL;
1889 if (!desc || !desc->chip) {
1890 pr_warn("%s: invalid GPIO\n", __func__);
1895 if (!chip->set || !chip->set_debounce) {
1897 "%s: missing set() or set_debounce() operations\n",
1902 spin_lock_irqsave(&gpio_lock, flags);
1904 status = gpio_ensure_requested(desc);
1908 /* now we know the gpio is valid and chip won't vanish */
1910 spin_unlock_irqrestore(&gpio_lock, flags);
1912 might_sleep_if(chip->can_sleep);
1914 offset = gpio_chip_hwgpio(desc);
1915 return chip->set_debounce(chip, offset, debounce);
1918 spin_unlock_irqrestore(&gpio_lock, flags);
1920 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1924 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1927 * gpiod_is_active_low - test whether a GPIO is active-low or not
1928 * @desc: the gpio descriptor to test
1930 * Returns 1 if the GPIO is active-low, 0 otherwise.
1932 int gpiod_is_active_low(const struct gpio_desc *desc)
1934 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1936 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1938 /* I/O calls are only valid after configuration completed; the relevant
1939 * "is this a valid GPIO" error checks should already have been done.
1941 * "Get" operations are often inlinable as reading a pin value register,
1942 * and masking the relevant bit in that register.
1944 * When "set" operations are inlinable, they involve writing that mask to
1945 * one register to set a low value, or a different register to set it high.
1946 * Otherwise locking is needed, so there may be little value to inlining.
1948 *------------------------------------------------------------------------
1950 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1951 * have requested the GPIO. That can include implicit requesting by
1952 * a direction setting call. Marking a gpio as requested locks its chip
1953 * in memory, guaranteeing that these table lookups need no more locking
1954 * and that gpiochip_remove() will fail.
1956 * REVISIT when debugging, consider adding some instrumentation to ensure
1957 * that the GPIO was actually requested.
1960 static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1962 struct gpio_chip *chip;
1967 offset = gpio_chip_hwgpio(desc);
1968 value = chip->get ? chip->get(chip, offset) : 0;
1969 trace_gpio_value(desc_to_gpio(desc), 1, value);
1974 * gpiod_get_raw_value() - return a gpio's raw value
1975 * @desc: gpio whose value will be returned
1977 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1978 * its ACTIVE_LOW status.
1980 * This function should be called from contexts where we cannot sleep, and will
1981 * complain if the GPIO chip functions potentially sleep.
1983 int gpiod_get_raw_value(const struct gpio_desc *desc)
1987 /* Should be using gpio_get_value_cansleep() */
1988 WARN_ON(desc->chip->can_sleep);
1989 return _gpiod_get_raw_value(desc);
1991 EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1994 * gpiod_get_value() - return a gpio's value
1995 * @desc: gpio whose value will be returned
1997 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2000 * This function should be called from contexts where we cannot sleep, and will
2001 * complain if the GPIO chip functions potentially sleep.
2003 int gpiod_get_value(const struct gpio_desc *desc)
2008 /* Should be using gpio_get_value_cansleep() */
2009 WARN_ON(desc->chip->can_sleep);
2011 value = _gpiod_get_raw_value(desc);
2012 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2017 EXPORT_SYMBOL_GPL(gpiod_get_value);
2020 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
2021 * @desc: gpio descriptor whose state need to be set.
2022 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2024 static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
2027 struct gpio_chip *chip = desc->chip;
2028 int offset = gpio_chip_hwgpio(desc);
2031 err = chip->direction_input(chip, offset);
2033 clear_bit(FLAG_IS_OUT, &desc->flags);
2035 err = chip->direction_output(chip, offset, 0);
2037 set_bit(FLAG_IS_OUT, &desc->flags);
2039 trace_gpio_direction(desc_to_gpio(desc), value, err);
2042 "%s: Error in set_value for open drain err %d\n",
2047 * _gpio_set_open_source_value() - Set the open source gpio's value.
2048 * @desc: gpio descriptor whose state need to be set.
2049 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2051 static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
2054 struct gpio_chip *chip = desc->chip;
2055 int offset = gpio_chip_hwgpio(desc);
2058 err = chip->direction_output(chip, offset, 1);
2060 set_bit(FLAG_IS_OUT, &desc->flags);
2062 err = chip->direction_input(chip, offset);
2064 clear_bit(FLAG_IS_OUT, &desc->flags);
2066 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2069 "%s: Error in set_value for open source err %d\n",
2073 static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
2075 struct gpio_chip *chip;
2078 trace_gpio_value(desc_to_gpio(desc), 0, value);
2079 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2080 _gpio_set_open_drain_value(desc, value);
2081 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2082 _gpio_set_open_source_value(desc, value);
2084 chip->set(chip, gpio_chip_hwgpio(desc), value);
2088 * gpiod_set_raw_value() - assign a gpio's raw value
2089 * @desc: gpio whose value will be assigned
2090 * @value: value to assign
2092 * Set the raw value of the GPIO, i.e. the value of its physical line without
2093 * regard for its ACTIVE_LOW status.
2095 * This function should be called from contexts where we cannot sleep, and will
2096 * complain if the GPIO chip functions potentially sleep.
2098 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2102 /* Should be using gpio_set_value_cansleep() */
2103 WARN_ON(desc->chip->can_sleep);
2104 _gpiod_set_raw_value(desc, value);
2106 EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2109 * gpiod_set_value() - assign a gpio's value
2110 * @desc: gpio whose value will be assigned
2111 * @value: value to assign
2113 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2116 * This function should be called from contexts where we cannot sleep, and will
2117 * complain if the GPIO chip functions potentially sleep.
2119 void gpiod_set_value(struct gpio_desc *desc, int value)
2123 /* Should be using gpio_set_value_cansleep() */
2124 WARN_ON(desc->chip->can_sleep);
2125 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2127 _gpiod_set_raw_value(desc, value);
2129 EXPORT_SYMBOL_GPL(gpiod_set_value);
2132 * gpiod_cansleep() - report whether gpio value access may sleep
2133 * @desc: gpio to check
2136 int gpiod_cansleep(const struct gpio_desc *desc)
2140 return desc->chip->can_sleep;
2142 EXPORT_SYMBOL_GPL(gpiod_cansleep);
2145 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2146 * @desc: gpio whose IRQ will be returned (already requested)
2148 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2151 int gpiod_to_irq(const struct gpio_desc *desc)
2153 struct gpio_chip *chip;
2159 offset = gpio_chip_hwgpio(desc);
2160 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2162 EXPORT_SYMBOL_GPL(gpiod_to_irq);
2165 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2166 * @gpio: the GPIO line to lock as used for IRQ
2168 * This is used directly by GPIO drivers that want to lock down
2169 * a certain GPIO line to be used as IRQs, for example in the
2170 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2171 * of its irq_chip implementation if the GPIO is known from that
2174 int gpiod_lock_as_irq(struct gpio_desc *desc)
2179 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2181 "%s: tried to flag a GPIO set as output for IRQ\n",
2186 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2189 EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
2191 int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2193 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2195 EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2198 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2199 * @gpio: the GPIO line to unlock from IRQ usage
2201 * This is used directly by GPIO drivers that want to indicate
2202 * that a certain GPIO is no longer used exclusively for IRQ.
2204 void gpiod_unlock_as_irq(struct gpio_desc *desc)
2209 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2211 EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
2213 void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2215 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2217 EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
2220 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2221 * @desc: gpio whose value will be returned
2223 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2224 * its ACTIVE_LOW status.
2226 * This function is to be called from contexts that can sleep.
2228 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2230 might_sleep_if(extra_checks);
2233 return _gpiod_get_raw_value(desc);
2235 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2238 * gpiod_get_value_cansleep() - return a gpio's value
2239 * @desc: gpio whose value will be returned
2241 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2244 * This function is to be called from contexts that can sleep.
2246 int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2250 might_sleep_if(extra_checks);
2254 value = _gpiod_get_raw_value(desc);
2255 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2260 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2263 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2264 * @desc: gpio whose value will be assigned
2265 * @value: value to assign
2267 * Set the raw value of the GPIO, i.e. the value of its physical line without
2268 * regard for its ACTIVE_LOW status.
2270 * This function is to be called from contexts that can sleep.
2272 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2274 might_sleep_if(extra_checks);
2277 _gpiod_set_raw_value(desc, value);
2279 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2282 * gpiod_set_value_cansleep() - assign a gpio's value
2283 * @desc: gpio whose value will be assigned
2284 * @value: value to assign
2286 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2289 * This function is to be called from contexts that can sleep.
2291 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2293 might_sleep_if(extra_checks);
2297 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2299 _gpiod_set_raw_value(desc, value);
2301 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2304 * gpiod_add_lookup_table() - register GPIO device consumers
2305 * @table: table of consumers to register
2307 void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
2309 mutex_lock(&gpio_lookup_lock);
2311 list_add_tail(&table->list, &gpio_lookup_list);
2313 mutex_unlock(&gpio_lookup_lock);
2317 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2319 enum gpio_lookup_flags *flags)
2321 char prop_name[32]; /* 32 is max size of property name */
2322 enum of_gpio_flags of_flags;
2323 struct gpio_desc *desc;
2326 snprintf(prop_name, 32, "%s-gpios", con_id);
2328 snprintf(prop_name, 32, "gpios");
2330 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2336 if (of_flags & OF_GPIO_ACTIVE_LOW)
2337 *flags |= GPIO_ACTIVE_LOW;
2342 static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2344 enum gpio_lookup_flags *flags)
2346 return ERR_PTR(-ENODEV);
2350 static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2352 enum gpio_lookup_flags *flags)
2354 struct acpi_gpio_info info;
2355 struct gpio_desc *desc;
2357 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2361 if (info.gpioint && info.active_low)
2362 *flags |= GPIO_ACTIVE_LOW;
2367 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2369 const char *dev_id = dev ? dev_name(dev) : NULL;
2370 struct gpiod_lookup_table *table;
2372 mutex_lock(&gpio_lookup_lock);
2374 list_for_each_entry(table, &gpio_lookup_list, list) {
2375 if (table->dev_id && dev_id) {
2377 * Valid strings on both ends, must be identical to have
2380 if (!strcmp(table->dev_id, dev_id))
2384 * One of the pointers is NULL, so both must be to have
2387 if (dev_id == table->dev_id)
2394 mutex_unlock(&gpio_lookup_lock);
2398 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2400 enum gpio_lookup_flags *flags)
2402 struct gpio_desc *desc = ERR_PTR(-ENOENT);
2403 struct gpiod_lookup_table *table;
2404 struct gpiod_lookup *p;
2406 table = gpiod_find_lookup_table(dev);
2410 for (p = &table->table[0]; p->chip_label; p++) {
2411 struct gpio_chip *chip;
2413 /* idx must always match exactly */
2417 /* If the lookup entry has a con_id, require exact match */
2418 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2421 chip = find_chip_by_name(p->chip_label);
2424 dev_err(dev, "cannot find GPIO chip %s\n",
2426 return ERR_PTR(-ENODEV);
2429 if (chip->ngpio <= p->chip_hwnum) {
2431 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2432 idx, chip->ngpio, chip->label);
2433 return ERR_PTR(-EINVAL);
2436 desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2446 * gpio_get - obtain a GPIO for a given GPIO function
2447 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2448 * @con_id: function within the GPIO consumer
2450 * Return the GPIO descriptor corresponding to the function con_id of device
2451 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2452 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
2454 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2456 return gpiod_get_index(dev, con_id, 0);
2458 EXPORT_SYMBOL_GPL(gpiod_get);
2461 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2462 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2463 * @con_id: function within the GPIO consumer
2464 * @idx: index of the GPIO to obtain in the consumer
2466 * This variant of gpiod_get() allows to access GPIOs other than the first
2467 * defined one for functions that define several GPIOs.
2469 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2470 * requested function and/or index, or another IS_ERR() code if an error
2471 * occured while trying to acquire the GPIO.
2473 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2477 struct gpio_desc *desc = NULL;
2479 enum gpio_lookup_flags flags = 0;
2481 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2483 /* Using device tree? */
2484 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2485 dev_dbg(dev, "using device tree for GPIO lookup\n");
2486 desc = of_find_gpio(dev, con_id, idx, &flags);
2487 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2488 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2489 desc = acpi_find_gpio(dev, con_id, idx, &flags);
2493 * Either we are not using DT or ACPI, or their lookup did not return
2494 * a result. In that case, use platform lookup as a fallback.
2496 if (!desc || desc == ERR_PTR(-ENOENT)) {
2497 dev_dbg(dev, "using lookup tables for GPIO lookup");
2498 desc = gpiod_find(dev, con_id, idx, &flags);
2502 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
2506 status = gpiod_request(desc, con_id);
2509 return ERR_PTR(status);
2511 if (flags & GPIO_ACTIVE_LOW)
2512 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2513 if (flags & GPIO_OPEN_DRAIN)
2514 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2515 if (flags & GPIO_OPEN_SOURCE)
2516 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2520 EXPORT_SYMBOL_GPL(gpiod_get_index);
2523 * gpiod_put - dispose of a GPIO descriptor
2524 * @desc: GPIO descriptor to dispose of
2526 * No descriptor can be used after gpiod_put() has been called on it.
2528 void gpiod_put(struct gpio_desc *desc)
2532 EXPORT_SYMBOL_GPL(gpiod_put);
2534 #ifdef CONFIG_DEBUG_FS
2536 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2539 unsigned gpio = chip->base;
2540 struct gpio_desc *gdesc = &chip->desc[0];
2544 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2545 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
2548 gpiod_get_direction(gdesc);
2549 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2550 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2551 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
2553 is_out ? "out" : "in ",
2555 ? (chip->get(chip, i) ? "hi" : "lo")
2557 is_irq ? "IRQ" : " ");
2558 seq_printf(s, "\n");
2562 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
2564 unsigned long flags;
2565 struct gpio_chip *chip = NULL;
2566 loff_t index = *pos;
2570 spin_lock_irqsave(&gpio_lock, flags);
2571 list_for_each_entry(chip, &gpio_chips, list)
2573 spin_unlock_irqrestore(&gpio_lock, flags);
2576 spin_unlock_irqrestore(&gpio_lock, flags);
2581 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2583 unsigned long flags;
2584 struct gpio_chip *chip = v;
2587 spin_lock_irqsave(&gpio_lock, flags);
2588 if (list_is_last(&chip->list, &gpio_chips))
2591 ret = list_entry(chip->list.next, struct gpio_chip, list);
2592 spin_unlock_irqrestore(&gpio_lock, flags);
2600 static void gpiolib_seq_stop(struct seq_file *s, void *v)
2604 static int gpiolib_seq_show(struct seq_file *s, void *v)
2606 struct gpio_chip *chip = v;
2609 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
2610 chip->base, chip->base + chip->ngpio - 1);
2613 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
2616 seq_printf(s, ", %s", chip->label);
2617 if (chip->can_sleep)
2618 seq_printf(s, ", can sleep");
2619 seq_printf(s, ":\n");
2622 chip->dbg_show(s, chip);
2624 gpiolib_dbg_show(s, chip);
2629 static const struct seq_operations gpiolib_seq_ops = {
2630 .start = gpiolib_seq_start,
2631 .next = gpiolib_seq_next,
2632 .stop = gpiolib_seq_stop,
2633 .show = gpiolib_seq_show,
2636 static int gpiolib_open(struct inode *inode, struct file *file)
2638 return seq_open(file, &gpiolib_seq_ops);
2641 static const struct file_operations gpiolib_operations = {
2642 .owner = THIS_MODULE,
2643 .open = gpiolib_open,
2645 .llseek = seq_lseek,
2646 .release = seq_release,
2649 static int __init gpiolib_debugfs_init(void)
2651 /* /sys/kernel/debug/gpio */
2652 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2653 NULL, NULL, &gpiolib_operations);
2656 subsys_initcall(gpiolib_debugfs_init);
2658 #endif /* DEBUG_FS */