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