1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/device.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/gpio.h>
19 /* Optional implementation infrastructure for GPIO interfaces.
21 * Platforms may want to use this if they tend to use very many GPIOs
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
32 /* When debugging, extend minimal trust to callers and platform code.
33 * Also emit diagnostic messages that may help initial bringup, when
34 * board setup or driver bugs are most common.
36 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 #define extra_checks 1
41 #define extra_checks 0
44 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
45 * While any GPIO is requested, its gpio_chip is not removable;
46 * each GPIO's "requested" flag serves as a lock and refcount.
48 static DEFINE_SPINLOCK(gpio_lock
);
51 struct gpio_chip
*chip
;
53 /* flag symbols are bit numbers */
54 #define FLAG_REQUESTED 0
56 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
57 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58 #define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59 #define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60 #define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
61 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
64 #define ID_SHIFT 16 /* add new flags before this one */
66 #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
69 #ifdef CONFIG_DEBUG_FS
73 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
75 #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
77 static LIST_HEAD(gpio_chips
);
79 #ifdef CONFIG_GPIO_SYSFS
80 static DEFINE_IDR(dirent_idr
);
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
87 static int gpiod_request(struct gpio_desc
*desc
, const char *label
);
88 static void gpiod_free(struct gpio_desc
*desc
);
89 static int gpiod_direction_input(struct gpio_desc
*desc
);
90 static int gpiod_direction_output(struct gpio_desc
*desc
, int value
);
91 static int gpiod_get_direction(const struct gpio_desc
*desc
);
92 static int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
);
93 static int gpiod_get_value_cansleep(const struct gpio_desc
*desc
);
94 static void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
);
95 static int gpiod_get_value(const struct gpio_desc
*desc
);
96 static void gpiod_set_value(struct gpio_desc
*desc
, int value
);
97 static int gpiod_cansleep(const struct gpio_desc
*desc
);
98 static int gpiod_to_irq(const struct gpio_desc
*desc
);
99 static int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
);
100 static int gpiod_export_link(struct device
*dev
, const char *name
,
101 struct gpio_desc
*desc
);
102 static int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
);
103 static void gpiod_unexport(struct gpio_desc
*desc
);
106 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
108 #ifdef CONFIG_DEBUG_FS
114 * Return the GPIO number of the passed descriptor relative to its chip
116 static int gpio_chip_hwgpio(const struct gpio_desc
*desc
)
118 return desc
- &desc
->chip
->desc
[0];
122 * Convert a GPIO number to its descriptor
124 static struct gpio_desc
*gpio_to_desc(unsigned gpio
)
126 if (WARN(!gpio_is_valid(gpio
), "invalid GPIO %d\n", gpio
))
129 return &gpio_desc
[gpio
];
133 * Convert a GPIO descriptor to the integer namespace.
134 * This should disappear in the future but is needed since we still
135 * use GPIO numbers for error messages and sysfs nodes
137 static int desc_to_gpio(const struct gpio_desc
*desc
)
139 return desc
->chip
->base
+ gpio_chip_hwgpio(desc
);
143 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
144 * when setting direction, and otherwise illegal. Until board setup code
145 * and drivers use explicit requests everywhere (which won't happen when
146 * those calls have no teeth) we can't avoid autorequesting. This nag
147 * message should motivate switching to explicit requests... so should
148 * the weaker cleanup after faults, compared to gpio_request().
150 * NOTE: the autorequest mechanism is going away; at this point it's
151 * only "legal" in the sense that (old) code using it won't break yet,
152 * but instead only triggers a WARN() stack dump.
154 static int gpio_ensure_requested(struct gpio_desc
*desc
)
156 const struct gpio_chip
*chip
= desc
->chip
;
157 const int gpio
= desc_to_gpio(desc
);
159 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
160 "autorequest GPIO-%d\n", gpio
)) {
161 if (!try_module_get(chip
->owner
)) {
162 pr_err("GPIO-%d: module can't be gotten \n", gpio
);
163 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
167 desc_set_label(desc
, "[auto]");
168 /* caller must chip->request() w/o spinlock */
175 static struct gpio_chip
*gpiod_to_chip(const struct gpio_desc
*desc
)
177 return desc
? desc
->chip
: NULL
;
180 /* caller holds gpio_lock *OR* gpio is marked as requested */
181 struct gpio_chip
*gpio_to_chip(unsigned gpio
)
183 return gpiod_to_chip(gpio_to_desc(gpio
));
186 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
187 static int gpiochip_find_base(int ngpio
)
189 struct gpio_chip
*chip
;
190 int base
= ARCH_NR_GPIOS
- ngpio
;
192 list_for_each_entry_reverse(chip
, &gpio_chips
, list
) {
193 /* found a free space? */
194 if (chip
->base
+ chip
->ngpio
<= base
)
197 /* nope, check the space right before the chip */
198 base
= chip
->base
- ngpio
;
201 if (gpio_is_valid(base
)) {
202 pr_debug("%s: found new base at %d\n", __func__
, base
);
205 pr_err("%s: cannot find free range\n", __func__
);
210 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
211 static int gpiod_get_direction(const struct gpio_desc
*desc
)
213 struct gpio_chip
*chip
;
215 int status
= -EINVAL
;
217 chip
= gpiod_to_chip(desc
);
218 offset
= gpio_chip_hwgpio(desc
);
220 if (!chip
->get_direction
)
223 status
= chip
->get_direction(chip
, offset
);
225 /* GPIOF_DIR_IN, or other positive */
227 /* FLAG_IS_OUT is just a cache of the result of get_direction(),
228 * so it does not affect constness per se */
229 clear_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
233 set_bit(FLAG_IS_OUT
, &((struct gpio_desc
*)desc
)->flags
);
238 #ifdef CONFIG_GPIO_SYSFS
240 /* lock protects against unexport_gpio() being called while
241 * sysfs files are active.
243 static DEFINE_MUTEX(sysfs_lock
);
246 * /sys/class/gpio/gpioN... only for GPIOs that are exported
248 * * MAY BE OMITTED if kernel won't allow direction changes
249 * * is read/write as "in" or "out"
250 * * may also be written as "high" or "low", initializing
251 * output value as specified ("out" implies "low")
253 * * always readable, subject to hardware behavior
254 * * may be writable, as zero/nonzero
256 * * configures behavior of poll(2) on /value
257 * * available only if pin can generate IRQs on input
258 * * is read/write as "none", "falling", "rising", or "both"
260 * * configures polarity of /value
261 * * is read/write as zero/nonzero
262 * * also affects existing and subsequent "falling" and "rising"
263 * /edge configuration
266 static ssize_t
gpio_direction_show(struct device
*dev
,
267 struct device_attribute
*attr
, char *buf
)
269 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
272 mutex_lock(&sysfs_lock
);
274 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
277 gpiod_get_direction(desc
);
278 status
= sprintf(buf
, "%s\n",
279 test_bit(FLAG_IS_OUT
, &desc
->flags
)
283 mutex_unlock(&sysfs_lock
);
287 static ssize_t
gpio_direction_store(struct device
*dev
,
288 struct device_attribute
*attr
, const char *buf
, size_t size
)
290 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
293 mutex_lock(&sysfs_lock
);
295 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
297 else if (sysfs_streq(buf
, "high"))
298 status
= gpiod_direction_output(desc
, 1);
299 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
300 status
= gpiod_direction_output(desc
, 0);
301 else if (sysfs_streq(buf
, "in"))
302 status
= gpiod_direction_input(desc
);
306 mutex_unlock(&sysfs_lock
);
307 return status
? : size
;
310 static /* const */ DEVICE_ATTR(direction
, 0644,
311 gpio_direction_show
, gpio_direction_store
);
313 static ssize_t
gpio_value_show(struct device
*dev
,
314 struct device_attribute
*attr
, char *buf
)
316 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
319 mutex_lock(&sysfs_lock
);
321 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
326 value
= !!gpiod_get_value_cansleep(desc
);
327 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
330 status
= sprintf(buf
, "%d\n", value
);
333 mutex_unlock(&sysfs_lock
);
337 static ssize_t
gpio_value_store(struct device
*dev
,
338 struct device_attribute
*attr
, const char *buf
, size_t size
)
340 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
343 mutex_lock(&sysfs_lock
);
345 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
347 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
352 status
= strict_strtol(buf
, 0, &value
);
354 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
356 gpiod_set_value_cansleep(desc
, value
!= 0);
361 mutex_unlock(&sysfs_lock
);
365 static DEVICE_ATTR(value
, 0644,
366 gpio_value_show
, gpio_value_store
);
368 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
370 struct sysfs_dirent
*value_sd
= priv
;
372 sysfs_notify_dirent(value_sd
);
376 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
377 unsigned long gpio_flags
)
379 struct sysfs_dirent
*value_sd
;
380 unsigned long irq_flags
;
383 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
386 irq
= gpiod_to_irq(desc
);
390 id
= desc
->flags
>> ID_SHIFT
;
391 value_sd
= idr_find(&dirent_idr
, id
);
393 free_irq(irq
, value_sd
);
395 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
402 irq_flags
= IRQF_SHARED
;
403 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
404 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
405 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
406 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
407 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
408 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
411 value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, NULL
, "value");
417 ret
= idr_alloc(&dirent_idr
, value_sd
, 1, 0, GFP_KERNEL
);
422 desc
->flags
&= GPIO_FLAGS_MASK
;
423 desc
->flags
|= (unsigned long)id
<< ID_SHIFT
;
425 if (desc
->flags
>> ID_SHIFT
!= id
) {
431 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
432 "gpiolib", value_sd
);
436 desc
->flags
|= gpio_flags
;
440 idr_remove(&dirent_idr
, id
);
441 desc
->flags
&= GPIO_FLAGS_MASK
;
449 static const struct {
452 } trigger_types
[] = {
454 { "falling", BIT(FLAG_TRIG_FALL
) },
455 { "rising", BIT(FLAG_TRIG_RISE
) },
456 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
459 static ssize_t
gpio_edge_show(struct device
*dev
,
460 struct device_attribute
*attr
, char *buf
)
462 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
465 mutex_lock(&sysfs_lock
);
467 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
473 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
474 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
475 == trigger_types
[i
].flags
) {
476 status
= sprintf(buf
, "%s\n",
477 trigger_types
[i
].name
);
482 mutex_unlock(&sysfs_lock
);
486 static ssize_t
gpio_edge_store(struct device
*dev
,
487 struct device_attribute
*attr
, const char *buf
, size_t size
)
489 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
493 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
494 if (sysfs_streq(trigger_types
[i
].name
, buf
))
499 mutex_lock(&sysfs_lock
);
501 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
504 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
509 mutex_unlock(&sysfs_lock
);
514 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
516 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
521 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
525 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
527 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
529 /* reconfigure poll(2) support if enabled on one edge only */
530 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
531 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
532 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
534 gpio_setup_irq(desc
, dev
, 0);
535 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
541 static ssize_t
gpio_active_low_show(struct device
*dev
,
542 struct device_attribute
*attr
, char *buf
)
544 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
547 mutex_lock(&sysfs_lock
);
549 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
552 status
= sprintf(buf
, "%d\n",
553 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
555 mutex_unlock(&sysfs_lock
);
560 static ssize_t
gpio_active_low_store(struct device
*dev
,
561 struct device_attribute
*attr
, const char *buf
, size_t size
)
563 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
566 mutex_lock(&sysfs_lock
);
568 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
573 status
= strict_strtol(buf
, 0, &value
);
575 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
578 mutex_unlock(&sysfs_lock
);
580 return status
? : size
;
583 static DEVICE_ATTR(active_low
, 0644,
584 gpio_active_low_show
, gpio_active_low_store
);
586 static struct attribute
*gpio_attrs
[] = {
587 &dev_attr_value
.attr
,
588 &dev_attr_active_low
.attr
,
592 static const struct attribute_group gpio_attr_group
= {
597 * /sys/class/gpio/gpiochipN/
598 * /base ... matching gpio_chip.base (N)
599 * /label ... matching gpio_chip.label
600 * /ngpio ... matching gpio_chip.ngpio
603 static ssize_t
chip_base_show(struct device
*dev
,
604 struct device_attribute
*attr
, char *buf
)
606 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
608 return sprintf(buf
, "%d\n", chip
->base
);
610 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
612 static ssize_t
chip_label_show(struct device
*dev
,
613 struct device_attribute
*attr
, char *buf
)
615 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
617 return sprintf(buf
, "%s\n", chip
->label
? : "");
619 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
621 static ssize_t
chip_ngpio_show(struct device
*dev
,
622 struct device_attribute
*attr
, char *buf
)
624 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
626 return sprintf(buf
, "%u\n", chip
->ngpio
);
628 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
630 static struct attribute
*gpiochip_attrs
[] = {
632 &dev_attr_label
.attr
,
633 &dev_attr_ngpio
.attr
,
637 static const struct attribute_group gpiochip_attr_group
= {
638 .attrs
= gpiochip_attrs
,
642 * /sys/class/gpio/export ... write-only
643 * integer N ... number of GPIO to export (full access)
644 * /sys/class/gpio/unexport ... write-only
645 * integer N ... number of GPIO to unexport
647 static ssize_t
export_store(struct class *class,
648 struct class_attribute
*attr
,
649 const char *buf
, size_t len
)
652 struct gpio_desc
*desc
;
655 status
= strict_strtol(buf
, 0, &gpio
);
659 desc
= gpio_to_desc(gpio
);
660 /* reject invalid GPIOs */
662 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
666 /* No extra locking here; FLAG_SYSFS just signifies that the
667 * request and export were done by on behalf of userspace, so
668 * they may be undone on its behalf too.
671 status
= gpiod_request(desc
, "sysfs");
673 if (status
== -EPROBE_DEFER
)
677 status
= gpiod_export(desc
, true);
681 set_bit(FLAG_SYSFS
, &desc
->flags
);
685 pr_debug("%s: status %d\n", __func__
, status
);
686 return status
? : len
;
689 static ssize_t
unexport_store(struct class *class,
690 struct class_attribute
*attr
,
691 const char *buf
, size_t len
)
694 struct gpio_desc
*desc
;
697 status
= strict_strtol(buf
, 0, &gpio
);
701 desc
= gpio_to_desc(gpio
);
702 /* reject bogus commands (gpio_unexport ignores them) */
704 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
710 /* No extra locking here; FLAG_SYSFS just signifies that the
711 * request and export were done by on behalf of userspace, so
712 * they may be undone on its behalf too.
714 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
)) {
720 pr_debug("%s: status %d\n", __func__
, status
);
721 return status
? : len
;
724 static struct class_attribute gpio_class_attrs
[] = {
725 __ATTR(export
, 0200, NULL
, export_store
),
726 __ATTR(unexport
, 0200, NULL
, unexport_store
),
730 static struct class gpio_class
= {
732 .owner
= THIS_MODULE
,
734 .class_attrs
= gpio_class_attrs
,
739 * gpio_export - export a GPIO through sysfs
740 * @gpio: gpio to make available, already requested
741 * @direction_may_change: true if userspace may change gpio direction
742 * Context: arch_initcall or later
744 * When drivers want to make a GPIO accessible to userspace after they
745 * have requested it -- perhaps while debugging, or as part of their
746 * public interface -- they may use this routine. If the GPIO can
747 * change direction (some can't) and the caller allows it, userspace
748 * will see "direction" sysfs attribute which may be used to change
749 * the gpio's direction. A "value" attribute will always be provided.
751 * Returns zero on success, else an error.
753 static int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
)
755 struct gpio_chip
*chip
;
758 const char *ioname
= NULL
;
762 /* can't export until sysfs is available ... */
764 pr_debug("%s: called too early!\n", __func__
);
769 pr_debug("%s: invalid gpio descriptor\n", __func__
);
775 mutex_lock(&sysfs_lock
);
777 /* check if chip is being removed */
778 if (!chip
|| !chip
->exported
) {
783 spin_lock_irqsave(&gpio_lock
, flags
);
784 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
785 test_bit(FLAG_EXPORT
, &desc
->flags
)) {
786 spin_unlock_irqrestore(&gpio_lock
, flags
);
787 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
788 __func__
, desc_to_gpio(desc
),
789 test_bit(FLAG_REQUESTED
, &desc
->flags
),
790 test_bit(FLAG_EXPORT
, &desc
->flags
));
795 if (!desc
->chip
->direction_input
|| !desc
->chip
->direction_output
)
796 direction_may_change
= false;
797 spin_unlock_irqrestore(&gpio_lock
, flags
);
799 offset
= gpio_chip_hwgpio(desc
);
800 if (desc
->chip
->names
&& desc
->chip
->names
[offset
])
801 ioname
= desc
->chip
->names
[offset
];
803 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
804 desc
, ioname
? ioname
: "gpio%u",
807 status
= PTR_ERR(dev
);
811 status
= sysfs_create_group(&dev
->kobj
, &gpio_attr_group
);
813 goto fail_unregister_device
;
815 if (direction_may_change
) {
816 status
= device_create_file(dev
, &dev_attr_direction
);
818 goto fail_remove_attr_group
;
821 if (gpiod_to_irq(desc
) >= 0 && (direction_may_change
||
822 !test_bit(FLAG_IS_OUT
, &desc
->flags
))) {
823 status
= device_create_file(dev
, &dev_attr_edge
);
825 goto fail_remove_attr_direction
;
828 set_bit(FLAG_EXPORT
, &desc
->flags
);
829 mutex_unlock(&sysfs_lock
);
832 fail_remove_attr_direction
:
833 device_remove_file(dev
, &dev_attr_direction
);
834 fail_remove_attr_group
:
835 sysfs_remove_group(&dev
->kobj
, &gpio_attr_group
);
836 fail_unregister_device
:
837 device_unregister(dev
);
839 mutex_unlock(&sysfs_lock
);
840 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
845 int gpio_export(unsigned gpio
, bool direction_may_change
)
847 return gpiod_export(gpio_to_desc(gpio
), direction_may_change
);
849 EXPORT_SYMBOL_GPL(gpio_export
);
851 static int match_export(struct device
*dev
, const void *data
)
853 return dev_get_drvdata(dev
) == data
;
857 * gpio_export_link - create a sysfs link to an exported GPIO node
858 * @dev: device under which to create symlink
859 * @name: name of the symlink
860 * @gpio: gpio to create symlink to, already exported
862 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
863 * node. Caller is responsible for unlinking.
865 * Returns zero on success, else an error.
867 static int gpiod_export_link(struct device
*dev
, const char *name
,
868 struct gpio_desc
*desc
)
870 int status
= -EINVAL
;
873 pr_warn("%s: invalid GPIO\n", __func__
);
877 mutex_lock(&sysfs_lock
);
879 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
882 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
884 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
892 mutex_unlock(&sysfs_lock
);
895 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
901 int gpio_export_link(struct device
*dev
, const char *name
, unsigned gpio
)
903 return gpiod_export_link(dev
, name
, gpio_to_desc(gpio
));
905 EXPORT_SYMBOL_GPL(gpio_export_link
);
908 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
909 * @gpio: gpio to change
910 * @value: non-zero to use active low, i.e. inverted values
912 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
913 * The GPIO does not have to be exported yet. If poll(2) support has
914 * been enabled for either rising or falling edge, it will be
915 * reconfigured to follow the new polarity.
917 * Returns zero on success, else an error.
919 static int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
)
921 struct device
*dev
= NULL
;
922 int status
= -EINVAL
;
925 pr_warn("%s: invalid GPIO\n", __func__
);
929 mutex_lock(&sysfs_lock
);
931 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
932 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
939 status
= sysfs_set_active_low(desc
, dev
, value
);
942 mutex_unlock(&sysfs_lock
);
945 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
951 int gpio_sysfs_set_active_low(unsigned gpio
, int value
)
953 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio
), value
);
955 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low
);
958 * gpio_unexport - reverse effect of gpio_export()
959 * @gpio: gpio to make unavailable
961 * This is implicit on gpio_free().
963 static void gpiod_unexport(struct gpio_desc
*desc
)
966 struct device
*dev
= NULL
;
969 pr_warn("%s: invalid GPIO\n", __func__
);
973 mutex_lock(&sysfs_lock
);
975 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
977 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
979 gpio_setup_irq(desc
, dev
, 0);
980 clear_bit(FLAG_EXPORT
, &desc
->flags
);
985 mutex_unlock(&sysfs_lock
);
988 device_remove_file(dev
, &dev_attr_edge
);
989 device_remove_file(dev
, &dev_attr_direction
);
990 sysfs_remove_group(&dev
->kobj
, &gpio_attr_group
);
991 device_unregister(dev
);
996 pr_debug("%s: gpio%d status %d\n", __func__
, desc_to_gpio(desc
),
1000 void gpio_unexport(unsigned gpio
)
1002 gpiod_unexport(gpio_to_desc(gpio
));
1004 EXPORT_SYMBOL_GPL(gpio_unexport
);
1006 static int gpiochip_export(struct gpio_chip
*chip
)
1011 /* Many systems register gpio chips for SOC support very early,
1012 * before driver model support is available. In those cases we
1013 * export this later, in gpiolib_sysfs_init() ... here we just
1014 * verify that _some_ field of gpio_class got initialized.
1019 /* use chip->base for the ID; it's already known to be unique */
1020 mutex_lock(&sysfs_lock
);
1021 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
1022 "gpiochip%d", chip
->base
);
1024 status
= sysfs_create_group(&dev
->kobj
,
1025 &gpiochip_attr_group
);
1027 status
= PTR_ERR(dev
);
1028 chip
->exported
= (status
== 0);
1029 mutex_unlock(&sysfs_lock
);
1032 unsigned long flags
;
1035 spin_lock_irqsave(&gpio_lock
, flags
);
1037 while (gpio
< chip
->ngpio
)
1038 chip
->desc
[gpio
++].chip
= NULL
;
1039 spin_unlock_irqrestore(&gpio_lock
, flags
);
1041 pr_debug("%s: chip %s status %d\n", __func__
,
1042 chip
->label
, status
);
1048 static void gpiochip_unexport(struct gpio_chip
*chip
)
1052 struct gpio_desc
*desc
;
1055 mutex_lock(&sysfs_lock
);
1056 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
1058 sysfs_remove_group(&dev
->kobj
, &gpiochip_attr_group
);
1060 device_unregister(dev
);
1061 /* prevent further gpiod exports */
1066 mutex_unlock(&sysfs_lock
);
1069 pr_debug("%s: chip %s status %d\n", __func__
,
1070 chip
->label
, status
);
1072 /* unregister gpiod class devices owned by sysfs */
1073 for (i
= 0; i
< chip
->ngpio
; i
++) {
1074 desc
= &chip
->desc
[i
];
1075 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
))
1080 static int __init
gpiolib_sysfs_init(void)
1083 unsigned long flags
;
1084 struct gpio_chip
*chip
;
1086 status
= class_register(&gpio_class
);
1090 /* Scan and register the gpio_chips which registered very
1091 * early (e.g. before the class_register above was called).
1093 * We run before arch_initcall() so chip->dev nodes can have
1094 * registered, and so arch_initcall() can always gpio_export().
1096 spin_lock_irqsave(&gpio_lock
, flags
);
1097 list_for_each_entry(chip
, &gpio_chips
, list
) {
1098 if (!chip
|| chip
->exported
)
1101 spin_unlock_irqrestore(&gpio_lock
, flags
);
1102 status
= gpiochip_export(chip
);
1103 spin_lock_irqsave(&gpio_lock
, flags
);
1105 spin_unlock_irqrestore(&gpio_lock
, flags
);
1110 postcore_initcall(gpiolib_sysfs_init
);
1113 static inline int gpiochip_export(struct gpio_chip
*chip
)
1118 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
1122 static inline int gpiod_export(struct gpio_desc
*desc
,
1123 bool direction_may_change
)
1128 static inline int gpiod_export_link(struct device
*dev
, const char *name
,
1129 struct gpio_desc
*desc
)
1134 static inline int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
)
1139 static inline void gpiod_unexport(struct gpio_desc
*desc
)
1143 #endif /* CONFIG_GPIO_SYSFS */
1146 * Add a new chip to the global chips list, keeping the list of chips sorted
1149 * Return -EBUSY if the new chip overlaps with some other chip's integer
1152 static int gpiochip_add_to_list(struct gpio_chip
*chip
)
1154 struct list_head
*pos
= &gpio_chips
;
1155 struct gpio_chip
*_chip
;
1158 /* find where to insert our chip */
1159 list_for_each(pos
, &gpio_chips
) {
1160 _chip
= list_entry(pos
, struct gpio_chip
, list
);
1161 /* shall we insert before _chip? */
1162 if (_chip
->base
>= chip
->base
+ chip
->ngpio
)
1166 /* are we stepping on the chip right before? */
1167 if (pos
!= &gpio_chips
&& pos
->prev
!= &gpio_chips
) {
1168 _chip
= list_entry(pos
->prev
, struct gpio_chip
, list
);
1169 if (_chip
->base
+ _chip
->ngpio
> chip
->base
) {
1171 "GPIO integer space overlap, cannot add chip\n");
1177 list_add_tail(&chip
->list
, pos
);
1183 * gpiochip_add() - register a gpio_chip
1184 * @chip: the chip to register, with chip->base initialized
1185 * Context: potentially before irqs or kmalloc will work
1187 * Returns a negative errno if the chip can't be registered, such as
1188 * because the chip->base is invalid or already associated with a
1189 * different chip. Otherwise it returns zero as a success code.
1191 * When gpiochip_add() is called very early during boot, so that GPIOs
1192 * can be freely used, the chip->dev device must be registered before
1193 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1194 * for GPIOs will fail rudely.
1196 * If chip->base is negative, this requests dynamic assignment of
1197 * a range of valid GPIOs.
1199 int gpiochip_add(struct gpio_chip
*chip
)
1201 unsigned long flags
;
1204 int base
= chip
->base
;
1206 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
1212 spin_lock_irqsave(&gpio_lock
, flags
);
1215 base
= gpiochip_find_base(chip
->ngpio
);
1223 status
= gpiochip_add_to_list(chip
);
1226 chip
->desc
= &gpio_desc
[chip
->base
];
1228 for (id
= 0; id
< chip
->ngpio
; id
++) {
1229 struct gpio_desc
*desc
= &chip
->desc
[id
];
1232 /* REVISIT: most hardware initializes GPIOs as
1233 * inputs (often with pullups enabled) so power
1234 * usage is minimized. Linux code should set the
1235 * gpio direction first thing; but until it does,
1236 * and in case chip->get_direction is not set,
1237 * we may expose the wrong direction in sysfs.
1239 desc
->flags
= !chip
->direction_input
1240 ? (1 << FLAG_IS_OUT
)
1245 #ifdef CONFIG_PINCTRL
1246 INIT_LIST_HEAD(&chip
->pin_ranges
);
1249 of_gpiochip_add(chip
);
1252 spin_unlock_irqrestore(&gpio_lock
, flags
);
1257 status
= gpiochip_export(chip
);
1261 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1262 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1263 chip
->label
? : "generic");
1267 /* failures here can mean systems won't boot... */
1268 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1269 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1270 chip
->label
? : "generic");
1273 EXPORT_SYMBOL_GPL(gpiochip_add
);
1276 * gpiochip_remove() - unregister a gpio_chip
1277 * @chip: the chip to unregister
1279 * A gpio_chip with any GPIOs still requested may not be removed.
1281 int gpiochip_remove(struct gpio_chip
*chip
)
1283 unsigned long flags
;
1287 gpiochip_unexport(chip
);
1289 spin_lock_irqsave(&gpio_lock
, flags
);
1291 gpiochip_remove_pin_ranges(chip
);
1292 of_gpiochip_remove(chip
);
1294 for (id
= 0; id
< chip
->ngpio
; id
++) {
1295 if (test_bit(FLAG_REQUESTED
, &chip
->desc
[id
].flags
)) {
1301 for (id
= 0; id
< chip
->ngpio
; id
++)
1302 chip
->desc
[id
].chip
= NULL
;
1304 list_del(&chip
->list
);
1307 spin_unlock_irqrestore(&gpio_lock
, flags
);
1311 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1314 * gpiochip_find() - iterator for locating a specific gpio_chip
1315 * @data: data to pass to match function
1316 * @callback: Callback function to check gpio_chip
1318 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1319 * determined by a user supplied @match callback. The callback should return
1320 * 0 if the device doesn't match and non-zero if it does. If the callback is
1321 * non-zero, this function will return to the caller and not iterate over any
1324 struct gpio_chip
*gpiochip_find(void *data
,
1325 int (*match
)(struct gpio_chip
*chip
,
1328 struct gpio_chip
*chip
;
1329 unsigned long flags
;
1331 spin_lock_irqsave(&gpio_lock
, flags
);
1332 list_for_each_entry(chip
, &gpio_chips
, list
)
1333 if (match(chip
, data
))
1337 if (&chip
->list
== &gpio_chips
)
1339 spin_unlock_irqrestore(&gpio_lock
, flags
);
1343 EXPORT_SYMBOL_GPL(gpiochip_find
);
1345 #ifdef CONFIG_PINCTRL
1348 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1349 * @chip: the gpiochip to add the range for
1350 * @pinctrl_name: the dev_name() of the pin controller to map to
1351 * @gpio_offset: the start offset in the current gpio_chip number space
1352 * @pin_offset: the start offset in the pin controller number space
1353 * @npins: the number of pins from the offset of each pin space (GPIO and
1354 * pin controller) to accumulate in this range
1356 int gpiochip_add_pin_range(struct gpio_chip
*chip
, const char *pinctl_name
,
1357 unsigned int gpio_offset
, unsigned int pin_offset
,
1360 struct gpio_pin_range
*pin_range
;
1363 pin_range
= kzalloc(sizeof(*pin_range
), GFP_KERNEL
);
1365 pr_err("%s: GPIO chip: failed to allocate pin ranges\n",
1370 /* Use local offset as range ID */
1371 pin_range
->range
.id
= gpio_offset
;
1372 pin_range
->range
.gc
= chip
;
1373 pin_range
->range
.name
= chip
->label
;
1374 pin_range
->range
.base
= chip
->base
+ gpio_offset
;
1375 pin_range
->range
.pin_base
= pin_offset
;
1376 pin_range
->range
.npins
= npins
;
1377 pin_range
->pctldev
= pinctrl_find_and_add_gpio_range(pinctl_name
,
1379 if (IS_ERR(pin_range
->pctldev
)) {
1380 ret
= PTR_ERR(pin_range
->pctldev
);
1381 pr_err("%s: GPIO chip: could not create pin range\n",
1386 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n",
1387 chip
->label
, gpio_offset
, gpio_offset
+ npins
- 1,
1389 pin_offset
, pin_offset
+ npins
- 1);
1391 list_add_tail(&pin_range
->node
, &chip
->pin_ranges
);
1395 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range
);
1398 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1399 * @chip: the chip to remove all the mappings for
1401 void gpiochip_remove_pin_ranges(struct gpio_chip
*chip
)
1403 struct gpio_pin_range
*pin_range
, *tmp
;
1405 list_for_each_entry_safe(pin_range
, tmp
, &chip
->pin_ranges
, node
) {
1406 list_del(&pin_range
->node
);
1407 pinctrl_remove_gpio_range(pin_range
->pctldev
,
1412 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges
);
1414 #endif /* CONFIG_PINCTRL */
1416 /* These "optional" allocation calls help prevent drivers from stomping
1417 * on each other, and help provide better diagnostics in debugfs.
1418 * They're called even less than the "set direction" calls.
1420 static int gpiod_request(struct gpio_desc
*desc
, const char *label
)
1422 struct gpio_chip
*chip
;
1423 int status
= -EPROBE_DEFER
;
1424 unsigned long flags
;
1427 pr_warn("%s: invalid GPIO\n", __func__
);
1431 spin_lock_irqsave(&gpio_lock
, flags
);
1437 if (!try_module_get(chip
->owner
))
1440 /* NOTE: gpio_request() can be called in early boot,
1441 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1444 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1445 desc_set_label(desc
, label
? : "?");
1449 module_put(chip
->owner
);
1453 if (chip
->request
) {
1454 /* chip->request may sleep */
1455 spin_unlock_irqrestore(&gpio_lock
, flags
);
1456 status
= chip
->request(chip
, gpio_chip_hwgpio(desc
));
1457 spin_lock_irqsave(&gpio_lock
, flags
);
1460 desc_set_label(desc
, NULL
);
1461 module_put(chip
->owner
);
1462 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1466 if (chip
->get_direction
) {
1467 /* chip->get_direction may sleep */
1468 spin_unlock_irqrestore(&gpio_lock
, flags
);
1469 gpiod_get_direction(desc
);
1470 spin_lock_irqsave(&gpio_lock
, flags
);
1474 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1475 desc_to_gpio(desc
), label
? : "?", status
);
1476 spin_unlock_irqrestore(&gpio_lock
, flags
);
1480 int gpio_request(unsigned gpio
, const char *label
)
1482 return gpiod_request(gpio_to_desc(gpio
), label
);
1484 EXPORT_SYMBOL_GPL(gpio_request
);
1486 static void gpiod_free(struct gpio_desc
*desc
)
1488 unsigned long flags
;
1489 struct gpio_chip
*chip
;
1494 WARN_ON(extra_checks
);
1498 gpiod_unexport(desc
);
1500 spin_lock_irqsave(&gpio_lock
, flags
);
1503 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1505 spin_unlock_irqrestore(&gpio_lock
, flags
);
1506 might_sleep_if(chip
->can_sleep
);
1507 chip
->free(chip
, gpio_chip_hwgpio(desc
));
1508 spin_lock_irqsave(&gpio_lock
, flags
);
1510 desc_set_label(desc
, NULL
);
1511 module_put(desc
->chip
->owner
);
1512 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1513 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1514 clear_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1515 clear_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1517 WARN_ON(extra_checks
);
1519 spin_unlock_irqrestore(&gpio_lock
, flags
);
1522 void gpio_free(unsigned gpio
)
1524 gpiod_free(gpio_to_desc(gpio
));
1526 EXPORT_SYMBOL_GPL(gpio_free
);
1529 * gpio_request_one - request a single GPIO with initial configuration
1530 * @gpio: the GPIO number
1531 * @flags: GPIO configuration as specified by GPIOF_*
1532 * @label: a literal description string of this GPIO
1534 int gpio_request_one(unsigned gpio
, unsigned long flags
, const char *label
)
1536 struct gpio_desc
*desc
;
1539 desc
= gpio_to_desc(gpio
);
1541 err
= gpiod_request(desc
, label
);
1545 if (flags
& GPIOF_OPEN_DRAIN
)
1546 set_bit(FLAG_OPEN_DRAIN
, &desc
->flags
);
1548 if (flags
& GPIOF_OPEN_SOURCE
)
1549 set_bit(FLAG_OPEN_SOURCE
, &desc
->flags
);
1551 if (flags
& GPIOF_DIR_IN
)
1552 err
= gpiod_direction_input(desc
);
1554 err
= gpiod_direction_output(desc
,
1555 (flags
& GPIOF_INIT_HIGH
) ? 1 : 0);
1560 if (flags
& GPIOF_EXPORT
) {
1561 err
= gpiod_export(desc
, flags
& GPIOF_EXPORT_CHANGEABLE
);
1572 EXPORT_SYMBOL_GPL(gpio_request_one
);
1575 * gpio_request_array - request multiple GPIOs in a single call
1576 * @array: array of the 'struct gpio'
1577 * @num: how many GPIOs in the array
1579 int gpio_request_array(const struct gpio
*array
, size_t num
)
1583 for (i
= 0; i
< num
; i
++, array
++) {
1584 err
= gpio_request_one(array
->gpio
, array
->flags
, array
->label
);
1592 gpio_free((--array
)->gpio
);
1595 EXPORT_SYMBOL_GPL(gpio_request_array
);
1598 * gpio_free_array - release multiple GPIOs in a single call
1599 * @array: array of the 'struct gpio'
1600 * @num: how many GPIOs in the array
1602 void gpio_free_array(const struct gpio
*array
, size_t num
)
1605 gpio_free((array
++)->gpio
);
1607 EXPORT_SYMBOL_GPL(gpio_free_array
);
1610 * gpiochip_is_requested - return string iff signal was requested
1611 * @chip: controller managing the signal
1612 * @offset: of signal within controller's 0..(ngpio - 1) range
1614 * Returns NULL if the GPIO is not currently requested, else a string.
1615 * If debugfs support is enabled, the string returned is the label passed
1616 * to gpio_request(); otherwise it is a meaningless constant.
1618 * This function is for use by GPIO controller drivers. The label can
1619 * help with diagnostics, and knowing that the signal is used as a GPIO
1620 * can help avoid accidentally multiplexing it to another controller.
1622 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1624 struct gpio_desc
*desc
;
1626 if (!GPIO_OFFSET_VALID(chip
, offset
))
1629 desc
= &chip
->desc
[offset
];
1631 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) == 0)
1633 #ifdef CONFIG_DEBUG_FS
1639 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1642 /* Drivers MUST set GPIO direction before making get/set calls. In
1643 * some cases this is done in early boot, before IRQs are enabled.
1645 * As a rule these aren't called more than once (except for drivers
1646 * using the open-drain emulation idiom) so these are natural places
1647 * to accumulate extra debugging checks. Note that we can't (yet)
1648 * rely on gpio_request() having been called beforehand.
1651 static int gpiod_direction_input(struct gpio_desc
*desc
)
1653 unsigned long flags
;
1654 struct gpio_chip
*chip
;
1655 int status
= -EINVAL
;
1659 pr_warn("%s: invalid GPIO\n", __func__
);
1663 spin_lock_irqsave(&gpio_lock
, flags
);
1666 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
1668 status
= gpio_ensure_requested(desc
);
1672 /* now we know the gpio is valid and chip won't vanish */
1674 spin_unlock_irqrestore(&gpio_lock
, flags
);
1676 might_sleep_if(chip
->can_sleep
);
1678 offset
= gpio_chip_hwgpio(desc
);
1680 status
= chip
->request(chip
, offset
);
1682 pr_debug("GPIO-%d: chip request fail, %d\n",
1683 desc_to_gpio(desc
), status
);
1684 /* and it's not available to anyone else ...
1685 * gpio_request() is the fully clean solution.
1691 status
= chip
->direction_input(chip
, offset
);
1693 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1695 trace_gpio_direction(desc_to_gpio(desc
), 1, status
);
1699 spin_unlock_irqrestore(&gpio_lock
, flags
);
1701 pr_debug("%s: gpio-%d status %d\n", __func__
,
1702 desc_to_gpio(desc
), status
);
1706 int gpio_direction_input(unsigned gpio
)
1708 return gpiod_direction_input(gpio_to_desc(gpio
));
1710 EXPORT_SYMBOL_GPL(gpio_direction_input
);
1712 static int gpiod_direction_output(struct gpio_desc
*desc
, int value
)
1714 unsigned long flags
;
1715 struct gpio_chip
*chip
;
1716 int status
= -EINVAL
;
1720 pr_warn("%s: invalid GPIO\n", __func__
);
1724 /* Open drain pin should not be driven to 1 */
1725 if (value
&& test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1726 return gpiod_direction_input(desc
);
1728 /* Open source pin should not be driven to 0 */
1729 if (!value
&& test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1730 return gpiod_direction_input(desc
);
1732 spin_lock_irqsave(&gpio_lock
, flags
);
1735 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
1737 status
= gpio_ensure_requested(desc
);
1741 /* now we know the gpio is valid and chip won't vanish */
1743 spin_unlock_irqrestore(&gpio_lock
, flags
);
1745 might_sleep_if(chip
->can_sleep
);
1747 offset
= gpio_chip_hwgpio(desc
);
1749 status
= chip
->request(chip
, offset
);
1751 pr_debug("GPIO-%d: chip request fail, %d\n",
1752 desc_to_gpio(desc
), status
);
1753 /* and it's not available to anyone else ...
1754 * gpio_request() is the fully clean solution.
1760 status
= chip
->direction_output(chip
, offset
, value
);
1762 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1763 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1764 trace_gpio_direction(desc_to_gpio(desc
), 0, status
);
1768 spin_unlock_irqrestore(&gpio_lock
, flags
);
1770 pr_debug("%s: gpio-%d status %d\n", __func__
,
1771 desc_to_gpio(desc
), status
);
1775 int gpio_direction_output(unsigned gpio
, int value
)
1777 return gpiod_direction_output(gpio_to_desc(gpio
), value
);
1779 EXPORT_SYMBOL_GPL(gpio_direction_output
);
1782 * gpio_set_debounce - sets @debounce time for a @gpio
1783 * @gpio: the gpio to set debounce time
1784 * @debounce: debounce time is microseconds
1786 static int gpiod_set_debounce(struct gpio_desc
*desc
, unsigned debounce
)
1788 unsigned long flags
;
1789 struct gpio_chip
*chip
;
1790 int status
= -EINVAL
;
1794 pr_warn("%s: invalid GPIO\n", __func__
);
1798 spin_lock_irqsave(&gpio_lock
, flags
);
1801 if (!chip
|| !chip
->set
|| !chip
->set_debounce
)
1804 status
= gpio_ensure_requested(desc
);
1808 /* now we know the gpio is valid and chip won't vanish */
1810 spin_unlock_irqrestore(&gpio_lock
, flags
);
1812 might_sleep_if(chip
->can_sleep
);
1814 offset
= gpio_chip_hwgpio(desc
);
1815 return chip
->set_debounce(chip
, offset
, debounce
);
1818 spin_unlock_irqrestore(&gpio_lock
, flags
);
1820 pr_debug("%s: gpio-%d status %d\n", __func__
,
1821 desc_to_gpio(desc
), status
);
1826 int gpio_set_debounce(unsigned gpio
, unsigned debounce
)
1828 return gpiod_set_debounce(gpio_to_desc(gpio
), debounce
);
1830 EXPORT_SYMBOL_GPL(gpio_set_debounce
);
1832 /* I/O calls are only valid after configuration completed; the relevant
1833 * "is this a valid GPIO" error checks should already have been done.
1835 * "Get" operations are often inlinable as reading a pin value register,
1836 * and masking the relevant bit in that register.
1838 * When "set" operations are inlinable, they involve writing that mask to
1839 * one register to set a low value, or a different register to set it high.
1840 * Otherwise locking is needed, so there may be little value to inlining.
1842 *------------------------------------------------------------------------
1844 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1845 * have requested the GPIO. That can include implicit requesting by
1846 * a direction setting call. Marking a gpio as requested locks its chip
1847 * in memory, guaranteeing that these table lookups need no more locking
1848 * and that gpiochip_remove() will fail.
1850 * REVISIT when debugging, consider adding some instrumentation to ensure
1851 * that the GPIO was actually requested.
1855 * __gpio_get_value() - return a gpio's value
1856 * @gpio: gpio whose value will be returned
1859 * This is used directly or indirectly to implement gpio_get_value().
1860 * It returns the zero or nonzero value provided by the associated
1861 * gpio_chip.get() method; or zero if no such method is provided.
1863 static int gpiod_get_value(const struct gpio_desc
*desc
)
1865 struct gpio_chip
*chip
;
1872 offset
= gpio_chip_hwgpio(desc
);
1873 /* Should be using gpio_get_value_cansleep() */
1874 WARN_ON(chip
->can_sleep
);
1875 value
= chip
->get
? chip
->get(chip
, offset
) : 0;
1876 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
1880 int __gpio_get_value(unsigned gpio
)
1882 return gpiod_get_value(gpio_to_desc(gpio
));
1884 EXPORT_SYMBOL_GPL(__gpio_get_value
);
1887 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1888 * @gpio: Gpio whose state need to be set.
1890 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1892 static void _gpio_set_open_drain_value(struct gpio_desc
*desc
, int value
)
1895 struct gpio_chip
*chip
= desc
->chip
;
1896 int offset
= gpio_chip_hwgpio(desc
);
1899 err
= chip
->direction_input(chip
, offset
);
1901 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1903 err
= chip
->direction_output(chip
, offset
, 0);
1905 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1907 trace_gpio_direction(desc_to_gpio(desc
), value
, err
);
1909 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1910 __func__
, desc_to_gpio(desc
), err
);
1914 * _gpio_set_open_source() - Set the open source gpio's value.
1915 * @gpio: Gpio whose state need to be set.
1917 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1919 static void _gpio_set_open_source_value(struct gpio_desc
*desc
, int value
)
1922 struct gpio_chip
*chip
= desc
->chip
;
1923 int offset
= gpio_chip_hwgpio(desc
);
1926 err
= chip
->direction_output(chip
, offset
, 1);
1928 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1930 err
= chip
->direction_input(chip
, offset
);
1932 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1934 trace_gpio_direction(desc_to_gpio(desc
), !value
, err
);
1936 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1937 __func__
, desc_to_gpio(desc
), err
);
1941 * __gpio_set_value() - assign a gpio's value
1942 * @gpio: gpio whose value will be assigned
1943 * @value: value to assign
1946 * This is used directly or indirectly to implement gpio_set_value().
1947 * It invokes the associated gpio_chip.set() method.
1949 static void gpiod_set_value(struct gpio_desc
*desc
, int value
)
1951 struct gpio_chip
*chip
;
1956 /* Should be using gpio_set_value_cansleep() */
1957 WARN_ON(chip
->can_sleep
);
1958 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
1959 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
1960 _gpio_set_open_drain_value(desc
, value
);
1961 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
1962 _gpio_set_open_source_value(desc
, value
);
1964 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
1967 void __gpio_set_value(unsigned gpio
, int value
)
1969 return gpiod_set_value(gpio_to_desc(gpio
), value
);
1971 EXPORT_SYMBOL_GPL(__gpio_set_value
);
1974 * __gpio_cansleep() - report whether gpio value access will sleep
1975 * @gpio: gpio in question
1978 * This is used directly or indirectly to implement gpio_cansleep(). It
1979 * returns nonzero if access reading or writing the GPIO value can sleep.
1981 static int gpiod_cansleep(const struct gpio_desc
*desc
)
1985 /* only call this on GPIOs that are valid! */
1986 return desc
->chip
->can_sleep
;
1989 int __gpio_cansleep(unsigned gpio
)
1991 return gpiod_cansleep(gpio_to_desc(gpio
));
1993 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
1996 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1997 * @gpio: gpio whose IRQ will be returned (already requested)
2000 * This is used directly or indirectly to implement gpio_to_irq().
2001 * It returns the number of the IRQ signaled by this (input) GPIO,
2002 * or a negative errno.
2004 static int gpiod_to_irq(const struct gpio_desc
*desc
)
2006 struct gpio_chip
*chip
;
2012 offset
= gpio_chip_hwgpio(desc
);
2013 return chip
->to_irq
? chip
->to_irq(chip
, offset
) : -ENXIO
;
2016 int __gpio_to_irq(unsigned gpio
)
2018 return gpiod_to_irq(gpio_to_desc(gpio
));
2020 EXPORT_SYMBOL_GPL(__gpio_to_irq
);
2023 /* There's no value in making it easy to inline GPIO calls that may sleep.
2024 * Common examples include ones connected to I2C or SPI chips.
2027 static int gpiod_get_value_cansleep(const struct gpio_desc
*desc
)
2029 struct gpio_chip
*chip
;
2033 might_sleep_if(extra_checks
);
2037 offset
= gpio_chip_hwgpio(desc
);
2038 value
= chip
->get
? chip
->get(chip
, offset
) : 0;
2039 trace_gpio_value(desc_to_gpio(desc
), 1, value
);
2043 int gpio_get_value_cansleep(unsigned gpio
)
2045 return gpiod_get_value_cansleep(gpio_to_desc(gpio
));
2047 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
2049 static void gpiod_set_value_cansleep(struct gpio_desc
*desc
, int value
)
2051 struct gpio_chip
*chip
;
2053 might_sleep_if(extra_checks
);
2057 trace_gpio_value(desc_to_gpio(desc
), 0, value
);
2058 if (test_bit(FLAG_OPEN_DRAIN
, &desc
->flags
))
2059 _gpio_set_open_drain_value(desc
, value
);
2060 else if (test_bit(FLAG_OPEN_SOURCE
, &desc
->flags
))
2061 _gpio_set_open_source_value(desc
, value
);
2063 chip
->set(chip
, gpio_chip_hwgpio(desc
), value
);
2066 void gpio_set_value_cansleep(unsigned gpio
, int value
)
2068 return gpiod_set_value_cansleep(gpio_to_desc(gpio
), value
);
2070 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
2072 #ifdef CONFIG_DEBUG_FS
2074 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
2077 unsigned gpio
= chip
->base
;
2078 struct gpio_desc
*gdesc
= &chip
->desc
[0];
2081 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
2082 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
2085 gpiod_get_direction(gdesc
);
2086 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
2087 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s",
2089 is_out
? "out" : "in ",
2091 ? (chip
->get(chip
, i
) ? "hi" : "lo")
2093 seq_printf(s
, "\n");
2097 static void *gpiolib_seq_start(struct seq_file
*s
, loff_t
*pos
)
2099 unsigned long flags
;
2100 struct gpio_chip
*chip
= NULL
;
2101 loff_t index
= *pos
;
2105 spin_lock_irqsave(&gpio_lock
, flags
);
2106 list_for_each_entry(chip
, &gpio_chips
, list
)
2108 spin_unlock_irqrestore(&gpio_lock
, flags
);
2111 spin_unlock_irqrestore(&gpio_lock
, flags
);
2116 static void *gpiolib_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2118 unsigned long flags
;
2119 struct gpio_chip
*chip
= v
;
2122 spin_lock_irqsave(&gpio_lock
, flags
);
2123 if (list_is_last(&chip
->list
, &gpio_chips
))
2126 ret
= list_entry(chip
->list
.next
, struct gpio_chip
, list
);
2127 spin_unlock_irqrestore(&gpio_lock
, flags
);
2135 static void gpiolib_seq_stop(struct seq_file
*s
, void *v
)
2139 static int gpiolib_seq_show(struct seq_file
*s
, void *v
)
2141 struct gpio_chip
*chip
= v
;
2144 seq_printf(s
, "%sGPIOs %d-%d", (char *)s
->private,
2145 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
2148 seq_printf(s
, ", %s/%s", dev
->bus
? dev
->bus
->name
: "no-bus",
2151 seq_printf(s
, ", %s", chip
->label
);
2152 if (chip
->can_sleep
)
2153 seq_printf(s
, ", can sleep");
2154 seq_printf(s
, ":\n");
2157 chip
->dbg_show(s
, chip
);
2159 gpiolib_dbg_show(s
, chip
);
2164 static const struct seq_operations gpiolib_seq_ops
= {
2165 .start
= gpiolib_seq_start
,
2166 .next
= gpiolib_seq_next
,
2167 .stop
= gpiolib_seq_stop
,
2168 .show
= gpiolib_seq_show
,
2171 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
2173 return seq_open(file
, &gpiolib_seq_ops
);
2176 static const struct file_operations gpiolib_operations
= {
2177 .owner
= THIS_MODULE
,
2178 .open
= gpiolib_open
,
2180 .llseek
= seq_lseek
,
2181 .release
= seq_release
,
2184 static int __init
gpiolib_debugfs_init(void)
2186 /* /sys/kernel/debug/gpio */
2187 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
2188 NULL
, NULL
, &gpiolib_operations
);
2191 subsys_initcall(gpiolib_debugfs_init
);
2193 #endif /* DEBUG_FS */