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>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/gpio.h>
21 /* Implementation infrastructure for GPIO interfaces.
23 * The GPIO programming interface allows for inlining speed-critical
24 * get/set operations for common cases, so that access to SOC-integrated
25 * GPIOs can sometimes cost only an instruction or two per bit.
29 /* When debugging, extend minimal trust to callers and platform code.
30 * Also emit diagnostic messages that may help initial bringup, when
31 * board setup or driver bugs are most common.
33 * Otherwise, minimize overhead in what may be bitbanging codepaths.
36 #define extra_checks 1
38 #define extra_checks 0
41 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
42 * While any GPIO is requested, its gpio_chip is not removable;
43 * each GPIO's "requested" flag serves as a lock and refcount.
45 static DEFINE_SPINLOCK(gpio_lock
);
48 struct gpio_chip
*chip
;
50 /* flag symbols are bit numbers */
51 #define FLAG_REQUESTED 0
53 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
54 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
55 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
56 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
57 #define FLAG_ACTIVE_LOW 6 /* value has active low */
58 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
59 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
60 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
62 #define ID_SHIFT 16 /* add new flags before this one */
64 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
65 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
67 #ifdef CONFIG_DEBUG_FS
71 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
73 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
75 static DEFINE_MUTEX(gpio_lookup_lock
);
76 static LIST_HEAD(gpio_lookup_list
);
77 static LIST_HEAD(gpio_chips
);
79 #ifdef CONFIG_GPIO_SYSFS
80 static DEFINE_IDR(dirent_idr
);
83 static int gpiod_request(struct gpio_desc
*desc
, const char *label
);
84 static void gpiod_free(struct gpio_desc
*desc
);
86 #ifdef CONFIG_DEBUG_FS
87 #define gpiod_emerg(desc, fmt, ...) \
88 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
90 #define gpiod_crit(desc, fmt, ...) \
91 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
93 #define gpiod_err(desc, fmt, ...) \
94 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
96 #define gpiod_warn(desc, fmt, ...) \
97 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
99 #define gpiod_info(desc, fmt, ...) \
100 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
102 #define gpiod_dbg(desc, fmt, ...) \
103 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
106 #define gpiod_emerg(desc, fmt, ...) \
107 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
108 #define gpiod_crit(desc, fmt, ...) \
109 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
110 #define gpiod_err(desc, fmt, ...) \
111 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
112 #define gpiod_warn(desc, fmt, ...) \
113 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
114 #define gpiod_info(desc, fmt, ...) \
115 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
116 #define gpiod_dbg(desc, fmt, ...) \
117 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
120 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
122 #ifdef CONFIG_DEBUG_FS
128 * Return the GPIO number of the passed descriptor relative to its chip
130 static int gpio_chip_hwgpio(const struct gpio_desc
*desc
)
132 return desc
- &desc
->chip
->desc
[0];
136 * Convert a GPIO number to its descriptor
138 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
140 if (WARN(!gpio_is_valid(gpio
), "invalid GPIO %d\n", gpio
))
143 return &gpio_desc
[gpio
];
145 EXPORT_SYMBOL_GPL(gpio_to_desc
);
148 * Convert an offset on a certain chip to a corresponding descriptor
150 static struct gpio_desc
*gpiochip_offset_to_desc(struct gpio_chip
*chip
,
153 if (offset
>= chip
->ngpio
)
154 return ERR_PTR(-EINVAL
);
156 return &chip
->desc
[offset
];
160 * Convert a GPIO descriptor to the integer namespace.
161 * This should disappear in the future but is needed since we still
162 * use GPIO numbers for error messages and sysfs nodes
164 int desc_to_gpio(const struct gpio_desc
*desc
)
166 return desc
- &gpio_desc
[0];
168 EXPORT_SYMBOL_GPL(desc_to_gpio
);
171 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
172 * when setting direction, and otherwise illegal. Until board setup code
173 * and drivers use explicit requests everywhere (which won't happen when
174 * those calls have no teeth) we can't avoid autorequesting. This nag
175 * message should motivate switching to explicit requests... so should
176 * the weaker cleanup after faults, compared to gpio_request().
178 * NOTE: the autorequest mechanism is going away; at this point it's
179 * only "legal" in the sense that (old) code using it won't break yet,
180 * but instead only triggers a WARN() stack dump.
182 static int gpio_ensure_requested(struct gpio_desc
*desc
)
184 const struct gpio_chip
*chip
= desc
->chip
;
185 const int gpio
= desc_to_gpio(desc
);
187 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
188 "autorequest GPIO-%d\n", gpio
)) {
189 if (!try_module_get(chip
->owner
)) {
190 pr_err("GPIO-%d: module can't be gotten \n", gpio
);
191 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
195 desc_set_label(desc
, "[auto]");
196 /* caller must chip->request() w/o spinlock */
204 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
205 * @desc: descriptor to return the chip of
207 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
209 return desc
? desc
->chip
: NULL
;
211 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
213 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
214 static int gpiochip_find_base(int ngpio
)
216 struct gpio_chip
*chip
;
217 int base
= ARCH_NR_GPIOS
- ngpio
;
219 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
220 /* found a free space? */
221 if (chip
->base
+ chip
->ngpio
<= base
)
224 /* nope, check the space right before the chip */
225 base
= chip
->base
- ngpio
;
228 if (gpio_is_valid(base
)) {
229 pr_debug("%s: found new base at %d\n", __func__
, base
);
232 pr_err("%s: cannot find free range\n", __func__
);
238 * gpiod_get_direction - return the current direction of a GPIO
239 * @desc: GPIO to get the direction of
241 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
243 * This function may sleep if gpiod_cansleep() is true.
245 int gpiod_get_direction(const struct gpio_desc
*desc
)
247 struct gpio_chip
*chip
;
249 int status
= -EINVAL
;
251 chip
= gpiod_to_chip(desc
);
252 offset
= gpio_chip_hwgpio(desc
);
254 if (!chip
->get_direction
)
257 status
= chip
->get_direction(chip
, offset
);
259 /* GPIOF_DIR_IN, or other positive */
261 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
262 * so it does not affect constness per se */
263 clear_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
267 set_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
271 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
273 #ifdef CONFIG_GPIO_SYSFS
275 /* lock protects against unexport_gpio() being called while
276 * sysfs files are active.
278 static DEFINE_MUTEX(sysfs_lock
);
281 * /sys/class/gpio/gpioN... only for GPIOs that are exported
283 * * MAY BE OMITTED if kernel won't allow direction changes
284 * * is read/write as "in" or "out"
285 * * may also be written as "high" or "low", initializing
286 * output value as specified ("out" implies "low")
288 * * always readable, subject to hardware behavior
289 * * may be writable, as zero/nonzero
291 * * configures behavior of poll(2) on /value
292 * * available only if pin can generate IRQs on input
293 * * is read/write as "none", "falling", "rising", or "both"
295 * * configures polarity of /value
296 * * is read/write as zero/nonzero
297 * * also affects existing and subsequent "falling" and "rising"
298 * /edge configuration
301 static ssize_t
gpio_direction_show(struct device
*dev
,
302 struct device_attribute
*attr
, char *buf
)
304 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
307 mutex_lock(&sysfs_lock
);
309 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
312 gpiod_get_direction(desc
);
313 status
= sprintf(buf
, "%s\n",
314 test_bit(FLAG_IS_OUT
, &desc
->flags
)
318 mutex_unlock(&sysfs_lock
);
322 static ssize_t
gpio_direction_store(struct device
*dev
,
323 struct device_attribute
*attr
, const char *buf
, size_t size
)
325 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
328 mutex_lock(&sysfs_lock
);
330 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
332 else if (sysfs_streq(buf
, "high"))
333 status
= gpiod_direction_output(desc
, 1);
334 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
335 status
= gpiod_direction_output(desc
, 0);
336 else if (sysfs_streq(buf
, "in"))
337 status
= gpiod_direction_input(desc
);
341 mutex_unlock(&sysfs_lock
);
342 return status
? : size
;
345 static /* const */ DEVICE_ATTR(direction
, 0644,
346 gpio_direction_show
, gpio_direction_store
);
348 static ssize_t
gpio_value_show(struct device
*dev
,
349 struct device_attribute
*attr
, char *buf
)
351 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
354 mutex_lock(&sysfs_lock
);
356 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
359 status
= sprintf(buf
, "%d\n", gpiod_get_value_cansleep(desc
));
361 mutex_unlock(&sysfs_lock
);
365 static ssize_t
gpio_value_store(struct device
*dev
,
366 struct device_attribute
*attr
, const char *buf
, size_t size
)
368 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
371 mutex_lock(&sysfs_lock
);
373 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
375 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
380 status
= kstrtol(buf
, 0, &value
);
382 gpiod_set_value_cansleep(desc
, value
);
387 mutex_unlock(&sysfs_lock
);
391 static const DEVICE_ATTR(value
, 0644,
392 gpio_value_show
, gpio_value_store
);
394 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
396 struct sysfs_dirent
*value_sd
= priv
;
398 sysfs_notify_dirent(value_sd
);
402 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
403 unsigned long gpio_flags
)
405 struct sysfs_dirent
*value_sd
;
406 unsigned long irq_flags
;
409 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
412 irq
= gpiod_to_irq(desc
);
416 id
= desc
->flags
>> ID_SHIFT
;
417 value_sd
= idr_find(&dirent_idr
, id
);
419 free_irq(irq
, value_sd
);
421 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
424 gpiod_unlock_as_irq(desc
);
429 irq_flags
= IRQF_SHARED
;
430 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
431 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
432 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
433 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
434 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
435 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
438 value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, "value");
444 ret
= idr_alloc(&dirent_idr
, value_sd
, 1, 0, GFP_KERNEL
);
449 desc
->flags
&= GPIO_FLAGS_MASK
;
450 desc
->flags
|= (unsigned long)id
<< ID_SHIFT
;
452 if (desc
->flags
>> ID_SHIFT
!= id
) {
458 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
459 "gpiolib", value_sd
);
463 ret
= gpiod_lock_as_irq(desc
);
465 gpiod_warn(desc
, "failed to flag the GPIO for IRQ\n");
469 desc
->flags
|= gpio_flags
;
473 idr_remove(&dirent_idr
, id
);
474 desc
->flags
&= GPIO_FLAGS_MASK
;
482 static const struct {
485 } trigger_types
[] = {
487 { "falling", BIT(FLAG_TRIG_FALL
) },
488 { "rising", BIT(FLAG_TRIG_RISE
) },
489 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
492 static ssize_t
gpio_edge_show(struct device
*dev
,
493 struct device_attribute
*attr
, char *buf
)
495 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
498 mutex_lock(&sysfs_lock
);
500 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
506 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
507 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
508 == trigger_types
[i
].flags
) {
509 status
= sprintf(buf
, "%s\n",
510 trigger_types
[i
].name
);
515 mutex_unlock(&sysfs_lock
);
519 static ssize_t
gpio_edge_store(struct device
*dev
,
520 struct device_attribute
*attr
, const char *buf
, size_t size
)
522 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
526 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
527 if (sysfs_streq(trigger_types
[i
].name
, buf
))
532 mutex_lock(&sysfs_lock
);
534 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
537 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
542 mutex_unlock(&sysfs_lock
);
547 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
549 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
554 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
558 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
560 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
562 /* reconfigure poll(2) support if enabled on one edge only */
563 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
564 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
565 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
567 gpio_setup_irq(desc
, dev
, 0);
568 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
574 static ssize_t
gpio_active_low_show(struct device
*dev
,
575 struct device_attribute
*attr
, char *buf
)
577 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
580 mutex_lock(&sysfs_lock
);
582 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
585 status
= sprintf(buf
, "%d\n",
586 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
588 mutex_unlock(&sysfs_lock
);
593 static ssize_t
gpio_active_low_store(struct device
*dev
,
594 struct device_attribute
*attr
, const char *buf
, size_t size
)
596 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
599 mutex_lock(&sysfs_lock
);
601 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
606 status
= kstrtol(buf
, 0, &value
);
608 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
611 mutex_unlock(&sysfs_lock
);
613 return status
? : size
;
616 static const DEVICE_ATTR(active_low
, 0644,
617 gpio_active_low_show
, gpio_active_low_store
);
619 static const struct attribute
*gpio_attrs
[] = {
620 &dev_attr_value
.attr
,
621 &dev_attr_active_low
.attr
,
625 static const struct attribute_group gpio_attr_group
= {
626 .attrs
= (struct attribute
**) gpio_attrs
,
630 * /sys/class/gpio/gpiochipN/
631 * /base ... matching gpio_chip.base (N)
632 * /label ... matching gpio_chip.label
633 * /ngpio ... matching gpio_chip.ngpio
636 static ssize_t
chip_base_show(struct device
*dev
,
637 struct device_attribute
*attr
, char *buf
)
639 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
641 return sprintf(buf
, "%d\n", chip
->base
);
643 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
645 static ssize_t
chip_label_show(struct device
*dev
,
646 struct device_attribute
*attr
, char *buf
)
648 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
650 return sprintf(buf
, "%s\n", chip
->label
? : "");
652 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
654 static ssize_t
chip_ngpio_show(struct device
*dev
,
655 struct device_attribute
*attr
, char *buf
)
657 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
659 return sprintf(buf
, "%u\n", chip
->ngpio
);
661 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
663 static const struct attribute
*gpiochip_attrs
[] = {
665 &dev_attr_label
.attr
,
666 &dev_attr_ngpio
.attr
,
670 static const struct attribute_group gpiochip_attr_group
= {
671 .attrs
= (struct attribute
**) gpiochip_attrs
,
675 * /sys/class/gpio/export ... write-only
676 * integer N ... number of GPIO to export (full access)
677 * /sys/class/gpio/unexport ... write-only
678 * integer N ... number of GPIO to unexport
680 static ssize_t
export_store(struct class *class,
681 struct class_attribute
*attr
,
682 const char *buf
, size_t len
)
685 struct gpio_desc
*desc
;
688 status
= kstrtol(buf
, 0, &gpio
);
692 desc
= gpio_to_desc(gpio
);
693 /* reject invalid GPIOs */
695 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
699 /* No extra locking here; FLAG_SYSFS just signifies that the
700 * request and export were done by on behalf of userspace, so
701 * they may be undone on its behalf too.
704 status
= gpiod_request(desc
, "sysfs");
706 if (status
== -EPROBE_DEFER
)
710 status
= gpiod_export(desc
, true);
714 set_bit(FLAG_SYSFS
, &desc
->flags
);
718 pr_debug("%s: status %d\n", __func__
, status
);
719 return status
? : len
;
722 static ssize_t
unexport_store(struct class *class,
723 struct class_attribute
*attr
,
724 const char *buf
, size_t len
)
727 struct gpio_desc
*desc
;
730 status
= kstrtol(buf
, 0, &gpio
);
734 desc
= gpio_to_desc(gpio
);
735 /* reject bogus commands (gpio_unexport ignores them) */
737 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
743 /* No extra locking here; FLAG_SYSFS just signifies that the
744 * request and export were done by on behalf of userspace, so
745 * they may be undone on its behalf too.
747 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
)) {
753 pr_debug("%s: status %d\n", __func__
, status
);
754 return status
? : len
;
757 static struct class_attribute gpio_class_attrs
[] = {
758 __ATTR(export
, 0200, NULL
, export_store
),
759 __ATTR(unexport
, 0200, NULL
, unexport_store
),
763 static struct class gpio_class
= {
765 .owner
= THIS_MODULE
,
767 .class_attrs
= gpio_class_attrs
,
772 * gpiod_export - export a GPIO through sysfs
773 * @gpio: gpio to make available, already requested
774 * @direction_may_change: true if userspace may change gpio direction
775 * Context: arch_initcall or later
777 * When drivers want to make a GPIO accessible to userspace after they
778 * have requested it -- perhaps while debugging, or as part of their
779 * public interface -- they may use this routine. If the GPIO can
780 * change direction (some can't) and the caller allows it, userspace
781 * will see "direction" sysfs attribute which may be used to change
782 * the gpio's direction. A "value" attribute will always be provided.
784 * Returns zero on success, else an error.
786 int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
)
790 const char *ioname
= NULL
;
794 /* can't export until sysfs is available ... */
796 pr_debug("%s: called too early!\n", __func__
);
801 pr_debug("%s: invalid gpio descriptor\n", __func__
);
805 mutex_lock(&sysfs_lock
);
807 spin_lock_irqsave(&gpio_lock
, flags
);
808 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
809 test_bit(FLAG_EXPORT
, &desc
->flags
)) {
810 spin_unlock_irqrestore(&gpio_lock
, flags
);
811 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
812 __func__
, desc_to_gpio(desc
),
813 test_bit(FLAG_REQUESTED
, &desc
->flags
),
814 test_bit(FLAG_EXPORT
, &desc
->flags
));
819 if (!desc
->chip
->direction_input
|| !desc
->chip
->direction_output
)
820 direction_may_change
= false;
821 spin_unlock_irqrestore(&gpio_lock
, flags
);
823 offset
= gpio_chip_hwgpio(desc
);
824 if (desc
->chip
->names
&& desc
->chip
->names
[offset
])
825 ioname
= desc
->chip
->names
[offset
];
827 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
828 desc
, ioname
? ioname
: "gpio%u",
831 status
= PTR_ERR(dev
);
835 status
= sysfs_create_group(&dev
->kobj
, &gpio_attr_group
);
837 goto fail_unregister_device
;
839 if (direction_may_change
) {
840 status
= device_create_file(dev
, &dev_attr_direction
);
842 goto fail_unregister_device
;
845 if (gpiod_to_irq(desc
) >= 0 && (direction_may_change
||
846 !test_bit(FLAG_IS_OUT
, &desc
->flags
))) {
847 status
= device_create_file(dev
, &dev_attr_edge
);
849 goto fail_unregister_device
;
852 set_bit(FLAG_EXPORT
, &desc
->flags
);
853 mutex_unlock(&sysfs_lock
);
856 fail_unregister_device
:
857 device_unregister(dev
);
859 mutex_unlock(&sysfs_lock
);
860 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
864 EXPORT_SYMBOL_GPL(gpiod_export
);
866 static int match_export(struct device
*dev
, const void *data
)
868 return dev_get_drvdata(dev
) == data
;
872 * gpiod_export_link - create a sysfs link to an exported GPIO node
873 * @dev: device under which to create symlink
874 * @name: name of the symlink
875 * @gpio: gpio to create symlink to, already exported
877 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
878 * node. Caller is responsible for unlinking.
880 * Returns zero on success, else an error.
882 int gpiod_export_link(struct device
*dev
, const char *name
,
883 struct gpio_desc
*desc
)
885 int status
= -EINVAL
;
888 pr_warn("%s: invalid GPIO\n", __func__
);
892 mutex_lock(&sysfs_lock
);
894 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
897 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
899 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
906 mutex_unlock(&sysfs_lock
);
909 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
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 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
959 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low
);
962 * gpiod_unexport - reverse effect of gpio_export()
963 * @gpio: gpio to make unavailable
965 * This is implicit on gpio_free().
967 void gpiod_unexport(struct gpio_desc
*desc
)
970 struct device
*dev
= NULL
;
973 pr_warn("%s: invalid GPIO\n", __func__
);
977 mutex_lock(&sysfs_lock
);
979 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
981 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
983 gpio_setup_irq(desc
, dev
, 0);
984 clear_bit(FLAG_EXPORT
, &desc
->flags
);
989 mutex_unlock(&sysfs_lock
);
992 device_unregister(dev
);
997 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
1000 EXPORT_SYMBOL_GPL(gpiod_unexport
);
1002 static int gpiochip_export(struct gpio_chip
*chip
)
1007 /* Many systems register gpio chips for SOC support very early,
1008 * before driver model support is available. In those cases we
1009 * export this later, in gpiolib_sysfs_init() ... here we just
1010 * verify that _some_ field of gpio_class got initialized.
1015 /* use chip->base for the ID; it's already known to be unique */
1016 mutex_lock(&sysfs_lock
);
1017 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
1018 "gpiochip%d", chip
->base
);
1020 status
= sysfs_create_group(&dev
->kobj
,
1021 &gpiochip_attr_group
);
1023 status
= PTR_ERR(dev
);
1024 chip
->exported
= (status
== 0);
1025 mutex_unlock(&sysfs_lock
);
1028 unsigned long flags
;
1031 spin_lock_irqsave(&gpio_lock
, flags
);
1033 while (gpio
< chip
->ngpio
)
1034 chip
->desc
[gpio
++].chip
= NULL
;
1035 spin_unlock_irqrestore(&gpio_lock
, flags
);
1037 pr_debug("%s: chip %s status %d\n", __func__
,
1038 chip
->label
, status
);
1044 static void gpiochip_unexport(struct gpio_chip
*chip
)
1049 mutex_lock(&sysfs_lock
);
1050 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
1053 device_unregister(dev
);
1054 chip
->exported
= false;
1058 mutex_unlock(&sysfs_lock
);
1061 pr_debug("%s: chip %s status %d\n", __func__
,
1062 chip
->label
, status
);
1065 static int __init
gpiolib_sysfs_init(void)
1068 unsigned long flags
;
1069 struct gpio_chip
*chip
;
1071 status
= class_register(&gpio_class
);
1075 /* Scan and register the gpio_chips which registered very
1076 * early (e.g. before the class_register above was called).
1078 * We run before arch_initcall() so chip->dev nodes can have
1079 * registered, and so arch_initcall() can always gpio_export().
1081 spin_lock_irqsave(&gpio_lock
, flags
);
1082 list_for_each_entry(chip
, &gpio_chips
, list
) {
1083 if (!chip
|| chip
->exported
)
1086 spin_unlock_irqrestore(&gpio_lock
, flags
);
1087 status
= gpiochip_export(chip
);
1088 spin_lock_irqsave(&gpio_lock
, flags
);
1090 spin_unlock_irqrestore(&gpio_lock
, flags
);
1095 postcore_initcall(gpiolib_sysfs_init
);
1098 static inline int gpiochip_export(struct gpio_chip
*chip
)
1103 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
1107 #endif /* CONFIG_GPIO_SYSFS */
1110 * Add a new chip to the global chips list, keeping the list of chips sorted
1113 * Return -EBUSY if the new chip overlaps with some other chip's integer
1116 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
1118 struct list_head
*pos
= &gpio_chips
;
1119 struct gpio_chip
*_chip
;
1122 /* find where to insert our chip */
1123 list_for_each(pos
, &gpio_chips
) {
1124 _chip
= list_entry(pos
, struct gpio_chip
, list
);
1125 /* shall we insert before _chip? */
1126 if (_chip
->base
>= chip
->base
+ chip
->ngpio
)
1130 /* are we stepping on the chip right before? */
1131 if (pos
!= &gpio_chips
&& pos
->prev
!= &gpio_chips
) {
1132 _chip
= list_entry(pos
->prev
, struct gpio_chip
, list
);
1133 if (_chip
->base
+ _chip
->ngpio
> chip
->base
) {
1135 "GPIO integer space overlap, cannot add chip\n");
1141 list_add_tail(&chip
->list
, pos
);
1147 * gpiochip_add() - register a gpio_chip
1148 * @chip: the chip to register, with chip->base initialized
1149 * Context: potentially before irqs or kmalloc will work
1151 * Returns a negative errno if the chip can't be registered, such as
1152 * because the chip->base is invalid or already associated with a
1153 * different chip. Otherwise it returns zero as a success code.
1155 * When gpiochip_add() is called very early during boot, so that GPIOs
1156 * can be freely used, the chip->dev device must be registered before
1157 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1158 * for GPIOs will fail rudely.
1160 * If chip->base is negative, this requests dynamic assignment of
1161 * a range of valid GPIOs.
1163 int gpiochip_add(struct gpio_chip
*chip
)
1165 unsigned long flags
;
1168 int base
= chip
->base
;
1170 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
1176 spin_lock_irqsave(&gpio_lock
, flags
);
1179 base
= gpiochip_find_base(chip
->ngpio
);
1187 status
= gpiochip_add_to_list(chip
);
1190 chip
->desc
= &gpio_desc
[chip
->base
];
1192 for (id
= 0; id
< chip
->ngpio
; id
++) {
1193 struct gpio_desc
*desc
= &chip
->desc
[id
];
1196 /* REVISIT: most hardware initializes GPIOs as
1197 * inputs (often with pullups enabled) so power
1198 * usage is minimized. Linux code should set the
1199 * gpio direction first thing; but until it does,
1200 * and in case chip->get_direction is not set,
1201 * we may expose the wrong direction in sysfs.
1203 desc
->flags
= !chip
->direction_input
1204 ? (1 << FLAG_IS_OUT
)
1209 spin_unlock_irqrestore(&gpio_lock
, flags
);
1211 #ifdef CONFIG_PINCTRL
1212 INIT_LIST_HEAD(&chip
->pin_ranges
);
1215 of_gpiochip_add(chip
);
1220 status
= gpiochip_export(chip
);
1224 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1225 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1226 chip
->label
? : "generic");
1231 spin_unlock_irqrestore(&gpio_lock
, flags
);
1233 /* failures here can mean systems won't boot... */
1234 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1235 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1236 chip
->label
? : "generic");
1239 EXPORT_SYMBOL_GPL(gpiochip_add
);
1242 * gpiochip_remove() - unregister a gpio_chip
1243 * @chip: the chip to unregister
1245 * A gpio_chip with any GPIOs still requested may not be removed.
1247 int gpiochip_remove(struct gpio_chip
*chip
)
1249 unsigned long flags
;
1253 spin_lock_irqsave(&gpio_lock
, flags
);
1255 gpiochip_remove_pin_ranges(chip
);
1256 of_gpiochip_remove(chip
);
1258 for (id
= 0; id
< chip
->ngpio
; id
++) {
1259 if (test_bit(FLAG_REQUESTED
, &chip
->desc
[id
].flags
)) {
1265 for (id
= 0; id
< chip
->ngpio
; id
++)
1266 chip
->desc
[id
].chip
= NULL
;
1268 list_del(&chip
->list
);
1271 spin_unlock_irqrestore(&gpio_lock
, flags
);
1274 gpiochip_unexport(chip
);
1278 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1281 * gpiochip_find() - iterator for locating a specific gpio_chip
1282 * @data: data to pass to match function
1283 * @callback: Callback function to check gpio_chip
1285 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1286 * determined by a user supplied @match callback. The callback should return
1287 * 0 if the device doesn't match and non-zero if it does. If the callback is
1288 * non-zero, this function will return to the caller and not iterate over any
1291 struct gpio_chip
*gpiochip_find(void *data
,
1292 int (*match
)(struct gpio_chip
*chip
,
1295 struct gpio_chip
*chip
;
1296 unsigned long flags
;
1298 spin_lock_irqsave(&gpio_lock
, flags
);
1299 list_for_each_entry(chip
, &gpio_chips
, list
)
1300 if (match(chip
, data
))
1304 if (&chip
->list
== &gpio_chips
)
1306 spin_unlock_irqrestore(&gpio_lock
, flags
);
1310 EXPORT_SYMBOL_GPL(gpiochip_find
);
1312 #ifdef CONFIG_PINCTRL
1315 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1316 * @chip: the gpiochip to add the range for
1317 * @pinctrl: the dev_name() of the pin controller to map to
1318 * @gpio_offset: the start offset in the current gpio_chip number space
1319 * @pin_group: name of the pin group inside the pin controller
1321 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
1322 struct pinctrl_dev
*pctldev
,
1323 unsigned int gpio_offset
, const char *pin_group
)
1325 struct gpio_pin_range
*pin_range
;
1328 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1330 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1335 /* Use local offset as range ID */
1336 pin_range
->range
.id
= gpio_offset
;
1337 pin_range
->range
.gc
= chip
;
1338 pin_range
->range
.name
= chip
->label
;
1339 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1340 pin_range
->pctldev
= pctldev
;
1342 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
1343 &pin_range
->range
.pins
,
1344 &pin_range
->range
.npins
);
1348 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
1350 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1351 chip
->label
, gpio_offset
,
1352 gpio_offset
+ pin_range
->range
.npins
- 1,
1353 pinctrl_dev_get_devname(pctldev
), pin_group
);
1355 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1359 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
1362 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1363 * @chip: the gpiochip to add the range for
1364 * @pinctrl_name: the dev_name() of the pin controller to map to
1365 * @gpio_offset: the start offset in the current gpio_chip number space
1366 * @pin_offset: the start offset in the pin controller number space
1367 * @npins: the number of pins from the offset of each pin space (GPIO and
1368 * pin controller) to accumulate in this range
1370 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1371 unsigned int gpio_offset
, unsigned int pin_offset
,
1374 struct gpio_pin_range
*pin_range
;
1377 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1379 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1384 /* Use local offset as range ID */
1385 pin_range
->range
.id
= gpio_offset
;
1386 pin_range
->range
.gc
= chip
;
1387 pin_range
->range
.name
= chip
->label
;
1388 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1389 pin_range
->range
.pin_base
= pin_offset
;
1390 pin_range
->range
.npins
= npins
;
1391 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
1393 if (IS_ERR(pin_range
->pctldev
)) {
1394 ret
= PTR_ERR(pin_range
->pctldev
);
1395 pr_err("%s: GPIO chip: could not create pin range\n",
1400 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1401 chip
->label
, gpio_offset
, gpio_offset
+ npins
- 1,
1403 pin_offset
, pin_offset
+ npins
- 1);
1405 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1409 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1412 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1413 * @chip: the chip to remove all the mappings for
1415 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1417 struct gpio_pin_range
*pin_range
, *tmp
;
1419 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
1420 list_del(&pin_range
->node
);
1421 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1426 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1428 #endif /* CONFIG_PINCTRL */
1430 /* These "optional" allocation calls help prevent drivers from stomping
1431 * on each other, and help provide better diagnostics in debugfs.
1432 * They're called even less than the "set direction" calls.
1434 static int gpiod_request(struct gpio_desc
*desc
, const char *label
)
1436 struct gpio_chip
*chip
;
1437 int status
= -EPROBE_DEFER
;
1438 unsigned long flags
;
1441 pr_warn("%s: invalid GPIO\n", __func__
);
1445 spin_lock_irqsave(&gpio_lock
, flags
);
1451 if (!try_module_get(chip
->owner
))
1454 /* NOTE: gpio_request() can be called in early boot,
1455 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1458 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1459 desc_set_label(desc
, label
? : "?");
1463 module_put(chip
->owner
);
1467 if (chip
->request
) {
1468 /* chip->request may sleep */
1469 spin_unlock_irqrestore(&gpio_lock
, flags
);
1470 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
1471 spin_lock_irqsave(&gpio_lock
, flags
);
1474 desc_set_label(desc
, NULL
);
1475 module_put(chip
->owner
);
1476 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1480 if (chip
->get_direction
) {
1481 /* chip->get_direction may sleep */
1482 spin_unlock_irqrestore(&gpio_lock
, flags
);
1483 gpiod_get_direction(desc
);
1484 spin_lock_irqsave(&gpio_lock
, flags
);
1488 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1489 desc_to_gpio(desc
), label
? : "?", status
);
1490 spin_unlock_irqrestore(&gpio_lock
, flags
);
1494 int gpio_request(unsigned gpio
, const char *label
)
1496 return gpiod_request(gpio_to_desc(gpio
), label
);
1498 EXPORT_SYMBOL_GPL(gpio_request
);
1500 static void gpiod_free(struct gpio_desc
*desc
)
1502 unsigned long flags
;
1503 struct gpio_chip
*chip
;
1508 WARN_ON(extra_checks
);
1512 gpiod_unexport(desc
);
1514 spin_lock_irqsave(&gpio_lock
, flags
);
1517 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1519 spin_unlock_irqrestore(&gpio_lock
, flags
);
1520 might_sleep_if(chip
->can_sleep
);
1521 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1522 spin_lock_irqsave(&gpio_lock
, flags
);
1524 desc_set_label(desc
, NULL
);
1525 module_put(desc
->chip
->owner
);
1526 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1527 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1528 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1529 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1531 WARN_ON(extra_checks
);
1533 spin_unlock_irqrestore(&gpio_lock
, flags
);
1536 void gpio_free(unsigned gpio
)
1538 gpiod_free(gpio_to_desc(gpio
));
1540 EXPORT_SYMBOL_GPL(gpio_free
);
1543 * gpio_request_one - request a single GPIO with initial configuration
1544 * @gpio: the GPIO number
1545 * @flags: GPIO configuration as specified by GPIOF_*
1546 * @label: a literal description string of this GPIO
1548 int gpio_request_one(unsigned gpio
, unsigned long flags
, const char *label
)
1550 struct gpio_desc
*desc
;
1553 desc
= gpio_to_desc(gpio
);
1555 err
= gpiod_request(desc
, label
);
1559 if (flags
& GPIOF_OPEN_DRAIN
)
1560 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1562 if (flags
& GPIOF_OPEN_SOURCE
)
1563 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1565 if (flags
& GPIOF_DIR_IN
)
1566 err
= gpiod_direction_input(desc
);
1568 err
= gpiod_direction_output(desc
,
1569 (flags
& GPIOF_INIT_HIGH
) ? 1 : 0);
1574 if (flags
& GPIOF_EXPORT
) {
1575 err
= gpiod_export(desc
, flags
& GPIOF_EXPORT_CHANGEABLE
);
1586 EXPORT_SYMBOL_GPL(gpio_request_one
);
1589 * gpio_request_array - request multiple GPIOs in a single call
1590 * @array: array of the 'struct gpio'
1591 * @num: how many GPIOs in the array
1593 int gpio_request_array(const struct gpio
*array
, size_t num
)
1597 for (i
= 0; i
< num
; i
++, array
++) {
1598 err
= gpio_request_one(array
->gpio
, array
->flags
, array
->label
);
1606 gpio_free((--array
)->gpio
);
1609 EXPORT_SYMBOL_GPL(gpio_request_array
);
1612 * gpio_free_array - release multiple GPIOs in a single call
1613 * @array: array of the 'struct gpio'
1614 * @num: how many GPIOs in the array
1616 void gpio_free_array(const struct gpio
*array
, size_t num
)
1619 gpio_free((array
++)->gpio
);
1621 EXPORT_SYMBOL_GPL(gpio_free_array
);
1624 * gpiochip_is_requested - return string iff signal was requested
1625 * @chip: controller managing the signal
1626 * @offset: of signal within controller's 0..(ngpio - 1) range
1628 * Returns NULL if the GPIO is not currently requested, else a string.
1629 * If debugfs support is enabled, the string returned is the label passed
1630 * to gpio_request(); otherwise it is a meaningless constant.
1632 * This function is for use by GPIO controller drivers. The label can
1633 * help with diagnostics, and knowing that the signal is used as a GPIO
1634 * can help avoid accidentally multiplexing it to another controller.
1636 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1638 struct gpio_desc
*desc
;
1640 if (!GPIO_OFFSET_VALID(chip
, offset
))
1643 desc
= &chip
->desc
[offset
];
1645 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1647 #ifdef CONFIG_DEBUG_FS
1653 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1656 /* Drivers MUST set GPIO direction before making get/set calls. In
1657 * some cases this is done in early boot, before IRQs are enabled.
1659 * As a rule these aren't called more than once (except for drivers
1660 * using the open-drain emulation idiom) so these are natural places
1661 * to accumulate extra debugging checks. Note that we can't (yet)
1662 * rely on gpio_request() having been called beforehand.
1666 * gpiod_direction_input - set the GPIO direction to input
1667 * @desc: GPIO to set to input
1669 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1670 * be called safely on it.
1672 * Return 0 in case of success, else an error code.
1674 int gpiod_direction_input(struct gpio_desc
*desc
)
1676 unsigned long flags
;
1677 struct gpio_chip
*chip
;
1678 int status
= -EINVAL
;
1681 if (!desc
|| !desc
->chip
) {
1682 pr_warn("%s: invalid GPIO\n", __func__
);
1687 if (!chip
->get
|| !chip
->direction_input
) {
1689 "%s: missing get() or direction_input() operations\n",
1694 spin_lock_irqsave(&gpio_lock
, flags
);
1696 status
= gpio_ensure_requested(desc
);
1700 /* now we know the gpio is valid and chip won't vanish */
1702 spin_unlock_irqrestore(&gpio_lock
, flags
);
1704 might_sleep_if(chip
->can_sleep
);
1706 offset
= gpio_chip_hwgpio(desc
);
1708 status
= chip
->request(chip
, offset
);
1710 gpiod_dbg(desc
, "chip request fail, %d\n", status
);
1711 /* and it's not available to anyone else ...
1712 * gpio_request() is the fully clean solution.
1718 status
= chip
->direction_input(chip
, offset
);
1720 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1722 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1726 spin_unlock_irqrestore(&gpio_lock
, flags
);
1728 gpiod_dbg(desc
, "%s status %d\n", __func__
, status
);
1731 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1734 * gpiod_direction_output - set the GPIO direction to input
1735 * @desc: GPIO to set to output
1736 * @value: initial output value of the GPIO
1738 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1739 * be called safely on it. The initial value of the output must be specified.
1741 * Return 0 in case of success, else an error code.
1743 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1745 unsigned long flags
;
1746 struct gpio_chip
*chip
;
1747 int status
= -EINVAL
;
1750 if (!desc
|| !desc
->chip
) {
1751 pr_warn("%s: invalid GPIO\n", __func__
);
1755 /* GPIOs used for IRQs shall not be set as output */
1756 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1758 "%s: tried to set a GPIO tied to an IRQ as output\n",
1763 /* Open drain pin should not be driven to 1 */
1764 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1765 return gpiod_direction_input(desc
);
1767 /* Open source pin should not be driven to 0 */
1768 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1769 return gpiod_direction_input(desc
);
1772 if (!chip
->set
|| !chip
->direction_output
) {
1774 "%s: missing set() or direction_output() operations\n",
1779 spin_lock_irqsave(&gpio_lock
, flags
);
1781 status
= gpio_ensure_requested(desc
);
1785 /* now we know the gpio is valid and chip won't vanish */
1787 spin_unlock_irqrestore(&gpio_lock
, flags
);
1789 might_sleep_if(chip
->can_sleep
);
1791 offset
= gpio_chip_hwgpio(desc
);
1793 status
= chip
->request(chip
, offset
);
1795 gpiod_dbg(desc
, "chip request fail, %d\n", status
);
1796 /* and it's not available to anyone else ...
1797 * gpio_request() is the fully clean solution.
1803 status
= chip
->direction_output(chip
, offset
, value
);
1805 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1806 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1807 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1811 spin_unlock_irqrestore(&gpio_lock
, flags
);
1813 gpiod_dbg(desc
, "%s: gpio status %d\n", __func__
, status
);
1816 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1819 * gpiod_set_debounce - sets @debounce time for a @gpio
1820 * @gpio: the gpio to set debounce time
1821 * @debounce: debounce time is microseconds
1823 * returns -ENOTSUPP if the controller does not support setting
1826 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1828 unsigned long flags
;
1829 struct gpio_chip
*chip
;
1830 int status
= -EINVAL
;
1833 if (!desc
|| !desc
->chip
) {
1834 pr_warn("%s: invalid GPIO\n", __func__
);
1839 if (!chip
->set
|| !chip
->set_debounce
) {
1841 "%s: missing set() or set_debounce() operations\n",
1846 spin_lock_irqsave(&gpio_lock
, flags
);
1848 status
= gpio_ensure_requested(desc
);
1852 /* now we know the gpio is valid and chip won't vanish */
1854 spin_unlock_irqrestore(&gpio_lock
, flags
);
1856 might_sleep_if(chip
->can_sleep
);
1858 offset
= gpio_chip_hwgpio(desc
);
1859 return chip
->set_debounce(chip
, offset
, debounce
);
1862 spin_unlock_irqrestore(&gpio_lock
, flags
);
1864 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1868 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1871 * gpiod_is_active_low - test whether a GPIO is active-low or not
1872 * @desc: the gpio descriptor to test
1874 * Returns 1 if the GPIO is active-low, 0 otherwise.
1876 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1878 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1880 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1882 /* I/O calls are only valid after configuration completed; the relevant
1883 * "is this a valid GPIO" error checks should already have been done.
1885 * "Get" operations are often inlinable as reading a pin value register,
1886 * and masking the relevant bit in that register.
1888 * When "set" operations are inlinable, they involve writing that mask to
1889 * one register to set a low value, or a different register to set it high.
1890 * Otherwise locking is needed, so there may be little value to inlining.
1892 *------------------------------------------------------------------------
1894 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1895 * have requested the GPIO. That can include implicit requesting by
1896 * a direction setting call. Marking a gpio as requested locks its chip
1897 * in memory, guaranteeing that these table lookups need no more locking
1898 * and that gpiochip_remove() will fail.
1900 * REVISIT when debugging, consider adding some instrumentation to ensure
1901 * that the GPIO was actually requested.
1904 static int _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1906 struct gpio_chip
*chip
;
1911 offset
= gpio_chip_hwgpio(desc
);
1912 value
= chip
->get
? chip
->get(chip
, offset
) : 0;
1913 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1918 * gpiod_get_raw_value() - return a gpio's raw value
1919 * @desc: gpio whose value will be returned
1921 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1922 * its ACTIVE_LOW status.
1924 * This function should be called from contexts where we cannot sleep, and will
1925 * complain if the GPIO chip functions potentially sleep.
1927 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1931 /* Should be using gpio_get_value_cansleep() */
1932 WARN_ON(desc
->chip
->can_sleep
);
1933 return _gpiod_get_raw_value(desc
);
1935 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1938 * gpiod_get_value() - return a gpio's value
1939 * @desc: gpio whose value will be returned
1941 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1944 * This function should be called from contexts where we cannot sleep, and will
1945 * complain if the GPIO chip functions potentially sleep.
1947 int gpiod_get_value(const struct gpio_desc
*desc
)
1952 /* Should be using gpio_get_value_cansleep() */
1953 WARN_ON(desc
->chip
->can_sleep
);
1955 value
= _gpiod_get_raw_value(desc
);
1956 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1961 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1964 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1965 * @desc: gpio descriptor whose state need to be set.
1966 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1968 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, int value
)
1971 struct gpio_chip
*chip
= desc
->chip
;
1972 int offset
= gpio_chip_hwgpio(desc
);
1975 err
= chip
->direction_input(chip
, offset
);
1977 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1979 err
= chip
->direction_output(chip
, offset
, 0);
1981 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1983 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1986 "%s: Error in set_value for open drain err %d\n",
1991 * _gpio_set_open_source_value() - Set the open source gpio's value.
1992 * @desc: gpio descriptor whose state need to be set.
1993 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1995 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, int value
)
1998 struct gpio_chip
*chip
= desc
->chip
;
1999 int offset
= gpio_chip_hwgpio(desc
);
2002 err
= chip
->direction_output(chip
, offset
, 1);
2004 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2006 err
= chip
->direction_input(chip
, offset
);
2008 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2010 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
2013 "%s: Error in set_value for open source err %d\n",
2017 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2019 struct gpio_chip
*chip
;
2022 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
2023 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
2024 _gpio_set_open_drain_value(desc
, value
);
2025 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
2026 _gpio_set_open_source_value(desc
, value
);
2028 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
2032 * gpiod_set_raw_value() - assign a gpio's raw value
2033 * @desc: gpio whose value will be assigned
2034 * @value: value to assign
2036 * Set the raw value of the GPIO, i.e. the value of its physical line without
2037 * regard for its ACTIVE_LOW status.
2039 * This function should be called from contexts where we cannot sleep, and will
2040 * complain if the GPIO chip functions potentially sleep.
2042 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2046 /* Should be using gpio_set_value_cansleep() */
2047 WARN_ON(desc
->chip
->can_sleep
);
2048 _gpiod_set_raw_value(desc
, value
);
2050 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
2053 * gpiod_set_value() - assign a gpio's value
2054 * @desc: gpio whose value will be assigned
2055 * @value: value to assign
2057 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2060 * This function should be called from contexts where we cannot sleep, and will
2061 * complain if the GPIO chip functions potentially sleep.
2063 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
2067 /* Should be using gpio_set_value_cansleep() */
2068 WARN_ON(desc
->chip
->can_sleep
);
2069 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2071 _gpiod_set_raw_value(desc
, value
);
2073 EXPORT_SYMBOL_GPL(gpiod_set_value
);
2076 * gpiod_cansleep() - report whether gpio value access may sleep
2077 * @desc: gpio to check
2080 int gpiod_cansleep(const struct gpio_desc
*desc
)
2084 return desc
->chip
->can_sleep
;
2086 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
2089 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2090 * @desc: gpio whose IRQ will be returned (already requested)
2092 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2095 int gpiod_to_irq(const struct gpio_desc
*desc
)
2097 struct gpio_chip
*chip
;
2103 offset
= gpio_chip_hwgpio(desc
);
2104 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
2106 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
2109 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2110 * @gpio: the GPIO line to lock as used for IRQ
2112 * This is used directly by GPIO drivers that want to lock down
2113 * a certain GPIO line to be used as IRQs, for example in the
2114 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2115 * of its irq_chip implementation if the GPIO is known from that
2118 int gpiod_lock_as_irq(struct gpio_desc
*desc
)
2123 if (test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
2125 "%s: tried to flag a GPIO set as output for IRQ\n",
2130 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
2133 EXPORT_SYMBOL_GPL(gpiod_lock_as_irq
);
2135 int gpio_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2137 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip
, offset
));
2139 EXPORT_SYMBOL_GPL(gpio_lock_as_irq
);
2142 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2143 * @gpio: the GPIO line to unlock from IRQ usage
2145 * This is used directly by GPIO drivers that want to indicate
2146 * that a certain GPIO is no longer used exclusively for IRQ.
2148 void gpiod_unlock_as_irq(struct gpio_desc
*desc
)
2153 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
2155 EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq
);
2157 void gpio_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2159 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip
, offset
));
2161 EXPORT_SYMBOL_GPL(gpio_unlock_as_irq
);
2164 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2165 * @desc: gpio whose value will be returned
2167 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2168 * its ACTIVE_LOW status.
2170 * This function is to be called from contexts that can sleep.
2172 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
2174 might_sleep_if(extra_checks
);
2177 return _gpiod_get_raw_value(desc
);
2179 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
2182 * gpiod_get_value_cansleep() - return a gpio's value
2183 * @desc: gpio whose value will be returned
2185 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2188 * This function is to be called from contexts that can sleep.
2190 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
2194 might_sleep_if(extra_checks
);
2198 value
= _gpiod_get_raw_value(desc
);
2199 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2204 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
2207 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2208 * @desc: gpio whose value will be assigned
2209 * @value: value to assign
2211 * Set the raw value of the GPIO, i.e. the value of its physical line without
2212 * regard for its ACTIVE_LOW status.
2214 * This function is to be called from contexts that can sleep.
2216 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
2218 might_sleep_if(extra_checks
);
2221 _gpiod_set_raw_value(desc
, value
);
2223 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
2226 * gpiod_set_value_cansleep() - assign a gpio's value
2227 * @desc: gpio whose value will be assigned
2228 * @value: value to assign
2230 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2233 * This function is to be called from contexts that can sleep.
2235 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
2237 might_sleep_if(extra_checks
);
2241 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2243 _gpiod_set_raw_value(desc
, value
);
2245 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
2248 * gpiod_add_table() - register GPIO device consumers
2249 * @table: array of consumers to register
2250 * @num: number of consumers in table
2252 void gpiod_add_table(struct gpiod_lookup
*table
, size_t size
)
2254 mutex_lock(&gpio_lookup_lock
);
2257 list_add_tail(&table
->list
, &gpio_lookup_list
);
2261 mutex_unlock(&gpio_lookup_lock
);
2265 * Caller must have a acquired gpio_lookup_lock
2267 static struct gpio_chip
*find_chip_by_name(const char *name
)
2269 struct gpio_chip
*chip
= NULL
;
2271 list_for_each_entry(chip
, &gpio_lookup_list
, list
) {
2272 if (chip
->label
== NULL
)
2274 if (!strcmp(chip
->label
, name
))
2282 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2283 unsigned int idx
, unsigned long *flags
)
2285 char prop_name
[32]; /* 32 is max size of property name */
2286 enum of_gpio_flags of_flags
;
2287 struct gpio_desc
*desc
;
2290 snprintf(prop_name
, 32, "%s-gpios", con_id
);
2292 snprintf(prop_name
, 32, "gpios");
2294 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
2300 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2301 *flags
|= GPIOF_ACTIVE_LOW
;
2306 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2307 unsigned int idx
, unsigned long *flags
)
2309 return ERR_PTR(-ENODEV
);
2313 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
2314 unsigned int idx
, unsigned long *flags
)
2316 struct acpi_gpio_info info
;
2317 struct gpio_desc
*desc
;
2319 desc
= acpi_get_gpiod_by_index(dev
, idx
, &info
);
2323 if (info
.gpioint
&& info
.active_low
)
2324 *flags
|= GPIOF_ACTIVE_LOW
;
2329 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
2330 unsigned int idx
, unsigned long *flags
)
2332 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
2333 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2334 unsigned int match
, best
= 0;
2335 struct gpiod_lookup
*p
;
2337 mutex_lock(&gpio_lookup_lock
);
2339 list_for_each_entry(p
, &gpio_lookup_list
, list
) {
2343 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
2350 if (!con_id
|| strcmp(p
->con_id
, con_id
))
2360 struct gpio_chip
*chip
;
2362 chip
= find_chip_by_name(p
->chip_label
);
2365 dev_warn(dev
, "cannot find GPIO chip %s\n",
2370 if (chip
->ngpio
>= p
->chip_hwnum
) {
2371 dev_warn(dev
, "GPIO chip %s has %d GPIOs\n",
2372 chip
->label
, chip
->ngpio
);
2376 desc
= gpio_to_desc(chip
->base
+ p
->chip_hwnum
);
2386 mutex_unlock(&gpio_lookup_lock
);
2392 * gpio_get - obtain a GPIO for a given GPIO function
2393 * @dev: GPIO consumer
2394 * @con_id: function within the GPIO consumer
2396 * Return the GPIO descriptor corresponding to the function con_id of device
2397 * dev, or an IS_ERR() condition if an error occured.
2399 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
)
2401 return gpiod_get_index(dev
, con_id
, 0);
2403 EXPORT_SYMBOL_GPL(gpiod_get
);
2406 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2407 * @dev: GPIO consumer
2408 * @con_id: function within the GPIO consumer
2409 * @idx: index of the GPIO to obtain in the consumer
2411 * This variant of gpiod_get() allows to access GPIOs other than the first
2412 * defined one for functions that define several GPIOs.
2414 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2416 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
2420 struct gpio_desc
*desc
;
2422 unsigned long flags
= 0;
2424 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
2426 /* Using device tree? */
2427 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
) {
2428 dev_dbg(dev
, "using device tree for GPIO lookup\n");
2429 desc
= of_find_gpio(dev
, con_id
, idx
, &flags
);
2430 } else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
)) {
2431 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
2432 desc
= acpi_find_gpio(dev
, con_id
, idx
, &flags
);
2434 dev_dbg(dev
, "using lookup tables for GPIO lookup");
2435 desc
= gpiod_find(dev
, con_id
, idx
, &flags
);
2439 dev_warn(dev
, "lookup for GPIO %s failed\n", con_id
);
2443 status
= gpiod_request(desc
, con_id
);
2446 return ERR_PTR(status
);
2448 if (flags
& GPIOF_ACTIVE_LOW
)
2449 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2453 EXPORT_SYMBOL_GPL(gpiod_get_index
);
2456 * gpiod_put - dispose of a GPIO descriptor
2457 * @desc: GPIO descriptor to dispose of
2459 * No descriptor can be used after gpiod_put() has been called on it.
2461 void gpiod_put(struct gpio_desc
*desc
)
2465 EXPORT_SYMBOL_GPL(gpiod_put
);
2467 #ifdef CONFIG_DEBUG_FS
2469 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
2472 unsigned gpio
= chip
->base
;
2473 struct gpio_desc
*gdesc
= &chip
->desc
[0];
2477 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
2478 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
2481 gpiod_get_direction(gdesc
);
2482 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2483 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2484 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s",
2486 is_out
? "out" : "in ",
2488 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2490 is_irq
? "IRQ" : " ");
2491 seq_printf(s
, "\n");
2495 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2497 unsigned long flags
;
2498 struct gpio_chip
*chip
= NULL
;
2499 loff_t index
= *pos
;
2503 spin_lock_irqsave(&gpio_lock
, flags
);
2504 list_for_each_entry(chip
, &gpio_chips
, list
)
2506 spin_unlock_irqrestore(&gpio_lock
, flags
);
2509 spin_unlock_irqrestore(&gpio_lock
, flags
);
2514 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2516 unsigned long flags
;
2517 struct gpio_chip
*chip
= v
;
2520 spin_lock_irqsave(&gpio_lock
, flags
);
2521 if (list_is_last(&chip
->list
, &gpio_chips
))
2524 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
2525 spin_unlock_irqrestore(&gpio_lock
, flags
);
2533 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2537 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2539 struct gpio_chip
*chip
= v
;
2542 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
2543 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
2546 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
2549 seq_printf(s
, ", %s", chip
->label
);
2550 if (chip
->can_sleep
)
2551 seq_printf(s
, ", can sleep");
2552 seq_printf(s
, ":\n");
2555 chip
->dbg_show(s
, chip
);
2557 gpiolib_dbg_show(s
, chip
);
2562 static const struct seq_operations gpiolib_seq_ops
= {
2563 .start
= gpiolib_seq_start
,
2564 .next
= gpiolib_seq_next
,
2565 .stop
= gpiolib_seq_stop
,
2566 .show
= gpiolib_seq_show
,
2569 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2571 return seq_open(file
, &gpiolib_seq_ops
);
2574 static const struct file_operations gpiolib_operations
= {
2575 .owner
= THIS_MODULE
,
2576 .open
= gpiolib_open
,
2578 .llseek
= seq_lseek
,
2579 .release
= seq_release
,
2582 static int __init
gpiolib_debugfs_init(void)
2584 /* /sys/kernel/debug/gpio */
2585 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2586 NULL
, NULL
, &gpiolib_operations
);
2589 subsys_initcall(gpiolib_debugfs_init
);
2591 #endif /* DEBUG_FS */