2 #include <linux/mutex.h>
3 #include <linux/device.h>
4 #include <linux/sysfs.h>
5 #include <linux/gpio.h>
6 #include <linux/gpio/consumer.h>
7 #include <linux/gpio/driver.h>
8 #include <linux/interrupt.h>
9 #include <linux/kdev_t.h>
10 #include <linux/slab.h>
14 #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
15 #define GPIO_IRQF_TRIGGER_RISING BIT(1)
16 #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
17 GPIO_IRQF_TRIGGER_RISING)
20 struct gpio_desc *desc;
23 struct kernfs_node *value_kn;
25 unsigned char irq_flags;
27 bool direction_can_change;
31 * Lock to serialise gpiod export and unexport, and prevent re-export of
32 * gpiod whose chip is being unregistered.
34 static DEFINE_MUTEX(sysfs_lock);
37 * /sys/class/gpio/gpioN... only for GPIOs that are exported
39 * * MAY BE OMITTED if kernel won't allow direction changes
40 * * is read/write as "in" or "out"
41 * * may also be written as "high" or "low", initializing
42 * output value as specified ("out" implies "low")
44 * * always readable, subject to hardware behavior
45 * * may be writable, as zero/nonzero
47 * * configures behavior of poll(2) on /value
48 * * available only if pin can generate IRQs on input
49 * * is read/write as "none", "falling", "rising", or "both"
51 * * configures polarity of /value
52 * * is read/write as zero/nonzero
53 * * also affects existing and subsequent "falling" and "rising"
57 static ssize_t direction_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
60 struct gpiod_data *data = dev_get_drvdata(dev);
61 struct gpio_desc *desc = data->desc;
64 mutex_lock(&data->mutex);
66 gpiod_get_direction(desc);
67 status = sprintf(buf, "%s\n",
68 test_bit(FLAG_IS_OUT, &desc->flags)
71 mutex_unlock(&data->mutex);
76 static ssize_t direction_store(struct device *dev,
77 struct device_attribute *attr, const char *buf, size_t size)
79 struct gpiod_data *data = dev_get_drvdata(dev);
80 struct gpio_desc *desc = data->desc;
83 mutex_lock(&data->mutex);
85 if (sysfs_streq(buf, "high"))
86 status = gpiod_direction_output_raw(desc, 1);
87 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
88 status = gpiod_direction_output_raw(desc, 0);
89 else if (sysfs_streq(buf, "in"))
90 status = gpiod_direction_input(desc);
94 mutex_unlock(&data->mutex);
96 return status ? : size;
98 static DEVICE_ATTR_RW(direction);
100 static ssize_t value_show(struct device *dev,
101 struct device_attribute *attr, char *buf)
103 struct gpiod_data *data = dev_get_drvdata(dev);
104 struct gpio_desc *desc = data->desc;
107 mutex_lock(&data->mutex);
109 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
111 mutex_unlock(&data->mutex);
116 static ssize_t value_store(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t size)
119 struct gpiod_data *data = dev_get_drvdata(dev);
120 struct gpio_desc *desc = data->desc;
123 mutex_lock(&data->mutex);
125 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
130 status = kstrtol(buf, 0, &value);
132 gpiod_set_value_cansleep(desc, value);
137 mutex_unlock(&data->mutex);
141 static DEVICE_ATTR_RW(value);
143 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
145 struct gpiod_data *data = priv;
147 sysfs_notify_dirent(data->value_kn);
152 /* Caller holds gpiod-data mutex. */
153 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
155 struct gpiod_data *data = dev_get_drvdata(dev);
156 struct gpio_desc *desc = data->desc;
157 unsigned long irq_flags;
160 data->irq = gpiod_to_irq(desc);
164 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
168 irq_flags = IRQF_SHARED;
169 if (flags & GPIO_IRQF_TRIGGER_FALLING)
170 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
171 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
172 if (flags & GPIO_IRQF_TRIGGER_RISING)
173 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
174 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
177 * FIXME: This should be done in the irq_request_resources callback
178 * when the irq is requested, but a few drivers currently fail
181 * Remove this redundant call (along with the corresponding
182 * unlock) when those drivers have been fixed.
184 ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
188 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
193 data->irq_flags = flags;
198 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
200 sysfs_put(data->value_kn);
206 * Caller holds gpiod-data mutex (unless called after class-device
209 static void gpio_sysfs_free_irq(struct device *dev)
211 struct gpiod_data *data = dev_get_drvdata(dev);
212 struct gpio_desc *desc = data->desc;
215 free_irq(data->irq, data);
216 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
217 sysfs_put(data->value_kn);
220 static const struct {
223 } trigger_types[] = {
225 { "falling", GPIO_IRQF_TRIGGER_FALLING },
226 { "rising", GPIO_IRQF_TRIGGER_RISING },
227 { "both", GPIO_IRQF_TRIGGER_BOTH },
230 static ssize_t edge_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct gpiod_data *data = dev_get_drvdata(dev);
237 mutex_lock(&data->mutex);
239 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
240 if (data->irq_flags == trigger_types[i].flags) {
241 status = sprintf(buf, "%s\n", trigger_types[i].name);
246 mutex_unlock(&data->mutex);
251 static ssize_t edge_store(struct device *dev,
252 struct device_attribute *attr, const char *buf, size_t size)
254 struct gpiod_data *data = dev_get_drvdata(dev);
256 ssize_t status = size;
259 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
260 if (sysfs_streq(trigger_types[i].name, buf))
264 if (i == ARRAY_SIZE(trigger_types))
267 flags = trigger_types[i].flags;
269 mutex_lock(&data->mutex);
271 if (flags == data->irq_flags) {
277 gpio_sysfs_free_irq(dev);
280 status = gpio_sysfs_request_irq(dev, flags);
286 mutex_unlock(&data->mutex);
290 static DEVICE_ATTR_RW(edge);
292 /* Caller holds gpiod-data mutex. */
293 static int gpio_sysfs_set_active_low(struct device *dev, int value)
295 struct gpiod_data *data = dev_get_drvdata(dev);
296 struct gpio_desc *desc = data->desc;
298 unsigned int flags = data->irq_flags;
300 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
304 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
306 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
308 /* reconfigure poll(2) support if enabled on one edge only */
309 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
310 flags == GPIO_IRQF_TRIGGER_RISING) {
311 gpio_sysfs_free_irq(dev);
312 status = gpio_sysfs_request_irq(dev, flags);
318 static ssize_t active_low_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct gpiod_data *data = dev_get_drvdata(dev);
322 struct gpio_desc *desc = data->desc;
325 mutex_lock(&data->mutex);
327 status = sprintf(buf, "%d\n",
328 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
330 mutex_unlock(&data->mutex);
335 static ssize_t active_low_store(struct device *dev,
336 struct device_attribute *attr, const char *buf, size_t size)
338 struct gpiod_data *data = dev_get_drvdata(dev);
342 mutex_lock(&data->mutex);
344 status = kstrtol(buf, 0, &value);
346 status = gpio_sysfs_set_active_low(dev, value);
348 mutex_unlock(&data->mutex);
350 return status ? : size;
352 static DEVICE_ATTR_RW(active_low);
354 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
357 struct device *dev = container_of(kobj, struct device, kobj);
358 struct gpiod_data *data = dev_get_drvdata(dev);
359 struct gpio_desc *desc = data->desc;
360 umode_t mode = attr->mode;
361 bool show_direction = data->direction_can_change;
363 if (attr == &dev_attr_direction.attr) {
366 } else if (attr == &dev_attr_edge.attr) {
367 if (gpiod_to_irq(desc) < 0)
369 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
376 static struct attribute *gpio_attrs[] = {
377 &dev_attr_direction.attr,
379 &dev_attr_value.attr,
380 &dev_attr_active_low.attr,
384 static const struct attribute_group gpio_group = {
386 .is_visible = gpio_is_visible,
389 static const struct attribute_group *gpio_groups[] = {
395 * /sys/class/gpio/gpiochipN/
396 * /base ... matching gpio_chip.base (N)
397 * /label ... matching gpio_chip.label
398 * /ngpio ... matching gpio_chip.ngpio
401 static ssize_t base_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
404 const struct gpio_chip *chip = dev_get_drvdata(dev);
406 return sprintf(buf, "%d\n", chip->base);
408 static DEVICE_ATTR_RO(base);
410 static ssize_t label_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 const struct gpio_chip *chip = dev_get_drvdata(dev);
415 return sprintf(buf, "%s\n", chip->label ? : "");
417 static DEVICE_ATTR_RO(label);
419 static ssize_t ngpio_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 const struct gpio_chip *chip = dev_get_drvdata(dev);
424 return sprintf(buf, "%u\n", chip->ngpio);
426 static DEVICE_ATTR_RO(ngpio);
428 static struct attribute *gpiochip_attrs[] = {
430 &dev_attr_label.attr,
431 &dev_attr_ngpio.attr,
434 ATTRIBUTE_GROUPS(gpiochip);
436 static struct gpio_desc *gpio_to_valid_desc(int gpio)
438 return gpio_is_valid(gpio) ? gpio_to_desc(gpio) : NULL;
442 * /sys/class/gpio/export ... write-only
443 * integer N ... number of GPIO to export (full access)
444 * /sys/class/gpio/unexport ... write-only
445 * integer N ... number of GPIO to unexport
447 static ssize_t export_store(struct class *class,
448 struct class_attribute *attr,
449 const char *buf, size_t len)
452 struct gpio_desc *desc;
455 status = kstrtol(buf, 0, &gpio);
459 desc = gpio_to_valid_desc(gpio);
460 /* reject invalid GPIOs */
462 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
466 /* No extra locking here; FLAG_SYSFS just signifies that the
467 * request and export were done by on behalf of userspace, so
468 * they may be undone on its behalf too.
471 status = gpiod_request(desc, "sysfs");
473 if (status == -EPROBE_DEFER)
477 status = gpiod_export(desc, true);
481 set_bit(FLAG_SYSFS, &desc->flags);
485 pr_debug("%s: status %d\n", __func__, status);
486 return status ? : len;
488 static CLASS_ATTR_WO(export);
490 static ssize_t unexport_store(struct class *class,
491 struct class_attribute *attr,
492 const char *buf, size_t len)
495 struct gpio_desc *desc;
498 status = kstrtol(buf, 0, &gpio);
502 desc = gpio_to_valid_desc(gpio);
503 /* reject bogus commands (gpio_unexport ignores them) */
505 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
511 /* No extra locking here; FLAG_SYSFS just signifies that the
512 * request and export were done by on behalf of userspace, so
513 * they may be undone on its behalf too.
515 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
521 pr_debug("%s: status %d\n", __func__, status);
522 return status ? : len;
524 static CLASS_ATTR_WO(unexport);
526 static struct attribute *gpio_class_attrs[] = {
527 &class_attr_export.attr,
528 &class_attr_unexport.attr,
531 ATTRIBUTE_GROUPS(gpio_class);
533 static struct class gpio_class = {
535 .owner = THIS_MODULE,
537 .class_groups = gpio_class_groups,
542 * gpiod_export - export a GPIO through sysfs
543 * @desc: GPIO to make available, already requested
544 * @direction_may_change: true if userspace may change GPIO direction
545 * Context: arch_initcall or later
547 * When drivers want to make a GPIO accessible to userspace after they
548 * have requested it -- perhaps while debugging, or as part of their
549 * public interface -- they may use this routine. If the GPIO can
550 * change direction (some can't) and the caller allows it, userspace
551 * will see "direction" sysfs attribute which may be used to change
552 * the gpio's direction. A "value" attribute will always be provided.
554 * Returns zero on success, else an error.
556 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
558 struct gpio_chip *chip;
559 struct gpio_device *gdev;
560 struct gpiod_data *data;
563 const char *ioname = NULL;
567 /* can't export until sysfs is available ... */
569 pr_debug("%s: called too early!\n", __func__);
574 pr_debug("%s: invalid gpio descriptor\n", __func__);
581 mutex_lock(&sysfs_lock);
583 /* check if chip is being removed */
584 if (!chip || !gdev->mockdev) {
589 spin_lock_irqsave(&gpio_lock, flags);
590 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
591 test_bit(FLAG_EXPORT, &desc->flags)) {
592 spin_unlock_irqrestore(&gpio_lock, flags);
593 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
595 test_bit(FLAG_REQUESTED, &desc->flags),
596 test_bit(FLAG_EXPORT, &desc->flags));
600 spin_unlock_irqrestore(&gpio_lock, flags);
602 data = kzalloc(sizeof(*data), GFP_KERNEL);
609 mutex_init(&data->mutex);
610 if (chip->direction_input && chip->direction_output)
611 data->direction_can_change = direction_may_change;
613 data->direction_can_change = false;
615 offset = gpio_chip_hwgpio(desc);
616 if (chip->names && chip->names[offset])
617 ioname = chip->names[offset];
619 dev = device_create_with_groups(&gpio_class, &gdev->dev,
620 MKDEV(0, 0), data, gpio_groups,
621 ioname ? ioname : "gpio%u",
624 status = PTR_ERR(dev);
628 set_bit(FLAG_EXPORT, &desc->flags);
629 mutex_unlock(&sysfs_lock);
635 mutex_unlock(&sysfs_lock);
636 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
639 EXPORT_SYMBOL_GPL(gpiod_export);
641 static int match_export(struct device *dev, const void *desc)
643 struct gpiod_data *data = dev_get_drvdata(dev);
645 return data->desc == desc;
649 * gpiod_export_link - create a sysfs link to an exported GPIO node
650 * @dev: device under which to create symlink
651 * @name: name of the symlink
652 * @desc: GPIO to create symlink to, already exported
654 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
655 * node. Caller is responsible for unlinking.
657 * Returns zero on success, else an error.
659 int gpiod_export_link(struct device *dev, const char *name,
660 struct gpio_desc *desc)
666 pr_warn("%s: invalid GPIO\n", __func__);
670 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
674 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
679 EXPORT_SYMBOL_GPL(gpiod_export_link);
682 * gpiod_unexport - reverse effect of gpiod_export()
683 * @desc: GPIO to make unavailable
685 * This is implicit on gpiod_free().
687 void gpiod_unexport(struct gpio_desc *desc)
689 struct gpiod_data *data;
693 pr_warn("%s: invalid GPIO\n", __func__);
697 mutex_lock(&sysfs_lock);
699 if (!test_bit(FLAG_EXPORT, &desc->flags))
702 dev = class_find_device(&gpio_class, NULL, desc, match_export);
706 data = dev_get_drvdata(dev);
708 clear_bit(FLAG_EXPORT, &desc->flags);
710 device_unregister(dev);
713 * Release irq after deregistration to prevent race with edge_store.
716 gpio_sysfs_free_irq(dev);
718 mutex_unlock(&sysfs_lock);
726 mutex_unlock(&sysfs_lock);
728 EXPORT_SYMBOL_GPL(gpiod_unexport);
730 int gpiochip_sysfs_register(struct gpio_device *gdev)
733 struct device *parent;
734 struct gpio_chip *chip = gdev->chip;
737 * Many systems add gpio chips for SOC support very early,
738 * before driver model support is available. In those cases we
739 * register later, in gpiolib_sysfs_init() ... here we just
740 * verify that _some_ field of gpio_class got initialized.
746 * For sysfs backward compatibility we need to preserve this
747 * preferred parenting to the gpio_chip parent field, if set.
750 parent = chip->parent;
754 /* use chip->base for the ID; it's already known to be unique */
755 dev = device_create_with_groups(&gpio_class, parent,
757 chip, gpiochip_groups,
758 "gpiochip%d", chip->base);
762 mutex_lock(&sysfs_lock);
764 mutex_unlock(&sysfs_lock);
769 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
771 struct gpio_desc *desc;
772 struct gpio_chip *chip = gdev->chip;
778 device_unregister(gdev->mockdev);
780 /* prevent further gpiod exports */
781 mutex_lock(&sysfs_lock);
782 gdev->mockdev = NULL;
783 mutex_unlock(&sysfs_lock);
785 /* unregister gpiod class devices owned by sysfs */
786 for (i = 0; i < chip->ngpio; i++) {
787 desc = &gdev->descs[i];
788 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
793 static int __init gpiolib_sysfs_init(void)
797 struct gpio_device *gdev;
799 status = class_register(&gpio_class);
803 /* Scan and register the gpio_chips which registered very
804 * early (e.g. before the class_register above was called).
806 * We run before arch_initcall() so chip->dev nodes can have
807 * registered, and so arch_initcall() can always gpio_export().
809 spin_lock_irqsave(&gpio_lock, flags);
810 list_for_each_entry(gdev, &gpio_devices, list) {
815 * TODO we yield gpio_lock here because
816 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
817 * and needs to be fixed.
819 * Also it would be nice to use gpiochip_find() here so we
820 * can keep gpio_chips local to gpiolib.c, but the yield of
821 * gpio_lock prevents us from doing this.
823 spin_unlock_irqrestore(&gpio_lock, flags);
824 status = gpiochip_sysfs_register(gdev);
825 spin_lock_irqsave(&gpio_lock, flags);
827 spin_unlock_irqrestore(&gpio_lock, flags);
831 postcore_initcall(gpiolib_sysfs_init);