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
) {
198 ret
= request_any_context_irq(irq
, gpio_sysfs_irq
, irq_flags
,
199 "gpiolib", value_sd
);
203 ret
= gpiochip_lock_as_irq(desc
->chip
, gpio_chip_hwgpio(desc
));
205 gpiod_warn(desc
, "failed to flag the GPIO for IRQ\n");
209 desc
->flags
|= gpio_flags
;
213 idr_remove(&dirent_idr
, id
);
214 desc
->flags
&= GPIO_FLAGS_MASK
;
222 static const struct {
225 } trigger_types
[] = {
227 { "falling", BIT(FLAG_TRIG_FALL
) },
228 { "rising", BIT(FLAG_TRIG_RISE
) },
229 { "both", BIT(FLAG_TRIG_FALL
) | BIT(FLAG_TRIG_RISE
) },
232 static ssize_t
gpio_edge_show(struct device
*dev
,
233 struct device_attribute
*attr
, char *buf
)
235 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
238 mutex_lock(&sysfs_lock
);
240 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
246 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
247 if ((desc
->flags
& GPIO_TRIGGER_MASK
)
248 == trigger_types
[i
].flags
) {
249 status
= sprintf(buf
, "%s\n",
250 trigger_types
[i
].name
);
255 mutex_unlock(&sysfs_lock
);
259 static ssize_t
gpio_edge_store(struct device
*dev
,
260 struct device_attribute
*attr
, const char *buf
, size_t size
)
262 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
266 for (i
= 0; i
< ARRAY_SIZE(trigger_types
); i
++)
267 if (sysfs_streq(trigger_types
[i
].name
, buf
))
272 mutex_lock(&sysfs_lock
);
274 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
277 status
= gpio_setup_irq(desc
, dev
, trigger_types
[i
].flags
);
282 mutex_unlock(&sysfs_lock
);
287 static DEVICE_ATTR(edge
, 0644, gpio_edge_show
, gpio_edge_store
);
289 static int sysfs_set_active_low(struct gpio_desc
*desc
, struct device
*dev
,
294 if (!!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
) == !!value
)
298 set_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
300 clear_bit(FLAG_ACTIVE_LOW
, &desc
->flags
);
302 /* reconfigure poll(2) support if enabled on one edge only */
303 if (dev
!= NULL
&& (!!test_bit(FLAG_TRIG_RISE
, &desc
->flags
) ^
304 !!test_bit(FLAG_TRIG_FALL
, &desc
->flags
))) {
305 unsigned long trigger_flags
= desc
->flags
& GPIO_TRIGGER_MASK
;
307 gpio_setup_irq(desc
, dev
, 0);
308 status
= gpio_setup_irq(desc
, dev
, trigger_flags
);
314 static ssize_t
gpio_active_low_show(struct device
*dev
,
315 struct device_attribute
*attr
, char *buf
)
317 const struct gpio_desc
*desc
= dev_get_drvdata(dev
);
320 mutex_lock(&sysfs_lock
);
322 if (!test_bit(FLAG_EXPORT
, &desc
->flags
))
325 status
= sprintf(buf
, "%d\n",
326 !!test_bit(FLAG_ACTIVE_LOW
, &desc
->flags
));
328 mutex_unlock(&sysfs_lock
);
333 static ssize_t
gpio_active_low_store(struct device
*dev
,
334 struct device_attribute
*attr
, const char *buf
, size_t size
)
336 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
339 mutex_lock(&sysfs_lock
);
341 if (!test_bit(FLAG_EXPORT
, &desc
->flags
)) {
346 status
= kstrtol(buf
, 0, &value
);
348 status
= sysfs_set_active_low(desc
, dev
, value
!= 0);
351 mutex_unlock(&sysfs_lock
);
353 return status
? : size
;
356 static DEVICE_ATTR(active_low
, 0644,
357 gpio_active_low_show
, gpio_active_low_store
);
359 static umode_t
gpio_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
362 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
363 struct gpio_desc
*desc
= dev_get_drvdata(dev
);
364 umode_t mode
= attr
->mode
;
365 bool show_direction
= test_bit(FLAG_SYSFS_DIR
, &desc
->flags
);
367 if (attr
== &dev_attr_direction
.attr
) {
370 } else if (attr
== &dev_attr_edge
.attr
) {
371 if (gpiod_to_irq(desc
) < 0)
373 if (!show_direction
&& test_bit(FLAG_IS_OUT
, &desc
->flags
))
380 static struct attribute
*gpio_attrs
[] = {
381 &dev_attr_direction
.attr
,
383 &dev_attr_value
.attr
,
384 &dev_attr_active_low
.attr
,
388 static const struct attribute_group gpio_group
= {
390 .is_visible
= gpio_is_visible
,
393 static const struct attribute_group
*gpio_groups
[] = {
399 * /sys/class/gpio/gpiochipN/
400 * /base ... matching gpio_chip.base (N)
401 * /label ... matching gpio_chip.label
402 * /ngpio ... matching gpio_chip.ngpio
405 static ssize_t
chip_base_show(struct device
*dev
,
406 struct device_attribute
*attr
, char *buf
)
408 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
410 return sprintf(buf
, "%d\n", chip
->base
);
412 static DEVICE_ATTR(base
, 0444, chip_base_show
, NULL
);
414 static ssize_t
chip_label_show(struct device
*dev
,
415 struct device_attribute
*attr
, char *buf
)
417 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
419 return sprintf(buf
, "%s\n", chip
->label
? : "");
421 static DEVICE_ATTR(label
, 0444, chip_label_show
, NULL
);
423 static ssize_t
chip_ngpio_show(struct device
*dev
,
424 struct device_attribute
*attr
, char *buf
)
426 const struct gpio_chip
*chip
= dev_get_drvdata(dev
);
428 return sprintf(buf
, "%u\n", chip
->ngpio
);
430 static DEVICE_ATTR(ngpio
, 0444, chip_ngpio_show
, NULL
);
432 static struct attribute
*gpiochip_attrs
[] = {
434 &dev_attr_label
.attr
,
435 &dev_attr_ngpio
.attr
,
438 ATTRIBUTE_GROUPS(gpiochip
);
441 * /sys/class/gpio/export ... write-only
442 * integer N ... number of GPIO to export (full access)
443 * /sys/class/gpio/unexport ... write-only
444 * integer N ... number of GPIO to unexport
446 static ssize_t
export_store(struct class *class,
447 struct class_attribute
*attr
,
448 const char *buf
, size_t len
)
451 struct gpio_desc
*desc
;
454 status
= kstrtol(buf
, 0, &gpio
);
458 desc
= gpio_to_desc(gpio
);
459 /* reject invalid GPIOs */
461 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
465 /* No extra locking here; FLAG_SYSFS just signifies that the
466 * request and export were done by on behalf of userspace, so
467 * they may be undone on its behalf too.
470 status
= gpiod_request(desc
, "sysfs");
472 if (status
== -EPROBE_DEFER
)
476 status
= gpiod_export(desc
, true);
480 set_bit(FLAG_SYSFS
, &desc
->flags
);
484 pr_debug("%s: status %d\n", __func__
, status
);
485 return status
? : len
;
488 static ssize_t
unexport_store(struct class *class,
489 struct class_attribute
*attr
,
490 const char *buf
, size_t len
)
493 struct gpio_desc
*desc
;
496 status
= kstrtol(buf
, 0, &gpio
);
500 desc
= gpio_to_desc(gpio
);
501 /* reject bogus commands (gpio_unexport ignores them) */
503 pr_warn("%s: invalid GPIO %ld\n", __func__
, gpio
);
509 /* No extra locking here; FLAG_SYSFS just signifies that the
510 * request and export were done by on behalf of userspace, so
511 * they may be undone on its behalf too.
513 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
)) {
519 pr_debug("%s: status %d\n", __func__
, status
);
520 return status
? : len
;
523 static struct class_attribute gpio_class_attrs
[] = {
524 __ATTR(export
, 0200, NULL
, export_store
),
525 __ATTR(unexport
, 0200, NULL
, unexport_store
),
529 static struct class gpio_class
= {
531 .owner
= THIS_MODULE
,
533 .class_attrs
= gpio_class_attrs
,
538 * gpiod_export - export a GPIO through sysfs
539 * @gpio: gpio to make available, already requested
540 * @direction_may_change: true if userspace may change gpio direction
541 * Context: arch_initcall or later
543 * When drivers want to make a GPIO accessible to userspace after they
544 * have requested it -- perhaps while debugging, or as part of their
545 * public interface -- they may use this routine. If the GPIO can
546 * change direction (some can't) and the caller allows it, userspace
547 * will see "direction" sysfs attribute which may be used to change
548 * the gpio's direction. A "value" attribute will always be provided.
550 * Returns zero on success, else an error.
552 int gpiod_export(struct gpio_desc
*desc
, bool direction_may_change
)
554 struct gpio_chip
*chip
;
557 const char *ioname
= NULL
;
561 /* can't export until sysfs is available ... */
563 pr_debug("%s: called too early!\n", __func__
);
568 pr_debug("%s: invalid gpio descriptor\n", __func__
);
574 mutex_lock(&sysfs_lock
);
576 /* check if chip is being removed */
577 if (!chip
|| !chip
->exported
) {
582 spin_lock_irqsave(&gpio_lock
, flags
);
583 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
584 test_bit(FLAG_EXPORT
, &desc
->flags
)) {
585 spin_unlock_irqrestore(&gpio_lock
, flags
);
586 gpiod_dbg(desc
, "%s: unavailable (requested=%d, exported=%d)\n",
588 test_bit(FLAG_REQUESTED
, &desc
->flags
),
589 test_bit(FLAG_EXPORT
, &desc
->flags
));
594 if (desc
->chip
->direction_input
&& desc
->chip
->direction_output
&&
595 direction_may_change
) {
596 set_bit(FLAG_SYSFS_DIR
, &desc
->flags
);
599 spin_unlock_irqrestore(&gpio_lock
, flags
);
601 offset
= gpio_chip_hwgpio(desc
);
602 if (desc
->chip
->names
&& desc
->chip
->names
[offset
])
603 ioname
= desc
->chip
->names
[offset
];
605 dev
= device_create_with_groups(&gpio_class
, desc
->chip
->dev
,
606 MKDEV(0, 0), desc
, gpio_groups
,
607 ioname
? ioname
: "gpio%u",
610 status
= PTR_ERR(dev
);
614 set_bit(FLAG_EXPORT
, &desc
->flags
);
615 mutex_unlock(&sysfs_lock
);
619 mutex_unlock(&sysfs_lock
);
620 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
623 EXPORT_SYMBOL_GPL(gpiod_export
);
625 static int match_export(struct device
*dev
, const void *data
)
627 return dev_get_drvdata(dev
) == data
;
631 * gpiod_export_link - create a sysfs link to an exported GPIO node
632 * @dev: device under which to create symlink
633 * @name: name of the symlink
634 * @gpio: gpio to create symlink to, already exported
636 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
637 * node. Caller is responsible for unlinking.
639 * Returns zero on success, else an error.
641 int gpiod_export_link(struct device
*dev
, const char *name
,
642 struct gpio_desc
*desc
)
644 int status
= -EINVAL
;
647 pr_warn("%s: invalid GPIO\n", __func__
);
651 mutex_lock(&sysfs_lock
);
653 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
656 tdev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
658 status
= sysfs_create_link(&dev
->kobj
, &tdev
->kobj
,
666 mutex_unlock(&sysfs_lock
);
669 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
673 EXPORT_SYMBOL_GPL(gpiod_export_link
);
676 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
677 * @gpio: gpio to change
678 * @value: non-zero to use active low, i.e. inverted values
680 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
681 * The GPIO does not have to be exported yet. If poll(2) support has
682 * been enabled for either rising or falling edge, it will be
683 * reconfigured to follow the new polarity.
685 * Returns zero on success, else an error.
687 int gpiod_sysfs_set_active_low(struct gpio_desc
*desc
, int value
)
689 struct device
*dev
= NULL
;
690 int status
= -EINVAL
;
693 pr_warn("%s: invalid GPIO\n", __func__
);
697 mutex_lock(&sysfs_lock
);
699 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
700 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
707 status
= sysfs_set_active_low(desc
, dev
, value
);
710 mutex_unlock(&sysfs_lock
);
713 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
717 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low
);
720 * gpiod_unexport - reverse effect of gpio_export()
721 * @gpio: gpio to make unavailable
723 * This is implicit on gpio_free().
725 void gpiod_unexport(struct gpio_desc
*desc
)
728 struct device
*dev
= NULL
;
731 pr_warn("%s: invalid GPIO\n", __func__
);
735 mutex_lock(&sysfs_lock
);
737 if (test_bit(FLAG_EXPORT
, &desc
->flags
)) {
739 dev
= class_find_device(&gpio_class
, NULL
, desc
, match_export
);
741 gpio_setup_irq(desc
, dev
, 0);
742 clear_bit(FLAG_SYSFS_DIR
, &desc
->flags
);
743 clear_bit(FLAG_EXPORT
, &desc
->flags
);
748 mutex_unlock(&sysfs_lock
);
751 device_unregister(dev
);
756 gpiod_dbg(desc
, "%s: status %d\n", __func__
, status
);
758 EXPORT_SYMBOL_GPL(gpiod_unexport
);
760 int gpiochip_export(struct gpio_chip
*chip
)
765 /* Many systems register gpio chips for SOC support very early,
766 * before driver model support is available. In those cases we
767 * export this later, in gpiolib_sysfs_init() ... here we just
768 * verify that _some_ field of gpio_class got initialized.
773 /* use chip->base for the ID; it's already known to be unique */
774 mutex_lock(&sysfs_lock
);
775 dev
= device_create_with_groups(&gpio_class
, chip
->dev
, MKDEV(0, 0),
776 chip
, gpiochip_groups
,
777 "gpiochip%d", chip
->base
);
779 status
= PTR_ERR(dev
);
782 chip
->exported
= (status
== 0);
783 mutex_unlock(&sysfs_lock
);
786 chip_dbg(chip
, "%s: status %d\n", __func__
, status
);
791 void gpiochip_unexport(struct gpio_chip
*chip
)
795 struct gpio_desc
*desc
;
798 mutex_lock(&sysfs_lock
);
799 dev
= class_find_device(&gpio_class
, NULL
, chip
, match_export
);
802 device_unregister(dev
);
803 /* prevent further gpiod exports */
804 chip
->exported
= false;
808 mutex_unlock(&sysfs_lock
);
811 chip_dbg(chip
, "%s: status %d\n", __func__
, status
);
813 /* unregister gpiod class devices owned by sysfs */
814 for (i
= 0; i
< chip
->ngpio
; i
++) {
815 desc
= &chip
->desc
[i
];
816 if (test_and_clear_bit(FLAG_SYSFS
, &desc
->flags
))
821 static int __init
gpiolib_sysfs_init(void)
825 struct gpio_chip
*chip
;
827 status
= class_register(&gpio_class
);
831 /* Scan and register the gpio_chips which registered very
832 * early (e.g. before the class_register above was called).
834 * We run before arch_initcall() so chip->dev nodes can have
835 * registered, and so arch_initcall() can always gpio_export().
837 spin_lock_irqsave(&gpio_lock
, flags
);
838 list_for_each_entry(chip
, &gpio_chips
, list
) {
843 * TODO we yield gpio_lock here because gpiochip_export()
844 * acquires a mutex. This is unsafe and needs to be fixed.
846 * Also it would be nice to use gpiochip_find() here so we
847 * can keep gpio_chips local to gpiolib.c, but the yield of
848 * gpio_lock prevents us from doing this.
850 spin_unlock_irqrestore(&gpio_lock
, flags
);
851 status
= gpiochip_export(chip
);
852 spin_lock_irqsave(&gpio_lock
, flags
);
854 spin_unlock_irqrestore(&gpio_lock
, flags
);
859 postcore_initcall(gpiolib_sysfs_init
);