gpio: sysfs: rename gpiochip registration functions
[linux-2.6/btrfs-unstable.git] / drivers / gpio / gpiolib-sysfs.c
blobaeb73ef2955e05af419f340fe7415a1f7d6437ed
1 #include <linux/idr.h>
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>
10 #include "gpiolib.h"
12 static DEFINE_IDR(dirent_idr);
15 /* lock protects against unexport_gpio() being called while
16 * sysfs files are active.
18 static DEFINE_MUTEX(sysfs_lock);
21 * /sys/class/gpio/gpioN... only for GPIOs that are exported
22 * /direction
23 * * MAY BE OMITTED if kernel won't allow direction changes
24 * * is read/write as "in" or "out"
25 * * may also be written as "high" or "low", initializing
26 * output value as specified ("out" implies "low")
27 * /value
28 * * always readable, subject to hardware behavior
29 * * may be writable, as zero/nonzero
30 * /edge
31 * * configures behavior of poll(2) on /value
32 * * available only if pin can generate IRQs on input
33 * * is read/write as "none", "falling", "rising", or "both"
34 * /active_low
35 * * configures polarity of /value
36 * * is read/write as zero/nonzero
37 * * also affects existing and subsequent "falling" and "rising"
38 * /edge configuration
41 static ssize_t gpio_direction_show(struct device *dev,
42 struct device_attribute *attr, char *buf)
44 struct gpio_desc *desc = dev_get_drvdata(dev);
45 ssize_t status;
47 mutex_lock(&sysfs_lock);
49 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
50 status = -EIO;
51 } else {
52 gpiod_get_direction(desc);
53 status = sprintf(buf, "%s\n",
54 test_bit(FLAG_IS_OUT, &desc->flags)
55 ? "out" : "in");
58 mutex_unlock(&sysfs_lock);
59 return status;
62 static ssize_t gpio_direction_store(struct device *dev,
63 struct device_attribute *attr, const char *buf, size_t size)
65 struct gpio_desc *desc = dev_get_drvdata(dev);
66 ssize_t status;
68 mutex_lock(&sysfs_lock);
70 if (!test_bit(FLAG_EXPORT, &desc->flags))
71 status = -EIO;
72 else 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);
78 else
79 status = -EINVAL;
81 mutex_unlock(&sysfs_lock);
82 return status ? : size;
85 static /* const */ DEVICE_ATTR(direction, 0644,
86 gpio_direction_show, gpio_direction_store);
88 static ssize_t gpio_value_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
91 struct gpio_desc *desc = dev_get_drvdata(dev);
92 ssize_t status;
94 mutex_lock(&sysfs_lock);
96 if (!test_bit(FLAG_EXPORT, &desc->flags))
97 status = -EIO;
98 else
99 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
101 mutex_unlock(&sysfs_lock);
102 return status;
105 static ssize_t gpio_value_store(struct device *dev,
106 struct device_attribute *attr, const char *buf, size_t size)
108 struct gpio_desc *desc = dev_get_drvdata(dev);
109 ssize_t status;
111 mutex_lock(&sysfs_lock);
113 if (!test_bit(FLAG_EXPORT, &desc->flags))
114 status = -EIO;
115 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
116 status = -EPERM;
117 else {
118 long value;
120 status = kstrtol(buf, 0, &value);
121 if (status == 0) {
122 gpiod_set_value_cansleep(desc, value);
123 status = size;
127 mutex_unlock(&sysfs_lock);
128 return status;
131 static DEVICE_ATTR(value, 0644,
132 gpio_value_show, gpio_value_store);
134 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
136 struct kernfs_node *value_sd = priv;
138 sysfs_notify_dirent(value_sd);
139 return IRQ_HANDLED;
142 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
143 unsigned long gpio_flags)
145 struct kernfs_node *value_sd;
146 unsigned long irq_flags;
147 int ret, irq, id;
149 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
150 return 0;
152 irq = gpiod_to_irq(desc);
153 if (irq < 0)
154 return -EIO;
156 id = desc->flags >> ID_SHIFT;
157 value_sd = idr_find(&dirent_idr, id);
158 if (value_sd)
159 free_irq(irq, value_sd);
161 desc->flags &= ~GPIO_TRIGGER_MASK;
163 if (!gpio_flags) {
164 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
165 ret = 0;
166 goto free_id;
169 irq_flags = IRQF_SHARED;
170 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
171 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
172 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
173 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
174 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
175 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
177 if (!value_sd) {
178 value_sd = sysfs_get_dirent(dev->kobj.sd, "value");
179 if (!value_sd) {
180 ret = -ENODEV;
181 goto err_out;
184 ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL);
185 if (ret < 0)
186 goto free_sd;
187 id = ret;
189 desc->flags &= GPIO_FLAGS_MASK;
190 desc->flags |= (unsigned long)id << ID_SHIFT;
192 if (desc->flags >> ID_SHIFT != id) {
193 ret = -ERANGE;
194 goto free_id;
199 * FIXME: This should be done in the irq_request_resources callback
200 * when the irq is requested, but a few drivers currently fail
201 * to do so.
203 * Remove this redundant call (along with the corresponding
204 * unlock) when those drivers have been fixed.
206 ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
207 if (ret < 0)
208 goto free_id;
210 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
211 "gpiolib", value_sd);
212 if (ret < 0)
213 goto err_unlock;
215 desc->flags |= gpio_flags;
216 return 0;
218 err_unlock:
219 gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
220 free_id:
221 idr_remove(&dirent_idr, id);
222 desc->flags &= GPIO_FLAGS_MASK;
223 free_sd:
224 if (value_sd)
225 sysfs_put(value_sd);
226 err_out:
227 return ret;
230 static const struct {
231 const char *name;
232 unsigned long flags;
233 } trigger_types[] = {
234 { "none", 0 },
235 { "falling", BIT(FLAG_TRIG_FALL) },
236 { "rising", BIT(FLAG_TRIG_RISE) },
237 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
240 static ssize_t gpio_edge_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
243 const struct gpio_desc *desc = dev_get_drvdata(dev);
244 ssize_t status;
246 mutex_lock(&sysfs_lock);
248 if (!test_bit(FLAG_EXPORT, &desc->flags))
249 status = -EIO;
250 else {
251 int i;
253 status = 0;
254 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
255 if ((desc->flags & GPIO_TRIGGER_MASK)
256 == trigger_types[i].flags) {
257 status = sprintf(buf, "%s\n",
258 trigger_types[i].name);
259 break;
263 mutex_unlock(&sysfs_lock);
264 return status;
267 static ssize_t gpio_edge_store(struct device *dev,
268 struct device_attribute *attr, const char *buf, size_t size)
270 struct gpio_desc *desc = dev_get_drvdata(dev);
271 ssize_t status;
272 int i;
274 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
275 if (sysfs_streq(trigger_types[i].name, buf))
276 goto found;
277 return -EINVAL;
279 found:
280 mutex_lock(&sysfs_lock);
282 if (!test_bit(FLAG_EXPORT, &desc->flags))
283 status = -EIO;
284 else {
285 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
286 if (!status)
287 status = size;
290 mutex_unlock(&sysfs_lock);
292 return status;
295 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
297 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
298 int value)
300 int status = 0;
302 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
303 return 0;
305 if (value)
306 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
307 else
308 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
310 /* reconfigure poll(2) support if enabled on one edge only */
311 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
312 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
313 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
315 gpio_setup_irq(desc, dev, 0);
316 status = gpio_setup_irq(desc, dev, trigger_flags);
319 return status;
322 static ssize_t gpio_active_low_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 const struct gpio_desc *desc = dev_get_drvdata(dev);
326 ssize_t status;
328 mutex_lock(&sysfs_lock);
330 if (!test_bit(FLAG_EXPORT, &desc->flags))
331 status = -EIO;
332 else
333 status = sprintf(buf, "%d\n",
334 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
336 mutex_unlock(&sysfs_lock);
338 return status;
341 static ssize_t gpio_active_low_store(struct device *dev,
342 struct device_attribute *attr, const char *buf, size_t size)
344 struct gpio_desc *desc = dev_get_drvdata(dev);
345 ssize_t status;
347 mutex_lock(&sysfs_lock);
349 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
350 status = -EIO;
351 } else {
352 long value;
354 status = kstrtol(buf, 0, &value);
355 if (status == 0)
356 status = sysfs_set_active_low(desc, dev, value != 0);
359 mutex_unlock(&sysfs_lock);
361 return status ? : size;
364 static DEVICE_ATTR(active_low, 0644,
365 gpio_active_low_show, gpio_active_low_store);
367 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
368 int n)
370 struct device *dev = container_of(kobj, struct device, kobj);
371 struct gpio_desc *desc = dev_get_drvdata(dev);
372 umode_t mode = attr->mode;
373 bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
375 if (attr == &dev_attr_direction.attr) {
376 if (!show_direction)
377 mode = 0;
378 } else if (attr == &dev_attr_edge.attr) {
379 if (gpiod_to_irq(desc) < 0)
380 mode = 0;
381 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
382 mode = 0;
385 return mode;
388 static struct attribute *gpio_attrs[] = {
389 &dev_attr_direction.attr,
390 &dev_attr_edge.attr,
391 &dev_attr_value.attr,
392 &dev_attr_active_low.attr,
393 NULL,
396 static const struct attribute_group gpio_group = {
397 .attrs = gpio_attrs,
398 .is_visible = gpio_is_visible,
401 static const struct attribute_group *gpio_groups[] = {
402 &gpio_group,
403 NULL
407 * /sys/class/gpio/gpiochipN/
408 * /base ... matching gpio_chip.base (N)
409 * /label ... matching gpio_chip.label
410 * /ngpio ... matching gpio_chip.ngpio
413 static ssize_t chip_base_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 const struct gpio_chip *chip = dev_get_drvdata(dev);
418 return sprintf(buf, "%d\n", chip->base);
420 static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
422 static ssize_t chip_label_show(struct device *dev,
423 struct device_attribute *attr, char *buf)
425 const struct gpio_chip *chip = dev_get_drvdata(dev);
427 return sprintf(buf, "%s\n", chip->label ? : "");
429 static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
431 static ssize_t chip_ngpio_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
434 const struct gpio_chip *chip = dev_get_drvdata(dev);
436 return sprintf(buf, "%u\n", chip->ngpio);
438 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
440 static struct attribute *gpiochip_attrs[] = {
441 &dev_attr_base.attr,
442 &dev_attr_label.attr,
443 &dev_attr_ngpio.attr,
444 NULL,
446 ATTRIBUTE_GROUPS(gpiochip);
449 * /sys/class/gpio/export ... write-only
450 * integer N ... number of GPIO to export (full access)
451 * /sys/class/gpio/unexport ... write-only
452 * integer N ... number of GPIO to unexport
454 static ssize_t export_store(struct class *class,
455 struct class_attribute *attr,
456 const char *buf, size_t len)
458 long gpio;
459 struct gpio_desc *desc;
460 int status;
462 status = kstrtol(buf, 0, &gpio);
463 if (status < 0)
464 goto done;
466 desc = gpio_to_desc(gpio);
467 /* reject invalid GPIOs */
468 if (!desc) {
469 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
470 return -EINVAL;
473 /* No extra locking here; FLAG_SYSFS just signifies that the
474 * request and export were done by on behalf of userspace, so
475 * they may be undone on its behalf too.
478 status = gpiod_request(desc, "sysfs");
479 if (status < 0) {
480 if (status == -EPROBE_DEFER)
481 status = -ENODEV;
482 goto done;
484 status = gpiod_export(desc, true);
485 if (status < 0)
486 gpiod_free(desc);
487 else
488 set_bit(FLAG_SYSFS, &desc->flags);
490 done:
491 if (status)
492 pr_debug("%s: status %d\n", __func__, status);
493 return status ? : len;
496 static ssize_t unexport_store(struct class *class,
497 struct class_attribute *attr,
498 const char *buf, size_t len)
500 long gpio;
501 struct gpio_desc *desc;
502 int status;
504 status = kstrtol(buf, 0, &gpio);
505 if (status < 0)
506 goto done;
508 desc = gpio_to_desc(gpio);
509 /* reject bogus commands (gpio_unexport ignores them) */
510 if (!desc) {
511 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
512 return -EINVAL;
515 status = -EINVAL;
517 /* No extra locking here; FLAG_SYSFS just signifies that the
518 * request and export were done by on behalf of userspace, so
519 * they may be undone on its behalf too.
521 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
522 status = 0;
523 gpiod_free(desc);
525 done:
526 if (status)
527 pr_debug("%s: status %d\n", __func__, status);
528 return status ? : len;
531 static struct class_attribute gpio_class_attrs[] = {
532 __ATTR(export, 0200, NULL, export_store),
533 __ATTR(unexport, 0200, NULL, unexport_store),
534 __ATTR_NULL,
537 static struct class gpio_class = {
538 .name = "gpio",
539 .owner = THIS_MODULE,
541 .class_attrs = gpio_class_attrs,
546 * gpiod_export - export a GPIO through sysfs
547 * @gpio: gpio to make available, already requested
548 * @direction_may_change: true if userspace may change gpio direction
549 * Context: arch_initcall or later
551 * When drivers want to make a GPIO accessible to userspace after they
552 * have requested it -- perhaps while debugging, or as part of their
553 * public interface -- they may use this routine. If the GPIO can
554 * change direction (some can't) and the caller allows it, userspace
555 * will see "direction" sysfs attribute which may be used to change
556 * the gpio's direction. A "value" attribute will always be provided.
558 * Returns zero on success, else an error.
560 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
562 struct gpio_chip *chip;
563 unsigned long flags;
564 int status;
565 const char *ioname = NULL;
566 struct device *dev;
567 int offset;
569 /* can't export until sysfs is available ... */
570 if (!gpio_class.p) {
571 pr_debug("%s: called too early!\n", __func__);
572 return -ENOENT;
575 if (!desc) {
576 pr_debug("%s: invalid gpio descriptor\n", __func__);
577 return -EINVAL;
580 chip = desc->chip;
582 mutex_lock(&sysfs_lock);
584 /* check if chip is being removed */
585 if (!chip || !chip->cdev) {
586 status = -ENODEV;
587 goto fail_unlock;
590 spin_lock_irqsave(&gpio_lock, flags);
591 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
592 test_bit(FLAG_EXPORT, &desc->flags)) {
593 spin_unlock_irqrestore(&gpio_lock, flags);
594 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
595 __func__,
596 test_bit(FLAG_REQUESTED, &desc->flags),
597 test_bit(FLAG_EXPORT, &desc->flags));
598 status = -EPERM;
599 goto fail_unlock;
602 if (chip->direction_input && chip->direction_output &&
603 direction_may_change) {
604 set_bit(FLAG_SYSFS_DIR, &desc->flags);
607 spin_unlock_irqrestore(&gpio_lock, flags);
609 offset = gpio_chip_hwgpio(desc);
610 if (chip->names && chip->names[offset])
611 ioname = chip->names[offset];
613 dev = device_create_with_groups(&gpio_class, chip->dev,
614 MKDEV(0, 0), desc, gpio_groups,
615 ioname ? ioname : "gpio%u",
616 desc_to_gpio(desc));
617 if (IS_ERR(dev)) {
618 status = PTR_ERR(dev);
619 goto fail_unlock;
622 set_bit(FLAG_EXPORT, &desc->flags);
623 mutex_unlock(&sysfs_lock);
624 return 0;
626 fail_unlock:
627 mutex_unlock(&sysfs_lock);
628 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
629 return status;
631 EXPORT_SYMBOL_GPL(gpiod_export);
633 static int match_export(struct device *dev, const void *data)
635 return dev_get_drvdata(dev) == data;
639 * gpiod_export_link - create a sysfs link to an exported GPIO node
640 * @dev: device under which to create symlink
641 * @name: name of the symlink
642 * @gpio: gpio to create symlink to, already exported
644 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
645 * node. Caller is responsible for unlinking.
647 * Returns zero on success, else an error.
649 int gpiod_export_link(struct device *dev, const char *name,
650 struct gpio_desc *desc)
652 int status = -EINVAL;
654 if (!desc) {
655 pr_warn("%s: invalid GPIO\n", __func__);
656 return -EINVAL;
659 mutex_lock(&sysfs_lock);
661 if (test_bit(FLAG_EXPORT, &desc->flags)) {
662 struct device *tdev;
664 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
665 if (tdev != NULL) {
666 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
667 name);
668 put_device(tdev);
669 } else {
670 status = -ENODEV;
674 mutex_unlock(&sysfs_lock);
676 if (status)
677 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
679 return status;
681 EXPORT_SYMBOL_GPL(gpiod_export_link);
684 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
685 * @gpio: gpio to change
686 * @value: non-zero to use active low, i.e. inverted values
688 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute.
689 * The GPIO does not have to be exported yet. If poll(2) support has
690 * been enabled for either rising or falling edge, it will be
691 * reconfigured to follow the new polarity.
693 * Returns zero on success, else an error.
695 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
697 struct device *dev = NULL;
698 int status = -EINVAL;
700 if (!desc) {
701 pr_warn("%s: invalid GPIO\n", __func__);
702 return -EINVAL;
705 mutex_lock(&sysfs_lock);
707 if (test_bit(FLAG_EXPORT, &desc->flags)) {
708 dev = class_find_device(&gpio_class, NULL, desc, match_export);
709 if (dev == NULL) {
710 status = -ENODEV;
711 goto unlock;
715 status = sysfs_set_active_low(desc, dev, value);
716 put_device(dev);
717 unlock:
718 mutex_unlock(&sysfs_lock);
720 if (status)
721 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
723 return status;
725 EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
728 * gpiod_unexport - reverse effect of gpio_export()
729 * @gpio: gpio to make unavailable
731 * This is implicit on gpio_free().
733 void gpiod_unexport(struct gpio_desc *desc)
735 int status = 0;
736 struct device *dev = NULL;
738 if (!desc) {
739 pr_warn("%s: invalid GPIO\n", __func__);
740 return;
743 mutex_lock(&sysfs_lock);
745 if (test_bit(FLAG_EXPORT, &desc->flags)) {
747 dev = class_find_device(&gpio_class, NULL, desc, match_export);
748 if (dev) {
749 gpio_setup_irq(desc, dev, 0);
750 clear_bit(FLAG_SYSFS_DIR, &desc->flags);
751 clear_bit(FLAG_EXPORT, &desc->flags);
752 } else
753 status = -ENODEV;
756 mutex_unlock(&sysfs_lock);
758 if (dev) {
759 device_unregister(dev);
760 put_device(dev);
763 if (status)
764 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
766 EXPORT_SYMBOL_GPL(gpiod_unexport);
768 int gpiochip_sysfs_register(struct gpio_chip *chip)
770 struct device *dev;
773 * Many systems add gpio chips for SOC support very early,
774 * before driver model support is available. In those cases we
775 * register later, in gpiolib_sysfs_init() ... here we just
776 * verify that _some_ field of gpio_class got initialized.
778 if (!gpio_class.p)
779 return 0;
781 /* use chip->base for the ID; it's already known to be unique */
782 dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
783 chip, gpiochip_groups,
784 "gpiochip%d", chip->base);
785 if (IS_ERR(dev))
786 return PTR_ERR(dev);
788 mutex_lock(&sysfs_lock);
789 chip->cdev = dev;
790 mutex_unlock(&sysfs_lock);
792 return 0;
795 void gpiochip_sysfs_unregister(struct gpio_chip *chip)
797 struct gpio_desc *desc;
798 unsigned int i;
800 if (!chip->cdev)
801 return;
803 device_unregister(chip->cdev);
805 /* prevent further gpiod exports */
806 mutex_lock(&sysfs_lock);
807 chip->cdev = NULL;
808 mutex_unlock(&sysfs_lock);
810 /* unregister gpiod class devices owned by sysfs */
811 for (i = 0; i < chip->ngpio; i++) {
812 desc = &chip->desc[i];
813 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
814 gpiod_free(desc);
818 static int __init gpiolib_sysfs_init(void)
820 int status;
821 unsigned long flags;
822 struct gpio_chip *chip;
824 status = class_register(&gpio_class);
825 if (status < 0)
826 return status;
828 /* Scan and register the gpio_chips which registered very
829 * early (e.g. before the class_register above was called).
831 * We run before arch_initcall() so chip->dev nodes can have
832 * registered, and so arch_initcall() can always gpio_export().
834 spin_lock_irqsave(&gpio_lock, flags);
835 list_for_each_entry(chip, &gpio_chips, list) {
836 if (chip->cdev)
837 continue;
840 * TODO we yield gpio_lock here because
841 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
842 * and needs to be fixed.
844 * Also it would be nice to use gpiochip_find() here so we
845 * can keep gpio_chips local to gpiolib.c, but the yield of
846 * gpio_lock prevents us from doing this.
848 spin_unlock_irqrestore(&gpio_lock, flags);
849 status = gpiochip_sysfs_register(chip);
850 spin_lock_irqsave(&gpio_lock, flags);
852 spin_unlock_irqrestore(&gpio_lock, flags);
855 return status;
857 postcore_initcall(gpiolib_sysfs_init);