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