1#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/interrupt.h>
4#include <linux/irq.h>
5#include <linux/spinlock.h>
6#include <linux/device.h>
7#include <linux/err.h>
8#include <linux/debugfs.h>
9#include <linux/seq_file.h>
10#include <linux/gpio.h>
11#include <linux/of_gpio.h>
12#include <linux/idr.h>
13#include <linux/slab.h>
14
15#define CREATE_TRACE_POINTS
16#include <trace/events/gpio.h>
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#ifdef DEBUG
38#define extra_checks 1
39#else
40#define extra_checks 0
41#endif
42
43
44
45
46
47static DEFINE_SPINLOCK(gpio_lock);
48
49struct gpio_desc {
50 struct gpio_chip *chip;
51 unsigned long flags;
52
53#define FLAG_REQUESTED 0
54#define FLAG_IS_OUT 1
55#define FLAG_RESERVED 2
56#define FLAG_EXPORT 3
57#define FLAG_SYSFS 4
58#define FLAG_TRIG_FALL 5
59#define FLAG_TRIG_RISE 6
60#define FLAG_ACTIVE_LOW 7
61#define FLAG_OPEN_DRAIN 8
62#define FLAG_OPEN_SOURCE 9
63
64#define ID_SHIFT 16
65
66#define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1)
67#define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE))
68
69#ifdef CONFIG_DEBUG_FS
70 const char *label;
71#endif
72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74
75#ifdef CONFIG_GPIO_SYSFS
76static DEFINE_IDR(dirent_idr);
77#endif
78
79static inline void desc_set_label(struct gpio_desc *d, const char *label)
80{
81#ifdef CONFIG_DEBUG_FS
82 d->label = label;
83#endif
84}
85
86
87
88
89
90
91
92
93
94
95
96
97static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
98{
99 const struct gpio_chip *chip = desc->chip;
100 const int gpio = chip->base + offset;
101
102 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
103 "autorequest GPIO-%d\n", gpio)) {
104 if (!try_module_get(chip->owner)) {
105 pr_err("GPIO-%d: module can't be gotten \n", gpio);
106 clear_bit(FLAG_REQUESTED, &desc->flags);
107
108 return -EIO;
109 }
110 desc_set_label(desc, "[auto]");
111
112 if (chip->request)
113 return 1;
114 }
115 return 0;
116}
117
118
119struct gpio_chip *gpio_to_chip(unsigned gpio)
120{
121 return gpio_desc[gpio].chip;
122}
123
124
125static int gpiochip_find_base(int ngpio)
126{
127 int i;
128 int spare = 0;
129 int base = -ENOSPC;
130
131 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
132 struct gpio_desc *desc = &gpio_desc[i];
133 struct gpio_chip *chip = desc->chip;
134
135 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) {
136 spare++;
137 if (spare == ngpio) {
138 base = i;
139 break;
140 }
141 } else {
142 spare = 0;
143 if (chip)
144 i -= chip->ngpio - 1;
145 }
146 }
147
148 if (gpio_is_valid(base))
149 pr_debug("%s: found new base at %d\n", __func__, base);
150 return base;
151}
152
153
154
155
156
157
158
159
160
161
162
163
164int __init gpiochip_reserve(int start, int ngpio)
165{
166 int ret = 0;
167 unsigned long flags;
168 int i;
169
170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
171 return -EINVAL;
172
173 spin_lock_irqsave(&gpio_lock, flags);
174
175 for (i = start; i < start + ngpio; i++) {
176 struct gpio_desc *desc = &gpio_desc[i];
177
178 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
179 ret = -EBUSY;
180 goto err;
181 }
182
183 set_bit(FLAG_RESERVED, &desc->flags);
184 }
185
186 pr_debug("%s: reserved gpios from %d to %d\n",
187 __func__, start, start + ngpio - 1);
188err:
189 spin_unlock_irqrestore(&gpio_lock, flags);
190
191 return ret;
192}
193
194#ifdef CONFIG_GPIO_SYSFS
195
196
197
198
199static DEFINE_MUTEX(sysfs_lock);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222static ssize_t gpio_direction_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 const struct gpio_desc *desc = dev_get_drvdata(dev);
226 ssize_t status;
227
228 mutex_lock(&sysfs_lock);
229
230 if (!test_bit(FLAG_EXPORT, &desc->flags))
231 status = -EIO;
232 else
233 status = sprintf(buf, "%s\n",
234 test_bit(FLAG_IS_OUT, &desc->flags)
235 ? "out" : "in");
236
237 mutex_unlock(&sysfs_lock);
238 return status;
239}
240
241static ssize_t gpio_direction_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t size)
243{
244 const struct gpio_desc *desc = dev_get_drvdata(dev);
245 unsigned gpio = desc - gpio_desc;
246 ssize_t status;
247
248 mutex_lock(&sysfs_lock);
249
250 if (!test_bit(FLAG_EXPORT, &desc->flags))
251 status = -EIO;
252 else if (sysfs_streq(buf, "high"))
253 status = gpio_direction_output(gpio, 1);
254 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
255 status = gpio_direction_output(gpio, 0);
256 else if (sysfs_streq(buf, "in"))
257 status = gpio_direction_input(gpio);
258 else
259 status = -EINVAL;
260
261 mutex_unlock(&sysfs_lock);
262 return status ? : size;
263}
264
265static DEVICE_ATTR(direction, 0644,
266 gpio_direction_show, gpio_direction_store);
267
268static ssize_t gpio_value_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
271 const struct gpio_desc *desc = dev_get_drvdata(dev);
272 unsigned gpio = desc - gpio_desc;
273 ssize_t status;
274
275 mutex_lock(&sysfs_lock);
276
277 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
278 status = -EIO;
279 } else {
280 int value;
281
282 value = !!gpio_get_value_cansleep(gpio);
283 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
284 value = !value;
285
286 status = sprintf(buf, "%d\n", value);
287 }
288
289 mutex_unlock(&sysfs_lock);
290 return status;
291}
292
293static ssize_t gpio_value_store(struct device *dev,
294 struct device_attribute *attr, const char *buf, size_t size)
295{
296 const struct gpio_desc *desc = dev_get_drvdata(dev);
297 unsigned gpio = desc - gpio_desc;
298 ssize_t status;
299
300 mutex_lock(&sysfs_lock);
301
302 if (!test_bit(FLAG_EXPORT, &desc->flags))
303 status = -EIO;
304 else if (!test_bit(FLAG_IS_OUT, &desc->flags))
305 status = -EPERM;
306 else {
307 long value;
308
309 status = strict_strtol(buf, 0, &value);
310 if (status == 0) {
311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 value = !value;
313 gpio_set_value_cansleep(gpio, value != 0);
314 status = size;
315 }
316 }
317
318 mutex_unlock(&sysfs_lock);
319 return status;
320}
321
322static const DEVICE_ATTR(value, 0644,
323 gpio_value_show, gpio_value_store);
324
325static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
326{
327 struct sysfs_dirent *value_sd = priv;
328
329 sysfs_notify_dirent(value_sd);
330 return IRQ_HANDLED;
331}
332
333static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
334 unsigned long gpio_flags)
335{
336 struct sysfs_dirent *value_sd;
337 unsigned long irq_flags;
338 int ret, irq, id;
339
340 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
341 return 0;
342
343 irq = gpio_to_irq(desc - gpio_desc);
344 if (irq < 0)
345 return -EIO;
346
347 id = desc->flags >> ID_SHIFT;
348 value_sd = idr_find(&dirent_idr, id);
349 if (value_sd)
350 free_irq(irq, value_sd);
351
352 desc->flags &= ~GPIO_TRIGGER_MASK;
353
354 if (!gpio_flags) {
355 ret = 0;
356 goto free_id;
357 }
358
359 irq_flags = IRQF_SHARED;
360 if (test_bit(FLAG_TRIG_FALL, &gpio_flags))
361 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
362 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
363 if (test_bit(FLAG_TRIG_RISE, &gpio_flags))
364 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
365 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
366
367 if (!value_sd) {
368 value_sd = sysfs_get_dirent(dev->kobj.sd, NULL, "value");
369 if (!value_sd) {
370 ret = -ENODEV;
371 goto err_out;
372 }
373
374 do {
375 ret = -ENOMEM;
376 if (idr_pre_get(&dirent_idr, GFP_KERNEL))
377 ret = idr_get_new_above(&dirent_idr, value_sd,
378 1, &id);
379 } while (ret == -EAGAIN);
380
381 if (ret)
382 goto free_sd;
383
384 desc->flags &= GPIO_FLAGS_MASK;
385 desc->flags |= (unsigned long)id << ID_SHIFT;
386
387 if (desc->flags >> ID_SHIFT != id) {
388 ret = -ERANGE;
389 goto free_id;
390 }
391 }
392
393 ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags,
394 "gpiolib", value_sd);
395 if (ret < 0)
396 goto free_id;
397
398 desc->flags |= gpio_flags;
399 return 0;
400
401free_id:
402 idr_remove(&dirent_idr, id);
403 desc->flags &= GPIO_FLAGS_MASK;
404free_sd:
405 if (value_sd)
406 sysfs_put(value_sd);
407err_out:
408 return ret;
409}
410
411static const struct {
412 const char *name;
413 unsigned long flags;
414} trigger_types[] = {
415 { "none", 0 },
416 { "falling", BIT(FLAG_TRIG_FALL) },
417 { "rising", BIT(FLAG_TRIG_RISE) },
418 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) },
419};
420
421static ssize_t gpio_edge_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 const struct gpio_desc *desc = dev_get_drvdata(dev);
425 ssize_t status;
426
427 mutex_lock(&sysfs_lock);
428
429 if (!test_bit(FLAG_EXPORT, &desc->flags))
430 status = -EIO;
431 else {
432 int i;
433
434 status = 0;
435 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
436 if ((desc->flags & GPIO_TRIGGER_MASK)
437 == trigger_types[i].flags) {
438 status = sprintf(buf, "%s\n",
439 trigger_types[i].name);
440 break;
441 }
442 }
443
444 mutex_unlock(&sysfs_lock);
445 return status;
446}
447
448static ssize_t gpio_edge_store(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t size)
450{
451 struct gpio_desc *desc = dev_get_drvdata(dev);
452 ssize_t status;
453 int i;
454
455 for (i = 0; i < ARRAY_SIZE(trigger_types); i++)
456 if (sysfs_streq(trigger_types[i].name, buf))
457 goto found;
458 return -EINVAL;
459
460found:
461 mutex_lock(&sysfs_lock);
462
463 if (!test_bit(FLAG_EXPORT, &desc->flags))
464 status = -EIO;
465 else {
466 status = gpio_setup_irq(desc, dev, trigger_types[i].flags);
467 if (!status)
468 status = size;
469 }
470
471 mutex_unlock(&sysfs_lock);
472
473 return status;
474}
475
476static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store);
477
478static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev,
479 int value)
480{
481 int status = 0;
482
483 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
484 return 0;
485
486 if (value)
487 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
488 else
489 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
490
491
492 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^
493 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) {
494 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK;
495
496 gpio_setup_irq(desc, dev, 0);
497 status = gpio_setup_irq(desc, dev, trigger_flags);
498 }
499
500 return status;
501}
502
503static ssize_t gpio_active_low_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 const struct gpio_desc *desc = dev_get_drvdata(dev);
507 ssize_t status;
508
509 mutex_lock(&sysfs_lock);
510
511 if (!test_bit(FLAG_EXPORT, &desc->flags))
512 status = -EIO;
513 else
514 status = sprintf(buf, "%d\n",
515 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
516
517 mutex_unlock(&sysfs_lock);
518
519 return status;
520}
521
522static ssize_t gpio_active_low_store(struct device *dev,
523 struct device_attribute *attr, const char *buf, size_t size)
524{
525 struct gpio_desc *desc = dev_get_drvdata(dev);
526 ssize_t status;
527
528 mutex_lock(&sysfs_lock);
529
530 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
531 status = -EIO;
532 } else {
533 long value;
534
535 status = strict_strtol(buf, 0, &value);
536 if (status == 0)
537 status = sysfs_set_active_low(desc, dev, value != 0);
538 }
539
540 mutex_unlock(&sysfs_lock);
541
542 return status ? : size;
543}
544
545static const DEVICE_ATTR(active_low, 0644,
546 gpio_active_low_show, gpio_active_low_store);
547
548static const struct attribute *gpio_attrs[] = {
549 &dev_attr_value.attr,
550 &dev_attr_active_low.attr,
551 NULL,
552};
553
554static const struct attribute_group gpio_attr_group = {
555 .attrs = (struct attribute **) gpio_attrs,
556};
557
558
559
560
561
562
563
564
565static ssize_t chip_base_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 const struct gpio_chip *chip = dev_get_drvdata(dev);
569
570 return sprintf(buf, "%d\n", chip->base);
571}
572static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
573
574static ssize_t chip_label_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
577 const struct gpio_chip *chip = dev_get_drvdata(dev);
578
579 return sprintf(buf, "%s\n", chip->label ? : "");
580}
581static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
582
583static ssize_t chip_ngpio_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
586 const struct gpio_chip *chip = dev_get_drvdata(dev);
587
588 return sprintf(buf, "%u\n", chip->ngpio);
589}
590static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
591
592static const struct attribute *gpiochip_attrs[] = {
593 &dev_attr_base.attr,
594 &dev_attr_label.attr,
595 &dev_attr_ngpio.attr,
596 NULL,
597};
598
599static const struct attribute_group gpiochip_attr_group = {
600 .attrs = (struct attribute **) gpiochip_attrs,
601};
602
603
604
605
606
607
608
609static ssize_t export_store(struct class *class,
610 struct class_attribute *attr,
611 const char *buf, size_t len)
612{
613 long gpio;
614 int status;
615
616 status = strict_strtol(buf, 0, &gpio);
617 if (status < 0)
618 goto done;
619
620
621
622
623
624
625 status = gpio_request(gpio, "sysfs");
626 if (status < 0) {
627 if (status == -EPROBE_DEFER)
628 status = -ENODEV;
629 goto done;
630 }
631 status = gpio_export(gpio, true);
632 if (status < 0)
633 gpio_free(gpio);
634 else
635 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
636
637done:
638 if (status)
639 pr_debug("%s: status %d\n", __func__, status);
640 return status ? : len;
641}
642
643static ssize_t unexport_store(struct class *class,
644 struct class_attribute *attr,
645 const char *buf, size_t len)
646{
647 long gpio;
648 int status;
649
650 status = strict_strtol(buf, 0, &gpio);
651 if (status < 0)
652 goto done;
653
654 status = -EINVAL;
655
656
657 if (!gpio_is_valid(gpio))
658 goto done;
659
660
661
662
663
664 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
665 status = 0;
666 gpio_free(gpio);
667 }
668done:
669 if (status)
670 pr_debug("%s: status %d\n", __func__, status);
671 return status ? : len;
672}
673
674static struct class_attribute gpio_class_attrs[] = {
675 __ATTR(export, 0200, NULL, export_store),
676 __ATTR(unexport, 0200, NULL, unexport_store),
677 __ATTR_NULL,
678};
679
680static struct class gpio_class = {
681 .name = "gpio",
682 .owner = THIS_MODULE,
683
684 .class_attrs = gpio_class_attrs,
685};
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703int gpio_export(unsigned gpio, bool direction_may_change)
704{
705 unsigned long flags;
706 struct gpio_desc *desc;
707 int status = -EINVAL;
708 const char *ioname = NULL;
709
710
711 if (!gpio_class.p) {
712 pr_debug("%s: called too early!\n", __func__);
713 return -ENOENT;
714 }
715
716 if (!gpio_is_valid(gpio))
717 goto done;
718
719 mutex_lock(&sysfs_lock);
720
721 spin_lock_irqsave(&gpio_lock, flags);
722 desc = &gpio_desc[gpio];
723 if (test_bit(FLAG_REQUESTED, &desc->flags)
724 && !test_bit(FLAG_EXPORT, &desc->flags)) {
725 status = 0;
726 if (!desc->chip->direction_input
727 || !desc->chip->direction_output)
728 direction_may_change = false;
729 }
730 spin_unlock_irqrestore(&gpio_lock, flags);
731
732 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base])
733 ioname = desc->chip->names[gpio - desc->chip->base];
734
735 if (status == 0) {
736 struct device *dev;
737
738 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
739 desc, ioname ? ioname : "gpio%u", gpio);
740 if (!IS_ERR(dev)) {
741 status = sysfs_create_group(&dev->kobj,
742 &gpio_attr_group);
743
744 if (!status && direction_may_change)
745 status = device_create_file(dev,
746 &dev_attr_direction);
747
748 if (!status && gpio_to_irq(gpio) >= 0
749 && (direction_may_change
750 || !test_bit(FLAG_IS_OUT,
751 &desc->flags)))
752 status = device_create_file(dev,
753 &dev_attr_edge);
754
755 if (status != 0)
756 device_unregister(dev);
757 } else
758 status = PTR_ERR(dev);
759 if (status == 0)
760 set_bit(FLAG_EXPORT, &desc->flags);
761 }
762
763 mutex_unlock(&sysfs_lock);
764
765done:
766 if (status)
767 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
768
769 return status;
770}
771EXPORT_SYMBOL_GPL(gpio_export);
772
773static int match_export(struct device *dev, void *data)
774{
775 return dev_get_drvdata(dev) == data;
776}
777
778
779
780
781
782
783
784
785
786
787
788
789int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
790{
791 struct gpio_desc *desc;
792 int status = -EINVAL;
793
794 if (!gpio_is_valid(gpio))
795 goto done;
796
797 mutex_lock(&sysfs_lock);
798
799 desc = &gpio_desc[gpio];
800
801 if (test_bit(FLAG_EXPORT, &desc->flags)) {
802 struct device *tdev;
803
804 tdev = class_find_device(&gpio_class, NULL, desc, match_export);
805 if (tdev != NULL) {
806 status = sysfs_create_link(&dev->kobj, &tdev->kobj,
807 name);
808 } else {
809 status = -ENODEV;
810 }
811 }
812
813 mutex_unlock(&sysfs_lock);
814
815done:
816 if (status)
817 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
818
819 return status;
820}
821EXPORT_SYMBOL_GPL(gpio_export_link);
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836int gpio_sysfs_set_active_low(unsigned gpio, int value)
837{
838 struct gpio_desc *desc;
839 struct device *dev = NULL;
840 int status = -EINVAL;
841
842 if (!gpio_is_valid(gpio))
843 goto done;
844
845 mutex_lock(&sysfs_lock);
846
847 desc = &gpio_desc[gpio];
848
849 if (test_bit(FLAG_EXPORT, &desc->flags)) {
850 dev = class_find_device(&gpio_class, NULL, desc, match_export);
851 if (dev == NULL) {
852 status = -ENODEV;
853 goto unlock;
854 }
855 }
856
857 status = sysfs_set_active_low(desc, dev, value);
858
859unlock:
860 mutex_unlock(&sysfs_lock);
861
862done:
863 if (status)
864 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
865
866 return status;
867}
868EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
869
870
871
872
873
874
875
876void gpio_unexport(unsigned gpio)
877{
878 struct gpio_desc *desc;
879 int status = 0;
880 struct device *dev = NULL;
881
882 if (!gpio_is_valid(gpio)) {
883 status = -EINVAL;
884 goto done;
885 }
886
887 mutex_lock(&sysfs_lock);
888
889 desc = &gpio_desc[gpio];
890
891 if (test_bit(FLAG_EXPORT, &desc->flags)) {
892
893 dev = class_find_device(&gpio_class, NULL, desc, match_export);
894 if (dev) {
895 gpio_setup_irq(desc, dev, 0);
896 clear_bit(FLAG_EXPORT, &desc->flags);
897 } else
898 status = -ENODEV;
899 }
900
901 mutex_unlock(&sysfs_lock);
902 if (dev) {
903 device_unregister(dev);
904 put_device(dev);
905 }
906done:
907 if (status)
908 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
909}
910EXPORT_SYMBOL_GPL(gpio_unexport);
911
912static int gpiochip_export(struct gpio_chip *chip)
913{
914 int status;
915 struct device *dev;
916
917
918
919
920
921
922 if (!gpio_class.p)
923 return 0;
924
925
926 mutex_lock(&sysfs_lock);
927 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip,
928 "gpiochip%d", chip->base);
929 if (!IS_ERR(dev)) {
930 status = sysfs_create_group(&dev->kobj,
931 &gpiochip_attr_group);
932 } else
933 status = PTR_ERR(dev);
934 chip->exported = (status == 0);
935 mutex_unlock(&sysfs_lock);
936
937 if (status) {
938 unsigned long flags;
939 unsigned gpio;
940
941 spin_lock_irqsave(&gpio_lock, flags);
942 gpio = chip->base;
943 while (gpio_desc[gpio].chip == chip)
944 gpio_desc[gpio++].chip = NULL;
945 spin_unlock_irqrestore(&gpio_lock, flags);
946
947 pr_debug("%s: chip %s status %d\n", __func__,
948 chip->label, status);
949 }
950
951 return status;
952}
953
954static void gpiochip_unexport(struct gpio_chip *chip)
955{
956 int status;
957 struct device *dev;
958
959 mutex_lock(&sysfs_lock);
960 dev = class_find_device(&gpio_class, NULL, chip, match_export);
961 if (dev) {
962 put_device(dev);
963 device_unregister(dev);
964 chip->exported = 0;
965 status = 0;
966 } else
967 status = -ENODEV;
968 mutex_unlock(&sysfs_lock);
969
970 if (status)
971 pr_debug("%s: chip %s status %d\n", __func__,
972 chip->label, status);
973}
974
975static int __init gpiolib_sysfs_init(void)
976{
977 int status;
978 unsigned long flags;
979 unsigned gpio;
980
981 status = class_register(&gpio_class);
982 if (status < 0)
983 return status;
984
985
986
987
988
989
990
991 spin_lock_irqsave(&gpio_lock, flags);
992 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
993 struct gpio_chip *chip;
994
995 chip = gpio_desc[gpio].chip;
996 if (!chip || chip->exported)
997 continue;
998
999 spin_unlock_irqrestore(&gpio_lock, flags);
1000 status = gpiochip_export(chip);
1001 spin_lock_irqsave(&gpio_lock, flags);
1002 }
1003 spin_unlock_irqrestore(&gpio_lock, flags);
1004
1005
1006 return status;
1007}
1008postcore_initcall(gpiolib_sysfs_init);
1009
1010#else
1011static inline int gpiochip_export(struct gpio_chip *chip)
1012{
1013 return 0;
1014}
1015
1016static inline void gpiochip_unexport(struct gpio_chip *chip)
1017{
1018}
1019
1020#endif
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039int gpiochip_add(struct gpio_chip *chip)
1040{
1041 unsigned long flags;
1042 int status = 0;
1043 unsigned id;
1044 int base = chip->base;
1045
1046 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))
1047 && base >= 0) {
1048 status = -EINVAL;
1049 goto fail;
1050 }
1051
1052 spin_lock_irqsave(&gpio_lock, flags);
1053
1054 if (base < 0) {
1055 base = gpiochip_find_base(chip->ngpio);
1056 if (base < 0) {
1057 status = base;
1058 goto unlock;
1059 }
1060 chip->base = base;
1061 }
1062
1063
1064 for (id = base; id < base + chip->ngpio; id++) {
1065 if (gpio_desc[id].chip != NULL) {
1066 status = -EBUSY;
1067 break;
1068 }
1069 }
1070 if (status == 0) {
1071 for (id = base; id < base + chip->ngpio; id++) {
1072 gpio_desc[id].chip = chip;
1073
1074
1075
1076
1077
1078
1079
1080 gpio_desc[id].flags = !chip->direction_input
1081 ? (1 << FLAG_IS_OUT)
1082 : 0;
1083 }
1084 }
1085
1086 of_gpiochip_add(chip);
1087
1088unlock:
1089 spin_unlock_irqrestore(&gpio_lock, flags);
1090
1091 if (status)
1092 goto fail;
1093
1094 status = gpiochip_export(chip);
1095 if (status)
1096 goto fail;
1097
1098 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n",
1099 chip->base, chip->base + chip->ngpio - 1,
1100 chip->label ? : "generic");
1101
1102 return 0;
1103fail:
1104
1105 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n",
1106 chip->base, chip->base + chip->ngpio - 1,
1107 chip->label ? : "generic");
1108 return status;
1109}
1110EXPORT_SYMBOL_GPL(gpiochip_add);
1111
1112
1113
1114
1115
1116
1117
1118int gpiochip_remove(struct gpio_chip *chip)
1119{
1120 unsigned long flags;
1121 int status = 0;
1122 unsigned id;
1123
1124 spin_lock_irqsave(&gpio_lock, flags);
1125
1126 of_gpiochip_remove(chip);
1127
1128 for (id = chip->base; id < chip->base + chip->ngpio; id++) {
1129 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) {
1130 status = -EBUSY;
1131 break;
1132 }
1133 }
1134 if (status == 0) {
1135 for (id = chip->base; id < chip->base + chip->ngpio; id++)
1136 gpio_desc[id].chip = NULL;
1137 }
1138
1139 spin_unlock_irqrestore(&gpio_lock, flags);
1140
1141 if (status == 0)
1142 gpiochip_unexport(chip);
1143
1144 return status;
1145}
1146EXPORT_SYMBOL_GPL(gpiochip_remove);
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159struct gpio_chip *gpiochip_find(void *data,
1160 int (*match)(struct gpio_chip *chip,
1161 void *data))
1162{
1163 struct gpio_chip *chip = NULL;
1164 unsigned long flags;
1165 int i;
1166
1167 spin_lock_irqsave(&gpio_lock, flags);
1168 for (i = 0; i < ARCH_NR_GPIOS; i++) {
1169 if (!gpio_desc[i].chip)
1170 continue;
1171
1172 if (match(gpio_desc[i].chip, data)) {
1173 chip = gpio_desc[i].chip;
1174 break;
1175 }
1176 }
1177 spin_unlock_irqrestore(&gpio_lock, flags);
1178
1179 return chip;
1180}
1181EXPORT_SYMBOL_GPL(gpiochip_find);
1182
1183
1184
1185
1186
1187int gpio_request(unsigned gpio, const char *label)
1188{
1189 struct gpio_desc *desc;
1190 struct gpio_chip *chip;
1191 int status = -EPROBE_DEFER;
1192 unsigned long flags;
1193
1194 spin_lock_irqsave(&gpio_lock, flags);
1195
1196 if (!gpio_is_valid(gpio)) {
1197 status = -EINVAL;
1198 goto done;
1199 }
1200 desc = &gpio_desc[gpio];
1201 chip = desc->chip;
1202 if (chip == NULL)
1203 goto done;
1204
1205 if (!try_module_get(chip->owner))
1206 goto done;
1207
1208
1209
1210
1211
1212 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1213 desc_set_label(desc, label ? : "?");
1214 status = 0;
1215 } else {
1216 status = -EBUSY;
1217 module_put(chip->owner);
1218 goto done;
1219 }
1220
1221 if (chip->request) {
1222
1223 spin_unlock_irqrestore(&gpio_lock, flags);
1224 status = chip->request(chip, gpio - chip->base);
1225 spin_lock_irqsave(&gpio_lock, flags);
1226
1227 if (status < 0) {
1228 desc_set_label(desc, NULL);
1229 module_put(chip->owner);
1230 clear_bit(FLAG_REQUESTED, &desc->flags);
1231 }
1232 }
1233
1234done:
1235 if (status)
1236 pr_debug("gpio_request: gpio-%d (%s) status %d\n",
1237 gpio, label ? : "?", status);
1238 spin_unlock_irqrestore(&gpio_lock, flags);
1239 return status;
1240}
1241EXPORT_SYMBOL_GPL(gpio_request);
1242
1243void gpio_free(unsigned gpio)
1244{
1245 unsigned long flags;
1246 struct gpio_desc *desc;
1247 struct gpio_chip *chip;
1248
1249 might_sleep();
1250
1251 if (!gpio_is_valid(gpio)) {
1252 WARN_ON(extra_checks);
1253 return;
1254 }
1255
1256 gpio_unexport(gpio);
1257
1258 spin_lock_irqsave(&gpio_lock, flags);
1259
1260 desc = &gpio_desc[gpio];
1261 chip = desc->chip;
1262 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1263 if (chip->free) {
1264 spin_unlock_irqrestore(&gpio_lock, flags);
1265 might_sleep_if(chip->can_sleep);
1266 chip->free(chip, gpio - chip->base);
1267 spin_lock_irqsave(&gpio_lock, flags);
1268 }
1269 desc_set_label(desc, NULL);
1270 module_put(desc->chip->owner);
1271 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1272 clear_bit(FLAG_REQUESTED, &desc->flags);
1273 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1274 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1275 } else
1276 WARN_ON(extra_checks);
1277
1278 spin_unlock_irqrestore(&gpio_lock, flags);
1279}
1280EXPORT_SYMBOL_GPL(gpio_free);
1281
1282
1283
1284
1285
1286
1287
1288int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1289{
1290 int err;
1291
1292 err = gpio_request(gpio, label);
1293 if (err)
1294 return err;
1295
1296 if (flags & GPIOF_OPEN_DRAIN)
1297 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1298
1299 if (flags & GPIOF_OPEN_SOURCE)
1300 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1301
1302 if (flags & GPIOF_DIR_IN)
1303 err = gpio_direction_input(gpio);
1304 else
1305 err = gpio_direction_output(gpio,
1306 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1307
1308 if (err)
1309 goto free_gpio;
1310
1311 if (flags & GPIOF_EXPORT) {
1312 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE);
1313 if (err)
1314 goto free_gpio;
1315 }
1316
1317 return 0;
1318
1319 free_gpio:
1320 gpio_free(gpio);
1321 return err;
1322}
1323EXPORT_SYMBOL_GPL(gpio_request_one);
1324
1325
1326
1327
1328
1329
1330int gpio_request_array(const struct gpio *array, size_t num)
1331{
1332 int i, err;
1333
1334 for (i = 0; i < num; i++, array++) {
1335 err = gpio_request_one(array->gpio, array->flags, array->label);
1336 if (err)
1337 goto err_free;
1338 }
1339 return 0;
1340
1341err_free:
1342 while (i--)
1343 gpio_free((--array)->gpio);
1344 return err;
1345}
1346EXPORT_SYMBOL_GPL(gpio_request_array);
1347
1348
1349
1350
1351
1352
1353void gpio_free_array(const struct gpio *array, size_t num)
1354{
1355 while (num--)
1356 gpio_free((array++)->gpio);
1357}
1358EXPORT_SYMBOL_GPL(gpio_free_array);
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1374{
1375 unsigned gpio = chip->base + offset;
1376
1377 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip)
1378 return NULL;
1379 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0)
1380 return NULL;
1381#ifdef CONFIG_DEBUG_FS
1382 return gpio_desc[gpio].label;
1383#else
1384 return "?";
1385#endif
1386}
1387EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399int gpio_direction_input(unsigned gpio)
1400{
1401 unsigned long flags;
1402 struct gpio_chip *chip;
1403 struct gpio_desc *desc = &gpio_desc[gpio];
1404 int status = -EINVAL;
1405
1406 spin_lock_irqsave(&gpio_lock, flags);
1407
1408 if (!gpio_is_valid(gpio))
1409 goto fail;
1410 chip = desc->chip;
1411 if (!chip || !chip->get || !chip->direction_input)
1412 goto fail;
1413 gpio -= chip->base;
1414 if (gpio >= chip->ngpio)
1415 goto fail;
1416 status = gpio_ensure_requested(desc, gpio);
1417 if (status < 0)
1418 goto fail;
1419
1420
1421
1422 spin_unlock_irqrestore(&gpio_lock, flags);
1423
1424 might_sleep_if(chip->can_sleep);
1425
1426 if (status) {
1427 status = chip->request(chip, gpio);
1428 if (status < 0) {
1429 pr_debug("GPIO-%d: chip request fail, %d\n",
1430 chip->base + gpio, status);
1431
1432
1433
1434 goto lose;
1435 }
1436 }
1437
1438 status = chip->direction_input(chip, gpio);
1439 if (status == 0)
1440 clear_bit(FLAG_IS_OUT, &desc->flags);
1441
1442 trace_gpio_direction(chip->base + gpio, 1, status);
1443lose:
1444 return status;
1445fail:
1446 spin_unlock_irqrestore(&gpio_lock, flags);
1447 if (status)
1448 pr_debug("%s: gpio-%d status %d\n",
1449 __func__, gpio, status);
1450 return status;
1451}
1452EXPORT_SYMBOL_GPL(gpio_direction_input);
1453
1454int gpio_direction_output(unsigned gpio, int value)
1455{
1456 unsigned long flags;
1457 struct gpio_chip *chip;
1458 struct gpio_desc *desc = &gpio_desc[gpio];
1459 int status = -EINVAL;
1460
1461
1462 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1463 return gpio_direction_input(gpio);
1464
1465
1466 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1467 return gpio_direction_input(gpio);
1468
1469 spin_lock_irqsave(&gpio_lock, flags);
1470
1471 if (!gpio_is_valid(gpio))
1472 goto fail;
1473 chip = desc->chip;
1474 if (!chip || !chip->set || !chip->direction_output)
1475 goto fail;
1476 gpio -= chip->base;
1477 if (gpio >= chip->ngpio)
1478 goto fail;
1479 status = gpio_ensure_requested(desc, gpio);
1480 if (status < 0)
1481 goto fail;
1482
1483
1484
1485 spin_unlock_irqrestore(&gpio_lock, flags);
1486
1487 might_sleep_if(chip->can_sleep);
1488
1489 if (status) {
1490 status = chip->request(chip, gpio);
1491 if (status < 0) {
1492 pr_debug("GPIO-%d: chip request fail, %d\n",
1493 chip->base + gpio, status);
1494
1495
1496
1497 goto lose;
1498 }
1499 }
1500
1501 status = chip->direction_output(chip, gpio, value);
1502 if (status == 0)
1503 set_bit(FLAG_IS_OUT, &desc->flags);
1504 trace_gpio_value(chip->base + gpio, 0, value);
1505 trace_gpio_direction(chip->base + gpio, 0, status);
1506lose:
1507 return status;
1508fail:
1509 spin_unlock_irqrestore(&gpio_lock, flags);
1510 if (status)
1511 pr_debug("%s: gpio-%d status %d\n",
1512 __func__, gpio, status);
1513 return status;
1514}
1515EXPORT_SYMBOL_GPL(gpio_direction_output);
1516
1517
1518
1519
1520
1521
1522int gpio_set_debounce(unsigned gpio, unsigned debounce)
1523{
1524 unsigned long flags;
1525 struct gpio_chip *chip;
1526 struct gpio_desc *desc = &gpio_desc[gpio];
1527 int status = -EINVAL;
1528
1529 spin_lock_irqsave(&gpio_lock, flags);
1530
1531 if (!gpio_is_valid(gpio))
1532 goto fail;
1533 chip = desc->chip;
1534 if (!chip || !chip->set || !chip->set_debounce)
1535 goto fail;
1536 gpio -= chip->base;
1537 if (gpio >= chip->ngpio)
1538 goto fail;
1539 status = gpio_ensure_requested(desc, gpio);
1540 if (status < 0)
1541 goto fail;
1542
1543
1544
1545 spin_unlock_irqrestore(&gpio_lock, flags);
1546
1547 might_sleep_if(chip->can_sleep);
1548
1549 return chip->set_debounce(chip, gpio, debounce);
1550
1551fail:
1552 spin_unlock_irqrestore(&gpio_lock, flags);
1553 if (status)
1554 pr_debug("%s: gpio-%d status %d\n",
1555 __func__, gpio, status);
1556
1557 return status;
1558}
1559EXPORT_SYMBOL_GPL(gpio_set_debounce);
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592int __gpio_get_value(unsigned gpio)
1593{
1594 struct gpio_chip *chip;
1595 int value;
1596
1597 chip = gpio_to_chip(gpio);
1598
1599 WARN_ON(chip->can_sleep);
1600 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1601 trace_gpio_value(gpio, 1, value);
1602 return value;
1603}
1604EXPORT_SYMBOL_GPL(__gpio_get_value);
1605
1606
1607
1608
1609
1610
1611
1612static void _gpio_set_open_drain_value(unsigned gpio,
1613 struct gpio_chip *chip, int value)
1614{
1615 int err = 0;
1616 if (value) {
1617 err = chip->direction_input(chip, gpio - chip->base);
1618 if (!err)
1619 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1620 } else {
1621 err = chip->direction_output(chip, gpio - chip->base, 0);
1622 if (!err)
1623 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1624 }
1625 trace_gpio_direction(gpio, value, err);
1626 if (err < 0)
1627 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1628 __func__, gpio, err);
1629}
1630
1631
1632
1633
1634
1635
1636
1637static void _gpio_set_open_source_value(unsigned gpio,
1638 struct gpio_chip *chip, int value)
1639{
1640 int err = 0;
1641 if (value) {
1642 err = chip->direction_output(chip, gpio - chip->base, 1);
1643 if (!err)
1644 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1645 } else {
1646 err = chip->direction_input(chip, gpio - chip->base);
1647 if (!err)
1648 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1649 }
1650 trace_gpio_direction(gpio, !value, err);
1651 if (err < 0)
1652 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1653 __func__, gpio, err);
1654}
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666void __gpio_set_value(unsigned gpio, int value)
1667{
1668 struct gpio_chip *chip;
1669
1670 chip = gpio_to_chip(gpio);
1671
1672 WARN_ON(chip->can_sleep);
1673 trace_gpio_value(gpio, 0, value);
1674 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1675 _gpio_set_open_drain_value(gpio, chip, value);
1676 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1677 _gpio_set_open_source_value(gpio, chip, value);
1678 else
1679 chip->set(chip, gpio - chip->base, value);
1680}
1681EXPORT_SYMBOL_GPL(__gpio_set_value);
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691int __gpio_cansleep(unsigned gpio)
1692{
1693 struct gpio_chip *chip;
1694
1695
1696 chip = gpio_to_chip(gpio);
1697
1698 return chip->can_sleep;
1699}
1700EXPORT_SYMBOL_GPL(__gpio_cansleep);
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711int __gpio_to_irq(unsigned gpio)
1712{
1713 struct gpio_chip *chip;
1714
1715 chip = gpio_to_chip(gpio);
1716 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1717}
1718EXPORT_SYMBOL_GPL(__gpio_to_irq);
1719
1720
1721
1722
1723
1724
1725
1726int gpio_get_value_cansleep(unsigned gpio)
1727{
1728 struct gpio_chip *chip;
1729 int value;
1730
1731 might_sleep_if(extra_checks);
1732 chip = gpio_to_chip(gpio);
1733 value = chip->get ? chip->get(chip, gpio - chip->base) : 0;
1734 trace_gpio_value(gpio, 1, value);
1735 return value;
1736}
1737EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1738
1739void gpio_set_value_cansleep(unsigned gpio, int value)
1740{
1741 struct gpio_chip *chip;
1742
1743 might_sleep_if(extra_checks);
1744 chip = gpio_to_chip(gpio);
1745 trace_gpio_value(gpio, 0, value);
1746 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1747 _gpio_set_open_drain_value(gpio, chip, value);
1748 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1749 _gpio_set_open_source_value(gpio, chip, value);
1750 else
1751 chip->set(chip, gpio - chip->base, value);
1752}
1753EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1754
1755
1756#ifdef CONFIG_DEBUG_FS
1757
1758static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1759{
1760 unsigned i;
1761 unsigned gpio = chip->base;
1762 struct gpio_desc *gdesc = &gpio_desc[gpio];
1763 int is_out;
1764
1765 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1766 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1767 continue;
1768
1769 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1770 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1771 gpio, gdesc->label,
1772 is_out ? "out" : "in ",
1773 chip->get
1774 ? (chip->get(chip, i) ? "hi" : "lo")
1775 : "? ");
1776 seq_printf(s, "\n");
1777 }
1778}
1779
1780static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
1781{
1782 struct gpio_chip *chip = NULL;
1783 unsigned int gpio;
1784 void *ret = NULL;
1785 loff_t index = 0;
1786
1787
1788
1789 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
1790 if (gpio_desc[gpio].chip == chip)
1791 continue;
1792
1793 chip = gpio_desc[gpio].chip;
1794 if (!chip)
1795 continue;
1796
1797 if (index++ >= *pos) {
1798 ret = chip;
1799 break;
1800 }
1801 }
1802
1803 s->private = "";
1804
1805 return ret;
1806}
1807
1808static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1809{
1810 struct gpio_chip *chip = v;
1811 unsigned int gpio;
1812 void *ret = NULL;
1813
1814
1815 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) {
1816 chip = gpio_desc[gpio].chip;
1817 if (chip) {
1818 ret = chip;
1819 break;
1820 }
1821 }
1822
1823 s->private = "\n";
1824 ++*pos;
1825
1826 return ret;
1827}
1828
1829static void gpiolib_seq_stop(struct seq_file *s, void *v)
1830{
1831}
1832
1833static int gpiolib_seq_show(struct seq_file *s, void *v)
1834{
1835 struct gpio_chip *chip = v;
1836 struct device *dev;
1837
1838 seq_printf(s, "%sGPIOs %d-%d", (char *)s->private,
1839 chip->base, chip->base + chip->ngpio - 1);
1840 dev = chip->dev;
1841 if (dev)
1842 seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus",
1843 dev_name(dev));
1844 if (chip->label)
1845 seq_printf(s, ", %s", chip->label);
1846 if (chip->can_sleep)
1847 seq_printf(s, ", can sleep");
1848 seq_printf(s, ":\n");
1849
1850 if (chip->dbg_show)
1851 chip->dbg_show(s, chip);
1852 else
1853 gpiolib_dbg_show(s, chip);
1854
1855 return 0;
1856}
1857
1858static const struct seq_operations gpiolib_seq_ops = {
1859 .start = gpiolib_seq_start,
1860 .next = gpiolib_seq_next,
1861 .stop = gpiolib_seq_stop,
1862 .show = gpiolib_seq_show,
1863};
1864
1865static int gpiolib_open(struct inode *inode, struct file *file)
1866{
1867 return seq_open(file, &gpiolib_seq_ops);
1868}
1869
1870static const struct file_operations gpiolib_operations = {
1871 .owner = THIS_MODULE,
1872 .open = gpiolib_open,
1873 .read = seq_read,
1874 .llseek = seq_lseek,
1875 .release = seq_release,
1876};
1877
1878static int __init gpiolib_debugfs_init(void)
1879{
1880
1881 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
1882 NULL, NULL, &gpiolib_operations);
1883 return 0;
1884}
1885subsys_initcall(gpiolib_debugfs_init);
1886
1887#endif
1888