hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/gpio/gpiolib.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 #include <linux/bitmap.h>
23 #include <linux/kernel.h>
34 #include <linux/module.h>
....@@ -10,28 +11,27 @@
1011 #include <linux/debugfs.h>
1112 #include <linux/seq_file.h>
1213 #include <linux/gpio.h>
13
-#include <linux/of_gpio.h>
1414 #include <linux/idr.h>
1515 #include <linux/slab.h>
1616 #include <linux/acpi.h>
1717 #include <linux/gpio/driver.h>
1818 #include <linux/gpio/machine.h>
1919 #include <linux/pinctrl/consumer.h>
20
-#include <linux/cdev.h>
2120 #include <linux/fs.h>
22
-#include <linux/uaccess.h>
2321 #include <linux/compat.h>
24
-#include <linux/anon_inodes.h>
2522 #include <linux/file.h>
26
-#include <linux/kfifo.h>
27
-#include <linux/poll.h>
28
-#include <linux/timekeeping.h>
2923 #include <uapi/linux/gpio.h>
3024
3125 #include "gpiolib.h"
26
+#include "gpiolib-of.h"
27
+#include "gpiolib-acpi.h"
28
+#include "gpiolib-cdev.h"
29
+#include "gpiolib-sysfs.h"
3230
3331 #define CREATE_TRACE_POINTS
3432 #include <trace/events/gpio.h>
33
+#undef CREATE_TRACE_POINTS
34
+#include <trace/hooks/gpiolib.h>
3535
3636 /* Implementation infrastructure for GPIO interfaces.
3737 *
....@@ -57,8 +57,10 @@
5757 static DEFINE_IDA(gpio_ida);
5858 static dev_t gpio_devt;
5959 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60
+static int gpio_bus_match(struct device *dev, struct device_driver *drv);
6061 static struct bus_type gpio_bus_type = {
6162 .name = "gpio",
63
+ .match = gpio_bus_match,
6264 };
6365
6466 /*
....@@ -79,13 +81,14 @@
7981 static DEFINE_MUTEX(gpio_machine_hogs_mutex);
8082 static LIST_HEAD(gpio_machine_hogs);
8183
82
-static void gpiochip_free_hogs(struct gpio_chip *chip);
83
-static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
84
+static void gpiochip_free_hogs(struct gpio_chip *gc);
85
+static int gpiochip_add_irqchip(struct gpio_chip *gc,
8486 struct lock_class_key *lock_key,
8587 struct lock_class_key *request_key);
86
-static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
87
-static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
88
-static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
88
+static void gpiochip_irqchip_remove(struct gpio_chip *gc);
89
+static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
90
+static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
91
+static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
8992
9093 static bool gpiolib_initialized;
9194
....@@ -129,23 +132,24 @@
129132 /**
130133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
131134 * hardware number for this chip
132
- * @chip: GPIO chip
135
+ * @gc: GPIO chip
133136 * @hwnum: hardware number of the GPIO for this chip
134137 *
135138 * Returns:
136
- * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
139
+ * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
137140 * in the given chip for the specified hardware number.
138141 */
139
-struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
140
- u16 hwnum)
142
+struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
143
+ unsigned int hwnum)
141144 {
142
- struct gpio_device *gdev = chip->gpiodev;
145
+ struct gpio_device *gdev = gc->gpiodev;
143146
144147 if (hwnum >= gdev->ngpio)
145148 return ERR_PTR(-EINVAL);
146149
147150 return &gdev->descs[hwnum];
148151 }
152
+EXPORT_SYMBOL_GPL(gpiochip_get_desc);
149153
150154 /**
151155 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
....@@ -210,11 +214,11 @@
210214 */
211215 int gpiod_get_direction(struct gpio_desc *desc)
212216 {
213
- struct gpio_chip *chip;
214
- unsigned offset;
215
- int status = -EINVAL;
217
+ struct gpio_chip *gc;
218
+ unsigned offset;
219
+ int ret;
216220
217
- chip = gpiod_to_chip(desc);
221
+ gc = gpiod_to_chip(desc);
218222 offset = gpio_chip_hwgpio(desc);
219223
220224 /*
....@@ -225,20 +229,20 @@
225229 test_bit(FLAG_IS_OUT, &desc->flags))
226230 return 0;
227231
228
- if (!chip->get_direction)
229
- return status;
232
+ if (!gc->get_direction)
233
+ return -ENOTSUPP;
230234
231
- status = chip->get_direction(chip, offset);
232
- if (status > 0) {
233
- /* GPIOF_DIR_IN, or other positive */
234
- status = 1;
235
- clear_bit(FLAG_IS_OUT, &desc->flags);
236
- }
237
- if (status == 0) {
238
- /* GPIOF_DIR_OUT */
239
- set_bit(FLAG_IS_OUT, &desc->flags);
240
- }
241
- return status;
235
+ ret = gc->get_direction(gc, offset);
236
+ if (ret < 0)
237
+ return ret;
238
+
239
+ /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
240
+ if (ret > 0)
241
+ ret = 1;
242
+
243
+ assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
244
+
245
+ return ret;
242246 }
243247 EXPORT_SYMBOL_GPL(gpiod_get_direction);
244248
....@@ -292,11 +296,17 @@
292296
293297 /*
294298 * Convert a GPIO name to its descriptor
299
+ * Note that there is no guarantee that GPIO names are globally unique!
300
+ * Hence this function will return, if it exists, a reference to the first GPIO
301
+ * line found that matches the given name.
295302 */
296303 static struct gpio_desc *gpio_name_to_desc(const char * const name)
297304 {
298305 struct gpio_device *gdev;
299306 unsigned long flags;
307
+
308
+ if (!name)
309
+ return NULL;
300310
301311 spin_lock_irqsave(&gpio_lock, flags);
302312
....@@ -306,7 +316,7 @@
306316 for (i = 0; i != gdev->ngpio; ++i) {
307317 struct gpio_desc *desc = &gdev->descs[i];
308318
309
- if (!desc->name || !name)
319
+ if (!desc->name)
310320 continue;
311321
312322 if (!strcmp(desc->name, name)) {
....@@ -322,18 +332,17 @@
322332 }
323333
324334 /*
325
- * Takes the names from gc->names and checks if they are all unique. If they
326
- * are, they are assigned to their gpio descriptors.
335
+ * Take the names from gc->names and assign them to their GPIO descriptors.
336
+ * Warn if a name is already used for a GPIO line on a different GPIO chip.
327337 *
328
- * Warning if one of the names is already used for a different GPIO.
338
+ * Note that:
339
+ * 1. Non-unique names are still accepted,
340
+ * 2. Name collisions within the same GPIO chip are not reported.
329341 */
330342 static int gpiochip_set_desc_names(struct gpio_chip *gc)
331343 {
332344 struct gpio_device *gdev = gc->gpiodev;
333345 int i;
334
-
335
- if (!gc->names)
336
- return 0;
337346
338347 /* First check all names if they are unique */
339348 for (i = 0; i != gc->ngpio; ++i) {
....@@ -353,873 +362,173 @@
353362 return 0;
354363 }
355364
356
-static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
365
+/*
366
+ * devprop_gpiochip_set_names - Set GPIO line names using device properties
367
+ * @chip: GPIO chip whose lines should be named, if possible
368
+ *
369
+ * Looks for device property "gpio-line-names" and if it exists assigns
370
+ * GPIO line names for the chip. The memory allocated for the assigned
371
+ * names belong to the underlying firmware node and should not be released
372
+ * by the caller.
373
+ */
374
+static int devprop_gpiochip_set_names(struct gpio_chip *chip)
375
+{
376
+ struct gpio_device *gdev = chip->gpiodev;
377
+ struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
378
+ const char **names;
379
+ int ret, i;
380
+ int count;
381
+
382
+ count = fwnode_property_string_array_count(fwnode, "gpio-line-names");
383
+ if (count < 0)
384
+ return 0;
385
+
386
+ if (count > gdev->ngpio) {
387
+ dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
388
+ count, gdev->ngpio);
389
+ count = gdev->ngpio;
390
+ }
391
+
392
+ names = kcalloc(count, sizeof(*names), GFP_KERNEL);
393
+ if (!names)
394
+ return -ENOMEM;
395
+
396
+ ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
397
+ names, count);
398
+ if (ret < 0) {
399
+ dev_warn(&gdev->dev, "failed to read GPIO line names\n");
400
+ kfree(names);
401
+ return ret;
402
+ }
403
+
404
+ for (i = 0; i < count; i++)
405
+ gdev->descs[i].name = names[i];
406
+
407
+ kfree(names);
408
+
409
+ return 0;
410
+}
411
+
412
+static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
357413 {
358414 unsigned long *p;
359415
360
- p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
416
+ p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
361417 if (!p)
362418 return NULL;
363419
364420 /* Assume by default all GPIOs are valid */
365
- bitmap_fill(p, chip->ngpio);
421
+ bitmap_fill(p, gc->ngpio);
366422
367423 return p;
368424 }
369425
370
-static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
426
+static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
371427 {
372
-#ifdef CONFIG_OF_GPIO
373
- int size;
374
- struct device_node *np = gpiochip->of_node;
375
-
376
- size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
377
- if (size > 0 && size % 2 == 0)
378
- gpiochip->need_valid_mask = true;
379
-#endif
380
-
381
- if (!gpiochip->need_valid_mask)
428
+ if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
382429 return 0;
383430
384
- gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
385
- if (!gpiochip->valid_mask)
431
+ gc->valid_mask = gpiochip_allocate_mask(gc);
432
+ if (!gc->valid_mask)
386433 return -ENOMEM;
387434
388435 return 0;
389436 }
390437
391
-static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
438
+static int gpiochip_init_valid_mask(struct gpio_chip *gc)
392439 {
393
- kfree(gpiochip->valid_mask);
394
- gpiochip->valid_mask = NULL;
440
+ if (gc->init_valid_mask)
441
+ return gc->init_valid_mask(gc,
442
+ gc->valid_mask,
443
+ gc->ngpio);
444
+
445
+ return 0;
395446 }
396447
397
-bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
448
+static void gpiochip_free_valid_mask(struct gpio_chip *gc)
449
+{
450
+ bitmap_free(gc->valid_mask);
451
+ gc->valid_mask = NULL;
452
+}
453
+
454
+static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
455
+{
456
+ if (gc->add_pin_ranges)
457
+ return gc->add_pin_ranges(gc);
458
+
459
+ return 0;
460
+}
461
+
462
+bool gpiochip_line_is_valid(const struct gpio_chip *gc,
398463 unsigned int offset)
399464 {
400465 /* No mask means all valid */
401
- if (likely(!gpiochip->valid_mask))
466
+ if (likely(!gc->valid_mask))
402467 return true;
403
- return test_bit(offset, gpiochip->valid_mask);
468
+ return test_bit(offset, gc->valid_mask);
404469 }
405470 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
406471
407
-/*
408
- * GPIO line handle management
409
- */
410
-
411
-/**
412
- * struct linehandle_state - contains the state of a userspace handle
413
- * @gdev: the GPIO device the handle pertains to
414
- * @label: consumer label used to tag descriptors
415
- * @descs: the GPIO descriptors held by this handle
416
- * @numdescs: the number of descriptors held in the descs array
417
- */
418
-struct linehandle_state {
419
- struct gpio_device *gdev;
420
- const char *label;
421
- struct gpio_desc *descs[GPIOHANDLES_MAX];
422
- u32 numdescs;
423
-};
424
-
425
-#define GPIOHANDLE_REQUEST_VALID_FLAGS \
426
- (GPIOHANDLE_REQUEST_INPUT | \
427
- GPIOHANDLE_REQUEST_OUTPUT | \
428
- GPIOHANDLE_REQUEST_ACTIVE_LOW | \
429
- GPIOHANDLE_REQUEST_OPEN_DRAIN | \
430
- GPIOHANDLE_REQUEST_OPEN_SOURCE)
431
-
432
-static long linehandle_ioctl(struct file *filep, unsigned int cmd,
433
- unsigned long arg)
434
-{
435
- struct linehandle_state *lh = filep->private_data;
436
- void __user *ip = (void __user *)arg;
437
- struct gpiohandle_data ghd;
438
- int vals[GPIOHANDLES_MAX];
439
- int i;
440
-
441
- if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
442
- /* NOTE: It's ok to read values of output lines. */
443
- int ret = gpiod_get_array_value_complex(false,
444
- true,
445
- lh->numdescs,
446
- lh->descs,
447
- vals);
448
- if (ret)
449
- return ret;
450
-
451
- memset(&ghd, 0, sizeof(ghd));
452
- for (i = 0; i < lh->numdescs; i++)
453
- ghd.values[i] = vals[i];
454
-
455
- if (copy_to_user(ip, &ghd, sizeof(ghd)))
456
- return -EFAULT;
457
-
458
- return 0;
459
- } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
460
- /*
461
- * All line descriptors were created at once with the same
462
- * flags so just check if the first one is really output.
463
- */
464
- if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
465
- return -EPERM;
466
-
467
- if (copy_from_user(&ghd, ip, sizeof(ghd)))
468
- return -EFAULT;
469
-
470
- /* Clamp all values to [0,1] */
471
- for (i = 0; i < lh->numdescs; i++)
472
- vals[i] = !!ghd.values[i];
473
-
474
- /* Reuse the array setting function */
475
- return gpiod_set_array_value_complex(false,
476
- true,
477
- lh->numdescs,
478
- lh->descs,
479
- vals);
480
- }
481
- return -EINVAL;
482
-}
483
-
484
-#ifdef CONFIG_COMPAT
485
-static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
486
- unsigned long arg)
487
-{
488
- return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
489
-}
490
-#endif
491
-
492
-static int linehandle_release(struct inode *inode, struct file *filep)
493
-{
494
- struct linehandle_state *lh = filep->private_data;
495
- struct gpio_device *gdev = lh->gdev;
496
- int i;
497
-
498
- for (i = 0; i < lh->numdescs; i++)
499
- gpiod_free(lh->descs[i]);
500
- kfree(lh->label);
501
- kfree(lh);
502
- put_device(&gdev->dev);
503
- return 0;
504
-}
505
-
506
-static const struct file_operations linehandle_fileops = {
507
- .release = linehandle_release,
508
- .owner = THIS_MODULE,
509
- .llseek = noop_llseek,
510
- .unlocked_ioctl = linehandle_ioctl,
511
-#ifdef CONFIG_COMPAT
512
- .compat_ioctl = linehandle_ioctl_compat,
513
-#endif
514
-};
515
-
516
-static int linehandle_create(struct gpio_device *gdev, void __user *ip)
517
-{
518
- struct gpiohandle_request handlereq;
519
- struct linehandle_state *lh;
520
- struct file *file;
521
- int fd, i, count = 0, ret;
522
- u32 lflags;
523
-
524
- if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
525
- return -EFAULT;
526
- if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
527
- return -EINVAL;
528
-
529
- lflags = handlereq.flags;
530
-
531
- /* Return an error if an unknown flag is set */
532
- if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
533
- return -EINVAL;
534
-
535
- /*
536
- * Do not allow both INPUT & OUTPUT flags to be set as they are
537
- * contradictory.
538
- */
539
- if ((lflags & GPIOHANDLE_REQUEST_INPUT) &&
540
- (lflags & GPIOHANDLE_REQUEST_OUTPUT))
541
- return -EINVAL;
542
-
543
- /*
544
- * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
545
- * the hardware actually supports enabling both at the same time the
546
- * electrical result would be disastrous.
547
- */
548
- if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
549
- (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
550
- return -EINVAL;
551
-
552
- /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
553
- if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
554
- ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
555
- (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
556
- return -EINVAL;
557
-
558
- lh = kzalloc(sizeof(*lh), GFP_KERNEL);
559
- if (!lh)
560
- return -ENOMEM;
561
- lh->gdev = gdev;
562
- get_device(&gdev->dev);
563
-
564
- /* Make sure this is terminated */
565
- handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
566
- if (strlen(handlereq.consumer_label)) {
567
- lh->label = kstrdup(handlereq.consumer_label,
568
- GFP_KERNEL);
569
- if (!lh->label) {
570
- ret = -ENOMEM;
571
- goto out_free_lh;
572
- }
573
- }
574
-
575
- /* Request each GPIO */
576
- for (i = 0; i < handlereq.lines; i++) {
577
- u32 offset = handlereq.lineoffsets[i];
578
- struct gpio_desc *desc;
579
-
580
- if (offset >= gdev->ngpio) {
581
- ret = -EINVAL;
582
- goto out_free_descs;
583
- }
584
-
585
- desc = &gdev->descs[offset];
586
- ret = gpiod_request(desc, lh->label);
587
- if (ret)
588
- goto out_free_descs;
589
- lh->descs[i] = desc;
590
- count = i + 1;
591
-
592
- if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
593
- set_bit(FLAG_ACTIVE_LOW, &desc->flags);
594
- if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
595
- set_bit(FLAG_OPEN_DRAIN, &desc->flags);
596
- if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
597
- set_bit(FLAG_OPEN_SOURCE, &desc->flags);
598
-
599
- ret = gpiod_set_transitory(desc, false);
600
- if (ret < 0)
601
- goto out_free_descs;
602
-
603
- /*
604
- * Lines have to be requested explicitly for input
605
- * or output, else the line will be treated "as is".
606
- */
607
- if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
608
- int val = !!handlereq.default_values[i];
609
-
610
- ret = gpiod_direction_output(desc, val);
611
- if (ret)
612
- goto out_free_descs;
613
- } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
614
- ret = gpiod_direction_input(desc);
615
- if (ret)
616
- goto out_free_descs;
617
- }
618
- dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
619
- offset);
620
- }
621
- /* Let i point at the last handle */
622
- i--;
623
- lh->numdescs = handlereq.lines;
624
-
625
- fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
626
- if (fd < 0) {
627
- ret = fd;
628
- goto out_free_descs;
629
- }
630
-
631
- file = anon_inode_getfile("gpio-linehandle",
632
- &linehandle_fileops,
633
- lh,
634
- O_RDONLY | O_CLOEXEC);
635
- if (IS_ERR(file)) {
636
- ret = PTR_ERR(file);
637
- goto out_put_unused_fd;
638
- }
639
-
640
- handlereq.fd = fd;
641
- if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
642
- /*
643
- * fput() will trigger the release() callback, so do not go onto
644
- * the regular error cleanup path here.
645
- */
646
- fput(file);
647
- put_unused_fd(fd);
648
- return -EFAULT;
649
- }
650
-
651
- fd_install(fd, file);
652
-
653
- dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
654
- lh->numdescs);
655
-
656
- return 0;
657
-
658
-out_put_unused_fd:
659
- put_unused_fd(fd);
660
-out_free_descs:
661
- for (i = 0; i < count; i++)
662
- gpiod_free(lh->descs[i]);
663
- kfree(lh->label);
664
-out_free_lh:
665
- kfree(lh);
666
- put_device(&gdev->dev);
667
- return ret;
668
-}
669
-
670
-/*
671
- * GPIO line event management
672
- */
673
-
674
-/**
675
- * struct lineevent_state - contains the state of a userspace event
676
- * @gdev: the GPIO device the event pertains to
677
- * @label: consumer label used to tag descriptors
678
- * @desc: the GPIO descriptor held by this event
679
- * @eflags: the event flags this line was requested with
680
- * @irq: the interrupt that trigger in response to events on this GPIO
681
- * @wait: wait queue that handles blocking reads of events
682
- * @events: KFIFO for the GPIO events
683
- * @read_lock: mutex lock to protect reads from colliding with adding
684
- * new events to the FIFO
685
- * @timestamp: cache for the timestamp storing it between hardirq
686
- * and IRQ thread, used to bring the timestamp close to the actual
687
- * event
688
- */
689
-struct lineevent_state {
690
- struct gpio_device *gdev;
691
- const char *label;
692
- struct gpio_desc *desc;
693
- u32 eflags;
694
- int irq;
695
- wait_queue_head_t wait;
696
- DECLARE_KFIFO(events, struct gpioevent_data, 16);
697
- struct mutex read_lock;
698
- u64 timestamp;
699
-};
700
-
701
-#define GPIOEVENT_REQUEST_VALID_FLAGS \
702
- (GPIOEVENT_REQUEST_RISING_EDGE | \
703
- GPIOEVENT_REQUEST_FALLING_EDGE)
704
-
705
-static __poll_t lineevent_poll(struct file *filep,
706
- struct poll_table_struct *wait)
707
-{
708
- struct lineevent_state *le = filep->private_data;
709
- __poll_t events = 0;
710
-
711
- poll_wait(filep, &le->wait, wait);
712
-
713
- if (!kfifo_is_empty(&le->events))
714
- events = EPOLLIN | EPOLLRDNORM;
715
-
716
- return events;
717
-}
718
-
719
-
720
-static ssize_t lineevent_read(struct file *filep,
721
- char __user *buf,
722
- size_t count,
723
- loff_t *f_ps)
724
-{
725
- struct lineevent_state *le = filep->private_data;
726
- unsigned int copied;
727
- int ret;
728
-
729
- if (count < sizeof(struct gpioevent_data))
730
- return -EINVAL;
731
-
732
- do {
733
- if (kfifo_is_empty(&le->events)) {
734
- if (filep->f_flags & O_NONBLOCK)
735
- return -EAGAIN;
736
-
737
- ret = wait_event_interruptible(le->wait,
738
- !kfifo_is_empty(&le->events));
739
- if (ret)
740
- return ret;
741
- }
742
-
743
- if (mutex_lock_interruptible(&le->read_lock))
744
- return -ERESTARTSYS;
745
- ret = kfifo_to_user(&le->events, buf, count, &copied);
746
- mutex_unlock(&le->read_lock);
747
-
748
- if (ret)
749
- return ret;
750
-
751
- /*
752
- * If we couldn't read anything from the fifo (a different
753
- * thread might have been faster) we either return -EAGAIN if
754
- * the file descriptor is non-blocking, otherwise we go back to
755
- * sleep and wait for more data to arrive.
756
- */
757
- if (copied == 0 && (filep->f_flags & O_NONBLOCK))
758
- return -EAGAIN;
759
-
760
- } while (copied == 0);
761
-
762
- return copied;
763
-}
764
-
765
-static int lineevent_release(struct inode *inode, struct file *filep)
766
-{
767
- struct lineevent_state *le = filep->private_data;
768
- struct gpio_device *gdev = le->gdev;
769
-
770
- free_irq(le->irq, le);
771
- gpiod_free(le->desc);
772
- kfree(le->label);
773
- kfree(le);
774
- put_device(&gdev->dev);
775
- return 0;
776
-}
777
-
778
-static long lineevent_ioctl(struct file *filep, unsigned int cmd,
779
- unsigned long arg)
780
-{
781
- struct lineevent_state *le = filep->private_data;
782
- void __user *ip = (void __user *)arg;
783
- struct gpiohandle_data ghd;
784
-
785
- /*
786
- * We can get the value for an event line but not set it,
787
- * because it is input by definition.
788
- */
789
- if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
790
- int val;
791
-
792
- memset(&ghd, 0, sizeof(ghd));
793
-
794
- val = gpiod_get_value_cansleep(le->desc);
795
- if (val < 0)
796
- return val;
797
- ghd.values[0] = val;
798
-
799
- if (copy_to_user(ip, &ghd, sizeof(ghd)))
800
- return -EFAULT;
801
-
802
- return 0;
803
- }
804
- return -EINVAL;
805
-}
806
-
807
-#ifdef CONFIG_COMPAT
808
-static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
809
- unsigned long arg)
810
-{
811
- return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
812
-}
813
-#endif
814
-
815
-static const struct file_operations lineevent_fileops = {
816
- .release = lineevent_release,
817
- .read = lineevent_read,
818
- .poll = lineevent_poll,
819
- .owner = THIS_MODULE,
820
- .llseek = noop_llseek,
821
- .unlocked_ioctl = lineevent_ioctl,
822
-#ifdef CONFIG_COMPAT
823
- .compat_ioctl = lineevent_ioctl_compat,
824
-#endif
825
-};
826
-
827
-static irqreturn_t lineevent_irq_thread(int irq, void *p)
828
-{
829
- struct lineevent_state *le = p;
830
- struct gpioevent_data ge;
831
- int ret, level;
832
-
833
- /* Do not leak kernel stack to userspace */
834
- memset(&ge, 0, sizeof(ge));
835
-
836
- /*
837
- * We may be running from a nested threaded interrupt in which case
838
- * we didn't get the timestamp from lineevent_irq_handler().
839
- */
840
- if (!le->timestamp)
841
- ge.timestamp = ktime_get_real_ns();
842
- else
843
- ge.timestamp = le->timestamp;
844
-
845
- level = gpiod_get_value_cansleep(le->desc);
846
-
847
- if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
848
- && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
849
- if (level)
850
- /* Emit low-to-high event */
851
- ge.id = GPIOEVENT_EVENT_RISING_EDGE;
852
- else
853
- /* Emit high-to-low event */
854
- ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
855
- } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
856
- /* Emit low-to-high event */
857
- ge.id = GPIOEVENT_EVENT_RISING_EDGE;
858
- } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
859
- /* Emit high-to-low event */
860
- ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
861
- } else {
862
- return IRQ_NONE;
863
- }
864
-
865
- ret = kfifo_put(&le->events, ge);
866
- if (ret != 0)
867
- wake_up_poll(&le->wait, EPOLLIN);
868
-
869
- return IRQ_HANDLED;
870
-}
871
-
872
-static irqreturn_t lineevent_irq_handler(int irq, void *p)
873
-{
874
- struct lineevent_state *le = p;
875
-
876
- /*
877
- * Just store the timestamp in hardirq context so we get it as
878
- * close in time as possible to the actual event.
879
- */
880
- le->timestamp = ktime_get_real_ns();
881
-
882
- return IRQ_WAKE_THREAD;
883
-}
884
-
885
-static int lineevent_create(struct gpio_device *gdev, void __user *ip)
886
-{
887
- struct gpioevent_request eventreq;
888
- struct lineevent_state *le;
889
- struct gpio_desc *desc;
890
- struct file *file;
891
- u32 offset;
892
- u32 lflags;
893
- u32 eflags;
894
- int fd;
895
- int ret;
896
- int irqflags = 0;
897
-
898
- if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
899
- return -EFAULT;
900
-
901
- le = kzalloc(sizeof(*le), GFP_KERNEL);
902
- if (!le)
903
- return -ENOMEM;
904
- le->gdev = gdev;
905
- get_device(&gdev->dev);
906
-
907
- /* Make sure this is terminated */
908
- eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
909
- if (strlen(eventreq.consumer_label)) {
910
- le->label = kstrdup(eventreq.consumer_label,
911
- GFP_KERNEL);
912
- if (!le->label) {
913
- ret = -ENOMEM;
914
- goto out_free_le;
915
- }
916
- }
917
-
918
- offset = eventreq.lineoffset;
919
- lflags = eventreq.handleflags;
920
- eflags = eventreq.eventflags;
921
-
922
- if (offset >= gdev->ngpio) {
923
- ret = -EINVAL;
924
- goto out_free_label;
925
- }
926
-
927
- /* Return an error if a unknown flag is set */
928
- if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
929
- (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
930
- ret = -EINVAL;
931
- goto out_free_label;
932
- }
933
-
934
- /* This is just wrong: we don't look for events on output lines */
935
- if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
936
- (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
937
- (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
938
- ret = -EINVAL;
939
- goto out_free_label;
940
- }
941
-
942
- desc = &gdev->descs[offset];
943
- ret = gpiod_request(desc, le->label);
944
- if (ret)
945
- goto out_free_label;
946
- le->desc = desc;
947
- le->eflags = eflags;
948
-
949
- if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
950
- set_bit(FLAG_ACTIVE_LOW, &desc->flags);
951
-
952
- ret = gpiod_direction_input(desc);
953
- if (ret)
954
- goto out_free_desc;
955
-
956
- le->irq = gpiod_to_irq(desc);
957
- if (le->irq <= 0) {
958
- ret = -ENODEV;
959
- goto out_free_desc;
960
- }
961
-
962
- if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
963
- irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
964
- IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
965
- if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
966
- irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
967
- IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
968
- irqflags |= IRQF_ONESHOT;
969
- irqflags |= IRQF_SHARED;
970
-
971
- INIT_KFIFO(le->events);
972
- init_waitqueue_head(&le->wait);
973
- mutex_init(&le->read_lock);
974
-
975
- /* Request a thread to read the events */
976
- ret = request_threaded_irq(le->irq,
977
- lineevent_irq_handler,
978
- lineevent_irq_thread,
979
- irqflags,
980
- le->label,
981
- le);
982
- if (ret)
983
- goto out_free_desc;
984
-
985
- fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
986
- if (fd < 0) {
987
- ret = fd;
988
- goto out_free_irq;
989
- }
990
-
991
- file = anon_inode_getfile("gpio-event",
992
- &lineevent_fileops,
993
- le,
994
- O_RDONLY | O_CLOEXEC);
995
- if (IS_ERR(file)) {
996
- ret = PTR_ERR(file);
997
- goto out_put_unused_fd;
998
- }
999
-
1000
- eventreq.fd = fd;
1001
- if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
1002
- /*
1003
- * fput() will trigger the release() callback, so do not go onto
1004
- * the regular error cleanup path here.
1005
- */
1006
- fput(file);
1007
- put_unused_fd(fd);
1008
- return -EFAULT;
1009
- }
1010
-
1011
- fd_install(fd, file);
1012
-
1013
- return 0;
1014
-
1015
-out_put_unused_fd:
1016
- put_unused_fd(fd);
1017
-out_free_irq:
1018
- free_irq(le->irq, le);
1019
-out_free_desc:
1020
- gpiod_free(le->desc);
1021
-out_free_label:
1022
- kfree(le->label);
1023
-out_free_le:
1024
- kfree(le);
1025
- put_device(&gdev->dev);
1026
- return ret;
1027
-}
1028
-
1029
-/*
1030
- * gpio_ioctl() - ioctl handler for the GPIO chardev
1031
- */
1032
-static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1033
-{
1034
- struct gpio_device *gdev = filp->private_data;
1035
- struct gpio_chip *chip = gdev->chip;
1036
- void __user *ip = (void __user *)arg;
1037
-
1038
- /* We fail any subsequent ioctl():s when the chip is gone */
1039
- if (!chip)
1040
- return -ENODEV;
1041
-
1042
- /* Fill in the struct and pass to userspace */
1043
- if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
1044
- struct gpiochip_info chipinfo;
1045
-
1046
- memset(&chipinfo, 0, sizeof(chipinfo));
1047
-
1048
- strncpy(chipinfo.name, dev_name(&gdev->dev),
1049
- sizeof(chipinfo.name));
1050
- chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
1051
- strncpy(chipinfo.label, gdev->label,
1052
- sizeof(chipinfo.label));
1053
- chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
1054
- chipinfo.lines = gdev->ngpio;
1055
- if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1056
- return -EFAULT;
1057
- return 0;
1058
- } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1059
- struct gpioline_info lineinfo;
1060
- struct gpio_desc *desc;
1061
-
1062
- if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1063
- return -EFAULT;
1064
- if (lineinfo.line_offset >= gdev->ngpio)
1065
- return -EINVAL;
1066
-
1067
- desc = &gdev->descs[lineinfo.line_offset];
1068
- if (desc->name) {
1069
- strncpy(lineinfo.name, desc->name,
1070
- sizeof(lineinfo.name));
1071
- lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1072
- } else {
1073
- lineinfo.name[0] = '\0';
1074
- }
1075
- if (desc->label) {
1076
- strncpy(lineinfo.consumer, desc->label,
1077
- sizeof(lineinfo.consumer));
1078
- lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
1079
- } else {
1080
- lineinfo.consumer[0] = '\0';
1081
- }
1082
-
1083
- /*
1084
- * Userspace only need to know that the kernel is using
1085
- * this GPIO so it can't use it.
1086
- */
1087
- lineinfo.flags = 0;
1088
- if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1089
- test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1090
- test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1091
- test_bit(FLAG_EXPORT, &desc->flags) ||
1092
- test_bit(FLAG_SYSFS, &desc->flags))
1093
- lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
1094
- if (test_bit(FLAG_IS_OUT, &desc->flags))
1095
- lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
1096
- if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1097
- lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1098
- if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1099
- lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1100
- GPIOLINE_FLAG_IS_OUT);
1101
- if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1102
- lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1103
- GPIOLINE_FLAG_IS_OUT);
1104
-
1105
- if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1106
- return -EFAULT;
1107
- return 0;
1108
- } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1109
- return linehandle_create(gdev, ip);
1110
- } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1111
- return lineevent_create(gdev, ip);
1112
- }
1113
- return -EINVAL;
1114
-}
1115
-
1116
-#ifdef CONFIG_COMPAT
1117
-static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1118
- unsigned long arg)
1119
-{
1120
- return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1121
-}
1122
-#endif
1123
-
1124
-/**
1125
- * gpio_chrdev_open() - open the chardev for ioctl operations
1126
- * @inode: inode for this chardev
1127
- * @filp: file struct for storing private data
1128
- * Returns 0 on success
1129
- */
1130
-static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1131
-{
1132
- struct gpio_device *gdev = container_of(inode->i_cdev,
1133
- struct gpio_device, chrdev);
1134
-
1135
- /* Fail on open if the backing gpiochip is gone */
1136
- if (!gdev->chip)
1137
- return -ENODEV;
1138
- get_device(&gdev->dev);
1139
- filp->private_data = gdev;
1140
-
1141
- return nonseekable_open(inode, filp);
1142
-}
1143
-
1144
-/**
1145
- * gpio_chrdev_release() - close chardev after ioctl operations
1146
- * @inode: inode for this chardev
1147
- * @filp: file struct for storing private data
1148
- * Returns 0 on success
1149
- */
1150
-static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1151
-{
1152
- struct gpio_device *gdev = container_of(inode->i_cdev,
1153
- struct gpio_device, chrdev);
1154
-
1155
- put_device(&gdev->dev);
1156
- return 0;
1157
-}
1158
-
1159
-
1160
-static const struct file_operations gpio_fileops = {
1161
- .release = gpio_chrdev_release,
1162
- .open = gpio_chrdev_open,
1163
- .owner = THIS_MODULE,
1164
- .llseek = no_llseek,
1165
- .unlocked_ioctl = gpio_ioctl,
1166
-#ifdef CONFIG_COMPAT
1167
- .compat_ioctl = gpio_ioctl_compat,
1168
-#endif
1169
-};
1170
-
1171472 static void gpiodevice_release(struct device *dev)
1172473 {
1173
- struct gpio_device *gdev = dev_get_drvdata(dev);
474
+ struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
475
+ unsigned long flags;
1174476
477
+ spin_lock_irqsave(&gpio_lock, flags);
1175478 list_del(&gdev->list);
1176
- ida_simple_remove(&gpio_ida, gdev->id);
479
+ spin_unlock_irqrestore(&gpio_lock, flags);
480
+
481
+ ida_free(&gpio_ida, gdev->id);
1177482 kfree_const(gdev->label);
1178483 kfree(gdev->descs);
1179484 kfree(gdev);
1180485 }
1181486
487
+#ifdef CONFIG_GPIO_CDEV
488
+#define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
489
+#define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
490
+#else
491
+/*
492
+ * gpiolib_cdev_register() indirectly calls device_add(), which is still
493
+ * required even when cdev is not selected.
494
+ */
495
+#define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
496
+#define gcdev_unregister(gdev) device_del(&(gdev)->dev)
497
+#endif
498
+
1182499 static int gpiochip_setup_dev(struct gpio_device *gdev)
1183500 {
1184
- int status;
501
+ int ret;
1185502
1186
- cdev_init(&gdev->chrdev, &gpio_fileops);
1187
- gdev->chrdev.owner = THIS_MODULE;
1188
- gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
503
+ ret = gcdev_register(gdev, gpio_devt);
504
+ if (ret)
505
+ return ret;
1189506
1190
- status = cdev_device_add(&gdev->chrdev, &gdev->dev);
1191
- if (status)
1192
- return status;
1193
-
1194
- chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1195
- MAJOR(gpio_devt), gdev->id);
1196
-
1197
- status = gpiochip_sysfs_register(gdev);
1198
- if (status)
507
+ ret = gpiochip_sysfs_register(gdev);
508
+ if (ret)
1199509 goto err_remove_device;
1200510
1201511 /* From this point, the .release() function cleans up gpio_device */
1202512 gdev->dev.release = gpiodevice_release;
1203
- pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1204
- __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1205
- dev_name(&gdev->dev), gdev->chip->label ? : "generic");
513
+ dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
514
+ gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
1206515
1207516 return 0;
1208517
1209518 err_remove_device:
1210
- cdev_device_del(&gdev->chrdev, &gdev->dev);
1211
- return status;
519
+ gcdev_unregister(gdev);
520
+ return ret;
1212521 }
1213522
1214
-static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
523
+static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
1215524 {
1216525 struct gpio_desc *desc;
1217526 int rv;
1218527
1219
- desc = gpiochip_get_desc(chip, hog->chip_hwnum);
528
+ desc = gpiochip_get_desc(gc, hog->chip_hwnum);
1220529 if (IS_ERR(desc)) {
1221
- pr_err("%s: unable to get GPIO desc: %ld\n",
1222
- __func__, PTR_ERR(desc));
530
+ chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
531
+ PTR_ERR(desc));
1223532 return;
1224533 }
1225534
....@@ -1228,19 +537,19 @@
1228537
1229538 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1230539 if (rv)
1231
- pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1232
- __func__, chip->label, hog->chip_hwnum, rv);
540
+ gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
541
+ __func__, gc->label, hog->chip_hwnum, rv);
1233542 }
1234543
1235
-static void machine_gpiochip_add(struct gpio_chip *chip)
544
+static void machine_gpiochip_add(struct gpio_chip *gc)
1236545 {
1237546 struct gpiod_hog *hog;
1238547
1239548 mutex_lock(&gpio_machine_hogs_mutex);
1240549
1241550 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1242
- if (!strcmp(chip->label, hog->chip_label))
1243
- gpiochip_machine_hog(chip, hog);
551
+ if (!strcmp(gc->label, hog->chip_label))
552
+ gpiochip_machine_hog(gc, hog);
1244553 }
1245554
1246555 mutex_unlock(&gpio_machine_hogs_mutex);
....@@ -1249,25 +558,27 @@
1249558 static void gpiochip_setup_devs(void)
1250559 {
1251560 struct gpio_device *gdev;
1252
- int err;
561
+ int ret;
1253562
1254563 list_for_each_entry(gdev, &gpio_devices, list) {
1255
- err = gpiochip_setup_dev(gdev);
1256
- if (err)
1257
- pr_err("%s: Failed to initialize gpio device (%d)\n",
1258
- dev_name(&gdev->dev), err);
564
+ ret = gpiochip_setup_dev(gdev);
565
+ if (ret)
566
+ dev_err(&gdev->dev,
567
+ "Failed to initialize gpio device (%d)\n", ret);
1259568 }
1260569 }
1261570
1262
-int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
571
+int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
1263572 struct lock_class_key *lock_key,
1264573 struct lock_class_key *request_key)
1265574 {
575
+ struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
1266576 unsigned long flags;
1267
- int status = 0;
577
+ int ret = 0;
1268578 unsigned i;
1269
- int base = chip->base;
579
+ int base = gc->base;
1270580 struct gpio_device *gdev;
581
+ bool block_gpio_read = false;
1271582
1272583 /*
1273584 * First: allocate and populate the internal stat container, and
....@@ -1277,60 +588,63 @@
1277588 if (!gdev)
1278589 return -ENOMEM;
1279590 gdev->dev.bus = &gpio_bus_type;
1280
- gdev->chip = chip;
1281
- chip->gpiodev = gdev;
1282
- if (chip->parent) {
1283
- gdev->dev.parent = chip->parent;
1284
- gdev->dev.of_node = chip->parent->of_node;
591
+ gdev->chip = gc;
592
+ gc->gpiodev = gdev;
593
+ if (gc->parent) {
594
+ gdev->dev.parent = gc->parent;
595
+ gdev->dev.of_node = gc->parent->of_node;
1285596 }
1286597
1287
-#ifdef CONFIG_OF_GPIO
1288
- /* If the gpiochip has an assigned OF node this takes precedence */
1289
- if (chip->of_node)
1290
- gdev->dev.of_node = chip->of_node;
1291
- else
1292
- chip->of_node = gdev->dev.of_node;
1293
-#endif
598
+ of_gpio_dev_init(gc, gdev);
1294599
1295
- gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
600
+ /*
601
+ * Assign fwnode depending on the result of the previous calls,
602
+ * if none of them succeed, assign it to the parent's one.
603
+ */
604
+ gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
605
+
606
+ gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
1296607 if (gdev->id < 0) {
1297
- status = gdev->id;
608
+ ret = gdev->id;
1298609 goto err_free_gdev;
1299610 }
1300
- dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
611
+
612
+ ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
613
+ if (ret)
614
+ goto err_free_ida;
615
+
1301616 device_initialize(&gdev->dev);
1302
- dev_set_drvdata(&gdev->dev, gdev);
1303
- if (chip->parent && chip->parent->driver)
1304
- gdev->owner = chip->parent->driver->owner;
1305
- else if (chip->owner)
617
+ if (gc->parent && gc->parent->driver)
618
+ gdev->owner = gc->parent->driver->owner;
619
+ else if (gc->owner)
1306620 /* TODO: remove chip->owner */
1307
- gdev->owner = chip->owner;
621
+ gdev->owner = gc->owner;
1308622 else
1309623 gdev->owner = THIS_MODULE;
1310624
1311
- gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
625
+ gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1312626 if (!gdev->descs) {
1313
- status = -ENOMEM;
1314
- goto err_free_ida;
627
+ ret = -ENOMEM;
628
+ goto err_free_dev_name;
1315629 }
1316630
1317
- if (chip->ngpio == 0) {
1318
- chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1319
- status = -EINVAL;
631
+ if (gc->ngpio == 0) {
632
+ chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
633
+ ret = -EINVAL;
1320634 goto err_free_descs;
1321635 }
1322636
1323
- if (chip->ngpio > FASTPATH_NGPIO)
1324
- chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1325
- chip->ngpio, FASTPATH_NGPIO);
637
+ if (gc->ngpio > FASTPATH_NGPIO)
638
+ chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
639
+ gc->ngpio, FASTPATH_NGPIO);
1326640
1327
- gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
641
+ gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
1328642 if (!gdev->label) {
1329
- status = -ENOMEM;
643
+ ret = -ENOMEM;
1330644 goto err_free_descs;
1331645 }
1332646
1333
- gdev->ngpio = chip->ngpio;
647
+ gdev->ngpio = gc->ngpio;
1334648 gdev->data = data;
1335649
1336650 spin_lock_irqsave(&gpio_lock, flags);
....@@ -1343,9 +657,9 @@
1343657 * of the sysfs interface anyways.
1344658 */
1345659 if (base < 0) {
1346
- base = gpiochip_find_base(chip->ngpio);
660
+ base = gpiochip_find_base(gc->ngpio);
1347661 if (base < 0) {
1348
- status = base;
662
+ ret = base;
1349663 spin_unlock_irqrestore(&gpio_lock, flags);
1350664 goto err_free_label;
1351665 }
....@@ -1355,59 +669,80 @@
1355669 * see if anyone makes use of this, else drop this and assign
1356670 * a poison instead.
1357671 */
1358
- chip->base = base;
672
+ gc->base = base;
1359673 }
1360674 gdev->base = base;
1361675
1362
- status = gpiodev_add_to_list(gdev);
1363
- if (status) {
676
+ ret = gpiodev_add_to_list(gdev);
677
+ if (ret) {
1364678 spin_unlock_irqrestore(&gpio_lock, flags);
1365679 goto err_free_label;
1366680 }
1367681
682
+ for (i = 0; i < gc->ngpio; i++)
683
+ gdev->descs[i].gdev = gdev;
684
+
1368685 spin_unlock_irqrestore(&gpio_lock, flags);
1369686
1370
- for (i = 0; i < chip->ngpio; i++) {
1371
- struct gpio_desc *desc = &gdev->descs[i];
1372
-
1373
- desc->gdev = gdev;
1374
-
1375
- /* REVISIT: most hardware initializes GPIOs as inputs (often
1376
- * with pullups enabled) so power usage is minimized. Linux
1377
- * code should set the gpio direction first thing; but until
1378
- * it does, and in case chip->get_direction is not set, we may
1379
- * expose the wrong direction in sysfs.
1380
- */
1381
- desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
1382
- }
687
+ BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
1383688
1384689 #ifdef CONFIG_PINCTRL
1385690 INIT_LIST_HEAD(&gdev->pin_ranges);
1386691 #endif
1387692
1388
- status = gpiochip_set_desc_names(chip);
1389
- if (status)
693
+ if (gc->names)
694
+ ret = gpiochip_set_desc_names(gc);
695
+ else
696
+ ret = devprop_gpiochip_set_names(gc);
697
+ if (ret)
1390698 goto err_remove_from_list;
1391699
1392
- status = gpiochip_irqchip_init_valid_mask(chip);
1393
- if (status)
700
+ ret = gpiochip_alloc_valid_mask(gc);
701
+ if (ret)
1394702 goto err_remove_from_list;
1395703
1396
- status = gpiochip_init_valid_mask(chip);
1397
- if (status)
704
+ ret = of_gpiochip_add(gc);
705
+ if (ret)
706
+ goto err_free_gpiochip_mask;
707
+
708
+ ret = gpiochip_init_valid_mask(gc);
709
+ if (ret)
710
+ goto err_remove_of_chip;
711
+
712
+ trace_android_vh_gpio_block_read(gdev, &block_gpio_read);
713
+ if (!block_gpio_read) {
714
+ for (i = 0; i < gc->ngpio; i++) {
715
+ struct gpio_desc *desc = &gdev->descs[i];
716
+
717
+ if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
718
+ assign_bit(FLAG_IS_OUT,
719
+ &desc->flags, !gc->get_direction(gc, i));
720
+ } else {
721
+ assign_bit(FLAG_IS_OUT,
722
+ &desc->flags, !gc->direction_input);
723
+ }
724
+ }
725
+ }
726
+
727
+ ret = gpiochip_add_pin_ranges(gc);
728
+ if (ret)
729
+ goto err_remove_of_chip;
730
+
731
+ acpi_gpiochip_add(gc);
732
+
733
+ machine_gpiochip_add(gc);
734
+
735
+ ret = gpiochip_irqchip_init_valid_mask(gc);
736
+ if (ret)
737
+ goto err_remove_acpi_chip;
738
+
739
+ ret = gpiochip_irqchip_init_hw(gc);
740
+ if (ret)
741
+ goto err_remove_acpi_chip;
742
+
743
+ ret = gpiochip_add_irqchip(gc, lock_key, request_key);
744
+ if (ret)
1398745 goto err_remove_irqchip_mask;
1399
-
1400
- status = gpiochip_add_irqchip(chip, lock_key, request_key);
1401
- if (status)
1402
- goto err_remove_chip;
1403
-
1404
- status = of_gpiochip_add(chip);
1405
- if (status)
1406
- goto err_remove_chip;
1407
-
1408
- acpi_gpiochip_add(chip);
1409
-
1410
- machine_gpiochip_add(chip);
1411746
1412747 /*
1413748 * By first adding the chardev, and then adding the device,
....@@ -1418,19 +753,24 @@
1418753 * Otherwise, defer until later.
1419754 */
1420755 if (gpiolib_initialized) {
1421
- status = gpiochip_setup_dev(gdev);
1422
- if (status)
1423
- goto err_remove_chip;
756
+ ret = gpiochip_setup_dev(gdev);
757
+ if (ret)
758
+ goto err_remove_irqchip;
1424759 }
1425760 return 0;
1426761
1427
-err_remove_chip:
1428
- acpi_gpiochip_remove(chip);
1429
- gpiochip_free_hogs(chip);
1430
- of_gpiochip_remove(chip);
1431
- gpiochip_free_valid_mask(chip);
762
+err_remove_irqchip:
763
+ gpiochip_irqchip_remove(gc);
1432764 err_remove_irqchip_mask:
1433
- gpiochip_irqchip_free_valid_mask(chip);
765
+ gpiochip_irqchip_free_valid_mask(gc);
766
+err_remove_acpi_chip:
767
+ acpi_gpiochip_remove(gc);
768
+err_remove_of_chip:
769
+ gpiochip_free_hogs(gc);
770
+ of_gpiochip_remove(gc);
771
+err_free_gpiochip_mask:
772
+ gpiochip_remove_pin_ranges(gc);
773
+ gpiochip_free_valid_mask(gc);
1434774 err_remove_from_list:
1435775 spin_lock_irqsave(&gpio_lock, flags);
1436776 list_del(&gdev->list);
....@@ -1439,55 +779,55 @@
1439779 kfree_const(gdev->label);
1440780 err_free_descs:
1441781 kfree(gdev->descs);
782
+err_free_dev_name:
783
+ kfree(dev_name(&gdev->dev));
1442784 err_free_ida:
1443
- ida_simple_remove(&gpio_ida, gdev->id);
785
+ ida_free(&gpio_ida, gdev->id);
1444786 err_free_gdev:
1445787 /* failures here can mean systems won't boot... */
1446788 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1447789 gdev->base, gdev->base + gdev->ngpio - 1,
1448
- chip->label ? : "generic", status);
790
+ gc->label ? : "generic", ret);
1449791 kfree(gdev);
1450
- return status;
792
+ return ret;
1451793 }
1452794 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1453795
1454796 /**
1455797 * gpiochip_get_data() - get per-subdriver data for the chip
1456
- * @chip: GPIO chip
798
+ * @gc: GPIO chip
1457799 *
1458800 * Returns:
1459801 * The per-subdriver data for the chip.
1460802 */
1461
-void *gpiochip_get_data(struct gpio_chip *chip)
803
+void *gpiochip_get_data(struct gpio_chip *gc)
1462804 {
1463
- return chip->gpiodev->data;
805
+ return gc->gpiodev->data;
1464806 }
1465807 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1466808
1467809 /**
1468810 * gpiochip_remove() - unregister a gpio_chip
1469
- * @chip: the chip to unregister
811
+ * @gc: the chip to unregister
1470812 *
1471813 * A gpio_chip with any GPIOs still requested may not be removed.
1472814 */
1473
-void gpiochip_remove(struct gpio_chip *chip)
815
+void gpiochip_remove(struct gpio_chip *gc)
1474816 {
1475
- struct gpio_device *gdev = chip->gpiodev;
1476
- struct gpio_desc *desc;
817
+ struct gpio_device *gdev = gc->gpiodev;
1477818 unsigned long flags;
1478
- unsigned i;
1479
- bool requested = false;
819
+ unsigned int i;
1480820
1481821 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1482822 gpiochip_sysfs_unregister(gdev);
1483
- gpiochip_free_hogs(chip);
823
+ gpiochip_free_hogs(gc);
1484824 /* Numb the device, cancelling all outstanding operations */
1485825 gdev->chip = NULL;
1486
- gpiochip_irqchip_remove(chip);
1487
- acpi_gpiochip_remove(chip);
1488
- gpiochip_remove_pin_ranges(chip);
1489
- of_gpiochip_remove(chip);
1490
- gpiochip_free_valid_mask(chip);
826
+ gpiochip_irqchip_remove(gc);
827
+ acpi_gpiochip_remove(gc);
828
+ of_gpiochip_remove(gc);
829
+ gpiochip_remove_pin_ranges(gc);
830
+ gpiochip_free_valid_mask(gc);
1491831 /*
1492832 * We accept no more calls into the driver from this point, so
1493833 * NULL the driver data pointer
....@@ -1496,13 +836,12 @@
1496836
1497837 spin_lock_irqsave(&gpio_lock, flags);
1498838 for (i = 0; i < gdev->ngpio; i++) {
1499
- desc = &gdev->descs[i];
1500
- if (test_bit(FLAG_REQUESTED, &desc->flags))
1501
- requested = true;
839
+ if (gpiochip_is_requested(gc, i))
840
+ break;
1502841 }
1503842 spin_unlock_irqrestore(&gpio_lock, flags);
1504843
1505
- if (requested)
844
+ if (i != gdev->ngpio)
1506845 dev_crit(&gdev->dev,
1507846 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1508847
....@@ -1512,86 +851,10 @@
1512851 * be removed, else it will be dangling until the last user is
1513852 * gone.
1514853 */
1515
- cdev_device_del(&gdev->chrdev, &gdev->dev);
854
+ gcdev_unregister(gdev);
1516855 put_device(&gdev->dev);
1517856 }
1518857 EXPORT_SYMBOL_GPL(gpiochip_remove);
1519
-
1520
-static void devm_gpio_chip_release(struct device *dev, void *res)
1521
-{
1522
- struct gpio_chip *chip = *(struct gpio_chip **)res;
1523
-
1524
- gpiochip_remove(chip);
1525
-}
1526
-
1527
-static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1528
-
1529
-{
1530
- struct gpio_chip **r = res;
1531
-
1532
- if (!r || !*r) {
1533
- WARN_ON(!r || !*r);
1534
- return 0;
1535
- }
1536
-
1537
- return *r == data;
1538
-}
1539
-
1540
-/**
1541
- * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
1542
- * @dev: the device pointer on which irq_chip belongs to.
1543
- * @chip: the chip to register, with chip->base initialized
1544
- * @data: driver-private data associated with this chip
1545
- *
1546
- * Context: potentially before irqs will work
1547
- *
1548
- * The gpio chip automatically be released when the device is unbound.
1549
- *
1550
- * Returns:
1551
- * A negative errno if the chip can't be registered, such as because the
1552
- * chip->base is invalid or already associated with a different chip.
1553
- * Otherwise it returns zero as a success code.
1554
- */
1555
-int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1556
- void *data)
1557
-{
1558
- struct gpio_chip **ptr;
1559
- int ret;
1560
-
1561
- ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1562
- GFP_KERNEL);
1563
- if (!ptr)
1564
- return -ENOMEM;
1565
-
1566
- ret = gpiochip_add_data(chip, data);
1567
- if (ret < 0) {
1568
- devres_free(ptr);
1569
- return ret;
1570
- }
1571
-
1572
- *ptr = chip;
1573
- devres_add(dev, ptr);
1574
-
1575
- return 0;
1576
-}
1577
-EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1578
-
1579
-/**
1580
- * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1581
- * @dev: device for which which resource was allocated
1582
- * @chip: the chip to remove
1583
- *
1584
- * A gpio_chip with any GPIOs still requested may not be removed.
1585
- */
1586
-void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1587
-{
1588
- int ret;
1589
-
1590
- ret = devres_release(dev, devm_gpio_chip_release,
1591
- devm_gpio_chip_match, chip);
1592
- WARN_ON(ret);
1593
-}
1594
-EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1595858
1596859 /**
1597860 * gpiochip_find() - iterator for locating a specific gpio_chip
....@@ -1605,31 +868,31 @@
1605868 * more gpio_chips.
1606869 */
1607870 struct gpio_chip *gpiochip_find(void *data,
1608
- int (*match)(struct gpio_chip *chip,
871
+ int (*match)(struct gpio_chip *gc,
1609872 void *data))
1610873 {
1611874 struct gpio_device *gdev;
1612
- struct gpio_chip *chip = NULL;
875
+ struct gpio_chip *gc = NULL;
1613876 unsigned long flags;
1614877
1615878 spin_lock_irqsave(&gpio_lock, flags);
1616879 list_for_each_entry(gdev, &gpio_devices, list)
1617880 if (gdev->chip && match(gdev->chip, data)) {
1618
- chip = gdev->chip;
881
+ gc = gdev->chip;
1619882 break;
1620883 }
1621884
1622885 spin_unlock_irqrestore(&gpio_lock, flags);
1623886
1624
- return chip;
887
+ return gc;
1625888 }
1626889 EXPORT_SYMBOL_GPL(gpiochip_find);
1627890
1628
-static int gpiochip_match_name(struct gpio_chip *chip, void *data)
891
+static int gpiochip_match_name(struct gpio_chip *gc, void *data)
1629892 {
1630893 const char *name = data;
1631894
1632
- return !strcmp(chip->label, name);
895
+ return !strcmp(gc->label, name);
1633896 }
1634897
1635898 static struct gpio_chip *find_chip_by_name(const char *name)
....@@ -1643,126 +906,397 @@
1643906 * The following is irqchip helper code for gpiochips.
1644907 */
1645908
1646
-static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
909
+static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1647910 {
1648
- if (!gpiochip->irq.need_valid_mask)
911
+ struct gpio_irq_chip *girq = &gc->irq;
912
+
913
+ if (!girq->init_hw)
1649914 return 0;
1650915
1651
- gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
1652
- if (!gpiochip->irq.valid_mask)
916
+ return girq->init_hw(gc);
917
+}
918
+
919
+static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
920
+{
921
+ struct gpio_irq_chip *girq = &gc->irq;
922
+
923
+ if (!girq->init_valid_mask)
924
+ return 0;
925
+
926
+ girq->valid_mask = gpiochip_allocate_mask(gc);
927
+ if (!girq->valid_mask)
1653928 return -ENOMEM;
929
+
930
+ girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1654931
1655932 return 0;
1656933 }
1657934
1658
-static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
935
+static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1659936 {
1660
- kfree(gpiochip->irq.valid_mask);
1661
- gpiochip->irq.valid_mask = NULL;
937
+ bitmap_free(gc->irq.valid_mask);
938
+ gc->irq.valid_mask = NULL;
1662939 }
1663940
1664
-bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
941
+bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1665942 unsigned int offset)
1666943 {
1667
- if (!gpiochip_line_is_valid(gpiochip, offset))
944
+ if (!gpiochip_line_is_valid(gc, offset))
1668945 return false;
1669946 /* No mask means all valid */
1670
- if (likely(!gpiochip->irq.valid_mask))
947
+ if (likely(!gc->irq.valid_mask))
1671948 return true;
1672
- return test_bit(offset, gpiochip->irq.valid_mask);
949
+ return test_bit(offset, gc->irq.valid_mask);
1673950 }
1674951 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1675952
1676953 /**
1677954 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
1678
- * @gpiochip: the gpiochip to set the irqchip chain to
1679
- * @irqchip: the irqchip to chain to the gpiochip
955
+ * @gc: the gpiochip to set the irqchip chain to
1680956 * @parent_irq: the irq number corresponding to the parent IRQ for this
1681
- * chained irqchip
957
+ * cascaded irqchip
1682958 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1683959 * coming out of the gpiochip. If the interrupt is nested rather than
1684960 * cascaded, pass NULL in this handler argument
1685961 */
1686
-static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1687
- struct irq_chip *irqchip,
962
+static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
1688963 unsigned int parent_irq,
1689964 irq_flow_handler_t parent_handler)
1690965 {
1691
- unsigned int offset;
966
+ struct gpio_irq_chip *girq = &gc->irq;
967
+ struct device *dev = &gc->gpiodev->dev;
1692968
1693
- if (!gpiochip->irq.domain) {
1694
- chip_err(gpiochip, "called %s before setting up irqchip\n",
969
+ if (!girq->domain) {
970
+ chip_err(gc, "called %s before setting up irqchip\n",
1695971 __func__);
1696972 return;
1697973 }
1698974
1699975 if (parent_handler) {
1700
- if (gpiochip->can_sleep) {
1701
- chip_err(gpiochip,
976
+ if (gc->can_sleep) {
977
+ chip_err(gc,
1702978 "you cannot have chained interrupts on a chip that may sleep\n");
1703979 return;
1704980 }
981
+ girq->parents = devm_kcalloc(dev, 1,
982
+ sizeof(*girq->parents),
983
+ GFP_KERNEL);
984
+ if (!girq->parents) {
985
+ chip_err(gc, "out of memory allocating parent IRQ\n");
986
+ return;
987
+ }
988
+ girq->parents[0] = parent_irq;
989
+ girq->num_parents = 1;
1705990 /*
1706991 * The parent irqchip is already using the chip_data for this
1707992 * irqchip, so our callbacks simply use the handler_data.
1708993 */
1709994 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1710
- gpiochip);
1711
-
1712
- gpiochip->irq.parent_irq = parent_irq;
1713
- gpiochip->irq.parents = &gpiochip->irq.parent_irq;
1714
- gpiochip->irq.num_parents = 1;
1715
- }
1716
-
1717
- /* Set the parent IRQ for all affected IRQs */
1718
- for (offset = 0; offset < gpiochip->ngpio; offset++) {
1719
- if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1720
- continue;
1721
- irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
1722
- parent_irq);
995
+ gc);
1723996 }
1724997 }
1725
-
1726
-/**
1727
- * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1728
- * @gpiochip: the gpiochip to set the irqchip chain to
1729
- * @irqchip: the irqchip to chain to the gpiochip
1730
- * @parent_irq: the irq number corresponding to the parent IRQ for this
1731
- * chained irqchip
1732
- * @parent_handler: the parent interrupt handler for the accumulated IRQ
1733
- * coming out of the gpiochip. If the interrupt is nested rather than
1734
- * cascaded, pass NULL in this handler argument
1735
- */
1736
-void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1737
- struct irq_chip *irqchip,
1738
- unsigned int parent_irq,
1739
- irq_flow_handler_t parent_handler)
1740
-{
1741
- if (gpiochip->irq.threaded) {
1742
- chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1743
- return;
1744
- }
1745
-
1746
- gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1747
- parent_handler);
1748
-}
1749
-EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1750998
1751999 /**
17521000 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1753
- * @gpiochip: the gpiochip to set the irqchip nested handler to
1001
+ * @gc: the gpiochip to set the irqchip nested handler to
17541002 * @irqchip: the irqchip to nest to the gpiochip
17551003 * @parent_irq: the irq number corresponding to the parent IRQ for this
17561004 * nested irqchip
17571005 */
1758
-void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1006
+void gpiochip_set_nested_irqchip(struct gpio_chip *gc,
17591007 struct irq_chip *irqchip,
17601008 unsigned int parent_irq)
17611009 {
1762
- gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1763
- NULL);
1010
+ gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL);
17641011 }
17651012 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1013
+
1014
+#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1015
+
1016
+/**
1017
+ * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1018
+ * to a gpiochip
1019
+ * @gc: the gpiochip to set the irqchip hierarchical handler to
1020
+ * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1021
+ * will then percolate up to the parent
1022
+ */
1023
+static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1024
+ struct irq_chip *irqchip)
1025
+{
1026
+ /* DT will deal with mapping each IRQ as we go along */
1027
+ if (is_of_node(gc->irq.fwnode))
1028
+ return;
1029
+
1030
+ /*
1031
+ * This is for legacy and boardfile "irqchip" fwnodes: allocate
1032
+ * irqs upfront instead of dynamically since we don't have the
1033
+ * dynamic type of allocation that hardware description languages
1034
+ * provide. Once all GPIO drivers using board files are gone from
1035
+ * the kernel we can delete this code, but for a transitional period
1036
+ * it is necessary to keep this around.
1037
+ */
1038
+ if (is_fwnode_irqchip(gc->irq.fwnode)) {
1039
+ int i;
1040
+ int ret;
1041
+
1042
+ for (i = 0; i < gc->ngpio; i++) {
1043
+ struct irq_fwspec fwspec;
1044
+ unsigned int parent_hwirq;
1045
+ unsigned int parent_type;
1046
+ struct gpio_irq_chip *girq = &gc->irq;
1047
+
1048
+ /*
1049
+ * We call the child to parent translation function
1050
+ * only to check if the child IRQ is valid or not.
1051
+ * Just pick the rising edge type here as that is what
1052
+ * we likely need to support.
1053
+ */
1054
+ ret = girq->child_to_parent_hwirq(gc, i,
1055
+ IRQ_TYPE_EDGE_RISING,
1056
+ &parent_hwirq,
1057
+ &parent_type);
1058
+ if (ret) {
1059
+ chip_err(gc, "skip set-up on hwirq %d\n",
1060
+ i);
1061
+ continue;
1062
+ }
1063
+
1064
+ fwspec.fwnode = gc->irq.fwnode;
1065
+ /* This is the hwirq for the GPIO line side of things */
1066
+ fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1067
+ /* Just pick something */
1068
+ fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1069
+ fwspec.param_count = 2;
1070
+ ret = __irq_domain_alloc_irqs(gc->irq.domain,
1071
+ /* just pick something */
1072
+ -1,
1073
+ 1,
1074
+ NUMA_NO_NODE,
1075
+ &fwspec,
1076
+ false,
1077
+ NULL);
1078
+ if (ret < 0) {
1079
+ chip_err(gc,
1080
+ "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1081
+ i, parent_hwirq,
1082
+ ret);
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1088
+
1089
+ return;
1090
+}
1091
+
1092
+static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1093
+ struct irq_fwspec *fwspec,
1094
+ unsigned long *hwirq,
1095
+ unsigned int *type)
1096
+{
1097
+ /* We support standard DT translation */
1098
+ if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1099
+ return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1100
+ }
1101
+
1102
+ /* This is for board files and others not using DT */
1103
+ if (is_fwnode_irqchip(fwspec->fwnode)) {
1104
+ int ret;
1105
+
1106
+ ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1107
+ if (ret)
1108
+ return ret;
1109
+ WARN_ON(*type == IRQ_TYPE_NONE);
1110
+ return 0;
1111
+ }
1112
+ return -EINVAL;
1113
+}
1114
+
1115
+static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1116
+ unsigned int irq,
1117
+ unsigned int nr_irqs,
1118
+ void *data)
1119
+{
1120
+ struct gpio_chip *gc = d->host_data;
1121
+ irq_hw_number_t hwirq;
1122
+ unsigned int type = IRQ_TYPE_NONE;
1123
+ struct irq_fwspec *fwspec = data;
1124
+ void *parent_arg;
1125
+ unsigned int parent_hwirq;
1126
+ unsigned int parent_type;
1127
+ struct gpio_irq_chip *girq = &gc->irq;
1128
+ int ret;
1129
+
1130
+ /*
1131
+ * The nr_irqs parameter is always one except for PCI multi-MSI
1132
+ * so this should not happen.
1133
+ */
1134
+ WARN_ON(nr_irqs != 1);
1135
+
1136
+ ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1137
+ if (ret)
1138
+ return ret;
1139
+
1140
+ chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1141
+
1142
+ ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1143
+ &parent_hwirq, &parent_type);
1144
+ if (ret) {
1145
+ chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1146
+ return ret;
1147
+ }
1148
+ chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1149
+
1150
+ /*
1151
+ * We set handle_bad_irq because the .set_type() should
1152
+ * always be invoked and set the right type of handler.
1153
+ */
1154
+ irq_domain_set_info(d,
1155
+ irq,
1156
+ hwirq,
1157
+ gc->irq.chip,
1158
+ gc,
1159
+ girq->handler,
1160
+ NULL, NULL);
1161
+ irq_set_probe(irq);
1162
+
1163
+ /* This parent only handles asserted level IRQs */
1164
+ parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1165
+ if (!parent_arg)
1166
+ return -ENOMEM;
1167
+
1168
+ chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1169
+ irq, parent_hwirq);
1170
+ irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1171
+ ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1172
+ /*
1173
+ * If the parent irqdomain is msi, the interrupts have already
1174
+ * been allocated, so the EEXIST is good.
1175
+ */
1176
+ if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1177
+ ret = 0;
1178
+ if (ret)
1179
+ chip_err(gc,
1180
+ "failed to allocate parent hwirq %d for hwirq %lu\n",
1181
+ parent_hwirq, hwirq);
1182
+
1183
+ kfree(parent_arg);
1184
+ return ret;
1185
+}
1186
+
1187
+static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1188
+ unsigned int offset)
1189
+{
1190
+ return offset;
1191
+}
1192
+
1193
+static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1194
+{
1195
+ ops->activate = gpiochip_irq_domain_activate;
1196
+ ops->deactivate = gpiochip_irq_domain_deactivate;
1197
+ ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1198
+ ops->free = irq_domain_free_irqs_common;
1199
+
1200
+ /*
1201
+ * We only allow overriding the translate() function for
1202
+ * hierarchical chips, and this should only be done if the user
1203
+ * really need something other than 1:1 translation.
1204
+ */
1205
+ if (!ops->translate)
1206
+ ops->translate = gpiochip_hierarchy_irq_domain_translate;
1207
+}
1208
+
1209
+static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1210
+{
1211
+ if (!gc->irq.child_to_parent_hwirq ||
1212
+ !gc->irq.fwnode) {
1213
+ chip_err(gc, "missing irqdomain vital data\n");
1214
+ return -EINVAL;
1215
+ }
1216
+
1217
+ if (!gc->irq.child_offset_to_irq)
1218
+ gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1219
+
1220
+ if (!gc->irq.populate_parent_alloc_arg)
1221
+ gc->irq.populate_parent_alloc_arg =
1222
+ gpiochip_populate_parent_fwspec_twocell;
1223
+
1224
+ gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1225
+
1226
+ gc->irq.domain = irq_domain_create_hierarchy(
1227
+ gc->irq.parent_domain,
1228
+ 0,
1229
+ gc->ngpio,
1230
+ gc->irq.fwnode,
1231
+ &gc->irq.child_irq_domain_ops,
1232
+ gc);
1233
+
1234
+ if (!gc->irq.domain)
1235
+ return -ENOMEM;
1236
+
1237
+ gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1238
+
1239
+ return 0;
1240
+}
1241
+
1242
+static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1243
+{
1244
+ return !!gc->irq.parent_domain;
1245
+}
1246
+
1247
+void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1248
+ unsigned int parent_hwirq,
1249
+ unsigned int parent_type)
1250
+{
1251
+ struct irq_fwspec *fwspec;
1252
+
1253
+ fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1254
+ if (!fwspec)
1255
+ return NULL;
1256
+
1257
+ fwspec->fwnode = gc->irq.parent_domain->fwnode;
1258
+ fwspec->param_count = 2;
1259
+ fwspec->param[0] = parent_hwirq;
1260
+ fwspec->param[1] = parent_type;
1261
+
1262
+ return fwspec;
1263
+}
1264
+EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1265
+
1266
+void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1267
+ unsigned int parent_hwirq,
1268
+ unsigned int parent_type)
1269
+{
1270
+ struct irq_fwspec *fwspec;
1271
+
1272
+ fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1273
+ if (!fwspec)
1274
+ return NULL;
1275
+
1276
+ fwspec->fwnode = gc->irq.parent_domain->fwnode;
1277
+ fwspec->param_count = 4;
1278
+ fwspec->param[0] = 0;
1279
+ fwspec->param[1] = parent_hwirq;
1280
+ fwspec->param[2] = 0;
1281
+ fwspec->param[3] = parent_type;
1282
+
1283
+ return fwspec;
1284
+}
1285
+EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1286
+
1287
+#else
1288
+
1289
+static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1290
+{
1291
+ return -EINVAL;
1292
+}
1293
+
1294
+static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1295
+{
1296
+ return false;
1297
+}
1298
+
1299
+#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
17661300
17671301 /**
17681302 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
....@@ -1777,38 +1311,38 @@
17771311 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
17781312 irq_hw_number_t hwirq)
17791313 {
1780
- struct gpio_chip *chip = d->host_data;
1781
- int err = 0;
1314
+ struct gpio_chip *gc = d->host_data;
1315
+ int ret = 0;
17821316
1783
- if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1317
+ if (!gpiochip_irqchip_irq_valid(gc, hwirq))
17841318 return -ENXIO;
17851319
1786
- irq_set_chip_data(irq, chip);
1320
+ irq_set_chip_data(irq, gc);
17871321 /*
17881322 * This lock class tells lockdep that GPIO irqs are in a different
17891323 * category than their parents, so it won't report false recursion.
17901324 */
1791
- irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
1792
- irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
1325
+ irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1326
+ irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
17931327 /* Chips that use nested thread handlers have them marked */
1794
- if (chip->irq.threaded)
1328
+ if (gc->irq.threaded)
17951329 irq_set_nested_thread(irq, 1);
17961330 irq_set_noprobe(irq);
17971331
1798
- if (chip->irq.num_parents == 1)
1799
- err = irq_set_parent(irq, chip->irq.parents[0]);
1800
- else if (chip->irq.map)
1801
- err = irq_set_parent(irq, chip->irq.map[hwirq]);
1332
+ if (gc->irq.num_parents == 1)
1333
+ ret = irq_set_parent(irq, gc->irq.parents[0]);
1334
+ else if (gc->irq.map)
1335
+ ret = irq_set_parent(irq, gc->irq.map[hwirq]);
18021336
1803
- if (err < 0)
1804
- return err;
1337
+ if (ret < 0)
1338
+ return ret;
18051339
18061340 /*
18071341 * No set-up of the hardware will happen if IRQ_TYPE_NONE
18081342 * is passed as default type.
18091343 */
1810
- if (chip->irq.default_type != IRQ_TYPE_NONE)
1811
- irq_set_irq_type(irq, chip->irq.default_type);
1344
+ if (gc->irq.default_type != IRQ_TYPE_NONE)
1345
+ irq_set_irq_type(irq, gc->irq.default_type);
18121346
18131347 return 0;
18141348 }
....@@ -1816,9 +1350,9 @@
18161350
18171351 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
18181352 {
1819
- struct gpio_chip *chip = d->host_data;
1353
+ struct gpio_chip *gc = d->host_data;
18201354
1821
- if (chip->irq.threaded)
1355
+ if (gc->irq.threaded)
18221356 irq_set_nested_thread(irq, 0);
18231357 irq_set_chip_and_handler(irq, NULL, NULL);
18241358 irq_set_chip_data(irq, NULL);
....@@ -1832,53 +1366,181 @@
18321366 .xlate = irq_domain_xlate_twocell,
18331367 };
18341368
1369
+/*
1370
+ * TODO: move these activate/deactivate in under the hierarchicial
1371
+ * irqchip implementation as static once SPMI and SSBI (all external
1372
+ * users) are phased over.
1373
+ */
1374
+/**
1375
+ * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1376
+ * @domain: The IRQ domain used by this IRQ chip
1377
+ * @data: Outermost irq_data associated with the IRQ
1378
+ * @reserve: If set, only reserve an interrupt vector instead of assigning one
1379
+ *
1380
+ * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1381
+ * used as the activate function for the &struct irq_domain_ops. The host_data
1382
+ * for the IRQ domain must be the &struct gpio_chip.
1383
+ */
1384
+int gpiochip_irq_domain_activate(struct irq_domain *domain,
1385
+ struct irq_data *data, bool reserve)
1386
+{
1387
+ struct gpio_chip *gc = domain->host_data;
1388
+
1389
+ return gpiochip_lock_as_irq(gc, data->hwirq);
1390
+}
1391
+EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1392
+
1393
+/**
1394
+ * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1395
+ * @domain: The IRQ domain used by this IRQ chip
1396
+ * @data: Outermost irq_data associated with the IRQ
1397
+ *
1398
+ * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1399
+ * be used as the deactivate function for the &struct irq_domain_ops. The
1400
+ * host_data for the IRQ domain must be the &struct gpio_chip.
1401
+ */
1402
+void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1403
+ struct irq_data *data)
1404
+{
1405
+ struct gpio_chip *gc = domain->host_data;
1406
+
1407
+ return gpiochip_unlock_as_irq(gc, data->hwirq);
1408
+}
1409
+EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1410
+
1411
+static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset)
1412
+{
1413
+ struct irq_domain *domain = gc->irq.domain;
1414
+
1415
+#ifdef CONFIG_GPIOLIB_IRQCHIP
1416
+ /*
1417
+ * Avoid race condition with other code, which tries to lookup
1418
+ * an IRQ before the irqchip has been properly registered,
1419
+ * i.e. while gpiochip is still being brought up.
1420
+ */
1421
+ if (!gc->irq.initialized)
1422
+ return -EPROBE_DEFER;
1423
+#endif
1424
+
1425
+ if (!gpiochip_irqchip_irq_valid(gc, offset))
1426
+ return -ENXIO;
1427
+
1428
+#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1429
+ if (irq_domain_is_hierarchy(domain)) {
1430
+ struct irq_fwspec spec;
1431
+
1432
+ spec.fwnode = domain->fwnode;
1433
+ spec.param_count = 2;
1434
+ spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1435
+ spec.param[1] = IRQ_TYPE_NONE;
1436
+
1437
+ return irq_create_fwspec_mapping(&spec);
1438
+ }
1439
+#endif
1440
+
1441
+ return irq_create_mapping(domain, offset);
1442
+}
1443
+
18351444 static int gpiochip_irq_reqres(struct irq_data *d)
18361445 {
1837
- struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1838
- int ret;
1446
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
18391447
1840
- if (!try_module_get(chip->gpiodev->owner))
1841
- return -ENODEV;
1842
-
1843
- ret = gpiochip_lock_as_irq(chip, d->hwirq);
1844
- if (ret) {
1845
- chip_err(chip,
1846
- "unable to lock HW IRQ %lu for IRQ\n",
1847
- d->hwirq);
1848
- module_put(chip->gpiodev->owner);
1849
- return ret;
1850
- }
1851
- return 0;
1448
+ return gpiochip_reqres_irq(gc, d->hwirq);
18521449 }
18531450
18541451 static void gpiochip_irq_relres(struct irq_data *d)
18551452 {
1856
- struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1453
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
18571454
1858
- gpiochip_unlock_as_irq(chip, d->hwirq);
1859
- module_put(chip->gpiodev->owner);
1455
+ gpiochip_relres_irq(gc, d->hwirq);
18601456 }
18611457
1862
-static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1458
+static void gpiochip_irq_mask(struct irq_data *d)
18631459 {
1864
- if (!gpiochip_irqchip_irq_valid(chip, offset))
1865
- return -ENXIO;
1460
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
18661461
1867
- return irq_create_mapping(chip->irq.domain, offset);
1462
+ if (gc->irq.irq_mask)
1463
+ gc->irq.irq_mask(d);
1464
+ gpiochip_disable_irq(gc, d->hwirq);
1465
+}
1466
+
1467
+static void gpiochip_irq_unmask(struct irq_data *d)
1468
+{
1469
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1470
+
1471
+ gpiochip_enable_irq(gc, d->hwirq);
1472
+ if (gc->irq.irq_unmask)
1473
+ gc->irq.irq_unmask(d);
1474
+}
1475
+
1476
+static void gpiochip_irq_enable(struct irq_data *d)
1477
+{
1478
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1479
+
1480
+ gpiochip_enable_irq(gc, d->hwirq);
1481
+ gc->irq.irq_enable(d);
1482
+}
1483
+
1484
+static void gpiochip_irq_disable(struct irq_data *d)
1485
+{
1486
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1487
+
1488
+ gc->irq.irq_disable(d);
1489
+ gpiochip_disable_irq(gc, d->hwirq);
1490
+}
1491
+
1492
+static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1493
+{
1494
+ struct irq_chip *irqchip = gc->irq.chip;
1495
+
1496
+ if (!irqchip->irq_request_resources &&
1497
+ !irqchip->irq_release_resources) {
1498
+ irqchip->irq_request_resources = gpiochip_irq_reqres;
1499
+ irqchip->irq_release_resources = gpiochip_irq_relres;
1500
+ }
1501
+ if (WARN_ON(gc->irq.irq_enable))
1502
+ return;
1503
+ /* Check if the irqchip already has this hook... */
1504
+ if (irqchip->irq_enable == gpiochip_irq_enable ||
1505
+ irqchip->irq_mask == gpiochip_irq_mask) {
1506
+ /*
1507
+ * ...and if so, give a gentle warning that this is bad
1508
+ * practice.
1509
+ */
1510
+ chip_info(gc,
1511
+ "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1512
+ return;
1513
+ }
1514
+
1515
+ if (irqchip->irq_disable) {
1516
+ gc->irq.irq_disable = irqchip->irq_disable;
1517
+ irqchip->irq_disable = gpiochip_irq_disable;
1518
+ } else {
1519
+ gc->irq.irq_mask = irqchip->irq_mask;
1520
+ irqchip->irq_mask = gpiochip_irq_mask;
1521
+ }
1522
+
1523
+ if (irqchip->irq_enable) {
1524
+ gc->irq.irq_enable = irqchip->irq_enable;
1525
+ irqchip->irq_enable = gpiochip_irq_enable;
1526
+ } else {
1527
+ gc->irq.irq_unmask = irqchip->irq_unmask;
1528
+ irqchip->irq_unmask = gpiochip_irq_unmask;
1529
+ }
18681530 }
18691531
18701532 /**
18711533 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1872
- * @gpiochip: the GPIO chip to add the IRQ chip to
1534
+ * @gc: the GPIO chip to add the IRQ chip to
18731535 * @lock_key: lockdep class for IRQ lock
18741536 * @request_key: lockdep class for IRQ request
18751537 */
1876
-static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1538
+static int gpiochip_add_irqchip(struct gpio_chip *gc,
18771539 struct lock_class_key *lock_key,
18781540 struct lock_class_key *request_key)
18791541 {
1880
- struct irq_chip *irqchip = gpiochip->irq.chip;
1881
- const struct irq_domain_ops *ops;
1542
+ struct irq_chip *irqchip = gc->irq.chip;
1543
+ const struct irq_domain_ops *ops = NULL;
18821544 struct device_node *np;
18831545 unsigned int type;
18841546 unsigned int i;
....@@ -1886,13 +1548,13 @@
18861548 if (!irqchip)
18871549 return 0;
18881550
1889
- if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1890
- chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
1551
+ if (gc->irq.parent_handler && gc->can_sleep) {
1552
+ chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
18911553 return -EINVAL;
18921554 }
18931555
1894
- np = gpiochip->gpiodev->dev.of_node;
1895
- type = gpiochip->irq.default_type;
1556
+ np = gc->gpiodev->dev.of_node;
1557
+ type = gc->irq.default_type;
18961558
18971559 /*
18981560 * Specifying a default trigger is a terrible idea if DT or ACPI is
....@@ -1903,83 +1565,83 @@
19031565 "%s: Ignoring %u default trigger\n", np->full_name, type))
19041566 type = IRQ_TYPE_NONE;
19051567
1906
- if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1907
- acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1568
+ if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1569
+ acpi_handle_warn(ACPI_HANDLE(gc->parent),
19081570 "Ignoring %u default trigger\n", type);
19091571 type = IRQ_TYPE_NONE;
19101572 }
19111573
1912
-#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1913
- if (!gpiochip->to_irq)
1914
-#endif
1915
- gpiochip->to_irq = gpiochip_to_irq;
1574
+ gc->to_irq = gpiochip_to_irq;
1575
+ gc->irq.default_type = type;
1576
+ gc->irq.lock_key = lock_key;
1577
+ gc->irq.request_key = request_key;
19161578
1917
- gpiochip->irq.default_type = type;
1918
- gpiochip->irq.lock_key = lock_key;
1919
- gpiochip->irq.request_key = request_key;
1579
+ /* If a parent irqdomain is provided, let's build a hierarchy */
1580
+ if (gpiochip_hierarchy_is_hierarchical(gc)) {
1581
+ int ret = gpiochip_hierarchy_add_domain(gc);
1582
+ if (ret)
1583
+ return ret;
1584
+ } else {
1585
+ /* Some drivers provide custom irqdomain ops */
1586
+ if (gc->irq.domain_ops)
1587
+ ops = gc->irq.domain_ops;
19201588
1921
- if (gpiochip->irq.domain_ops)
1922
- ops = gpiochip->irq.domain_ops;
1923
- else
1924
- ops = &gpiochip_domain_ops;
1925
-
1926
-#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1927
- if (gpiochip->irq.parent_domain)
1928
- gpiochip->irq.domain = irq_domain_add_hierarchy(gpiochip->irq.parent_domain,
1929
- 0, gpiochip->ngpio,
1930
- np, ops, gpiochip);
1931
- else
1932
-#endif
1933
- gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
1934
- gpiochip->irq.first,
1935
- ops, gpiochip);
1936
- if (!gpiochip->irq.domain)
1937
- return -EINVAL;
1938
-
1939
- /*
1940
- * It is possible for a driver to override this, but only if the
1941
- * alternative functions are both implemented.
1942
- */
1943
- if (!irqchip->irq_request_resources &&
1944
- !irqchip->irq_release_resources) {
1945
- irqchip->irq_request_resources = gpiochip_irq_reqres;
1946
- irqchip->irq_release_resources = gpiochip_irq_relres;
1589
+ if (!ops)
1590
+ ops = &gpiochip_domain_ops;
1591
+ gc->irq.domain = irq_domain_add_simple(np,
1592
+ gc->ngpio,
1593
+ gc->irq.first,
1594
+ ops, gc);
1595
+ if (!gc->irq.domain)
1596
+ return -EINVAL;
19471597 }
19481598
1949
- if (gpiochip->irq.parent_handler) {
1950
- void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1599
+ if (gc->irq.parent_handler) {
1600
+ void *data = gc->irq.parent_handler_data ?: gc;
19511601
1952
- for (i = 0; i < gpiochip->irq.num_parents; i++) {
1602
+ for (i = 0; i < gc->irq.num_parents; i++) {
19531603 /*
19541604 * The parent IRQ chip is already using the chip_data
19551605 * for this IRQ chip, so our callbacks simply use the
19561606 * handler_data.
19571607 */
1958
- irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1959
- gpiochip->irq.parent_handler,
1608
+ irq_set_chained_handler_and_data(gc->irq.parents[i],
1609
+ gc->irq.parent_handler,
19601610 data);
19611611 }
19621612 }
19631613
1964
- acpi_gpiochip_request_interrupts(gpiochip);
1614
+ gpiochip_set_irq_hooks(gc);
1615
+
1616
+ /*
1617
+ * Using barrier() here to prevent compiler from reordering
1618
+ * gc->irq.initialized before initialization of above
1619
+ * GPIO chip irq members.
1620
+ */
1621
+ barrier();
1622
+
1623
+ gc->irq.initialized = true;
1624
+
1625
+ acpi_gpiochip_request_interrupts(gc);
19651626
19661627 return 0;
19671628 }
19681629
19691630 /**
19701631 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1971
- * @gpiochip: the gpiochip to remove the irqchip from
1632
+ * @gc: the gpiochip to remove the irqchip from
19721633 *
19731634 * This is called only from gpiochip_remove()
19741635 */
1975
-static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1636
+static void gpiochip_irqchip_remove(struct gpio_chip *gc)
19761637 {
1638
+ struct irq_chip *irqchip = gc->irq.chip;
19771639 unsigned int offset;
19781640
1979
- acpi_gpiochip_free_interrupts(gpiochip);
1641
+ acpi_gpiochip_free_interrupts(gc);
19801642
1981
- if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
1982
- struct gpio_irq_chip *irq = &gpiochip->irq;
1643
+ if (irqchip && gc->irq.parent_handler) {
1644
+ struct gpio_irq_chip *irq = &gc->irq;
19831645 unsigned int i;
19841646
19851647 for (i = 0; i < irq->num_parents; i++)
....@@ -1988,32 +1650,40 @@
19881650 }
19891651
19901652 /* Remove all IRQ mappings and delete the domain */
1991
- if (gpiochip->irq.domain) {
1653
+ if (gc->irq.domain) {
19921654 unsigned int irq;
19931655
1994
- for (offset = 0; offset < gpiochip->ngpio; offset++) {
1995
- if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1656
+ for (offset = 0; offset < gc->ngpio; offset++) {
1657
+ if (!gpiochip_irqchip_irq_valid(gc, offset))
19961658 continue;
19971659
1998
- irq = irq_find_mapping(gpiochip->irq.domain, offset);
1660
+ irq = irq_find_mapping(gc->irq.domain, offset);
19991661 irq_dispose_mapping(irq);
20001662 }
20011663
2002
- irq_domain_remove(gpiochip->irq.domain);
1664
+ irq_domain_remove(gc->irq.domain);
20031665 }
20041666
2005
- if (gpiochip->irq.chip) {
2006
- gpiochip->irq.chip->irq_request_resources = NULL;
2007
- gpiochip->irq.chip->irq_release_resources = NULL;
2008
- gpiochip->irq.chip = NULL;
1667
+ if (irqchip) {
1668
+ if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1669
+ irqchip->irq_request_resources = NULL;
1670
+ irqchip->irq_release_resources = NULL;
1671
+ }
1672
+ if (irqchip->irq_enable == gpiochip_irq_enable) {
1673
+ irqchip->irq_enable = gc->irq.irq_enable;
1674
+ irqchip->irq_disable = gc->irq.irq_disable;
1675
+ }
20091676 }
1677
+ gc->irq.irq_enable = NULL;
1678
+ gc->irq.irq_disable = NULL;
1679
+ gc->irq.chip = NULL;
20101680
2011
- gpiochip_irqchip_free_valid_mask(gpiochip);
1681
+ gpiochip_irqchip_free_valid_mask(gc);
20121682 }
20131683
20141684 /**
20151685 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
2016
- * @gpiochip: the gpiochip to add the irqchip to
1686
+ * @gc: the gpiochip to add the irqchip to
20171687 * @irqchip: the irqchip to add to the gpiochip
20181688 * @first_irq: if not dynamically assigned, the base (first) IRQ to
20191689 * allocate gpiochip irqs from
....@@ -2038,7 +1708,7 @@
20381708 * the pins on the gpiochip can generate a unique IRQ. Everything else
20391709 * need to be open coded.
20401710 */
2041
-int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1711
+int gpiochip_irqchip_add_key(struct gpio_chip *gc,
20421712 struct irq_chip *irqchip,
20431713 unsigned int first_irq,
20441714 irq_flow_handler_t handler,
....@@ -2049,23 +1719,23 @@
20491719 {
20501720 struct device_node *of_node;
20511721
2052
- if (!gpiochip || !irqchip)
1722
+ if (!gc || !irqchip)
20531723 return -EINVAL;
20541724
2055
- if (!gpiochip->parent) {
2056
- pr_err("missing gpiochip .dev parent pointer\n");
1725
+ if (!gc->parent) {
1726
+ chip_err(gc, "missing gpiochip .dev parent pointer\n");
20571727 return -EINVAL;
20581728 }
2059
- gpiochip->irq.threaded = threaded;
2060
- of_node = gpiochip->parent->of_node;
1729
+ gc->irq.threaded = threaded;
1730
+ of_node = gc->parent->of_node;
20611731 #ifdef CONFIG_OF_GPIO
20621732 /*
20631733 * If the gpiochip has an assigned OF node this takes precedence
2064
- * FIXME: get rid of this and use gpiochip->parent->of_node
1734
+ * FIXME: get rid of this and use gc->parent->of_node
20651735 * everywhere
20661736 */
2067
- if (gpiochip->of_node)
2068
- of_node = gpiochip->of_node;
1737
+ if (gc->of_node)
1738
+ of_node = gc->of_node;
20691739 #endif
20701740 /*
20711741 * Specifying a default trigger is a terrible idea if DT or ACPI is
....@@ -2075,93 +1745,120 @@
20751745 if (WARN(of_node && type != IRQ_TYPE_NONE,
20761746 "%pOF: Ignoring %d default trigger\n", of_node, type))
20771747 type = IRQ_TYPE_NONE;
2078
- if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2079
- acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1748
+ if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1749
+ acpi_handle_warn(ACPI_HANDLE(gc->parent),
20801750 "Ignoring %d default trigger\n", type);
20811751 type = IRQ_TYPE_NONE;
20821752 }
20831753
2084
- gpiochip->irq.chip = irqchip;
2085
- gpiochip->irq.handler = handler;
2086
- gpiochip->irq.default_type = type;
2087
- gpiochip->to_irq = gpiochip_to_irq;
2088
- gpiochip->irq.lock_key = lock_key;
2089
- gpiochip->irq.request_key = request_key;
2090
- gpiochip->irq.domain = irq_domain_add_simple(of_node,
2091
- gpiochip->ngpio, first_irq,
2092
- &gpiochip_domain_ops, gpiochip);
2093
- if (!gpiochip->irq.domain) {
2094
- gpiochip->irq.chip = NULL;
1754
+ gc->irq.chip = irqchip;
1755
+ gc->irq.handler = handler;
1756
+ gc->irq.default_type = type;
1757
+ gc->to_irq = gpiochip_to_irq;
1758
+ gc->irq.lock_key = lock_key;
1759
+ gc->irq.request_key = request_key;
1760
+ gc->irq.domain = irq_domain_add_simple(of_node,
1761
+ gc->ngpio, first_irq,
1762
+ &gpiochip_domain_ops, gc);
1763
+ if (!gc->irq.domain) {
1764
+ gc->irq.chip = NULL;
20951765 return -EINVAL;
20961766 }
20971767
2098
- /*
2099
- * It is possible for a driver to override this, but only if the
2100
- * alternative functions are both implemented.
2101
- */
2102
- if (!irqchip->irq_request_resources &&
2103
- !irqchip->irq_release_resources) {
2104
- irqchip->irq_request_resources = gpiochip_irq_reqres;
2105
- irqchip->irq_release_resources = gpiochip_irq_relres;
2106
- }
1768
+ gpiochip_set_irq_hooks(gc);
21071769
2108
- acpi_gpiochip_request_interrupts(gpiochip);
1770
+ acpi_gpiochip_request_interrupts(gc);
21091771
21101772 return 0;
21111773 }
21121774 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
21131775
1776
+/**
1777
+ * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1778
+ * @gc: the gpiochip to add the irqchip to
1779
+ * @domain: the irqdomain to add to the gpiochip
1780
+ *
1781
+ * This function adds an IRQ domain to the gpiochip.
1782
+ */
1783
+int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1784
+ struct irq_domain *domain)
1785
+{
1786
+ if (!domain)
1787
+ return -EINVAL;
1788
+
1789
+ gc->to_irq = gpiochip_to_irq;
1790
+ gc->irq.domain = domain;
1791
+
1792
+ return 0;
1793
+}
1794
+EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1795
+
21141796 #else /* CONFIG_GPIOLIB_IRQCHIP */
21151797
2116
-static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1798
+static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
21171799 struct lock_class_key *lock_key,
21181800 struct lock_class_key *request_key)
21191801 {
21201802 return 0;
21211803 }
1804
+static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
21221805
2123
-static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
2124
-static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1806
+static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
21251807 {
21261808 return 0;
21271809 }
2128
-static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1810
+
1811
+static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1812
+{
1813
+ return 0;
1814
+}
1815
+static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
21291816 { }
21301817
21311818 #endif /* CONFIG_GPIOLIB_IRQCHIP */
21321819
21331820 /**
21341821 * gpiochip_generic_request() - request the gpio function for a pin
2135
- * @chip: the gpiochip owning the GPIO
1822
+ * @gc: the gpiochip owning the GPIO
21361823 * @offset: the offset of the GPIO to request for GPIO function
21371824 */
2138
-int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1825
+int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset)
21391826 {
2140
- return pinctrl_gpio_request(chip->gpiodev->base + offset);
1827
+#ifdef CONFIG_PINCTRL
1828
+ if (list_empty(&gc->gpiodev->pin_ranges))
1829
+ return 0;
1830
+#endif
1831
+
1832
+ return pinctrl_gpio_request(gc->gpiodev->base + offset);
21411833 }
21421834 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
21431835
21441836 /**
21451837 * gpiochip_generic_free() - free the gpio function from a pin
2146
- * @chip: the gpiochip to request the gpio function for
1838
+ * @gc: the gpiochip to request the gpio function for
21471839 * @offset: the offset of the GPIO to free from GPIO function
21481840 */
2149
-void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1841
+void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset)
21501842 {
2151
- pinctrl_gpio_free(chip->gpiodev->base + offset);
1843
+#ifdef CONFIG_PINCTRL
1844
+ if (list_empty(&gc->gpiodev->pin_ranges))
1845
+ return;
1846
+#endif
1847
+
1848
+ pinctrl_gpio_free(gc->gpiodev->base + offset);
21521849 }
21531850 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
21541851
21551852 /**
21561853 * gpiochip_generic_config() - apply configuration for a pin
2157
- * @chip: the gpiochip owning the GPIO
1854
+ * @gc: the gpiochip owning the GPIO
21581855 * @offset: the offset of the GPIO to apply the configuration
21591856 * @config: the configuration to be applied
21601857 */
2161
-int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1858
+int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset,
21621859 unsigned long config)
21631860 {
2164
- return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1861
+ return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
21651862 }
21661863 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
21671864
....@@ -2169,7 +1866,7 @@
21691866
21701867 /**
21711868 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2172
- * @chip: the gpiochip to add the range for
1869
+ * @gc: the gpiochip to add the range for
21731870 * @pctldev: the pin controller to map to
21741871 * @gpio_offset: the start offset in the current gpio_chip number space
21751872 * @pin_group: name of the pin group inside the pin controller
....@@ -2179,24 +1876,24 @@
21791876 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
21801877 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
21811878 */
2182
-int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1879
+int gpiochip_add_pingroup_range(struct gpio_chip *gc,
21831880 struct pinctrl_dev *pctldev,
21841881 unsigned int gpio_offset, const char *pin_group)
21851882 {
21861883 struct gpio_pin_range *pin_range;
2187
- struct gpio_device *gdev = chip->gpiodev;
1884
+ struct gpio_device *gdev = gc->gpiodev;
21881885 int ret;
21891886
21901887 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
21911888 if (!pin_range) {
2192
- chip_err(chip, "failed to allocate pin ranges\n");
1889
+ chip_err(gc, "failed to allocate pin ranges\n");
21931890 return -ENOMEM;
21941891 }
21951892
21961893 /* Use local offset as range ID */
21971894 pin_range->range.id = gpio_offset;
2198
- pin_range->range.gc = chip;
2199
- pin_range->range.name = chip->label;
1895
+ pin_range->range.gc = gc;
1896
+ pin_range->range.name = gc->label;
22001897 pin_range->range.base = gdev->base + gpio_offset;
22011898 pin_range->pctldev = pctldev;
22021899
....@@ -2210,7 +1907,7 @@
22101907
22111908 pinctrl_add_gpio_range(pctldev, &pin_range->range);
22121909
2213
- chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1910
+ chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
22141911 gpio_offset, gpio_offset + pin_range->range.npins - 1,
22151912 pinctrl_dev_get_devname(pctldev), pin_group);
22161913
....@@ -2222,7 +1919,7 @@
22221919
22231920 /**
22241921 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2225
- * @chip: the gpiochip to add the range for
1922
+ * @gc: the gpiochip to add the range for
22261923 * @pinctl_name: the dev_name() of the pin controller to map to
22271924 * @gpio_offset: the start offset in the current gpio_chip number space
22281925 * @pin_offset: the start offset in the pin controller number space
....@@ -2237,24 +1934,24 @@
22371934 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
22381935 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
22391936 */
2240
-int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1937
+int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
22411938 unsigned int gpio_offset, unsigned int pin_offset,
22421939 unsigned int npins)
22431940 {
22441941 struct gpio_pin_range *pin_range;
2245
- struct gpio_device *gdev = chip->gpiodev;
1942
+ struct gpio_device *gdev = gc->gpiodev;
22461943 int ret;
22471944
22481945 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
22491946 if (!pin_range) {
2250
- chip_err(chip, "failed to allocate pin ranges\n");
1947
+ chip_err(gc, "failed to allocate pin ranges\n");
22511948 return -ENOMEM;
22521949 }
22531950
22541951 /* Use local offset as range ID */
22551952 pin_range->range.id = gpio_offset;
2256
- pin_range->range.gc = chip;
2257
- pin_range->range.name = chip->label;
1953
+ pin_range->range.gc = gc;
1954
+ pin_range->range.name = gc->label;
22581955 pin_range->range.base = gdev->base + gpio_offset;
22591956 pin_range->range.pin_base = pin_offset;
22601957 pin_range->range.npins = npins;
....@@ -2262,11 +1959,11 @@
22621959 &pin_range->range);
22631960 if (IS_ERR(pin_range->pctldev)) {
22641961 ret = PTR_ERR(pin_range->pctldev);
2265
- chip_err(chip, "could not create pin range\n");
1962
+ chip_err(gc, "could not create pin range\n");
22661963 kfree(pin_range);
22671964 return ret;
22681965 }
2269
- chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1966
+ chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
22701967 gpio_offset, gpio_offset + npins - 1,
22711968 pinctl_name,
22721969 pin_offset, pin_offset + npins - 1);
....@@ -2279,12 +1976,12 @@
22791976
22801977 /**
22811978 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2282
- * @chip: the chip to remove all the mappings for
1979
+ * @gc: the chip to remove all the mappings for
22831980 */
2284
-void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1981
+void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
22851982 {
22861983 struct gpio_pin_range *pin_range, *tmp;
2287
- struct gpio_device *gdev = chip->gpiodev;
1984
+ struct gpio_device *gdev = gc->gpiodev;
22881985
22891986 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
22901987 list_del(&pin_range->node);
....@@ -2303,8 +2000,8 @@
23032000 */
23042001 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
23052002 {
2306
- struct gpio_chip *chip = desc->gdev->chip;
2307
- int status;
2003
+ struct gpio_chip *gc = desc->gdev->chip;
2004
+ int ret;
23082005 unsigned long flags;
23092006 unsigned offset;
23102007
....@@ -2322,39 +2019,39 @@
23222019
23232020 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
23242021 desc_set_label(desc, label ? : "?");
2325
- status = 0;
2022
+ ret = 0;
23262023 } else {
23272024 kfree_const(label);
2328
- status = -EBUSY;
2025
+ ret = -EBUSY;
23292026 goto done;
23302027 }
23312028
2332
- if (chip->request) {
2333
- /* chip->request may sleep */
2029
+ if (gc->request) {
2030
+ /* gc->request may sleep */
23342031 spin_unlock_irqrestore(&gpio_lock, flags);
23352032 offset = gpio_chip_hwgpio(desc);
2336
- if (gpiochip_line_is_valid(chip, offset))
2337
- status = chip->request(chip, offset);
2033
+ if (gpiochip_line_is_valid(gc, offset))
2034
+ ret = gc->request(gc, offset);
23382035 else
2339
- status = -EINVAL;
2036
+ ret = -EINVAL;
23402037 spin_lock_irqsave(&gpio_lock, flags);
23412038
2342
- if (status < 0) {
2039
+ if (ret < 0) {
23432040 desc_set_label(desc, NULL);
23442041 kfree_const(label);
23452042 clear_bit(FLAG_REQUESTED, &desc->flags);
23462043 goto done;
23472044 }
23482045 }
2349
- if (chip->get_direction) {
2350
- /* chip->get_direction may sleep */
2046
+ if (gc->get_direction) {
2047
+ /* gc->get_direction may sleep */
23512048 spin_unlock_irqrestore(&gpio_lock, flags);
23522049 gpiod_get_direction(desc);
23532050 spin_lock_irqsave(&gpio_lock, flags);
23542051 }
23552052 done:
23562053 spin_unlock_irqrestore(&gpio_lock, flags);
2357
- return status;
2054
+ return ret;
23582055 }
23592056
23602057 /*
....@@ -2397,31 +2094,31 @@
23972094
23982095 int gpiod_request(struct gpio_desc *desc, const char *label)
23992096 {
2400
- int status = -EPROBE_DEFER;
2097
+ int ret = -EPROBE_DEFER;
24012098 struct gpio_device *gdev;
24022099
24032100 VALIDATE_DESC(desc);
24042101 gdev = desc->gdev;
24052102
24062103 if (try_module_get(gdev->owner)) {
2407
- status = gpiod_request_commit(desc, label);
2408
- if (status < 0)
2104
+ ret = gpiod_request_commit(desc, label);
2105
+ if (ret < 0)
24092106 module_put(gdev->owner);
24102107 else
24112108 get_device(&gdev->dev);
24122109 }
24132110
2414
- if (status)
2415
- gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2111
+ if (ret)
2112
+ gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
24162113
2417
- return status;
2114
+ return ret;
24182115 }
24192116
24202117 static bool gpiod_free_commit(struct gpio_desc *desc)
24212118 {
24222119 bool ret = false;
24232120 unsigned long flags;
2424
- struct gpio_chip *chip;
2121
+ struct gpio_chip *gc;
24252122
24262123 might_sleep();
24272124
....@@ -2429,12 +2126,12 @@
24292126
24302127 spin_lock_irqsave(&gpio_lock, flags);
24312128
2432
- chip = desc->gdev->chip;
2433
- if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2434
- if (chip->free) {
2129
+ gc = desc->gdev->chip;
2130
+ if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2131
+ if (gc->free) {
24352132 spin_unlock_irqrestore(&gpio_lock, flags);
2436
- might_sleep_if(chip->can_sleep);
2437
- chip->free(chip, gpio_chip_hwgpio(desc));
2133
+ might_sleep_if(gc->can_sleep);
2134
+ gc->free(gc, gpio_chip_hwgpio(desc));
24382135 spin_lock_irqsave(&gpio_lock, flags);
24392136 }
24402137 kfree_const(desc->label);
....@@ -2443,11 +2140,25 @@
24432140 clear_bit(FLAG_REQUESTED, &desc->flags);
24442141 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
24452142 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2143
+ clear_bit(FLAG_PULL_UP, &desc->flags);
2144
+ clear_bit(FLAG_PULL_DOWN, &desc->flags);
2145
+ clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2146
+ clear_bit(FLAG_EDGE_RISING, &desc->flags);
2147
+ clear_bit(FLAG_EDGE_FALLING, &desc->flags);
24462148 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2149
+#ifdef CONFIG_OF_DYNAMIC
2150
+ desc->hog = NULL;
2151
+#endif
2152
+#ifdef CONFIG_GPIO_CDEV
2153
+ WRITE_ONCE(desc->debounce_period_us, 0);
2154
+#endif
24472155 ret = true;
24482156 }
24492157
24502158 spin_unlock_irqrestore(&gpio_lock, flags);
2159
+ blocking_notifier_call_chain(&desc->gdev->notifier,
2160
+ GPIOLINE_CHANGED_RELEASED, desc);
2161
+
24512162 return ret;
24522163 }
24532164
....@@ -2463,7 +2174,7 @@
24632174
24642175 /**
24652176 * gpiochip_is_requested - return string iff signal was requested
2466
- * @chip: controller managing the signal
2177
+ * @gc: controller managing the signal
24672178 * @offset: of signal within controller's 0..(ngpio - 1) range
24682179 *
24692180 * Returns NULL if the GPIO is not currently requested, else a string.
....@@ -2474,14 +2185,16 @@
24742185 * help with diagnostics, and knowing that the signal is used as a GPIO
24752186 * can help avoid accidentally multiplexing it to another controller.
24762187 */
2477
-const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2188
+const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset)
24782189 {
24792190 struct gpio_desc *desc;
24802191
2481
- if (offset >= chip->ngpio)
2192
+ if (offset >= gc->ngpio)
24822193 return NULL;
24832194
2484
- desc = &chip->gpiodev->descs[offset];
2195
+ desc = gpiochip_get_desc(gc, offset);
2196
+ if (IS_ERR(desc))
2197
+ return NULL;
24852198
24862199 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
24872200 return NULL;
....@@ -2491,9 +2204,14 @@
24912204
24922205 /**
24932206 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2494
- * @chip: GPIO chip
2207
+ * @gc: GPIO chip
24952208 * @hwnum: hardware number of the GPIO for which to request the descriptor
24962209 * @label: label for the GPIO
2210
+ * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2211
+ * specify things like line inversion semantics with the machine flags
2212
+ * such as GPIO_OUT_LOW
2213
+ * @dflags: descriptor request flags for this GPIO or 0 if default, this
2214
+ * can be used to specify consumer semantics such as open drain
24972215 *
24982216 * Function allows GPIO chip drivers to request and use their own GPIO
24992217 * descriptors via gpiolib API. Difference to gpiod_request() is that this
....@@ -2505,20 +2223,30 @@
25052223 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
25062224 * code on failure.
25072225 */
2508
-struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2509
- const char *label)
2226
+struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2227
+ unsigned int hwnum,
2228
+ const char *label,
2229
+ enum gpio_lookup_flags lflags,
2230
+ enum gpiod_flags dflags)
25102231 {
2511
- struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2512
- int err;
2232
+ struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2233
+ int ret;
25132234
25142235 if (IS_ERR(desc)) {
2515
- chip_err(chip, "failed to get GPIO descriptor\n");
2236
+ chip_err(gc, "failed to get GPIO descriptor\n");
25162237 return desc;
25172238 }
25182239
2519
- err = gpiod_request_commit(desc, label);
2520
- if (err < 0)
2521
- return ERR_PTR(err);
2240
+ ret = gpiod_request_commit(desc, label);
2241
+ if (ret < 0)
2242
+ return ERR_PTR(ret);
2243
+
2244
+ ret = gpiod_configure_flags(desc, label, lflags, dflags);
2245
+ if (ret) {
2246
+ chip_err(gc, "setup of own GPIO %s failed\n", label);
2247
+ gpiod_free_commit(desc);
2248
+ return ERR_PTR(ret);
2249
+ }
25222250
25232251 return desc;
25242252 }
....@@ -2548,6 +2276,55 @@
25482276 * rely on gpio_request() having been called beforehand.
25492277 */
25502278
2279
+static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2280
+ unsigned long config)
2281
+{
2282
+ if (!gc->set_config)
2283
+ return -ENOTSUPP;
2284
+
2285
+ return gc->set_config(gc, offset, config);
2286
+}
2287
+
2288
+static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2289
+{
2290
+ struct gpio_chip *gc = desc->gdev->chip;
2291
+ unsigned long config;
2292
+ unsigned arg;
2293
+
2294
+ switch (mode) {
2295
+ case PIN_CONFIG_BIAS_PULL_DOWN:
2296
+ case PIN_CONFIG_BIAS_PULL_UP:
2297
+ arg = 1;
2298
+ break;
2299
+
2300
+ default:
2301
+ arg = 0;
2302
+ }
2303
+
2304
+ config = PIN_CONF_PACKED(mode, arg);
2305
+ return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2306
+}
2307
+
2308
+static int gpio_set_bias(struct gpio_desc *desc)
2309
+{
2310
+ int bias = 0;
2311
+ int ret = 0;
2312
+
2313
+ if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2314
+ bias = PIN_CONFIG_BIAS_DISABLE;
2315
+ else if (test_bit(FLAG_PULL_UP, &desc->flags))
2316
+ bias = PIN_CONFIG_BIAS_PULL_UP;
2317
+ else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2318
+ bias = PIN_CONFIG_BIAS_PULL_DOWN;
2319
+
2320
+ if (bias) {
2321
+ ret = gpio_set_config(desc, bias);
2322
+ if (ret != -ENOTSUPP)
2323
+ return ret;
2324
+ }
2325
+ return 0;
2326
+}
2327
+
25512328 /**
25522329 * gpiod_direction_input - set the GPIO direction to input
25532330 * @desc: GPIO to set to input
....@@ -2559,44 +2336,49 @@
25592336 */
25602337 int gpiod_direction_input(struct gpio_desc *desc)
25612338 {
2562
- struct gpio_chip *chip;
2563
- int status = 0;
2339
+ struct gpio_chip *gc;
2340
+ int ret = 0;
25642341
25652342 VALIDATE_DESC(desc);
2566
- chip = desc->gdev->chip;
2343
+ gc = desc->gdev->chip;
25672344
2568
- if (!chip->get && chip->direction_input) {
2345
+ /*
2346
+ * It is legal to have no .get() and .direction_input() specified if
2347
+ * the chip is output-only, but you can't specify .direction_input()
2348
+ * and not support the .get() operation, that doesn't make sense.
2349
+ */
2350
+ if (!gc->get && gc->direction_input) {
25692351 gpiod_warn(desc,
2570
- "%s: missing get() and direction_input() operations\n",
2571
- __func__);
2352
+ "%s: missing get() but have direction_input()\n",
2353
+ __func__);
25722354 return -EIO;
25732355 }
25742356
2575
- if (chip->direction_input) {
2576
- status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2577
- } else if (chip->get_direction &&
2578
- (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2357
+ /*
2358
+ * If we have a .direction_input() callback, things are simple,
2359
+ * just call it. Else we are some input-only chip so try to check the
2360
+ * direction (if .get_direction() is supported) else we silently
2361
+ * assume we are in input mode after this.
2362
+ */
2363
+ if (gc->direction_input) {
2364
+ ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2365
+ } else if (gc->get_direction &&
2366
+ (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
25792367 gpiod_warn(desc,
2580
- "%s: missing direction_input() operation\n",
2581
- __func__);
2368
+ "%s: missing direction_input() operation and line is output\n",
2369
+ __func__);
25822370 return -EIO;
25832371 }
2584
- if (status == 0)
2372
+ if (ret == 0) {
25852373 clear_bit(FLAG_IS_OUT, &desc->flags);
2374
+ ret = gpio_set_bias(desc);
2375
+ }
25862376
2587
- trace_gpio_direction(desc_to_gpio(desc), 1, status);
2377
+ trace_gpio_direction(desc_to_gpio(desc), 1, ret);
25882378
2589
- return status;
2379
+ return ret;
25902380 }
25912381 EXPORT_SYMBOL_GPL(gpiod_direction_input);
2592
-
2593
-static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2594
- enum pin_config_param mode)
2595
-{
2596
- unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2597
-
2598
- return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2599
-}
26002382
26012383 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
26022384 {
....@@ -2604,16 +2386,22 @@
26042386 int val = !!value;
26052387 int ret = 0;
26062388
2389
+ /*
2390
+ * It's OK not to specify .direction_output() if the gpiochip is
2391
+ * output-only, but if there is then not even a .set() operation it
2392
+ * is pretty tricky to drive the output line.
2393
+ */
26072394 if (!gc->set && !gc->direction_output) {
26082395 gpiod_warn(desc,
2609
- "%s: missing set() and direction_output() operations\n",
2610
- __func__);
2396
+ "%s: missing set() and direction_output() operations\n",
2397
+ __func__);
26112398 return -EIO;
26122399 }
26132400
26142401 if (gc->direction_output) {
26152402 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
26162403 } else {
2404
+ /* Check that we are in output mode if we can */
26172405 if (gc->get_direction &&
26182406 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
26192407 gpiod_warn(desc,
....@@ -2621,6 +2409,10 @@
26212409 __func__);
26222410 return -EIO;
26232411 }
2412
+ /*
2413
+ * If we can't actively set the direction, we are some
2414
+ * output-only chip, so just drive the output as desired.
2415
+ */
26242416 gc->set(gc, gpio_chip_hwgpio(desc), val);
26252417 }
26262418
....@@ -2663,7 +2455,6 @@
26632455 */
26642456 int gpiod_direction_output(struct gpio_desc *desc, int value)
26652457 {
2666
- struct gpio_chip *gc;
26672458 int ret;
26682459
26692460 VALIDATE_DESC(desc);
....@@ -2672,19 +2463,18 @@
26722463 else
26732464 value = !!value;
26742465
2675
- /* GPIOs used for IRQs shall not be set as output */
2676
- if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2466
+ /* GPIOs used for enabled IRQs shall not be set as output */
2467
+ if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2468
+ test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
26772469 gpiod_err(desc,
26782470 "%s: tried to set a GPIO tied to an IRQ as output\n",
26792471 __func__);
26802472 return -EIO;
26812473 }
26822474
2683
- gc = desc->gdev->chip;
26842475 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
26852476 /* First see if we can enable open drain in hardware */
2686
- ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2687
- PIN_CONFIG_DRIVE_OPEN_DRAIN);
2477
+ ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
26882478 if (!ret)
26892479 goto set_output_value;
26902480 /* Emulate open drain by not actively driving the line high */
....@@ -2694,8 +2484,7 @@
26942484 }
26952485 }
26962486 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2697
- ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2698
- PIN_CONFIG_DRIVE_OPEN_SOURCE);
2487
+ ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
26992488 if (!ret)
27002489 goto set_output_value;
27012490 /* Emulate open source by not actively driving the line low */
....@@ -2704,11 +2493,13 @@
27042493 goto set_output_flag;
27052494 }
27062495 } else {
2707
- gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2708
- PIN_CONFIG_DRIVE_PUSH_PULL);
2496
+ gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
27092497 }
27102498
27112499 set_output_value:
2500
+ ret = gpio_set_bias(desc);
2501
+ if (ret)
2502
+ return ret;
27122503 return gpiod_direction_output_raw_commit(desc, value);
27132504
27142505 set_output_flag:
....@@ -2725,6 +2516,26 @@
27252516 EXPORT_SYMBOL_GPL(gpiod_direction_output);
27262517
27272518 /**
2519
+ * gpiod_set_config - sets @config for a GPIO
2520
+ * @desc: descriptor of the GPIO for which to set the configuration
2521
+ * @config: Same packed config format as generic pinconf
2522
+ *
2523
+ * Returns:
2524
+ * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2525
+ * configuration.
2526
+ */
2527
+int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2528
+{
2529
+ struct gpio_chip *gc;
2530
+
2531
+ VALIDATE_DESC(desc);
2532
+ gc = desc->gdev->chip;
2533
+
2534
+ return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2535
+}
2536
+EXPORT_SYMBOL_GPL(gpiod_set_config);
2537
+
2538
+/**
27282539 * gpiod_set_debounce - sets @debounce time for a GPIO
27292540 * @desc: descriptor of the GPIO for which to set debounce time
27302541 * @debounce: debounce time in microseconds
....@@ -2735,20 +2546,10 @@
27352546 */
27362547 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
27372548 {
2738
- struct gpio_chip *chip;
2739
- unsigned long config;
2740
-
2741
- VALIDATE_DESC(desc);
2742
- chip = desc->gdev->chip;
2743
- if (!chip->set || !chip->set_config) {
2744
- gpiod_dbg(desc,
2745
- "%s: missing set() or set_config() operations\n",
2746
- __func__);
2747
- return -ENOTSUPP;
2748
- }
2549
+ unsigned long config;
27492550
27502551 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2751
- return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2552
+ return gpiod_set_config(desc, config);
27522553 }
27532554 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
27542555
....@@ -2762,7 +2563,7 @@
27622563 */
27632564 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
27642565 {
2765
- struct gpio_chip *chip;
2566
+ struct gpio_chip *gc;
27662567 unsigned long packed;
27672568 int gpio;
27682569 int rc;
....@@ -2772,20 +2573,17 @@
27722573 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
27732574 * persistence state.
27742575 */
2775
- if (transitory)
2776
- set_bit(FLAG_TRANSITORY, &desc->flags);
2777
- else
2778
- clear_bit(FLAG_TRANSITORY, &desc->flags);
2576
+ assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
27792577
27802578 /* If the driver supports it, set the persistence state now */
2781
- chip = desc->gdev->chip;
2782
- if (!chip->set_config)
2579
+ gc = desc->gdev->chip;
2580
+ if (!gc->set_config)
27832581 return 0;
27842582
27852583 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
27862584 !transitory);
27872585 gpio = gpio_chip_hwgpio(desc);
2788
- rc = chip->set_config(chip, gpio, packed);
2586
+ rc = gpio_do_set_config(gc, gpio, packed);
27892587 if (rc == -ENOTSUPP) {
27902588 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
27912589 gpio);
....@@ -2808,6 +2606,17 @@
28082606 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
28092607 }
28102608 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2609
+
2610
+/**
2611
+ * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2612
+ * @desc: the gpio descriptor to change
2613
+ */
2614
+void gpiod_toggle_active_low(struct gpio_desc *desc)
2615
+{
2616
+ VALIDATE_DESC_VOID(desc);
2617
+ change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2618
+}
2619
+EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
28112620
28122621 /* I/O calls are only valid after configuration completed; the relevant
28132622 * "is this a valid GPIO" error checks should already have been done.
....@@ -2833,28 +2642,28 @@
28332642
28342643 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
28352644 {
2836
- struct gpio_chip *chip;
2645
+ struct gpio_chip *gc;
28372646 int offset;
28382647 int value;
28392648
2840
- chip = desc->gdev->chip;
2649
+ gc = desc->gdev->chip;
28412650 offset = gpio_chip_hwgpio(desc);
2842
- value = chip->get ? chip->get(chip, offset) : -EIO;
2651
+ value = gc->get ? gc->get(gc, offset) : -EIO;
28432652 value = value < 0 ? value : !!value;
28442653 trace_gpio_value(desc_to_gpio(desc), 1, value);
28452654 return value;
28462655 }
28472656
2848
-static int gpio_chip_get_multiple(struct gpio_chip *chip,
2657
+static int gpio_chip_get_multiple(struct gpio_chip *gc,
28492658 unsigned long *mask, unsigned long *bits)
28502659 {
2851
- if (chip->get_multiple) {
2852
- return chip->get_multiple(chip, mask, bits);
2853
- } else if (chip->get) {
2660
+ if (gc->get_multiple) {
2661
+ return gc->get_multiple(gc, mask, bits);
2662
+ } else if (gc->get) {
28542663 int i, value;
28552664
2856
- for_each_set_bit(i, mask, chip->ngpio) {
2857
- value = chip->get(chip, i);
2665
+ for_each_set_bit(i, mask, gc->ngpio) {
2666
+ value = gc->get(gc, i);
28582667 if (value < 0)
28592668 return value;
28602669 __assign_bit(i, bits, value);
....@@ -2867,31 +2676,60 @@
28672676 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
28682677 unsigned int array_size,
28692678 struct gpio_desc **desc_array,
2870
- int *value_array)
2679
+ struct gpio_array *array_info,
2680
+ unsigned long *value_bitmap)
28712681 {
2872
- int i = 0;
2682
+ int ret, i = 0;
2683
+
2684
+ /*
2685
+ * Validate array_info against desc_array and its size.
2686
+ * It should immediately follow desc_array if both
2687
+ * have been obtained from the same gpiod_get_array() call.
2688
+ */
2689
+ if (array_info && array_info->desc == desc_array &&
2690
+ array_size <= array_info->size &&
2691
+ (void *)array_info == desc_array + array_info->size) {
2692
+ if (!can_sleep)
2693
+ WARN_ON(array_info->chip->can_sleep);
2694
+
2695
+ ret = gpio_chip_get_multiple(array_info->chip,
2696
+ array_info->get_mask,
2697
+ value_bitmap);
2698
+ if (ret)
2699
+ return ret;
2700
+
2701
+ if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2702
+ bitmap_xor(value_bitmap, value_bitmap,
2703
+ array_info->invert_mask, array_size);
2704
+
2705
+ i = find_first_zero_bit(array_info->get_mask, array_size);
2706
+ if (i == array_size)
2707
+ return 0;
2708
+ } else {
2709
+ array_info = NULL;
2710
+ }
28732711
28742712 while (i < array_size) {
2875
- struct gpio_chip *chip = desc_array[i]->gdev->chip;
2713
+ struct gpio_chip *gc = desc_array[i]->gdev->chip;
28762714 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
28772715 unsigned long *mask, *bits;
28782716 int first, j, ret;
28792717
2880
- if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2718
+ if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
28812719 mask = fastpath;
28822720 } else {
2883
- mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2721
+ mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
28842722 sizeof(*mask),
28852723 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
28862724 if (!mask)
28872725 return -ENOMEM;
28882726 }
28892727
2890
- bits = mask + BITS_TO_LONGS(chip->ngpio);
2891
- bitmap_zero(mask, chip->ngpio);
2728
+ bits = mask + BITS_TO_LONGS(gc->ngpio);
2729
+ bitmap_zero(mask, gc->ngpio);
28922730
28932731 if (!can_sleep)
2894
- WARN_ON(chip->can_sleep);
2732
+ WARN_ON(gc->can_sleep);
28952733
28962734 /* collect all inputs belonging to the same chip */
28972735 first = i;
....@@ -2901,25 +2739,34 @@
29012739
29022740 __set_bit(hwgpio, mask);
29032741 i++;
2904
- } while ((i < array_size) &&
2905
- (desc_array[i]->gdev->chip == chip));
29062742
2907
- ret = gpio_chip_get_multiple(chip, mask, bits);
2743
+ if (array_info)
2744
+ i = find_next_zero_bit(array_info->get_mask,
2745
+ array_size, i);
2746
+ } while ((i < array_size) &&
2747
+ (desc_array[i]->gdev->chip == gc));
2748
+
2749
+ ret = gpio_chip_get_multiple(gc, mask, bits);
29082750 if (ret) {
29092751 if (mask != fastpath)
29102752 kfree(mask);
29112753 return ret;
29122754 }
29132755
2914
- for (j = first; j < i; j++) {
2756
+ for (j = first; j < i; ) {
29152757 const struct gpio_desc *desc = desc_array[j];
29162758 int hwgpio = gpio_chip_hwgpio(desc);
29172759 int value = test_bit(hwgpio, bits);
29182760
29192761 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
29202762 value = !value;
2921
- value_array[j] = value;
2763
+ __assign_bit(j, value_bitmap, value);
29222764 trace_gpio_value(desc_to_gpio(desc), 1, value);
2765
+ j++;
2766
+
2767
+ if (array_info)
2768
+ j = find_next_zero_bit(array_info->get_mask, i,
2769
+ j);
29232770 }
29242771
29252772 if (mask != fastpath)
....@@ -2935,7 +2782,7 @@
29352782 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
29362783 * its ACTIVE_LOW status, or negative errno on failure.
29372784 *
2938
- * This function should be called from contexts where we cannot sleep, and will
2785
+ * This function can be called from contexts where we cannot sleep, and will
29392786 * complain if the GPIO chip functions potentially sleep.
29402787 */
29412788 int gpiod_get_raw_value(const struct gpio_desc *desc)
....@@ -2954,7 +2801,7 @@
29542801 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
29552802 * account, or negative errno on failure.
29562803 *
2957
- * This function should be called from contexts where we cannot sleep, and will
2804
+ * This function can be called from contexts where we cannot sleep, and will
29582805 * complain if the GPIO chip functions potentially sleep.
29592806 */
29602807 int gpiod_get_value(const struct gpio_desc *desc)
....@@ -2978,46 +2825,54 @@
29782825
29792826 /**
29802827 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2981
- * @array_size: number of elements in the descriptor / value arrays
2828
+ * @array_size: number of elements in the descriptor array / value bitmap
29822829 * @desc_array: array of GPIO descriptors whose values will be read
2983
- * @value_array: array to store the read values
2830
+ * @array_info: information on applicability of fast bitmap processing path
2831
+ * @value_bitmap: bitmap to store the read values
29842832 *
29852833 * Read the raw values of the GPIOs, i.e. the values of the physical lines
29862834 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
29872835 * else an error code.
29882836 *
2989
- * This function should be called from contexts where we cannot sleep,
2837
+ * This function can be called from contexts where we cannot sleep,
29902838 * and it will complain if the GPIO chip functions potentially sleep.
29912839 */
29922840 int gpiod_get_raw_array_value(unsigned int array_size,
2993
- struct gpio_desc **desc_array, int *value_array)
2841
+ struct gpio_desc **desc_array,
2842
+ struct gpio_array *array_info,
2843
+ unsigned long *value_bitmap)
29942844 {
29952845 if (!desc_array)
29962846 return -EINVAL;
29972847 return gpiod_get_array_value_complex(true, false, array_size,
2998
- desc_array, value_array);
2848
+ desc_array, array_info,
2849
+ value_bitmap);
29992850 }
30002851 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
30012852
30022853 /**
30032854 * gpiod_get_array_value() - read values from an array of GPIOs
3004
- * @array_size: number of elements in the descriptor / value arrays
2855
+ * @array_size: number of elements in the descriptor array / value bitmap
30052856 * @desc_array: array of GPIO descriptors whose values will be read
3006
- * @value_array: array to store the read values
2857
+ * @array_info: information on applicability of fast bitmap processing path
2858
+ * @value_bitmap: bitmap to store the read values
30072859 *
30082860 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
30092861 * into account. Return 0 in case of success, else an error code.
30102862 *
3011
- * This function should be called from contexts where we cannot sleep,
2863
+ * This function can be called from contexts where we cannot sleep,
30122864 * and it will complain if the GPIO chip functions potentially sleep.
30132865 */
30142866 int gpiod_get_array_value(unsigned int array_size,
3015
- struct gpio_desc **desc_array, int *value_array)
2867
+ struct gpio_desc **desc_array,
2868
+ struct gpio_array *array_info,
2869
+ unsigned long *value_bitmap)
30162870 {
30172871 if (!desc_array)
30182872 return -EINVAL;
30192873 return gpiod_get_array_value_complex(false, false, array_size,
3020
- desc_array, value_array);
2874
+ desc_array, array_info,
2875
+ value_bitmap);
30212876 }
30222877 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
30232878
....@@ -3028,22 +2883,22 @@
30282883 */
30292884 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
30302885 {
3031
- int err = 0;
3032
- struct gpio_chip *chip = desc->gdev->chip;
2886
+ int ret = 0;
2887
+ struct gpio_chip *gc = desc->gdev->chip;
30332888 int offset = gpio_chip_hwgpio(desc);
30342889
30352890 if (value) {
3036
- err = chip->direction_input(chip, offset);
2891
+ ret = gc->direction_input(gc, offset);
30372892 } else {
3038
- err = chip->direction_output(chip, offset, 0);
3039
- if (!err)
2893
+ ret = gc->direction_output(gc, offset, 0);
2894
+ if (!ret)
30402895 set_bit(FLAG_IS_OUT, &desc->flags);
30412896 }
3042
- trace_gpio_direction(desc_to_gpio(desc), value, err);
3043
- if (err < 0)
2897
+ trace_gpio_direction(desc_to_gpio(desc), value, ret);
2898
+ if (ret < 0)
30442899 gpiod_err(desc,
30452900 "%s: Error in set_value for open drain err %d\n",
3046
- __func__, err);
2901
+ __func__, ret);
30472902 }
30482903
30492904 /*
....@@ -3053,91 +2908,125 @@
30532908 */
30542909 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
30552910 {
3056
- int err = 0;
3057
- struct gpio_chip *chip = desc->gdev->chip;
2911
+ int ret = 0;
2912
+ struct gpio_chip *gc = desc->gdev->chip;
30582913 int offset = gpio_chip_hwgpio(desc);
30592914
30602915 if (value) {
3061
- err = chip->direction_output(chip, offset, 1);
3062
- if (!err)
2916
+ ret = gc->direction_output(gc, offset, 1);
2917
+ if (!ret)
30632918 set_bit(FLAG_IS_OUT, &desc->flags);
30642919 } else {
3065
- err = chip->direction_input(chip, offset);
2920
+ ret = gc->direction_input(gc, offset);
30662921 }
3067
- trace_gpio_direction(desc_to_gpio(desc), !value, err);
3068
- if (err < 0)
2922
+ trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2923
+ if (ret < 0)
30692924 gpiod_err(desc,
30702925 "%s: Error in set_value for open source err %d\n",
3071
- __func__, err);
2926
+ __func__, ret);
30722927 }
30732928
30742929 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
30752930 {
3076
- struct gpio_chip *chip;
2931
+ struct gpio_chip *gc;
30772932
3078
- chip = desc->gdev->chip;
2933
+ gc = desc->gdev->chip;
30792934 trace_gpio_value(desc_to_gpio(desc), 0, value);
3080
- chip->set(chip, gpio_chip_hwgpio(desc), value);
2935
+ gc->set(gc, gpio_chip_hwgpio(desc), value);
30812936 }
30822937
30832938 /*
30842939 * set multiple outputs on the same chip;
30852940 * use the chip's set_multiple function if available;
30862941 * otherwise set the outputs sequentially;
2942
+ * @chip: the GPIO chip we operate on
30872943 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
30882944 * defines which outputs are to be changed
30892945 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
30902946 * defines the values the outputs specified by mask are to be set to
30912947 */
3092
-static void gpio_chip_set_multiple(struct gpio_chip *chip,
2948
+static void gpio_chip_set_multiple(struct gpio_chip *gc,
30932949 unsigned long *mask, unsigned long *bits)
30942950 {
3095
- if (chip->set_multiple) {
3096
- chip->set_multiple(chip, mask, bits);
2951
+ if (gc->set_multiple) {
2952
+ gc->set_multiple(gc, mask, bits);
30972953 } else {
30982954 unsigned int i;
30992955
31002956 /* set outputs if the corresponding mask bit is set */
3101
- for_each_set_bit(i, mask, chip->ngpio)
3102
- chip->set(chip, i, test_bit(i, bits));
2957
+ for_each_set_bit(i, mask, gc->ngpio)
2958
+ gc->set(gc, i, test_bit(i, bits));
31032959 }
31042960 }
31052961
31062962 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3107
- unsigned int array_size,
3108
- struct gpio_desc **desc_array,
3109
- int *value_array)
2963
+ unsigned int array_size,
2964
+ struct gpio_desc **desc_array,
2965
+ struct gpio_array *array_info,
2966
+ unsigned long *value_bitmap)
31102967 {
31112968 int i = 0;
31122969
2970
+ /*
2971
+ * Validate array_info against desc_array and its size.
2972
+ * It should immediately follow desc_array if both
2973
+ * have been obtained from the same gpiod_get_array() call.
2974
+ */
2975
+ if (array_info && array_info->desc == desc_array &&
2976
+ array_size <= array_info->size &&
2977
+ (void *)array_info == desc_array + array_info->size) {
2978
+ if (!can_sleep)
2979
+ WARN_ON(array_info->chip->can_sleep);
2980
+
2981
+ if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2982
+ bitmap_xor(value_bitmap, value_bitmap,
2983
+ array_info->invert_mask, array_size);
2984
+
2985
+ gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2986
+ value_bitmap);
2987
+
2988
+ i = find_first_zero_bit(array_info->set_mask, array_size);
2989
+ if (i == array_size)
2990
+ return 0;
2991
+ } else {
2992
+ array_info = NULL;
2993
+ }
2994
+
31132995 while (i < array_size) {
3114
- struct gpio_chip *chip = desc_array[i]->gdev->chip;
2996
+ struct gpio_chip *gc = desc_array[i]->gdev->chip;
31152997 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
31162998 unsigned long *mask, *bits;
31172999 int count = 0;
31183000
3119
- if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3001
+ if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
31203002 mask = fastpath;
31213003 } else {
3122
- mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3004
+ mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
31233005 sizeof(*mask),
31243006 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
31253007 if (!mask)
31263008 return -ENOMEM;
31273009 }
31283010
3129
- bits = mask + BITS_TO_LONGS(chip->ngpio);
3130
- bitmap_zero(mask, chip->ngpio);
3011
+ bits = mask + BITS_TO_LONGS(gc->ngpio);
3012
+ bitmap_zero(mask, gc->ngpio);
31313013
31323014 if (!can_sleep)
3133
- WARN_ON(chip->can_sleep);
3015
+ WARN_ON(gc->can_sleep);
31343016
31353017 do {
31363018 struct gpio_desc *desc = desc_array[i];
31373019 int hwgpio = gpio_chip_hwgpio(desc);
3138
- int value = value_array[i];
3020
+ int value = test_bit(i, value_bitmap);
31393021
3140
- if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3022
+ /*
3023
+ * Pins applicable for fast input but not for
3024
+ * fast output processing may have been already
3025
+ * inverted inside the fast path, skip them.
3026
+ */
3027
+ if (!raw && !(array_info &&
3028
+ test_bit(i, array_info->invert_mask)) &&
3029
+ test_bit(FLAG_ACTIVE_LOW, &desc->flags))
31413030 value = !value;
31423031 trace_gpio_value(desc_to_gpio(desc), 0, value);
31433032 /*
....@@ -3150,18 +3039,19 @@
31503039 gpio_set_open_source_value_commit(desc, value);
31513040 } else {
31523041 __set_bit(hwgpio, mask);
3153
- if (value)
3154
- __set_bit(hwgpio, bits);
3155
- else
3156
- __clear_bit(hwgpio, bits);
3042
+ __assign_bit(hwgpio, bits, value);
31573043 count++;
31583044 }
31593045 i++;
3046
+
3047
+ if (array_info)
3048
+ i = find_next_zero_bit(array_info->set_mask,
3049
+ array_size, i);
31603050 } while ((i < array_size) &&
3161
- (desc_array[i]->gdev->chip == chip));
3051
+ (desc_array[i]->gdev->chip == gc));
31623052 /* push collected bits to outputs */
31633053 if (count != 0)
3164
- gpio_chip_set_multiple(chip, mask, bits);
3054
+ gpio_chip_set_multiple(gc, mask, bits);
31653055
31663056 if (mask != fastpath)
31673057 kfree(mask);
....@@ -3177,7 +3067,7 @@
31773067 * Set the raw value of the GPIO, i.e. the value of its physical line without
31783068 * regard for its ACTIVE_LOW status.
31793069 *
3180
- * This function should be called from contexts where we cannot sleep, and will
3070
+ * This function can be called from contexts where we cannot sleep, and will
31813071 * complain if the GPIO chip functions potentially sleep.
31823072 */
31833073 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
....@@ -3218,7 +3108,7 @@
32183108 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
32193109 * OPEN_DRAIN and OPEN_SOURCE flags into account.
32203110 *
3221
- * This function should be called from contexts where we cannot sleep, and will
3111
+ * This function can be called from contexts where we cannot sleep, and will
32223112 * complain if the GPIO chip functions potentially sleep.
32233113 */
32243114 void gpiod_set_value(struct gpio_desc *desc, int value)
....@@ -3232,45 +3122,52 @@
32323122
32333123 /**
32343124 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3235
- * @array_size: number of elements in the descriptor / value arrays
3125
+ * @array_size: number of elements in the descriptor array / value bitmap
32363126 * @desc_array: array of GPIO descriptors whose values will be assigned
3237
- * @value_array: array of values to assign
3127
+ * @array_info: information on applicability of fast bitmap processing path
3128
+ * @value_bitmap: bitmap of values to assign
32383129 *
32393130 * Set the raw values of the GPIOs, i.e. the values of the physical lines
32403131 * without regard for their ACTIVE_LOW status.
32413132 *
3242
- * This function should be called from contexts where we cannot sleep, and will
3133
+ * This function can be called from contexts where we cannot sleep, and will
32433134 * complain if the GPIO chip functions potentially sleep.
32443135 */
32453136 int gpiod_set_raw_array_value(unsigned int array_size,
3246
- struct gpio_desc **desc_array, int *value_array)
3137
+ struct gpio_desc **desc_array,
3138
+ struct gpio_array *array_info,
3139
+ unsigned long *value_bitmap)
32473140 {
32483141 if (!desc_array)
32493142 return -EINVAL;
32503143 return gpiod_set_array_value_complex(true, false, array_size,
3251
- desc_array, value_array);
3144
+ desc_array, array_info, value_bitmap);
32523145 }
32533146 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
32543147
32553148 /**
32563149 * gpiod_set_array_value() - assign values to an array of GPIOs
3257
- * @array_size: number of elements in the descriptor / value arrays
3150
+ * @array_size: number of elements in the descriptor array / value bitmap
32583151 * @desc_array: array of GPIO descriptors whose values will be assigned
3259
- * @value_array: array of values to assign
3152
+ * @array_info: information on applicability of fast bitmap processing path
3153
+ * @value_bitmap: bitmap of values to assign
32603154 *
32613155 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
32623156 * into account.
32633157 *
3264
- * This function should be called from contexts where we cannot sleep, and will
3158
+ * This function can be called from contexts where we cannot sleep, and will
32653159 * complain if the GPIO chip functions potentially sleep.
32663160 */
3267
-void gpiod_set_array_value(unsigned int array_size,
3268
- struct gpio_desc **desc_array, int *value_array)
3161
+int gpiod_set_array_value(unsigned int array_size,
3162
+ struct gpio_desc **desc_array,
3163
+ struct gpio_array *array_info,
3164
+ unsigned long *value_bitmap)
32693165 {
32703166 if (!desc_array)
3271
- return;
3272
- gpiod_set_array_value_complex(false, false, array_size, desc_array,
3273
- value_array);
3167
+ return -EINVAL;
3168
+ return gpiod_set_array_value_complex(false, false, array_size,
3169
+ desc_array, array_info,
3170
+ value_bitmap);
32743171 }
32753172 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
32763173
....@@ -3316,7 +3213,7 @@
33163213 */
33173214 int gpiod_to_irq(const struct gpio_desc *desc)
33183215 {
3319
- struct gpio_chip *chip;
3216
+ struct gpio_chip *gc;
33203217 int offset;
33213218
33223219 /*
....@@ -3327,10 +3224,10 @@
33273224 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
33283225 return -EINVAL;
33293226
3330
- chip = desc->gdev->chip;
3227
+ gc = desc->gdev->chip;
33313228 offset = gpio_chip_hwgpio(desc);
3332
- if (chip->to_irq) {
3333
- int retirq = chip->to_irq(chip, offset);
3229
+ if (gc->to_irq) {
3230
+ int retirq = gc->to_irq(gc, offset);
33343231
33353232 /* Zero means NO_IRQ */
33363233 if (!retirq)
....@@ -3338,23 +3235,33 @@
33383235
33393236 return retirq;
33403237 }
3238
+#ifdef CONFIG_GPIOLIB_IRQCHIP
3239
+ if (gc->irq.chip) {
3240
+ /*
3241
+ * Avoid race condition with other code, which tries to lookup
3242
+ * an IRQ before the irqchip has been properly registered,
3243
+ * i.e. while gpiochip is still being brought up.
3244
+ */
3245
+ return -EPROBE_DEFER;
3246
+ }
3247
+#endif
33413248 return -ENXIO;
33423249 }
33433250 EXPORT_SYMBOL_GPL(gpiod_to_irq);
33443251
33453252 /**
33463253 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3347
- * @chip: the chip the GPIO to lock belongs to
3254
+ * @gc: the chip the GPIO to lock belongs to
33483255 * @offset: the offset of the GPIO to lock as IRQ
33493256 *
33503257 * This is used directly by GPIO drivers that want to lock down
33513258 * a certain GPIO line to be used for IRQs.
33523259 */
3353
-int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3260
+int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
33543261 {
33553262 struct gpio_desc *desc;
33563263
3357
- desc = gpiochip_get_desc(chip, offset);
3264
+ desc = gpiochip_get_desc(gc, offset);
33583265 if (IS_ERR(desc))
33593266 return PTR_ERR(desc);
33603267
....@@ -3362,24 +3269,27 @@
33623269 * If it's fast: flush the direction setting if something changed
33633270 * behind our back
33643271 */
3365
- if (!chip->can_sleep && chip->get_direction) {
3272
+ if (!gc->can_sleep && gc->get_direction) {
33663273 int dir = gpiod_get_direction(desc);
33673274
33683275 if (dir < 0) {
3369
- chip_err(chip, "%s: cannot get GPIO direction\n",
3276
+ chip_err(gc, "%s: cannot get GPIO direction\n",
33703277 __func__);
33713278 return dir;
33723279 }
33733280 }
33743281
3375
- if (test_bit(FLAG_IS_OUT, &desc->flags)) {
3376
- chip_err(chip,
3282
+ /* To be valid for IRQ the line needs to be input or open drain */
3283
+ if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3284
+ !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3285
+ chip_err(gc,
33773286 "%s: tried to flag a GPIO set as output for IRQ\n",
33783287 __func__);
33793288 return -EIO;
33803289 }
33813290
33823291 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3292
+ set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
33833293
33843294 /*
33853295 * If the consumer has not set up a label (such as when the
....@@ -3395,21 +3305,22 @@
33953305
33963306 /**
33973307 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3398
- * @chip: the chip the GPIO to lock belongs to
3308
+ * @gc: the chip the GPIO to lock belongs to
33993309 * @offset: the offset of the GPIO to lock as IRQ
34003310 *
34013311 * This is used directly by GPIO drivers that want to indicate
34023312 * that a certain GPIO is no longer used exclusively for IRQ.
34033313 */
3404
-void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3314
+void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
34053315 {
34063316 struct gpio_desc *desc;
34073317
3408
- desc = gpiochip_get_desc(chip, offset);
3318
+ desc = gpiochip_get_desc(gc, offset);
34093319 if (IS_ERR(desc))
34103320 return;
34113321
34123322 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3323
+ clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
34133324
34143325 /* If we only had this marking, erase it */
34153326 if (desc->label && !strcmp(desc->label, "interrupt"))
....@@ -3417,39 +3328,90 @@
34173328 }
34183329 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
34193330
3420
-bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3331
+void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
34213332 {
3422
- if (offset >= chip->ngpio)
3333
+ struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3334
+
3335
+ if (!IS_ERR(desc) &&
3336
+ !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3337
+ clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3338
+}
3339
+EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3340
+
3341
+void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3342
+{
3343
+ struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3344
+
3345
+ if (!IS_ERR(desc) &&
3346
+ !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3347
+ /*
3348
+ * We must not be output when using IRQ UNLESS we are
3349
+ * open drain.
3350
+ */
3351
+ WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3352
+ !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3353
+ set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3354
+ }
3355
+}
3356
+EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3357
+
3358
+bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3359
+{
3360
+ if (offset >= gc->ngpio)
34233361 return false;
34243362
3425
- return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3363
+ return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
34263364 }
34273365 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
34283366
3429
-bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3367
+int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
34303368 {
3431
- if (offset >= chip->ngpio)
3369
+ int ret;
3370
+
3371
+ if (!try_module_get(gc->gpiodev->owner))
3372
+ return -ENODEV;
3373
+
3374
+ ret = gpiochip_lock_as_irq(gc, offset);
3375
+ if (ret) {
3376
+ chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3377
+ module_put(gc->gpiodev->owner);
3378
+ return ret;
3379
+ }
3380
+ return 0;
3381
+}
3382
+EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3383
+
3384
+void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3385
+{
3386
+ gpiochip_unlock_as_irq(gc, offset);
3387
+ module_put(gc->gpiodev->owner);
3388
+}
3389
+EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3390
+
3391
+bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3392
+{
3393
+ if (offset >= gc->ngpio)
34323394 return false;
34333395
3434
- return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3396
+ return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
34353397 }
34363398 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
34373399
3438
-bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3400
+bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
34393401 {
3440
- if (offset >= chip->ngpio)
3402
+ if (offset >= gc->ngpio)
34413403 return false;
34423404
3443
- return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3405
+ return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
34443406 }
34453407 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
34463408
3447
-bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3409
+bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
34483410 {
3449
- if (offset >= chip->ngpio)
3411
+ if (offset >= gc->ngpio)
34503412 return false;
34513413
3452
- return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
3414
+ return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
34533415 }
34543416 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
34553417
....@@ -3498,9 +3460,10 @@
34983460
34993461 /**
35003462 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3501
- * @array_size: number of elements in the descriptor / value arrays
3463
+ * @array_size: number of elements in the descriptor array / value bitmap
35023464 * @desc_array: array of GPIO descriptors whose values will be read
3503
- * @value_array: array to store the read values
3465
+ * @array_info: information on applicability of fast bitmap processing path
3466
+ * @value_bitmap: bitmap to store the read values
35043467 *
35053468 * Read the raw values of the GPIOs, i.e. the values of the physical lines
35063469 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
....@@ -3510,21 +3473,24 @@
35103473 */
35113474 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
35123475 struct gpio_desc **desc_array,
3513
- int *value_array)
3476
+ struct gpio_array *array_info,
3477
+ unsigned long *value_bitmap)
35143478 {
35153479 might_sleep_if(extra_checks);
35163480 if (!desc_array)
35173481 return -EINVAL;
35183482 return gpiod_get_array_value_complex(true, true, array_size,
3519
- desc_array, value_array);
3483
+ desc_array, array_info,
3484
+ value_bitmap);
35203485 }
35213486 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
35223487
35233488 /**
35243489 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3525
- * @array_size: number of elements in the descriptor / value arrays
3490
+ * @array_size: number of elements in the descriptor array / value bitmap
35263491 * @desc_array: array of GPIO descriptors whose values will be read
3527
- * @value_array: array to store the read values
3492
+ * @array_info: information on applicability of fast bitmap processing path
3493
+ * @value_bitmap: bitmap to store the read values
35283494 *
35293495 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
35303496 * into account. Return 0 in case of success, else an error code.
....@@ -3533,13 +3499,15 @@
35333499 */
35343500 int gpiod_get_array_value_cansleep(unsigned int array_size,
35353501 struct gpio_desc **desc_array,
3536
- int *value_array)
3502
+ struct gpio_array *array_info,
3503
+ unsigned long *value_bitmap)
35373504 {
35383505 might_sleep_if(extra_checks);
35393506 if (!desc_array)
35403507 return -EINVAL;
35413508 return gpiod_get_array_value_complex(false, true, array_size,
3542
- desc_array, value_array);
3509
+ desc_array, array_info,
3510
+ value_bitmap);
35433511 }
35443512 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
35453513
....@@ -3581,9 +3549,10 @@
35813549
35823550 /**
35833551 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3584
- * @array_size: number of elements in the descriptor / value arrays
3552
+ * @array_size: number of elements in the descriptor array / value bitmap
35853553 * @desc_array: array of GPIO descriptors whose values will be assigned
3586
- * @value_array: array of values to assign
3554
+ * @array_info: information on applicability of fast bitmap processing path
3555
+ * @value_bitmap: bitmap of values to assign
35873556 *
35883557 * Set the raw values of the GPIOs, i.e. the values of the physical lines
35893558 * without regard for their ACTIVE_LOW status.
....@@ -3591,14 +3560,15 @@
35913560 * This function is to be called from contexts that can sleep.
35923561 */
35933562 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3594
- struct gpio_desc **desc_array,
3595
- int *value_array)
3563
+ struct gpio_desc **desc_array,
3564
+ struct gpio_array *array_info,
3565
+ unsigned long *value_bitmap)
35963566 {
35973567 might_sleep_if(extra_checks);
35983568 if (!desc_array)
35993569 return -EINVAL;
36003570 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3601
- value_array);
3571
+ array_info, value_bitmap);
36023572 }
36033573 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
36043574
....@@ -3621,24 +3591,27 @@
36213591
36223592 /**
36233593 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3624
- * @array_size: number of elements in the descriptor / value arrays
3594
+ * @array_size: number of elements in the descriptor array / value bitmap
36253595 * @desc_array: array of GPIO descriptors whose values will be assigned
3626
- * @value_array: array of values to assign
3596
+ * @array_info: information on applicability of fast bitmap processing path
3597
+ * @value_bitmap: bitmap of values to assign
36273598 *
36283599 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
36293600 * into account.
36303601 *
36313602 * This function is to be called from contexts that can sleep.
36323603 */
3633
-void gpiod_set_array_value_cansleep(unsigned int array_size,
3634
- struct gpio_desc **desc_array,
3635
- int *value_array)
3604
+int gpiod_set_array_value_cansleep(unsigned int array_size,
3605
+ struct gpio_desc **desc_array,
3606
+ struct gpio_array *array_info,
3607
+ unsigned long *value_bitmap)
36363608 {
36373609 might_sleep_if(extra_checks);
36383610 if (!desc_array)
3639
- return;
3640
- gpiod_set_array_value_complex(false, true, array_size, desc_array,
3641
- value_array);
3611
+ return -EINVAL;
3612
+ return gpiod_set_array_value_complex(false, true, array_size,
3613
+ desc_array, array_info,
3614
+ value_bitmap);
36423615 }
36433616 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
36443617
....@@ -3676,7 +3649,7 @@
36763649 */
36773650 void gpiod_add_hogs(struct gpiod_hog *hogs)
36783651 {
3679
- struct gpio_chip *chip;
3652
+ struct gpio_chip *gc;
36803653 struct gpiod_hog *hog;
36813654
36823655 mutex_lock(&gpio_machine_hogs_mutex);
....@@ -3688,9 +3661,9 @@
36883661 * The chip may have been registered earlier, so check if it
36893662 * exists and, if so, try to hog the line now.
36903663 */
3691
- chip = find_chip_by_name(hog->chip_label);
3692
- if (chip)
3693
- gpiochip_machine_hog(chip, hog);
3664
+ gc = find_chip_by_name(hog->chip_label);
3665
+ if (gc)
3666
+ gpiochip_machine_hog(gc, hog);
36943667 }
36953668
36963669 mutex_unlock(&gpio_machine_hogs_mutex);
....@@ -3729,8 +3702,7 @@
37293702 }
37303703
37313704 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3732
- unsigned int idx,
3733
- enum gpio_lookup_flags *flags)
3705
+ unsigned int idx, unsigned long *flags)
37343706 {
37353707 struct gpio_desc *desc = ERR_PTR(-ENOENT);
37363708 struct gpiod_lookup_table *table;
....@@ -3740,8 +3712,8 @@
37403712 if (!table)
37413713 return desc;
37423714
3743
- for (p = &table->table[0]; p->chip_label; p++) {
3744
- struct gpio_chip *chip;
3715
+ for (p = &table->table[0]; p->key; p++) {
3716
+ struct gpio_chip *gc;
37453717
37463718 /* idx must always match exactly */
37473719 if (p->idx != idx)
....@@ -3751,57 +3723,48 @@
37513723 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
37523724 continue;
37533725
3754
- chip = find_chip_by_name(p->chip_label);
3726
+ if (p->chip_hwnum == U16_MAX) {
3727
+ desc = gpio_name_to_desc(p->key);
3728
+ if (desc) {
3729
+ *flags = p->flags;
3730
+ return desc;
3731
+ }
37553732
3756
- if (!chip) {
3733
+ dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3734
+ p->key);
3735
+ return ERR_PTR(-EPROBE_DEFER);
3736
+ }
3737
+
3738
+ gc = find_chip_by_name(p->key);
3739
+
3740
+ if (!gc) {
37573741 /*
37583742 * As the lookup table indicates a chip with
3759
- * p->chip_label should exist, assume it may
3743
+ * p->key should exist, assume it may
37603744 * still appear later and let the interested
37613745 * consumer be probed again or let the Deferred
37623746 * Probe infrastructure handle the error.
37633747 */
37643748 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3765
- p->chip_label);
3749
+ p->key);
37663750 return ERR_PTR(-EPROBE_DEFER);
37673751 }
37683752
3769
- if (chip->ngpio <= p->chip_hwnum) {
3753
+ if (gc->ngpio <= p->chip_hwnum) {
37703754 dev_err(dev,
37713755 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3772
- idx, p->chip_hwnum, chip->ngpio - 1,
3773
- chip->label);
3756
+ idx, p->chip_hwnum, gc->ngpio - 1,
3757
+ gc->label);
37743758 return ERR_PTR(-EINVAL);
37753759 }
37763760
3777
- desc = gpiochip_get_desc(chip, p->chip_hwnum);
3761
+ desc = gpiochip_get_desc(gc, p->chip_hwnum);
37783762 *flags = p->flags;
37793763
37803764 return desc;
37813765 }
37823766
37833767 return desc;
3784
-}
3785
-
3786
-static int dt_gpio_count(struct device *dev, const char *con_id)
3787
-{
3788
- int ret;
3789
- char propname[32];
3790
- unsigned int i;
3791
-
3792
- for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3793
- if (con_id)
3794
- snprintf(propname, sizeof(propname), "%s-%s",
3795
- con_id, gpio_suffixes[i]);
3796
- else
3797
- snprintf(propname, sizeof(propname), "%s",
3798
- gpio_suffixes[i]);
3799
-
3800
- ret = of_gpio_named_count(dev->of_node, propname);
3801
- if (ret > 0)
3802
- break;
3803
- }
3804
- return ret ? ret : -ENOENT;
38053768 }
38063769
38073770 static int platform_gpio_count(struct device *dev, const char *con_id)
....@@ -3814,7 +3777,7 @@
38143777 if (!table)
38153778 return -ENOENT;
38163779
3817
- for (p = &table->table[0]; p->chip_label; p++) {
3780
+ for (p = &table->table[0]; p->key; p++) {
38183781 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
38193782 (!con_id && !p->con_id))
38203783 count++;
....@@ -3824,6 +3787,54 @@
38243787
38253788 return count;
38263789 }
3790
+
3791
+/**
3792
+ * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3793
+ * @fwnode: handle of the firmware node
3794
+ * @con_id: function within the GPIO consumer
3795
+ * @index: index of the GPIO to obtain for the consumer
3796
+ * @flags: GPIO initialization flags
3797
+ * @label: label to attach to the requested GPIO
3798
+ *
3799
+ * This function can be used for drivers that get their configuration
3800
+ * from opaque firmware.
3801
+ *
3802
+ * The function properly finds the corresponding GPIO using whatever is the
3803
+ * underlying firmware interface and then makes sure that the GPIO
3804
+ * descriptor is requested before it is returned to the caller.
3805
+ *
3806
+ * Returns:
3807
+ * On successful request the GPIO pin is configured in accordance with
3808
+ * provided @flags.
3809
+ *
3810
+ * In case of error an ERR_PTR() is returned.
3811
+ */
3812
+struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3813
+ const char *con_id, int index,
3814
+ enum gpiod_flags flags,
3815
+ const char *label)
3816
+{
3817
+ struct gpio_desc *desc;
3818
+ char prop_name[32]; /* 32 is max size of property name */
3819
+ unsigned int i;
3820
+
3821
+ for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3822
+ if (con_id)
3823
+ snprintf(prop_name, sizeof(prop_name), "%s-%s",
3824
+ con_id, gpio_suffixes[i]);
3825
+ else
3826
+ snprintf(prop_name, sizeof(prop_name), "%s",
3827
+ gpio_suffixes[i]);
3828
+
3829
+ desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3830
+ label);
3831
+ if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
3832
+ break;
3833
+ }
3834
+
3835
+ return desc;
3836
+}
3837
+EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
38273838
38283839 /**
38293840 * gpiod_count - return the number of GPIOs associated with a device / function
....@@ -3836,7 +3847,7 @@
38363847 int count = -ENOENT;
38373848
38383849 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3839
- count = dt_gpio_count(dev, con_id);
3850
+ count = of_gpio_get_count(dev, con_id);
38403851 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
38413852 count = acpi_gpio_count(dev, con_id);
38423853
....@@ -3887,8 +3898,8 @@
38873898 * gpiod_configure_flags - helper function to configure a given GPIO
38883899 * @desc: gpio whose value will be assigned
38893900 * @con_id: function within the GPIO consumer
3890
- * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3891
- * of_get_gpio_hog()
3901
+ * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3902
+ * of_find_gpio() or of_get_gpio_hog()
38923903 * @dflags: gpiod_flags - optional GPIO initialization flags
38933904 *
38943905 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
....@@ -3898,7 +3909,7 @@
38983909 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
38993910 unsigned long lflags, enum gpiod_flags dflags)
39003911 {
3901
- int status;
3912
+ int ret;
39023913
39033914 if (lflags & GPIO_ACTIVE_LOW)
39043915 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
....@@ -3920,24 +3931,35 @@
39203931 if (lflags & GPIO_OPEN_SOURCE)
39213932 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
39223933
3923
- status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3924
- if (status < 0)
3925
- return status;
3934
+ if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3935
+ gpiod_err(desc,
3936
+ "both pull-up and pull-down enabled, invalid configuration\n");
3937
+ return -EINVAL;
3938
+ }
3939
+
3940
+ if (lflags & GPIO_PULL_UP)
3941
+ set_bit(FLAG_PULL_UP, &desc->flags);
3942
+ else if (lflags & GPIO_PULL_DOWN)
3943
+ set_bit(FLAG_PULL_DOWN, &desc->flags);
3944
+
3945
+ ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3946
+ if (ret < 0)
3947
+ return ret;
39263948
39273949 /* No particular flag request, return here... */
39283950 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3929
- pr_debug("no flags found for %s\n", con_id);
3951
+ gpiod_dbg(desc, "no flags found for %s\n", con_id);
39303952 return 0;
39313953 }
39323954
39333955 /* Process flags */
39343956 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3935
- status = gpiod_direction_output(desc,
3957
+ ret = gpiod_direction_output(desc,
39363958 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
39373959 else
3938
- status = gpiod_direction_input(desc);
3960
+ ret = gpiod_direction_input(desc);
39393961
3940
- return status;
3962
+ return ret;
39413963 }
39423964
39433965 /**
....@@ -3959,9 +3981,9 @@
39593981 unsigned int idx,
39603982 enum gpiod_flags flags)
39613983 {
3984
+ unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
39623985 struct gpio_desc *desc = NULL;
3963
- int status;
3964
- enum gpio_lookup_flags lookupflags = 0;
3986
+ int ret;
39653987 /* Maybe we have a device name, maybe not */
39663988 const char *devname = dev ? dev_name(dev) : "?";
39673989
....@@ -3996,91 +4018,38 @@
39964018 * If a connection label was passed use that, else attempt to use
39974019 * the device name as label
39984020 */
3999
- status = gpiod_request(desc, con_id ? con_id : devname);
4000
- if (status < 0)
4001
- return ERR_PTR(status);
4021
+ ret = gpiod_request(desc, con_id ? con_id : devname);
4022
+ if (ret < 0) {
4023
+ if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
4024
+ /*
4025
+ * This happens when there are several consumers for
4026
+ * the same GPIO line: we just return here without
4027
+ * further initialization. It is a bit if a hack.
4028
+ * This is necessary to support fixed regulators.
4029
+ *
4030
+ * FIXME: Make this more sane and safe.
4031
+ */
4032
+ dev_info(dev, "nonexclusive access to GPIO for %s\n",
4033
+ con_id ? con_id : devname);
4034
+ return desc;
4035
+ } else {
4036
+ return ERR_PTR(ret);
4037
+ }
4038
+ }
40024039
4003
- status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4004
- if (status < 0) {
4040
+ ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4041
+ if (ret < 0) {
40054042 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
40064043 gpiod_put(desc);
4007
- return ERR_PTR(status);
4044
+ return ERR_PTR(ret);
40084045 }
4046
+
4047
+ blocking_notifier_call_chain(&desc->gdev->notifier,
4048
+ GPIOLINE_CHANGED_REQUESTED, desc);
40094049
40104050 return desc;
40114051 }
40124052 EXPORT_SYMBOL_GPL(gpiod_get_index);
4013
-
4014
-/**
4015
- * gpiod_get_from_of_node() - obtain a GPIO from an OF node
4016
- * @node: handle of the OF node
4017
- * @propname: name of the DT property representing the GPIO
4018
- * @index: index of the GPIO to obtain for the consumer
4019
- * @dflags: GPIO initialization flags
4020
- * @label: label to attach to the requested GPIO
4021
- *
4022
- * Returns:
4023
- * On successful request the GPIO pin is configured in accordance with
4024
- * provided @dflags. If the node does not have the requested GPIO
4025
- * property, NULL is returned.
4026
- *
4027
- * In case of error an ERR_PTR() is returned.
4028
- */
4029
-struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
4030
- const char *propname, int index,
4031
- enum gpiod_flags dflags,
4032
- const char *label)
4033
-{
4034
- struct gpio_desc *desc;
4035
- unsigned long lflags = 0;
4036
- enum of_gpio_flags flags;
4037
- bool active_low = false;
4038
- bool single_ended = false;
4039
- bool open_drain = false;
4040
- bool transitory = false;
4041
- int ret;
4042
-
4043
- desc = of_get_named_gpiod_flags(node, propname,
4044
- index, &flags);
4045
-
4046
- if (!desc || IS_ERR(desc)) {
4047
- /* If it is not there, just return NULL */
4048
- if (PTR_ERR(desc) == -ENOENT)
4049
- return NULL;
4050
- return desc;
4051
- }
4052
-
4053
- active_low = flags & OF_GPIO_ACTIVE_LOW;
4054
- single_ended = flags & OF_GPIO_SINGLE_ENDED;
4055
- open_drain = flags & OF_GPIO_OPEN_DRAIN;
4056
- transitory = flags & OF_GPIO_TRANSITORY;
4057
-
4058
- ret = gpiod_request(desc, label);
4059
- if (ret)
4060
- return ERR_PTR(ret);
4061
-
4062
- if (active_low)
4063
- lflags |= GPIO_ACTIVE_LOW;
4064
-
4065
- if (single_ended) {
4066
- if (open_drain)
4067
- lflags |= GPIO_OPEN_DRAIN;
4068
- else
4069
- lflags |= GPIO_OPEN_SOURCE;
4070
- }
4071
-
4072
- if (transitory)
4073
- lflags |= GPIO_TRANSITORY;
4074
-
4075
- ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4076
- if (ret < 0) {
4077
- gpiod_put(desc);
4078
- return ERR_PTR(ret);
4079
- }
4080
-
4081
- return desc;
4082
-}
4083
-EXPORT_SYMBOL(gpiod_get_from_of_node);
40844053
40854054 /**
40864055 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
....@@ -4108,8 +4077,8 @@
41084077 enum gpiod_flags dflags,
41094078 const char *label)
41104079 {
4080
+ unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
41114081 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4112
- unsigned long lflags = 0;
41134082 int ret;
41144083
41154084 if (!fwnode)
....@@ -4129,9 +4098,7 @@
41294098 return desc;
41304099
41314100 acpi_gpio_update_gpiod_flags(&dflags, &info);
4132
-
4133
- if (info.polarity == GPIO_ACTIVE_LOW)
4134
- lflags |= GPIO_ACTIVE_LOW;
4101
+ acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
41354102 }
41364103
41374104 /* Currently only ACPI takes this path */
....@@ -4144,6 +4111,9 @@
41444111 gpiod_put(desc);
41454112 return ERR_PTR(ret);
41464113 }
4114
+
4115
+ blocking_notifier_call_chain(&desc->gdev->notifier,
4116
+ GPIOLINE_CHANGED_REQUESTED, desc);
41474117
41484118 return desc;
41494119 }
....@@ -4182,62 +4152,52 @@
41824152 * gpiod_hog - Hog the specified GPIO desc given the provided flags
41834153 * @desc: gpio whose value will be assigned
41844154 * @name: gpio line name
4185
- * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4186
- * of_get_gpio_hog()
4155
+ * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4156
+ * of_find_gpio() or of_get_gpio_hog()
41874157 * @dflags: gpiod_flags - optional GPIO initialization flags
41884158 */
41894159 int gpiod_hog(struct gpio_desc *desc, const char *name,
41904160 unsigned long lflags, enum gpiod_flags dflags)
41914161 {
4192
- struct gpio_chip *chip;
4162
+ struct gpio_chip *gc;
41934163 struct gpio_desc *local_desc;
41944164 int hwnum;
4195
- int status;
4165
+ int ret;
41964166
4197
- chip = gpiod_to_chip(desc);
4167
+ gc = gpiod_to_chip(desc);
41984168 hwnum = gpio_chip_hwgpio(desc);
41994169
4200
- local_desc = gpiochip_request_own_desc(chip, hwnum, name);
4170
+ local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4171
+ lflags, dflags);
42014172 if (IS_ERR(local_desc)) {
4202
- status = PTR_ERR(local_desc);
4173
+ ret = PTR_ERR(local_desc);
42034174 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4204
- name, chip->label, hwnum, status);
4205
- return status;
4206
- }
4207
-
4208
- status = gpiod_configure_flags(desc, name, lflags, dflags);
4209
- if (status < 0) {
4210
- pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
4211
- name, chip->label, hwnum, status);
4212
- gpiochip_free_own_desc(desc);
4213
- return status;
4175
+ name, gc->label, hwnum, ret);
4176
+ return ret;
42144177 }
42154178
42164179 /* Mark GPIO as hogged so it can be identified and removed later */
42174180 set_bit(FLAG_IS_HOGGED, &desc->flags);
42184181
4219
- pr_info("GPIO line %d (%s) hogged as %s%s\n",
4220
- desc_to_gpio(desc), name,
4221
- (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4222
- (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4223
- (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4182
+ gpiod_info(desc, "hogged as %s%s\n",
4183
+ (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4184
+ (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4185
+ (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
42244186
42254187 return 0;
42264188 }
42274189
42284190 /**
42294191 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4230
- * @chip: gpio chip to act on
4231
- *
4232
- * This is only used by of_gpiochip_remove to free hogged gpios
4192
+ * @gc: gpio chip to act on
42334193 */
4234
-static void gpiochip_free_hogs(struct gpio_chip *chip)
4194
+static void gpiochip_free_hogs(struct gpio_chip *gc)
42354195 {
42364196 int id;
42374197
4238
- for (id = 0; id < chip->ngpio; id++) {
4239
- if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4240
- gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4198
+ for (id = 0; id < gc->ngpio; id++) {
4199
+ if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4200
+ gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
42414201 }
42424202 }
42434203
....@@ -4259,7 +4219,9 @@
42594219 {
42604220 struct gpio_desc *desc;
42614221 struct gpio_descs *descs;
4262
- int count;
4222
+ struct gpio_array *array_info = NULL;
4223
+ struct gpio_chip *gc;
4224
+ int count, bitmap_size;
42634225
42644226 count = gpiod_count(dev, con_id);
42654227 if (count < 0)
....@@ -4275,9 +4237,92 @@
42754237 gpiod_put_array(descs);
42764238 return ERR_CAST(desc);
42774239 }
4240
+
42784241 descs->desc[descs->ndescs] = desc;
4242
+
4243
+ gc = gpiod_to_chip(desc);
4244
+ /*
4245
+ * If pin hardware number of array member 0 is also 0, select
4246
+ * its chip as a candidate for fast bitmap processing path.
4247
+ */
4248
+ if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4249
+ struct gpio_descs *array;
4250
+
4251
+ bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4252
+ gc->ngpio : count);
4253
+
4254
+ array = kzalloc(struct_size(descs, desc, count) +
4255
+ struct_size(array_info, invert_mask,
4256
+ 3 * bitmap_size), GFP_KERNEL);
4257
+ if (!array) {
4258
+ gpiod_put_array(descs);
4259
+ return ERR_PTR(-ENOMEM);
4260
+ }
4261
+
4262
+ memcpy(array, descs,
4263
+ struct_size(descs, desc, descs->ndescs + 1));
4264
+ kfree(descs);
4265
+
4266
+ descs = array;
4267
+ array_info = (void *)(descs->desc + count);
4268
+ array_info->get_mask = array_info->invert_mask +
4269
+ bitmap_size;
4270
+ array_info->set_mask = array_info->get_mask +
4271
+ bitmap_size;
4272
+
4273
+ array_info->desc = descs->desc;
4274
+ array_info->size = count;
4275
+ array_info->chip = gc;
4276
+ bitmap_set(array_info->get_mask, descs->ndescs,
4277
+ count - descs->ndescs);
4278
+ bitmap_set(array_info->set_mask, descs->ndescs,
4279
+ count - descs->ndescs);
4280
+ descs->info = array_info;
4281
+ }
4282
+ /* Unmark array members which don't belong to the 'fast' chip */
4283
+ if (array_info && array_info->chip != gc) {
4284
+ __clear_bit(descs->ndescs, array_info->get_mask);
4285
+ __clear_bit(descs->ndescs, array_info->set_mask);
4286
+ }
4287
+ /*
4288
+ * Detect array members which belong to the 'fast' chip
4289
+ * but their pins are not in hardware order.
4290
+ */
4291
+ else if (array_info &&
4292
+ gpio_chip_hwgpio(desc) != descs->ndescs) {
4293
+ /*
4294
+ * Don't use fast path if all array members processed so
4295
+ * far belong to the same chip as this one but its pin
4296
+ * hardware number is different from its array index.
4297
+ */
4298
+ if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4299
+ array_info = NULL;
4300
+ } else {
4301
+ __clear_bit(descs->ndescs,
4302
+ array_info->get_mask);
4303
+ __clear_bit(descs->ndescs,
4304
+ array_info->set_mask);
4305
+ }
4306
+ } else if (array_info) {
4307
+ /* Exclude open drain or open source from fast output */
4308
+ if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4309
+ gpiochip_line_is_open_source(gc, descs->ndescs))
4310
+ __clear_bit(descs->ndescs,
4311
+ array_info->set_mask);
4312
+ /* Identify 'fast' pins which require invertion */
4313
+ if (gpiod_is_active_low(desc))
4314
+ __set_bit(descs->ndescs,
4315
+ array_info->invert_mask);
4316
+ }
4317
+
42794318 descs->ndescs++;
42804319 }
4320
+ if (array_info)
4321
+ dev_dbg(dev,
4322
+ "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4323
+ array_info->chip->label, array_info->size,
4324
+ *array_info->get_mask, *array_info->set_mask,
4325
+ *array_info->invert_mask);
42814326 return descs;
42824327 }
42834328 EXPORT_SYMBOL_GPL(gpiod_get_array);
....@@ -4299,7 +4344,7 @@
42994344 struct gpio_descs *descs;
43004345
43014346 descs = gpiod_get_array(dev, con_id, flags);
4302
- if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4347
+ if (PTR_ERR(descs) == -ENOENT)
43034348 return NULL;
43044349
43054350 return descs;
....@@ -4314,7 +4359,8 @@
43144359 */
43154360 void gpiod_put(struct gpio_desc *desc)
43164361 {
4317
- gpiod_free(desc);
4362
+ if (desc)
4363
+ gpiod_free(desc);
43184364 }
43194365 EXPORT_SYMBOL_GPL(gpiod_put);
43204366
....@@ -4333,6 +4379,41 @@
43334379 }
43344380 EXPORT_SYMBOL_GPL(gpiod_put_array);
43354381
4382
+
4383
+static int gpio_bus_match(struct device *dev, struct device_driver *drv)
4384
+{
4385
+ /*
4386
+ * Only match if the fwnode doesn't already have a proper struct device
4387
+ * created for it.
4388
+ */
4389
+ if (dev->fwnode && dev->fwnode->dev != dev)
4390
+ return 0;
4391
+ return 1;
4392
+}
4393
+
4394
+static int gpio_stub_drv_probe(struct device *dev)
4395
+{
4396
+ /*
4397
+ * The DT node of some GPIO chips have a "compatible" property, but
4398
+ * never have a struct device added and probed by a driver to register
4399
+ * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4400
+ * the consumers of the GPIO chip to get probe deferred forever because
4401
+ * they will be waiting for a device associated with the GPIO chip
4402
+ * firmware node to get added and bound to a driver.
4403
+ *
4404
+ * To allow these consumers to probe, we associate the struct
4405
+ * gpio_device of the GPIO chip with the firmware node and then simply
4406
+ * bind it to this stub driver.
4407
+ */
4408
+ return 0;
4409
+}
4410
+
4411
+static struct device_driver gpio_stub_drv = {
4412
+ .name = "gpio_stub_drv",
4413
+ .bus = &gpio_bus_type,
4414
+ .probe = gpio_stub_drv_probe,
4415
+};
4416
+
43364417 static int __init gpiolib_dev_init(void)
43374418 {
43384419 int ret;
....@@ -4344,14 +4425,28 @@
43444425 return ret;
43454426 }
43464427
4347
- ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4428
+ ret = driver_register(&gpio_stub_drv);
4429
+ if (ret < 0) {
4430
+ pr_err("gpiolib: could not register GPIO stub driver\n");
4431
+ bus_unregister(&gpio_bus_type);
4432
+ return ret;
4433
+ }
4434
+
4435
+ ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
43484436 if (ret < 0) {
43494437 pr_err("gpiolib: failed to allocate char dev region\n");
4438
+ driver_unregister(&gpio_stub_drv);
43504439 bus_unregister(&gpio_bus_type);
4351
- } else {
4352
- gpiolib_initialized = true;
4353
- gpiochip_setup_devs();
4440
+ return ret;
43544441 }
4442
+
4443
+ gpiolib_initialized = true;
4444
+ gpiochip_setup_devs();
4445
+
4446
+#if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4447
+ WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4448
+#endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4449
+
43554450 return ret;
43564451 }
43574452 core_initcall(gpiolib_dev_init);
....@@ -4361,11 +4456,12 @@
43614456 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
43624457 {
43634458 unsigned i;
4364
- struct gpio_chip *chip = gdev->chip;
4459
+ struct gpio_chip *gc = gdev->chip;
43654460 unsigned gpio = gdev->base;
43664461 struct gpio_desc *gdesc = &gdev->descs[0];
4367
- int is_out;
4368
- int is_irq;
4462
+ bool is_out;
4463
+ bool is_irq;
4464
+ bool active_low;
43694465
43704466 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
43714467 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
....@@ -4379,11 +4475,13 @@
43794475 gpiod_get_direction(gdesc);
43804476 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
43814477 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4382
- seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4478
+ active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4479
+ seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
43834480 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
43844481 is_out ? "out" : "in ",
4385
- chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
4386
- is_irq ? "IRQ" : " ");
4482
+ gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4483
+ is_irq ? "IRQ " : "",
4484
+ active_low ? "ACTIVE LOW" : "");
43874485 seq_printf(s, "\n");
43884486 }
43894487 }
....@@ -4433,10 +4531,10 @@
44334531 static int gpiolib_seq_show(struct seq_file *s, void *v)
44344532 {
44354533 struct gpio_device *gdev = v;
4436
- struct gpio_chip *chip = gdev->chip;
4534
+ struct gpio_chip *gc = gdev->chip;
44374535 struct device *parent;
44384536
4439
- if (!chip) {
4537
+ if (!gc) {
44404538 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
44414539 dev_name(&gdev->dev));
44424540 return 0;
....@@ -4445,50 +4543,37 @@
44454543 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
44464544 dev_name(&gdev->dev),
44474545 gdev->base, gdev->base + gdev->ngpio - 1);
4448
- parent = chip->parent;
4546
+ parent = gc->parent;
44494547 if (parent)
44504548 seq_printf(s, ", parent: %s/%s",
44514549 parent->bus ? parent->bus->name : "no-bus",
44524550 dev_name(parent));
4453
- if (chip->label)
4454
- seq_printf(s, ", %s", chip->label);
4455
- if (chip->can_sleep)
4551
+ if (gc->label)
4552
+ seq_printf(s, ", %s", gc->label);
4553
+ if (gc->can_sleep)
44564554 seq_printf(s, ", can sleep");
44574555 seq_printf(s, ":\n");
44584556
4459
- if (chip->dbg_show)
4460
- chip->dbg_show(s, chip);
4557
+ if (gc->dbg_show)
4558
+ gc->dbg_show(s, gc);
44614559 else
44624560 gpiolib_dbg_show(s, gdev);
44634561
44644562 return 0;
44654563 }
44664564
4467
-static const struct seq_operations gpiolib_seq_ops = {
4565
+static const struct seq_operations gpiolib_sops = {
44684566 .start = gpiolib_seq_start,
44694567 .next = gpiolib_seq_next,
44704568 .stop = gpiolib_seq_stop,
44714569 .show = gpiolib_seq_show,
44724570 };
4473
-
4474
-static int gpiolib_open(struct inode *inode, struct file *file)
4475
-{
4476
- return seq_open(file, &gpiolib_seq_ops);
4477
-}
4478
-
4479
-static const struct file_operations gpiolib_operations = {
4480
- .owner = THIS_MODULE,
4481
- .open = gpiolib_open,
4482
- .read = seq_read,
4483
- .llseek = seq_lseek,
4484
- .release = seq_release,
4485
-};
4571
+DEFINE_SEQ_ATTRIBUTE(gpiolib);
44864572
44874573 static int __init gpiolib_debugfs_init(void)
44884574 {
44894575 /* /sys/kernel/debug/gpio */
4490
- (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4491
- NULL, NULL, &gpiolib_operations);
4576
+ debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
44924577 return 0;
44934578 }
44944579 subsys_initcall(gpiolib_debugfs_init);