682e4d34999c4e5f349322f5b237c328e5233080
[cascardo/linux.git] / drivers / gpio / gpiolib-sysfs.c
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>
9 #include <linux/slab.h>
10
11 #include "gpiolib.h"
12
13 struct gpiod_data {
14         struct gpio_desc *desc;
15         struct kernfs_node *value_kn;
16         int irq;
17 };
18
19 /* lock protects against unexport_gpio() being called while
20  * sysfs files are active.
21  */
22 static DEFINE_MUTEX(sysfs_lock);
23
24 /*
25  * /sys/class/gpio/gpioN... only for GPIOs that are exported
26  *   /direction
27  *      * MAY BE OMITTED if kernel won't allow direction changes
28  *      * is read/write as "in" or "out"
29  *      * may also be written as "high" or "low", initializing
30  *        output value as specified ("out" implies "low")
31  *   /value
32  *      * always readable, subject to hardware behavior
33  *      * may be writable, as zero/nonzero
34  *   /edge
35  *      * configures behavior of poll(2) on /value
36  *      * available only if pin can generate IRQs on input
37  *      * is read/write as "none", "falling", "rising", or "both"
38  *   /active_low
39  *      * configures polarity of /value
40  *      * is read/write as zero/nonzero
41  *      * also affects existing and subsequent "falling" and "rising"
42  *        /edge configuration
43  */
44
45 static ssize_t direction_show(struct device *dev,
46                 struct device_attribute *attr, char *buf)
47 {
48         struct gpiod_data *data = dev_get_drvdata(dev);
49         struct gpio_desc *desc = data->desc;
50         ssize_t                 status;
51
52         mutex_lock(&sysfs_lock);
53
54         gpiod_get_direction(desc);
55         status = sprintf(buf, "%s\n",
56                         test_bit(FLAG_IS_OUT, &desc->flags)
57                                 ? "out" : "in");
58
59         mutex_unlock(&sysfs_lock);
60         return status;
61 }
62
63 static ssize_t direction_store(struct device *dev,
64                 struct device_attribute *attr, const char *buf, size_t size)
65 {
66         struct gpiod_data *data = dev_get_drvdata(dev);
67         struct gpio_desc *desc = data->desc;
68         ssize_t                 status;
69
70         mutex_lock(&sysfs_lock);
71
72         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;
80
81         mutex_unlock(&sysfs_lock);
82         return status ? : size;
83 }
84 static DEVICE_ATTR_RW(direction);
85
86 static ssize_t value_show(struct device *dev,
87                 struct device_attribute *attr, char *buf)
88 {
89         struct gpiod_data *data = dev_get_drvdata(dev);
90         struct gpio_desc *desc = data->desc;
91         ssize_t                 status;
92
93         mutex_lock(&sysfs_lock);
94
95         status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
96
97         mutex_unlock(&sysfs_lock);
98         return status;
99 }
100
101 static ssize_t value_store(struct device *dev,
102                 struct device_attribute *attr, const char *buf, size_t size)
103 {
104         struct gpiod_data *data = dev_get_drvdata(dev);
105         struct gpio_desc *desc = data->desc;
106         ssize_t                 status;
107
108         mutex_lock(&sysfs_lock);
109
110         if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
111                 status = -EPERM;
112         } else {
113                 long            value;
114
115                 status = kstrtol(buf, 0, &value);
116                 if (status == 0) {
117                         gpiod_set_value_cansleep(desc, value);
118                         status = size;
119                 }
120         }
121
122         mutex_unlock(&sysfs_lock);
123         return status;
124 }
125 static DEVICE_ATTR_RW(value);
126
127 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
128 {
129         struct gpiod_data *data = priv;
130
131         sysfs_notify_dirent(data->value_kn);
132
133         return IRQ_HANDLED;
134 }
135
136 static int gpio_sysfs_request_irq(struct device *dev, unsigned long gpio_flags)
137 {
138         struct gpiod_data       *data = dev_get_drvdata(dev);
139         struct gpio_desc        *desc = data->desc;
140         unsigned long           irq_flags;
141         int                     ret;
142
143         data->irq = gpiod_to_irq(desc);
144         if (data->irq < 0)
145                 return -EIO;
146
147         data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
148         if (!data->value_kn)
149                 return -ENODEV;
150
151         irq_flags = IRQF_SHARED;
152         if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
153                 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
154                         IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
155         if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
156                 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
157                         IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
158
159         /*
160          * FIXME: This should be done in the irq_request_resources callback
161          *        when the irq is requested, but a few drivers currently fail
162          *        to do so.
163          *
164          *        Remove this redundant call (along with the corresponding
165          *        unlock) when those drivers have been fixed.
166          */
167         ret = gpiochip_lock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
168         if (ret < 0)
169                 goto err_put_kn;
170
171         ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
172                                 "gpiolib", data);
173         if (ret < 0)
174                 goto err_unlock;
175
176         desc->flags |= gpio_flags;
177
178         return 0;
179
180 err_unlock:
181         gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
182 err_put_kn:
183         sysfs_put(data->value_kn);
184
185         return ret;
186 }
187
188 static void gpio_sysfs_free_irq(struct device *dev)
189 {
190         struct gpiod_data *data = dev_get_drvdata(dev);
191         struct gpio_desc *desc = data->desc;
192
193         desc->flags &= ~GPIO_TRIGGER_MASK;
194         free_irq(data->irq, data);
195         gpiochip_unlock_as_irq(desc->chip, gpio_chip_hwgpio(desc));
196         sysfs_put(data->value_kn);
197 }
198
199 static const struct {
200         const char *name;
201         unsigned long flags;
202 } trigger_types[] = {
203         { "none",    0 },
204         { "falling", BIT(FLAG_TRIG_FALL) },
205         { "rising",  BIT(FLAG_TRIG_RISE) },
206         { "both",    BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
207 };
208
209 static ssize_t edge_show(struct device *dev,
210                 struct device_attribute *attr, char *buf)
211 {
212         struct gpiod_data *data = dev_get_drvdata(dev);
213         struct gpio_desc *desc = data->desc;
214         unsigned long mask;
215         ssize_t status = 0;
216         int i;
217
218         mutex_lock(&sysfs_lock);
219
220         for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
221                 mask = desc->flags & GPIO_TRIGGER_MASK;
222                 if (mask == trigger_types[i].flags) {
223                         status = sprintf(buf, "%s\n", trigger_types[i].name);
224                         break;
225                 }
226         }
227
228         mutex_unlock(&sysfs_lock);
229         return status;
230 }
231
232 static ssize_t edge_store(struct device *dev,
233                 struct device_attribute *attr, const char *buf, size_t size)
234 {
235         struct gpiod_data *data = dev_get_drvdata(dev);
236         struct gpio_desc *desc = data->desc;
237         unsigned long flags;
238         ssize_t status = size;
239         int i;
240
241         for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
242                 if (sysfs_streq(trigger_types[i].name, buf))
243                         break;
244         }
245
246         if (i == ARRAY_SIZE(trigger_types))
247                 return -EINVAL;
248
249         flags = trigger_types[i].flags;
250
251         mutex_lock(&sysfs_lock);
252
253         if ((desc->flags & GPIO_TRIGGER_MASK) == flags) {
254                 status = size;
255                 goto out_unlock;
256         }
257
258         if (desc->flags & GPIO_TRIGGER_MASK)
259                 gpio_sysfs_free_irq(dev);
260
261         if (flags) {
262                 status = gpio_sysfs_request_irq(dev, flags);
263                 if (!status)
264                         status = size;
265         }
266
267 out_unlock:
268         mutex_unlock(&sysfs_lock);
269
270         return status;
271 }
272 static DEVICE_ATTR_RW(edge);
273
274 static int sysfs_set_active_low(struct device *dev, int value)
275 {
276         struct gpiod_data       *data = dev_get_drvdata(dev);
277         struct gpio_desc        *desc = data->desc;
278         int                     status = 0;
279
280         if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
281                 return 0;
282
283         if (value)
284                 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
285         else
286                 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
287
288         /* reconfigure poll(2) support if enabled on one edge only */
289         if (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
290                                 !!test_bit(FLAG_TRIG_FALL, &desc->flags)) {
291                 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
292
293                 gpio_sysfs_free_irq(dev);
294                 status = gpio_sysfs_request_irq(dev, trigger_flags);
295         }
296
297         return status;
298 }
299
300 static ssize_t active_low_show(struct device *dev,
301                 struct device_attribute *attr, char *buf)
302 {
303         struct gpiod_data *data = dev_get_drvdata(dev);
304         struct gpio_desc *desc = data->desc;
305         ssize_t                 status;
306
307         mutex_lock(&sysfs_lock);
308
309         status = sprintf(buf, "%d\n",
310                                 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
311
312         mutex_unlock(&sysfs_lock);
313
314         return status;
315 }
316
317 static ssize_t active_low_store(struct device *dev,
318                 struct device_attribute *attr, const char *buf, size_t size)
319 {
320         ssize_t                 status;
321         long                    value;
322
323         mutex_lock(&sysfs_lock);
324
325         status = kstrtol(buf, 0, &value);
326         if (status == 0)
327                 status = sysfs_set_active_low(dev, value != 0);
328
329         mutex_unlock(&sysfs_lock);
330
331         return status ? : size;
332 }
333 static DEVICE_ATTR_RW(active_low);
334
335 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
336                                int n)
337 {
338         struct device *dev = container_of(kobj, struct device, kobj);
339         struct gpiod_data *data = dev_get_drvdata(dev);
340         struct gpio_desc *desc = data->desc;
341         umode_t mode = attr->mode;
342         bool show_direction = test_bit(FLAG_SYSFS_DIR, &desc->flags);
343
344         if (attr == &dev_attr_direction.attr) {
345                 if (!show_direction)
346                         mode = 0;
347         } else if (attr == &dev_attr_edge.attr) {
348                 if (gpiod_to_irq(desc) < 0)
349                         mode = 0;
350                 if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
351                         mode = 0;
352         }
353
354         return mode;
355 }
356
357 static struct attribute *gpio_attrs[] = {
358         &dev_attr_direction.attr,
359         &dev_attr_edge.attr,
360         &dev_attr_value.attr,
361         &dev_attr_active_low.attr,
362         NULL,
363 };
364
365 static const struct attribute_group gpio_group = {
366         .attrs = gpio_attrs,
367         .is_visible = gpio_is_visible,
368 };
369
370 static const struct attribute_group *gpio_groups[] = {
371         &gpio_group,
372         NULL
373 };
374
375 /*
376  * /sys/class/gpio/gpiochipN/
377  *   /base ... matching gpio_chip.base (N)
378  *   /label ... matching gpio_chip.label
379  *   /ngpio ... matching gpio_chip.ngpio
380  */
381
382 static ssize_t base_show(struct device *dev,
383                                struct device_attribute *attr, char *buf)
384 {
385         const struct gpio_chip  *chip = dev_get_drvdata(dev);
386
387         return sprintf(buf, "%d\n", chip->base);
388 }
389 static DEVICE_ATTR_RO(base);
390
391 static ssize_t label_show(struct device *dev,
392                                struct device_attribute *attr, char *buf)
393 {
394         const struct gpio_chip  *chip = dev_get_drvdata(dev);
395
396         return sprintf(buf, "%s\n", chip->label ? : "");
397 }
398 static DEVICE_ATTR_RO(label);
399
400 static ssize_t ngpio_show(struct device *dev,
401                                struct device_attribute *attr, char *buf)
402 {
403         const struct gpio_chip  *chip = dev_get_drvdata(dev);
404
405         return sprintf(buf, "%u\n", chip->ngpio);
406 }
407 static DEVICE_ATTR_RO(ngpio);
408
409 static struct attribute *gpiochip_attrs[] = {
410         &dev_attr_base.attr,
411         &dev_attr_label.attr,
412         &dev_attr_ngpio.attr,
413         NULL,
414 };
415 ATTRIBUTE_GROUPS(gpiochip);
416
417 /*
418  * /sys/class/gpio/export ... write-only
419  *      integer N ... number of GPIO to export (full access)
420  * /sys/class/gpio/unexport ... write-only
421  *      integer N ... number of GPIO to unexport
422  */
423 static ssize_t export_store(struct class *class,
424                                 struct class_attribute *attr,
425                                 const char *buf, size_t len)
426 {
427         long                    gpio;
428         struct gpio_desc        *desc;
429         int                     status;
430
431         status = kstrtol(buf, 0, &gpio);
432         if (status < 0)
433                 goto done;
434
435         desc = gpio_to_desc(gpio);
436         /* reject invalid GPIOs */
437         if (!desc) {
438                 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
439                 return -EINVAL;
440         }
441
442         /* No extra locking here; FLAG_SYSFS just signifies that the
443          * request and export were done by on behalf of userspace, so
444          * they may be undone on its behalf too.
445          */
446
447         status = gpiod_request(desc, "sysfs");
448         if (status < 0) {
449                 if (status == -EPROBE_DEFER)
450                         status = -ENODEV;
451                 goto done;
452         }
453         status = gpiod_export(desc, true);
454         if (status < 0)
455                 gpiod_free(desc);
456         else
457                 set_bit(FLAG_SYSFS, &desc->flags);
458
459 done:
460         if (status)
461                 pr_debug("%s: status %d\n", __func__, status);
462         return status ? : len;
463 }
464
465 static ssize_t unexport_store(struct class *class,
466                                 struct class_attribute *attr,
467                                 const char *buf, size_t len)
468 {
469         long                    gpio;
470         struct gpio_desc        *desc;
471         int                     status;
472
473         status = kstrtol(buf, 0, &gpio);
474         if (status < 0)
475                 goto done;
476
477         desc = gpio_to_desc(gpio);
478         /* reject bogus commands (gpio_unexport ignores them) */
479         if (!desc) {
480                 pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
481                 return -EINVAL;
482         }
483
484         status = -EINVAL;
485
486         /* No extra locking here; FLAG_SYSFS just signifies that the
487          * request and export were done by on behalf of userspace, so
488          * they may be undone on its behalf too.
489          */
490         if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
491                 status = 0;
492                 gpiod_free(desc);
493         }
494 done:
495         if (status)
496                 pr_debug("%s: status %d\n", __func__, status);
497         return status ? : len;
498 }
499
500 static struct class_attribute gpio_class_attrs[] = {
501         __ATTR(export, 0200, NULL, export_store),
502         __ATTR(unexport, 0200, NULL, unexport_store),
503         __ATTR_NULL,
504 };
505
506 static struct class gpio_class = {
507         .name =         "gpio",
508         .owner =        THIS_MODULE,
509
510         .class_attrs =  gpio_class_attrs,
511 };
512
513
514 /**
515  * gpiod_export - export a GPIO through sysfs
516  * @gpio: gpio to make available, already requested
517  * @direction_may_change: true if userspace may change gpio direction
518  * Context: arch_initcall or later
519  *
520  * When drivers want to make a GPIO accessible to userspace after they
521  * have requested it -- perhaps while debugging, or as part of their
522  * public interface -- they may use this routine.  If the GPIO can
523  * change direction (some can't) and the caller allows it, userspace
524  * will see "direction" sysfs attribute which may be used to change
525  * the gpio's direction.  A "value" attribute will always be provided.
526  *
527  * Returns zero on success, else an error.
528  */
529 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
530 {
531         struct gpio_chip        *chip;
532         struct gpiod_data       *data;
533         unsigned long           flags;
534         int                     status;
535         const char              *ioname = NULL;
536         struct device           *dev;
537         int                     offset;
538
539         /* can't export until sysfs is available ... */
540         if (!gpio_class.p) {
541                 pr_debug("%s: called too early!\n", __func__);
542                 return -ENOENT;
543         }
544
545         if (!desc) {
546                 pr_debug("%s: invalid gpio descriptor\n", __func__);
547                 return -EINVAL;
548         }
549
550         chip = desc->chip;
551
552         mutex_lock(&sysfs_lock);
553
554         /* check if chip is being removed */
555         if (!chip || !chip->cdev) {
556                 status = -ENODEV;
557                 goto err_unlock;
558         }
559
560         spin_lock_irqsave(&gpio_lock, flags);
561         if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
562              test_bit(FLAG_EXPORT, &desc->flags)) {
563                 spin_unlock_irqrestore(&gpio_lock, flags);
564                 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
565                                 __func__,
566                                 test_bit(FLAG_REQUESTED, &desc->flags),
567                                 test_bit(FLAG_EXPORT, &desc->flags));
568                 status = -EPERM;
569                 goto err_unlock;
570         }
571
572         if (chip->direction_input && chip->direction_output &&
573                         direction_may_change) {
574                 set_bit(FLAG_SYSFS_DIR, &desc->flags);
575         }
576
577         spin_unlock_irqrestore(&gpio_lock, flags);
578
579         data = kzalloc(sizeof(*data), GFP_KERNEL);
580         if (!data) {
581                 status = -ENOMEM;
582                 goto err_unlock;
583         }
584
585         data->desc = desc;
586
587         offset = gpio_chip_hwgpio(desc);
588         if (chip->names && chip->names[offset])
589                 ioname = chip->names[offset];
590
591         dev = device_create_with_groups(&gpio_class, chip->dev,
592                                         MKDEV(0, 0), data, gpio_groups,
593                                         ioname ? ioname : "gpio%u",
594                                         desc_to_gpio(desc));
595         if (IS_ERR(dev)) {
596                 status = PTR_ERR(dev);
597                 goto err_free_data;
598         }
599
600         set_bit(FLAG_EXPORT, &desc->flags);
601         mutex_unlock(&sysfs_lock);
602         return 0;
603
604 err_free_data:
605         kfree(data);
606 err_unlock:
607         mutex_unlock(&sysfs_lock);
608         gpiod_dbg(desc, "%s: status %d\n", __func__, status);
609         return status;
610 }
611 EXPORT_SYMBOL_GPL(gpiod_export);
612
613 static int match_export(struct device *dev, const void *desc)
614 {
615         struct gpiod_data *data = dev_get_drvdata(dev);
616
617         return data->desc == desc;
618 }
619
620 /**
621  * gpiod_export_link - create a sysfs link to an exported GPIO node
622  * @dev: device under which to create symlink
623  * @name: name of the symlink
624  * @gpio: gpio to create symlink to, already exported
625  *
626  * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
627  * node. Caller is responsible for unlinking.
628  *
629  * Returns zero on success, else an error.
630  */
631 int gpiod_export_link(struct device *dev, const char *name,
632                       struct gpio_desc *desc)
633 {
634         struct device *cdev;
635         int ret;
636
637         if (!desc) {
638                 pr_warn("%s: invalid GPIO\n", __func__);
639                 return -EINVAL;
640         }
641
642         cdev = class_find_device(&gpio_class, NULL, desc, match_export);
643         if (!cdev)
644                 return -ENODEV;
645
646         ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
647         put_device(cdev);
648
649         return ret;
650 }
651 EXPORT_SYMBOL_GPL(gpiod_export_link);
652
653 /**
654  * gpiod_unexport - reverse effect of gpio_export()
655  * @gpio: gpio to make unavailable
656  *
657  * This is implicit on gpio_free().
658  */
659 void gpiod_unexport(struct gpio_desc *desc)
660 {
661         struct gpiod_data       *data;
662         int                     status = 0;
663         struct device           *dev = NULL;
664
665         if (!desc) {
666                 pr_warn("%s: invalid GPIO\n", __func__);
667                 return;
668         }
669
670         mutex_lock(&sysfs_lock);
671
672         if (test_bit(FLAG_EXPORT, &desc->flags)) {
673
674                 dev = class_find_device(&gpio_class, NULL, desc, match_export);
675                 if (dev) {
676                         clear_bit(FLAG_SYSFS_DIR, &desc->flags);
677                         clear_bit(FLAG_EXPORT, &desc->flags);
678                 } else
679                         status = -ENODEV;
680         }
681
682         mutex_unlock(&sysfs_lock);
683
684         if (dev) {
685                 data = dev_get_drvdata(dev);
686                 device_unregister(dev);
687                 /*
688                  * Release irq after deregistration to prevent race with
689                  * edge_store.
690                  */
691                 if (desc->flags & GPIO_TRIGGER_MASK)
692                         gpio_sysfs_free_irq(dev);
693                 put_device(dev);
694                 kfree(data);
695         }
696
697         if (status)
698                 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
699 }
700 EXPORT_SYMBOL_GPL(gpiod_unexport);
701
702 int gpiochip_sysfs_register(struct gpio_chip *chip)
703 {
704         struct device   *dev;
705
706         /*
707          * Many systems add gpio chips for SOC support very early,
708          * before driver model support is available.  In those cases we
709          * register later, in gpiolib_sysfs_init() ... here we just
710          * verify that _some_ field of gpio_class got initialized.
711          */
712         if (!gpio_class.p)
713                 return 0;
714
715         /* use chip->base for the ID; it's already known to be unique */
716         dev = device_create_with_groups(&gpio_class, chip->dev, MKDEV(0, 0),
717                                         chip, gpiochip_groups,
718                                         "gpiochip%d", chip->base);
719         if (IS_ERR(dev))
720                 return PTR_ERR(dev);
721
722         mutex_lock(&sysfs_lock);
723         chip->cdev = dev;
724         mutex_unlock(&sysfs_lock);
725
726         return 0;
727 }
728
729 void gpiochip_sysfs_unregister(struct gpio_chip *chip)
730 {
731         struct gpio_desc *desc;
732         unsigned int i;
733
734         if (!chip->cdev)
735                 return;
736
737         device_unregister(chip->cdev);
738
739         /* prevent further gpiod exports */
740         mutex_lock(&sysfs_lock);
741         chip->cdev = NULL;
742         mutex_unlock(&sysfs_lock);
743
744         /* unregister gpiod class devices owned by sysfs */
745         for (i = 0; i < chip->ngpio; i++) {
746                 desc = &chip->desc[i];
747                 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
748                         gpiod_free(desc);
749         }
750 }
751
752 static int __init gpiolib_sysfs_init(void)
753 {
754         int             status;
755         unsigned long   flags;
756         struct gpio_chip *chip;
757
758         status = class_register(&gpio_class);
759         if (status < 0)
760                 return status;
761
762         /* Scan and register the gpio_chips which registered very
763          * early (e.g. before the class_register above was called).
764          *
765          * We run before arch_initcall() so chip->dev nodes can have
766          * registered, and so arch_initcall() can always gpio_export().
767          */
768         spin_lock_irqsave(&gpio_lock, flags);
769         list_for_each_entry(chip, &gpio_chips, list) {
770                 if (chip->cdev)
771                         continue;
772
773                 /*
774                  * TODO we yield gpio_lock here because
775                  * gpiochip_sysfs_register() acquires a mutex. This is unsafe
776                  * and needs to be fixed.
777                  *
778                  * Also it would be nice to use gpiochip_find() here so we
779                  * can keep gpio_chips local to gpiolib.c, but the yield of
780                  * gpio_lock prevents us from doing this.
781                  */
782                 spin_unlock_irqrestore(&gpio_lock, flags);
783                 status = gpiochip_sysfs_register(chip);
784                 spin_lock_irqsave(&gpio_lock, flags);
785         }
786         spin_unlock_irqrestore(&gpio_lock, flags);
787
788
789         return status;
790 }
791 postcore_initcall(gpiolib_sysfs_init);