1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <linux/cleanup.h>
5 #include <linux/device.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/kdev_t.h>
10 #include <linux/kstrtox.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/printk.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <linux/srcu.h>
18 #include <linux/sysfs.h>
19 #include <linux/types.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/driver.h>
24 #include <uapi/linux/gpio.h>
27 #include "gpiolib-sysfs.h"
31 #define GPIO_IRQF_TRIGGER_NONE 0
32 #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
33 #define GPIO_IRQF_TRIGGER_RISING BIT(1)
34 #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
35 GPIO_IRQF_TRIGGER_RISING)
38 struct gpio_desc *desc;
41 struct kernfs_node *value_kn;
43 unsigned char irq_flags;
45 bool direction_can_change;
49 * Lock to serialise gpiod export and unexport, and prevent re-export of
50 * gpiod whose chip is being unregistered.
52 static DEFINE_MUTEX(sysfs_lock);
55 * /sys/class/gpio/gpioN... only for GPIOs that are exported
57 * * MAY BE OMITTED if kernel won't allow direction changes
58 * * is read/write as "in" or "out"
59 * * may also be written as "high" or "low", initializing
60 * output value as specified ("out" implies "low")
62 * * always readable, subject to hardware behavior
63 * * may be writable, as zero/nonzero
65 * * configures behavior of poll(2) on /value
66 * * available only if pin can generate IRQs on input
67 * * is read/write as "none", "falling", "rising", or "both"
69 * * configures polarity of /value
70 * * is read/write as zero/nonzero
71 * * also affects existing and subsequent "falling" and "rising"
75 static ssize_t direction_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
78 struct gpiod_data *data = dev_get_drvdata(dev);
79 struct gpio_desc *desc = data->desc;
82 scoped_guard(mutex, &data->mutex) {
83 gpiod_get_direction(desc);
84 value = !!test_bit(FLAG_IS_OUT, &desc->flags);
87 return sysfs_emit(buf, "%s\n", value ? "out" : "in");
90 static ssize_t direction_store(struct device *dev,
91 struct device_attribute *attr, const char *buf, size_t size)
93 struct gpiod_data *data = dev_get_drvdata(dev);
94 struct gpio_desc *desc = data->desc;
97 guard(mutex)(&data->mutex);
99 if (sysfs_streq(buf, "high"))
100 status = gpiod_direction_output_raw(desc, 1);
101 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
102 status = gpiod_direction_output_raw(desc, 0);
103 else if (sysfs_streq(buf, "in"))
104 status = gpiod_direction_input(desc);
108 return status ? : size;
110 static DEVICE_ATTR_RW(direction);
112 static ssize_t value_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
115 struct gpiod_data *data = dev_get_drvdata(dev);
116 struct gpio_desc *desc = data->desc;
119 scoped_guard(mutex, &data->mutex)
120 status = gpiod_get_value_cansleep(desc);
125 return sysfs_emit(buf, "%zd\n", status);
128 static ssize_t value_store(struct device *dev,
129 struct device_attribute *attr, const char *buf, size_t size)
131 struct gpiod_data *data = dev_get_drvdata(dev);
132 struct gpio_desc *desc = data->desc;
136 status = kstrtol(buf, 0, &value);
140 guard(mutex)(&data->mutex);
142 status = gpiod_set_value_cansleep(desc, value);
148 static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
150 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
152 struct gpiod_data *data = priv;
154 sysfs_notify_dirent(data->value_kn);
159 /* Caller holds gpiod-data mutex. */
160 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
162 struct gpiod_data *data = dev_get_drvdata(dev);
163 struct gpio_desc *desc = data->desc;
164 unsigned long irq_flags;
167 CLASS(gpio_chip_guard, guard)(desc);
171 data->irq = gpiod_to_irq(desc);
175 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
179 irq_flags = IRQF_SHARED;
180 if (flags & GPIO_IRQF_TRIGGER_FALLING) {
181 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
182 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
183 set_bit(FLAG_EDGE_FALLING, &desc->flags);
185 if (flags & GPIO_IRQF_TRIGGER_RISING) {
186 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
187 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
188 set_bit(FLAG_EDGE_RISING, &desc->flags);
192 * FIXME: This should be done in the irq_request_resources callback
193 * when the irq is requested, but a few drivers currently fail
196 * Remove this redundant call (along with the corresponding
197 * unlock) when those drivers have been fixed.
199 ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
203 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
208 data->irq_flags = flags;
213 gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
215 clear_bit(FLAG_EDGE_RISING, &desc->flags);
216 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
217 sysfs_put(data->value_kn);
223 * Caller holds gpiod-data mutex (unless called after class-device
226 static void gpio_sysfs_free_irq(struct device *dev)
228 struct gpiod_data *data = dev_get_drvdata(dev);
229 struct gpio_desc *desc = data->desc;
231 CLASS(gpio_chip_guard, guard)(desc);
236 free_irq(data->irq, data);
237 gpiochip_unlock_as_irq(guard.gc, gpio_chip_hwgpio(desc));
238 clear_bit(FLAG_EDGE_RISING, &desc->flags);
239 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
240 sysfs_put(data->value_kn);
243 static const char * const trigger_names[] = {
244 [GPIO_IRQF_TRIGGER_NONE] = "none",
245 [GPIO_IRQF_TRIGGER_FALLING] = "falling",
246 [GPIO_IRQF_TRIGGER_RISING] = "rising",
247 [GPIO_IRQF_TRIGGER_BOTH] = "both",
250 static ssize_t edge_show(struct device *dev,
251 struct device_attribute *attr, char *buf)
253 struct gpiod_data *data = dev_get_drvdata(dev);
256 scoped_guard(mutex, &data->mutex)
257 flags = data->irq_flags;
259 if (flags >= ARRAY_SIZE(trigger_names))
262 return sysfs_emit(buf, "%s\n", trigger_names[flags]);
265 static ssize_t edge_store(struct device *dev,
266 struct device_attribute *attr, const char *buf, size_t size)
268 struct gpiod_data *data = dev_get_drvdata(dev);
269 ssize_t status = size;
272 flags = sysfs_match_string(trigger_names, buf);
276 guard(mutex)(&data->mutex);
278 if (flags == data->irq_flags)
282 gpio_sysfs_free_irq(dev);
287 status = gpio_sysfs_request_irq(dev, flags);
291 gpiod_line_state_notify(data->desc, GPIO_V2_LINE_CHANGED_CONFIG);
295 static DEVICE_ATTR_RW(edge);
297 /* Caller holds gpiod-data mutex. */
298 static int gpio_sysfs_set_active_low(struct device *dev, int value)
300 struct gpiod_data *data = dev_get_drvdata(dev);
301 unsigned int flags = data->irq_flags;
302 struct gpio_desc *desc = data->desc;
306 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
309 assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
311 /* reconfigure poll(2) support if enabled on one edge only */
312 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
313 flags == GPIO_IRQF_TRIGGER_RISING) {
314 gpio_sysfs_free_irq(dev);
315 status = gpio_sysfs_request_irq(dev, flags);
318 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
323 static ssize_t active_low_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct gpiod_data *data = dev_get_drvdata(dev);
327 struct gpio_desc *desc = data->desc;
330 scoped_guard(mutex, &data->mutex)
331 value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
333 return sysfs_emit(buf, "%d\n", value);
336 static ssize_t active_low_store(struct device *dev,
337 struct device_attribute *attr, const char *buf, size_t size)
339 struct gpiod_data *data = dev_get_drvdata(dev);
343 status = kstrtol(buf, 0, &value);
347 guard(mutex)(&data->mutex);
349 return gpio_sysfs_set_active_low(dev, value) ?: size;
351 static DEVICE_ATTR_RW(active_low);
353 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
356 struct device *dev = kobj_to_dev(kobj);
357 struct gpiod_data *data = dev_get_drvdata(dev);
358 struct gpio_desc *desc = data->desc;
359 umode_t mode = attr->mode;
360 bool show_direction = data->direction_can_change;
362 if (attr == &dev_attr_direction.attr) {
365 } else if (attr == &dev_attr_edge.attr) {
366 if (gpiod_to_irq(desc) < 0)
368 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
375 static struct attribute *gpio_attrs[] = {
376 &dev_attr_direction.attr,
378 &dev_attr_value.attr,
379 &dev_attr_active_low.attr,
383 static const struct attribute_group gpio_group = {
385 .is_visible = gpio_is_visible,
388 static const struct attribute_group *gpio_groups[] = {
394 * /sys/class/gpio/gpiochipN/
395 * /base ... matching gpio_chip.base (N)
396 * /label ... matching gpio_chip.label
397 * /ngpio ... matching gpio_chip.ngpio
400 static ssize_t base_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 const struct gpio_device *gdev = dev_get_drvdata(dev);
405 return sysfs_emit(buf, "%u\n", gdev->base);
407 static DEVICE_ATTR_RO(base);
409 static ssize_t label_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 const struct gpio_device *gdev = dev_get_drvdata(dev);
414 return sysfs_emit(buf, "%s\n", gdev->label);
416 static DEVICE_ATTR_RO(label);
418 static ssize_t ngpio_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
421 const struct gpio_device *gdev = dev_get_drvdata(dev);
423 return sysfs_emit(buf, "%u\n", gdev->ngpio);
425 static DEVICE_ATTR_RO(ngpio);
427 static struct attribute *gpiochip_attrs[] = {
429 &dev_attr_label.attr,
430 &dev_attr_ngpio.attr,
433 ATTRIBUTE_GROUPS(gpiochip);
436 * /sys/class/gpio/export ... write-only
437 * integer N ... number of GPIO to export (full access)
438 * /sys/class/gpio/unexport ... write-only
439 * integer N ... number of GPIO to unexport
441 static ssize_t export_store(const struct class *class,
442 const struct class_attribute *attr,
443 const char *buf, size_t len)
445 struct gpio_desc *desc;
449 status = kstrtol(buf, 0, &gpio);
453 desc = gpio_to_desc(gpio);
454 /* reject invalid GPIOs */
456 pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio);
460 CLASS(gpio_chip_guard, guard)(desc);
464 offset = gpio_chip_hwgpio(desc);
465 if (!gpiochip_line_is_valid(guard.gc, offset)) {
466 pr_debug_ratelimited("%s: GPIO %ld masked\n", __func__, gpio);
470 /* No extra locking here; FLAG_SYSFS just signifies that the
471 * request and export were done by on behalf of userspace, so
472 * they may be undone on its behalf too.
475 status = gpiod_request_user(desc, "sysfs");
479 status = gpiod_set_transitory(desc, false);
485 status = gpiod_export(desc, true);
489 set_bit(FLAG_SYSFS, &desc->flags);
490 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
495 pr_debug("%s: status %d\n", __func__, status);
496 return status ? : len;
498 static CLASS_ATTR_WO(export);
500 static ssize_t unexport_store(const struct class *class,
501 const struct class_attribute *attr,
502 const char *buf, size_t len)
504 struct gpio_desc *desc;
508 status = kstrtol(buf, 0, &gpio);
512 desc = gpio_to_desc(gpio);
513 /* reject bogus commands (gpiod_unexport() ignores them) */
515 pr_debug_ratelimited("%s: invalid GPIO %ld\n", __func__, gpio);
521 /* No extra locking here; FLAG_SYSFS just signifies that the
522 * request and export were done by on behalf of userspace, so
523 * they may be undone on its behalf too.
525 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
526 gpiod_unexport(desc);
532 pr_debug("%s: status %d\n", __func__, status);
533 return status ? : len;
535 static CLASS_ATTR_WO(unexport);
537 static struct attribute *gpio_class_attrs[] = {
538 &class_attr_export.attr,
539 &class_attr_unexport.attr,
542 ATTRIBUTE_GROUPS(gpio_class);
544 static const struct class gpio_class = {
546 .class_groups = gpio_class_groups,
550 * gpiod_export - export a GPIO through sysfs
551 * @desc: GPIO to make available, already requested
552 * @direction_may_change: true if userspace may change GPIO direction
553 * Context: arch_initcall or later
555 * When drivers want to make a GPIO accessible to userspace after they
556 * have requested it -- perhaps while debugging, or as part of their
557 * public interface -- they may use this routine. If the GPIO can
558 * change direction (some can't) and the caller allows it, userspace
559 * will see "direction" sysfs attribute which may be used to change
560 * the gpio's direction. A "value" attribute will always be provided.
563 * 0 on success, or negative errno on failure.
565 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
567 struct gpio_device *gdev;
568 struct gpiod_data *data;
572 /* can't export until sysfs is available ... */
573 if (!class_is_registered(&gpio_class)) {
574 pr_debug("%s: called too early!\n", __func__);
579 pr_debug("%s: invalid gpio descriptor\n", __func__);
583 CLASS(gpio_chip_guard, guard)(desc);
587 if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
592 guard(mutex)(&sysfs_lock);
594 /* check if chip is being removed */
595 if (!gdev->mockdev) {
600 if (!test_bit(FLAG_REQUESTED, &desc->flags)) {
601 gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__);
606 data = kzalloc(sizeof(*data), GFP_KERNEL);
613 mutex_init(&data->mutex);
614 if (guard.gc->direction_input && guard.gc->direction_output)
615 data->direction_can_change = direction_may_change;
617 data->direction_can_change = false;
619 dev = device_create_with_groups(&gpio_class, &gdev->dev,
620 MKDEV(0, 0), data, gpio_groups,
621 "gpio%u", desc_to_gpio(desc));
623 status = PTR_ERR(dev);
632 clear_bit(FLAG_EXPORT, &desc->flags);
633 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
636 EXPORT_SYMBOL_GPL(gpiod_export);
638 static int match_export(struct device *dev, const void *desc)
640 struct gpiod_data *data = dev_get_drvdata(dev);
642 return data->desc == desc;
646 * gpiod_export_link - create a sysfs link to an exported GPIO node
647 * @dev: device under which to create symlink
648 * @name: name of the symlink
649 * @desc: GPIO to create symlink to, already exported
651 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
652 * node. Caller is responsible for unlinking.
655 * 0 on success, or negative errno on failure.
657 int gpiod_export_link(struct device *dev, const char *name,
658 struct gpio_desc *desc)
664 pr_warn("%s: invalid GPIO\n", __func__);
668 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
672 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
677 EXPORT_SYMBOL_GPL(gpiod_export_link);
680 * gpiod_unexport - reverse effect of gpiod_export()
681 * @desc: GPIO to make unavailable
683 * This is implicit on gpiod_free().
685 void gpiod_unexport(struct gpio_desc *desc)
687 struct gpiod_data *data;
691 pr_warn("%s: invalid GPIO\n", __func__);
695 scoped_guard(mutex, &sysfs_lock) {
696 if (!test_bit(FLAG_EXPORT, &desc->flags))
699 dev = class_find_device(&gpio_class, NULL, desc, match_export);
703 data = dev_get_drvdata(dev);
704 clear_bit(FLAG_EXPORT, &desc->flags);
705 device_unregister(dev);
708 * Release irq after deregistration to prevent race with
712 gpio_sysfs_free_irq(dev);
718 EXPORT_SYMBOL_GPL(gpiod_unexport);
720 int gpiochip_sysfs_register(struct gpio_device *gdev)
722 struct gpio_chip *chip;
723 struct device *parent;
727 * Many systems add gpio chips for SOC support very early,
728 * before driver model support is available. In those cases we
729 * register later, in gpiolib_sysfs_init() ... here we just
730 * verify that _some_ field of gpio_class got initialized.
732 if (!class_is_registered(&gpio_class))
735 guard(srcu)(&gdev->srcu);
737 chip = srcu_dereference(gdev->chip, &gdev->srcu);
742 * For sysfs backward compatibility we need to preserve this
743 * preferred parenting to the gpio_chip parent field, if set.
746 parent = chip->parent;
750 /* use chip->base for the ID; it's already known to be unique */
751 dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), gdev,
752 gpiochip_groups, GPIOCHIP_NAME "%d",
757 guard(mutex)(&sysfs_lock);
763 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
765 struct gpio_desc *desc;
766 struct gpio_chip *chip;
768 scoped_guard(mutex, &sysfs_lock) {
772 device_unregister(gdev->mockdev);
774 /* prevent further gpiod exports */
775 gdev->mockdev = NULL;
778 guard(srcu)(&gdev->srcu);
780 chip = srcu_dereference(gdev->chip, &gdev->srcu);
784 /* unregister gpiod class devices owned by sysfs */
785 for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
786 gpiod_unexport(desc);
792 * We're not really looking for a device - we just want to iterate over the
793 * list and call this callback for each GPIO device. This is why this function
796 static int gpiofind_sysfs_register(struct gpio_chip *gc, const void *data)
798 struct gpio_device *gdev = gc->gpiodev;
804 ret = gpiochip_sysfs_register(gdev);
806 chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
811 static int __init gpiolib_sysfs_init(void)
815 status = class_register(&gpio_class);
819 /* Scan and register the gpio_chips which registered very
820 * early (e.g. before the class_register above was called).
822 * We run before arch_initcall() so chip->dev nodes can have
823 * registered, and so arch_initcall() can always gpiod_export().
825 (void)gpio_device_find(NULL, gpiofind_sysfs_register);
829 postcore_initcall(gpiolib_sysfs_init);