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>
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/gpio.h>
20 /* Implementation infrastructure for GPIO interfaces.
22 * The GPIO programming interface allows for inlining speed-critical
23 * get/set operations for common cases, so that access to SOC-integrated
24 * GPIOs can sometimes cost only an instruction or two per bit.
28 /* When debugging, extend minimal trust to callers and platform code.
29 * Also emit diagnostic messages that may help initial bringup, when
30 * board setup or driver bugs are most common.
32 * Otherwise, minimize overhead in what may be bitbanging codepaths.
35 #define extra_checks 1
37 #define extra_checks 0
40 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
41 * While any GPIO is requested, its gpio_chip is not removable;
42 * each GPIO's "requested" flag serves as a lock and refcount.
44 static DEFINE_SPINLOCK(gpio_lock
);
47 struct gpio_chip
*chip
;
49 /* flag symbols are bit numbers */
50 #define FLAG_REQUESTED 0
52 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
53 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
54 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
55 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
56 #define FLAG_ACTIVE_LOW 6 /* value has active low */
57 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
58 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
59 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
61 #define ID_SHIFT 16 /* add new flags before this one */
63 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
64 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
66 #ifdef CONFIG_DEBUG_FS
70 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
72 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
74 static DEFINE_MUTEX(gpio_lookup_lock
);
75 static LIST_HEAD(gpio_lookup_list
);
76 static LIST_HEAD(gpio_chips
);
78 #ifdef CONFIG_GPIO_SYSFS
79 static DEFINE_IDR(dirent_idr
);
82 static int gpiod_request(struct gpio_desc
*desc
, const char *label
);
83 static void gpiod_free(struct gpio_desc
*desc
);
85 #ifdef CONFIG_DEBUG_FS
86 #define gpiod_emerg(desc, fmt, ...) \
87 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
89 #define gpiod_crit(desc, fmt, ...) \
90 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
92 #define gpiod_err(desc, fmt, ...) \
93 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
95 #define gpiod_warn(desc, fmt, ...) \
96 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
98 #define gpiod_info(desc, fmt, ...) \
99 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
101 #define gpiod_dbg(desc, fmt, ...) \
102 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
105 #define gpiod_emerg(desc, fmt, ...) \
106 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
107 #define gpiod_crit(desc, fmt, ...) \
108 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109 #define gpiod_err(desc, fmt, ...) \
110 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111 #define gpiod_warn(desc, fmt, ...) \
112 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113 #define gpiod_info(desc, fmt, ...) \
114 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115 #define gpiod_dbg(desc, fmt, ...) \
116 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
119 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
121 #ifdef CONFIG_DEBUG_FS
127 * Return the GPIO number of the passed descriptor relative to its chip
129 static int gpio_chip_hwgpio(const struct gpio_desc
*desc
)
131 return desc
- &desc
->chip
->desc
[0];
135 * Convert a GPIO number to its descriptor
137 struct gpio_desc
*gpio_to_desc(unsigned gpio
)
139 if (WARN(!gpio_is_valid(gpio
), "invalid GPIO %d\n", gpio
))
142 return &gpio_desc
[gpio
];
144 EXPORT_SYMBOL_GPL(gpio_to_desc
);
147 * Convert an offset on a certain chip to a corresponding descriptor
149 static struct gpio_desc
*gpiochip_offset_to_desc(struct gpio_chip
*chip
,
152 unsigned int gpio
= chip
->base
+ offset
;
154 return gpio_to_desc(gpio
);
158 * Convert a GPIO descriptor to the integer namespace.
159 * This should disappear in the future but is needed since we still
160 * use GPIO numbers for error messages and sysfs nodes
162 int desc_to_gpio(const struct gpio_desc
*desc
)
164 return desc
- &gpio_desc
[0];
166 EXPORT_SYMBOL_GPL(desc_to_gpio
);
169 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
170 * when setting direction, and otherwise illegal. Until board setup code
171 * and drivers use explicit requests everywhere (which won't happen when
172 * those calls have no teeth) we can't avoid autorequesting. This nag
173 * message should motivate switching to explicit requests... so should
174 * the weaker cleanup after faults, compared to gpio_request().
176 * NOTE: the autorequest mechanism is going away; at this point it's
177 * only "legal" in the sense that (old) code using it won't break yet,
178 * but instead only triggers a WARN() stack dump.
180 static int gpio_ensure_requested(struct gpio_desc
*desc
)
182 const struct gpio_chip
*chip
= desc
->chip
;
183 const int gpio
= desc_to_gpio(desc
);
185 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
186 "autorequest GPIO-%d\n", gpio
)) {
187 if (!try_module_get(chip
->owner
)) {
188 pr_err("GPIO-%d: module can't be gotten \n", gpio
);
189 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
193 desc_set_label(desc
, "[auto]");
194 /* caller must chip->request() w/o spinlock */
202 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
203 * @desc: descriptor to return the chip of
205 struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
207 return desc
? desc
->chip
: NULL
;
209 EXPORT_SYMBOL_GPL(gpiod_to_chip
);
211 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
212 static int gpiochip_find_base(int ngpio
)
214 struct gpio_chip
*chip
;
215 int base
= ARCH_NR_GPIOS
- ngpio
;
217 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
218 /* found a free space? */
219 if (chip
->base
+ chip
->ngpio
<= base
)
222 /* nope, check the space right before the chip */
223 base
= chip
->base
- ngpio
;
226 if (gpio_is_valid(base
)) {
227 pr_debug("%s: found new base at %d\n", __func__
, base
);
230 pr_err("%s: cannot find free range\n", __func__
);
236 * gpiod_get_direction - return the current direction of a GPIO
237 * @desc: GPIO to get the direction of
239 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
241 * This function may sleep if gpiod_cansleep() is true.
243 int gpiod_get_direction(const struct gpio_desc
*desc
)
245 struct gpio_chip
*chip
;
247 int status
= -EINVAL
;
249 chip
= gpiod_to_chip(desc
);
250 offset
= gpio_chip_hwgpio(desc
);
252 if (!chip
->get_direction
)
255 status
= chip
->get_direction(chip
, offset
);
257 /* GPIOF_DIR_IN, or other positive */
259 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
260 * so it does not affect constness per se */
261 clear_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
265 set_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
269 EXPORT_SYMBOL_GPL(gpiod_get_direction
);
271 #ifdef CONFIG_GPIO_SYSFS
273 /* lock protects against unexport_gpio() being called while
274 * sysfs files are active.
276 static DEFINE_MUTEX(sysfs_lock
);
279 * /sys/class/gpio/gpioN... only for GPIOs that are exported
281 * * MAY BE OMITTED if kernel won't allow direction changes
282 * * is read/write as "in" or "out"
283 * * may also be written as "high" or "low", initializing
284 * output value as specified ("out" implies "low")
286 * * always readable, subject to hardware behavior
287 * * may be writable, as zero/nonzero
289 * * configures behavior of poll(2) on /value
290 * * available only if pin can generate IRQs on input
291 * * is read/write as "none", "falling", "rising", or "both"
293 * * configures polarity of /value
294 * * is read/write as zero/nonzero
295 * * also affects existing and subsequent "falling" and "rising"
296 * /edge configuration
299 static ssize_t
gpio_direction_show(struct device
*dev
,
300 struct device_attribute
*attr
, char *buf
)
302 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
305 mutex_lock(&sysfs_lock
);
307 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
310 gpiod_get_direction(desc
);
311 status
= sprintf(buf
, "%s\n",
312 test_bit(FLAG_IS_OUT
, &desc
->flags
)
316 mutex_unlock(&sysfs_lock
);
320 static ssize_t
gpio_direction_store(struct device
*dev
,
321 struct device_attribute
*attr
, const char *buf
, size_t size
)
323 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
326 mutex_lock(&sysfs_lock
);
328 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
330 else if (sysfs_streq(buf
, "high"))
331 status
= gpiod_direction_output(desc
, 1);
332 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
333 status
= gpiod_direction_output(desc
, 0);
334 else if (sysfs_streq(buf
, "in"))
335 status
= gpiod_direction_input(desc
);
339 mutex_unlock(&sysfs_lock
);
340 return status
? : size
;
343 static /* const */ DEVICE_ATTR(direction
, 0644,
344 gpio_direction_show
, gpio_direction_store
);
346 static ssize_t
gpio_value_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
349 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
352 mutex_lock(&sysfs_lock
);
354 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
357 status
= sprintf(buf
, "%d\n", gpiod_get_value_cansleep(desc
));
359 mutex_unlock(&sysfs_lock
);
363 static ssize_t
gpio_value_store(struct device
*dev
,
364 struct device_attribute
*attr
, const char *buf
, size_t size
)
366 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
369 mutex_lock(&sysfs_lock
);
371 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
373 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
378 status
= kstrtol(buf
, 0, &value
);
380 gpiod_set_value_cansleep(desc
, value
);
385 mutex_unlock(&sysfs_lock
);
389 static const DEVICE_ATTR(value
, 0644,
390 gpio_value_show
, gpio_value_store
);
392 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
394 struct sysfs_dirent
*value_sd
= priv
;
396 sysfs_notify_dirent(value_sd
);
400 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
401 unsigned long gpio_flags
)
403 struct sysfs_dirent
*value_sd
;
404 unsigned long irq_flags
;
407 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
410 irq
= gpiod_to_irq(desc
);
414 id
= desc
->flags
>> ID_SHIFT
;
415 value_sd
= idr_find(&dirent_idr
, id
);
417 free_irq(irq
, value_sd
);
419 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
422 gpiod_unlock_as_irq(desc
);
427 irq_flags
= IRQF_SHARED
;
428 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
429 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
430 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
431 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
432 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
433 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
436 value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, "value");
442 ret
= idr_alloc(&dirent_idr
, value_sd
, 1, 0, GFP_KERNEL
);
447 desc
->flags
&= GPIO_FLAGS_MASK
;
448 desc
->flags
|= (unsigned long)id
<< ID_SHIFT
;
450 if (desc
->flags
>> ID_SHIFT
!= id
) {
456 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
457 "gpiolib", value_sd
);
461 ret
= gpiod_lock_as_irq(desc
);
463 gpiod_warn(desc
, "failed to flag the GPIO for IRQ\n");
467 desc
->flags
|= gpio_flags
;
471 idr_remove(&dirent_idr
, id
);
472 desc
->flags
&= GPIO_FLAGS_MASK
;
480 static const struct {
483 } trigger_types
[] = {
485 { "falling", BIT(FLAG_TRIG_FALL
) },
486 { "rising", BIT(FLAG_TRIG_RISE
) },
487 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
490 static ssize_t
gpio_edge_show(struct device
*dev
,
491 struct device_attribute
*attr
, char *buf
)
493 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
496 mutex_lock(&sysfs_lock
);
498 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
504 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
505 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
506 == trigger_types
[i
].flags
) {
507 status
= sprintf(buf
, "%s\n",
508 trigger_types
[i
].name
);
513 mutex_unlock(&sysfs_lock
);
517 static ssize_t
gpio_edge_store(struct device
*dev
,
518 struct device_attribute
*attr
, const char *buf
, size_t size
)
520 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
524 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
525 if (sysfs_streq(trigger_types
[i
].name
, buf
))
530 mutex_lock(&sysfs_lock
);
532 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
535 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
540 mutex_unlock(&sysfs_lock
);
545 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
547 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
552 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
556 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
558 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
560 /* reconfigure poll(2) support if enabled on one edge only */
561 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
562 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
563 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
565 gpio_setup_irq(desc
, dev
, 0);
566 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
572 static ssize_t
gpio_active_low_show(struct device
*dev
,
573 struct device_attribute
*attr
, char *buf
)
575 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
578 mutex_lock(&sysfs_lock
);
580 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
583 status
= sprintf(buf
, "%d\n",
584 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
586 mutex_unlock(&sysfs_lock
);
591 static ssize_t
gpio_active_low_store(struct device
*dev
,
592 struct device_attribute
*attr
, const char *buf
, size_t size
)
594 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
597 mutex_lock(&sysfs_lock
);
599 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
604 status
= kstrtol(buf
, 0, &value
);
606 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
609 mutex_unlock(&sysfs_lock
);
611 return status
? : size
;
614 static const DEVICE_ATTR(active_low
, 0644,
615 gpio_active_low_show
, gpio_active_low_store
);
617 static const struct attribute
*gpio_attrs
[] = {
618 &dev_attr_value
.attr
,
619 &dev_attr_active_low
.attr
,
623 static const struct attribute_group gpio_attr_group
= {
624 .attrs
= (struct attribute
**) gpio_attrs
,
628 * /sys/class/gpio/gpiochipN/
629 * /base ... matching gpio_chip.base (N)
630 * /label ... matching gpio_chip.label
631 * /ngpio ... matching gpio_chip.ngpio
634 static ssize_t
chip_base_show(struct device
*dev
,
635 struct device_attribute
*attr
, char *buf
)
637 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
639 return sprintf(buf
, "%d\n", chip
->base
);
641 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
643 static ssize_t
chip_label_show(struct device
*dev
,
644 struct device_attribute
*attr
, char *buf
)
646 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
648 return sprintf(buf
, "%s\n", chip
->label
? : "");
650 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
652 static ssize_t
chip_ngpio_show(struct device
*dev
,
653 struct device_attribute
*attr
, char *buf
)
655 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
657 return sprintf(buf
, "%u\n", chip
->ngpio
);
659 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
661 static const struct attribute
*gpiochip_attrs
[] = {
663 &dev_attr_label
.attr
,
664 &dev_attr_ngpio
.attr
,
668 static const struct attribute_group gpiochip_attr_group
= {
669 .attrs
= (struct attribute
**) gpiochip_attrs
,
673 * /sys/class/gpio/export ... write-only
674 * integer N ... number of GPIO to export (full access)
675 * /sys/class/gpio/unexport ... write-only
676 * integer N ... number of GPIO to unexport
678 static ssize_t
export_store(struct class *class,
679 struct class_attribute
*attr
,
680 const char *buf
, size_t len
)
683 struct gpio_desc
*desc
;
686 status
= kstrtol(buf
, 0, &gpio
);
690 desc
= gpio_to_desc(gpio
);
691 /* reject invalid GPIOs */
693 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
697 /* No extra locking here; FLAG_SYSFS just signifies that the
698 * request and export were done by on behalf of userspace, so
699 * they may be undone on its behalf too.
702 status
= gpiod_request(desc
, "sysfs");
704 if (status
== -EPROBE_DEFER
)
708 status
= gpiod_export(desc
, true);
712 set_bit(FLAG_SYSFS
, &desc
->flags
);
716 pr_debug("%s: status %d\n", __func__
, status
);
717 return status
? : len
;
720 static ssize_t
unexport_store(struct class *class,
721 struct class_attribute
*attr
,
722 const char *buf
, size_t len
)
725 struct gpio_desc
*desc
;
728 status
= kstrtol(buf
, 0, &gpio
);
732 desc
= gpio_to_desc(gpio
);
733 /* reject bogus commands (gpio_unexport ignores them) */
735 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
741 /* No extra locking here; FLAG_SYSFS just signifies that the
742 * request and export were done by on behalf of userspace, so
743 * they may be undone on its behalf too.
745 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
)) {
751 pr_debug("%s: status %d\n", __func__
, status
);
752 return status
? : len
;
755 static struct class_attribute gpio_class_attrs
[] = {
756 __ATTR(export
, 0200, NULL
, export_store
),
757 __ATTR(unexport
, 0200, NULL
, unexport_store
),
761 static struct class gpio_class
= {
763 .owner
= THIS_MODULE
,
765 .class_attrs
= gpio_class_attrs
,
770 * gpiod_export - export a GPIO through sysfs
771 * @gpio: gpio to make available, already requested
772 * @direction_may_change: true if userspace may change gpio direction
773 * Context: arch_initcall or later
775 * When drivers want to make a GPIO accessible to userspace after they
776 * have requested it -- perhaps while debugging, or as part of their
777 * public interface -- they may use this routine. If the GPIO can
778 * change direction (some can't) and the caller allows it, userspace
779 * will see "direction" sysfs attribute which may be used to change
780 * the gpio's direction. A "value" attribute will always be provided.
782 * Returns zero on success, else an error.
784 int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
)
788 const char *ioname
= NULL
;
792 /* can't export until sysfs is available ... */
794 pr_debug("%s: called too early!\n", __func__
);
799 pr_debug("%s: invalid gpio descriptor\n", __func__
);
803 mutex_lock(&sysfs_lock
);
805 spin_lock_irqsave(&gpio_lock
, flags
);
806 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
807 test_bit(FLAG_EXPORT
, &desc
->flags
)) {
808 spin_unlock_irqrestore(&gpio_lock
, flags
);
809 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
810 __func__
, desc_to_gpio(desc
),
811 test_bit(FLAG_REQUESTED
, &desc
->flags
),
812 test_bit(FLAG_EXPORT
, &desc
->flags
));
817 if (!desc
->chip
->direction_input
|| !desc
->chip
->direction_output
)
818 direction_may_change
= false;
819 spin_unlock_irqrestore(&gpio_lock
, flags
);
821 offset
= gpio_chip_hwgpio(desc
);
822 if (desc
->chip
->names
&& desc
->chip
->names
[offset
])
823 ioname
= desc
->chip
->names
[offset
];
825 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
826 desc
, ioname
? ioname
: "gpio%u",
829 status
= PTR_ERR(dev
);
833 status
= sysfs_create_group(&dev
->kobj
, &gpio_attr_group
);
835 goto fail_unregister_device
;
837 if (direction_may_change
) {
838 status
= device_create_file(dev
, &dev_attr_direction
);
840 goto fail_unregister_device
;
843 if (gpiod_to_irq(desc
) >= 0 && (direction_may_change
||
844 !test_bit(FLAG_IS_OUT
, &desc
->flags
))) {
845 status
= device_create_file(dev
, &dev_attr_edge
);
847 goto fail_unregister_device
;
850 set_bit(FLAG_EXPORT
, &desc
->flags
);
851 mutex_unlock(&sysfs_lock
);
854 fail_unregister_device
:
855 device_unregister(dev
);
857 mutex_unlock(&sysfs_lock
);
858 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
862 EXPORT_SYMBOL_GPL(gpiod_export
);
864 static int match_export(struct device
*dev
, const void *data
)
866 return dev_get_drvdata(dev
) == data
;
870 * gpiod_export_link - create a sysfs link to an exported GPIO node
871 * @dev: device under which to create symlink
872 * @name: name of the symlink
873 * @gpio: gpio to create symlink to, already exported
875 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
876 * node. Caller is responsible for unlinking.
878 * Returns zero on success, else an error.
880 int gpiod_export_link(struct device
*dev
, const char *name
,
881 struct gpio_desc
*desc
)
883 int status
= -EINVAL
;
886 pr_warn("%s: invalid GPIO\n", __func__
);
890 mutex_lock(&sysfs_lock
);
892 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
895 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
897 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
904 mutex_unlock(&sysfs_lock
);
907 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
912 EXPORT_SYMBOL_GPL(gpiod_export_link
);
915 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
916 * @gpio: gpio to change
917 * @value: non-zero to use active low, i.e. inverted values
919 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
920 * The GPIO does not have to be exported yet. If poll(2) support has
921 * been enabled for either rising or falling edge, it will be
922 * reconfigured to follow the new polarity.
924 * Returns zero on success, else an error.
926 int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
)
928 struct device
*dev
= NULL
;
929 int status
= -EINVAL
;
932 pr_warn("%s: invalid GPIO\n", __func__
);
936 mutex_lock(&sysfs_lock
);
938 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
939 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
946 status
= sysfs_set_active_low(desc
, dev
, value
);
949 mutex_unlock(&sysfs_lock
);
952 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
957 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low
);
960 * gpiod_unexport - reverse effect of gpio_export()
961 * @gpio: gpio to make unavailable
963 * This is implicit on gpio_free().
965 void gpiod_unexport(struct gpio_desc
*desc
)
968 struct device
*dev
= NULL
;
971 pr_warn("%s: invalid GPIO\n", __func__
);
975 mutex_lock(&sysfs_lock
);
977 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
979 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
981 gpio_setup_irq(desc
, dev
, 0);
982 clear_bit(FLAG_EXPORT
, &desc
->flags
);
987 mutex_unlock(&sysfs_lock
);
990 device_unregister(dev
);
995 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
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
);
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("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
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("gpiochip_add: gpios %d..%d (%s) failed to register\n",
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 #ifdef CONFIG_PINCTRL
1313 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1314 * @chip: the gpiochip to add the range for
1315 * @pinctrl: the dev_name() of the pin controller to map to
1316 * @gpio_offset: the start offset in the current gpio_chip number space
1317 * @pin_group: name of the pin group inside the pin controller
1319 int gpiochip_add_pingroup_range(struct gpio_chip
*chip
,
1320 struct pinctrl_dev
*pctldev
,
1321 unsigned int gpio_offset
, const char *pin_group
)
1323 struct gpio_pin_range
*pin_range
;
1326 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1328 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1333 /* Use local offset as range ID */
1334 pin_range
->range
.id
= gpio_offset
;
1335 pin_range
->range
.gc
= chip
;
1336 pin_range
->range
.name
= chip
->label
;
1337 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1338 pin_range
->pctldev
= pctldev
;
1340 ret
= pinctrl_get_group_pins(pctldev
, pin_group
,
1341 &pin_range
->range
.pins
,
1342 &pin_range
->range
.npins
);
1346 pinctrl_add_gpio_range(pctldev
, &pin_range
->range
);
1348 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n",
1349 chip
->label
, gpio_offset
,
1350 gpio_offset
+ pin_range
->range
.npins
- 1,
1351 pinctrl_dev_get_devname(pctldev
), pin_group
);
1353 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1357 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range
);
1360 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1361 * @chip: the gpiochip to add the range for
1362 * @pinctrl_name: the dev_name() of the pin controller to map to
1363 * @gpio_offset: the start offset in the current gpio_chip number space
1364 * @pin_offset: the start offset in the pin controller number space
1365 * @npins: the number of pins from the offset of each pin space (GPIO and
1366 * pin controller) to accumulate in this range
1368 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1369 unsigned int gpio_offset
, unsigned int pin_offset
,
1372 struct gpio_pin_range
*pin_range
;
1375 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1377 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1382 /* Use local offset as range ID */
1383 pin_range
->range
.id
= gpio_offset
;
1384 pin_range
->range
.gc
= chip
;
1385 pin_range
->range
.name
= chip
->label
;
1386 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1387 pin_range
->range
.pin_base
= pin_offset
;
1388 pin_range
->range
.npins
= npins
;
1389 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
1391 if (IS_ERR(pin_range
->pctldev
)) {
1392 ret
= PTR_ERR(pin_range
->pctldev
);
1393 pr_err("%s: GPIO chip: could not create pin range\n",
1398 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1399 chip
->label
, gpio_offset
, gpio_offset
+ npins
- 1,
1401 pin_offset
, pin_offset
+ npins
- 1);
1403 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1407 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1410 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1411 * @chip: the chip to remove all the mappings for
1413 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1415 struct gpio_pin_range
*pin_range
, *tmp
;
1417 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
1418 list_del(&pin_range
->node
);
1419 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1424 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1426 #endif /* CONFIG_PINCTRL */
1428 /* These "optional" allocation calls help prevent drivers from stomping
1429 * on each other, and help provide better diagnostics in debugfs.
1430 * They're called even less than the "set direction" calls.
1432 static int gpiod_request(struct gpio_desc
*desc
, const char *label
)
1434 struct gpio_chip
*chip
;
1435 int status
= -EPROBE_DEFER
;
1436 unsigned long flags
;
1439 pr_warn("%s: invalid GPIO\n", __func__
);
1443 spin_lock_irqsave(&gpio_lock
, flags
);
1449 if (!try_module_get(chip
->owner
))
1452 /* NOTE: gpio_request() can be called in early boot,
1453 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1456 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1457 desc_set_label(desc
, label
? : "?");
1461 module_put(chip
->owner
);
1465 if (chip
->request
) {
1466 /* chip->request may sleep */
1467 spin_unlock_irqrestore(&gpio_lock
, flags
);
1468 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
1469 spin_lock_irqsave(&gpio_lock
, flags
);
1472 desc_set_label(desc
, NULL
);
1473 module_put(chip
->owner
);
1474 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1478 if (chip
->get_direction
) {
1479 /* chip->get_direction may sleep */
1480 spin_unlock_irqrestore(&gpio_lock
, flags
);
1481 gpiod_get_direction(desc
);
1482 spin_lock_irqsave(&gpio_lock
, flags
);
1486 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1487 desc_to_gpio(desc
), label
? : "?", status
);
1488 spin_unlock_irqrestore(&gpio_lock
, flags
);
1492 int gpio_request(unsigned gpio
, const char *label
)
1494 return gpiod_request(gpio_to_desc(gpio
), label
);
1496 EXPORT_SYMBOL_GPL(gpio_request
);
1498 static void gpiod_free(struct gpio_desc
*desc
)
1500 unsigned long flags
;
1501 struct gpio_chip
*chip
;
1506 WARN_ON(extra_checks
);
1510 gpiod_unexport(desc
);
1512 spin_lock_irqsave(&gpio_lock
, flags
);
1515 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1517 spin_unlock_irqrestore(&gpio_lock
, flags
);
1518 might_sleep_if(chip
->can_sleep
);
1519 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1520 spin_lock_irqsave(&gpio_lock
, flags
);
1522 desc_set_label(desc
, NULL
);
1523 module_put(desc
->chip
->owner
);
1524 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1525 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1526 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1527 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1529 WARN_ON(extra_checks
);
1531 spin_unlock_irqrestore(&gpio_lock
, flags
);
1534 void gpio_free(unsigned gpio
)
1536 gpiod_free(gpio_to_desc(gpio
));
1538 EXPORT_SYMBOL_GPL(gpio_free
);
1541 * gpio_request_one - request a single GPIO with initial configuration
1542 * @gpio: the GPIO number
1543 * @flags: GPIO configuration as specified by GPIOF_*
1544 * @label: a literal description string of this GPIO
1546 int gpio_request_one(unsigned gpio
, unsigned long flags
, const char *label
)
1548 struct gpio_desc
*desc
;
1551 desc
= gpio_to_desc(gpio
);
1553 err
= gpiod_request(desc
, label
);
1557 if (flags
& GPIOF_OPEN_DRAIN
)
1558 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1560 if (flags
& GPIOF_OPEN_SOURCE
)
1561 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1563 if (flags
& GPIOF_DIR_IN
)
1564 err
= gpiod_direction_input(desc
);
1566 err
= gpiod_direction_output(desc
,
1567 (flags
& GPIOF_INIT_HIGH
) ? 1 : 0);
1572 if (flags
& GPIOF_EXPORT
) {
1573 err
= gpiod_export(desc
, flags
& GPIOF_EXPORT_CHANGEABLE
);
1584 EXPORT_SYMBOL_GPL(gpio_request_one
);
1587 * gpio_request_array - request multiple GPIOs in a single call
1588 * @array: array of the 'struct gpio'
1589 * @num: how many GPIOs in the array
1591 int gpio_request_array(const struct gpio
*array
, size_t num
)
1595 for (i
= 0; i
< num
; i
++, array
++) {
1596 err
= gpio_request_one(array
->gpio
, array
->flags
, array
->label
);
1604 gpio_free((--array
)->gpio
);
1607 EXPORT_SYMBOL_GPL(gpio_request_array
);
1610 * gpio_free_array - release multiple GPIOs in a single call
1611 * @array: array of the 'struct gpio'
1612 * @num: how many GPIOs in the array
1614 void gpio_free_array(const struct gpio
*array
, size_t num
)
1617 gpio_free((array
++)->gpio
);
1619 EXPORT_SYMBOL_GPL(gpio_free_array
);
1622 * gpiochip_is_requested - return string iff signal was requested
1623 * @chip: controller managing the signal
1624 * @offset: of signal within controller's 0..(ngpio - 1) range
1626 * Returns NULL if the GPIO is not currently requested, else a string.
1627 * If debugfs support is enabled, the string returned is the label passed
1628 * to gpio_request(); otherwise it is a meaningless constant.
1630 * This function is for use by GPIO controller drivers. The label can
1631 * help with diagnostics, and knowing that the signal is used as a GPIO
1632 * can help avoid accidentally multiplexing it to another controller.
1634 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1636 struct gpio_desc
*desc
;
1638 if (!GPIO_OFFSET_VALID(chip
, offset
))
1641 desc
= &chip
->desc
[offset
];
1643 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1645 #ifdef CONFIG_DEBUG_FS
1651 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1654 /* Drivers MUST set GPIO direction before making get/set calls. In
1655 * some cases this is done in early boot, before IRQs are enabled.
1657 * As a rule these aren't called more than once (except for drivers
1658 * using the open-drain emulation idiom) so these are natural places
1659 * to accumulate extra debugging checks. Note that we can't (yet)
1660 * rely on gpio_request() having been called beforehand.
1664 * gpiod_direction_input - set the GPIO direction to input
1665 * @desc: GPIO to set to input
1667 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1668 * be called safely on it.
1670 * Return 0 in case of success, else an error code.
1672 int gpiod_direction_input(struct gpio_desc
*desc
)
1674 unsigned long flags
;
1675 struct gpio_chip
*chip
;
1676 int status
= -EINVAL
;
1679 if (!desc
|| !desc
->chip
) {
1680 pr_warn("%s: invalid GPIO\n", __func__
);
1685 if (!chip
->get
|| !chip
->direction_input
) {
1687 "%s: missing get() or direction_input() operations\n",
1692 spin_lock_irqsave(&gpio_lock
, flags
);
1694 status
= gpio_ensure_requested(desc
);
1698 /* now we know the gpio is valid and chip won't vanish */
1700 spin_unlock_irqrestore(&gpio_lock
, flags
);
1702 might_sleep_if(chip
->can_sleep
);
1704 offset
= gpio_chip_hwgpio(desc
);
1706 status
= chip
->request(chip
, offset
);
1708 gpiod_dbg(desc
, "chip request fail, %d\n", status
);
1709 /* and it's not available to anyone else ...
1710 * gpio_request() is the fully clean solution.
1716 status
= chip
->direction_input(chip
, offset
);
1718 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1720 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1724 spin_unlock_irqrestore(&gpio_lock
, flags
);
1726 gpiod_dbg(desc
, "%s status %d\n", __func__
, status
);
1729 EXPORT_SYMBOL_GPL(gpiod_direction_input
);
1732 * gpiod_direction_output - set the GPIO direction to input
1733 * @desc: GPIO to set to output
1734 * @value: initial output value of the GPIO
1736 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1737 * be called safely on it. The initial value of the output must be specified.
1739 * Return 0 in case of success, else an error code.
1741 int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1743 unsigned long flags
;
1744 struct gpio_chip
*chip
;
1745 int status
= -EINVAL
;
1748 if (!desc
|| !desc
->chip
) {
1749 pr_warn("%s: invalid GPIO\n", __func__
);
1753 /* GPIOs used for IRQs shall not be set as output */
1754 if (test_bit(FLAG_USED_AS_IRQ
, &desc
->flags
)) {
1756 "%s: tried to set a GPIO tied to an IRQ as output\n",
1761 /* Open drain pin should not be driven to 1 */
1762 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1763 return gpiod_direction_input(desc
);
1765 /* Open source pin should not be driven to 0 */
1766 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1767 return gpiod_direction_input(desc
);
1770 if (!chip
->set
|| !chip
->direction_output
) {
1772 "%s: missing set() or direction_output() operations\n",
1777 spin_lock_irqsave(&gpio_lock
, flags
);
1779 status
= gpio_ensure_requested(desc
);
1783 /* now we know the gpio is valid and chip won't vanish */
1785 spin_unlock_irqrestore(&gpio_lock
, flags
);
1787 might_sleep_if(chip
->can_sleep
);
1789 offset
= gpio_chip_hwgpio(desc
);
1791 status
= chip
->request(chip
, offset
);
1793 gpiod_dbg(desc
, "chip request fail, %d\n", status
);
1794 /* and it's not available to anyone else ...
1795 * gpio_request() is the fully clean solution.
1801 status
= chip
->direction_output(chip
, offset
, value
);
1803 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1804 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1805 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1809 spin_unlock_irqrestore(&gpio_lock
, flags
);
1811 gpiod_dbg(desc
, "%s: gpio status %d\n", __func__
, status
);
1814 EXPORT_SYMBOL_GPL(gpiod_direction_output
);
1817 * gpiod_set_debounce - sets @debounce time for a @gpio
1818 * @gpio: the gpio to set debounce time
1819 * @debounce: debounce time is microseconds
1821 * returns -ENOTSUPP if the controller does not support setting
1824 int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1826 unsigned long flags
;
1827 struct gpio_chip
*chip
;
1828 int status
= -EINVAL
;
1831 if (!desc
|| !desc
->chip
) {
1832 pr_warn("%s: invalid GPIO\n", __func__
);
1837 if (!chip
->set
|| !chip
->set_debounce
) {
1839 "%s: missing set() or set_debounce() operations\n",
1844 spin_lock_irqsave(&gpio_lock
, flags
);
1846 status
= gpio_ensure_requested(desc
);
1850 /* now we know the gpio is valid and chip won't vanish */
1852 spin_unlock_irqrestore(&gpio_lock
, flags
);
1854 might_sleep_if(chip
->can_sleep
);
1856 offset
= gpio_chip_hwgpio(desc
);
1857 return chip
->set_debounce(chip
, offset
, debounce
);
1860 spin_unlock_irqrestore(&gpio_lock
, flags
);
1862 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
1866 EXPORT_SYMBOL_GPL(gpiod_set_debounce
);
1869 * gpiod_is_active_low - test whether a GPIO is active-low or not
1870 * @desc: the gpio descriptor to test
1872 * Returns 1 if the GPIO is active-low, 0 otherwise.
1874 int gpiod_is_active_low(const struct gpio_desc
*desc
)
1876 return test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1878 EXPORT_SYMBOL_GPL(gpiod_is_active_low
);
1880 /* I/O calls are only valid after configuration completed; the relevant
1881 * "is this a valid GPIO" error checks should already have been done.
1883 * "Get" operations are often inlinable as reading a pin value register,
1884 * and masking the relevant bit in that register.
1886 * When "set" operations are inlinable, they involve writing that mask to
1887 * one register to set a low value, or a different register to set it high.
1888 * Otherwise locking is needed, so there may be little value to inlining.
1890 *------------------------------------------------------------------------
1892 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1893 * have requested the GPIO. That can include implicit requesting by
1894 * a direction setting call. Marking a gpio as requested locks its chip
1895 * in memory, guaranteeing that these table lookups need no more locking
1896 * and that gpiochip_remove() will fail.
1898 * REVISIT when debugging, consider adding some instrumentation to ensure
1899 * that the GPIO was actually requested.
1902 static int _gpiod_get_raw_value(const struct gpio_desc
*desc
)
1904 struct gpio_chip
*chip
;
1909 offset
= gpio_chip_hwgpio(desc
);
1910 value
= chip
->get
? chip
->get(chip
, offset
) : 0;
1911 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1916 * gpiod_get_raw_value() - return a gpio's raw value
1917 * @desc: gpio whose value will be returned
1919 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1920 * its ACTIVE_LOW status.
1922 * This function should be called from contexts where we cannot sleep, and will
1923 * complain if the GPIO chip functions potentially sleep.
1925 int gpiod_get_raw_value(const struct gpio_desc
*desc
)
1929 /* Should be using gpio_get_value_cansleep() */
1930 WARN_ON(desc
->chip
->can_sleep
);
1931 return _gpiod_get_raw_value(desc
);
1933 EXPORT_SYMBOL_GPL(gpiod_get_raw_value
);
1936 * gpiod_get_value() - return a gpio's value
1937 * @desc: gpio whose value will be returned
1939 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1942 * This function should be called from contexts where we cannot sleep, and will
1943 * complain if the GPIO chip functions potentially sleep.
1945 int gpiod_get_value(const struct gpio_desc
*desc
)
1950 /* Should be using gpio_get_value_cansleep() */
1951 WARN_ON(desc
->chip
->can_sleep
);
1953 value
= _gpiod_get_raw_value(desc
);
1954 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
1959 EXPORT_SYMBOL_GPL(gpiod_get_value
);
1962 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1963 * @desc: gpio descriptor whose state need to be set.
1964 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1966 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, int value
)
1969 struct gpio_chip
*chip
= desc
->chip
;
1970 int offset
= gpio_chip_hwgpio(desc
);
1973 err
= chip
->direction_input(chip
, offset
);
1975 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1977 err
= chip
->direction_output(chip
, offset
, 0);
1979 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1981 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1984 "%s: Error in set_value for open drain err %d\n",
1989 * _gpio_set_open_source_value() - Set the open source gpio's value.
1990 * @desc: gpio descriptor whose state need to be set.
1991 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1993 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, int value
)
1996 struct gpio_chip
*chip
= desc
->chip
;
1997 int offset
= gpio_chip_hwgpio(desc
);
2000 err
= chip
->direction_output(chip
, offset
, 1);
2002 set_bit(FLAG_IS_OUT
, &desc
->flags
);
2004 err
= chip
->direction_input(chip
, offset
);
2006 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
2008 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
2011 "%s: Error in set_value for open source err %d\n",
2015 static void _gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2017 struct gpio_chip
*chip
;
2020 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
2021 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
2022 _gpio_set_open_drain_value(desc
, value
);
2023 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
2024 _gpio_set_open_source_value(desc
, value
);
2026 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
2030 * gpiod_set_raw_value() - assign a gpio's raw value
2031 * @desc: gpio whose value will be assigned
2032 * @value: value to assign
2034 * Set the raw value of the GPIO, i.e. the value of its physical line without
2035 * regard for its ACTIVE_LOW status.
2037 * This function should be called from contexts where we cannot sleep, and will
2038 * complain if the GPIO chip functions potentially sleep.
2040 void gpiod_set_raw_value(struct gpio_desc
*desc
, int value
)
2044 /* Should be using gpio_set_value_cansleep() */
2045 WARN_ON(desc
->chip
->can_sleep
);
2046 _gpiod_set_raw_value(desc
, value
);
2048 EXPORT_SYMBOL_GPL(gpiod_set_raw_value
);
2051 * gpiod_set_value() - assign a gpio's value
2052 * @desc: gpio whose value will be assigned
2053 * @value: value to assign
2055 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2058 * This function should be called from contexts where we cannot sleep, and will
2059 * complain if the GPIO chip functions potentially sleep.
2061 void gpiod_set_value(struct gpio_desc
*desc
, int value
)
2065 /* Should be using gpio_set_value_cansleep() */
2066 WARN_ON(desc
->chip
->can_sleep
);
2067 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2069 _gpiod_set_raw_value(desc
, value
);
2071 EXPORT_SYMBOL_GPL(gpiod_set_value
);
2074 * gpiod_cansleep() - report whether gpio value access may sleep
2075 * @desc: gpio to check
2078 int gpiod_cansleep(const struct gpio_desc
*desc
)
2082 return desc
->chip
->can_sleep
;
2084 EXPORT_SYMBOL_GPL(gpiod_cansleep
);
2087 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2088 * @desc: gpio whose IRQ will be returned (already requested)
2090 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2093 int gpiod_to_irq(const struct gpio_desc
*desc
)
2095 struct gpio_chip
*chip
;
2101 offset
= gpio_chip_hwgpio(desc
);
2102 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
2104 EXPORT_SYMBOL_GPL(gpiod_to_irq
);
2107 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2108 * @gpio: the GPIO line to lock as used for IRQ
2110 * This is used directly by GPIO drivers that want to lock down
2111 * a certain GPIO line to be used as IRQs, for example in the
2112 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2113 * of its irq_chip implementation if the GPIO is known from that
2116 int gpiod_lock_as_irq(struct gpio_desc
*desc
)
2121 if (test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
2123 "%s: tried to flag a GPIO set as output for IRQ\n",
2128 set_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
2131 EXPORT_SYMBOL_GPL(gpiod_lock_as_irq
);
2133 int gpio_lock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2135 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip
, offset
));
2137 EXPORT_SYMBOL_GPL(gpio_lock_as_irq
);
2140 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2141 * @gpio: the GPIO line to unlock from IRQ usage
2143 * This is used directly by GPIO drivers that want to indicate
2144 * that a certain GPIO is no longer used exclusively for IRQ.
2146 void gpiod_unlock_as_irq(struct gpio_desc
*desc
)
2151 clear_bit(FLAG_USED_AS_IRQ
, &desc
->flags
);
2153 EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq
);
2155 void gpio_unlock_as_irq(struct gpio_chip
*chip
, unsigned int offset
)
2157 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip
, offset
));
2159 EXPORT_SYMBOL_GPL(gpio_unlock_as_irq
);
2162 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2163 * @desc: gpio whose value will be returned
2165 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2166 * its ACTIVE_LOW status.
2168 * This function is to be called from contexts that can sleep.
2170 int gpiod_get_raw_value_cansleep(const struct gpio_desc
*desc
)
2172 might_sleep_if(extra_checks
);
2175 return _gpiod_get_raw_value(desc
);
2177 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep
);
2180 * gpiod_get_value_cansleep() - return a gpio's value
2181 * @desc: gpio whose value will be returned
2183 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2186 * This function is to be called from contexts that can sleep.
2188 int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
2192 might_sleep_if(extra_checks
);
2196 value
= _gpiod_get_raw_value(desc
);
2197 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2202 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep
);
2205 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2206 * @desc: gpio whose value will be assigned
2207 * @value: value to assign
2209 * Set the raw value of the GPIO, i.e. the value of its physical line without
2210 * regard for its ACTIVE_LOW status.
2212 * This function is to be called from contexts that can sleep.
2214 void gpiod_set_raw_value_cansleep(struct gpio_desc
*desc
, int value
)
2216 might_sleep_if(extra_checks
);
2219 _gpiod_set_raw_value(desc
, value
);
2221 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep
);
2224 * gpiod_set_value_cansleep() - assign a gpio's value
2225 * @desc: gpio whose value will be assigned
2226 * @value: value to assign
2228 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2231 * This function is to be called from contexts that can sleep.
2233 void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
2235 might_sleep_if(extra_checks
);
2239 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
2241 _gpiod_set_raw_value(desc
, value
);
2243 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep
);
2246 * gpiod_add_table() - register GPIO device consumers
2247 * @table: array of consumers to register
2248 * @num: number of consumers in table
2250 void gpiod_add_table(struct gpiod_lookup
*table
, size_t size
)
2252 mutex_lock(&gpio_lookup_lock
);
2255 list_add_tail(&table
->list
, &gpio_lookup_list
);
2259 mutex_unlock(&gpio_lookup_lock
);
2263 * Caller must have a acquired gpio_lookup_lock
2265 static struct gpio_chip
*find_chip_by_name(const char *name
)
2267 struct gpio_chip
*chip
= NULL
;
2269 list_for_each_entry(chip
, &gpio_lookup_list
, list
) {
2270 if (chip
->label
== NULL
)
2272 if (!strcmp(chip
->label
, name
))
2280 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2281 unsigned int idx
, unsigned long *flags
)
2283 char prop_name
[32]; /* 32 is max size of property name */
2284 enum of_gpio_flags of_flags
;
2285 struct gpio_desc
*desc
;
2288 snprintf(prop_name
, 32, "%s-gpios", con_id
);
2290 snprintf(prop_name
, 32, "gpios");
2292 desc
= of_get_named_gpiod_flags(dev
->of_node
, prop_name
, idx
,
2298 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
2299 *flags
|= GPIOF_ACTIVE_LOW
;
2304 static struct gpio_desc
*of_find_gpio(struct device
*dev
, const char *con_id
,
2305 unsigned int idx
, unsigned long *flags
)
2307 return ERR_PTR(-ENODEV
);
2311 static struct gpio_desc
*acpi_find_gpio(struct device
*dev
, const char *con_id
,
2312 unsigned int idx
, unsigned long *flags
)
2314 struct acpi_gpio_info info
;
2315 struct gpio_desc
*desc
;
2317 desc
= acpi_get_gpiod_by_index(dev
, idx
, &info
);
2321 if (info
.gpioint
&& info
.active_low
)
2322 *flags
|= GPIOF_ACTIVE_LOW
;
2327 static struct gpio_desc
*gpiod_find(struct device
*dev
, const char *con_id
,
2328 unsigned int idx
, unsigned long *flags
)
2330 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
2331 struct gpio_desc
*desc
= ERR_PTR(-ENODEV
);
2332 unsigned int match
, best
= 0;
2333 struct gpiod_lookup
*p
;
2335 mutex_lock(&gpio_lookup_lock
);
2337 list_for_each_entry(p
, &gpio_lookup_list
, list
) {
2341 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
2348 if (!con_id
|| strcmp(p
->con_id
, con_id
))
2358 struct gpio_chip
*chip
;
2360 chip
= find_chip_by_name(p
->chip_label
);
2363 dev_warn(dev
, "cannot find GPIO chip %s\n",
2368 if (chip
->ngpio
>= p
->chip_hwnum
) {
2369 dev_warn(dev
, "GPIO chip %s has %d GPIOs\n",
2370 chip
->label
, chip
->ngpio
);
2374 desc
= gpio_to_desc(chip
->base
+ p
->chip_hwnum
);
2384 mutex_unlock(&gpio_lookup_lock
);
2390 * gpio_get - obtain a GPIO for a given GPIO function
2391 * @dev: GPIO consumer
2392 * @con_id: function within the GPIO consumer
2394 * Return the GPIO descriptor corresponding to the function con_id of device
2395 * dev, or an IS_ERR() condition if an error occured.
2397 struct gpio_desc
*__must_check
gpiod_get(struct device
*dev
, const char *con_id
)
2399 return gpiod_get_index(dev
, con_id
, 0);
2401 EXPORT_SYMBOL_GPL(gpiod_get
);
2404 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2405 * @dev: GPIO consumer
2406 * @con_id: function within the GPIO consumer
2407 * @idx: index of the GPIO to obtain in the consumer
2409 * This variant of gpiod_get() allows to access GPIOs other than the first
2410 * defined one for functions that define several GPIOs.
2412 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2414 struct gpio_desc
*__must_check
gpiod_get_index(struct device
*dev
,
2418 struct gpio_desc
*desc
;
2420 unsigned long flags
= 0;
2422 dev_dbg(dev
, "GPIO lookup for consumer %s\n", con_id
);
2424 /* Using device tree? */
2425 if (IS_ENABLED(CONFIG_OF
) && dev
&& dev
->of_node
) {
2426 dev_dbg(dev
, "using device tree for GPIO lookup\n");
2427 desc
= of_find_gpio(dev
, con_id
, idx
, &flags
);
2428 } else if (IS_ENABLED(CONFIG_ACPI
) && dev
&& ACPI_HANDLE(dev
)) {
2429 dev_dbg(dev
, "using ACPI for GPIO lookup\n");
2430 desc
= acpi_find_gpio(dev
, con_id
, idx
, &flags
);
2432 dev_dbg(dev
, "using lookup tables for GPIO lookup");
2433 desc
= gpiod_find(dev
, con_id
, idx
, &flags
);
2437 dev_warn(dev
, "lookup for GPIO %s failed\n", con_id
);
2441 status
= gpiod_request(desc
, con_id
);
2444 return ERR_PTR(status
);
2446 if (flags
& GPIOF_ACTIVE_LOW
)
2447 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
2451 EXPORT_SYMBOL_GPL(gpiod_get_index
);
2454 * gpiod_put - dispose of a GPIO descriptor
2455 * @desc: GPIO descriptor to dispose of
2457 * No descriptor can be used after gpiod_put() has been called on it.
2459 void gpiod_put(struct gpio_desc
*desc
)
2463 EXPORT_SYMBOL_GPL(gpiod_put
);
2465 #ifdef CONFIG_DEBUG_FS
2467 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
2470 unsigned gpio
= chip
->base
;
2471 struct gpio_desc
*gdesc
= &chip
->desc
[0];
2475 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
2476 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
2479 gpiod_get_direction(gdesc
);
2480 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2481 is_irq
= test_bit(FLAG_USED_AS_IRQ
, &gdesc
->flags
);
2482 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s %s",
2484 is_out
? "out" : "in ",
2486 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2488 is_irq
? "IRQ" : " ");
2489 seq_printf(s
, "\n");
2493 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2495 unsigned long flags
;
2496 struct gpio_chip
*chip
= NULL
;
2497 loff_t index
= *pos
;
2501 spin_lock_irqsave(&gpio_lock
, flags
);
2502 list_for_each_entry(chip
, &gpio_chips
, list
)
2504 spin_unlock_irqrestore(&gpio_lock
, flags
);
2507 spin_unlock_irqrestore(&gpio_lock
, flags
);
2512 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2514 unsigned long flags
;
2515 struct gpio_chip
*chip
= v
;
2518 spin_lock_irqsave(&gpio_lock
, flags
);
2519 if (list_is_last(&chip
->list
, &gpio_chips
))
2522 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
2523 spin_unlock_irqrestore(&gpio_lock
, flags
);
2531 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2535 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2537 struct gpio_chip
*chip
= v
;
2540 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
2541 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
2544 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
2547 seq_printf(s
, ", %s", chip
->label
);
2548 if (chip
->can_sleep
)
2549 seq_printf(s
, ", can sleep");
2550 seq_printf(s
, ":\n");
2553 chip
->dbg_show(s
, chip
);
2555 gpiolib_dbg_show(s
, chip
);
2560 static const struct seq_operations gpiolib_seq_ops
= {
2561 .start
= gpiolib_seq_start
,
2562 .next
= gpiolib_seq_next
,
2563 .stop
= gpiolib_seq_stop
,
2564 .show
= gpiolib_seq_show
,
2567 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2569 return seq_open(file
, &gpiolib_seq_ops
);
2572 static const struct file_operations gpiolib_operations
= {
2573 .owner
= THIS_MODULE
,
2574 .open
= gpiolib_open
,
2576 .llseek
= seq_lseek
,
2577 .release
= seq_release
,
2580 static int __init
gpiolib_debugfs_init(void)
2582 /* /sys/kernel/debug/gpio */
2583 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2584 NULL
, NULL
, &gpiolib_operations
);
2587 subsys_initcall(gpiolib_debugfs_init
);
2589 #endif /* DEBUG_FS */