0001
0002 #include <linux/idr.h>
0003 #include <linux/mutex.h>
0004 #include <linux/device.h>
0005 #include <linux/sysfs.h>
0006 #include <linux/gpio/consumer.h>
0007 #include <linux/gpio/driver.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/kdev_t.h>
0010 #include <linux/slab.h>
0011 #include <linux/ctype.h>
0012
0013 #include "gpiolib.h"
0014 #include "gpiolib-sysfs.h"
0015
0016 #define GPIO_IRQF_TRIGGER_NONE 0
0017 #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
0018 #define GPIO_IRQF_TRIGGER_RISING BIT(1)
0019 #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
0020 GPIO_IRQF_TRIGGER_RISING)
0021
0022 struct gpiod_data {
0023 struct gpio_desc *desc;
0024
0025 struct mutex mutex;
0026 struct kernfs_node *value_kn;
0027 int irq;
0028 unsigned char irq_flags;
0029
0030 bool direction_can_change;
0031 };
0032
0033
0034
0035
0036
0037 static DEFINE_MUTEX(sysfs_lock);
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 static ssize_t direction_show(struct device *dev,
0061 struct device_attribute *attr, char *buf)
0062 {
0063 struct gpiod_data *data = dev_get_drvdata(dev);
0064 struct gpio_desc *desc = data->desc;
0065 int value;
0066
0067 mutex_lock(&data->mutex);
0068
0069 gpiod_get_direction(desc);
0070 value = !!test_bit(FLAG_IS_OUT, &desc->flags);
0071
0072 mutex_unlock(&data->mutex);
0073
0074 return sysfs_emit(buf, "%s\n", value ? "out" : "in");
0075 }
0076
0077 static ssize_t direction_store(struct device *dev,
0078 struct device_attribute *attr, const char *buf, size_t size)
0079 {
0080 struct gpiod_data *data = dev_get_drvdata(dev);
0081 struct gpio_desc *desc = data->desc;
0082 ssize_t status;
0083
0084 mutex_lock(&data->mutex);
0085
0086 if (sysfs_streq(buf, "high"))
0087 status = gpiod_direction_output_raw(desc, 1);
0088 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
0089 status = gpiod_direction_output_raw(desc, 0);
0090 else if (sysfs_streq(buf, "in"))
0091 status = gpiod_direction_input(desc);
0092 else
0093 status = -EINVAL;
0094
0095 mutex_unlock(&data->mutex);
0096
0097 return status ? : size;
0098 }
0099 static DEVICE_ATTR_RW(direction);
0100
0101 static ssize_t value_show(struct device *dev,
0102 struct device_attribute *attr, char *buf)
0103 {
0104 struct gpiod_data *data = dev_get_drvdata(dev);
0105 struct gpio_desc *desc = data->desc;
0106 ssize_t status;
0107
0108 mutex_lock(&data->mutex);
0109
0110 status = gpiod_get_value_cansleep(desc);
0111
0112 mutex_unlock(&data->mutex);
0113
0114 if (status < 0)
0115 return status;
0116
0117 return sysfs_emit(buf, "%zd\n", status);
0118 }
0119
0120 static ssize_t value_store(struct device *dev,
0121 struct device_attribute *attr, const char *buf, size_t size)
0122 {
0123 struct gpiod_data *data = dev_get_drvdata(dev);
0124 struct gpio_desc *desc = data->desc;
0125 ssize_t status;
0126 long value;
0127
0128 status = kstrtol(buf, 0, &value);
0129
0130 mutex_lock(&data->mutex);
0131
0132 if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
0133 status = -EPERM;
0134 } else if (status == 0) {
0135 gpiod_set_value_cansleep(desc, value);
0136 status = size;
0137 }
0138
0139 mutex_unlock(&data->mutex);
0140
0141 return status;
0142 }
0143 static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
0144
0145 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
0146 {
0147 struct gpiod_data *data = priv;
0148
0149 sysfs_notify_dirent(data->value_kn);
0150
0151 return IRQ_HANDLED;
0152 }
0153
0154
0155 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
0156 {
0157 struct gpiod_data *data = dev_get_drvdata(dev);
0158 struct gpio_desc *desc = data->desc;
0159 unsigned long irq_flags;
0160 int ret;
0161
0162 data->irq = gpiod_to_irq(desc);
0163 if (data->irq < 0)
0164 return -EIO;
0165
0166 data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
0167 if (!data->value_kn)
0168 return -ENODEV;
0169
0170 irq_flags = IRQF_SHARED;
0171 if (flags & GPIO_IRQF_TRIGGER_FALLING)
0172 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
0173 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
0174 if (flags & GPIO_IRQF_TRIGGER_RISING)
0175 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
0176 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186 ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
0187 if (ret < 0)
0188 goto err_put_kn;
0189
0190 ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
0191 "gpiolib", data);
0192 if (ret < 0)
0193 goto err_unlock;
0194
0195 data->irq_flags = flags;
0196
0197 return 0;
0198
0199 err_unlock:
0200 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
0201 err_put_kn:
0202 sysfs_put(data->value_kn);
0203
0204 return ret;
0205 }
0206
0207
0208
0209
0210
0211 static void gpio_sysfs_free_irq(struct device *dev)
0212 {
0213 struct gpiod_data *data = dev_get_drvdata(dev);
0214 struct gpio_desc *desc = data->desc;
0215
0216 data->irq_flags = 0;
0217 free_irq(data->irq, data);
0218 gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
0219 sysfs_put(data->value_kn);
0220 }
0221
0222 static const char * const trigger_names[] = {
0223 [GPIO_IRQF_TRIGGER_NONE] = "none",
0224 [GPIO_IRQF_TRIGGER_FALLING] = "falling",
0225 [GPIO_IRQF_TRIGGER_RISING] = "rising",
0226 [GPIO_IRQF_TRIGGER_BOTH] = "both",
0227 };
0228
0229 static ssize_t edge_show(struct device *dev,
0230 struct device_attribute *attr, char *buf)
0231 {
0232 struct gpiod_data *data = dev_get_drvdata(dev);
0233 int flags;
0234
0235 mutex_lock(&data->mutex);
0236
0237 flags = data->irq_flags;
0238
0239 mutex_unlock(&data->mutex);
0240
0241 if (flags >= ARRAY_SIZE(trigger_names))
0242 return 0;
0243
0244 return sysfs_emit(buf, "%s\n", trigger_names[flags]);
0245 }
0246
0247 static ssize_t edge_store(struct device *dev,
0248 struct device_attribute *attr, const char *buf, size_t size)
0249 {
0250 struct gpiod_data *data = dev_get_drvdata(dev);
0251 ssize_t status = size;
0252 int flags;
0253
0254 flags = sysfs_match_string(trigger_names, buf);
0255 if (flags < 0)
0256 return flags;
0257
0258 mutex_lock(&data->mutex);
0259
0260 if (flags == data->irq_flags) {
0261 status = size;
0262 goto out_unlock;
0263 }
0264
0265 if (data->irq_flags)
0266 gpio_sysfs_free_irq(dev);
0267
0268 if (flags) {
0269 status = gpio_sysfs_request_irq(dev, flags);
0270 if (!status)
0271 status = size;
0272 }
0273
0274 out_unlock:
0275 mutex_unlock(&data->mutex);
0276
0277 return status;
0278 }
0279 static DEVICE_ATTR_RW(edge);
0280
0281
0282 static int gpio_sysfs_set_active_low(struct device *dev, int value)
0283 {
0284 struct gpiod_data *data = dev_get_drvdata(dev);
0285 struct gpio_desc *desc = data->desc;
0286 int status = 0;
0287 unsigned int flags = data->irq_flags;
0288
0289 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
0290 return 0;
0291
0292 assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
0293
0294
0295 if (flags == GPIO_IRQF_TRIGGER_FALLING ||
0296 flags == GPIO_IRQF_TRIGGER_RISING) {
0297 gpio_sysfs_free_irq(dev);
0298 status = gpio_sysfs_request_irq(dev, flags);
0299 }
0300
0301 return status;
0302 }
0303
0304 static ssize_t active_low_show(struct device *dev,
0305 struct device_attribute *attr, char *buf)
0306 {
0307 struct gpiod_data *data = dev_get_drvdata(dev);
0308 struct gpio_desc *desc = data->desc;
0309 int value;
0310
0311 mutex_lock(&data->mutex);
0312
0313 value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
0314
0315 mutex_unlock(&data->mutex);
0316
0317 return sysfs_emit(buf, "%d\n", value);
0318 }
0319
0320 static ssize_t active_low_store(struct device *dev,
0321 struct device_attribute *attr, const char *buf, size_t size)
0322 {
0323 struct gpiod_data *data = dev_get_drvdata(dev);
0324 ssize_t status;
0325 long value;
0326
0327 status = kstrtol(buf, 0, &value);
0328 if (status)
0329 return status;
0330
0331 mutex_lock(&data->mutex);
0332
0333 status = gpio_sysfs_set_active_low(dev, value);
0334
0335 mutex_unlock(&data->mutex);
0336
0337 return status ? : size;
0338 }
0339 static DEVICE_ATTR_RW(active_low);
0340
0341 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
0342 int n)
0343 {
0344 struct device *dev = kobj_to_dev(kobj);
0345 struct gpiod_data *data = dev_get_drvdata(dev);
0346 struct gpio_desc *desc = data->desc;
0347 umode_t mode = attr->mode;
0348 bool show_direction = data->direction_can_change;
0349
0350 if (attr == &dev_attr_direction.attr) {
0351 if (!show_direction)
0352 mode = 0;
0353 } else if (attr == &dev_attr_edge.attr) {
0354 if (gpiod_to_irq(desc) < 0)
0355 mode = 0;
0356 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
0357 mode = 0;
0358 }
0359
0360 return mode;
0361 }
0362
0363 static struct attribute *gpio_attrs[] = {
0364 &dev_attr_direction.attr,
0365 &dev_attr_edge.attr,
0366 &dev_attr_value.attr,
0367 &dev_attr_active_low.attr,
0368 NULL,
0369 };
0370
0371 static const struct attribute_group gpio_group = {
0372 .attrs = gpio_attrs,
0373 .is_visible = gpio_is_visible,
0374 };
0375
0376 static const struct attribute_group *gpio_groups[] = {
0377 &gpio_group,
0378 NULL
0379 };
0380
0381
0382
0383
0384
0385
0386
0387
0388 static ssize_t base_show(struct device *dev,
0389 struct device_attribute *attr, char *buf)
0390 {
0391 const struct gpio_chip *chip = dev_get_drvdata(dev);
0392
0393 return sysfs_emit(buf, "%d\n", chip->base);
0394 }
0395 static DEVICE_ATTR_RO(base);
0396
0397 static ssize_t label_show(struct device *dev,
0398 struct device_attribute *attr, char *buf)
0399 {
0400 const struct gpio_chip *chip = dev_get_drvdata(dev);
0401
0402 return sysfs_emit(buf, "%s\n", chip->label ?: "");
0403 }
0404 static DEVICE_ATTR_RO(label);
0405
0406 static ssize_t ngpio_show(struct device *dev,
0407 struct device_attribute *attr, char *buf)
0408 {
0409 const struct gpio_chip *chip = dev_get_drvdata(dev);
0410
0411 return sysfs_emit(buf, "%u\n", chip->ngpio);
0412 }
0413 static DEVICE_ATTR_RO(ngpio);
0414
0415 static struct attribute *gpiochip_attrs[] = {
0416 &dev_attr_base.attr,
0417 &dev_attr_label.attr,
0418 &dev_attr_ngpio.attr,
0419 NULL,
0420 };
0421 ATTRIBUTE_GROUPS(gpiochip);
0422
0423
0424
0425
0426
0427
0428
0429 static ssize_t export_store(struct class *class,
0430 struct class_attribute *attr,
0431 const char *buf, size_t len)
0432 {
0433 long gpio;
0434 struct gpio_desc *desc;
0435 int status;
0436 struct gpio_chip *gc;
0437 int offset;
0438
0439 status = kstrtol(buf, 0, &gpio);
0440 if (status < 0)
0441 goto done;
0442
0443 desc = gpio_to_desc(gpio);
0444
0445 if (!desc) {
0446 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
0447 return -EINVAL;
0448 }
0449 gc = desc->gdev->chip;
0450 offset = gpio_chip_hwgpio(desc);
0451 if (!gpiochip_line_is_valid(gc, offset)) {
0452 pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
0453 return -EINVAL;
0454 }
0455
0456
0457
0458
0459
0460
0461 status = gpiod_request_user(desc, "sysfs");
0462 if (status)
0463 goto done;
0464
0465 status = gpiod_set_transitory(desc, false);
0466 if (!status) {
0467 status = gpiod_export(desc, true);
0468 if (status < 0)
0469 gpiod_free(desc);
0470 else
0471 set_bit(FLAG_SYSFS, &desc->flags);
0472 }
0473
0474 done:
0475 if (status)
0476 pr_debug("%s: status %d\n", __func__, status);
0477 return status ? : len;
0478 }
0479 static CLASS_ATTR_WO(export);
0480
0481 static ssize_t unexport_store(struct class *class,
0482 struct class_attribute *attr,
0483 const char *buf, size_t len)
0484 {
0485 long gpio;
0486 struct gpio_desc *desc;
0487 int status;
0488
0489 status = kstrtol(buf, 0, &gpio);
0490 if (status < 0)
0491 goto done;
0492
0493 desc = gpio_to_desc(gpio);
0494
0495 if (!desc) {
0496 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
0497 return -EINVAL;
0498 }
0499
0500 status = -EINVAL;
0501
0502
0503
0504
0505
0506 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
0507 status = 0;
0508 gpiod_free(desc);
0509 }
0510 done:
0511 if (status)
0512 pr_debug("%s: status %d\n", __func__, status);
0513 return status ? : len;
0514 }
0515 static CLASS_ATTR_WO(unexport);
0516
0517 static struct attribute *gpio_class_attrs[] = {
0518 &class_attr_export.attr,
0519 &class_attr_unexport.attr,
0520 NULL,
0521 };
0522 ATTRIBUTE_GROUPS(gpio_class);
0523
0524 static struct class gpio_class = {
0525 .name = "gpio",
0526 .owner = THIS_MODULE,
0527
0528 .class_groups = gpio_class_groups,
0529 };
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
0548 {
0549 struct gpio_chip *chip;
0550 struct gpio_device *gdev;
0551 struct gpiod_data *data;
0552 unsigned long flags;
0553 int status;
0554 const char *ioname = NULL;
0555 struct device *dev;
0556 int offset;
0557
0558
0559 if (!gpio_class.p) {
0560 pr_debug("%s: called too early!\n", __func__);
0561 return -ENOENT;
0562 }
0563
0564 if (!desc) {
0565 pr_debug("%s: invalid gpio descriptor\n", __func__);
0566 return -EINVAL;
0567 }
0568
0569 gdev = desc->gdev;
0570 chip = gdev->chip;
0571
0572 mutex_lock(&sysfs_lock);
0573
0574
0575 if (!chip || !gdev->mockdev) {
0576 status = -ENODEV;
0577 goto err_unlock;
0578 }
0579
0580 spin_lock_irqsave(&gpio_lock, flags);
0581 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
0582 test_bit(FLAG_EXPORT, &desc->flags)) {
0583 spin_unlock_irqrestore(&gpio_lock, flags);
0584 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
0585 __func__,
0586 test_bit(FLAG_REQUESTED, &desc->flags),
0587 test_bit(FLAG_EXPORT, &desc->flags));
0588 status = -EPERM;
0589 goto err_unlock;
0590 }
0591 spin_unlock_irqrestore(&gpio_lock, flags);
0592
0593 data = kzalloc(sizeof(*data), GFP_KERNEL);
0594 if (!data) {
0595 status = -ENOMEM;
0596 goto err_unlock;
0597 }
0598
0599 data->desc = desc;
0600 mutex_init(&data->mutex);
0601 if (chip->direction_input && chip->direction_output)
0602 data->direction_can_change = direction_may_change;
0603 else
0604 data->direction_can_change = false;
0605
0606 offset = gpio_chip_hwgpio(desc);
0607 if (chip->names && chip->names[offset])
0608 ioname = chip->names[offset];
0609
0610 dev = device_create_with_groups(&gpio_class, &gdev->dev,
0611 MKDEV(0, 0), data, gpio_groups,
0612 ioname ? ioname : "gpio%u",
0613 desc_to_gpio(desc));
0614 if (IS_ERR(dev)) {
0615 status = PTR_ERR(dev);
0616 goto err_free_data;
0617 }
0618
0619 set_bit(FLAG_EXPORT, &desc->flags);
0620 mutex_unlock(&sysfs_lock);
0621 return 0;
0622
0623 err_free_data:
0624 kfree(data);
0625 err_unlock:
0626 mutex_unlock(&sysfs_lock);
0627 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
0628 return status;
0629 }
0630 EXPORT_SYMBOL_GPL(gpiod_export);
0631
0632 static int match_export(struct device *dev, const void *desc)
0633 {
0634 struct gpiod_data *data = dev_get_drvdata(dev);
0635
0636 return data->desc == desc;
0637 }
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650 int gpiod_export_link(struct device *dev, const char *name,
0651 struct gpio_desc *desc)
0652 {
0653 struct device *cdev;
0654 int ret;
0655
0656 if (!desc) {
0657 pr_warn("%s: invalid GPIO\n", __func__);
0658 return -EINVAL;
0659 }
0660
0661 cdev = class_find_device(&gpio_class, NULL, desc, match_export);
0662 if (!cdev)
0663 return -ENODEV;
0664
0665 ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
0666 put_device(cdev);
0667
0668 return ret;
0669 }
0670 EXPORT_SYMBOL_GPL(gpiod_export_link);
0671
0672
0673
0674
0675
0676
0677
0678 void gpiod_unexport(struct gpio_desc *desc)
0679 {
0680 struct gpiod_data *data;
0681 struct device *dev;
0682
0683 if (!desc) {
0684 pr_warn("%s: invalid GPIO\n", __func__);
0685 return;
0686 }
0687
0688 mutex_lock(&sysfs_lock);
0689
0690 if (!test_bit(FLAG_EXPORT, &desc->flags))
0691 goto err_unlock;
0692
0693 dev = class_find_device(&gpio_class, NULL, desc, match_export);
0694 if (!dev)
0695 goto err_unlock;
0696
0697 data = dev_get_drvdata(dev);
0698
0699 clear_bit(FLAG_EXPORT, &desc->flags);
0700
0701 device_unregister(dev);
0702
0703
0704
0705
0706 if (data->irq_flags)
0707 gpio_sysfs_free_irq(dev);
0708
0709 mutex_unlock(&sysfs_lock);
0710
0711 put_device(dev);
0712 kfree(data);
0713
0714 return;
0715
0716 err_unlock:
0717 mutex_unlock(&sysfs_lock);
0718 }
0719 EXPORT_SYMBOL_GPL(gpiod_unexport);
0720
0721 int gpiochip_sysfs_register(struct gpio_device *gdev)
0722 {
0723 struct device *dev;
0724 struct device *parent;
0725 struct gpio_chip *chip = gdev->chip;
0726
0727
0728
0729
0730
0731
0732
0733 if (!gpio_class.p)
0734 return 0;
0735
0736
0737
0738
0739
0740 if (chip->parent)
0741 parent = chip->parent;
0742 else
0743 parent = &gdev->dev;
0744
0745
0746 dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), chip,
0747 gpiochip_groups, GPIOCHIP_NAME "%d",
0748 chip->base);
0749 if (IS_ERR(dev))
0750 return PTR_ERR(dev);
0751
0752 mutex_lock(&sysfs_lock);
0753 gdev->mockdev = dev;
0754 mutex_unlock(&sysfs_lock);
0755
0756 return 0;
0757 }
0758
0759 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
0760 {
0761 struct gpio_desc *desc;
0762 struct gpio_chip *chip = gdev->chip;
0763
0764 if (!gdev->mockdev)
0765 return;
0766
0767 device_unregister(gdev->mockdev);
0768
0769
0770 mutex_lock(&sysfs_lock);
0771 gdev->mockdev = NULL;
0772 mutex_unlock(&sysfs_lock);
0773
0774
0775 for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS)
0776 gpiod_free(desc);
0777 }
0778
0779 static int __init gpiolib_sysfs_init(void)
0780 {
0781 int status;
0782 unsigned long flags;
0783 struct gpio_device *gdev;
0784
0785 status = class_register(&gpio_class);
0786 if (status < 0)
0787 return status;
0788
0789
0790
0791
0792
0793
0794
0795 spin_lock_irqsave(&gpio_lock, flags);
0796 list_for_each_entry(gdev, &gpio_devices, list) {
0797 if (gdev->mockdev)
0798 continue;
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809 spin_unlock_irqrestore(&gpio_lock, flags);
0810 status = gpiochip_sysfs_register(gdev);
0811 spin_lock_irqsave(&gpio_lock, flags);
0812 }
0813 spin_unlock_irqrestore(&gpio_lock, flags);
0814
0815 return status;
0816 }
0817 postcore_initcall(gpiolib_sysfs_init);