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