1#include <linux/bitmap.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/interrupt.h>
5#include <linux/irq.h>
6#include <linux/spinlock.h>
7#include <linux/list.h>
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
13#include <linux/of_gpio.h>
14#include <linux/idr.h>
15#include <linux/slab.h>
16#include <linux/acpi.h>
17#include <linux/gpio/driver.h>
18#include <linux/gpio/machine.h>
19#include <linux/pinctrl/consumer.h>
20#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
23#include <linux/compat.h>
24#include <linux/anon_inodes.h>
25#include <linux/file.h>
26#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
29#include <uapi/linux/gpio.h>
30
31#include "gpiolib.h"
32
33#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
56
57static DEFINE_IDA(gpio_ida);
58static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
63
64
65
66
67
68DEFINE_SPINLOCK(gpio_lock);
69
70static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
72LIST_HEAD(gpio_devices);
73
74static void gpiochip_free_hogs(struct gpio_chip *chip);
75static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
76static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
77static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
78
79static bool gpiolib_initialized;
80
81static inline void desc_set_label(struct gpio_desc *d, const char *label)
82{
83 d->label = label;
84}
85
86
87
88
89
90
91
92
93
94struct gpio_desc *gpio_to_desc(unsigned gpio)
95{
96 struct gpio_device *gdev;
97 unsigned long flags;
98
99 spin_lock_irqsave(&gpio_lock, flags);
100
101 list_for_each_entry(gdev, &gpio_devices, list) {
102 if (gdev->base <= gpio &&
103 gdev->base + gdev->ngpio > gpio) {
104 spin_unlock_irqrestore(&gpio_lock, flags);
105 return &gdev->descs[gpio - gdev->base];
106 }
107 }
108
109 spin_unlock_irqrestore(&gpio_lock, flags);
110
111 if (!gpio_is_valid(gpio))
112 WARN(1, "invalid GPIO %d\n", gpio);
113
114 return NULL;
115}
116EXPORT_SYMBOL_GPL(gpio_to_desc);
117
118
119
120
121
122
123
124
125
126
127
128struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
129 u16 hwnum)
130{
131 struct gpio_device *gdev = chip->gpiodev;
132
133 if (hwnum >= gdev->ngpio)
134 return ERR_PTR(-EINVAL);
135
136 return &gdev->descs[hwnum];
137}
138
139
140
141
142
143
144
145
146
147
148
149int desc_to_gpio(const struct gpio_desc *desc)
150{
151 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
152}
153EXPORT_SYMBOL_GPL(desc_to_gpio);
154
155
156
157
158
159
160struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
161{
162 if (!desc || !desc->gdev || !desc->gdev->chip)
163 return NULL;
164 return desc->gdev->chip;
165}
166EXPORT_SYMBOL_GPL(gpiod_to_chip);
167
168
169static int gpiochip_find_base(int ngpio)
170{
171 struct gpio_device *gdev;
172 int base = ARCH_NR_GPIOS - ngpio;
173
174 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
175
176 if (gdev->base + gdev->ngpio <= base)
177 break;
178 else
179
180 base = gdev->base - ngpio;
181 }
182
183 if (gpio_is_valid(base)) {
184 pr_debug("%s: found new base at %d\n", __func__, base);
185 return base;
186 } else {
187 pr_err("%s: cannot find free range\n", __func__);
188 return -ENOSPC;
189 }
190}
191
192
193
194
195
196
197
198
199
200int gpiod_get_direction(struct gpio_desc *desc)
201{
202 struct gpio_chip *chip;
203 unsigned offset;
204 int status = -EINVAL;
205
206 chip = gpiod_to_chip(desc);
207 offset = gpio_chip_hwgpio(desc);
208
209 if (!chip->get_direction)
210 return status;
211
212 status = chip->get_direction(chip, offset);
213 if (status > 0) {
214
215 status = 1;
216 clear_bit(FLAG_IS_OUT, &desc->flags);
217 }
218 if (status == 0) {
219
220 set_bit(FLAG_IS_OUT, &desc->flags);
221 }
222 return status;
223}
224EXPORT_SYMBOL_GPL(gpiod_get_direction);
225
226
227
228
229
230
231
232
233static int gpiodev_add_to_list(struct gpio_device *gdev)
234{
235 struct gpio_device *prev, *next;
236
237 if (list_empty(&gpio_devices)) {
238
239 list_add_tail(&gdev->list, &gpio_devices);
240 return 0;
241 }
242
243 next = list_entry(gpio_devices.next, struct gpio_device, list);
244 if (gdev->base + gdev->ngpio <= next->base) {
245
246 list_add(&gdev->list, &gpio_devices);
247 return 0;
248 }
249
250 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
251 if (prev->base + prev->ngpio <= gdev->base) {
252
253 list_add_tail(&gdev->list, &gpio_devices);
254 return 0;
255 }
256
257 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
258
259 if (&next->list == &gpio_devices)
260 break;
261
262
263 if (prev->base + prev->ngpio <= gdev->base
264 && gdev->base + gdev->ngpio <= next->base) {
265 list_add(&gdev->list, &prev->list);
266 return 0;
267 }
268 }
269
270 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
271 return -EBUSY;
272}
273
274
275
276
277static struct gpio_desc *gpio_name_to_desc(const char * const name)
278{
279 struct gpio_device *gdev;
280 unsigned long flags;
281
282 spin_lock_irqsave(&gpio_lock, flags);
283
284 list_for_each_entry(gdev, &gpio_devices, list) {
285 int i;
286
287 for (i = 0; i != gdev->ngpio; ++i) {
288 struct gpio_desc *desc = &gdev->descs[i];
289
290 if (!desc->name || !name)
291 continue;
292
293 if (!strcmp(desc->name, name)) {
294 spin_unlock_irqrestore(&gpio_lock, flags);
295 return desc;
296 }
297 }
298 }
299
300 spin_unlock_irqrestore(&gpio_lock, flags);
301
302 return NULL;
303}
304
305
306
307
308
309
310
311static int gpiochip_set_desc_names(struct gpio_chip *gc)
312{
313 struct gpio_device *gdev = gc->gpiodev;
314 int i;
315
316 if (!gc->names)
317 return 0;
318
319
320 for (i = 0; i != gc->ngpio; ++i) {
321 struct gpio_desc *gpio;
322
323 gpio = gpio_name_to_desc(gc->names[i]);
324 if (gpio)
325 dev_warn(&gdev->dev,
326 "Detected name collision for GPIO name '%s'\n",
327 gc->names[i]);
328 }
329
330
331 for (i = 0; i != gc->ngpio; ++i)
332 gdev->descs[i].name = gc->names[i];
333
334 return 0;
335}
336
337
338
339
340
341
342
343
344
345
346
347
348struct linehandle_state {
349 struct gpio_device *gdev;
350 const char *label;
351 struct gpio_desc *descs[GPIOHANDLES_MAX];
352 u32 numdescs;
353};
354
355#define GPIOHANDLE_REQUEST_VALID_FLAGS \
356 (GPIOHANDLE_REQUEST_INPUT | \
357 GPIOHANDLE_REQUEST_OUTPUT | \
358 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
359 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
360 GPIOHANDLE_REQUEST_OPEN_SOURCE)
361
362static long linehandle_ioctl(struct file *filep, unsigned int cmd,
363 unsigned long arg)
364{
365 struct linehandle_state *lh = filep->private_data;
366 void __user *ip = (void __user *)arg;
367 struct gpiohandle_data ghd;
368 int i;
369
370 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
371 int val;
372
373 memset(&ghd, 0, sizeof(ghd));
374
375
376 for (i = 0; i < lh->numdescs; i++) {
377 val = gpiod_get_value_cansleep(lh->descs[i]);
378 if (val < 0)
379 return val;
380 ghd.values[i] = val;
381 }
382
383 if (copy_to_user(ip, &ghd, sizeof(ghd)))
384 return -EFAULT;
385
386 return 0;
387 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
388 int vals[GPIOHANDLES_MAX];
389
390
391 if (copy_from_user(&ghd, ip, sizeof(ghd)))
392 return -EFAULT;
393
394
395 for (i = 0; i < lh->numdescs; i++)
396 vals[i] = !!ghd.values[i];
397
398
399 gpiod_set_array_value_complex(false,
400 true,
401 lh->numdescs,
402 lh->descs,
403 vals);
404 return 0;
405 }
406 return -EINVAL;
407}
408
409#ifdef CONFIG_COMPAT
410static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
411 unsigned long arg)
412{
413 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
414}
415#endif
416
417static int linehandle_release(struct inode *inode, struct file *filep)
418{
419 struct linehandle_state *lh = filep->private_data;
420 struct gpio_device *gdev = lh->gdev;
421 int i;
422
423 for (i = 0; i < lh->numdescs; i++)
424 gpiod_free(lh->descs[i]);
425 kfree(lh->label);
426 kfree(lh);
427 put_device(&gdev->dev);
428 return 0;
429}
430
431static const struct file_operations linehandle_fileops = {
432 .release = linehandle_release,
433 .owner = THIS_MODULE,
434 .llseek = noop_llseek,
435 .unlocked_ioctl = linehandle_ioctl,
436#ifdef CONFIG_COMPAT
437 .compat_ioctl = linehandle_ioctl_compat,
438#endif
439};
440
441static int linehandle_create(struct gpio_device *gdev, void __user *ip)
442{
443 struct gpiohandle_request handlereq;
444 struct linehandle_state *lh;
445 struct file *file;
446 int fd, i, ret;
447
448 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
449 return -EFAULT;
450 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
451 return -EINVAL;
452
453 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
454 if (!lh)
455 return -ENOMEM;
456 lh->gdev = gdev;
457 get_device(&gdev->dev);
458
459
460 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
461 if (strlen(handlereq.consumer_label)) {
462 lh->label = kstrdup(handlereq.consumer_label,
463 GFP_KERNEL);
464 if (!lh->label) {
465 ret = -ENOMEM;
466 goto out_free_lh;
467 }
468 }
469
470
471 for (i = 0; i < handlereq.lines; i++) {
472 u32 offset = handlereq.lineoffsets[i];
473 u32 lflags = handlereq.flags;
474 struct gpio_desc *desc;
475
476 if (offset >= gdev->ngpio) {
477 ret = -EINVAL;
478 goto out_free_descs;
479 }
480
481
482 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
483 ret = -EINVAL;
484 goto out_free_descs;
485 }
486
487 desc = &gdev->descs[offset];
488 ret = gpiod_request(desc, lh->label);
489 if (ret)
490 goto out_free_descs;
491 lh->descs[i] = desc;
492
493 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
494 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
495 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
496 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
497 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
498 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
499
500
501
502
503
504 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
505 int val = !!handlereq.default_values[i];
506
507 ret = gpiod_direction_output(desc, val);
508 if (ret)
509 goto out_free_descs;
510 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
511 ret = gpiod_direction_input(desc);
512 if (ret)
513 goto out_free_descs;
514 }
515 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
516 offset);
517 }
518
519 i--;
520 lh->numdescs = handlereq.lines;
521
522 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
523 if (fd < 0) {
524 ret = fd;
525 goto out_free_descs;
526 }
527
528 file = anon_inode_getfile("gpio-linehandle",
529 &linehandle_fileops,
530 lh,
531 O_RDONLY | O_CLOEXEC);
532 if (IS_ERR(file)) {
533 ret = PTR_ERR(file);
534 goto out_put_unused_fd;
535 }
536
537 handlereq.fd = fd;
538 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
539
540
541
542
543 fput(file);
544 put_unused_fd(fd);
545 return -EFAULT;
546 }
547
548 fd_install(fd, file);
549
550 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
551 lh->numdescs);
552
553 return 0;
554
555out_put_unused_fd:
556 put_unused_fd(fd);
557out_free_descs:
558 for (; i >= 0; i--)
559 gpiod_free(lh->descs[i]);
560 kfree(lh->label);
561out_free_lh:
562 kfree(lh);
563 put_device(&gdev->dev);
564 return ret;
565}
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583struct lineevent_state {
584 struct gpio_device *gdev;
585 const char *label;
586 struct gpio_desc *desc;
587 u32 eflags;
588 int irq;
589 wait_queue_head_t wait;
590 DECLARE_KFIFO(events, struct gpioevent_data, 16);
591 struct mutex read_lock;
592};
593
594#define GPIOEVENT_REQUEST_VALID_FLAGS \
595 (GPIOEVENT_REQUEST_RISING_EDGE | \
596 GPIOEVENT_REQUEST_FALLING_EDGE)
597
598static unsigned int lineevent_poll(struct file *filep,
599 struct poll_table_struct *wait)
600{
601 struct lineevent_state *le = filep->private_data;
602 unsigned int events = 0;
603
604 poll_wait(filep, &le->wait, wait);
605
606 if (!kfifo_is_empty(&le->events))
607 events = POLLIN | POLLRDNORM;
608
609 return events;
610}
611
612
613static ssize_t lineevent_read(struct file *filep,
614 char __user *buf,
615 size_t count,
616 loff_t *f_ps)
617{
618 struct lineevent_state *le = filep->private_data;
619 unsigned int copied;
620 int ret;
621
622 if (count < sizeof(struct gpioevent_data))
623 return -EINVAL;
624
625 do {
626 if (kfifo_is_empty(&le->events)) {
627 if (filep->f_flags & O_NONBLOCK)
628 return -EAGAIN;
629
630 ret = wait_event_interruptible(le->wait,
631 !kfifo_is_empty(&le->events));
632 if (ret)
633 return ret;
634 }
635
636 if (mutex_lock_interruptible(&le->read_lock))
637 return -ERESTARTSYS;
638 ret = kfifo_to_user(&le->events, buf, count, &copied);
639 mutex_unlock(&le->read_lock);
640
641 if (ret)
642 return ret;
643
644
645
646
647
648
649
650 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
651 return -EAGAIN;
652
653 } while (copied == 0);
654
655 return copied;
656}
657
658static int lineevent_release(struct inode *inode, struct file *filep)
659{
660 struct lineevent_state *le = filep->private_data;
661 struct gpio_device *gdev = le->gdev;
662
663 free_irq(le->irq, le);
664 gpiod_free(le->desc);
665 kfree(le->label);
666 kfree(le);
667 put_device(&gdev->dev);
668 return 0;
669}
670
671static long lineevent_ioctl(struct file *filep, unsigned int cmd,
672 unsigned long arg)
673{
674 struct lineevent_state *le = filep->private_data;
675 void __user *ip = (void __user *)arg;
676 struct gpiohandle_data ghd;
677
678
679
680
681
682 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
683 int val;
684
685 memset(&ghd, 0, sizeof(ghd));
686
687 val = gpiod_get_value_cansleep(le->desc);
688 if (val < 0)
689 return val;
690 ghd.values[0] = val;
691
692 if (copy_to_user(ip, &ghd, sizeof(ghd)))
693 return -EFAULT;
694
695 return 0;
696 }
697 return -EINVAL;
698}
699
700#ifdef CONFIG_COMPAT
701static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
702 unsigned long arg)
703{
704 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
705}
706#endif
707
708static const struct file_operations lineevent_fileops = {
709 .release = lineevent_release,
710 .read = lineevent_read,
711 .poll = lineevent_poll,
712 .owner = THIS_MODULE,
713 .llseek = noop_llseek,
714 .unlocked_ioctl = lineevent_ioctl,
715#ifdef CONFIG_COMPAT
716 .compat_ioctl = lineevent_ioctl_compat,
717#endif
718};
719
720static irqreturn_t lineevent_irq_thread(int irq, void *p)
721{
722 struct lineevent_state *le = p;
723 struct gpioevent_data ge;
724 int ret, level;
725
726 ge.timestamp = ktime_get_real_ns();
727 level = gpiod_get_value_cansleep(le->desc);
728
729 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
730 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
731 if (level)
732
733 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
734 else
735
736 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
737 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
738
739 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
740 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
741
742 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
743 } else {
744 return IRQ_NONE;
745 }
746
747 ret = kfifo_put(&le->events, ge);
748 if (ret != 0)
749 wake_up_poll(&le->wait, POLLIN);
750
751 return IRQ_HANDLED;
752}
753
754static int lineevent_create(struct gpio_device *gdev, void __user *ip)
755{
756 struct gpioevent_request eventreq;
757 struct lineevent_state *le;
758 struct gpio_desc *desc;
759 struct file *file;
760 u32 offset;
761 u32 lflags;
762 u32 eflags;
763 int fd;
764 int ret;
765 int irqflags = 0;
766
767 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
768 return -EFAULT;
769
770 le = kzalloc(sizeof(*le), GFP_KERNEL);
771 if (!le)
772 return -ENOMEM;
773 le->gdev = gdev;
774 get_device(&gdev->dev);
775
776
777 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
778 if (strlen(eventreq.consumer_label)) {
779 le->label = kstrdup(eventreq.consumer_label,
780 GFP_KERNEL);
781 if (!le->label) {
782 ret = -ENOMEM;
783 goto out_free_le;
784 }
785 }
786
787 offset = eventreq.lineoffset;
788 lflags = eventreq.handleflags;
789 eflags = eventreq.eventflags;
790
791 if (offset >= gdev->ngpio) {
792 ret = -EINVAL;
793 goto out_free_label;
794 }
795
796
797 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
798 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
799 ret = -EINVAL;
800 goto out_free_label;
801 }
802
803
804 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
805 ret = -EINVAL;
806 goto out_free_label;
807 }
808
809 desc = &gdev->descs[offset];
810 ret = gpiod_request(desc, le->label);
811 if (ret)
812 goto out_free_desc;
813 le->desc = desc;
814 le->eflags = eflags;
815
816 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
817 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
818 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
819 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
820 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
821 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
822
823 ret = gpiod_direction_input(desc);
824 if (ret)
825 goto out_free_desc;
826
827 le->irq = gpiod_to_irq(desc);
828 if (le->irq <= 0) {
829 ret = -ENODEV;
830 goto out_free_desc;
831 }
832
833 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
834 irqflags |= IRQF_TRIGGER_RISING;
835 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
836 irqflags |= IRQF_TRIGGER_FALLING;
837 irqflags |= IRQF_ONESHOT;
838 irqflags |= IRQF_SHARED;
839
840 INIT_KFIFO(le->events);
841 init_waitqueue_head(&le->wait);
842 mutex_init(&le->read_lock);
843
844
845 ret = request_threaded_irq(le->irq,
846 NULL,
847 lineevent_irq_thread,
848 irqflags,
849 le->label,
850 le);
851 if (ret)
852 goto out_free_desc;
853
854 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
855 if (fd < 0) {
856 ret = fd;
857 goto out_free_irq;
858 }
859
860 file = anon_inode_getfile("gpio-event",
861 &lineevent_fileops,
862 le,
863 O_RDONLY | O_CLOEXEC);
864 if (IS_ERR(file)) {
865 ret = PTR_ERR(file);
866 goto out_put_unused_fd;
867 }
868
869 eventreq.fd = fd;
870 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
871
872
873
874
875 fput(file);
876 put_unused_fd(fd);
877 return -EFAULT;
878 }
879
880 fd_install(fd, file);
881
882 return 0;
883
884out_put_unused_fd:
885 put_unused_fd(fd);
886out_free_irq:
887 free_irq(le->irq, le);
888out_free_desc:
889 gpiod_free(le->desc);
890out_free_label:
891 kfree(le->label);
892out_free_le:
893 kfree(le);
894 put_device(&gdev->dev);
895 return ret;
896}
897
898
899
900
901static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
902{
903 struct gpio_device *gdev = filp->private_data;
904 struct gpio_chip *chip = gdev->chip;
905 void __user *ip = (void __user *)arg;
906
907
908 if (!chip)
909 return -ENODEV;
910
911
912 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
913 struct gpiochip_info chipinfo;
914
915 memset(&chipinfo, 0, sizeof(chipinfo));
916
917 strncpy(chipinfo.name, dev_name(&gdev->dev),
918 sizeof(chipinfo.name));
919 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
920 strncpy(chipinfo.label, gdev->label,
921 sizeof(chipinfo.label));
922 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
923 chipinfo.lines = gdev->ngpio;
924 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
925 return -EFAULT;
926 return 0;
927 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
928 struct gpioline_info lineinfo;
929 struct gpio_desc *desc;
930
931 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
932 return -EFAULT;
933 if (lineinfo.line_offset >= gdev->ngpio)
934 return -EINVAL;
935
936 desc = &gdev->descs[lineinfo.line_offset];
937 if (desc->name) {
938 strncpy(lineinfo.name, desc->name,
939 sizeof(lineinfo.name));
940 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
941 } else {
942 lineinfo.name[0] = '\0';
943 }
944 if (desc->label) {
945 strncpy(lineinfo.consumer, desc->label,
946 sizeof(lineinfo.consumer));
947 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
948 } else {
949 lineinfo.consumer[0] = '\0';
950 }
951
952
953
954
955
956 lineinfo.flags = 0;
957 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
958 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
959 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
960 test_bit(FLAG_EXPORT, &desc->flags) ||
961 test_bit(FLAG_SYSFS, &desc->flags))
962 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
963 if (test_bit(FLAG_IS_OUT, &desc->flags))
964 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
965 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
966 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
967 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
968 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
969 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
970 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
971
972 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
973 return -EFAULT;
974 return 0;
975 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
976 return linehandle_create(gdev, ip);
977 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
978 return lineevent_create(gdev, ip);
979 }
980 return -EINVAL;
981}
982
983#ifdef CONFIG_COMPAT
984static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
985 unsigned long arg)
986{
987 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
988}
989#endif
990
991
992
993
994
995
996
997static int gpio_chrdev_open(struct inode *inode, struct file *filp)
998{
999 struct gpio_device *gdev = container_of(inode->i_cdev,
1000 struct gpio_device, chrdev);
1001
1002
1003 if (!gdev->chip)
1004 return -ENODEV;
1005 get_device(&gdev->dev);
1006 filp->private_data = gdev;
1007
1008 return nonseekable_open(inode, filp);
1009}
1010
1011
1012
1013
1014
1015
1016
1017static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1018{
1019 struct gpio_device *gdev = container_of(inode->i_cdev,
1020 struct gpio_device, chrdev);
1021
1022 put_device(&gdev->dev);
1023 return 0;
1024}
1025
1026
1027static const struct file_operations gpio_fileops = {
1028 .release = gpio_chrdev_release,
1029 .open = gpio_chrdev_open,
1030 .owner = THIS_MODULE,
1031 .llseek = no_llseek,
1032 .unlocked_ioctl = gpio_ioctl,
1033#ifdef CONFIG_COMPAT
1034 .compat_ioctl = gpio_ioctl_compat,
1035#endif
1036};
1037
1038static void gpiodevice_release(struct device *dev)
1039{
1040 struct gpio_device *gdev = dev_get_drvdata(dev);
1041
1042 list_del(&gdev->list);
1043 ida_simple_remove(&gpio_ida, gdev->id);
1044 kfree(gdev->label);
1045 kfree(gdev->descs);
1046 kfree(gdev);
1047}
1048
1049static int gpiochip_setup_dev(struct gpio_device *gdev)
1050{
1051 int status;
1052
1053 cdev_init(&gdev->chrdev, &gpio_fileops);
1054 gdev->chrdev.owner = THIS_MODULE;
1055 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1056
1057 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
1058 if (status)
1059 return status;
1060
1061 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1062 MAJOR(gpio_devt), gdev->id);
1063
1064 status = gpiochip_sysfs_register(gdev);
1065 if (status)
1066 goto err_remove_device;
1067
1068
1069 gdev->dev.release = gpiodevice_release;
1070 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1071 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1072 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1073
1074 return 0;
1075
1076err_remove_device:
1077 cdev_device_del(&gdev->chrdev, &gdev->dev);
1078 return status;
1079}
1080
1081static void gpiochip_setup_devs(void)
1082{
1083 struct gpio_device *gdev;
1084 int err;
1085
1086 list_for_each_entry(gdev, &gpio_devices, list) {
1087 err = gpiochip_setup_dev(gdev);
1088 if (err)
1089 pr_err("%s: Failed to initialize gpio device (%d)\n",
1090 dev_name(&gdev->dev), err);
1091 }
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117int gpiochip_add_data(struct gpio_chip *chip, void *data)
1118{
1119 unsigned long flags;
1120 int status = 0;
1121 unsigned i;
1122 int base = chip->base;
1123 struct gpio_device *gdev;
1124
1125
1126
1127
1128
1129 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
1130 if (!gdev)
1131 return -ENOMEM;
1132 gdev->dev.bus = &gpio_bus_type;
1133 gdev->chip = chip;
1134 chip->gpiodev = gdev;
1135 if (chip->parent) {
1136 gdev->dev.parent = chip->parent;
1137 gdev->dev.of_node = chip->parent->of_node;
1138 }
1139
1140#ifdef CONFIG_OF_GPIO
1141
1142 if (chip->of_node)
1143 gdev->dev.of_node = chip->of_node;
1144#endif
1145
1146 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1147 if (gdev->id < 0) {
1148 status = gdev->id;
1149 goto err_free_gdev;
1150 }
1151 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1152 device_initialize(&gdev->dev);
1153 dev_set_drvdata(&gdev->dev, gdev);
1154 if (chip->parent && chip->parent->driver)
1155 gdev->owner = chip->parent->driver->owner;
1156 else if (chip->owner)
1157
1158 gdev->owner = chip->owner;
1159 else
1160 gdev->owner = THIS_MODULE;
1161
1162 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1163 if (!gdev->descs) {
1164 status = -ENOMEM;
1165 goto err_free_gdev;
1166 }
1167
1168 if (chip->ngpio == 0) {
1169 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1170 status = -EINVAL;
1171 goto err_free_descs;
1172 }
1173
1174 if (chip->label)
1175 gdev->label = kstrdup(chip->label, GFP_KERNEL);
1176 else
1177 gdev->label = kstrdup("unknown", GFP_KERNEL);
1178 if (!gdev->label) {
1179 status = -ENOMEM;
1180 goto err_free_descs;
1181 }
1182
1183 gdev->ngpio = chip->ngpio;
1184 gdev->data = data;
1185
1186 spin_lock_irqsave(&gpio_lock, flags);
1187
1188
1189
1190
1191
1192
1193
1194
1195 if (base < 0) {
1196 base = gpiochip_find_base(chip->ngpio);
1197 if (base < 0) {
1198 status = base;
1199 spin_unlock_irqrestore(&gpio_lock, flags);
1200 goto err_free_label;
1201 }
1202
1203
1204
1205
1206
1207
1208 chip->base = base;
1209 }
1210 gdev->base = base;
1211
1212 status = gpiodev_add_to_list(gdev);
1213 if (status) {
1214 spin_unlock_irqrestore(&gpio_lock, flags);
1215 goto err_free_label;
1216 }
1217
1218 spin_unlock_irqrestore(&gpio_lock, flags);
1219
1220 for (i = 0; i < chip->ngpio; i++) {
1221 struct gpio_desc *desc = &gdev->descs[i];
1222
1223 desc->gdev = gdev;
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233 if (chip->get_direction) {
1234
1235
1236
1237
1238 int dir = chip->get_direction(chip, i);
1239
1240 if (!dir)
1241 set_bit(FLAG_IS_OUT, &desc->flags);
1242 } else if (!chip->direction_input) {
1243
1244
1245
1246
1247 set_bit(FLAG_IS_OUT, &desc->flags);
1248 }
1249 }
1250
1251#ifdef CONFIG_PINCTRL
1252 INIT_LIST_HEAD(&gdev->pin_ranges);
1253#endif
1254
1255 status = gpiochip_set_desc_names(chip);
1256 if (status)
1257 goto err_remove_from_list;
1258
1259 status = gpiochip_irqchip_init_valid_mask(chip);
1260 if (status)
1261 goto err_remove_from_list;
1262
1263 status = of_gpiochip_add(chip);
1264 if (status)
1265 goto err_remove_chip;
1266
1267 acpi_gpiochip_add(chip);
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 if (gpiolib_initialized) {
1278 status = gpiochip_setup_dev(gdev);
1279 if (status)
1280 goto err_remove_chip;
1281 }
1282 return 0;
1283
1284err_remove_chip:
1285 acpi_gpiochip_remove(chip);
1286 gpiochip_free_hogs(chip);
1287 of_gpiochip_remove(chip);
1288 gpiochip_irqchip_free_valid_mask(chip);
1289err_remove_from_list:
1290 spin_lock_irqsave(&gpio_lock, flags);
1291 list_del(&gdev->list);
1292 spin_unlock_irqrestore(&gpio_lock, flags);
1293err_free_label:
1294 kfree(gdev->label);
1295err_free_descs:
1296 kfree(gdev->descs);
1297err_free_gdev:
1298 ida_simple_remove(&gpio_ida, gdev->id);
1299
1300 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1301 gdev->base, gdev->base + gdev->ngpio - 1,
1302 chip->label ? : "generic");
1303 kfree(gdev);
1304 return status;
1305}
1306EXPORT_SYMBOL_GPL(gpiochip_add_data);
1307
1308
1309
1310
1311
1312
1313
1314
1315void *gpiochip_get_data(struct gpio_chip *chip)
1316{
1317 return chip->gpiodev->data;
1318}
1319EXPORT_SYMBOL_GPL(gpiochip_get_data);
1320
1321
1322
1323
1324
1325
1326
1327void gpiochip_remove(struct gpio_chip *chip)
1328{
1329 struct gpio_device *gdev = chip->gpiodev;
1330 struct gpio_desc *desc;
1331 unsigned long flags;
1332 unsigned i;
1333 bool requested = false;
1334
1335
1336 gpiochip_sysfs_unregister(gdev);
1337 gpiochip_free_hogs(chip);
1338
1339 gdev->chip = NULL;
1340 gpiochip_irqchip_remove(chip);
1341 acpi_gpiochip_remove(chip);
1342 gpiochip_remove_pin_ranges(chip);
1343 of_gpiochip_remove(chip);
1344
1345
1346
1347
1348 gdev->data = NULL;
1349
1350 spin_lock_irqsave(&gpio_lock, flags);
1351 for (i = 0; i < gdev->ngpio; i++) {
1352 desc = &gdev->descs[i];
1353 if (test_bit(FLAG_REQUESTED, &desc->flags))
1354 requested = true;
1355 }
1356 spin_unlock_irqrestore(&gpio_lock, flags);
1357
1358 if (requested)
1359 dev_crit(&gdev->dev,
1360 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1361
1362
1363
1364
1365
1366
1367
1368 cdev_device_del(&gdev->chrdev, &gdev->dev);
1369 put_device(&gdev->dev);
1370}
1371EXPORT_SYMBOL_GPL(gpiochip_remove);
1372
1373static void devm_gpio_chip_release(struct device *dev, void *res)
1374{
1375 struct gpio_chip *chip = *(struct gpio_chip **)res;
1376
1377 gpiochip_remove(chip);
1378}
1379
1380static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1381
1382{
1383 struct gpio_chip **r = res;
1384
1385 if (!r || !*r) {
1386 WARN_ON(!r || !*r);
1387 return 0;
1388 }
1389
1390 return *r == data;
1391}
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1409 void *data)
1410{
1411 struct gpio_chip **ptr;
1412 int ret;
1413
1414 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1415 GFP_KERNEL);
1416 if (!ptr)
1417 return -ENOMEM;
1418
1419 ret = gpiochip_add_data(chip, data);
1420 if (ret < 0) {
1421 devres_free(ptr);
1422 return ret;
1423 }
1424
1425 *ptr = chip;
1426 devres_add(dev, ptr);
1427
1428 return 0;
1429}
1430EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1431
1432
1433
1434
1435
1436
1437
1438
1439void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1440{
1441 int ret;
1442
1443 ret = devres_release(dev, devm_gpio_chip_release,
1444 devm_gpio_chip_match, chip);
1445 WARN_ON(ret);
1446}
1447EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460struct gpio_chip *gpiochip_find(void *data,
1461 int (*match)(struct gpio_chip *chip,
1462 void *data))
1463{
1464 struct gpio_device *gdev;
1465 struct gpio_chip *chip = NULL;
1466 unsigned long flags;
1467
1468 spin_lock_irqsave(&gpio_lock, flags);
1469 list_for_each_entry(gdev, &gpio_devices, list)
1470 if (gdev->chip && match(gdev->chip, data)) {
1471 chip = gdev->chip;
1472 break;
1473 }
1474
1475 spin_unlock_irqrestore(&gpio_lock, flags);
1476
1477 return chip;
1478}
1479EXPORT_SYMBOL_GPL(gpiochip_find);
1480
1481static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1482{
1483 const char *name = data;
1484
1485 return !strcmp(chip->label, name);
1486}
1487
1488static struct gpio_chip *find_chip_by_name(const char *name)
1489{
1490 return gpiochip_find((void *)name, gpiochip_match_name);
1491}
1492
1493#ifdef CONFIG_GPIOLIB_IRQCHIP
1494
1495
1496
1497
1498
1499static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1500{
1501 if (!gpiochip->irq_need_valid_mask)
1502 return 0;
1503
1504 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1505 sizeof(long), GFP_KERNEL);
1506 if (!gpiochip->irq_valid_mask)
1507 return -ENOMEM;
1508
1509
1510 bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio);
1511
1512 return 0;
1513}
1514
1515static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1516{
1517 kfree(gpiochip->irq_valid_mask);
1518 gpiochip->irq_valid_mask = NULL;
1519}
1520
1521static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1522 unsigned int offset)
1523{
1524
1525 if (likely(!gpiochip->irq_valid_mask))
1526 return true;
1527 return test_bit(offset, gpiochip->irq_valid_mask);
1528}
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1541 struct irq_chip *irqchip,
1542 unsigned int parent_irq,
1543 irq_flow_handler_t parent_handler)
1544{
1545 unsigned int offset;
1546
1547 if (!gpiochip->irqdomain) {
1548 chip_err(gpiochip, "called %s before setting up irqchip\n",
1549 __func__);
1550 return;
1551 }
1552
1553 if (parent_handler) {
1554 if (gpiochip->can_sleep) {
1555 chip_err(gpiochip,
1556 "you cannot have chained interrupts on a "
1557 "chip that may sleep\n");
1558 return;
1559 }
1560
1561
1562
1563
1564 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1565 gpiochip);
1566
1567 gpiochip->irq_chained_parent = parent_irq;
1568 }
1569
1570
1571 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1572 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1573 continue;
1574 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1575 parent_irq);
1576 }
1577}
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1590 struct irq_chip *irqchip,
1591 unsigned int parent_irq,
1592 irq_flow_handler_t parent_handler)
1593{
1594 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1595 parent_handler);
1596}
1597EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1598
1599
1600
1601
1602
1603
1604
1605
1606void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1607 struct irq_chip *irqchip,
1608 unsigned int parent_irq)
1609{
1610 if (!gpiochip->irq_nested) {
1611 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1612 return;
1613 }
1614 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1615 NULL);
1616}
1617EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1630 irq_hw_number_t hwirq)
1631{
1632 struct gpio_chip *chip = d->host_data;
1633
1634 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1635 return -ENXIO;
1636
1637 irq_set_chip_data(irq, chip);
1638
1639
1640
1641
1642 irq_set_lockdep_class(irq, chip->lock_key);
1643 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
1644
1645 if (chip->irq_nested)
1646 irq_set_nested_thread(irq, 1);
1647 irq_set_noprobe(irq);
1648
1649
1650
1651
1652
1653 if (chip->irq_default_type != IRQ_TYPE_NONE)
1654 irq_set_irq_type(irq, chip->irq_default_type);
1655
1656 return 0;
1657}
1658
1659static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1660{
1661 struct gpio_chip *chip = d->host_data;
1662
1663 if (chip->irq_nested)
1664 irq_set_nested_thread(irq, 0);
1665 irq_set_chip_and_handler(irq, NULL, NULL);
1666 irq_set_chip_data(irq, NULL);
1667}
1668
1669static const struct irq_domain_ops gpiochip_domain_ops = {
1670 .map = gpiochip_irq_map,
1671 .unmap = gpiochip_irq_unmap,
1672
1673 .xlate = irq_domain_xlate_twocell,
1674};
1675
1676static int gpiochip_irq_reqres(struct irq_data *d)
1677{
1678 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1679
1680 if (!try_module_get(chip->gpiodev->owner))
1681 return -ENODEV;
1682
1683 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
1684 chip_err(chip,
1685 "unable to lock HW IRQ %lu for IRQ\n",
1686 d->hwirq);
1687 module_put(chip->gpiodev->owner);
1688 return -EINVAL;
1689 }
1690 return 0;
1691}
1692
1693static void gpiochip_irq_relres(struct irq_data *d)
1694{
1695 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1696
1697 gpiochip_unlock_as_irq(chip, d->hwirq);
1698 module_put(chip->gpiodev->owner);
1699}
1700
1701static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1702{
1703 if (!gpiochip_irqchip_irq_valid(chip, offset))
1704 return -ENXIO;
1705 return irq_create_mapping(chip->irqdomain, offset);
1706}
1707
1708
1709
1710
1711
1712
1713
1714static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1715{
1716 unsigned int offset;
1717
1718 acpi_gpiochip_free_interrupts(gpiochip);
1719
1720 if (gpiochip->irq_chained_parent) {
1721 irq_set_chained_handler(gpiochip->irq_chained_parent, NULL);
1722 irq_set_handler_data(gpiochip->irq_chained_parent, NULL);
1723 }
1724
1725
1726 if (gpiochip->irqdomain) {
1727 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1728 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1729 continue;
1730 irq_dispose_mapping(
1731 irq_find_mapping(gpiochip->irqdomain, offset));
1732 }
1733 irq_domain_remove(gpiochip->irqdomain);
1734 }
1735
1736 if (gpiochip->irqchip) {
1737 gpiochip->irqchip->irq_request_resources = NULL;
1738 gpiochip->irqchip->irq_release_resources = NULL;
1739 gpiochip->irqchip = NULL;
1740 }
1741
1742 gpiochip_irqchip_free_valid_mask(gpiochip);
1743}
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1773 struct irq_chip *irqchip,
1774 unsigned int first_irq,
1775 irq_flow_handler_t handler,
1776 unsigned int type,
1777 bool nested,
1778 struct lock_class_key *lock_key)
1779{
1780 struct device_node *of_node;
1781
1782 if (!gpiochip || !irqchip)
1783 return -EINVAL;
1784
1785 if (!gpiochip->parent) {
1786 pr_err("missing gpiochip .dev parent pointer\n");
1787 return -EINVAL;
1788 }
1789 gpiochip->irq_nested = nested;
1790 of_node = gpiochip->parent->of_node;
1791#ifdef CONFIG_OF_GPIO
1792
1793
1794
1795
1796
1797 if (gpiochip->of_node)
1798 of_node = gpiochip->of_node;
1799#endif
1800
1801
1802
1803
1804
1805 if (WARN(of_node && type != IRQ_TYPE_NONE,
1806 "%pOF: Ignoring %d default trigger\n", of_node, type))
1807 type = IRQ_TYPE_NONE;
1808 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1809 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1810 "Ignoring %d default trigger\n", type);
1811 type = IRQ_TYPE_NONE;
1812 }
1813
1814 gpiochip->irqchip = irqchip;
1815 gpiochip->irq_handler = handler;
1816 gpiochip->irq_default_type = type;
1817 gpiochip->to_irq = gpiochip_to_irq;
1818 gpiochip->lock_key = lock_key;
1819 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1820 gpiochip->ngpio, first_irq,
1821 &gpiochip_domain_ops, gpiochip);
1822 if (!gpiochip->irqdomain) {
1823 gpiochip->irqchip = NULL;
1824 return -EINVAL;
1825 }
1826
1827
1828
1829
1830
1831 if (!irqchip->irq_request_resources &&
1832 !irqchip->irq_release_resources) {
1833 irqchip->irq_request_resources = gpiochip_irq_reqres;
1834 irqchip->irq_release_resources = gpiochip_irq_relres;
1835 }
1836
1837 acpi_gpiochip_request_interrupts(gpiochip);
1838
1839 return 0;
1840}
1841EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
1842
1843#else
1844
1845static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1846static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1847{
1848 return 0;
1849}
1850static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1851{ }
1852
1853#endif
1854
1855
1856
1857
1858
1859
1860int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1861{
1862 return pinctrl_request_gpio(chip->gpiodev->base + offset);
1863}
1864EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1865
1866
1867
1868
1869
1870
1871void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1872{
1873 pinctrl_free_gpio(chip->gpiodev->base + offset);
1874}
1875EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1876
1877
1878
1879
1880
1881
1882
1883int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1884 unsigned long config)
1885{
1886 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1887}
1888EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1889
1890#ifdef CONFIG_PINCTRL
1891
1892
1893
1894
1895
1896
1897
1898
1899int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1900 struct pinctrl_dev *pctldev,
1901 unsigned int gpio_offset, const char *pin_group)
1902{
1903 struct gpio_pin_range *pin_range;
1904 struct gpio_device *gdev = chip->gpiodev;
1905 int ret;
1906
1907 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1908 if (!pin_range) {
1909 chip_err(chip, "failed to allocate pin ranges\n");
1910 return -ENOMEM;
1911 }
1912
1913
1914 pin_range->range.id = gpio_offset;
1915 pin_range->range.gc = chip;
1916 pin_range->range.name = chip->label;
1917 pin_range->range.base = gdev->base + gpio_offset;
1918 pin_range->pctldev = pctldev;
1919
1920 ret = pinctrl_get_group_pins(pctldev, pin_group,
1921 &pin_range->range.pins,
1922 &pin_range->range.npins);
1923 if (ret < 0) {
1924 kfree(pin_range);
1925 return ret;
1926 }
1927
1928 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1929
1930 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1931 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1932 pinctrl_dev_get_devname(pctldev), pin_group);
1933
1934 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1935
1936 return 0;
1937}
1938EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1953 unsigned int gpio_offset, unsigned int pin_offset,
1954 unsigned int npins)
1955{
1956 struct gpio_pin_range *pin_range;
1957 struct gpio_device *gdev = chip->gpiodev;
1958 int ret;
1959
1960 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1961 if (!pin_range) {
1962 chip_err(chip, "failed to allocate pin ranges\n");
1963 return -ENOMEM;
1964 }
1965
1966
1967 pin_range->range.id = gpio_offset;
1968 pin_range->range.gc = chip;
1969 pin_range->range.name = chip->label;
1970 pin_range->range.base = gdev->base + gpio_offset;
1971 pin_range->range.pin_base = pin_offset;
1972 pin_range->range.npins = npins;
1973 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
1974 &pin_range->range);
1975 if (IS_ERR(pin_range->pctldev)) {
1976 ret = PTR_ERR(pin_range->pctldev);
1977 chip_err(chip, "could not create pin range\n");
1978 kfree(pin_range);
1979 return ret;
1980 }
1981 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1982 gpio_offset, gpio_offset + npins - 1,
1983 pinctl_name,
1984 pin_offset, pin_offset + npins - 1);
1985
1986 list_add_tail(&pin_range->node, &gdev->pin_ranges);
1987
1988 return 0;
1989}
1990EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
1991
1992
1993
1994
1995
1996void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1997{
1998 struct gpio_pin_range *pin_range, *tmp;
1999 struct gpio_device *gdev = chip->gpiodev;
2000
2001 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
2002 list_del(&pin_range->node);
2003 pinctrl_remove_gpio_range(pin_range->pctldev,
2004 &pin_range->range);
2005 kfree(pin_range);
2006 }
2007}
2008EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2009
2010#endif
2011
2012
2013
2014
2015
2016static int __gpiod_request(struct gpio_desc *desc, const char *label)
2017{
2018 struct gpio_chip *chip = desc->gdev->chip;
2019 int status;
2020 unsigned long flags;
2021
2022 spin_lock_irqsave(&gpio_lock, flags);
2023
2024
2025
2026
2027
2028 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2029 desc_set_label(desc, label ? : "?");
2030 status = 0;
2031 } else {
2032 status = -EBUSY;
2033 goto done;
2034 }
2035
2036 if (chip->request) {
2037
2038 spin_unlock_irqrestore(&gpio_lock, flags);
2039 status = chip->request(chip, gpio_chip_hwgpio(desc));
2040 spin_lock_irqsave(&gpio_lock, flags);
2041
2042 if (status < 0) {
2043 desc_set_label(desc, NULL);
2044 clear_bit(FLAG_REQUESTED, &desc->flags);
2045 goto done;
2046 }
2047 }
2048 if (chip->get_direction) {
2049
2050 spin_unlock_irqrestore(&gpio_lock, flags);
2051 gpiod_get_direction(desc);
2052 spin_lock_irqsave(&gpio_lock, flags);
2053 }
2054done:
2055 spin_unlock_irqrestore(&gpio_lock, flags);
2056 return status;
2057}
2058
2059
2060
2061
2062
2063
2064
2065#define VALIDATE_DESC(desc) do { \
2066 if (!desc) \
2067 return 0; \
2068 if (IS_ERR(desc)) { \
2069 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2070 return PTR_ERR(desc); \
2071 } \
2072 if (!desc->gdev) { \
2073 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
2074 return -EINVAL; \
2075 } \
2076 if ( !desc->gdev->chip ) { \
2077 dev_warn(&desc->gdev->dev, \
2078 "%s: backing chip is gone\n", __func__); \
2079 return 0; \
2080 } } while (0)
2081
2082#define VALIDATE_DESC_VOID(desc) do { \
2083 if (!desc) \
2084 return; \
2085 if (IS_ERR(desc)) { \
2086 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2087 return; \
2088 } \
2089 if (!desc->gdev) { \
2090 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
2091 return; \
2092 } \
2093 if (!desc->gdev->chip) { \
2094 dev_warn(&desc->gdev->dev, \
2095 "%s: backing chip is gone\n", __func__); \
2096 return; \
2097 } } while (0)
2098
2099
2100int gpiod_request(struct gpio_desc *desc, const char *label)
2101{
2102 int status = -EPROBE_DEFER;
2103 struct gpio_device *gdev;
2104
2105 VALIDATE_DESC(desc);
2106 gdev = desc->gdev;
2107
2108 if (try_module_get(gdev->owner)) {
2109 status = __gpiod_request(desc, label);
2110 if (status < 0)
2111 module_put(gdev->owner);
2112 else
2113 get_device(&gdev->dev);
2114 }
2115
2116 if (status)
2117 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2118
2119 return status;
2120}
2121
2122static bool __gpiod_free(struct gpio_desc *desc)
2123{
2124 bool ret = false;
2125 unsigned long flags;
2126 struct gpio_chip *chip;
2127
2128 might_sleep();
2129
2130 gpiod_unexport(desc);
2131
2132 spin_lock_irqsave(&gpio_lock, flags);
2133
2134 chip = desc->gdev->chip;
2135 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2136 if (chip->free) {
2137 spin_unlock_irqrestore(&gpio_lock, flags);
2138 might_sleep_if(chip->can_sleep);
2139 chip->free(chip, gpio_chip_hwgpio(desc));
2140 spin_lock_irqsave(&gpio_lock, flags);
2141 }
2142 desc_set_label(desc, NULL);
2143 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
2144 clear_bit(FLAG_REQUESTED, &desc->flags);
2145 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
2146 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2147 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2148 ret = true;
2149 }
2150
2151 spin_unlock_irqrestore(&gpio_lock, flags);
2152 return ret;
2153}
2154
2155void gpiod_free(struct gpio_desc *desc)
2156{
2157 if (desc && desc->gdev && __gpiod_free(desc)) {
2158 module_put(desc->gdev->owner);
2159 put_device(&desc->gdev->dev);
2160 } else {
2161 WARN_ON(extra_checks);
2162 }
2163}
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2179{
2180 struct gpio_desc *desc;
2181
2182 if (offset >= chip->ngpio)
2183 return NULL;
2184
2185 desc = &chip->gpiodev->descs[offset];
2186
2187 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
2188 return NULL;
2189 return desc->label;
2190}
2191EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2210 const char *label)
2211{
2212 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2213 int err;
2214
2215 if (IS_ERR(desc)) {
2216 chip_err(chip, "failed to get GPIO descriptor\n");
2217 return desc;
2218 }
2219
2220 err = __gpiod_request(desc, label);
2221 if (err < 0)
2222 return ERR_PTR(err);
2223
2224 return desc;
2225}
2226EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2227
2228
2229
2230
2231
2232
2233
2234
2235void gpiochip_free_own_desc(struct gpio_desc *desc)
2236{
2237 if (desc)
2238 __gpiod_free(desc);
2239}
2240EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261int gpiod_direction_input(struct gpio_desc *desc)
2262{
2263 struct gpio_chip *chip;
2264 int status = -EINVAL;
2265
2266 VALIDATE_DESC(desc);
2267 chip = desc->gdev->chip;
2268
2269 if (!chip->get || !chip->direction_input) {
2270 gpiod_warn(desc,
2271 "%s: missing get() or direction_input() operations\n",
2272 __func__);
2273 return -EIO;
2274 }
2275
2276 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2277 if (status == 0)
2278 clear_bit(FLAG_IS_OUT, &desc->flags);
2279
2280 trace_gpio_direction(desc_to_gpio(desc), 1, status);
2281
2282 return status;
2283}
2284EXPORT_SYMBOL_GPL(gpiod_direction_input);
2285
2286static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2287 enum pin_config_param mode)
2288{
2289 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2290
2291 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2292}
2293
2294static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2295{
2296 struct gpio_chip *gc = desc->gdev->chip;
2297 int val = !!value;
2298 int ret;
2299
2300
2301 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2302 gpiod_err(desc,
2303 "%s: tried to set a GPIO tied to an IRQ as output\n",
2304 __func__);
2305 return -EIO;
2306 }
2307
2308 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2309
2310 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2311 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2312 if (!ret)
2313 goto set_output_value;
2314
2315 if (val)
2316 return gpiod_direction_input(desc);
2317 }
2318 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2319 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2320 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2321 if (!ret)
2322 goto set_output_value;
2323
2324 if (!val)
2325 return gpiod_direction_input(desc);
2326 } else {
2327 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2328 PIN_CONFIG_DRIVE_PUSH_PULL);
2329 }
2330
2331set_output_value:
2332 if (!gc->set || !gc->direction_output) {
2333 gpiod_warn(desc,
2334 "%s: missing set() or direction_output() operations\n",
2335 __func__);
2336 return -EIO;
2337 }
2338
2339 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2340 if (!ret)
2341 set_bit(FLAG_IS_OUT, &desc->flags);
2342 trace_gpio_value(desc_to_gpio(desc), 0, val);
2343 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2344 return ret;
2345}
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2359{
2360 VALIDATE_DESC(desc);
2361 return _gpiod_direction_output_raw(desc, value);
2362}
2363EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377int gpiod_direction_output(struct gpio_desc *desc, int value)
2378{
2379 VALIDATE_DESC(desc);
2380 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2381 value = !value;
2382 else
2383 value = !!value;
2384 return _gpiod_direction_output_raw(desc, value);
2385}
2386EXPORT_SYMBOL_GPL(gpiod_direction_output);
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
2398{
2399 struct gpio_chip *chip;
2400 unsigned long config;
2401
2402 VALIDATE_DESC(desc);
2403 chip = desc->gdev->chip;
2404 if (!chip->set || !chip->set_config) {
2405 gpiod_dbg(desc,
2406 "%s: missing set() or set_config() operations\n",
2407 __func__);
2408 return -ENOTSUPP;
2409 }
2410
2411 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2412 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2413}
2414EXPORT_SYMBOL_GPL(gpiod_set_debounce);
2415
2416
2417
2418
2419
2420
2421
2422int gpiod_is_active_low(const struct gpio_desc *desc)
2423{
2424 VALIDATE_DESC(desc);
2425 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
2426}
2427EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451static int _gpiod_get_raw_value(const struct gpio_desc *desc)
2452{
2453 struct gpio_chip *chip;
2454 int offset;
2455 int value;
2456
2457 chip = desc->gdev->chip;
2458 offset = gpio_chip_hwgpio(desc);
2459 value = chip->get ? chip->get(chip, offset) : -EIO;
2460 value = value < 0 ? value : !!value;
2461 trace_gpio_value(desc_to_gpio(desc), 1, value);
2462 return value;
2463}
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475int gpiod_get_raw_value(const struct gpio_desc *desc)
2476{
2477 VALIDATE_DESC(desc);
2478
2479 WARN_ON(desc->gdev->chip->can_sleep);
2480 return _gpiod_get_raw_value(desc);
2481}
2482EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494int gpiod_get_value(const struct gpio_desc *desc)
2495{
2496 int value;
2497
2498 VALIDATE_DESC(desc);
2499
2500 WARN_ON(desc->gdev->chip->can_sleep);
2501
2502 value = _gpiod_get_raw_value(desc);
2503 if (value < 0)
2504 return value;
2505
2506 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2507 value = !value;
2508
2509 return value;
2510}
2511EXPORT_SYMBOL_GPL(gpiod_get_value);
2512
2513
2514
2515
2516
2517
2518static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
2519{
2520 int err = 0;
2521 struct gpio_chip *chip = desc->gdev->chip;
2522 int offset = gpio_chip_hwgpio(desc);
2523
2524 if (value) {
2525 err = chip->direction_input(chip, offset);
2526 if (!err)
2527 clear_bit(FLAG_IS_OUT, &desc->flags);
2528 } else {
2529 err = chip->direction_output(chip, offset, 0);
2530 if (!err)
2531 set_bit(FLAG_IS_OUT, &desc->flags);
2532 }
2533 trace_gpio_direction(desc_to_gpio(desc), value, err);
2534 if (err < 0)
2535 gpiod_err(desc,
2536 "%s: Error in set_value for open drain err %d\n",
2537 __func__, err);
2538}
2539
2540
2541
2542
2543
2544
2545static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
2546{
2547 int err = 0;
2548 struct gpio_chip *chip = desc->gdev->chip;
2549 int offset = gpio_chip_hwgpio(desc);
2550
2551 if (value) {
2552 err = chip->direction_output(chip, offset, 1);
2553 if (!err)
2554 set_bit(FLAG_IS_OUT, &desc->flags);
2555 } else {
2556 err = chip->direction_input(chip, offset);
2557 if (!err)
2558 clear_bit(FLAG_IS_OUT, &desc->flags);
2559 }
2560 trace_gpio_direction(desc_to_gpio(desc), !value, err);
2561 if (err < 0)
2562 gpiod_err(desc,
2563 "%s: Error in set_value for open source err %d\n",
2564 __func__, err);
2565}
2566
2567static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
2568{
2569 struct gpio_chip *chip;
2570
2571 chip = desc->gdev->chip;
2572 trace_gpio_value(desc_to_gpio(desc), 0, value);
2573 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2574 _gpio_set_open_drain_value(desc, value);
2575 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2576 _gpio_set_open_source_value(desc, value);
2577 else
2578 chip->set(chip, gpio_chip_hwgpio(desc), value);
2579}
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590static void gpio_chip_set_multiple(struct gpio_chip *chip,
2591 unsigned long *mask, unsigned long *bits)
2592{
2593 if (chip->set_multiple) {
2594 chip->set_multiple(chip, mask, bits);
2595 } else {
2596 unsigned int i;
2597
2598
2599 for_each_set_bit(i, mask, chip->ngpio)
2600 chip->set(chip, i, test_bit(i, bits));
2601 }
2602}
2603
2604void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2605 unsigned int array_size,
2606 struct gpio_desc **desc_array,
2607 int *value_array)
2608{
2609 int i = 0;
2610
2611 while (i < array_size) {
2612 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2613 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2614 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2615 int count = 0;
2616
2617 if (!can_sleep)
2618 WARN_ON(chip->can_sleep);
2619
2620 memset(mask, 0, sizeof(mask));
2621 do {
2622 struct gpio_desc *desc = desc_array[i];
2623 int hwgpio = gpio_chip_hwgpio(desc);
2624 int value = value_array[i];
2625
2626 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2627 value = !value;
2628 trace_gpio_value(desc_to_gpio(desc), 0, value);
2629
2630
2631
2632
2633 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2634 _gpio_set_open_drain_value(desc, value);
2635 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2636 _gpio_set_open_source_value(desc, value);
2637 } else {
2638 __set_bit(hwgpio, mask);
2639 if (value)
2640 __set_bit(hwgpio, bits);
2641 else
2642 __clear_bit(hwgpio, bits);
2643 count++;
2644 }
2645 i++;
2646 } while ((i < array_size) &&
2647 (desc_array[i]->gdev->chip == chip));
2648
2649 if (count != 0)
2650 gpio_chip_set_multiple(chip, mask, bits);
2651 }
2652}
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2666{
2667 VALIDATE_DESC_VOID(desc);
2668
2669 WARN_ON(desc->gdev->chip->can_sleep);
2670 _gpiod_set_raw_value(desc, value);
2671}
2672EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685void gpiod_set_value(struct gpio_desc *desc, int value)
2686{
2687 VALIDATE_DESC_VOID(desc);
2688
2689 WARN_ON(desc->gdev->chip->can_sleep);
2690 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2691 value = !value;
2692 _gpiod_set_raw_value(desc, value);
2693}
2694EXPORT_SYMBOL_GPL(gpiod_set_value);
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708void gpiod_set_raw_array_value(unsigned int array_size,
2709 struct gpio_desc **desc_array, int *value_array)
2710{
2711 if (!desc_array)
2712 return;
2713 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2714 value_array);
2715}
2716EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730void gpiod_set_array_value(unsigned int array_size,
2731 struct gpio_desc **desc_array, int *value_array)
2732{
2733 if (!desc_array)
2734 return;
2735 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2736 value_array);
2737}
2738EXPORT_SYMBOL_GPL(gpiod_set_array_value);
2739
2740
2741
2742
2743
2744
2745int gpiod_cansleep(const struct gpio_desc *desc)
2746{
2747 VALIDATE_DESC(desc);
2748 return desc->gdev->chip->can_sleep;
2749}
2750EXPORT_SYMBOL_GPL(gpiod_cansleep);
2751
2752
2753
2754
2755
2756
2757
2758
2759int gpiod_to_irq(const struct gpio_desc *desc)
2760{
2761 struct gpio_chip *chip;
2762 int offset;
2763
2764
2765
2766
2767
2768
2769 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
2770 return -EINVAL;
2771
2772 chip = desc->gdev->chip;
2773 offset = gpio_chip_hwgpio(desc);
2774 if (chip->to_irq) {
2775 int retirq = chip->to_irq(chip, offset);
2776
2777
2778 if (!retirq)
2779 return -ENXIO;
2780
2781 return retirq;
2782 }
2783 return -ENXIO;
2784}
2785EXPORT_SYMBOL_GPL(gpiod_to_irq);
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2796{
2797 struct gpio_desc *desc;
2798
2799 desc = gpiochip_get_desc(chip, offset);
2800 if (IS_ERR(desc))
2801 return PTR_ERR(desc);
2802
2803
2804
2805
2806
2807 if (!chip->can_sleep && chip->get_direction) {
2808 int dir = chip->get_direction(chip, offset);
2809
2810 if (dir)
2811 clear_bit(FLAG_IS_OUT, &desc->flags);
2812 else
2813 set_bit(FLAG_IS_OUT, &desc->flags);
2814 }
2815
2816 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2817 chip_err(chip,
2818 "%s: tried to flag a GPIO set as output for IRQ\n",
2819 __func__);
2820 return -EIO;
2821 }
2822
2823 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2824
2825
2826
2827
2828
2829
2830 if (!desc->label)
2831 desc_set_label(desc, "interrupt");
2832
2833 return 0;
2834}
2835EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2846{
2847 struct gpio_desc *desc;
2848
2849 desc = gpiochip_get_desc(chip, offset);
2850 if (IS_ERR(desc))
2851 return;
2852
2853 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2854
2855
2856 if (desc->label && !strcmp(desc->label, "interrupt"))
2857 desc_set_label(desc, NULL);
2858}
2859EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
2860
2861bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2862{
2863 if (offset >= chip->ngpio)
2864 return false;
2865
2866 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2867}
2868EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2869
2870bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2871{
2872 if (offset >= chip->ngpio)
2873 return false;
2874
2875 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2876}
2877EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2878
2879bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2880{
2881 if (offset >= chip->ngpio)
2882 return false;
2883
2884 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2885}
2886EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2887
2888bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
2889{
2890 if (offset >= chip->ngpio)
2891 return false;
2892
2893 return !test_bit(FLAG_SLEEP_MAY_LOOSE_VALUE,
2894 &chip->gpiodev->descs[offset].flags);
2895}
2896EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2908{
2909 might_sleep_if(extra_checks);
2910 VALIDATE_DESC(desc);
2911 return _gpiod_get_raw_value(desc);
2912}
2913EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2925{
2926 int value;
2927
2928 might_sleep_if(extra_checks);
2929 VALIDATE_DESC(desc);
2930 value = _gpiod_get_raw_value(desc);
2931 if (value < 0)
2932 return value;
2933
2934 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2935 value = !value;
2936
2937 return value;
2938}
2939EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2952{
2953 might_sleep_if(extra_checks);
2954 VALIDATE_DESC_VOID(desc);
2955 _gpiod_set_raw_value(desc, value);
2956}
2957EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2970{
2971 might_sleep_if(extra_checks);
2972 VALIDATE_DESC_VOID(desc);
2973 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2974 value = !value;
2975 _gpiod_set_raw_value(desc, value);
2976}
2977EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2991 struct gpio_desc **desc_array,
2992 int *value_array)
2993{
2994 might_sleep_if(extra_checks);
2995 if (!desc_array)
2996 return;
2997 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2998 value_array);
2999}
3000EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
3001
3002
3003
3004
3005
3006
3007void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3008{
3009 unsigned int i;
3010
3011 mutex_lock(&gpio_lookup_lock);
3012
3013 for (i = 0; i < n; i++)
3014 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3015
3016 mutex_unlock(&gpio_lookup_lock);
3017}
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030void gpiod_set_array_value_cansleep(unsigned int array_size,
3031 struct gpio_desc **desc_array,
3032 int *value_array)
3033{
3034 might_sleep_if(extra_checks);
3035 if (!desc_array)
3036 return;
3037 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3038 value_array);
3039}
3040EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
3041
3042
3043
3044
3045
3046void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
3047{
3048 mutex_lock(&gpio_lookup_lock);
3049
3050 list_add_tail(&table->list, &gpio_lookup_list);
3051
3052 mutex_unlock(&gpio_lookup_lock);
3053}
3054EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
3055
3056
3057
3058
3059
3060void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3061{
3062 mutex_lock(&gpio_lookup_lock);
3063
3064 list_del(&table->list);
3065
3066 mutex_unlock(&gpio_lookup_lock);
3067}
3068EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
3069
3070static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3071{
3072 const char *dev_id = dev ? dev_name(dev) : NULL;
3073 struct gpiod_lookup_table *table;
3074
3075 mutex_lock(&gpio_lookup_lock);
3076
3077 list_for_each_entry(table, &gpio_lookup_list, list) {
3078 if (table->dev_id && dev_id) {
3079
3080
3081
3082
3083 if (!strcmp(table->dev_id, dev_id))
3084 goto found;
3085 } else {
3086
3087
3088
3089
3090 if (dev_id == table->dev_id)
3091 goto found;
3092 }
3093 }
3094 table = NULL;
3095
3096found:
3097 mutex_unlock(&gpio_lookup_lock);
3098 return table;
3099}
3100
3101static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3102 unsigned int idx,
3103 enum gpio_lookup_flags *flags)
3104{
3105 struct gpio_desc *desc = ERR_PTR(-ENOENT);
3106 struct gpiod_lookup_table *table;
3107 struct gpiod_lookup *p;
3108
3109 table = gpiod_find_lookup_table(dev);
3110 if (!table)
3111 return desc;
3112
3113 for (p = &table->table[0]; p->chip_label; p++) {
3114 struct gpio_chip *chip;
3115
3116
3117 if (p->idx != idx)
3118 continue;
3119
3120
3121 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3122 continue;
3123
3124 chip = find_chip_by_name(p->chip_label);
3125
3126 if (!chip) {
3127 dev_err(dev, "cannot find GPIO chip %s\n",
3128 p->chip_label);
3129 return ERR_PTR(-ENODEV);
3130 }
3131
3132 if (chip->ngpio <= p->chip_hwnum) {
3133 dev_err(dev,
3134 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3135 idx, chip->ngpio, chip->label);
3136 return ERR_PTR(-EINVAL);
3137 }
3138
3139 desc = gpiochip_get_desc(chip, p->chip_hwnum);
3140 *flags = p->flags;
3141
3142 return desc;
3143 }
3144
3145 return desc;
3146}
3147
3148static int dt_gpio_count(struct device *dev, const char *con_id)
3149{
3150 int ret;
3151 char propname[32];
3152 unsigned int i;
3153
3154 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3155 if (con_id)
3156 snprintf(propname, sizeof(propname), "%s-%s",
3157 con_id, gpio_suffixes[i]);
3158 else
3159 snprintf(propname, sizeof(propname), "%s",
3160 gpio_suffixes[i]);
3161
3162 ret = of_gpio_named_count(dev->of_node, propname);
3163 if (ret > 0)
3164 break;
3165 }
3166 return ret ? ret : -ENOENT;
3167}
3168
3169static int platform_gpio_count(struct device *dev, const char *con_id)
3170{
3171 struct gpiod_lookup_table *table;
3172 struct gpiod_lookup *p;
3173 unsigned int count = 0;
3174
3175 table = gpiod_find_lookup_table(dev);
3176 if (!table)
3177 return -ENOENT;
3178
3179 for (p = &table->table[0]; p->chip_label; p++) {
3180 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3181 (!con_id && !p->con_id))
3182 count++;
3183 }
3184 if (!count)
3185 return -ENOENT;
3186
3187 return count;
3188}
3189
3190
3191
3192
3193
3194
3195
3196int gpiod_count(struct device *dev, const char *con_id)
3197{
3198 int count = -ENOENT;
3199
3200 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3201 count = dt_gpio_count(dev, con_id);
3202 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3203 count = acpi_gpio_count(dev, con_id);
3204
3205 if (count < 0)
3206 count = platform_gpio_count(dev, con_id);
3207
3208 return count;
3209}
3210EXPORT_SYMBOL_GPL(gpiod_count);
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
3223 enum gpiod_flags flags)
3224{
3225 return gpiod_get_index(dev, con_id, 0, flags);
3226}
3227EXPORT_SYMBOL_GPL(gpiod_get);
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
3240 const char *con_id,
3241 enum gpiod_flags flags)
3242{
3243 return gpiod_get_index_optional(dev, con_id, 0, flags);
3244}
3245EXPORT_SYMBOL_GPL(gpiod_get_optional);
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3261 unsigned long lflags, enum gpiod_flags dflags)
3262{
3263 int status;
3264
3265 if (lflags & GPIO_ACTIVE_LOW)
3266 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3267 if (lflags & GPIO_OPEN_DRAIN)
3268 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3269 if (lflags & GPIO_OPEN_SOURCE)
3270 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3271 if (lflags & GPIO_SLEEP_MAY_LOOSE_VALUE)
3272 set_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, &desc->flags);
3273
3274
3275 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3276 pr_debug("no flags found for %s\n", con_id);
3277 return 0;
3278 }
3279
3280
3281 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3282 status = gpiod_direction_output(desc,
3283 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
3284 else
3285 status = gpiod_direction_input(desc);
3286
3287 return status;
3288}
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
3305 const char *con_id,
3306 unsigned int idx,
3307 enum gpiod_flags flags)
3308{
3309 struct gpio_desc *desc = NULL;
3310 int status;
3311 enum gpio_lookup_flags lookupflags = 0;
3312
3313 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3314
3315 if (dev) {
3316
3317 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3318 dev_dbg(dev, "using device tree for GPIO lookup\n");
3319 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3320 } else if (ACPI_COMPANION(dev)) {
3321 dev_dbg(dev, "using ACPI for GPIO lookup\n");
3322 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
3323 }
3324 }
3325
3326
3327
3328
3329
3330 if (!desc || desc == ERR_PTR(-ENOENT)) {
3331 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
3332 desc = gpiod_find(dev, con_id, idx, &lookupflags);
3333 }
3334
3335 if (IS_ERR(desc)) {
3336 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
3337 return desc;
3338 }
3339
3340 status = gpiod_request(desc, con_id);
3341 if (status < 0)
3342 return ERR_PTR(status);
3343
3344 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3345 if (status < 0) {
3346 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3347 gpiod_put(desc);
3348 return ERR_PTR(status);
3349 }
3350
3351 return desc;
3352}
3353EXPORT_SYMBOL_GPL(gpiod_get_index);
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3377 const char *propname, int index,
3378 enum gpiod_flags dflags,
3379 const char *label)
3380{
3381 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3382 unsigned long lflags = 0;
3383 bool active_low = false;
3384 bool single_ended = false;
3385 bool open_drain = false;
3386 int ret;
3387
3388 if (!fwnode)
3389 return ERR_PTR(-EINVAL);
3390
3391 if (is_of_node(fwnode)) {
3392 enum of_gpio_flags flags;
3393
3394 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3395 index, &flags);
3396 if (!IS_ERR(desc)) {
3397 active_low = flags & OF_GPIO_ACTIVE_LOW;
3398 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3399 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3400 }
3401 } else if (is_acpi_node(fwnode)) {
3402 struct acpi_gpio_info info;
3403
3404 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3405 if (!IS_ERR(desc)) {
3406 active_low = info.polarity == GPIO_ACTIVE_LOW;
3407 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3408 if (ret)
3409 pr_debug("Override GPIO initialization flags\n");
3410 }
3411 }
3412
3413 if (IS_ERR(desc))
3414 return desc;
3415
3416 ret = gpiod_request(desc, label);
3417 if (ret)
3418 return ERR_PTR(ret);
3419
3420 if (active_low)
3421 lflags |= GPIO_ACTIVE_LOW;
3422
3423 if (single_ended) {
3424 if (open_drain)
3425 lflags |= GPIO_OPEN_DRAIN;
3426 else
3427 lflags |= GPIO_OPEN_SOURCE;
3428 }
3429
3430 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3431 if (ret < 0) {
3432 gpiod_put(desc);
3433 return ERR_PTR(ret);
3434 }
3435
3436 return desc;
3437}
3438EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
3453 const char *con_id,
3454 unsigned int index,
3455 enum gpiod_flags flags)
3456{
3457 struct gpio_desc *desc;
3458
3459 desc = gpiod_get_index(dev, con_id, index, flags);
3460 if (IS_ERR(desc)) {
3461 if (PTR_ERR(desc) == -ENOENT)
3462 return NULL;
3463 }
3464
3465 return desc;
3466}
3467EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477int gpiod_hog(struct gpio_desc *desc, const char *name,
3478 unsigned long lflags, enum gpiod_flags dflags)
3479{
3480 struct gpio_chip *chip;
3481 struct gpio_desc *local_desc;
3482 int hwnum;
3483 int status;
3484
3485 chip = gpiod_to_chip(desc);
3486 hwnum = gpio_chip_hwgpio(desc);
3487
3488 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3489 if (IS_ERR(local_desc)) {
3490 status = PTR_ERR(local_desc);
3491 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3492 name, chip->label, hwnum, status);
3493 return status;
3494 }
3495
3496 status = gpiod_configure_flags(desc, name, lflags, dflags);
3497 if (status < 0) {
3498 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3499 name, chip->label, hwnum, status);
3500 gpiochip_free_own_desc(desc);
3501 return status;
3502 }
3503
3504
3505 set_bit(FLAG_IS_HOGGED, &desc->flags);
3506
3507 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3508 desc_to_gpio(desc), name,
3509 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3510 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3511 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3512
3513 return 0;
3514}
3515
3516
3517
3518
3519
3520
3521
3522static void gpiochip_free_hogs(struct gpio_chip *chip)
3523{
3524 int id;
3525
3526 for (id = 0; id < chip->ngpio; id++) {
3527 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3528 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
3529 }
3530}
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3545 const char *con_id,
3546 enum gpiod_flags flags)
3547{
3548 struct gpio_desc *desc;
3549 struct gpio_descs *descs;
3550 int count;
3551
3552 count = gpiod_count(dev, con_id);
3553 if (count < 0)
3554 return ERR_PTR(count);
3555
3556 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3557 GFP_KERNEL);
3558 if (!descs)
3559 return ERR_PTR(-ENOMEM);
3560
3561 for (descs->ndescs = 0; descs->ndescs < count; ) {
3562 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3563 if (IS_ERR(desc)) {
3564 gpiod_put_array(descs);
3565 return ERR_CAST(desc);
3566 }
3567 descs->desc[descs->ndescs] = desc;
3568 descs->ndescs++;
3569 }
3570 return descs;
3571}
3572EXPORT_SYMBOL_GPL(gpiod_get_array);
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3585 const char *con_id,
3586 enum gpiod_flags flags)
3587{
3588 struct gpio_descs *descs;
3589
3590 descs = gpiod_get_array(dev, con_id, flags);
3591 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3592 return NULL;
3593
3594 return descs;
3595}
3596EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3597
3598
3599
3600
3601
3602
3603
3604void gpiod_put(struct gpio_desc *desc)
3605{
3606 gpiod_free(desc);
3607}
3608EXPORT_SYMBOL_GPL(gpiod_put);
3609
3610
3611
3612
3613
3614void gpiod_put_array(struct gpio_descs *descs)
3615{
3616 unsigned int i;
3617
3618 for (i = 0; i < descs->ndescs; i++)
3619 gpiod_put(descs->desc[i]);
3620
3621 kfree(descs);
3622}
3623EXPORT_SYMBOL_GPL(gpiod_put_array);
3624
3625static int __init gpiolib_dev_init(void)
3626{
3627 int ret;
3628
3629
3630 ret = bus_register(&gpio_bus_type);
3631 if (ret < 0) {
3632 pr_err("gpiolib: could not register GPIO bus type\n");
3633 return ret;
3634 }
3635
3636 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3637 if (ret < 0) {
3638 pr_err("gpiolib: failed to allocate char dev region\n");
3639 bus_unregister(&gpio_bus_type);
3640 } else {
3641 gpiolib_initialized = true;
3642 gpiochip_setup_devs();
3643 }
3644 return ret;
3645}
3646core_initcall(gpiolib_dev_init);
3647
3648#ifdef CONFIG_DEBUG_FS
3649
3650static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
3651{
3652 unsigned i;
3653 struct gpio_chip *chip = gdev->chip;
3654 unsigned gpio = gdev->base;
3655 struct gpio_desc *gdesc = &gdev->descs[0];
3656 int is_out;
3657 int is_irq;
3658
3659 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
3660 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3661 if (gdesc->name) {
3662 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3663 gpio, gdesc->name);
3664 }
3665 continue;
3666 }
3667
3668 gpiod_get_direction(gdesc);
3669 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
3670 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
3671 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3672 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
3673 is_out ? "out" : "in ",
3674 chip->get
3675 ? (chip->get(chip, i) ? "hi" : "lo")
3676 : "? ",
3677 is_irq ? "IRQ" : " ");
3678 seq_printf(s, "\n");
3679 }
3680}
3681
3682static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
3683{
3684 unsigned long flags;
3685 struct gpio_device *gdev = NULL;
3686 loff_t index = *pos;
3687
3688 s->private = "";
3689
3690 spin_lock_irqsave(&gpio_lock, flags);
3691 list_for_each_entry(gdev, &gpio_devices, list)
3692 if (index-- == 0) {
3693 spin_unlock_irqrestore(&gpio_lock, flags);
3694 return gdev;
3695 }
3696 spin_unlock_irqrestore(&gpio_lock, flags);
3697
3698 return NULL;
3699}
3700
3701static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3702{
3703 unsigned long flags;
3704 struct gpio_device *gdev = v;
3705 void *ret = NULL;
3706
3707 spin_lock_irqsave(&gpio_lock, flags);
3708 if (list_is_last(&gdev->list, &gpio_devices))
3709 ret = NULL;
3710 else
3711 ret = list_entry(gdev->list.next, struct gpio_device, list);
3712 spin_unlock_irqrestore(&gpio_lock, flags);
3713
3714 s->private = "\n";
3715 ++*pos;
3716
3717 return ret;
3718}
3719
3720static void gpiolib_seq_stop(struct seq_file *s, void *v)
3721{
3722}
3723
3724static int gpiolib_seq_show(struct seq_file *s, void *v)
3725{
3726 struct gpio_device *gdev = v;
3727 struct gpio_chip *chip = gdev->chip;
3728 struct device *parent;
3729
3730 if (!chip) {
3731 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3732 dev_name(&gdev->dev));
3733 return 0;
3734 }
3735
3736 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3737 dev_name(&gdev->dev),
3738 gdev->base, gdev->base + gdev->ngpio - 1);
3739 parent = chip->parent;
3740 if (parent)
3741 seq_printf(s, ", parent: %s/%s",
3742 parent->bus ? parent->bus->name : "no-bus",
3743 dev_name(parent));
3744 if (chip->label)
3745 seq_printf(s, ", %s", chip->label);
3746 if (chip->can_sleep)
3747 seq_printf(s, ", can sleep");
3748 seq_printf(s, ":\n");
3749
3750 if (chip->dbg_show)
3751 chip->dbg_show(s, chip);
3752 else
3753 gpiolib_dbg_show(s, gdev);
3754
3755 return 0;
3756}
3757
3758static const struct seq_operations gpiolib_seq_ops = {
3759 .start = gpiolib_seq_start,
3760 .next = gpiolib_seq_next,
3761 .stop = gpiolib_seq_stop,
3762 .show = gpiolib_seq_show,
3763};
3764
3765static int gpiolib_open(struct inode *inode, struct file *file)
3766{
3767 return seq_open(file, &gpiolib_seq_ops);
3768}
3769
3770static const struct file_operations gpiolib_operations = {
3771 .owner = THIS_MODULE,
3772 .open = gpiolib_open,
3773 .read = seq_read,
3774 .llseek = seq_lseek,
3775 .release = seq_release,
3776};
3777
3778static int __init gpiolib_debugfs_init(void)
3779{
3780
3781 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3782 NULL, NULL, &gpiolib_operations);
3783 return 0;
3784}
3785subsys_initcall(gpiolib_debugfs_init);
3786
3787#endif
3788