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/acpi_gpio.h>
14 #include <linux/idr.h>
15 #include <linux/slab.h>
16 #include <linux/acpi.h>
17 #include <linux/gpio/driver.h>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/gpio.h>
22 /* Implementation infrastructure for GPIO interfaces.
24 * The GPIO programming interface allows for inlining speed-critical
25 * get/set operations for common cases, so that access to SOC-integrated
26 * GPIOs can sometimes cost only an instruction or two per bit.
30 /* When debugging, extend minimal trust to callers and platform code.
31 * Also emit diagnostic messages that may help initial bringup, when
32 * board setup or driver bugs are most common.
34 * Otherwise, minimize overhead in what may be bitbanging codepaths.
37 #define extra_checks 1
39 #define extra_checks 0
42 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
43 * While any GPIO is requested, its gpio_chip is not removable;
44 * each GPIO's "requested" flag serves as a lock and refcount.
46 static DEFINE_SPINLOCK(gpio_lock
);
49 struct gpio_chip
*chip
;
51 /* flag symbols are bit numbers */
52 #define FLAG_REQUESTED 0
54 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
55 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
56 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
57 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
58 #define FLAG_ACTIVE_LOW 6 /* value has active low */
59 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
60 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
61 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
63 #define ID_SHIFT 16 /* add new flags before this one */
65 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
66 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
68 #ifdef CONFIG_DEBUG_FS
72 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
74 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76 static DEFINE_MUTEX(gpio_lookup_lock
);
77 static LIST_HEAD(gpio_lookup_list
);
78 static LIST_HEAD(gpio_chips
);
80 #ifdef CONFIG_GPIO_SYSFS
81 static DEFINE_IDR(dirent_idr
);
84 static int gpiod_request(struct gpio_desc
*desc
, const char *label
);
85 static void gpiod_free(struct gpio_desc
*desc
);
87 #ifdef CONFIG_DEBUG_FS
88 #define gpiod_emerg(desc, fmt, ...) \
89 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
91 #define gpiod_crit(desc, fmt, ...) \
92 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
94 #define gpiod_err(desc, fmt, ...) \
95 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
97 #define gpiod_warn(desc, fmt, ...) \
98 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
100 #define gpiod_info(desc, fmt, ...) \
101 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
103 #define gpiod_dbg(desc, fmt, ...) \
104 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
107 #define gpiod_emerg(desc, fmt, ...) \
108 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109 #define gpiod_crit(desc, fmt, ...) \
110 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111 #define gpiod_err(desc, fmt, ...) \
112 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113 #define gpiod_warn(desc, fmt, ...) \
114 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115 #define gpiod_info(desc, fmt, ...) \
116 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
117 #define gpiod_dbg(desc, fmt, ...) \
118 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
121 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
123 #ifdef CONFIG_DEBUG_FS
129 * Return the GPIO number of the passed descriptor relative to its chip
131 static int gpio_chip_hwgpio(const struct gpio_desc
*desc
)
133 return desc
- &desc
->chip
->desc
[0];
137 * Convert a GPIO number to its descriptor
139 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
141 if (WARN(!gpio_is_valid(gpio
), "invalid GPIO %d\n", gpio
))
144 return &gpio_desc
[gpio
];
146 EXPORT_SYMBOL_GPL(gpio_to_desc
);
149 * Convert an offset on a certain chip to a corresponding descriptor
151 static struct gpio_desc
*gpiochip_offset_to_desc(struct gpio_chip
*chip
,
154 if (offset
>= chip
->ngpio
)
155 return ERR_PTR(-EINVAL
);
157 return &chip
->desc
[offset
];
161 * Convert a GPIO descriptor to the integer namespace.
162 * This should disappear in the future but is needed since we still
163 * use GPIO numbers for error messages and sysfs nodes
165 int desc_to_gpio(const struct gpio_desc
*desc
)
167 return desc
- &gpio_desc
[0];
169 EXPORT_SYMBOL_GPL(desc_to_gpio
);
172 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
173 * when setting direction, and otherwise illegal. Until board setup code
174 * and drivers use explicit requests everywhere (which won't happen when
175 * those calls have no teeth) we can't avoid autorequesting. This nag
176 * message should motivate switching to explicit requests... so should
177 * the weaker cleanup after faults, compared to gpio_request().
179 * NOTE: the autorequest mechanism is going away; at this point it's
180 * only "legal" in the sense that (old) code using it won't break yet,
181 * but instead only triggers a WARN() stack dump.
183 static int gpio_ensure_requested(struct gpio_desc
*desc
)
185 const struct gpio_chip
*chip
= desc
->chip
;
186 const int gpio
= desc_to_gpio(desc
);
188 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
189 "autorequest GPIO-%d\n", gpio
)) {
190 if (!try_module_get(chip
->owner
)) {
191 gpiod_err(desc
, "%s: module can't be gotten\n",
193 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
197 desc_set_label(desc
, "[auto]");
198 /* caller must chip->request() w/o spinlock */
206 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
207 * @desc: descriptor to return the chip of
209 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
211 return desc
? desc
->chip
: NULL
;
213 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
215 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
216 static int gpiochip_find_base(int ngpio
)
218 struct gpio_chip
*chip
;
219 int base
= ARCH_NR_GPIOS
- ngpio
;
221 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
222 /* found a free space? */
223 if (chip
->base
+ chip
->ngpio
<= base
)
226 /* nope, check the space right before the chip */
227 base
= chip
->base
- ngpio
;
230 if (gpio_is_valid(base
)) {
231 pr_debug("%s: found new base at %d\n", __func__
, base
);
234 pr_err("%s: cannot find free range\n", __func__
);
240 * gpiod_get_direction - return the current direction of a GPIO
241 * @desc: GPIO to get the direction of
243 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
245 * This function may sleep if gpiod_cansleep() is true.
247 int gpiod_get_direction(const struct gpio_desc
*desc
)
249 struct gpio_chip
*chip
;
251 int status
= -EINVAL
;
253 chip
= gpiod_to_chip(desc
);
254 offset
= gpio_chip_hwgpio(desc
);
256 if (!chip
->get_direction
)
259 status
= chip
->get_direction(chip
, offset
);
261 /* GPIOF_DIR_IN, or other positive */
263 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
264 * so it does not affect constness per se */
265 clear_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
269 set_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
273 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
275 #ifdef CONFIG_GPIO_SYSFS
277 /* lock protects against unexport_gpio() being called while
278 * sysfs files are active.
280 static DEFINE_MUTEX(sysfs_lock
);
283 * /sys/class/gpio/gpioN... only for GPIOs that are exported
285 * * MAY BE OMITTED if kernel won't allow direction changes
286 * * is read/write as "in" or "out"
287 * * may also be written as "high" or "low", initializing
288 * output value as specified ("out" implies "low")
290 * * always readable, subject to hardware behavior
291 * * may be writable, as zero/nonzero
293 * * configures behavior of poll(2) on /value
294 * * available only if pin can generate IRQs on input
295 * * is read/write as "none", "falling", "rising", or "both"
297 * * configures polarity of /value
298 * * is read/write as zero/nonzero
299 * * also affects existing and subsequent "falling" and "rising"
300 * /edge configuration
303 static ssize_t
gpio_direction_show(struct device
*dev
,
304 struct device_attribute
*attr
, char *buf
)
306 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
309 mutex_lock(&sysfs_lock
);
311 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
314 gpiod_get_direction(desc
);
315 status
= sprintf(buf
, "%s\n",
316 test_bit(FLAG_IS_OUT
, &desc
->flags
)
320 mutex_unlock(&sysfs_lock
);
324 static ssize_t
gpio_direction_store(struct device
*dev
,
325 struct device_attribute
*attr
, const char *buf
, size_t size
)
327 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
330 mutex_lock(&sysfs_lock
);
332 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
334 else if (sysfs_streq(buf
, "high"))
335 status
= gpiod_direction_output(desc
, 1);
336 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
337 status
= gpiod_direction_output(desc
, 0);
338 else if (sysfs_streq(buf
, "in"))
339 status
= gpiod_direction_input(desc
);
343 mutex_unlock(&sysfs_lock
);
344 return status
? : size
;
347 static /* const */ DEVICE_ATTR(direction
, 0644,
348 gpio_direction_show
, gpio_direction_store
);
350 static ssize_t
gpio_value_show(struct device
*dev
,
351 struct device_attribute
*attr
, char *buf
)
353 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
356 mutex_lock(&sysfs_lock
);
358 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
361 status
= sprintf(buf
, "%d\n", gpiod_get_value_cansleep(desc
));
363 mutex_unlock(&sysfs_lock
);
367 static ssize_t
gpio_value_store(struct device
*dev
,
368 struct device_attribute
*attr
, const char *buf
, size_t size
)
370 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
373 mutex_lock(&sysfs_lock
);
375 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
377 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
382 status
= kstrtol(buf
, 0, &value
);
384 gpiod_set_value_cansleep(desc
, value
);
389 mutex_unlock(&sysfs_lock
);
393 static const DEVICE_ATTR(value
, 0644,
394 gpio_value_show
, gpio_value_store
);
396 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
398 struct sysfs_dirent
*value_sd
= priv
;
400 sysfs_notify_dirent(value_sd
);
404 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
405 unsigned long gpio_flags
)
407 struct sysfs_dirent
*value_sd
;
408 unsigned long irq_flags
;
411 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
414 irq
= gpiod_to_irq(desc
);
418 id
= desc
->flags
>> ID_SHIFT
;
419 value_sd
= idr_find(&dirent_idr
, id
);
421 free_irq(irq
, value_sd
);
423 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
426 gpiod_unlock_as_irq(desc
);
431 irq_flags
= IRQF_SHARED
;
432 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
433 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
434 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
435 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
436 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
437 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
440 value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, "value");
446 ret
= idr_alloc(&dirent_idr
, value_sd
, 1, 0, GFP_KERNEL
);
451 desc
->flags
&= GPIO_FLAGS_MASK
;
452 desc
->flags
|= (unsigned long)id
<< ID_SHIFT
;
454 if (desc
->flags
>> ID_SHIFT
!= id
) {
460 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
461 "gpiolib", value_sd
);
465 ret
= gpiod_lock_as_irq(desc
);
467 gpiod_warn(desc
, "failed to flag the GPIO for IRQ\n");
471 desc
->flags
|= gpio_flags
;
475 idr_remove(&dirent_idr
, id
);
476 desc
->flags
&= GPIO_FLAGS_MASK
;
484 static const struct {
487 } trigger_types
[] = {
489 { "falling", BIT(FLAG_TRIG_FALL
) },
490 { "rising", BIT(FLAG_TRIG_RISE
) },
491 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
494 static ssize_t
gpio_edge_show(struct device
*dev
,
495 struct device_attribute
*attr
, char *buf
)
497 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
500 mutex_lock(&sysfs_lock
);
502 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
508 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
509 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
510 == trigger_types
[i
].flags
) {
511 status
= sprintf(buf
, "%s\n",
512 trigger_types
[i
].name
);
517 mutex_unlock(&sysfs_lock
);
521 static ssize_t
gpio_edge_store(struct device
*dev
,
522 struct device_attribute
*attr
, const char *buf
, size_t size
)
524 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
528 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
529 if (sysfs_streq(trigger_types
[i
].name
, buf
))
534 mutex_lock(&sysfs_lock
);
536 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
539 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
544 mutex_unlock(&sysfs_lock
);
549 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
551 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
556 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
560 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
562 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
564 /* reconfigure poll(2) support if enabled on one edge only */
565 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
566 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
567 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
569 gpio_setup_irq(desc
, dev
, 0);
570 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
576 static ssize_t
gpio_active_low_show(struct device
*dev
,
577 struct device_attribute
*attr
, char *buf
)
579 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
582 mutex_lock(&sysfs_lock
);
584 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
587 status
= sprintf(buf
, "%d\n",
588 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
590 mutex_unlock(&sysfs_lock
);
595 static ssize_t
gpio_active_low_store(struct device
*dev
,
596 struct device_attribute
*attr
, const char *buf
, size_t size
)
598 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
601 mutex_lock(&sysfs_lock
);
603 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
608 status
= kstrtol(buf
, 0, &value
);
610 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
613 mutex_unlock(&sysfs_lock
);
615 return status
? : size
;
618 static const DEVICE_ATTR(active_low
, 0644,
619 gpio_active_low_show
, gpio_active_low_store
);
621 static const struct attribute
*gpio_attrs
[] = {
622 &dev_attr_value
.attr
,
623 &dev_attr_active_low
.attr
,
627 static const struct attribute_group gpio_attr_group
= {
628 .attrs
= (struct attribute
**) gpio_attrs
,
632 * /sys/class/gpio/gpiochipN/
633 * /base ... matching gpio_chip.base (N)
634 * /label ... matching gpio_chip.label
635 * /ngpio ... matching gpio_chip.ngpio
638 static ssize_t
chip_base_show(struct device
*dev
,
639 struct device_attribute
*attr
, char *buf
)
641 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
643 return sprintf(buf
, "%d\n", chip
->base
);
645 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
647 static ssize_t
chip_label_show(struct device
*dev
,
648 struct device_attribute
*attr
, char *buf
)
650 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
652 return sprintf(buf
, "%s\n", chip
->label
? : "");
654 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
656 static ssize_t
chip_ngpio_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
, "%u\n", chip
->ngpio
);
663 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
665 static const struct attribute
*gpiochip_attrs
[] = {
667 &dev_attr_label
.attr
,
668 &dev_attr_ngpio
.attr
,
672 static const struct attribute_group gpiochip_attr_group
= {
673 .attrs
= (struct attribute
**) gpiochip_attrs
,
677 * /sys/class/gpio/export ... write-only
678 * integer N ... number of GPIO to export (full access)
679 * /sys/class/gpio/unexport ... write-only
680 * integer N ... number of GPIO to unexport
682 static ssize_t
export_store(struct class *class,
683 struct class_attribute
*attr
,
684 const char *buf
, size_t len
)
687 struct gpio_desc
*desc
;
690 status
= kstrtol(buf
, 0, &gpio
);
694 desc
= gpio_to_desc(gpio
);
695 /* reject invalid GPIOs */
697 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
701 /* No extra locking here; FLAG_SYSFS just signifies that the
702 * request and export were done by on behalf of userspace, so
703 * they may be undone on its behalf too.
706 status
= gpiod_request(desc
, "sysfs");
708 if (status
== -EPROBE_DEFER
)
712 status
= gpiod_export(desc
, true);
716 set_bit(FLAG_SYSFS
, &desc
->flags
);
720 pr_debug("%s: status %d\n", __func__
, status
);
721 return status
? : len
;
724 static ssize_t
unexport_store(struct class *class,
725 struct class_attribute
*attr
,
726 const char *buf
, size_t len
)
729 struct gpio_desc
*desc
;
732 status
= kstrtol(buf
, 0, &gpio
);
736 desc
= gpio_to_desc(gpio
);
737 /* reject bogus commands (gpio_unexport ignores them) */
739 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
745 /* No extra locking here; FLAG_SYSFS just signifies that the
746 * request and export were done by on behalf of userspace, so
747 * they may be undone on its behalf too.
749 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
)) {
755 pr_debug("%s: status %d\n", __func__
, status
);
756 return status
? : len
;
759 static struct class_attribute gpio_class_attrs
[] = {
760 __ATTR(export
, 0200, NULL
, export_store
),
761 __ATTR(unexport
, 0200, NULL
, unexport_store
),
765 static struct class gpio_class
= {
767 .owner
= THIS_MODULE
,
769 .class_attrs
= gpio_class_attrs
,
774 * gpiod_export - export a GPIO through sysfs
775 * @gpio: gpio to make available, already requested
776 * @direction_may_change: true if userspace may change gpio direction
777 * Context: arch_initcall or later
779 * When drivers want to make a GPIO accessible to userspace after they
780 * have requested it -- perhaps while debugging, or as part of their
781 * public interface -- they may use this routine. If the GPIO can
782 * change direction (some can't) and the caller allows it, userspace
783 * will see "direction" sysfs attribute which may be used to change
784 * the gpio's direction. A "value" attribute will always be provided.
786 * Returns zero on success, else an error.
788 int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
)
792 const char *ioname
= NULL
;
796 /* can't export until sysfs is available ... */
798 pr_debug("%s: called too early!\n", __func__
);
803 pr_debug("%s: invalid gpio descriptor\n", __func__
);
807 mutex_lock(&sysfs_lock
);
809 spin_lock_irqsave(&gpio_lock
, flags
);
810 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
811 test_bit(FLAG_EXPORT
, &desc
->flags
)) {
812 spin_unlock_irqrestore(&gpio_lock
, flags
);
813 gpiod_dbg(desc
, "%s: unavailable (requested=%d, exported=%d)\n",
815 test_bit(FLAG_REQUESTED
, &desc
->flags
),
816 test_bit(FLAG_EXPORT
, &desc
->flags
));
821 if (!desc
->chip
->direction_input
|| !desc
->chip
->direction_output
)
822 direction_may_change
= false;
823 spin_unlock_irqrestore(&gpio_lock
, flags
);
825 offset
= gpio_chip_hwgpio(desc
);
826 if (desc
->chip
->names
&& desc
->chip
->names
[offset
])
827 ioname
= desc
->chip
->names
[offset
];
829 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
830 desc
, ioname
? ioname
: "gpio%u",
833 status
= PTR_ERR(dev
);
837 status
= sysfs_create_group(&dev
->kobj
, &gpio_attr_group
);
839 goto fail_unregister_device
;
841 if (direction_may_change
) {
842 status
= device_create_file(dev
, &dev_attr_direction
);
844 goto fail_unregister_device
;
847 if (gpiod_to_irq(desc
) >= 0 && (direction_may_change
||
848 !test_bit(FLAG_IS_OUT
, &desc
->flags
))) {
849 status
= device_create_file(dev
, &dev_attr_edge
);
851 goto fail_unregister_device
;
854 set_bit(FLAG_EXPORT
, &desc
->flags
);
855 mutex_unlock(&sysfs_lock
);
858 fail_unregister_device
:
859 device_unregister(dev
);
861 mutex_unlock(&sysfs_lock
);
862 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
865 EXPORT_SYMBOL_GPL(gpiod_export
);
867 static int match_export(struct device
*dev
, const void *data
)
869 return dev_get_drvdata(dev
) == data
;
873 * gpiod_export_link - create a sysfs link to an exported GPIO node
874 * @dev: device under which to create symlink
875 * @name: name of the symlink
876 * @gpio: gpio to create symlink to, already exported
878 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
879 * node. Caller is responsible for unlinking.
881 * Returns zero on success, else an error.
883 int gpiod_export_link(struct device
*dev
, const char *name
,
884 struct gpio_desc
*desc
)
886 int status
= -EINVAL
;
889 pr_warn("%s: invalid GPIO\n", __func__
);
893 mutex_lock(&sysfs_lock
);
895 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
898 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
900 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
907 mutex_unlock(&sysfs_lock
);
910 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
914 EXPORT_SYMBOL_GPL(gpiod_export_link
);
917 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
918 * @gpio: gpio to change
919 * @value: non-zero to use active low, i.e. inverted values
921 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
922 * The GPIO does not have to be exported yet. If poll(2) support has
923 * been enabled for either rising or falling edge, it will be
924 * reconfigured to follow the new polarity.
926 * Returns zero on success, else an error.
928 int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
)
930 struct device
*dev
= NULL
;
931 int status
= -EINVAL
;
934 pr_warn("%s: invalid GPIO\n", __func__
);
938 mutex_lock(&sysfs_lock
);
940 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
941 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
948 status
= sysfs_set_active_low(desc
, dev
, value
);
951 mutex_unlock(&sysfs_lock
);
954 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
958 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low
);
961 * gpiod_unexport - reverse effect of gpio_export()
962 * @gpio: gpio to make unavailable
964 * This is implicit on gpio_free().
966 void gpiod_unexport(struct gpio_desc
*desc
)
969 struct device
*dev
= NULL
;
972 pr_warn("%s: invalid GPIO\n", __func__
);
976 mutex_lock(&sysfs_lock
);
978 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
980 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
982 gpio_setup_irq(desc
, dev
, 0);
983 clear_bit(FLAG_EXPORT
, &desc
->flags
);
988 mutex_unlock(&sysfs_lock
);
991 device_unregister(dev
);
996 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
998 EXPORT_SYMBOL_GPL(gpiod_unexport
);
1000 static int gpiochip_export(struct gpio_chip
*chip
)
1005 /* Many systems register gpio chips for SOC support very early,
1006 * before driver model support is available. In those cases we
1007 * export this later, in gpiolib_sysfs_init() ... here we just
1008 * verify that _some_ field of gpio_class got initialized.
1013 /* use chip->base for the ID; it's already known to be unique */
1014 mutex_lock(&sysfs_lock
);
1015 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
1016 "gpiochip%d", chip
->base
);
1018 status
= sysfs_create_group(&dev
->kobj
,
1019 &gpiochip_attr_group
);
1021 status
= PTR_ERR(dev
);
1022 chip
->exported
= (status
== 0);
1023 mutex_unlock(&sysfs_lock
);
1026 unsigned long flags
;
1029 spin_lock_irqsave(&gpio_lock
, flags
);
1031 while (gpio
< chip
->ngpio
)
1032 chip
->desc
[gpio
++].chip
= NULL
;
1033 spin_unlock_irqrestore(&gpio_lock
, flags
);
1035 pr_debug("%s: chip %s status %d\n", __func__
,
1036 chip
->label
, status
);
1042 static void gpiochip_unexport(struct gpio_chip
*chip
)
1047 mutex_lock(&sysfs_lock
);
1048 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
1051 device_unregister(dev
);
1052 chip
->exported
= false;
1056 mutex_unlock(&sysfs_lock
);
1059 pr_debug("%s: chip %s status %d\n", __func__
,
1060 chip
->label
, status
);
1063 static int __init
gpiolib_sysfs_init(void)
1066 unsigned long flags
;
1067 struct gpio_chip
*chip
;
1069 status
= class_register(&gpio_class
);
1073 /* Scan and register the gpio_chips which registered very
1074 * early (e.g. before the class_register above was called).
1076 * We run before arch_initcall() so chip->dev nodes can have
1077 * registered, and so arch_initcall() can always gpio_export().
1079 spin_lock_irqsave(&gpio_lock
, flags
);
1080 list_for_each_entry(chip
, &gpio_chips
, list
) {
1081 if (!chip
|| chip
->exported
)
1084 spin_unlock_irqrestore(&gpio_lock
, flags
);
1085 status
= gpiochip_export(chip
);
1086 spin_lock_irqsave(&gpio_lock
, flags
);
1088 spin_unlock_irqrestore(&gpio_lock
, flags
);
1093 postcore_initcall(gpiolib_sysfs_init
);
1096 static inline int gpiochip_export(struct gpio_chip
*chip
)
1101 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
1105 #endif /* CONFIG_GPIO_SYSFS */
1108 * Add a new chip to the global chips list, keeping the list of chips sorted
1111 * Return -EBUSY if the new chip overlaps with some other chip's integer
1114 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
1116 struct list_head
*pos
= &gpio_chips
;
1117 struct gpio_chip
*_chip
;
1120 /* find where to insert our chip */
1121 list_for_each(pos
, &gpio_chips
) {
1122 _chip
= list_entry(pos
, struct gpio_chip
, list
);
1123 /* shall we insert before _chip? */
1124 if (_chip
->base
>= chip
->base
+ chip
->ngpio
)
1128 /* are we stepping on the chip right before? */
1129 if (pos
!= &gpio_chips
&& pos
->prev
!= &gpio_chips
) {
1130 _chip
= list_entry(pos
->prev
, struct gpio_chip
, list
);
1131 if (_chip
->base
+ _chip
->ngpio
> chip
->base
) {
1133 "GPIO integer space overlap, cannot add chip\n");
1139 list_add_tail(&chip
->list
, pos
);
1145 * gpiochip_add() - register a gpio_chip
1146 * @chip: the chip to register, with chip->base initialized
1147 * Context: potentially before irqs or kmalloc will work
1149 * Returns a negative errno if the chip can't be registered, such as
1150 * because the chip->base is invalid or already associated with a
1151 * different chip. Otherwise it returns zero as a success code.
1153 * When gpiochip_add() is called very early during boot, so that GPIOs
1154 * can be freely used, the chip->dev device must be registered before
1155 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1156 * for GPIOs will fail rudely.
1158 * If chip->base is negative, this requests dynamic assignment of
1159 * a range of valid GPIOs.
1161 int gpiochip_add(struct gpio_chip
*chip
)
1163 unsigned long flags
;
1166 int base
= chip
->base
;
1168 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
1174 spin_lock_irqsave(&gpio_lock
, flags
);
1177 base
= gpiochip_find_base(chip
->ngpio
);
1185 status
= gpiochip_add_to_list(chip
);
1188 chip
->desc
= &gpio_desc
[chip
->base
];
1190 for (id
= 0; id
< chip
->ngpio
; id
++) {
1191 struct gpio_desc
*desc
= &chip
->desc
[id
];
1194 /* REVISIT: most hardware initializes GPIOs as
1195 * inputs (often with pullups enabled) so power
1196 * usage is minimized. Linux code should set the
1197 * gpio direction first thing; but until it does,
1198 * and in case chip->get_direction is not set,
1199 * we may expose the wrong direction in sysfs.
1201 desc
->flags
= !chip
->direction_input
1202 ? (1 << FLAG_IS_OUT
)
1207 spin_unlock_irqrestore(&gpio_lock
, flags
);
1209 #ifdef CONFIG_PINCTRL
1210 INIT_LIST_HEAD(&chip
->pin_ranges
);
1213 of_gpiochip_add(chip
);
1218 status
= gpiochip_export(chip
);
1222 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__
,
1223 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1224 chip
->label
? : "generic");
1229 spin_unlock_irqrestore(&gpio_lock
, flags
);
1231 /* failures here can mean systems won't boot... */
1232 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__
,
1233 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1234 chip
->label
? : "generic");
1237 EXPORT_SYMBOL_GPL(gpiochip_add
);
1240 * gpiochip_remove() - unregister a gpio_chip
1241 * @chip: the chip to unregister
1243 * A gpio_chip with any GPIOs still requested may not be removed.
1245 int gpiochip_remove(struct gpio_chip
*chip
)
1247 unsigned long flags
;
1251 spin_lock_irqsave(&gpio_lock
, flags
);
1253 gpiochip_remove_pin_ranges(chip
);
1254 of_gpiochip_remove(chip
);
1256 for (id
= 0; id
< chip
->ngpio
; id
++) {
1257 if (test_bit(FLAG_REQUESTED
, &chip
->desc
[id
].flags
)) {
1263 for (id
= 0; id
< chip
->ngpio
; id
++)
1264 chip
->desc
[id
].chip
= NULL
;
1266 list_del(&chip
->list
);
1269 spin_unlock_irqrestore(&gpio_lock
, flags
);
1272 gpiochip_unexport(chip
);
1276 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1279 * gpiochip_find() - iterator for locating a specific gpio_chip
1280 * @data: data to pass to match function
1281 * @callback: Callback function to check gpio_chip
1283 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1284 * determined by a user supplied @match callback. The callback should return
1285 * 0 if the device doesn't match and non-zero if it does. If the callback is
1286 * non-zero, this function will return to the caller and not iterate over any
1289 struct gpio_chip
*gpiochip_find(void *data
,
1290 int (*match
)(struct gpio_chip
*chip
,
1293 struct gpio_chip
*chip
;
1294 unsigned long flags
;
1296 spin_lock_irqsave(&gpio_lock
, flags
);
1297 list_for_each_entry(chip
, &gpio_chips
, list
)
1298 if (match(chip
, data
))
1302 if (&chip
->list
== &gpio_chips
)
1304 spin_unlock_irqrestore(&gpio_lock
, flags
);
1308 EXPORT_SYMBOL_GPL(gpiochip_find
);
1310 static int gpiochip_match_name(struct gpio_chip
*chip
, void *data
)
1312 const char *name
= data
;
1314 return !strcmp(chip
->label
, name
);
1317 static struct gpio_chip
*find_chip_by_name(const char *name
)
1319 return gpiochip_find((void *)name
, gpiochip_match_name
);
1322 #ifdef CONFIG_PINCTRL
1325 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1326 * @chip: the gpiochip to add the range for
1327 * @pinctrl: the dev_name() of the pin controller to map to
1328 * @gpio_offset: the start offset in the current gpio_chip number space
1329 * @pin_group: name of the pin group inside the pin controller
1331 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
1332 struct pinctrl_dev
*pctldev
,
1333 unsigned int gpio_offset
, const char *pin_group
)
1335 struct gpio_pin_range
*pin_range
;
1338 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1340 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1345 /* Use local offset as range ID */
1346 pin_range
->range
.id
= gpio_offset
;
1347 pin_range
->range
.gc
= chip
;
1348 pin_range
->range
.name
= chip
->label
;
1349 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1350 pin_range
->pctldev
= pctldev
;
1352 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
1353 &pin_range
->range
.pins
,
1354 &pin_range
->range
.npins
);
1360 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
1362 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1363 chip
->label
, gpio_offset
,
1364 gpio_offset
+ pin_range
->range
.npins
- 1,
1365 pinctrl_dev_get_devname(pctldev
), pin_group
);
1367 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1371 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
1374 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1375 * @chip: the gpiochip to add the range for
1376 * @pinctrl_name: the dev_name() of the pin controller to map to
1377 * @gpio_offset: the start offset in the current gpio_chip number space
1378 * @pin_offset: the start offset in the pin controller number space
1379 * @npins: the number of pins from the offset of each pin space (GPIO and
1380 * pin controller) to accumulate in this range
1382 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1383 unsigned int gpio_offset
, unsigned int pin_offset
,
1386 struct gpio_pin_range
*pin_range
;
1389 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1391 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1396 /* Use local offset as range ID */
1397 pin_range
->range
.id
= gpio_offset
;
1398 pin_range
->range
.gc
= chip
;
1399 pin_range
->range
.name
= chip
->label
;
1400 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1401 pin_range
->range
.pin_base
= pin_offset
;
1402 pin_range
->range
.npins
= npins
;
1403 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
1405 if (IS_ERR(pin_range
->pctldev
)) {
1406 ret
= PTR_ERR(pin_range
->pctldev
);
1407 pr_err("%s: GPIO chip: could not create pin range\n",
1412 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1413 chip
->label
, gpio_offset
, gpio_offset
+ npins
- 1,
1415 pin_offset
, pin_offset
+ npins
- 1);
1417 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1421 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1424 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1425 * @chip: the chip to remove all the mappings for
1427 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1429 struct gpio_pin_range
*pin_range
, *tmp
;
1431 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
1432 list_del(&pin_range
->node
);
1433 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1438 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1440 #endif /* CONFIG_PINCTRL */
1442 /* These "optional" allocation calls help prevent drivers from stomping
1443 * on each other, and help provide better diagnostics in debugfs.
1444 * They're called even less than the "set direction" calls.
1446 static int gpiod_request(struct gpio_desc
*desc
, const char *label
)
1448 struct gpio_chip
*chip
;
1449 int status
= -EPROBE_DEFER
;
1450 unsigned long flags
;
1453 pr_warn("%s: invalid GPIO\n", __func__
);
1457 spin_lock_irqsave(&gpio_lock
, flags
);
1463 if (!try_module_get(chip
->owner
))
1466 /* NOTE: gpio_request() can be called in early boot,
1467 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1470 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1471 desc_set_label(desc
, label
? : "?");
1475 module_put(chip
->owner
);
1479 if (chip
->request
) {
1480 /* chip->request may sleep */
1481 spin_unlock_irqrestore(&gpio_lock
, flags
);
1482 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
1483 spin_lock_irqsave(&gpio_lock
, flags
);
1486 desc_set_label(desc
, NULL
);
1487 module_put(chip
->owner
);
1488 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1492 if (chip
->get_direction
) {
1493 /* chip->get_direction may sleep */
1494 spin_unlock_irqrestore(&gpio_lock
, flags
);
1495 gpiod_get_direction(desc
);
1496 spin_lock_irqsave(&gpio_lock
, flags
);
1500 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1501 spin_unlock_irqrestore(&gpio_lock
, flags
);
1505 int gpio_request(unsigned gpio
, const char *label
)
1507 return gpiod_request(gpio_to_desc(gpio
), label
);
1509 EXPORT_SYMBOL_GPL(gpio_request
);
1511 static void gpiod_free(struct gpio_desc
*desc
)
1513 unsigned long flags
;
1514 struct gpio_chip
*chip
;
1519 WARN_ON(extra_checks
);
1523 gpiod_unexport(desc
);
1525 spin_lock_irqsave(&gpio_lock
, flags
);
1528 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1530 spin_unlock_irqrestore(&gpio_lock
, flags
);
1531 might_sleep_if(chip
->can_sleep
);
1532 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1533 spin_lock_irqsave(&gpio_lock
, flags
);
1535 desc_set_label(desc
, NULL
);
1536 module_put(desc
->chip
->owner
);
1537 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1538 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1539 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1540 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1542 WARN_ON(extra_checks
);
1544 spin_unlock_irqrestore(&gpio_lock
, flags
);
1547 void gpio_free(unsigned gpio
)
1549 gpiod_free(gpio_to_desc(gpio
));
1551 EXPORT_SYMBOL_GPL(gpio_free
);
1554 * gpio_request_one - request a single GPIO with initial configuration
1555 * @gpio: the GPIO number
1556 * @flags: GPIO configuration as specified by GPIOF_*
1557 * @label: a literal description string of this GPIO
1559 int gpio_request_one(unsigned gpio
, unsigned long flags
, const char *label
)
1561 struct gpio_desc
*desc
;
1564 desc
= gpio_to_desc(gpio
);
1566 err
= gpiod_request(desc
, label
);
1570 if (flags
& GPIOF_OPEN_DRAIN
)
1571 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1573 if (flags
& GPIOF_OPEN_SOURCE
)
1574 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1576 if (flags
& GPIOF_DIR_IN
)
1577 err
= gpiod_direction_input(desc
);
1579 err
= gpiod_direction_output(desc
,
1580 (flags
& GPIOF_INIT_HIGH
) ? 1 : 0);
1585 if (flags
& GPIOF_EXPORT
) {
1586 err
= gpiod_export(desc
, flags
& GPIOF_EXPORT_CHANGEABLE
);
1597 EXPORT_SYMBOL_GPL(gpio_request_one
);
1600 * gpio_request_array - request multiple GPIOs in a single call
1601 * @array: array of the 'struct gpio'
1602 * @num: how many GPIOs in the array
1604 int gpio_request_array(const struct gpio
*array
, size_t num
)
1608 for (i
= 0; i
< num
; i
++, array
++) {
1609 err
= gpio_request_one(array
->gpio
, array
->flags
, array
->label
);
1617 gpio_free((--array
)->gpio
);
1620 EXPORT_SYMBOL_GPL(gpio_request_array
);
1623 * gpio_free_array - release multiple GPIOs in a single call
1624 * @array: array of the 'struct gpio'
1625 * @num: how many GPIOs in the array
1627 void gpio_free_array(const struct gpio
*array
, size_t num
)
1630 gpio_free((array
++)->gpio
);
1632 EXPORT_SYMBOL_GPL(gpio_free_array
);
1635 * gpiochip_is_requested - return string iff signal was requested
1636 * @chip: controller managing the signal
1637 * @offset: of signal within controller's 0..(ngpio - 1) range
1639 * Returns NULL if the GPIO is not currently requested, else a string.
1640 * If debugfs support is enabled, the string returned is the label passed
1641 * to gpio_request(); otherwise it is a meaningless constant.
1643 * This function is for use by GPIO controller drivers. The label can
1644 * help with diagnostics, and knowing that the signal is used as a GPIO
1645 * can help avoid accidentally multiplexing it to another controller.
1647 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1649 struct gpio_desc
*desc
;
1651 if (!GPIO_OFFSET_VALID(chip
, offset
))
1654 desc
= &chip
->desc
[offset
];
1656 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1658 #ifdef CONFIG_DEBUG_FS
1664 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1667 /* Drivers MUST set GPIO direction before making get/set calls. In
1668 * some cases this is done in early boot, before IRQs are enabled.
1670 * As a rule these aren't called more than once (except for drivers
1671 * using the open-drain emulation idiom) so these are natural places
1672 * to accumulate extra debugging checks. Note that we can't (yet)
1673 * rely on gpio_request() having been called beforehand.
1677 * gpiod_direction_input - set the GPIO direction to input
1678 * @desc: GPIO to set to input
1680 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1681 * be called safely on it.
1683 * Return 0 in case of success, else an error code.
1685 int gpiod_direction_input(struct gpio_desc
*desc
)
1687 unsigned long flags
;
1688 struct gpio_chip
*chip
;
1689 int status
= -EINVAL
;
1692 if (!desc
|| !desc
->chip
) {
1693 pr_warn("%s: invalid GPIO\n", __func__
);
1698 if (!chip
->get
|| !chip
->direction_input
) {
1700 "%s: missing get() or direction_input() operations\n",
1705 spin_lock_irqsave(&gpio_lock
, flags
);
1707 status
= gpio_ensure_requested(desc
);
1711 /* now we know the gpio is valid and chip won't vanish */
1713 spin_unlock_irqrestore(&gpio_lock
, flags
);
1715 might_sleep_if(chip
->can_sleep
);
1717 offset
= gpio_chip_hwgpio(desc
);
1719 status
= chip
->request(chip
, offset
);
1721 gpiod_dbg(desc
, "%s: chip request fail, %d\n",
1723 /* and it's not available to anyone else ...
1724 * gpio_request() is the fully clean solution.
1730 status
= chip
->direction_input(chip
, offset
);
1732 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1734 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1738 spin_unlock_irqrestore(&gpio_lock
, flags
);
1740 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1743 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1746 * gpiod_direction_output - set the GPIO direction to input
1747 * @desc: GPIO to set to output
1748 * @value: initial output value of the GPIO
1750 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1751 * be called safely on it. The initial value of the output must be specified.
1753 * Return 0 in case of success, else an error code.
1755 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1757 unsigned long flags
;
1758 struct gpio_chip
*chip
;
1759 int status
= -EINVAL
;
1762 if (!desc
|| !desc
->chip
) {
1763 pr_warn("%s: invalid GPIO\n", __func__
);
1767 /* GPIOs used for IRQs shall not be set as output */
1768 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1770 "%s: tried to set a GPIO tied to an IRQ as output\n",
1775 /* Open drain pin should not be driven to 1 */
1776 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1777 return gpiod_direction_input(desc
);
1779 /* Open source pin should not be driven to 0 */
1780 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1781 return gpiod_direction_input(desc
);
1784 if (!chip
->set
|| !chip
->direction_output
) {
1786 "%s: missing set() or direction_output() operations\n",
1791 spin_lock_irqsave(&gpio_lock
, flags
);
1793 status
= gpio_ensure_requested(desc
);
1797 /* now we know the gpio is valid and chip won't vanish */
1799 spin_unlock_irqrestore(&gpio_lock
, flags
);
1801 might_sleep_if(chip
->can_sleep
);
1803 offset
= gpio_chip_hwgpio(desc
);
1805 status
= chip
->request(chip
, offset
);
1807 gpiod_dbg(desc
, "%s: chip request fail, %d\n",
1809 /* and it's not available to anyone else ...
1810 * gpio_request() is the fully clean solution.
1816 status
= chip
->direction_output(chip
, offset
, value
);
1818 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1819 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1820 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1824 spin_unlock_irqrestore(&gpio_lock
, flags
);
1826 gpiod_dbg(desc
, "%s: gpio status %d\n", __func__
, status
);
1829 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1832 * gpiod_set_debounce - sets @debounce time for a @gpio
1833 * @gpio: the gpio to set debounce time
1834 * @debounce: debounce time is microseconds
1836 * returns -ENOTSUPP if the controller does not support setting
1839 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1841 unsigned long flags
;
1842 struct gpio_chip
*chip
;
1843 int status
= -EINVAL
;
1846 if (!desc
|| !desc
->chip
) {
1847 pr_warn("%s: invalid GPIO\n", __func__
);
1852 if (!chip
->set
|| !chip
->set_debounce
) {
1854 "%s: missing set() or set_debounce() operations\n",
1859 spin_lock_irqsave(&gpio_lock
, flags
);
1861 status
= gpio_ensure_requested(desc
);
1865 /* now we know the gpio is valid and chip won't vanish */
1867 spin_unlock_irqrestore(&gpio_lock
, flags
);
1869 might_sleep_if(chip
->can_sleep
);
1871 offset
= gpio_chip_hwgpio(desc
);
1872 return chip
->set_debounce(chip
, offset
, debounce
);
1875 spin_unlock_irqrestore(&gpio_lock
, flags
);
1877 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1881 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1884 * gpiod_is_active_low - test whether a GPIO is active-low or not
1885 * @desc: the gpio descriptor to test
1887 * Returns 1 if the GPIO is active-low, 0 otherwise.
1889 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1891 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1893 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1895 /* I/O calls are only valid after configuration completed; the relevant
1896 * "is this a valid GPIO" error checks should already have been done.
1898 * "Get" operations are often inlinable as reading a pin value register,
1899 * and masking the relevant bit in that register.
1901 * When "set" operations are inlinable, they involve writing that mask to
1902 * one register to set a low value, or a different register to set it high.
1903 * Otherwise locking is needed, so there may be little value to inlining.
1905 *------------------------------------------------------------------------
1907 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1908 * have requested the GPIO. That can include implicit requesting by
1909 * a direction setting call. Marking a gpio as requested locks its chip
1910 * in memory, guaranteeing that these table lookups need no more locking
1911 * and that gpiochip_remove() will fail.
1913 * REVISIT when debugging, consider adding some instrumentation to ensure
1914 * that the GPIO was actually requested.
1917 static int _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1919 struct gpio_chip
*chip
;
1924 offset
= gpio_chip_hwgpio(desc
);
1925 value
= chip
->get
? chip
->get(chip
, offset
) : 0;
1926 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1931 * gpiod_get_raw_value() - return a gpio's raw value
1932 * @desc: gpio whose value will be returned
1934 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1935 * its ACTIVE_LOW status.
1937 * This function should be called from contexts where we cannot sleep, and will
1938 * complain if the GPIO chip functions potentially sleep.
1940 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1944 /* Should be using gpio_get_value_cansleep() */
1945 WARN_ON(desc
->chip
->can_sleep
);
1946 return _gpiod_get_raw_value(desc
);
1948 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1951 * gpiod_get_value() - return a gpio's value
1952 * @desc: gpio whose value will be returned
1954 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1957 * This function should be called from contexts where we cannot sleep, and will
1958 * complain if the GPIO chip functions potentially sleep.
1960 int gpiod_get_value(const struct gpio_desc
*desc
)
1965 /* Should be using gpio_get_value_cansleep() */
1966 WARN_ON(desc
->chip
->can_sleep
);
1968 value
= _gpiod_get_raw_value(desc
);
1969 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1974 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1977 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1978 * @desc: gpio descriptor whose state need to be set.
1979 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1981 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, int value
)
1984 struct gpio_chip
*chip
= desc
->chip
;
1985 int offset
= gpio_chip_hwgpio(desc
);
1988 err
= chip
->direction_input(chip
, offset
);
1990 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1992 err
= chip
->direction_output(chip
, offset
, 0);
1994 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1996 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1999 "%s: Error in set_value for open drain err %d\n",
2004 * _gpio_set_open_source_value() - Set the open source gpio's value.
2005 * @desc: gpio descriptor whose state need to be set.
2006 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
2008 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, int value
)
2011 struct gpio_chip
*chip
= desc
->chip
;
2012 int offset
= gpio_chip_hwgpio(desc
);
2015 err
= chip
->direction_output(chip
, offset
, 1);
2017 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2019 err
= chip
->direction_input(chip
, offset
);
2021 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2023 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
2026 "%s: Error in set_value for open source err %d\n",
2030 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2032 struct gpio_chip
*chip
;
2035 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
2036 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
2037 _gpio_set_open_drain_value(desc
, value
);
2038 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
2039 _gpio_set_open_source_value(desc
, value
);
2041 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
2045 * gpiod_set_raw_value() - assign a gpio's raw value
2046 * @desc: gpio whose value will be assigned
2047 * @value: value to assign
2049 * Set the raw value of the GPIO, i.e. the value of its physical line without
2050 * regard for its ACTIVE_LOW status.
2052 * This function should be called from contexts where we cannot sleep, and will
2053 * complain if the GPIO chip functions potentially sleep.
2055 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2059 /* Should be using gpio_set_value_cansleep() */
2060 WARN_ON(desc
->chip
->can_sleep
);
2061 _gpiod_set_raw_value(desc
, value
);
2063 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
2066 * gpiod_set_value() - assign a gpio's value
2067 * @desc: gpio whose value will be assigned
2068 * @value: value to assign
2070 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2073 * This function should be called from contexts where we cannot sleep, and will
2074 * complain if the GPIO chip functions potentially sleep.
2076 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
2080 /* Should be using gpio_set_value_cansleep() */
2081 WARN_ON(desc
->chip
->can_sleep
);
2082 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2084 _gpiod_set_raw_value(desc
, value
);
2086 EXPORT_SYMBOL_GPL(gpiod_set_value
);
2089 * gpiod_cansleep() - report whether gpio value access may sleep
2090 * @desc: gpio to check
2093 int gpiod_cansleep(const struct gpio_desc
*desc
)
2097 return desc
->chip
->can_sleep
;
2099 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
2102 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2103 * @desc: gpio whose IRQ will be returned (already requested)
2105 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2108 int gpiod_to_irq(const struct gpio_desc
*desc
)
2110 struct gpio_chip
*chip
;
2116 offset
= gpio_chip_hwgpio(desc
);
2117 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
2119 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
2122 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2123 * @gpio: the GPIO line to lock as used for IRQ
2125 * This is used directly by GPIO drivers that want to lock down
2126 * a certain GPIO line to be used as IRQs, for example in the
2127 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2128 * of its irq_chip implementation if the GPIO is known from that
2131 int gpiod_lock_as_irq(struct gpio_desc
*desc
)
2136 if (test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
2138 "%s: tried to flag a GPIO set as output for IRQ\n",
2143 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
2146 EXPORT_SYMBOL_GPL(gpiod_lock_as_irq
);
2148 int gpio_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2150 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip
, offset
));
2152 EXPORT_SYMBOL_GPL(gpio_lock_as_irq
);
2155 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2156 * @gpio: the GPIO line to unlock from IRQ usage
2158 * This is used directly by GPIO drivers that want to indicate
2159 * that a certain GPIO is no longer used exclusively for IRQ.
2161 void gpiod_unlock_as_irq(struct gpio_desc
*desc
)
2166 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
2168 EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq
);
2170 void gpio_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2172 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip
, offset
));
2174 EXPORT_SYMBOL_GPL(gpio_unlock_as_irq
);
2177 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2178 * @desc: gpio whose value will be returned
2180 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2181 * its ACTIVE_LOW status.
2183 * This function is to be called from contexts that can sleep.
2185 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
2187 might_sleep_if(extra_checks
);
2190 return _gpiod_get_raw_value(desc
);
2192 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
2195 * gpiod_get_value_cansleep() - return a gpio's value
2196 * @desc: gpio whose value will be returned
2198 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2201 * This function is to be called from contexts that can sleep.
2203 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
2207 might_sleep_if(extra_checks
);
2211 value
= _gpiod_get_raw_value(desc
);
2212 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2217 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
2220 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2221 * @desc: gpio whose value will be assigned
2222 * @value: value to assign
2224 * Set the raw value of the GPIO, i.e. the value of its physical line without
2225 * regard for its ACTIVE_LOW status.
2227 * This function is to be called from contexts that can sleep.
2229 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
2231 might_sleep_if(extra_checks
);
2234 _gpiod_set_raw_value(desc
, value
);
2236 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
2239 * gpiod_set_value_cansleep() - assign a gpio's value
2240 * @desc: gpio whose value will be assigned
2241 * @value: value to assign
2243 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2246 * This function is to be called from contexts that can sleep.
2248 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
2250 might_sleep_if(extra_checks
);
2254 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2256 _gpiod_set_raw_value(desc
, value
);
2258 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
2261 * gpiod_add_lookup_table() - register GPIO device consumers
2262 * @table: table of consumers to register
2264 void gpiod_add_lookup_table(struct gpiod_lookup_table
*table
)
2266 mutex_lock(&gpio_lookup_lock
);
2268 list_add_tail(&table
->list
, &gpio_lookup_list
);
2270 mutex_unlock(&gpio_lookup_lock
);
2274 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2276 enum gpio_lookup_flags
*flags
)
2278 char prop_name
[32]; /* 32 is max size of property name */
2279 enum of_gpio_flags of_flags
;
2280 struct gpio_desc
*desc
;
2283 snprintf(prop_name
, 32, "%s-gpios", con_id
);
2285 snprintf(prop_name
, 32, "gpios");
2287 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
2293 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2294 *flags
|= GPIO_ACTIVE_LOW
;
2299 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2301 enum gpio_lookup_flags
*flags
)
2303 return ERR_PTR(-ENODEV
);
2307 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
2309 enum gpio_lookup_flags
*flags
)
2311 struct acpi_gpio_info info
;
2312 struct gpio_desc
*desc
;
2314 desc
= acpi_get_gpiod_by_index(dev
, idx
, &info
);
2318 if (info
.gpioint
&& info
.active_low
)
2319 *flags
|= GPIO_ACTIVE_LOW
;
2324 static struct gpiod_lookup_table
*gpiod_find_lookup_table(struct device
*dev
)
2326 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
2327 struct gpiod_lookup_table
*table
;
2329 mutex_lock(&gpio_lookup_lock
);
2331 list_for_each_entry(table
, &gpio_lookup_list
, list
) {
2332 if (table
->dev_id
&& dev_id
) {
2334 * Valid strings on both ends, must be identical to have
2337 if (!strcmp(table
->dev_id
, dev_id
))
2341 * One of the pointers is NULL, so both must be to have
2344 if (dev_id
== table
->dev_id
)
2351 mutex_unlock(&gpio_lookup_lock
);
2355 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
2357 enum gpio_lookup_flags
*flags
)
2359 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2360 struct gpiod_lookup_table
*table
;
2361 struct gpiod_lookup
*p
;
2363 table
= gpiod_find_lookup_table(dev
);
2367 for (p
= &table
->table
[0]; p
->chip_label
; p
++) {
2368 struct gpio_chip
*chip
;
2370 /* idx must always match exactly */
2374 /* If the lookup entry has a con_id, require exact match */
2375 if (p
->con_id
&& (!con_id
|| strcmp(p
->con_id
, con_id
)))
2378 chip
= find_chip_by_name(p
->chip_label
);
2381 dev_warn(dev
, "cannot find GPIO chip %s\n",
2386 if (chip
->ngpio
<= p
->chip_hwnum
) {
2387 dev_warn(dev
, "GPIO chip %s has %d GPIOs\n",
2388 chip
->label
, chip
->ngpio
);
2392 desc
= gpiochip_offset_to_desc(chip
, p
->chip_hwnum
);
2400 * gpio_get - obtain a GPIO for a given GPIO function
2401 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2402 * @con_id: function within the GPIO consumer
2404 * Return the GPIO descriptor corresponding to the function con_id of device
2405 * dev, or an IS_ERR() condition if an error occured.
2407 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
)
2409 return gpiod_get_index(dev
, con_id
, 0);
2411 EXPORT_SYMBOL_GPL(gpiod_get
);
2414 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2415 * @dev: GPIO consumer
2416 * @con_id: function within the GPIO consumer
2417 * @idx: index of the GPIO to obtain in the consumer
2419 * This variant of gpiod_get() allows to access GPIOs other than the first
2420 * defined one for functions that define several GPIOs.
2422 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2424 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
2428 struct gpio_desc
*desc
= NULL
;
2430 enum gpio_lookup_flags flags
= 0;
2432 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
2434 /* Using device tree? */
2435 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
) {
2436 dev_dbg(dev
, "using device tree for GPIO lookup\n");
2437 desc
= of_find_gpio(dev
, con_id
, idx
, &flags
);
2438 } else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
)) {
2439 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
2440 desc
= acpi_find_gpio(dev
, con_id
, idx
, &flags
);
2444 * Either we are not using DT or ACPI, or their lookup did not return
2445 * a result. In that case, use platform lookup as a fallback.
2447 if (!desc
|| IS_ERR(desc
)) {
2448 struct gpio_desc
*pdesc
;
2450 dev_dbg(dev
, "using lookup tables for GPIO lookup");
2451 pdesc
= gpiod_find(dev
, con_id
, idx
, &flags
);
2453 /* If used as fallback, do not replace the previous error */
2454 if (!IS_ERR(pdesc
) || !desc
)
2459 dev_dbg(dev
, "lookup for GPIO %s failed\n", con_id
);
2463 status
= gpiod_request(desc
, con_id
);
2466 return ERR_PTR(status
);
2468 if (flags
& GPIO_ACTIVE_LOW
)
2469 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2470 if (flags
& GPIO_OPEN_DRAIN
)
2471 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
2472 if (flags
& GPIO_OPEN_SOURCE
)
2473 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
2477 EXPORT_SYMBOL_GPL(gpiod_get_index
);
2480 * gpiod_put - dispose of a GPIO descriptor
2481 * @desc: GPIO descriptor to dispose of
2483 * No descriptor can be used after gpiod_put() has been called on it.
2485 void gpiod_put(struct gpio_desc
*desc
)
2489 EXPORT_SYMBOL_GPL(gpiod_put
);
2491 #ifdef CONFIG_DEBUG_FS
2493 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
2496 unsigned gpio
= chip
->base
;
2497 struct gpio_desc
*gdesc
= &chip
->desc
[0];
2501 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
2502 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
2505 gpiod_get_direction(gdesc
);
2506 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2507 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2508 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s",
2510 is_out
? "out" : "in ",
2512 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2514 is_irq
? "IRQ" : " ");
2515 seq_printf(s
, "\n");
2519 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2521 unsigned long flags
;
2522 struct gpio_chip
*chip
= NULL
;
2523 loff_t index
= *pos
;
2527 spin_lock_irqsave(&gpio_lock
, flags
);
2528 list_for_each_entry(chip
, &gpio_chips
, list
)
2530 spin_unlock_irqrestore(&gpio_lock
, flags
);
2533 spin_unlock_irqrestore(&gpio_lock
, flags
);
2538 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2540 unsigned long flags
;
2541 struct gpio_chip
*chip
= v
;
2544 spin_lock_irqsave(&gpio_lock
, flags
);
2545 if (list_is_last(&chip
->list
, &gpio_chips
))
2548 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
2549 spin_unlock_irqrestore(&gpio_lock
, flags
);
2557 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2561 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2563 struct gpio_chip
*chip
= v
;
2566 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
2567 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
2570 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
2573 seq_printf(s
, ", %s", chip
->label
);
2574 if (chip
->can_sleep
)
2575 seq_printf(s
, ", can sleep");
2576 seq_printf(s
, ":\n");
2579 chip
->dbg_show(s
, chip
);
2581 gpiolib_dbg_show(s
, chip
);
2586 static const struct seq_operations gpiolib_seq_ops
= {
2587 .start
= gpiolib_seq_start
,
2588 .next
= gpiolib_seq_next
,
2589 .stop
= gpiolib_seq_stop
,
2590 .show
= gpiolib_seq_show
,
2593 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2595 return seq_open(file
, &gpiolib_seq_ops
);
2598 static const struct file_operations gpiolib_operations
= {
2599 .owner
= THIS_MODULE
,
2600 .open
= gpiolib_open
,
2602 .llseek
= seq_lseek
,
2603 .release
= seq_release
,
2606 static int __init
gpiolib_debugfs_init(void)
2608 /* /sys/kernel/debug/gpio */
2609 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2610 NULL
, NULL
, &gpiolib_operations
);
2613 subsys_initcall(gpiolib_debugfs_init
);
2615 #endif /* DEBUG_FS */