2 #include <linux/mutex.h>
3 #include <linux/device.h>
4 #include <linux/sysfs.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/gpio/driver.h>
7 #include <linux/interrupt.h>
8 #include <linux/kdev_t.h>
12 static DEFINE_IDR(dirent_idr
);
15 /* lock protects against unexport_gpio() being called while
16 * sysfs files are active.
18 static DEFINE_MUTEX(sysfs_lock
);
21 * /sys/class/gpio/gpioN... only for GPIOs that are exported
23 * * MAY BE OMITTED if kernel won't allow direction changes
24 * * is read/write as "in" or "out"
25 * * may also be written as "high" or "low", initializing
26 * output value as specified ("out" implies "low")
28 * * always readable, subject to hardware behavior
29 * * may be writable, as zero/nonzero
31 * * configures behavior of poll(2) on /value
32 * * available only if pin can generate IRQs on input
33 * * is read/write as "none", "falling", "rising", or "both"
35 * * configures polarity of /value
36 * * is read/write as zero/nonzero
37 * * also affects existing and subsequent "falling" and "rising"
41 static ssize_t
gpio_direction_show(struct device
*dev
,
42 struct device_attribute
*attr
, char *buf
)
44 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
47 mutex_lock(&sysfs_lock
);
49 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
52 gpiod_get_direction(desc
);
53 status
= sprintf(buf
, "%s\n",
54 test_bit(FLAG_IS_OUT
, &desc
->flags
)
58 mutex_unlock(&sysfs_lock
);
62 static ssize_t
gpio_direction_store(struct device
*dev
,
63 struct device_attribute
*attr
, const char *buf
, size_t size
)
65 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
68 mutex_lock(&sysfs_lock
);
70 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
72 else if (sysfs_streq(buf
, "high"))
73 status
= gpiod_direction_output_raw(desc
, 1);
74 else if (sysfs_streq(buf
, "out") || sysfs_streq(buf
, "low"))
75 status
= gpiod_direction_output_raw(desc
, 0);
76 else if (sysfs_streq(buf
, "in"))
77 status
= gpiod_direction_input(desc
);
81 mutex_unlock(&sysfs_lock
);
82 return status
? : size
;
85 static /* const */ DEVICE_ATTR(direction
, 0644,
86 gpio_direction_show
, gpio_direction_store
);
88 static ssize_t
gpio_value_show(struct device
*dev
,
89 struct device_attribute
*attr
, char *buf
)
91 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
94 mutex_lock(&sysfs_lock
);
96 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
99 status
= sprintf(buf
, "%d\n", gpiod_get_value_cansleep(desc
));
101 mutex_unlock(&sysfs_lock
);
105 static ssize_t
gpio_value_store(struct device
*dev
,
106 struct device_attribute
*attr
, const char *buf
, size_t size
)
108 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
111 mutex_lock(&sysfs_lock
);
113 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
115 else if (!test_bit(FLAG_IS_OUT
, &desc
->flags
))
120 status
= kstrtol(buf
, 0, &value
);
122 gpiod_set_value_cansleep(desc
, value
);
127 mutex_unlock(&sysfs_lock
);
131 static DEVICE_ATTR(value
, 0644,
132 gpio_value_show
, gpio_value_store
);
134 static irqreturn_t
gpio_sysfs_irq(int irq
, void *priv
)
136 struct kernfs_node
*value_sd
= priv
;
138 sysfs_notify_dirent(value_sd
);
142 static int gpio_setup_irq(struct gpio_desc
*desc
, struct device
*dev
,
143 unsigned long gpio_flags
)
145 struct kernfs_node
*value_sd
;
146 unsigned long irq_flags
;
149 if ((desc
->flags
& GPIO_TRIGGER_MASK
) == gpio_flags
)
152 irq
= gpiod_to_irq(desc
);
156 id
= desc
->flags
>> ID_SHIFT
;
157 value_sd
= idr_find(&dirent_idr
, id
);
159 free_irq(irq
, value_sd
);
161 desc
->flags
&= ~GPIO_TRIGGER_MASK
;
164 gpiochip_unlock_as_irq(desc
->chip
, gpio_chip_hwgpio(desc
));
169 irq_flags
= IRQF_SHARED
;
170 if (test_bit(FLAG_TRIG_FALL
, &gpio_flags
))
171 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
172 IRQF_TRIGGER_RISING
: IRQF_TRIGGER_FALLING
;
173 if (test_bit(FLAG_TRIG_RISE
, &gpio_flags
))
174 irq_flags
|= test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) ?
175 IRQF_TRIGGER_FALLING
: IRQF_TRIGGER_RISING
;
178 value_sd
= sysfs_get_dirent(dev
->kobj
.sd
, "value");
184 ret
= idr_alloc(&dirent_idr
, value_sd
, 1, 0, GFP_KERNEL
);
189 desc
->flags
&= GPIO_FLAGS_MASK
;
190 desc
->flags
|= (unsigned long)id
<< ID_SHIFT
;
192 if (desc
->flags
>> ID_SHIFT
!= id
) {
199 * FIXME: This should be done in the irq_request_resources callback
200 * when the irq is requested, but a few drivers currently fail
203 * Remove this redundant call (along with the corresponding
204 * unlock) when those drivers have been fixed.
206 ret
= gpiochip_lock_as_irq(desc
->chip
, gpio_chip_hwgpio(desc
));
210 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
211 "gpiolib", value_sd
);
215 desc
->flags
|= gpio_flags
;
219 gpiochip_unlock_as_irq(desc
->chip
, gpio_chip_hwgpio(desc
));
221 idr_remove(&dirent_idr
, id
);
222 desc
->flags
&= GPIO_FLAGS_MASK
;
230 static const struct {
233 } trigger_types
[] = {
235 { "falling", BIT(FLAG_TRIG_FALL
) },
236 { "rising", BIT(FLAG_TRIG_RISE
) },
237 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
240 static ssize_t
gpio_edge_show(struct device
*dev
,
241 struct device_attribute
*attr
, char *buf
)
243 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
246 mutex_lock(&sysfs_lock
);
248 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
254 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
255 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
256 == trigger_types
[i
].flags
) {
257 status
= sprintf(buf
, "%s\n",
258 trigger_types
[i
].name
);
263 mutex_unlock(&sysfs_lock
);
267 static ssize_t
gpio_edge_store(struct device
*dev
,
268 struct device_attribute
*attr
, const char *buf
, size_t size
)
270 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
274 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
275 if (sysfs_streq(trigger_types
[i
].name
, buf
))
280 mutex_lock(&sysfs_lock
);
282 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
285 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
290 mutex_unlock(&sysfs_lock
);
295 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
297 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
302 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
306 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
308 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
310 /* reconfigure poll(2) support if enabled on one edge only */
311 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
312 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
313 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
315 gpio_setup_irq(desc
, dev
, 0);
316 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
322 static ssize_t
gpio_active_low_show(struct device
*dev
,
323 struct device_attribute
*attr
, char *buf
)
325 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
328 mutex_lock(&sysfs_lock
);
330 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
333 status
= sprintf(buf
, "%d\n",
334 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
336 mutex_unlock(&sysfs_lock
);
341 static ssize_t
gpio_active_low_store(struct device
*dev
,
342 struct device_attribute
*attr
, const char *buf
, size_t size
)
344 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
347 mutex_lock(&sysfs_lock
);
349 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
354 status
= kstrtol(buf
, 0, &value
);
356 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
359 mutex_unlock(&sysfs_lock
);
361 return status
? : size
;
364 static DEVICE_ATTR(active_low
, 0644,
365 gpio_active_low_show
, gpio_active_low_store
);
367 static umode_t
gpio_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
370 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
371 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
372 umode_t mode
= attr
->mode
;
373 bool show_direction
= test_bit(FLAG_SYSFS_DIR
, &desc
->flags
);
375 if (attr
== &dev_attr_direction
.attr
) {
378 } else if (attr
== &dev_attr_edge
.attr
) {
379 if (gpiod_to_irq(desc
) < 0)
381 if (!show_direction
&& test_bit(FLAG_IS_OUT
, &desc
->flags
))
388 static struct attribute
*gpio_attrs
[] = {
389 &dev_attr_direction
.attr
,
391 &dev_attr_value
.attr
,
392 &dev_attr_active_low
.attr
,
396 static const struct attribute_group gpio_group
= {
398 .is_visible
= gpio_is_visible
,
401 static const struct attribute_group
*gpio_groups
[] = {
407 * /sys/class/gpio/gpiochipN/
408 * /base ... matching gpio_chip.base (N)
409 * /label ... matching gpio_chip.label
410 * /ngpio ... matching gpio_chip.ngpio
413 static ssize_t
chip_base_show(struct device
*dev
,
414 struct device_attribute
*attr
, char *buf
)
416 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
418 return sprintf(buf
, "%d\n", chip
->base
);
420 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
422 static ssize_t
chip_label_show(struct device
*dev
,
423 struct device_attribute
*attr
, char *buf
)
425 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
427 return sprintf(buf
, "%s\n", chip
->label
? : "");
429 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
431 static ssize_t
chip_ngpio_show(struct device
*dev
,
432 struct device_attribute
*attr
, char *buf
)
434 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
436 return sprintf(buf
, "%u\n", chip
->ngpio
);
438 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
440 static struct attribute
*gpiochip_attrs
[] = {
442 &dev_attr_label
.attr
,
443 &dev_attr_ngpio
.attr
,
446 ATTRIBUTE_GROUPS(gpiochip
);
449 * /sys/class/gpio/export ... write-only
450 * integer N ... number of GPIO to export (full access)
451 * /sys/class/gpio/unexport ... write-only
452 * integer N ... number of GPIO to unexport
454 static ssize_t
export_store(struct class *class,
455 struct class_attribute
*attr
,
456 const char *buf
, size_t len
)
459 struct gpio_desc
*desc
;
462 status
= kstrtol(buf
, 0, &gpio
);
466 desc
= gpio_to_desc(gpio
);
467 /* reject invalid GPIOs */
469 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
473 /* No extra locking here; FLAG_SYSFS just signifies that the
474 * request and export were done by on behalf of userspace, so
475 * they may be undone on its behalf too.
478 status
= gpiod_request(desc
, "sysfs");
480 if (status
== -EPROBE_DEFER
)
484 status
= gpiod_export(desc
, true);
488 set_bit(FLAG_SYSFS
, &desc
->flags
);
492 pr_debug("%s: status %d\n", __func__
, status
);
493 return status
? : len
;
496 static ssize_t
unexport_store(struct class *class,
497 struct class_attribute
*attr
,
498 const char *buf
, size_t len
)
501 struct gpio_desc
*desc
;
504 status
= kstrtol(buf
, 0, &gpio
);
508 desc
= gpio_to_desc(gpio
);
509 /* reject bogus commands (gpio_unexport ignores them) */
511 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
517 /* No extra locking here; FLAG_SYSFS just signifies that the
518 * request and export were done by on behalf of userspace, so
519 * they may be undone on its behalf too.
521 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
)) {
527 pr_debug("%s: status %d\n", __func__
, status
);
528 return status
? : len
;
531 static struct class_attribute gpio_class_attrs
[] = {
532 __ATTR(export
, 0200, NULL
, export_store
),
533 __ATTR(unexport
, 0200, NULL
, unexport_store
),
537 static struct class gpio_class
= {
539 .owner
= THIS_MODULE
,
541 .class_attrs
= gpio_class_attrs
,
546 * gpiod_export - export a GPIO through sysfs
547 * @gpio: gpio to make available, already requested
548 * @direction_may_change: true if userspace may change gpio direction
549 * Context: arch_initcall or later
551 * When drivers want to make a GPIO accessible to userspace after they
552 * have requested it -- perhaps while debugging, or as part of their
553 * public interface -- they may use this routine. If the GPIO can
554 * change direction (some can't) and the caller allows it, userspace
555 * will see "direction" sysfs attribute which may be used to change
556 * the gpio's direction. A "value" attribute will always be provided.
558 * Returns zero on success, else an error.
560 int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
)
562 struct gpio_chip
*chip
;
565 const char *ioname
= NULL
;
569 /* can't export until sysfs is available ... */
571 pr_debug("%s: called too early!\n", __func__
);
576 pr_debug("%s: invalid gpio descriptor\n", __func__
);
582 mutex_lock(&sysfs_lock
);
584 /* check if chip is being removed */
585 if (!chip
|| !chip
->cdev
) {
590 spin_lock_irqsave(&gpio_lock
, flags
);
591 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
592 test_bit(FLAG_EXPORT
, &desc
->flags
)) {
593 spin_unlock_irqrestore(&gpio_lock
, flags
);
594 gpiod_dbg(desc
, "%s: unavailable (requested=%d, exported=%d)\n",
596 test_bit(FLAG_REQUESTED
, &desc
->flags
),
597 test_bit(FLAG_EXPORT
, &desc
->flags
));
602 if (chip
->direction_input
&& chip
->direction_output
&&
603 direction_may_change
) {
604 set_bit(FLAG_SYSFS_DIR
, &desc
->flags
);
607 spin_unlock_irqrestore(&gpio_lock
, flags
);
609 offset
= gpio_chip_hwgpio(desc
);
610 if (chip
->names
&& chip
->names
[offset
])
611 ioname
= chip
->names
[offset
];
613 dev
= device_create_with_groups(&gpio_class
, chip
->dev
,
614 MKDEV(0, 0), desc
, gpio_groups
,
615 ioname
? ioname
: "gpio%u",
618 status
= PTR_ERR(dev
);
622 set_bit(FLAG_EXPORT
, &desc
->flags
);
623 mutex_unlock(&sysfs_lock
);
627 mutex_unlock(&sysfs_lock
);
628 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
631 EXPORT_SYMBOL_GPL(gpiod_export
);
633 static int match_export(struct device
*dev
, const void *data
)
635 return dev_get_drvdata(dev
) == data
;
639 * gpiod_export_link - create a sysfs link to an exported GPIO node
640 * @dev: device under which to create symlink
641 * @name: name of the symlink
642 * @gpio: gpio to create symlink to, already exported
644 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
645 * node. Caller is responsible for unlinking.
647 * Returns zero on success, else an error.
649 int gpiod_export_link(struct device
*dev
, const char *name
,
650 struct gpio_desc
*desc
)
652 int status
= -EINVAL
;
655 pr_warn("%s: invalid GPIO\n", __func__
);
659 mutex_lock(&sysfs_lock
);
661 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
664 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
666 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
674 mutex_unlock(&sysfs_lock
);
677 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
681 EXPORT_SYMBOL_GPL(gpiod_export_link
);
684 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
685 * @gpio: gpio to change
686 * @value: non-zero to use active low, i.e. inverted values
688 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
689 * The GPIO does not have to be exported yet. If poll(2) support has
690 * been enabled for either rising or falling edge, it will be
691 * reconfigured to follow the new polarity.
693 * Returns zero on success, else an error.
695 int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
)
697 struct device
*dev
= NULL
;
698 int status
= -EINVAL
;
701 pr_warn("%s: invalid GPIO\n", __func__
);
705 mutex_lock(&sysfs_lock
);
707 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
708 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
715 status
= sysfs_set_active_low(desc
, dev
, value
);
718 mutex_unlock(&sysfs_lock
);
721 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
725 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low
);
728 * gpiod_unexport - reverse effect of gpio_export()
729 * @gpio: gpio to make unavailable
731 * This is implicit on gpio_free().
733 void gpiod_unexport(struct gpio_desc
*desc
)
736 struct device
*dev
= NULL
;
739 pr_warn("%s: invalid GPIO\n", __func__
);
743 mutex_lock(&sysfs_lock
);
745 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
747 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
749 gpio_setup_irq(desc
, dev
, 0);
750 clear_bit(FLAG_SYSFS_DIR
, &desc
->flags
);
751 clear_bit(FLAG_EXPORT
, &desc
->flags
);
756 mutex_unlock(&sysfs_lock
);
759 device_unregister(dev
);
764 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
766 EXPORT_SYMBOL_GPL(gpiod_unexport
);
768 int gpiochip_sysfs_register(struct gpio_chip
*chip
)
773 * Many systems add gpio chips for SOC support very early,
774 * before driver model support is available. In those cases we
775 * register later, in gpiolib_sysfs_init() ... here we just
776 * verify that _some_ field of gpio_class got initialized.
781 /* use chip->base for the ID; it's already known to be unique */
782 dev
= device_create_with_groups(&gpio_class
, chip
->dev
, MKDEV(0, 0),
783 chip
, gpiochip_groups
,
784 "gpiochip%d", chip
->base
);
788 mutex_lock(&sysfs_lock
);
790 mutex_unlock(&sysfs_lock
);
795 void gpiochip_sysfs_unregister(struct gpio_chip
*chip
)
797 struct gpio_desc
*desc
;
803 device_unregister(chip
->cdev
);
805 /* prevent further gpiod exports */
806 mutex_lock(&sysfs_lock
);
808 mutex_unlock(&sysfs_lock
);
810 /* unregister gpiod class devices owned by sysfs */
811 for (i
= 0; i
< chip
->ngpio
; i
++) {
812 desc
= &chip
->desc
[i
];
813 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
))
818 static int __init
gpiolib_sysfs_init(void)
822 struct gpio_chip
*chip
;
824 status
= class_register(&gpio_class
);
828 /* Scan and register the gpio_chips which registered very
829 * early (e.g. before the class_register above was called).
831 * We run before arch_initcall() so chip->dev nodes can have
832 * registered, and so arch_initcall() can always gpio_export().
834 spin_lock_irqsave(&gpio_lock
, flags
);
835 list_for_each_entry(chip
, &gpio_chips
, list
) {
840 * TODO we yield gpio_lock here because
841 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
842 * and needs to be fixed.
844 * Also it would be nice to use gpiochip_find() here so we
845 * can keep gpio_chips local to gpiolib.c, but the yield of
846 * gpio_lock prevents us from doing this.
848 spin_unlock_irqrestore(&gpio_lock
, flags
);
849 status
= gpiochip_sysfs_register(chip
);
850 spin_lock_irqsave(&gpio_lock
, flags
);
852 spin_unlock_irqrestore(&gpio_lock
, flags
);
857 postcore_initcall(gpiolib_sysfs_init
);