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