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