1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/interrupt.h>
5 #include <linux/spinlock.h>
6 #include <linux/device.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/gpio.h>
11 #include <linux/idr.h>
14 /* Optional implementation infrastructure for GPIO interfaces.
16 * Platforms may want to use this if they tend to use very many GPIOs
17 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc.
19 * When kernel footprint or instruction count is an issue, simpler
20 * implementations may be preferred. The GPIO programming interface
21 * allows for inlining speed-critical get/set operations for common
22 * cases, so that access to SOC-integrated GPIOs can sometimes cost
23 * only an instruction or two per bit.
27 /* When debugging, extend minimal trust to callers and platform code.
28 * Also emit diagnostic messages that may help initial bringup, when
29 * board setup or driver bugs are most common.
31 * Otherwise, minimize overhead in what may be bitbanging codepaths.
34 #define extra_checks 1
36 #define extra_checks 0
39 /* gpio_lock prevents conflicts during gpio_desc[] table updates.
40 * While any GPIO is requested, its gpio_chip is not removable;
41 * each GPIO's "requested" flag serves as a lock and refcount.
43 static DEFINE_SPINLOCK(gpio_lock
);
46 struct gpio_chip
*chip
;
48 /* flag symbols are bit numbers */
49 #define FLAG_REQUESTED 0
51 #define FLAG_RESERVED 2
52 #define FLAG_EXPORT 3 /* protected by sysfs_lock */
53 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
54 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */
55 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */
56 #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
58 #define PDESC_ID_SHIFT 16 /* add new flags before this one */
60 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1)
61 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
63 #ifdef CONFIG_DEBUG_FS
67 static struct gpio_desc gpio_desc
[ARCH_NR_GPIOS
];
69 #ifdef CONFIG_GPIO_SYSFS
71 struct work_struct work
;
72 struct sysfs_dirent
*value_sd
;
75 static struct idr pdesc_idr
;
78 static inline void desc_set_label(struct gpio_desc
*d
, const char *label
)
80 #ifdef CONFIG_DEBUG_FS
85 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised
86 * when setting direction, and otherwise illegal. Until board setup code
87 * and drivers use explicit requests everywhere (which won't happen when
88 * those calls have no teeth) we can't avoid autorequesting. This nag
89 * message should motivate switching to explicit requests... so should
90 * the weaker cleanup after faults, compared to gpio_request().
92 * NOTE: the autorequest mechanism is going away; at this point it's
93 * only "legal" in the sense that (old) code using it won't break yet,
94 * but instead only triggers a WARN() stack dump.
96 static int gpio_ensure_requested(struct gpio_desc
*desc
, unsigned offset
)
98 const struct gpio_chip
*chip
= desc
->chip
;
99 const int gpio
= chip
->base
+ offset
;
101 if (WARN(test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0,
102 "autorequest GPIO-%d\n", gpio
)) {
103 if (!try_module_get(chip
->owner
)) {
104 pr_err("GPIO-%d: module can't be gotten \n", gpio
);
105 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
109 desc_set_label(desc
, "[auto]");
110 /* caller must chip->request() w/o spinlock */
117 /* caller holds gpio_lock *OR* gpio is marked as requested */
118 static inline struct gpio_chip
*gpio_to_chip(unsigned gpio
)
120 return gpio_desc
[gpio
].chip
;
123 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
124 static int gpiochip_find_base(int ngpio
)
130 for (i
= ARCH_NR_GPIOS
- 1; i
>= 0 ; i
--) {
131 struct gpio_desc
*desc
= &gpio_desc
[i
];
132 struct gpio_chip
*chip
= desc
->chip
;
134 if (!chip
&& !test_bit(FLAG_RESERVED
, &desc
->flags
)) {
136 if (spare
== ngpio
) {
143 i
-= chip
->ngpio
- 1;
147 if (gpio_is_valid(base
))
148 pr_debug("%s: found new base at %d\n", __func__
, base
);
153 * gpiochip_reserve() - reserve range of gpios to use with platform code only
154 * @start: starting gpio number
155 * @ngpio: number of gpios to reserve
156 * Context: platform init, potentially before irqs or kmalloc will work
158 * Returns a negative errno if any gpio within the range is already reserved
159 * or registered, else returns zero as a success code. Use this function
160 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
161 * for example because its driver support is not yet loaded.
163 int __init
gpiochip_reserve(int start
, int ngpio
)
169 if (!gpio_is_valid(start
) || !gpio_is_valid(start
+ ngpio
- 1))
172 spin_lock_irqsave(&gpio_lock
, flags
);
174 for (i
= start
; i
< start
+ ngpio
; i
++) {
175 struct gpio_desc
*desc
= &gpio_desc
[i
];
177 if (desc
->chip
|| test_bit(FLAG_RESERVED
, &desc
->flags
)) {
182 set_bit(FLAG_RESERVED
, &desc
->flags
);
185 pr_debug("%s: reserved gpios from %d to %d\n",
186 __func__
, start
, start
+ ngpio
- 1);
188 spin_unlock_irqrestore(&gpio_lock
, flags
);
193 #ifdef CONFIG_GPIO_SYSFS
195 /* lock protects against unexport_gpio() being called while
196 * sysfs files are active.
198 static DEFINE_MUTEX(sysfs_lock
);
201 * /sys/class/gpio/gpioN... only for GPIOs that are exported
203 * * MAY BE OMITTED if kernel won't allow direction changes
204 * * is read/write as "in" or "out"
205 * * may also be written as "high" or "low", initializing
206 * output value as specified ("out" implies "low")
208 * * always readable, subject to hardware behavior
209 * * may be writable, as zero/nonzero
211 * * configures behavior of poll(2) on /value
212 * * available only if pin can generate IRQs on input
213 * * is read/write as "none", "falling", "rising", or "both"
215 * * configures polarity of /value
216 * * is read/write as zero/nonzero
217 * * also affects existing and subsequent "falling" and "rising"
218 * /edge configuration
221 static ssize_t
gpio_direction_show(struct device
*dev
,
222 struct device_attribute
*attr
, char *buf
)
224 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
227 mutex_lock(&sysfs_lock
);
229 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
232 status
= sprintf(buf
, "%s\n",
233 test_bit(FLAG_IS_OUT
, &desc
->flags
)
236 mutex_unlock(&sysfs_lock
);
240 static ssize_t
gpio_direction_store(struct device
*dev
,
241 struct device_attribute
*attr
, const char *buf
, size_t size
)
243 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
244 unsigned gpio
= desc
- gpio_desc
;
247 mutex_lock(&sysfs_lock
);
249 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
251 else if (sysfs_streq(buf
, "high"))
252 status
= gpio_direction_output(gpio
, 1);
253 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
254 status
= gpio_direction_output(gpio
, 0);
255 else if (sysfs_streq(buf
, "in"))
256 status
= gpio_direction_input(gpio
);
260 mutex_unlock(&sysfs_lock
);
261 return status
? : size
;
264 static /* const */ DEVICE_ATTR(direction
, 0644,
265 gpio_direction_show
, gpio_direction_store
);
267 static ssize_t
gpio_value_show(struct device
*dev
,
268 struct device_attribute
*attr
, char *buf
)
270 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
271 unsigned gpio
= desc
- gpio_desc
;
274 mutex_lock(&sysfs_lock
);
276 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
281 value
= !!gpio_get_value_cansleep(gpio
);
282 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
285 status
= sprintf(buf
, "%d\n", value
);
288 mutex_unlock(&sysfs_lock
);
292 static ssize_t
gpio_value_store(struct device
*dev
,
293 struct device_attribute
*attr
, const char *buf
, size_t size
)
295 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
296 unsigned gpio
= desc
- gpio_desc
;
299 mutex_lock(&sysfs_lock
);
301 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
303 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
308 status
= strict_strtol(buf
, 0, &value
);
310 if (test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
))
312 gpio_set_value_cansleep(gpio
, value
!= 0);
317 mutex_unlock(&sysfs_lock
);
321 static const DEVICE_ATTR(value
, 0644,
322 gpio_value_show
, gpio_value_store
);
324 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
326 struct work_struct
*work
= priv
;
332 static void gpio_notify_sysfs(struct work_struct
*work
)
334 struct poll_desc
*pdesc
;
336 pdesc
= container_of(work
, struct poll_desc
, work
);
337 sysfs_notify_dirent(pdesc
->value_sd
);
340 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
341 unsigned long gpio_flags
)
343 struct poll_desc
*pdesc
;
344 unsigned long irq_flags
;
347 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
350 irq
= gpio_to_irq(desc
- gpio_desc
);
354 id
= desc
->flags
>> PDESC_ID_SHIFT
;
355 pdesc
= idr_find(&pdesc_idr
, id
);
357 free_irq(irq
, &pdesc
->work
);
358 cancel_work_sync(&pdesc
->work
);
361 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
368 irq_flags
= IRQF_SHARED
;
369 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
370 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
371 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
372 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
373 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
374 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
377 pdesc
= kmalloc(sizeof(*pdesc
), GFP_KERNEL
);
385 if (idr_pre_get(&pdesc_idr
, GFP_KERNEL
))
386 ret
= idr_get_new_above(&pdesc_idr
,
388 } while (ret
== -EAGAIN
);
393 desc
->flags
&= GPIO_FLAGS_MASK
;
394 desc
->flags
|= (unsigned long)id
<< PDESC_ID_SHIFT
;
396 if (desc
->flags
>> PDESC_ID_SHIFT
!= id
) {
401 pdesc
->value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, "value");
402 if (!pdesc
->value_sd
) {
406 INIT_WORK(&pdesc
->work
, gpio_notify_sysfs
);
409 ret
= request_irq(irq
, gpio_sysfs_irq
, irq_flags
,
410 "gpiolib", &pdesc
->work
);
414 desc
->flags
|= gpio_flags
;
418 sysfs_put(pdesc
->value_sd
);
420 idr_remove(&pdesc_idr
, id
);
421 desc
->flags
&= GPIO_FLAGS_MASK
;
428 static const struct {
431 } trigger_types
[] = {
433 { "falling", BIT(FLAG_TRIG_FALL
) },
434 { "rising", BIT(FLAG_TRIG_RISE
) },
435 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
438 static ssize_t
gpio_edge_show(struct device
*dev
,
439 struct device_attribute
*attr
, char *buf
)
441 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
444 mutex_lock(&sysfs_lock
);
446 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
452 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
453 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
454 == trigger_types
[i
].flags
) {
455 status
= sprintf(buf
, "%s\n",
456 trigger_types
[i
].name
);
461 mutex_unlock(&sysfs_lock
);
465 static ssize_t
gpio_edge_store(struct device
*dev
,
466 struct device_attribute
*attr
, const char *buf
, size_t size
)
468 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
472 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
473 if (sysfs_streq(trigger_types
[i
].name
, buf
))
478 mutex_lock(&sysfs_lock
);
480 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
483 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
488 mutex_unlock(&sysfs_lock
);
493 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
495 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
500 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
504 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
506 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
508 /* reconfigure poll(2) support if enabled on one edge only */
509 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
510 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
511 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
513 gpio_setup_irq(desc
, dev
, 0);
514 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
520 static ssize_t
gpio_active_low_show(struct device
*dev
,
521 struct device_attribute
*attr
, char *buf
)
523 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
526 mutex_lock(&sysfs_lock
);
528 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
531 status
= sprintf(buf
, "%d\n",
532 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
534 mutex_unlock(&sysfs_lock
);
539 static ssize_t
gpio_active_low_store(struct device
*dev
,
540 struct device_attribute
*attr
, const char *buf
, size_t size
)
542 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
545 mutex_lock(&sysfs_lock
);
547 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
552 status
= strict_strtol(buf
, 0, &value
);
554 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
557 mutex_unlock(&sysfs_lock
);
559 return status
? : size
;
562 static const DEVICE_ATTR(active_low
, 0644,
563 gpio_active_low_show
, gpio_active_low_store
);
565 static const struct attribute
*gpio_attrs
[] = {
566 &dev_attr_value
.attr
,
567 &dev_attr_active_low
.attr
,
571 static const struct attribute_group gpio_attr_group
= {
572 .attrs
= (struct attribute
**) gpio_attrs
,
576 * /sys/class/gpio/gpiochipN/
577 * /base ... matching gpio_chip.base (N)
578 * /label ... matching gpio_chip.label
579 * /ngpio ... matching gpio_chip.ngpio
582 static ssize_t
chip_base_show(struct device
*dev
,
583 struct device_attribute
*attr
, char *buf
)
585 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
587 return sprintf(buf
, "%d\n", chip
->base
);
589 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
591 static ssize_t
chip_label_show(struct device
*dev
,
592 struct device_attribute
*attr
, char *buf
)
594 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
596 return sprintf(buf
, "%s\n", chip
->label
? : "");
598 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
600 static ssize_t
chip_ngpio_show(struct device
*dev
,
601 struct device_attribute
*attr
, char *buf
)
603 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
605 return sprintf(buf
, "%u\n", chip
->ngpio
);
607 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
609 static const struct attribute
*gpiochip_attrs
[] = {
611 &dev_attr_label
.attr
,
612 &dev_attr_ngpio
.attr
,
616 static const struct attribute_group gpiochip_attr_group
= {
617 .attrs
= (struct attribute
**) gpiochip_attrs
,
621 * /sys/class/gpio/export ... write-only
622 * integer N ... number of GPIO to export (full access)
623 * /sys/class/gpio/unexport ... write-only
624 * integer N ... number of GPIO to unexport
626 static ssize_t
export_store(struct class *class, const char *buf
, size_t len
)
631 status
= strict_strtol(buf
, 0, &gpio
);
635 /* No extra locking here; FLAG_SYSFS just signifies that the
636 * request and export were done by on behalf of userspace, so
637 * they may be undone on its behalf too.
640 status
= gpio_request(gpio
, "sysfs");
644 status
= gpio_export(gpio
, true);
648 set_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
);
652 pr_debug("%s: status %d\n", __func__
, status
);
653 return status
? : len
;
656 static ssize_t
unexport_store(struct class *class, const char *buf
, size_t len
)
661 status
= strict_strtol(buf
, 0, &gpio
);
667 /* reject bogus commands (gpio_unexport ignores them) */
668 if (!gpio_is_valid(gpio
))
671 /* No extra locking here; FLAG_SYSFS just signifies that the
672 * request and export were done by on behalf of userspace, so
673 * they may be undone on its behalf too.
675 if (test_and_clear_bit(FLAG_SYSFS
, &gpio_desc
[gpio
].flags
)) {
681 pr_debug("%s: status %d\n", __func__
, status
);
682 return status
? : len
;
685 static struct class_attribute gpio_class_attrs
[] = {
686 __ATTR(export
, 0200, NULL
, export_store
),
687 __ATTR(unexport
, 0200, NULL
, unexport_store
),
691 static struct class gpio_class
= {
693 .owner
= THIS_MODULE
,
695 .class_attrs
= gpio_class_attrs
,
700 * gpio_export - export a GPIO through sysfs
701 * @gpio: gpio to make available, already requested
702 * @direction_may_change: true if userspace may change gpio direction
703 * Context: arch_initcall or later
705 * When drivers want to make a GPIO accessible to userspace after they
706 * have requested it -- perhaps while debugging, or as part of their
707 * public interface -- they may use this routine. If the GPIO can
708 * change direction (some can't) and the caller allows it, userspace
709 * will see "direction" sysfs attribute which may be used to change
710 * the gpio's direction. A "value" attribute will always be provided.
712 * Returns zero on success, else an error.
714 int gpio_export(unsigned gpio
, bool direction_may_change
)
717 struct gpio_desc
*desc
;
718 int status
= -EINVAL
;
721 /* can't export until sysfs is available ... */
723 pr_debug("%s: called too early!\n", __func__
);
727 if (!gpio_is_valid(gpio
))
730 mutex_lock(&sysfs_lock
);
732 spin_lock_irqsave(&gpio_lock
, flags
);
733 desc
= &gpio_desc
[gpio
];
734 if (test_bit(FLAG_REQUESTED
, &desc
->flags
)
735 && !test_bit(FLAG_EXPORT
, &desc
->flags
)) {
737 if (!desc
->chip
->direction_input
738 || !desc
->chip
->direction_output
)
739 direction_may_change
= false;
741 spin_unlock_irqrestore(&gpio_lock
, flags
);
743 if (desc
->chip
->names
&& desc
->chip
->names
[gpio
- desc
->chip
->base
])
744 ioname
= desc
->chip
->names
[gpio
- desc
->chip
->base
];
749 dev
= device_create(&gpio_class
, desc
->chip
->dev
, MKDEV(0, 0),
750 desc
, ioname
? ioname
: "gpio%d", gpio
);
752 status
= sysfs_create_group(&dev
->kobj
,
755 if (!status
&& direction_may_change
)
756 status
= device_create_file(dev
,
757 &dev_attr_direction
);
759 if (!status
&& gpio_to_irq(gpio
) >= 0
760 && (direction_may_change
761 || !test_bit(FLAG_IS_OUT
,
763 status
= device_create_file(dev
,
767 device_unregister(dev
);
769 status
= PTR_ERR(dev
);
771 set_bit(FLAG_EXPORT
, &desc
->flags
);
774 mutex_unlock(&sysfs_lock
);
778 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
782 EXPORT_SYMBOL_GPL(gpio_export
);
784 static int match_export(struct device
*dev
, void *data
)
786 return dev_get_drvdata(dev
) == data
;
790 * gpio_export_link - create a sysfs link to an exported GPIO node
791 * @dev: device under which to create symlink
792 * @name: name of the symlink
793 * @gpio: gpio to create symlink to, already exported
795 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
796 * node. Caller is responsible for unlinking.
798 * Returns zero on success, else an error.
800 int gpio_export_link(struct device
*dev
, const char *name
, unsigned gpio
)
802 struct gpio_desc
*desc
;
803 int status
= -EINVAL
;
805 if (!gpio_is_valid(gpio
))
808 mutex_lock(&sysfs_lock
);
810 desc
= &gpio_desc
[gpio
];
812 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
815 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
817 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
824 mutex_unlock(&sysfs_lock
);
828 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
832 EXPORT_SYMBOL_GPL(gpio_export_link
);
836 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
837 * @gpio: gpio to change
838 * @value: non-zero to use active low, i.e. inverted values
840 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
841 * The GPIO does not have to be exported yet. If poll(2) support has
842 * been enabled for either rising or falling edge, it will be
843 * reconfigured to follow the new polarity.
845 * Returns zero on success, else an error.
847 int gpio_sysfs_set_active_low(unsigned gpio
, int value
)
849 struct gpio_desc
*desc
;
850 struct device
*dev
= NULL
;
851 int status
= -EINVAL
;
853 if (!gpio_is_valid(gpio
))
856 mutex_lock(&sysfs_lock
);
858 desc
= &gpio_desc
[gpio
];
860 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
863 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
870 status
= sysfs_set_active_low(desc
, dev
, value
);
873 mutex_unlock(&sysfs_lock
);
877 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
881 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low
);
884 * gpio_unexport - reverse effect of gpio_export()
885 * @gpio: gpio to make unavailable
887 * This is implicit on gpio_free().
889 void gpio_unexport(unsigned gpio
)
891 struct gpio_desc
*desc
;
892 int status
= -EINVAL
;
894 if (!gpio_is_valid(gpio
))
897 mutex_lock(&sysfs_lock
);
899 desc
= &gpio_desc
[gpio
];
901 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
902 struct device
*dev
= NULL
;
904 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
906 gpio_setup_irq(desc
, dev
, 0);
907 clear_bit(FLAG_EXPORT
, &desc
->flags
);
909 device_unregister(dev
);
915 mutex_unlock(&sysfs_lock
);
918 pr_debug("%s: gpio%d status %d\n", __func__
, gpio
, status
);
920 EXPORT_SYMBOL_GPL(gpio_unexport
);
922 static int gpiochip_export(struct gpio_chip
*chip
)
927 /* Many systems register gpio chips for SOC support very early,
928 * before driver model support is available. In those cases we
929 * export this later, in gpiolib_sysfs_init() ... here we just
930 * verify that _some_ field of gpio_class got initialized.
935 /* use chip->base for the ID; it's already known to be unique */
936 mutex_lock(&sysfs_lock
);
937 dev
= device_create(&gpio_class
, chip
->dev
, MKDEV(0, 0), chip
,
938 "gpiochip%d", chip
->base
);
940 status
= sysfs_create_group(&dev
->kobj
,
941 &gpiochip_attr_group
);
943 status
= PTR_ERR(dev
);
944 chip
->exported
= (status
== 0);
945 mutex_unlock(&sysfs_lock
);
951 spin_lock_irqsave(&gpio_lock
, flags
);
953 while (gpio_desc
[gpio
].chip
== chip
)
954 gpio_desc
[gpio
++].chip
= NULL
;
955 spin_unlock_irqrestore(&gpio_lock
, flags
);
957 pr_debug("%s: chip %s status %d\n", __func__
,
958 chip
->label
, status
);
964 static void gpiochip_unexport(struct gpio_chip
*chip
)
969 mutex_lock(&sysfs_lock
);
970 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
973 device_unregister(dev
);
978 mutex_unlock(&sysfs_lock
);
981 pr_debug("%s: chip %s status %d\n", __func__
,
982 chip
->label
, status
);
985 static int __init
gpiolib_sysfs_init(void)
991 idr_init(&pdesc_idr
);
993 status
= class_register(&gpio_class
);
997 /* Scan and register the gpio_chips which registered very
998 * early (e.g. before the class_register above was called).
1000 * We run before arch_initcall() so chip->dev nodes can have
1001 * registered, and so arch_initcall() can always gpio_export().
1003 spin_lock_irqsave(&gpio_lock
, flags
);
1004 for (gpio
= 0; gpio
< ARCH_NR_GPIOS
; gpio
++) {
1005 struct gpio_chip
*chip
;
1007 chip
= gpio_desc
[gpio
].chip
;
1008 if (!chip
|| chip
->exported
)
1011 spin_unlock_irqrestore(&gpio_lock
, flags
);
1012 status
= gpiochip_export(chip
);
1013 spin_lock_irqsave(&gpio_lock
, flags
);
1015 spin_unlock_irqrestore(&gpio_lock
, flags
);
1020 postcore_initcall(gpiolib_sysfs_init
);
1023 static inline int gpiochip_export(struct gpio_chip
*chip
)
1028 static inline void gpiochip_unexport(struct gpio_chip
*chip
)
1032 #endif /* CONFIG_GPIO_SYSFS */
1035 * gpiochip_add() - register a gpio_chip
1036 * @chip: the chip to register, with chip->base initialized
1037 * Context: potentially before irqs or kmalloc will work
1039 * Returns a negative errno if the chip can't be registered, such as
1040 * because the chip->base is invalid or already associated with a
1041 * different chip. Otherwise it returns zero as a success code.
1043 * When gpiochip_add() is called very early during boot, so that GPIOs
1044 * can be freely used, the chip->dev device must be registered before
1045 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1046 * for GPIOs will fail rudely.
1048 * If chip->base is negative, this requests dynamic assignment of
1049 * a range of valid GPIOs.
1051 int gpiochip_add(struct gpio_chip
*chip
)
1053 unsigned long flags
;
1056 int base
= chip
->base
;
1058 if ((!gpio_is_valid(base
) || !gpio_is_valid(base
+ chip
->ngpio
- 1))
1064 spin_lock_irqsave(&gpio_lock
, flags
);
1067 base
= gpiochip_find_base(chip
->ngpio
);
1075 /* these GPIO numbers must not be managed by another gpio_chip */
1076 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
1077 if (gpio_desc
[id
].chip
!= NULL
) {
1083 for (id
= base
; id
< base
+ chip
->ngpio
; id
++) {
1084 gpio_desc
[id
].chip
= chip
;
1086 /* REVISIT: most hardware initializes GPIOs as
1087 * inputs (often with pullups enabled) so power
1088 * usage is minimized. Linux code should set the
1089 * gpio direction first thing; but until it does,
1090 * we may expose the wrong direction in sysfs.
1092 gpio_desc
[id
].flags
= !chip
->direction_input
1093 ? (1 << FLAG_IS_OUT
)
1099 spin_unlock_irqrestore(&gpio_lock
, flags
);
1101 status
= gpiochip_export(chip
);
1103 /* failures here can mean systems won't boot... */
1105 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n",
1106 chip
->base
, chip
->base
+ chip
->ngpio
- 1,
1107 chip
->label
? : "generic");
1110 EXPORT_SYMBOL_GPL(gpiochip_add
);
1113 * gpiochip_remove() - unregister a gpio_chip
1114 * @chip: the chip to unregister
1116 * A gpio_chip with any GPIOs still requested may not be removed.
1118 int gpiochip_remove(struct gpio_chip
*chip
)
1120 unsigned long flags
;
1124 spin_lock_irqsave(&gpio_lock
, flags
);
1126 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++) {
1127 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[id
].flags
)) {
1133 for (id
= chip
->base
; id
< chip
->base
+ chip
->ngpio
; id
++)
1134 gpio_desc
[id
].chip
= NULL
;
1137 spin_unlock_irqrestore(&gpio_lock
, flags
);
1140 gpiochip_unexport(chip
);
1144 EXPORT_SYMBOL_GPL(gpiochip_remove
);
1147 /* These "optional" allocation calls help prevent drivers from stomping
1148 * on each other, and help provide better diagnostics in debugfs.
1149 * They're called even less than the "set direction" calls.
1151 int gpio_request(unsigned gpio
, const char *label
)
1153 struct gpio_desc
*desc
;
1154 struct gpio_chip
*chip
;
1155 int status
= -EINVAL
;
1156 unsigned long flags
;
1158 spin_lock_irqsave(&gpio_lock
, flags
);
1160 if (!gpio_is_valid(gpio
))
1162 desc
= &gpio_desc
[gpio
];
1167 if (!try_module_get(chip
->owner
))
1170 /* NOTE: gpio_request() can be called in early boot,
1171 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
1174 if (test_and_set_bit(FLAG_REQUESTED
, &desc
->flags
) == 0) {
1175 desc_set_label(desc
, label
? : "?");
1179 module_put(chip
->owner
);
1183 if (chip
->request
) {
1184 /* chip->request may sleep */
1185 spin_unlock_irqrestore(&gpio_lock
, flags
);
1186 status
= chip
->request(chip
, gpio
- chip
->base
);
1187 spin_lock_irqsave(&gpio_lock
, flags
);
1190 desc_set_label(desc
, NULL
);
1191 module_put(chip
->owner
);
1192 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1198 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1199 gpio
, label
? : "?", status
);
1200 spin_unlock_irqrestore(&gpio_lock
, flags
);
1203 EXPORT_SYMBOL_GPL(gpio_request
);
1205 void gpio_free(unsigned gpio
)
1207 unsigned long flags
;
1208 struct gpio_desc
*desc
;
1209 struct gpio_chip
*chip
;
1213 if (!gpio_is_valid(gpio
)) {
1214 WARN_ON(extra_checks
);
1218 gpio_unexport(gpio
);
1220 spin_lock_irqsave(&gpio_lock
, flags
);
1222 desc
= &gpio_desc
[gpio
];
1224 if (chip
&& test_bit(FLAG_REQUESTED
, &desc
->flags
)) {
1226 spin_unlock_irqrestore(&gpio_lock
, flags
);
1227 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1228 chip
->free(chip
, gpio
- chip
->base
);
1229 spin_lock_irqsave(&gpio_lock
, flags
);
1231 desc_set_label(desc
, NULL
);
1232 module_put(desc
->chip
->owner
);
1233 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
1234 clear_bit(FLAG_REQUESTED
, &desc
->flags
);
1236 WARN_ON(extra_checks
);
1238 spin_unlock_irqrestore(&gpio_lock
, flags
);
1240 EXPORT_SYMBOL_GPL(gpio_free
);
1244 * gpiochip_is_requested - return string iff signal was requested
1245 * @chip: controller managing the signal
1246 * @offset: of signal within controller's 0..(ngpio - 1) range
1248 * Returns NULL if the GPIO is not currently requested, else a string.
1249 * If debugfs support is enabled, the string returned is the label passed
1250 * to gpio_request(); otherwise it is a meaningless constant.
1252 * This function is for use by GPIO controller drivers. The label can
1253 * help with diagnostics, and knowing that the signal is used as a GPIO
1254 * can help avoid accidentally multiplexing it to another controller.
1256 const char *gpiochip_is_requested(struct gpio_chip
*chip
, unsigned offset
)
1258 unsigned gpio
= chip
->base
+ offset
;
1260 if (!gpio_is_valid(gpio
) || gpio_desc
[gpio
].chip
!= chip
)
1262 if (test_bit(FLAG_REQUESTED
, &gpio_desc
[gpio
].flags
) == 0)
1264 #ifdef CONFIG_DEBUG_FS
1265 return gpio_desc
[gpio
].label
;
1270 EXPORT_SYMBOL_GPL(gpiochip_is_requested
);
1273 /* Drivers MUST set GPIO direction before making get/set calls. In
1274 * some cases this is done in early boot, before IRQs are enabled.
1276 * As a rule these aren't called more than once (except for drivers
1277 * using the open-drain emulation idiom) so these are natural places
1278 * to accumulate extra debugging checks. Note that we can't (yet)
1279 * rely on gpio_request() having been called beforehand.
1282 int gpio_direction_input(unsigned gpio
)
1284 unsigned long flags
;
1285 struct gpio_chip
*chip
;
1286 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1287 int status
= -EINVAL
;
1289 spin_lock_irqsave(&gpio_lock
, flags
);
1291 if (!gpio_is_valid(gpio
))
1294 if (!chip
|| !chip
->get
|| !chip
->direction_input
)
1297 if (gpio
>= chip
->ngpio
)
1299 status
= gpio_ensure_requested(desc
, gpio
);
1303 /* now we know the gpio is valid and chip won't vanish */
1305 spin_unlock_irqrestore(&gpio_lock
, flags
);
1307 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1310 status
= chip
->request(chip
, gpio
);
1312 pr_debug("GPIO-%d: chip request fail, %d\n",
1313 chip
->base
+ gpio
, status
);
1314 /* and it's not available to anyone else ...
1315 * gpio_request() is the fully clean solution.
1321 status
= chip
->direction_input(chip
, gpio
);
1323 clear_bit(FLAG_IS_OUT
, &desc
->flags
);
1327 spin_unlock_irqrestore(&gpio_lock
, flags
);
1329 pr_debug("%s: gpio-%d status %d\n",
1330 __func__
, gpio
, status
);
1333 EXPORT_SYMBOL_GPL(gpio_direction_input
);
1335 int gpio_direction_output(unsigned gpio
, int value
)
1337 unsigned long flags
;
1338 struct gpio_chip
*chip
;
1339 struct gpio_desc
*desc
= &gpio_desc
[gpio
];
1340 int status
= -EINVAL
;
1342 spin_lock_irqsave(&gpio_lock
, flags
);
1344 if (!gpio_is_valid(gpio
))
1347 if (!chip
|| !chip
->set
|| !chip
->direction_output
)
1350 if (gpio
>= chip
->ngpio
)
1352 status
= gpio_ensure_requested(desc
, gpio
);
1356 /* now we know the gpio is valid and chip won't vanish */
1358 spin_unlock_irqrestore(&gpio_lock
, flags
);
1360 might_sleep_if(extra_checks
&& chip
->can_sleep
);
1363 status
= chip
->request(chip
, gpio
);
1365 pr_debug("GPIO-%d: chip request fail, %d\n",
1366 chip
->base
+ gpio
, status
);
1367 /* and it's not available to anyone else ...
1368 * gpio_request() is the fully clean solution.
1374 status
= chip
->direction_output(chip
, gpio
, value
);
1376 set_bit(FLAG_IS_OUT
, &desc
->flags
);
1380 spin_unlock_irqrestore(&gpio_lock
, flags
);
1382 pr_debug("%s: gpio-%d status %d\n",
1383 __func__
, gpio
, status
);
1386 EXPORT_SYMBOL_GPL(gpio_direction_output
);
1389 /* I/O calls are only valid after configuration completed; the relevant
1390 * "is this a valid GPIO" error checks should already have been done.
1392 * "Get" operations are often inlinable as reading a pin value register,
1393 * and masking the relevant bit in that register.
1395 * When "set" operations are inlinable, they involve writing that mask to
1396 * one register to set a low value, or a different register to set it high.
1397 * Otherwise locking is needed, so there may be little value to inlining.
1399 *------------------------------------------------------------------------
1401 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1402 * have requested the GPIO. That can include implicit requesting by
1403 * a direction setting call. Marking a gpio as requested locks its chip
1404 * in memory, guaranteeing that these table lookups need no more locking
1405 * and that gpiochip_remove() will fail.
1407 * REVISIT when debugging, consider adding some instrumentation to ensure
1408 * that the GPIO was actually requested.
1412 * __gpio_get_value() - return a gpio's value
1413 * @gpio: gpio whose value will be returned
1416 * This is used directly or indirectly to implement gpio_get_value().
1417 * It returns the zero or nonzero value provided by the associated
1418 * gpio_chip.get() method; or zero if no such method is provided.
1420 int __gpio_get_value(unsigned gpio
)
1422 struct gpio_chip
*chip
;
1424 chip
= gpio_to_chip(gpio
);
1425 WARN_ON(extra_checks
&& chip
->can_sleep
);
1426 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1428 EXPORT_SYMBOL_GPL(__gpio_get_value
);
1431 * __gpio_set_value() - assign a gpio's value
1432 * @gpio: gpio whose value will be assigned
1433 * @value: value to assign
1436 * This is used directly or indirectly to implement gpio_set_value().
1437 * It invokes the associated gpio_chip.set() method.
1439 void __gpio_set_value(unsigned gpio
, int value
)
1441 struct gpio_chip
*chip
;
1443 chip
= gpio_to_chip(gpio
);
1444 WARN_ON(extra_checks
&& chip
->can_sleep
);
1445 chip
->set(chip
, gpio
- chip
->base
, value
);
1447 EXPORT_SYMBOL_GPL(__gpio_set_value
);
1450 * __gpio_cansleep() - report whether gpio value access will sleep
1451 * @gpio: gpio in question
1454 * This is used directly or indirectly to implement gpio_cansleep(). It
1455 * returns nonzero if access reading or writing the GPIO value can sleep.
1457 int __gpio_cansleep(unsigned gpio
)
1459 struct gpio_chip
*chip
;
1461 /* only call this on GPIOs that are valid! */
1462 chip
= gpio_to_chip(gpio
);
1464 return chip
->can_sleep
;
1466 EXPORT_SYMBOL_GPL(__gpio_cansleep
);
1469 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1470 * @gpio: gpio whose IRQ will be returned (already requested)
1473 * This is used directly or indirectly to implement gpio_to_irq().
1474 * It returns the number of the IRQ signaled by this (input) GPIO,
1475 * or a negative errno.
1477 int __gpio_to_irq(unsigned gpio
)
1479 struct gpio_chip
*chip
;
1481 chip
= gpio_to_chip(gpio
);
1482 return chip
->to_irq
? chip
->to_irq(chip
, gpio
- chip
->base
) : -ENXIO
;
1484 EXPORT_SYMBOL_GPL(__gpio_to_irq
);
1488 /* There's no value in making it easy to inline GPIO calls that may sleep.
1489 * Common examples include ones connected to I2C or SPI chips.
1492 int gpio_get_value_cansleep(unsigned gpio
)
1494 struct gpio_chip
*chip
;
1496 might_sleep_if(extra_checks
);
1497 chip
= gpio_to_chip(gpio
);
1498 return chip
->get
? chip
->get(chip
, gpio
- chip
->base
) : 0;
1500 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep
);
1502 void gpio_set_value_cansleep(unsigned gpio
, int value
)
1504 struct gpio_chip
*chip
;
1506 might_sleep_if(extra_checks
);
1507 chip
= gpio_to_chip(gpio
);
1508 chip
->set(chip
, gpio
- chip
->base
, value
);
1510 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep
);
1513 #ifdef CONFIG_DEBUG_FS
1515 static void gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1518 unsigned gpio
= chip
->base
;
1519 struct gpio_desc
*gdesc
= &gpio_desc
[gpio
];
1522 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++, gdesc
++) {
1523 if (!test_bit(FLAG_REQUESTED
, &gdesc
->flags
))
1526 is_out
= test_bit(FLAG_IS_OUT
, &gdesc
->flags
);
1527 seq_printf(s
, " gpio-%-3d (%-20.20s) %s %s",
1529 is_out
? "out" : "in ",
1531 ? (chip
->get(chip
, i
) ? "hi" : "lo")
1535 int irq
= gpio_to_irq(gpio
);
1536 struct irq_desc
*desc
= irq_to_desc(irq
);
1538 /* This races with request_irq(), set_irq_type(),
1539 * and set_irq_wake() ... but those are "rare".
1541 * More significantly, trigger type flags aren't
1542 * currently maintained by genirq.
1544 if (irq
>= 0 && desc
->action
) {
1547 switch (desc
->status
& IRQ_TYPE_SENSE_MASK
) {
1549 trigger
= "(default)";
1551 case IRQ_TYPE_EDGE_FALLING
:
1552 trigger
= "edge-falling";
1554 case IRQ_TYPE_EDGE_RISING
:
1555 trigger
= "edge-rising";
1557 case IRQ_TYPE_EDGE_BOTH
:
1558 trigger
= "edge-both";
1560 case IRQ_TYPE_LEVEL_HIGH
:
1561 trigger
= "level-high";
1563 case IRQ_TYPE_LEVEL_LOW
:
1564 trigger
= "level-low";
1567 trigger
= "?trigger?";
1571 seq_printf(s
, " irq-%d %s%s",
1573 (desc
->status
& IRQ_WAKEUP
)
1578 seq_printf(s
, "\n");
1582 static int gpiolib_show(struct seq_file
*s
, void *unused
)
1584 struct gpio_chip
*chip
= NULL
;
1588 /* REVISIT this isn't locked against gpio_chip removal ... */
1590 for (gpio
= 0; gpio_is_valid(gpio
); gpio
++) {
1593 if (chip
== gpio_desc
[gpio
].chip
)
1595 chip
= gpio_desc
[gpio
].chip
;
1599 seq_printf(s
, "%sGPIOs %d-%d",
1600 started
? "\n" : "",
1601 chip
->base
, chip
->base
+ chip
->ngpio
- 1);
1604 seq_printf(s
, ", %s/%s",
1605 dev
->bus
? dev
->bus
->name
: "no-bus",
1608 seq_printf(s
, ", %s", chip
->label
);
1609 if (chip
->can_sleep
)
1610 seq_printf(s
, ", can sleep");
1611 seq_printf(s
, ":\n");
1615 chip
->dbg_show(s
, chip
);
1617 gpiolib_dbg_show(s
, chip
);
1622 static int gpiolib_open(struct inode
*inode
, struct file
*file
)
1624 return single_open(file
, gpiolib_show
, NULL
);
1627 static const struct file_operations gpiolib_operations
= {
1628 .open
= gpiolib_open
,
1630 .llseek
= seq_lseek
,
1631 .release
= single_release
,
1634 static int __init
gpiolib_debugfs_init(void)
1636 /* /sys/kernel/debug/gpio */
1637 (void) debugfs_create_file("gpio", S_IFREG
| S_IRUGO
,
1638 NULL
, NULL
, &gpiolib_operations
);
1641 subsys_initcall(gpiolib_debugfs_init
);
1643 #endif /* DEBUG_FS */