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>
9 #include <linux/slab.h>
14 struct gpio_desc *desc;
15 struct kernfs_node *value_kn;
19 /* lock protects against unexport_gpio() being called while
20 * sysfs files are active.
22 static DEFINE_MUTEX(sysfs_lock);
25 * /sys/class/gpio/gpioN... only for GPIOs that are exported
27 * * MAY BE OMITTED if kernel won't allow direction changes
28 * * is read/write as "in" or "out"
29 * * may also be written as "high" or "low", initializing
30 * output value as specified ("out" implies "low")
32 * * always readable, subject to hardware behavior
33 * * may be writable, as zero/nonzero
35 * * configures behavior of poll(2) on /value
36 * * available only if pin can generate IRQs on input
37 * * is read/write as "none", "falling", "rising", or "both"
39 * * configures polarity of /value
40 * * is read/write as zero/nonzero
41 * * also affects existing and subsequent "falling" and "rising"
45 static ssize_t direction_show(struct device *dev,
46 struct device_attribute *attr, char *buf)
48 struct gpiod_data *data = dev_get_drvdata(dev);
49 struct gpio_desc *desc = data->desc;
52 mutex_lock(&sysfs_lock);
54 gpiod_get_direction(desc);
55 status = sprintf(buf, "%s\n",
56 test_bit(FLAG_IS_OUT, &desc->flags)
59 mutex_unlock(&sysfs_lock);
63 static ssize_t direction_store(struct device *dev,
64 struct device_attribute *attr, const char *buf, size_t size)
66 struct gpiod_data *data = dev_get_drvdata(dev);
67 struct gpio_desc *desc = data->desc;
70 mutex_lock(&sysfs_lock);
72 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;
84 static DEVICE_ATTR_RW(direction);
86 static ssize_t value_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
89 struct gpiod_data *data = dev_get_drvdata(dev);
90 struct gpio_desc *desc = data->desc;
93 mutex_lock(&sysfs_lock);
95 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
97 mutex_unlock(&sysfs_lock);
101 static ssize_t value_store(struct device *dev,
102 struct device_attribute *attr, const char *buf, size_t size)
104 struct gpiod_data *data = dev_get_drvdata(dev);
105 struct gpio_desc *desc = data->desc;
108 mutex_lock(&sysfs_lock);
110 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
115 status = kstrtol(buf, 0, &value);
117 gpiod_set_value_cansleep(desc, value);
122 mutex_unlock(&sysfs_lock);
125 static DEVICE_ATTR_RW(value);
127 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
129 struct gpiod_data *data = priv;
131 sysfs_notify_dirent(data->value_kn);
136 static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
138 struct gpiod_data *data = dev_get_drvdata(dev);
139 struct gpio_desc *desc = data->desc;
140 unsigned long irq_flags;
143 data->irq = gpiod_to_irq(desc);
147 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
151 irq_flags = IRQF_SHARED;
152 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
153 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
154 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
155 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
156 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
157 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
160 * FIXME: This should be done in the irq_request_resources callback
161 * when the irq is requested, but a few drivers currently fail
164 * Remove this redundant call (along with the corresponding
165 * unlock) when those drivers have been fixed.
167 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
171 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
176 desc->flags |= gpio_flags;
181 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
183 sysfs_put(data->value_kn);
188 static void gpio_sysfs_free_irq(struct device *dev)
190 struct gpiod_data *data = dev_get_drvdata(dev);
191 struct gpio_desc *desc = data->desc;
193 desc->flags &= ~GPIO_TRIGGER_MASK;
194 free_irq(data->irq, data);
195 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
196 sysfs_put(data->value_kn);
199 static const struct {
202 } trigger_types[] = {
204 { "falling", BIT(FLAG_TRIG_FALL) },
205 { "rising", BIT(FLAG_TRIG_RISE) },
206 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
209 static ssize_t edge_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct gpiod_data *data = dev_get_drvdata(dev);
213 struct gpio_desc *desc = data->desc;
218 mutex_lock(&sysfs_lock);
220 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
221 mask = desc->flags & GPIO_TRIGGER_MASK;
222 if (mask == trigger_types[i].flags) {
223 status = sprintf(buf, "%s\n", trigger_types[i].name);
228 mutex_unlock(&sysfs_lock);
232 static ssize_t edge_store(struct device *dev,
233 struct device_attribute *attr, const char *buf, size_t size)
235 struct gpiod_data *data = dev_get_drvdata(dev);
236 struct gpio_desc *desc = data->desc;
238 ssize_t status = size;
241 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
242 if (sysfs_streq(trigger_types[i].name, buf))
246 if (i == ARRAY_SIZE(trigger_types))
249 flags = trigger_types[i].flags;
251 mutex_lock(&sysfs_lock);
253 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
258 if (desc->flags & GPIO_TRIGGER_MASK)
259 gpio_sysfs_free_irq(dev);
262 status = gpio_sysfs_request_irq(dev, flags);
268 mutex_unlock(&sysfs_lock);
272 static DEVICE_ATTR_RW(edge);
274 static int sysfs_set_active_low(struct device *dev, int value)
276 struct gpiod_data *data = dev_get_drvdata(dev);
277 struct gpio_desc *desc = data->desc;
280 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
284 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
286 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
288 /* reconfigure poll(2) support if enabled on one edge only */
289 if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
290 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
291 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
293 gpio_sysfs_free_irq(dev);
294 status = gpio_sysfs_request_irq(dev, trigger_flags);
300 static ssize_t active_low_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct gpiod_data *data = dev_get_drvdata(dev);
304 struct gpio_desc *desc = data->desc;
307 mutex_lock(&sysfs_lock);
309 status = sprintf(buf, "%d\n",
310 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
312 mutex_unlock(&sysfs_lock);
317 static ssize_t active_low_store(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t size)
323 mutex_lock(&sysfs_lock);
325 status = kstrtol(buf, 0, &value);
327 status = sysfs_set_active_low(dev, value != 0);
329 mutex_unlock(&sysfs_lock);
331 return status ? : size;
333 static DEVICE_ATTR_RW(active_low);
335 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
338 struct device *dev = container_of(kobj, struct device, kobj);
339 struct gpiod_data *data = dev_get_drvdata(dev);
340 struct gpio_desc *desc = data->desc;
341 umode_t mode = attr->mode;
342 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
344 if (attr == &dev_attr_direction.attr) {
347 } else if (attr == &dev_attr_edge.attr) {
348 if (gpiod_to_irq(desc) < 0)
350 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
357 static struct attribute *gpio_attrs[] = {
358 &dev_attr_direction.attr,
360 &dev_attr_value.attr,
361 &dev_attr_active_low.attr,
365 static const struct attribute_group gpio_group = {
367 .is_visible = gpio_is_visible,
370 static const struct attribute_group *gpio_groups[] = {
376 * /sys/class/gpio/gpiochipN/
377 * /base ... matching gpio_chip.base (N)
378 * /label ... matching gpio_chip.label
379 * /ngpio ... matching gpio_chip.ngpio
382 static ssize_t base_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 const struct gpio_chip *chip = dev_get_drvdata(dev);
387 return sprintf(buf, "%d\n", chip->base);
389 static DEVICE_ATTR_RO(base);
391 static ssize_t label_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
394 const struct gpio_chip *chip = dev_get_drvdata(dev);
396 return sprintf(buf, "%s\n", chip->label ? : "");
398 static DEVICE_ATTR_RO(label);
400 static ssize_t ngpio_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 const struct gpio_chip *chip = dev_get_drvdata(dev);
405 return sprintf(buf, "%u\n", chip->ngpio);
407 static DEVICE_ATTR_RO(ngpio);
409 static struct attribute *gpiochip_attrs[] = {
411 &dev_attr_label.attr,
412 &dev_attr_ngpio.attr,
415 ATTRIBUTE_GROUPS(gpiochip);
418 * /sys/class/gpio/export ... write-only
419 * integer N ... number of GPIO to export (full access)
420 * /sys/class/gpio/unexport ... write-only
421 * integer N ... number of GPIO to unexport
423 static ssize_t export_store(struct class *class,
424 struct class_attribute *attr,
425 const char *buf, size_t len)
428 struct gpio_desc *desc;
431 status = kstrtol(buf, 0, &gpio);
435 desc = gpio_to_desc(gpio);
436 /* reject invalid GPIOs */
438 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
442 /* No extra locking here; FLAG_SYSFS just signifies that the
443 * request and export were done by on behalf of userspace, so
444 * they may be undone on its behalf too.
447 status = gpiod_request(desc, "sysfs");
449 if (status == -EPROBE_DEFER)
453 status = gpiod_export(desc, true);
457 set_bit(FLAG_SYSFS, &desc->flags);
461 pr_debug("%s: status %d\n", __func__, status);
462 return status ? : len;
465 static ssize_t unexport_store(struct class *class,
466 struct class_attribute *attr,
467 const char *buf, size_t len)
470 struct gpio_desc *desc;
473 status = kstrtol(buf, 0, &gpio);
477 desc = gpio_to_desc(gpio);
478 /* reject bogus commands (gpio_unexport ignores them) */
480 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
486 /* No extra locking here; FLAG_SYSFS just signifies that the
487 * request and export were done by on behalf of userspace, so
488 * they may be undone on its behalf too.
490 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
496 pr_debug("%s: status %d\n", __func__, status);
497 return status ? : len;
500 static struct class_attribute gpio_class_attrs[] = {
501 __ATTR(export, 0200, NULL, export_store),
502 __ATTR(unexport, 0200, NULL, unexport_store),
506 static struct class gpio_class = {
508 .owner = THIS_MODULE,
510 .class_attrs = gpio_class_attrs,
515 * gpiod_export - export a GPIO through sysfs
516 * @gpio: gpio to make available, already requested
517 * @direction_may_change: true if userspace may change gpio direction
518 * Context: arch_initcall or later
520 * When drivers want to make a GPIO accessible to userspace after they
521 * have requested it -- perhaps while debugging, or as part of their
522 * public interface -- they may use this routine. If the GPIO can
523 * change direction (some can't) and the caller allows it, userspace
524 * will see "direction" sysfs attribute which may be used to change
525 * the gpio's direction. A "value" attribute will always be provided.
527 * Returns zero on success, else an error.
529 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
531 struct gpio_chip *chip;
532 struct gpiod_data *data;
535 const char *ioname = NULL;
539 /* can't export until sysfs is available ... */
541 pr_debug("%s: called too early!\n", __func__);
546 pr_debug("%s: invalid gpio descriptor\n", __func__);
552 mutex_lock(&sysfs_lock);
554 /* check if chip is being removed */
555 if (!chip || !chip->cdev) {
560 spin_lock_irqsave(&gpio_lock, flags);
561 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
562 test_bit(FLAG_EXPORT, &desc->flags)) {
563 spin_unlock_irqrestore(&gpio_lock, flags);
564 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
566 test_bit(FLAG_REQUESTED, &desc->flags),
567 test_bit(FLAG_EXPORT, &desc->flags));
572 if (chip->direction_input && chip->direction_output &&
573 direction_may_change) {
574 set_bit(FLAG_SYSFS_DIR, &desc->flags);
577 spin_unlock_irqrestore(&gpio_lock, flags);
579 data = kzalloc(sizeof(*data), GFP_KERNEL);
587 offset = gpio_chip_hwgpio(desc);
588 if (chip->names && chip->names[offset])
589 ioname = chip->names[offset];
591 dev = device_create_with_groups(&gpio_class, chip->dev,
592 MKDEV(0, 0), data, gpio_groups,
593 ioname ? ioname : "gpio%u",
596 status = PTR_ERR(dev);
600 set_bit(FLAG_EXPORT, &desc->flags);
601 mutex_unlock(&sysfs_lock);
607 mutex_unlock(&sysfs_lock);
608 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
611 EXPORT_SYMBOL_GPL(gpiod_export);
613 static int match_export(struct device *dev, const void *desc)
615 struct gpiod_data *data = dev_get_drvdata(dev);
617 return data->desc == desc;
621 * gpiod_export_link - create a sysfs link to an exported GPIO node
622 * @dev: device under which to create symlink
623 * @name: name of the symlink
624 * @gpio: gpio to create symlink to, already exported
626 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
627 * node. Caller is responsible for unlinking.
629 * Returns zero on success, else an error.
631 int gpiod_export_link(struct device *dev, const char *name,
632 struct gpio_desc *desc)
638 pr_warn("%s: invalid GPIO\n", __func__);
642 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
646 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
651 EXPORT_SYMBOL_GPL(gpiod_export_link);
654 * gpiod_unexport - reverse effect of gpio_export()
655 * @gpio: gpio to make unavailable
657 * This is implicit on gpio_free().
659 void gpiod_unexport(struct gpio_desc *desc)
661 struct gpiod_data *data;
663 struct device *dev = NULL;
666 pr_warn("%s: invalid GPIO\n", __func__);
670 mutex_lock(&sysfs_lock);
672 if (test_bit(FLAG_EXPORT, &desc->flags)) {
674 dev = class_find_device(&gpio_class, NULL, desc, match_export);
676 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
677 clear_bit(FLAG_EXPORT, &desc->flags);
682 mutex_unlock(&sysfs_lock);
685 data = dev_get_drvdata(dev);
686 device_unregister(dev);
688 * Release irq after deregistration to prevent race with
691 if (desc->flags & GPIO_TRIGGER_MASK)
692 gpio_sysfs_free_irq(dev);
698 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
700 EXPORT_SYMBOL_GPL(gpiod_unexport);
702 int gpiochip_sysfs_register(struct gpio_chip *chip)
707 * Many systems add gpio chips for SOC support very early,
708 * before driver model support is available. In those cases we
709 * register later, in gpiolib_sysfs_init() ... here we just
710 * verify that _some_ field of gpio_class got initialized.
715 /* use chip->base for the ID; it's already known to be unique */
716 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
717 chip, gpiochip_groups,
718 "gpiochip%d", chip->base);
722 mutex_lock(&sysfs_lock);
724 mutex_unlock(&sysfs_lock);
729 void gpiochip_sysfs_unregister(struct gpio_chip *chip)
731 struct gpio_desc *desc;
737 device_unregister(chip->cdev);
739 /* prevent further gpiod exports */
740 mutex_lock(&sysfs_lock);
742 mutex_unlock(&sysfs_lock);
744 /* unregister gpiod class devices owned by sysfs */
745 for (i = 0; i < chip->ngpio; i++) {
746 desc = &chip->desc[i];
747 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
752 static int __init gpiolib_sysfs_init(void)
756 struct gpio_chip *chip;
758 status = class_register(&gpio_class);
762 /* Scan and register the gpio_chips which registered very
763 * early (e.g. before the class_register above was called).
765 * We run before arch_initcall() so chip->dev nodes can have
766 * registered, and so arch_initcall() can always gpio_export().
768 spin_lock_irqsave(&gpio_lock, flags);
769 list_for_each_entry(chip, &gpio_chips, list) {
774 * TODO we yield gpio_lock here because
775 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
776 * and needs to be fixed.
778 * Also it would be nice to use gpiochip_find() here so we
779 * can keep gpio_chips local to gpiolib.c, but the yield of
780 * gpio_lock prevents us from doing this.
782 spin_unlock_irqrestore(&gpio_lock, flags);
783 status = gpiochip_sysfs_register(chip);
784 spin_lock_irqsave(&gpio_lock, flags);
786 spin_unlock_irqrestore(&gpio_lock, flags);
791 postcore_initcall(gpiolib_sysfs_init);