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;
17 struct kernfs_node *value_kn;
22 * Lock to serialise gpiod export and unexport, and prevent re-export of
23 * gpiod whose chip is being unregistered.
25 static DEFINE_MUTEX(sysfs_lock);
28 * /sys/class/gpio/gpioN... only for GPIOs that are exported
30 * * MAY BE OMITTED if kernel won't allow direction changes
31 * * is read/write as "in" or "out"
32 * * may also be written as "high" or "low", initializing
33 * output value as specified ("out" implies "low")
35 * * always readable, subject to hardware behavior
36 * * may be writable, as zero/nonzero
38 * * configures behavior of poll(2) on /value
39 * * available only if pin can generate IRQs on input
40 * * is read/write as "none", "falling", "rising", or "both"
42 * * configures polarity of /value
43 * * is read/write as zero/nonzero
44 * * also affects existing and subsequent "falling" and "rising"
48 static ssize_t direction_show(struct device *dev,
49 struct device_attribute *attr, char *buf)
51 struct gpiod_data *data = dev_get_drvdata(dev);
52 struct gpio_desc *desc = data->desc;
55 mutex_lock(&data->mutex);
57 gpiod_get_direction(desc);
58 status = sprintf(buf, "%s\n",
59 test_bit(FLAG_IS_OUT, &desc->flags)
62 mutex_unlock(&data->mutex);
67 static ssize_t direction_store(struct device *dev,
68 struct device_attribute *attr, const char *buf, size_t size)
70 struct gpiod_data *data = dev_get_drvdata(dev);
71 struct gpio_desc *desc = data->desc;
74 mutex_lock(&data->mutex);
76 if (sysfs_streq(buf, "high"))
77 status = gpiod_direction_output_raw(desc, 1);
78 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
79 status = gpiod_direction_output_raw(desc, 0);
80 else if (sysfs_streq(buf, "in"))
81 status = gpiod_direction_input(desc);
85 mutex_unlock(&data->mutex);
87 return status ? : size;
89 static DEVICE_ATTR_RW(direction);
91 static ssize_t value_show(struct device *dev,
92 struct device_attribute *attr, char *buf)
94 struct gpiod_data *data = dev_get_drvdata(dev);
95 struct gpio_desc *desc = data->desc;
98 mutex_lock(&data->mutex);
100 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
102 mutex_unlock(&data->mutex);
107 static ssize_t value_store(struct device *dev,
108 struct device_attribute *attr, const char *buf, size_t size)
110 struct gpiod_data *data = dev_get_drvdata(dev);
111 struct gpio_desc *desc = data->desc;
114 mutex_lock(&data->mutex);
116 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
121 status = kstrtol(buf, 0, &value);
123 gpiod_set_value_cansleep(desc, value);
128 mutex_unlock(&data->mutex);
132 static DEVICE_ATTR_RW(value);
134 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
136 struct gpiod_data *data = priv;
138 sysfs_notify_dirent(data->value_kn);
143 /* Caller holds gpiod-data mutex. */
144 static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
146 struct gpiod_data *data = dev_get_drvdata(dev);
147 struct gpio_desc *desc = data->desc;
148 unsigned long irq_flags;
151 data->irq = gpiod_to_irq(desc);
155 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
159 irq_flags = IRQF_SHARED;
160 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
161 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
162 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
163 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
164 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
165 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
168 * FIXME: This should be done in the irq_request_resources callback
169 * when the irq is requested, but a few drivers currently fail
172 * Remove this redundant call (along with the corresponding
173 * unlock) when those drivers have been fixed.
175 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
179 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
184 desc->flags |= gpio_flags;
189 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
191 sysfs_put(data->value_kn);
197 * Caller holds gpiod-data mutex (unless called after class-device
200 static void gpio_sysfs_free_irq(struct device *dev)
202 struct gpiod_data *data = dev_get_drvdata(dev);
203 struct gpio_desc *desc = data->desc;
205 desc->flags &= ~GPIO_TRIGGER_MASK;
206 free_irq(data->irq, data);
207 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
208 sysfs_put(data->value_kn);
211 static const struct {
214 } trigger_types[] = {
216 { "falling", BIT(FLAG_TRIG_FALL) },
217 { "rising", BIT(FLAG_TRIG_RISE) },
218 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
221 static ssize_t edge_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 struct gpiod_data *data = dev_get_drvdata(dev);
225 struct gpio_desc *desc = data->desc;
230 mutex_lock(&data->mutex);
232 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
233 mask = desc->flags & GPIO_TRIGGER_MASK;
234 if (mask == trigger_types[i].flags) {
235 status = sprintf(buf, "%s\n", trigger_types[i].name);
240 mutex_unlock(&data->mutex);
245 static ssize_t edge_store(struct device *dev,
246 struct device_attribute *attr, const char *buf, size_t size)
248 struct gpiod_data *data = dev_get_drvdata(dev);
249 struct gpio_desc *desc = data->desc;
251 ssize_t status = size;
254 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
255 if (sysfs_streq(trigger_types[i].name, buf))
259 if (i == ARRAY_SIZE(trigger_types))
262 flags = trigger_types[i].flags;
264 mutex_lock(&data->mutex);
266 if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
271 if (desc->flags & GPIO_TRIGGER_MASK)
272 gpio_sysfs_free_irq(dev);
275 status = gpio_sysfs_request_irq(dev, flags);
281 mutex_unlock(&data->mutex);
285 static DEVICE_ATTR_RW(edge);
287 /* Caller holds gpiod-data mutex. */
288 static int sysfs_set_active_low(struct device *dev, int value)
290 struct gpiod_data *data = dev_get_drvdata(dev);
291 struct gpio_desc *desc = data->desc;
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 (!!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_sysfs_free_irq(dev);
308 status = gpio_sysfs_request_irq(dev, trigger_flags);
314 static ssize_t active_low_show(struct device *dev,
315 struct device_attribute *attr, char *buf)
317 struct gpiod_data *data = dev_get_drvdata(dev);
318 struct gpio_desc *desc = data->desc;
321 mutex_lock(&data->mutex);
323 status = sprintf(buf, "%d\n",
324 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
326 mutex_unlock(&data->mutex);
331 static ssize_t active_low_store(struct device *dev,
332 struct device_attribute *attr, const char *buf, size_t size)
334 struct gpiod_data *data = dev_get_drvdata(dev);
338 mutex_lock(&data->mutex);
340 status = kstrtol(buf, 0, &value);
342 status = sysfs_set_active_low(dev, value != 0);
344 mutex_unlock(&data->mutex);
346 return status ? : size;
348 static DEVICE_ATTR_RW(active_low);
350 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
353 struct device *dev = container_of(kobj, struct device, kobj);
354 struct gpiod_data *data = dev_get_drvdata(dev);
355 struct gpio_desc *desc = data->desc;
356 umode_t mode = attr->mode;
357 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
359 if (attr == &dev_attr_direction.attr) {
362 } else if (attr == &dev_attr_edge.attr) {
363 if (gpiod_to_irq(desc) < 0)
365 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
372 static struct attribute *gpio_attrs[] = {
373 &dev_attr_direction.attr,
375 &dev_attr_value.attr,
376 &dev_attr_active_low.attr,
380 static const struct attribute_group gpio_group = {
382 .is_visible = gpio_is_visible,
385 static const struct attribute_group *gpio_groups[] = {
391 * /sys/class/gpio/gpiochipN/
392 * /base ... matching gpio_chip.base (N)
393 * /label ... matching gpio_chip.label
394 * /ngpio ... matching gpio_chip.ngpio
397 static ssize_t base_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 const struct gpio_chip *chip = dev_get_drvdata(dev);
402 return sprintf(buf, "%d\n", chip->base);
404 static DEVICE_ATTR_RO(base);
406 static ssize_t label_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
409 const struct gpio_chip *chip = dev_get_drvdata(dev);
411 return sprintf(buf, "%s\n", chip->label ? : "");
413 static DEVICE_ATTR_RO(label);
415 static ssize_t ngpio_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
418 const struct gpio_chip *chip = dev_get_drvdata(dev);
420 return sprintf(buf, "%u\n", chip->ngpio);
422 static DEVICE_ATTR_RO(ngpio);
424 static struct attribute *gpiochip_attrs[] = {
426 &dev_attr_label.attr,
427 &dev_attr_ngpio.attr,
430 ATTRIBUTE_GROUPS(gpiochip);
433 * /sys/class/gpio/export ... write-only
434 * integer N ... number of GPIO to export (full access)
435 * /sys/class/gpio/unexport ... write-only
436 * integer N ... number of GPIO to unexport
438 static ssize_t export_store(struct class *class,
439 struct class_attribute *attr,
440 const char *buf, size_t len)
443 struct gpio_desc *desc;
446 status = kstrtol(buf, 0, &gpio);
450 desc = gpio_to_desc(gpio);
451 /* reject invalid GPIOs */
453 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
457 /* No extra locking here; FLAG_SYSFS just signifies that the
458 * request and export were done by on behalf of userspace, so
459 * they may be undone on its behalf too.
462 status = gpiod_request(desc, "sysfs");
464 if (status == -EPROBE_DEFER)
468 status = gpiod_export(desc, true);
472 set_bit(FLAG_SYSFS, &desc->flags);
476 pr_debug("%s: status %d\n", __func__, status);
477 return status ? : len;
480 static ssize_t unexport_store(struct class *class,
481 struct class_attribute *attr,
482 const char *buf, size_t len)
485 struct gpio_desc *desc;
488 status = kstrtol(buf, 0, &gpio);
492 desc = gpio_to_desc(gpio);
493 /* reject bogus commands (gpio_unexport ignores them) */
495 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
501 /* No extra locking here; FLAG_SYSFS just signifies that the
502 * request and export were done by on behalf of userspace, so
503 * they may be undone on its behalf too.
505 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
511 pr_debug("%s: status %d\n", __func__, status);
512 return status ? : len;
515 static struct class_attribute gpio_class_attrs[] = {
516 __ATTR(export, 0200, NULL, export_store),
517 __ATTR(unexport, 0200, NULL, unexport_store),
521 static struct class gpio_class = {
523 .owner = THIS_MODULE,
525 .class_attrs = gpio_class_attrs,
530 * gpiod_export - export a GPIO through sysfs
531 * @gpio: gpio to make available, already requested
532 * @direction_may_change: true if userspace may change gpio direction
533 * Context: arch_initcall or later
535 * When drivers want to make a GPIO accessible to userspace after they
536 * have requested it -- perhaps while debugging, or as part of their
537 * public interface -- they may use this routine. If the GPIO can
538 * change direction (some can't) and the caller allows it, userspace
539 * will see "direction" sysfs attribute which may be used to change
540 * the gpio's direction. A "value" attribute will always be provided.
542 * Returns zero on success, else an error.
544 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
546 struct gpio_chip *chip;
547 struct gpiod_data *data;
550 const char *ioname = NULL;
554 /* can't export until sysfs is available ... */
556 pr_debug("%s: called too early!\n", __func__);
561 pr_debug("%s: invalid gpio descriptor\n", __func__);
567 mutex_lock(&sysfs_lock);
569 /* check if chip is being removed */
570 if (!chip || !chip->cdev) {
575 spin_lock_irqsave(&gpio_lock, flags);
576 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
577 test_bit(FLAG_EXPORT, &desc->flags)) {
578 spin_unlock_irqrestore(&gpio_lock, flags);
579 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
581 test_bit(FLAG_REQUESTED, &desc->flags),
582 test_bit(FLAG_EXPORT, &desc->flags));
587 if (chip->direction_input && chip->direction_output &&
588 direction_may_change) {
589 set_bit(FLAG_SYSFS_DIR, &desc->flags);
592 spin_unlock_irqrestore(&gpio_lock, flags);
594 data = kzalloc(sizeof(*data), GFP_KERNEL);
601 mutex_init(&data->mutex);
603 offset = gpio_chip_hwgpio(desc);
604 if (chip->names && chip->names[offset])
605 ioname = chip->names[offset];
607 dev = device_create_with_groups(&gpio_class, chip->dev,
608 MKDEV(0, 0), data, gpio_groups,
609 ioname ? ioname : "gpio%u",
612 status = PTR_ERR(dev);
616 set_bit(FLAG_EXPORT, &desc->flags);
617 mutex_unlock(&sysfs_lock);
623 mutex_unlock(&sysfs_lock);
624 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
627 EXPORT_SYMBOL_GPL(gpiod_export);
629 static int match_export(struct device *dev, const void *desc)
631 struct gpiod_data *data = dev_get_drvdata(dev);
633 return data->desc == desc;
637 * gpiod_export_link - create a sysfs link to an exported GPIO node
638 * @dev: device under which to create symlink
639 * @name: name of the symlink
640 * @gpio: gpio to create symlink to, already exported
642 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
643 * node. Caller is responsible for unlinking.
645 * Returns zero on success, else an error.
647 int gpiod_export_link(struct device *dev, const char *name,
648 struct gpio_desc *desc)
654 pr_warn("%s: invalid GPIO\n", __func__);
658 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
662 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
667 EXPORT_SYMBOL_GPL(gpiod_export_link);
670 * gpiod_unexport - reverse effect of gpio_export()
671 * @gpio: gpio to make unavailable
673 * This is implicit on gpio_free().
675 void gpiod_unexport(struct gpio_desc *desc)
677 struct gpiod_data *data;
679 struct device *dev = NULL;
682 pr_warn("%s: invalid GPIO\n", __func__);
686 mutex_lock(&sysfs_lock);
688 if (test_bit(FLAG_EXPORT, &desc->flags)) {
690 dev = class_find_device(&gpio_class, NULL, desc, match_export);
692 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
693 clear_bit(FLAG_EXPORT, &desc->flags);
698 mutex_unlock(&sysfs_lock);
701 data = dev_get_drvdata(dev);
702 device_unregister(dev);
704 * Release irq after deregistration to prevent race with
707 if (desc->flags & GPIO_TRIGGER_MASK)
708 gpio_sysfs_free_irq(dev);
714 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
716 EXPORT_SYMBOL_GPL(gpiod_unexport);
718 int gpiochip_sysfs_register(struct gpio_chip *chip)
723 * Many systems add gpio chips for SOC support very early,
724 * before driver model support is available. In those cases we
725 * register later, in gpiolib_sysfs_init() ... here we just
726 * verify that _some_ field of gpio_class got initialized.
731 /* use chip->base for the ID; it's already known to be unique */
732 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
733 chip, gpiochip_groups,
734 "gpiochip%d", chip->base);
738 mutex_lock(&sysfs_lock);
740 mutex_unlock(&sysfs_lock);
745 void gpiochip_sysfs_unregister(struct gpio_chip *chip)
747 struct gpio_desc *desc;
753 device_unregister(chip->cdev);
755 /* prevent further gpiod exports */
756 mutex_lock(&sysfs_lock);
758 mutex_unlock(&sysfs_lock);
760 /* unregister gpiod class devices owned by sysfs */
761 for (i = 0; i < chip->ngpio; i++) {
762 desc = &chip->desc[i];
763 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
768 static int __init gpiolib_sysfs_init(void)
772 struct gpio_chip *chip;
774 status = class_register(&gpio_class);
778 /* Scan and register the gpio_chips which registered very
779 * early (e.g. before the class_register above was called).
781 * We run before arch_initcall() so chip->dev nodes can have
782 * registered, and so arch_initcall() can always gpio_export().
784 spin_lock_irqsave(&gpio_lock, flags);
785 list_for_each_entry(chip, &gpio_chips, list) {
790 * TODO we yield gpio_lock here because
791 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
792 * and needs to be fixed.
794 * Also it would be nice to use gpiochip_find() here so we
795 * can keep gpio_chips local to gpiolib.c, but the yield of
796 * gpio_lock prevents us from doing this.
798 spin_unlock_irqrestore(&gpio_lock, flags);
799 status = gpiochip_sysfs_register(chip);
800 spin_lock_irqsave(&gpio_lock, flags);
802 spin_unlock_irqrestore(&gpio_lock, flags);
807 postcore_initcall(gpiolib_sysfs_init);