hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
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
....@@ -186,9 +190,8 @@
186190 /* found a free space? */
187191 if (gdev->base + gdev->ngpio <= base)
188192 break;
189
- else
190
- /* nope, check the space right before the chip */
191
- base = gdev->base - ngpio;
193
+ /* nope, check the space right before the chip */
194
+ base = gdev->base - ngpio;
192195 }
193196
194197 if (gpio_is_valid(base)) {
....@@ -210,11 +213,11 @@
210213 */
211214 int gpiod_get_direction(struct gpio_desc *desc)
212215 {
213
- struct gpio_chip *chip;
214
- unsigned offset;
215
- int status = -EINVAL;
216
+ struct gpio_chip *gc;
217
+ unsigned offset;
218
+ int ret;
216219
217
- chip = gpiod_to_chip(desc);
220
+ gc = gpiod_to_chip(desc);
218221 offset = gpio_chip_hwgpio(desc);
219222
220223 /*
....@@ -225,20 +228,20 @@
225228 test_bit(FLAG_IS_OUT, &desc->flags))
226229 return 0;
227230
228
- if (!chip->get_direction)
229
- return status;
231
+ if (!gc->get_direction)
232
+ return -ENOTSUPP;
230233
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;
234
+ ret = gc->get_direction(gc, offset);
235
+ if (ret < 0)
236
+ return ret;
237
+
238
+ /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
239
+ if (ret > 0)
240
+ ret = 1;
241
+
242
+ assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
243
+
244
+ return ret;
242245 }
243246 EXPORT_SYMBOL_GPL(gpiod_get_direction);
244247
....@@ -292,11 +295,17 @@
292295
293296 /*
294297 * Convert a GPIO name to its descriptor
298
+ * Note that there is no guarantee that GPIO names are globally unique!
299
+ * Hence this function will return, if it exists, a reference to the first GPIO
300
+ * line found that matches the given name.
295301 */
296302 static struct gpio_desc *gpio_name_to_desc(const char * const name)
297303 {
298304 struct gpio_device *gdev;
299305 unsigned long flags;
306
+
307
+ if (!name)
308
+ return NULL;
300309
301310 spin_lock_irqsave(&gpio_lock, flags);
302311
....@@ -306,7 +315,7 @@
306315 for (i = 0; i != gdev->ngpio; ++i) {
307316 struct gpio_desc *desc = &gdev->descs[i];
308317
309
- if (!desc->name || !name)
318
+ if (!desc->name)
310319 continue;
311320
312321 if (!strcmp(desc->name, name)) {
....@@ -322,18 +331,17 @@
322331 }
323332
324333 /*
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.
334
+ * Take the names from gc->names and assign them to their GPIO descriptors.
335
+ * Warn if a name is already used for a GPIO line on a different GPIO chip.
327336 *
328
- * Warning if one of the names is already used for a different GPIO.
337
+ * Note that:
338
+ * 1. Non-unique names are still accepted,
339
+ * 2. Name collisions within the same GPIO chip are not reported.
329340 */
330341 static int gpiochip_set_desc_names(struct gpio_chip *gc)
331342 {
332343 struct gpio_device *gdev = gc->gpiodev;
333344 int i;
334
-
335
- if (!gc->names)
336
- return 0;
337345
338346 /* First check all names if they are unique */
339347 for (i = 0; i != gc->ngpio; ++i) {
....@@ -353,873 +361,173 @@
353361 return 0;
354362 }
355363
356
-static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
364
+/*
365
+ * devprop_gpiochip_set_names - Set GPIO line names using device properties
366
+ * @chip: GPIO chip whose lines should be named, if possible
367
+ *
368
+ * Looks for device property "gpio-line-names" and if it exists assigns
369
+ * GPIO line names for the chip. The memory allocated for the assigned
370
+ * names belong to the underlying firmware node and should not be released
371
+ * by the caller.
372
+ */
373
+static int devprop_gpiochip_set_names(struct gpio_chip *chip)
374
+{
375
+ struct gpio_device *gdev = chip->gpiodev;
376
+ struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
377
+ const char **names;
378
+ int ret, i;
379
+ int count;
380
+
381
+ count = fwnode_property_string_array_count(fwnode, "gpio-line-names");
382
+ if (count < 0)
383
+ return 0;
384
+
385
+ if (count > gdev->ngpio) {
386
+ dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
387
+ count, gdev->ngpio);
388
+ count = gdev->ngpio;
389
+ }
390
+
391
+ names = kcalloc(count, sizeof(*names), GFP_KERNEL);
392
+ if (!names)
393
+ return -ENOMEM;
394
+
395
+ ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
396
+ names, count);
397
+ if (ret < 0) {
398
+ dev_warn(&gdev->dev, "failed to read GPIO line names\n");
399
+ kfree(names);
400
+ return ret;
401
+ }
402
+
403
+ for (i = 0; i < count; i++)
404
+ gdev->descs[i].name = names[i];
405
+
406
+ kfree(names);
407
+
408
+ return 0;
409
+}
410
+
411
+static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
357412 {
358413 unsigned long *p;
359414
360
- p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
415
+ p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
361416 if (!p)
362417 return NULL;
363418
364419 /* Assume by default all GPIOs are valid */
365
- bitmap_fill(p, chip->ngpio);
420
+ bitmap_fill(p, gc->ngpio);
366421
367422 return p;
368423 }
369424
370
-static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
425
+static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
371426 {
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)
427
+ if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
382428 return 0;
383429
384
- gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
385
- if (!gpiochip->valid_mask)
430
+ gc->valid_mask = gpiochip_allocate_mask(gc);
431
+ if (!gc->valid_mask)
386432 return -ENOMEM;
387433
388434 return 0;
389435 }
390436
391
-static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
437
+static int gpiochip_init_valid_mask(struct gpio_chip *gc)
392438 {
393
- kfree(gpiochip->valid_mask);
394
- gpiochip->valid_mask = NULL;
439
+ if (gc->init_valid_mask)
440
+ return gc->init_valid_mask(gc,
441
+ gc->valid_mask,
442
+ gc->ngpio);
443
+
444
+ return 0;
395445 }
396446
397
-bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
447
+static void gpiochip_free_valid_mask(struct gpio_chip *gc)
448
+{
449
+ bitmap_free(gc->valid_mask);
450
+ gc->valid_mask = NULL;
451
+}
452
+
453
+static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
454
+{
455
+ if (gc->add_pin_ranges)
456
+ return gc->add_pin_ranges(gc);
457
+
458
+ return 0;
459
+}
460
+
461
+bool gpiochip_line_is_valid(const struct gpio_chip *gc,
398462 unsigned int offset)
399463 {
400464 /* No mask means all valid */
401
- if (likely(!gpiochip->valid_mask))
465
+ if (likely(!gc->valid_mask))
402466 return true;
403
- return test_bit(offset, gpiochip->valid_mask);
467
+ return test_bit(offset, gc->valid_mask);
404468 }
405469 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
406470
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
-
1171471 static void gpiodevice_release(struct device *dev)
1172472 {
1173
- struct gpio_device *gdev = dev_get_drvdata(dev);
473
+ struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
474
+ unsigned long flags;
1174475
476
+ spin_lock_irqsave(&gpio_lock, flags);
1175477 list_del(&gdev->list);
1176
- ida_simple_remove(&gpio_ida, gdev->id);
478
+ spin_unlock_irqrestore(&gpio_lock, flags);
479
+
480
+ ida_free(&gpio_ida, gdev->id);
1177481 kfree_const(gdev->label);
1178482 kfree(gdev->descs);
1179483 kfree(gdev);
1180484 }
1181485
486
+#ifdef CONFIG_GPIO_CDEV
487
+#define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
488
+#define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
489
+#else
490
+/*
491
+ * gpiolib_cdev_register() indirectly calls device_add(), which is still
492
+ * required even when cdev is not selected.
493
+ */
494
+#define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
495
+#define gcdev_unregister(gdev) device_del(&(gdev)->dev)
496
+#endif
497
+
1182498 static int gpiochip_setup_dev(struct gpio_device *gdev)
1183499 {
1184
- int status;
500
+ int ret;
1185501
1186
- cdev_init(&gdev->chrdev, &gpio_fileops);
1187
- gdev->chrdev.owner = THIS_MODULE;
1188
- gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
502
+ ret = gcdev_register(gdev, gpio_devt);
503
+ if (ret)
504
+ return ret;
1189505
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)
506
+ ret = gpiochip_sysfs_register(gdev);
507
+ if (ret)
1199508 goto err_remove_device;
1200509
1201510 /* From this point, the .release() function cleans up gpio_device */
1202511 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");
512
+ dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
513
+ gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
1206514
1207515 return 0;
1208516
1209517 err_remove_device:
1210
- cdev_device_del(&gdev->chrdev, &gdev->dev);
1211
- return status;
518
+ gcdev_unregister(gdev);
519
+ return ret;
1212520 }
1213521
1214
-static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
522
+static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
1215523 {
1216524 struct gpio_desc *desc;
1217525 int rv;
1218526
1219
- desc = gpiochip_get_desc(chip, hog->chip_hwnum);
527
+ desc = gpiochip_get_desc(gc, hog->chip_hwnum);
1220528 if (IS_ERR(desc)) {
1221
- pr_err("%s: unable to get GPIO desc: %ld\n",
1222
- __func__, PTR_ERR(desc));
529
+ chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
530
+ PTR_ERR(desc));
1223531 return;
1224532 }
1225533
....@@ -1228,19 +536,19 @@
1228536
1229537 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1230538 if (rv)
1231
- pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1232
- __func__, chip->label, hog->chip_hwnum, rv);
539
+ gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
540
+ __func__, gc->label, hog->chip_hwnum, rv);
1233541 }
1234542
1235
-static void machine_gpiochip_add(struct gpio_chip *chip)
543
+static void machine_gpiochip_add(struct gpio_chip *gc)
1236544 {
1237545 struct gpiod_hog *hog;
1238546
1239547 mutex_lock(&gpio_machine_hogs_mutex);
1240548
1241549 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1242
- if (!strcmp(chip->label, hog->chip_label))
1243
- gpiochip_machine_hog(chip, hog);
550
+ if (!strcmp(gc->label, hog->chip_label))
551
+ gpiochip_machine_hog(gc, hog);
1244552 }
1245553
1246554 mutex_unlock(&gpio_machine_hogs_mutex);
....@@ -1249,25 +557,27 @@
1249557 static void gpiochip_setup_devs(void)
1250558 {
1251559 struct gpio_device *gdev;
1252
- int err;
560
+ int ret;
1253561
1254562 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);
563
+ ret = gpiochip_setup_dev(gdev);
564
+ if (ret)
565
+ dev_err(&gdev->dev,
566
+ "Failed to initialize gpio device (%d)\n", ret);
1259567 }
1260568 }
1261569
1262
-int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
570
+int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
1263571 struct lock_class_key *lock_key,
1264572 struct lock_class_key *request_key)
1265573 {
574
+ struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
1266575 unsigned long flags;
1267
- int status = 0;
576
+ int ret = 0;
1268577 unsigned i;
1269
- int base = chip->base;
578
+ int base = gc->base;
1270579 struct gpio_device *gdev;
580
+ bool block_gpio_read = false;
1271581
1272582 /*
1273583 * First: allocate and populate the internal stat container, and
....@@ -1277,60 +587,63 @@
1277587 if (!gdev)
1278588 return -ENOMEM;
1279589 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;
590
+ gdev->chip = gc;
591
+ gc->gpiodev = gdev;
592
+ if (gc->parent) {
593
+ gdev->dev.parent = gc->parent;
594
+ gdev->dev.of_node = gc->parent->of_node;
1285595 }
1286596
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
597
+ of_gpio_dev_init(gc, gdev);
1294598
1295
- gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
599
+ /*
600
+ * Assign fwnode depending on the result of the previous calls,
601
+ * if none of them succeed, assign it to the parent's one.
602
+ */
603
+ gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
604
+
605
+ gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
1296606 if (gdev->id < 0) {
1297
- status = gdev->id;
607
+ ret = gdev->id;
1298608 goto err_free_gdev;
1299609 }
1300
- dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
610
+
611
+ ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
612
+ if (ret)
613
+ goto err_free_ida;
614
+
1301615 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)
616
+ if (gc->parent && gc->parent->driver)
617
+ gdev->owner = gc->parent->driver->owner;
618
+ else if (gc->owner)
1306619 /* TODO: remove chip->owner */
1307
- gdev->owner = chip->owner;
620
+ gdev->owner = gc->owner;
1308621 else
1309622 gdev->owner = THIS_MODULE;
1310623
1311
- gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
624
+ gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
1312625 if (!gdev->descs) {
1313
- status = -ENOMEM;
1314
- goto err_free_ida;
626
+ ret = -ENOMEM;
627
+ goto err_free_dev_name;
1315628 }
1316629
1317
- if (chip->ngpio == 0) {
1318
- chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
1319
- status = -EINVAL;
630
+ if (gc->ngpio == 0) {
631
+ chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
632
+ ret = -EINVAL;
1320633 goto err_free_descs;
1321634 }
1322635
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);
636
+ if (gc->ngpio > FASTPATH_NGPIO)
637
+ chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
638
+ gc->ngpio, FASTPATH_NGPIO);
1326639
1327
- gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
640
+ gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
1328641 if (!gdev->label) {
1329
- status = -ENOMEM;
642
+ ret = -ENOMEM;
1330643 goto err_free_descs;
1331644 }
1332645
1333
- gdev->ngpio = chip->ngpio;
646
+ gdev->ngpio = gc->ngpio;
1334647 gdev->data = data;
1335648
1336649 spin_lock_irqsave(&gpio_lock, flags);
....@@ -1343,9 +656,9 @@
1343656 * of the sysfs interface anyways.
1344657 */
1345658 if (base < 0) {
1346
- base = gpiochip_find_base(chip->ngpio);
659
+ base = gpiochip_find_base(gc->ngpio);
1347660 if (base < 0) {
1348
- status = base;
661
+ ret = base;
1349662 spin_unlock_irqrestore(&gpio_lock, flags);
1350663 goto err_free_label;
1351664 }
....@@ -1355,59 +668,80 @@
1355668 * see if anyone makes use of this, else drop this and assign
1356669 * a poison instead.
1357670 */
1358
- chip->base = base;
671
+ gc->base = base;
1359672 }
1360673 gdev->base = base;
1361674
1362
- status = gpiodev_add_to_list(gdev);
1363
- if (status) {
675
+ ret = gpiodev_add_to_list(gdev);
676
+ if (ret) {
1364677 spin_unlock_irqrestore(&gpio_lock, flags);
1365678 goto err_free_label;
1366679 }
1367680
681
+ for (i = 0; i < gc->ngpio; i++)
682
+ gdev->descs[i].gdev = gdev;
683
+
1368684 spin_unlock_irqrestore(&gpio_lock, flags);
1369685
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
- }
686
+ BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
1383687
1384688 #ifdef CONFIG_PINCTRL
1385689 INIT_LIST_HEAD(&gdev->pin_ranges);
1386690 #endif
1387691
1388
- status = gpiochip_set_desc_names(chip);
1389
- if (status)
692
+ if (gc->names)
693
+ ret = gpiochip_set_desc_names(gc);
694
+ else
695
+ ret = devprop_gpiochip_set_names(gc);
696
+ if (ret)
1390697 goto err_remove_from_list;
1391698
1392
- status = gpiochip_irqchip_init_valid_mask(chip);
1393
- if (status)
699
+ ret = gpiochip_alloc_valid_mask(gc);
700
+ if (ret)
1394701 goto err_remove_from_list;
1395702
1396
- status = gpiochip_init_valid_mask(chip);
1397
- if (status)
703
+ ret = of_gpiochip_add(gc);
704
+ if (ret)
705
+ goto err_free_gpiochip_mask;
706
+
707
+ ret = gpiochip_init_valid_mask(gc);
708
+ if (ret)
709
+ goto err_remove_of_chip;
710
+
711
+ trace_android_vh_gpio_block_read(gdev, &block_gpio_read);
712
+ if (!block_gpio_read) {
713
+ for (i = 0; i < gc->ngpio; i++) {
714
+ struct gpio_desc *desc = &gdev->descs[i];
715
+
716
+ if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
717
+ assign_bit(FLAG_IS_OUT,
718
+ &desc->flags, !gc->get_direction(gc, i));
719
+ } else {
720
+ assign_bit(FLAG_IS_OUT,
721
+ &desc->flags, !gc->direction_input);
722
+ }
723
+ }
724
+ }
725
+
726
+ ret = gpiochip_add_pin_ranges(gc);
727
+ if (ret)
728
+ goto err_remove_of_chip;
729
+
730
+ acpi_gpiochip_add(gc);
731
+
732
+ machine_gpiochip_add(gc);
733
+
734
+ ret = gpiochip_irqchip_init_valid_mask(gc);
735
+ if (ret)
736
+ goto err_remove_acpi_chip;
737
+
738
+ ret = gpiochip_irqchip_init_hw(gc);
739
+ if (ret)
740
+ goto err_remove_acpi_chip;
741
+
742
+ ret = gpiochip_add_irqchip(gc, lock_key, request_key);
743
+ if (ret)
1398744 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);
1411745
1412746 /*
1413747 * By first adding the chardev, and then adding the device,
....@@ -1418,19 +752,24 @@
1418752 * Otherwise, defer until later.
1419753 */
1420754 if (gpiolib_initialized) {
1421
- status = gpiochip_setup_dev(gdev);
1422
- if (status)
1423
- goto err_remove_chip;
755
+ ret = gpiochip_setup_dev(gdev);
756
+ if (ret)
757
+ goto err_remove_irqchip;
1424758 }
1425759 return 0;
1426760
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);
761
+err_remove_irqchip:
762
+ gpiochip_irqchip_remove(gc);
1432763 err_remove_irqchip_mask:
1433
- gpiochip_irqchip_free_valid_mask(chip);
764
+ gpiochip_irqchip_free_valid_mask(gc);
765
+err_remove_acpi_chip:
766
+ acpi_gpiochip_remove(gc);
767
+err_remove_of_chip:
768
+ gpiochip_free_hogs(gc);
769
+ of_gpiochip_remove(gc);
770
+err_free_gpiochip_mask:
771
+ gpiochip_remove_pin_ranges(gc);
772
+ gpiochip_free_valid_mask(gc);
1434773 err_remove_from_list:
1435774 spin_lock_irqsave(&gpio_lock, flags);
1436775 list_del(&gdev->list);
....@@ -1439,55 +778,55 @@
1439778 kfree_const(gdev->label);
1440779 err_free_descs:
1441780 kfree(gdev->descs);
781
+err_free_dev_name:
782
+ kfree(dev_name(&gdev->dev));
1442783 err_free_ida:
1443
- ida_simple_remove(&gpio_ida, gdev->id);
784
+ ida_free(&gpio_ida, gdev->id);
1444785 err_free_gdev:
1445786 /* failures here can mean systems won't boot... */
1446787 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
1447788 gdev->base, gdev->base + gdev->ngpio - 1,
1448
- chip->label ? : "generic", status);
789
+ gc->label ? : "generic", ret);
1449790 kfree(gdev);
1450
- return status;
791
+ return ret;
1451792 }
1452793 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1453794
1454795 /**
1455796 * gpiochip_get_data() - get per-subdriver data for the chip
1456
- * @chip: GPIO chip
797
+ * @gc: GPIO chip
1457798 *
1458799 * Returns:
1459800 * The per-subdriver data for the chip.
1460801 */
1461
-void *gpiochip_get_data(struct gpio_chip *chip)
802
+void *gpiochip_get_data(struct gpio_chip *gc)
1462803 {
1463
- return chip->gpiodev->data;
804
+ return gc->gpiodev->data;
1464805 }
1465806 EXPORT_SYMBOL_GPL(gpiochip_get_data);
1466807
1467808 /**
1468809 * gpiochip_remove() - unregister a gpio_chip
1469
- * @chip: the chip to unregister
810
+ * @gc: the chip to unregister
1470811 *
1471812 * A gpio_chip with any GPIOs still requested may not be removed.
1472813 */
1473
-void gpiochip_remove(struct gpio_chip *chip)
814
+void gpiochip_remove(struct gpio_chip *gc)
1474815 {
1475
- struct gpio_device *gdev = chip->gpiodev;
1476
- struct gpio_desc *desc;
816
+ struct gpio_device *gdev = gc->gpiodev;
1477817 unsigned long flags;
1478
- unsigned i;
1479
- bool requested = false;
818
+ unsigned int i;
1480819
1481820 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
1482821 gpiochip_sysfs_unregister(gdev);
1483
- gpiochip_free_hogs(chip);
822
+ gpiochip_free_hogs(gc);
1484823 /* Numb the device, cancelling all outstanding operations */
1485824 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);
825
+ gpiochip_irqchip_remove(gc);
826
+ acpi_gpiochip_remove(gc);
827
+ of_gpiochip_remove(gc);
828
+ gpiochip_remove_pin_ranges(gc);
829
+ gpiochip_free_valid_mask(gc);
1491830 /*
1492831 * We accept no more calls into the driver from this point, so
1493832 * NULL the driver data pointer
....@@ -1496,13 +835,12 @@
1496835
1497836 spin_lock_irqsave(&gpio_lock, flags);
1498837 for (i = 0; i < gdev->ngpio; i++) {
1499
- desc = &gdev->descs[i];
1500
- if (test_bit(FLAG_REQUESTED, &desc->flags))
1501
- requested = true;
838
+ if (gpiochip_is_requested(gc, i))
839
+ break;
1502840 }
1503841 spin_unlock_irqrestore(&gpio_lock, flags);
1504842
1505
- if (requested)
843
+ if (i != gdev->ngpio)
1506844 dev_crit(&gdev->dev,
1507845 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
1508846
....@@ -1512,86 +850,10 @@
1512850 * be removed, else it will be dangling until the last user is
1513851 * gone.
1514852 */
1515
- cdev_device_del(&gdev->chrdev, &gdev->dev);
853
+ gcdev_unregister(gdev);
1516854 put_device(&gdev->dev);
1517855 }
1518856 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);
1595857
1596858 /**
1597859 * gpiochip_find() - iterator for locating a specific gpio_chip
....@@ -1605,31 +867,31 @@
1605867 * more gpio_chips.
1606868 */
1607869 struct gpio_chip *gpiochip_find(void *data,
1608
- int (*match)(struct gpio_chip *chip,
870
+ int (*match)(struct gpio_chip *gc,
1609871 void *data))
1610872 {
1611873 struct gpio_device *gdev;
1612
- struct gpio_chip *chip = NULL;
874
+ struct gpio_chip *gc = NULL;
1613875 unsigned long flags;
1614876
1615877 spin_lock_irqsave(&gpio_lock, flags);
1616878 list_for_each_entry(gdev, &gpio_devices, list)
1617879 if (gdev->chip && match(gdev->chip, data)) {
1618
- chip = gdev->chip;
880
+ gc = gdev->chip;
1619881 break;
1620882 }
1621883
1622884 spin_unlock_irqrestore(&gpio_lock, flags);
1623885
1624
- return chip;
886
+ return gc;
1625887 }
1626888 EXPORT_SYMBOL_GPL(gpiochip_find);
1627889
1628
-static int gpiochip_match_name(struct gpio_chip *chip, void *data)
890
+static int gpiochip_match_name(struct gpio_chip *gc, void *data)
1629891 {
1630892 const char *name = data;
1631893
1632
- return !strcmp(chip->label, name);
894
+ return !strcmp(gc->label, name);
1633895 }
1634896
1635897 static struct gpio_chip *find_chip_by_name(const char *name)
....@@ -1643,126 +905,397 @@
1643905 * The following is irqchip helper code for gpiochips.
1644906 */
1645907
1646
-static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
908
+static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1647909 {
1648
- if (!gpiochip->irq.need_valid_mask)
910
+ struct gpio_irq_chip *girq = &gc->irq;
911
+
912
+ if (!girq->init_hw)
1649913 return 0;
1650914
1651
- gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
1652
- if (!gpiochip->irq.valid_mask)
915
+ return girq->init_hw(gc);
916
+}
917
+
918
+static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
919
+{
920
+ struct gpio_irq_chip *girq = &gc->irq;
921
+
922
+ if (!girq->init_valid_mask)
923
+ return 0;
924
+
925
+ girq->valid_mask = gpiochip_allocate_mask(gc);
926
+ if (!girq->valid_mask)
1653927 return -ENOMEM;
928
+
929
+ girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1654930
1655931 return 0;
1656932 }
1657933
1658
-static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
934
+static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
1659935 {
1660
- kfree(gpiochip->irq.valid_mask);
1661
- gpiochip->irq.valid_mask = NULL;
936
+ bitmap_free(gc->irq.valid_mask);
937
+ gc->irq.valid_mask = NULL;
1662938 }
1663939
1664
-bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
940
+bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
1665941 unsigned int offset)
1666942 {
1667
- if (!gpiochip_line_is_valid(gpiochip, offset))
943
+ if (!gpiochip_line_is_valid(gc, offset))
1668944 return false;
1669945 /* No mask means all valid */
1670
- if (likely(!gpiochip->irq.valid_mask))
946
+ if (likely(!gc->irq.valid_mask))
1671947 return true;
1672
- return test_bit(offset, gpiochip->irq.valid_mask);
948
+ return test_bit(offset, gc->irq.valid_mask);
1673949 }
1674950 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
1675951
1676952 /**
1677953 * 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
954
+ * @gc: the gpiochip to set the irqchip chain to
1680955 * @parent_irq: the irq number corresponding to the parent IRQ for this
1681
- * chained irqchip
956
+ * cascaded irqchip
1682957 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1683958 * coming out of the gpiochip. If the interrupt is nested rather than
1684959 * cascaded, pass NULL in this handler argument
1685960 */
1686
-static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1687
- struct irq_chip *irqchip,
961
+static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
1688962 unsigned int parent_irq,
1689963 irq_flow_handler_t parent_handler)
1690964 {
1691
- unsigned int offset;
965
+ struct gpio_irq_chip *girq = &gc->irq;
966
+ struct device *dev = &gc->gpiodev->dev;
1692967
1693
- if (!gpiochip->irq.domain) {
1694
- chip_err(gpiochip, "called %s before setting up irqchip\n",
968
+ if (!girq->domain) {
969
+ chip_err(gc, "called %s before setting up irqchip\n",
1695970 __func__);
1696971 return;
1697972 }
1698973
1699974 if (parent_handler) {
1700
- if (gpiochip->can_sleep) {
1701
- chip_err(gpiochip,
975
+ if (gc->can_sleep) {
976
+ chip_err(gc,
1702977 "you cannot have chained interrupts on a chip that may sleep\n");
1703978 return;
1704979 }
980
+ girq->parents = devm_kcalloc(dev, 1,
981
+ sizeof(*girq->parents),
982
+ GFP_KERNEL);
983
+ if (!girq->parents) {
984
+ chip_err(gc, "out of memory allocating parent IRQ\n");
985
+ return;
986
+ }
987
+ girq->parents[0] = parent_irq;
988
+ girq->num_parents = 1;
1705989 /*
1706990 * The parent irqchip is already using the chip_data for this
1707991 * irqchip, so our callbacks simply use the handler_data.
1708992 */
1709993 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);
994
+ gc);
1723995 }
1724996 }
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);
1750997
1751998 /**
1752999 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1753
- * @gpiochip: the gpiochip to set the irqchip nested handler to
1000
+ * @gc: the gpiochip to set the irqchip nested handler to
17541001 * @irqchip: the irqchip to nest to the gpiochip
17551002 * @parent_irq: the irq number corresponding to the parent IRQ for this
17561003 * nested irqchip
17571004 */
1758
-void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1005
+void gpiochip_set_nested_irqchip(struct gpio_chip *gc,
17591006 struct irq_chip *irqchip,
17601007 unsigned int parent_irq)
17611008 {
1762
- gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1763
- NULL);
1009
+ gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL);
17641010 }
17651011 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1012
+
1013
+#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1014
+
1015
+/**
1016
+ * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1017
+ * to a gpiochip
1018
+ * @gc: the gpiochip to set the irqchip hierarchical handler to
1019
+ * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1020
+ * will then percolate up to the parent
1021
+ */
1022
+static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1023
+ struct irq_chip *irqchip)
1024
+{
1025
+ /* DT will deal with mapping each IRQ as we go along */
1026
+ if (is_of_node(gc->irq.fwnode))
1027
+ return;
1028
+
1029
+ /*
1030
+ * This is for legacy and boardfile "irqchip" fwnodes: allocate
1031
+ * irqs upfront instead of dynamically since we don't have the
1032
+ * dynamic type of allocation that hardware description languages
1033
+ * provide. Once all GPIO drivers using board files are gone from
1034
+ * the kernel we can delete this code, but for a transitional period
1035
+ * it is necessary to keep this around.
1036
+ */
1037
+ if (is_fwnode_irqchip(gc->irq.fwnode)) {
1038
+ int i;
1039
+ int ret;
1040
+
1041
+ for (i = 0; i < gc->ngpio; i++) {
1042
+ struct irq_fwspec fwspec;
1043
+ unsigned int parent_hwirq;
1044
+ unsigned int parent_type;
1045
+ struct gpio_irq_chip *girq = &gc->irq;
1046
+
1047
+ /*
1048
+ * We call the child to parent translation function
1049
+ * only to check if the child IRQ is valid or not.
1050
+ * Just pick the rising edge type here as that is what
1051
+ * we likely need to support.
1052
+ */
1053
+ ret = girq->child_to_parent_hwirq(gc, i,
1054
+ IRQ_TYPE_EDGE_RISING,
1055
+ &parent_hwirq,
1056
+ &parent_type);
1057
+ if (ret) {
1058
+ chip_err(gc, "skip set-up on hwirq %d\n",
1059
+ i);
1060
+ continue;
1061
+ }
1062
+
1063
+ fwspec.fwnode = gc->irq.fwnode;
1064
+ /* This is the hwirq for the GPIO line side of things */
1065
+ fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1066
+ /* Just pick something */
1067
+ fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1068
+ fwspec.param_count = 2;
1069
+ ret = __irq_domain_alloc_irqs(gc->irq.domain,
1070
+ /* just pick something */
1071
+ -1,
1072
+ 1,
1073
+ NUMA_NO_NODE,
1074
+ &fwspec,
1075
+ false,
1076
+ NULL);
1077
+ if (ret < 0) {
1078
+ chip_err(gc,
1079
+ "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1080
+ i, parent_hwirq,
1081
+ ret);
1082
+ }
1083
+ }
1084
+ }
1085
+
1086
+ chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1087
+
1088
+ return;
1089
+}
1090
+
1091
+static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1092
+ struct irq_fwspec *fwspec,
1093
+ unsigned long *hwirq,
1094
+ unsigned int *type)
1095
+{
1096
+ /* We support standard DT translation */
1097
+ if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1098
+ return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1099
+ }
1100
+
1101
+ /* This is for board files and others not using DT */
1102
+ if (is_fwnode_irqchip(fwspec->fwnode)) {
1103
+ int ret;
1104
+
1105
+ ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1106
+ if (ret)
1107
+ return ret;
1108
+ WARN_ON(*type == IRQ_TYPE_NONE);
1109
+ return 0;
1110
+ }
1111
+ return -EINVAL;
1112
+}
1113
+
1114
+static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1115
+ unsigned int irq,
1116
+ unsigned int nr_irqs,
1117
+ void *data)
1118
+{
1119
+ struct gpio_chip *gc = d->host_data;
1120
+ irq_hw_number_t hwirq;
1121
+ unsigned int type = IRQ_TYPE_NONE;
1122
+ struct irq_fwspec *fwspec = data;
1123
+ void *parent_arg;
1124
+ unsigned int parent_hwirq;
1125
+ unsigned int parent_type;
1126
+ struct gpio_irq_chip *girq = &gc->irq;
1127
+ int ret;
1128
+
1129
+ /*
1130
+ * The nr_irqs parameter is always one except for PCI multi-MSI
1131
+ * so this should not happen.
1132
+ */
1133
+ WARN_ON(nr_irqs != 1);
1134
+
1135
+ ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1136
+ if (ret)
1137
+ return ret;
1138
+
1139
+ chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1140
+
1141
+ ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1142
+ &parent_hwirq, &parent_type);
1143
+ if (ret) {
1144
+ chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1145
+ return ret;
1146
+ }
1147
+ chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
1148
+
1149
+ /*
1150
+ * We set handle_bad_irq because the .set_type() should
1151
+ * always be invoked and set the right type of handler.
1152
+ */
1153
+ irq_domain_set_info(d,
1154
+ irq,
1155
+ hwirq,
1156
+ gc->irq.chip,
1157
+ gc,
1158
+ girq->handler,
1159
+ NULL, NULL);
1160
+ irq_set_probe(irq);
1161
+
1162
+ /* This parent only handles asserted level IRQs */
1163
+ parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1164
+ if (!parent_arg)
1165
+ return -ENOMEM;
1166
+
1167
+ chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1168
+ irq, parent_hwirq);
1169
+ irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1170
+ ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
1171
+ /*
1172
+ * If the parent irqdomain is msi, the interrupts have already
1173
+ * been allocated, so the EEXIST is good.
1174
+ */
1175
+ if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1176
+ ret = 0;
1177
+ if (ret)
1178
+ chip_err(gc,
1179
+ "failed to allocate parent hwirq %d for hwirq %lu\n",
1180
+ parent_hwirq, hwirq);
1181
+
1182
+ kfree(parent_arg);
1183
+ return ret;
1184
+}
1185
+
1186
+static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
1187
+ unsigned int offset)
1188
+{
1189
+ return offset;
1190
+}
1191
+
1192
+static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1193
+{
1194
+ ops->activate = gpiochip_irq_domain_activate;
1195
+ ops->deactivate = gpiochip_irq_domain_deactivate;
1196
+ ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1197
+ ops->free = irq_domain_free_irqs_common;
1198
+
1199
+ /*
1200
+ * We only allow overriding the translate() function for
1201
+ * hierarchical chips, and this should only be done if the user
1202
+ * really need something other than 1:1 translation.
1203
+ */
1204
+ if (!ops->translate)
1205
+ ops->translate = gpiochip_hierarchy_irq_domain_translate;
1206
+}
1207
+
1208
+static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1209
+{
1210
+ if (!gc->irq.child_to_parent_hwirq ||
1211
+ !gc->irq.fwnode) {
1212
+ chip_err(gc, "missing irqdomain vital data\n");
1213
+ return -EINVAL;
1214
+ }
1215
+
1216
+ if (!gc->irq.child_offset_to_irq)
1217
+ gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1218
+
1219
+ if (!gc->irq.populate_parent_alloc_arg)
1220
+ gc->irq.populate_parent_alloc_arg =
1221
+ gpiochip_populate_parent_fwspec_twocell;
1222
+
1223
+ gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1224
+
1225
+ gc->irq.domain = irq_domain_create_hierarchy(
1226
+ gc->irq.parent_domain,
1227
+ 0,
1228
+ gc->ngpio,
1229
+ gc->irq.fwnode,
1230
+ &gc->irq.child_irq_domain_ops,
1231
+ gc);
1232
+
1233
+ if (!gc->irq.domain)
1234
+ return -ENOMEM;
1235
+
1236
+ gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1237
+
1238
+ return 0;
1239
+}
1240
+
1241
+static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1242
+{
1243
+ return !!gc->irq.parent_domain;
1244
+}
1245
+
1246
+void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
1247
+ unsigned int parent_hwirq,
1248
+ unsigned int parent_type)
1249
+{
1250
+ struct irq_fwspec *fwspec;
1251
+
1252
+ fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1253
+ if (!fwspec)
1254
+ return NULL;
1255
+
1256
+ fwspec->fwnode = gc->irq.parent_domain->fwnode;
1257
+ fwspec->param_count = 2;
1258
+ fwspec->param[0] = parent_hwirq;
1259
+ fwspec->param[1] = parent_type;
1260
+
1261
+ return fwspec;
1262
+}
1263
+EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1264
+
1265
+void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
1266
+ unsigned int parent_hwirq,
1267
+ unsigned int parent_type)
1268
+{
1269
+ struct irq_fwspec *fwspec;
1270
+
1271
+ fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1272
+ if (!fwspec)
1273
+ return NULL;
1274
+
1275
+ fwspec->fwnode = gc->irq.parent_domain->fwnode;
1276
+ fwspec->param_count = 4;
1277
+ fwspec->param[0] = 0;
1278
+ fwspec->param[1] = parent_hwirq;
1279
+ fwspec->param[2] = 0;
1280
+ fwspec->param[3] = parent_type;
1281
+
1282
+ return fwspec;
1283
+}
1284
+EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1285
+
1286
+#else
1287
+
1288
+static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1289
+{
1290
+ return -EINVAL;
1291
+}
1292
+
1293
+static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1294
+{
1295
+ return false;
1296
+}
1297
+
1298
+#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
17661299
17671300 /**
17681301 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
....@@ -1777,38 +1310,38 @@
17771310 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
17781311 irq_hw_number_t hwirq)
17791312 {
1780
- struct gpio_chip *chip = d->host_data;
1781
- int err = 0;
1313
+ struct gpio_chip *gc = d->host_data;
1314
+ int ret = 0;
17821315
1783
- if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1316
+ if (!gpiochip_irqchip_irq_valid(gc, hwirq))
17841317 return -ENXIO;
17851318
1786
- irq_set_chip_data(irq, chip);
1319
+ irq_set_chip_data(irq, gc);
17871320 /*
17881321 * This lock class tells lockdep that GPIO irqs are in a different
17891322 * category than their parents, so it won't report false recursion.
17901323 */
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);
1324
+ irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1325
+ irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
17931326 /* Chips that use nested thread handlers have them marked */
1794
- if (chip->irq.threaded)
1327
+ if (gc->irq.threaded)
17951328 irq_set_nested_thread(irq, 1);
17961329 irq_set_noprobe(irq);
17971330
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]);
1331
+ if (gc->irq.num_parents == 1)
1332
+ ret = irq_set_parent(irq, gc->irq.parents[0]);
1333
+ else if (gc->irq.map)
1334
+ ret = irq_set_parent(irq, gc->irq.map[hwirq]);
18021335
1803
- if (err < 0)
1804
- return err;
1336
+ if (ret < 0)
1337
+ return ret;
18051338
18061339 /*
18071340 * No set-up of the hardware will happen if IRQ_TYPE_NONE
18081341 * is passed as default type.
18091342 */
1810
- if (chip->irq.default_type != IRQ_TYPE_NONE)
1811
- irq_set_irq_type(irq, chip->irq.default_type);
1343
+ if (gc->irq.default_type != IRQ_TYPE_NONE)
1344
+ irq_set_irq_type(irq, gc->irq.default_type);
18121345
18131346 return 0;
18141347 }
....@@ -1816,9 +1349,9 @@
18161349
18171350 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
18181351 {
1819
- struct gpio_chip *chip = d->host_data;
1352
+ struct gpio_chip *gc = d->host_data;
18201353
1821
- if (chip->irq.threaded)
1354
+ if (gc->irq.threaded)
18221355 irq_set_nested_thread(irq, 0);
18231356 irq_set_chip_and_handler(irq, NULL, NULL);
18241357 irq_set_chip_data(irq, NULL);
....@@ -1832,53 +1365,181 @@
18321365 .xlate = irq_domain_xlate_twocell,
18331366 };
18341367
1368
+/*
1369
+ * TODO: move these activate/deactivate in under the hierarchicial
1370
+ * irqchip implementation as static once SPMI and SSBI (all external
1371
+ * users) are phased over.
1372
+ */
1373
+/**
1374
+ * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1375
+ * @domain: The IRQ domain used by this IRQ chip
1376
+ * @data: Outermost irq_data associated with the IRQ
1377
+ * @reserve: If set, only reserve an interrupt vector instead of assigning one
1378
+ *
1379
+ * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1380
+ * used as the activate function for the &struct irq_domain_ops. The host_data
1381
+ * for the IRQ domain must be the &struct gpio_chip.
1382
+ */
1383
+int gpiochip_irq_domain_activate(struct irq_domain *domain,
1384
+ struct irq_data *data, bool reserve)
1385
+{
1386
+ struct gpio_chip *gc = domain->host_data;
1387
+
1388
+ return gpiochip_lock_as_irq(gc, data->hwirq);
1389
+}
1390
+EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1391
+
1392
+/**
1393
+ * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1394
+ * @domain: The IRQ domain used by this IRQ chip
1395
+ * @data: Outermost irq_data associated with the IRQ
1396
+ *
1397
+ * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1398
+ * be used as the deactivate function for the &struct irq_domain_ops. The
1399
+ * host_data for the IRQ domain must be the &struct gpio_chip.
1400
+ */
1401
+void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1402
+ struct irq_data *data)
1403
+{
1404
+ struct gpio_chip *gc = domain->host_data;
1405
+
1406
+ return gpiochip_unlock_as_irq(gc, data->hwirq);
1407
+}
1408
+EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1409
+
1410
+static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset)
1411
+{
1412
+ struct irq_domain *domain = gc->irq.domain;
1413
+
1414
+#ifdef CONFIG_GPIOLIB_IRQCHIP
1415
+ /*
1416
+ * Avoid race condition with other code, which tries to lookup
1417
+ * an IRQ before the irqchip has been properly registered,
1418
+ * i.e. while gpiochip is still being brought up.
1419
+ */
1420
+ if (!gc->irq.initialized)
1421
+ return -EPROBE_DEFER;
1422
+#endif
1423
+
1424
+ if (!gpiochip_irqchip_irq_valid(gc, offset))
1425
+ return -ENXIO;
1426
+
1427
+#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1428
+ if (irq_domain_is_hierarchy(domain)) {
1429
+ struct irq_fwspec spec;
1430
+
1431
+ spec.fwnode = domain->fwnode;
1432
+ spec.param_count = 2;
1433
+ spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
1434
+ spec.param[1] = IRQ_TYPE_NONE;
1435
+
1436
+ return irq_create_fwspec_mapping(&spec);
1437
+ }
1438
+#endif
1439
+
1440
+ return irq_create_mapping(domain, offset);
1441
+}
1442
+
18351443 static int gpiochip_irq_reqres(struct irq_data *d)
18361444 {
1837
- struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1838
- int ret;
1445
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
18391446
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;
1447
+ return gpiochip_reqres_irq(gc, d->hwirq);
18521448 }
18531449
18541450 static void gpiochip_irq_relres(struct irq_data *d)
18551451 {
1856
- struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1452
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
18571453
1858
- gpiochip_unlock_as_irq(chip, d->hwirq);
1859
- module_put(chip->gpiodev->owner);
1454
+ gpiochip_relres_irq(gc, d->hwirq);
18601455 }
18611456
1862
-static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1457
+static void gpiochip_irq_mask(struct irq_data *d)
18631458 {
1864
- if (!gpiochip_irqchip_irq_valid(chip, offset))
1865
- return -ENXIO;
1459
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
18661460
1867
- return irq_create_mapping(chip->irq.domain, offset);
1461
+ if (gc->irq.irq_mask)
1462
+ gc->irq.irq_mask(d);
1463
+ gpiochip_disable_irq(gc, d->hwirq);
1464
+}
1465
+
1466
+static void gpiochip_irq_unmask(struct irq_data *d)
1467
+{
1468
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1469
+
1470
+ gpiochip_enable_irq(gc, d->hwirq);
1471
+ if (gc->irq.irq_unmask)
1472
+ gc->irq.irq_unmask(d);
1473
+}
1474
+
1475
+static void gpiochip_irq_enable(struct irq_data *d)
1476
+{
1477
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1478
+
1479
+ gpiochip_enable_irq(gc, d->hwirq);
1480
+ gc->irq.irq_enable(d);
1481
+}
1482
+
1483
+static void gpiochip_irq_disable(struct irq_data *d)
1484
+{
1485
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1486
+
1487
+ gc->irq.irq_disable(d);
1488
+ gpiochip_disable_irq(gc, d->hwirq);
1489
+}
1490
+
1491
+static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
1492
+{
1493
+ struct irq_chip *irqchip = gc->irq.chip;
1494
+
1495
+ if (!irqchip->irq_request_resources &&
1496
+ !irqchip->irq_release_resources) {
1497
+ irqchip->irq_request_resources = gpiochip_irq_reqres;
1498
+ irqchip->irq_release_resources = gpiochip_irq_relres;
1499
+ }
1500
+ if (WARN_ON(gc->irq.irq_enable))
1501
+ return;
1502
+ /* Check if the irqchip already has this hook... */
1503
+ if (irqchip->irq_enable == gpiochip_irq_enable ||
1504
+ irqchip->irq_mask == gpiochip_irq_mask) {
1505
+ /*
1506
+ * ...and if so, give a gentle warning that this is bad
1507
+ * practice.
1508
+ */
1509
+ chip_info(gc,
1510
+ "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1511
+ return;
1512
+ }
1513
+
1514
+ if (irqchip->irq_disable) {
1515
+ gc->irq.irq_disable = irqchip->irq_disable;
1516
+ irqchip->irq_disable = gpiochip_irq_disable;
1517
+ } else {
1518
+ gc->irq.irq_mask = irqchip->irq_mask;
1519
+ irqchip->irq_mask = gpiochip_irq_mask;
1520
+ }
1521
+
1522
+ if (irqchip->irq_enable) {
1523
+ gc->irq.irq_enable = irqchip->irq_enable;
1524
+ irqchip->irq_enable = gpiochip_irq_enable;
1525
+ } else {
1526
+ gc->irq.irq_unmask = irqchip->irq_unmask;
1527
+ irqchip->irq_unmask = gpiochip_irq_unmask;
1528
+ }
18681529 }
18691530
18701531 /**
18711532 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1872
- * @gpiochip: the GPIO chip to add the IRQ chip to
1533
+ * @gc: the GPIO chip to add the IRQ chip to
18731534 * @lock_key: lockdep class for IRQ lock
18741535 * @request_key: lockdep class for IRQ request
18751536 */
1876
-static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1537
+static int gpiochip_add_irqchip(struct gpio_chip *gc,
18771538 struct lock_class_key *lock_key,
18781539 struct lock_class_key *request_key)
18791540 {
1880
- struct irq_chip *irqchip = gpiochip->irq.chip;
1881
- const struct irq_domain_ops *ops;
1541
+ struct irq_chip *irqchip = gc->irq.chip;
1542
+ const struct irq_domain_ops *ops = NULL;
18821543 struct device_node *np;
18831544 unsigned int type;
18841545 unsigned int i;
....@@ -1886,13 +1547,13 @@
18861547 if (!irqchip)
18871548 return 0;
18881549
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");
1550
+ if (gc->irq.parent_handler && gc->can_sleep) {
1551
+ chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
18911552 return -EINVAL;
18921553 }
18931554
1894
- np = gpiochip->gpiodev->dev.of_node;
1895
- type = gpiochip->irq.default_type;
1555
+ np = gc->gpiodev->dev.of_node;
1556
+ type = gc->irq.default_type;
18961557
18971558 /*
18981559 * Specifying a default trigger is a terrible idea if DT or ACPI is
....@@ -1903,83 +1564,83 @@
19031564 "%s: Ignoring %u default trigger\n", np->full_name, type))
19041565 type = IRQ_TYPE_NONE;
19051566
1906
- if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1907
- acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1567
+ if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1568
+ acpi_handle_warn(ACPI_HANDLE(gc->parent),
19081569 "Ignoring %u default trigger\n", type);
19091570 type = IRQ_TYPE_NONE;
19101571 }
19111572
1912
-#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1913
- if (!gpiochip->to_irq)
1914
-#endif
1915
- gpiochip->to_irq = gpiochip_to_irq;
1573
+ gc->to_irq = gpiochip_to_irq;
1574
+ gc->irq.default_type = type;
1575
+ gc->irq.lock_key = lock_key;
1576
+ gc->irq.request_key = request_key;
19161577
1917
- gpiochip->irq.default_type = type;
1918
- gpiochip->irq.lock_key = lock_key;
1919
- gpiochip->irq.request_key = request_key;
1578
+ /* If a parent irqdomain is provided, let's build a hierarchy */
1579
+ if (gpiochip_hierarchy_is_hierarchical(gc)) {
1580
+ int ret = gpiochip_hierarchy_add_domain(gc);
1581
+ if (ret)
1582
+ return ret;
1583
+ } else {
1584
+ /* Some drivers provide custom irqdomain ops */
1585
+ if (gc->irq.domain_ops)
1586
+ ops = gc->irq.domain_ops;
19201587
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;
1588
+ if (!ops)
1589
+ ops = &gpiochip_domain_ops;
1590
+ gc->irq.domain = irq_domain_add_simple(np,
1591
+ gc->ngpio,
1592
+ gc->irq.first,
1593
+ ops, gc);
1594
+ if (!gc->irq.domain)
1595
+ return -EINVAL;
19471596 }
19481597
1949
- if (gpiochip->irq.parent_handler) {
1950
- void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1598
+ if (gc->irq.parent_handler) {
1599
+ void *data = gc->irq.parent_handler_data ?: gc;
19511600
1952
- for (i = 0; i < gpiochip->irq.num_parents; i++) {
1601
+ for (i = 0; i < gc->irq.num_parents; i++) {
19531602 /*
19541603 * The parent IRQ chip is already using the chip_data
19551604 * for this IRQ chip, so our callbacks simply use the
19561605 * handler_data.
19571606 */
1958
- irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1959
- gpiochip->irq.parent_handler,
1607
+ irq_set_chained_handler_and_data(gc->irq.parents[i],
1608
+ gc->irq.parent_handler,
19601609 data);
19611610 }
19621611 }
19631612
1964
- acpi_gpiochip_request_interrupts(gpiochip);
1613
+ gpiochip_set_irq_hooks(gc);
1614
+
1615
+ /*
1616
+ * Using barrier() here to prevent compiler from reordering
1617
+ * gc->irq.initialized before initialization of above
1618
+ * GPIO chip irq members.
1619
+ */
1620
+ barrier();
1621
+
1622
+ gc->irq.initialized = true;
1623
+
1624
+ acpi_gpiochip_request_interrupts(gc);
19651625
19661626 return 0;
19671627 }
19681628
19691629 /**
19701630 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1971
- * @gpiochip: the gpiochip to remove the irqchip from
1631
+ * @gc: the gpiochip to remove the irqchip from
19721632 *
19731633 * This is called only from gpiochip_remove()
19741634 */
1975
-static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1635
+static void gpiochip_irqchip_remove(struct gpio_chip *gc)
19761636 {
1637
+ struct irq_chip *irqchip = gc->irq.chip;
19771638 unsigned int offset;
19781639
1979
- acpi_gpiochip_free_interrupts(gpiochip);
1640
+ acpi_gpiochip_free_interrupts(gc);
19801641
1981
- if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
1982
- struct gpio_irq_chip *irq = &gpiochip->irq;
1642
+ if (irqchip && gc->irq.parent_handler) {
1643
+ struct gpio_irq_chip *irq = &gc->irq;
19831644 unsigned int i;
19841645
19851646 for (i = 0; i < irq->num_parents; i++)
....@@ -1988,32 +1649,40 @@
19881649 }
19891650
19901651 /* Remove all IRQ mappings and delete the domain */
1991
- if (gpiochip->irq.domain) {
1652
+ if (gc->irq.domain) {
19921653 unsigned int irq;
19931654
1994
- for (offset = 0; offset < gpiochip->ngpio; offset++) {
1995
- if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1655
+ for (offset = 0; offset < gc->ngpio; offset++) {
1656
+ if (!gpiochip_irqchip_irq_valid(gc, offset))
19961657 continue;
19971658
1998
- irq = irq_find_mapping(gpiochip->irq.domain, offset);
1659
+ irq = irq_find_mapping(gc->irq.domain, offset);
19991660 irq_dispose_mapping(irq);
20001661 }
20011662
2002
- irq_domain_remove(gpiochip->irq.domain);
1663
+ irq_domain_remove(gc->irq.domain);
20031664 }
20041665
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;
1666
+ if (irqchip) {
1667
+ if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1668
+ irqchip->irq_request_resources = NULL;
1669
+ irqchip->irq_release_resources = NULL;
1670
+ }
1671
+ if (irqchip->irq_enable == gpiochip_irq_enable) {
1672
+ irqchip->irq_enable = gc->irq.irq_enable;
1673
+ irqchip->irq_disable = gc->irq.irq_disable;
1674
+ }
20091675 }
1676
+ gc->irq.irq_enable = NULL;
1677
+ gc->irq.irq_disable = NULL;
1678
+ gc->irq.chip = NULL;
20101679
2011
- gpiochip_irqchip_free_valid_mask(gpiochip);
1680
+ gpiochip_irqchip_free_valid_mask(gc);
20121681 }
20131682
20141683 /**
20151684 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
2016
- * @gpiochip: the gpiochip to add the irqchip to
1685
+ * @gc: the gpiochip to add the irqchip to
20171686 * @irqchip: the irqchip to add to the gpiochip
20181687 * @first_irq: if not dynamically assigned, the base (first) IRQ to
20191688 * allocate gpiochip irqs from
....@@ -2038,7 +1707,7 @@
20381707 * the pins on the gpiochip can generate a unique IRQ. Everything else
20391708 * need to be open coded.
20401709 */
2041
-int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1710
+int gpiochip_irqchip_add_key(struct gpio_chip *gc,
20421711 struct irq_chip *irqchip,
20431712 unsigned int first_irq,
20441713 irq_flow_handler_t handler,
....@@ -2049,23 +1718,23 @@
20491718 {
20501719 struct device_node *of_node;
20511720
2052
- if (!gpiochip || !irqchip)
1721
+ if (!gc || !irqchip)
20531722 return -EINVAL;
20541723
2055
- if (!gpiochip->parent) {
2056
- pr_err("missing gpiochip .dev parent pointer\n");
1724
+ if (!gc->parent) {
1725
+ chip_err(gc, "missing gpiochip .dev parent pointer\n");
20571726 return -EINVAL;
20581727 }
2059
- gpiochip->irq.threaded = threaded;
2060
- of_node = gpiochip->parent->of_node;
1728
+ gc->irq.threaded = threaded;
1729
+ of_node = gc->parent->of_node;
20611730 #ifdef CONFIG_OF_GPIO
20621731 /*
20631732 * If the gpiochip has an assigned OF node this takes precedence
2064
- * FIXME: get rid of this and use gpiochip->parent->of_node
1733
+ * FIXME: get rid of this and use gc->parent->of_node
20651734 * everywhere
20661735 */
2067
- if (gpiochip->of_node)
2068
- of_node = gpiochip->of_node;
1736
+ if (gc->of_node)
1737
+ of_node = gc->of_node;
20691738 #endif
20701739 /*
20711740 * Specifying a default trigger is a terrible idea if DT or ACPI is
....@@ -2075,93 +1744,120 @@
20751744 if (WARN(of_node && type != IRQ_TYPE_NONE,
20761745 "%pOF: Ignoring %d default trigger\n", of_node, type))
20771746 type = IRQ_TYPE_NONE;
2078
- if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2079
- acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1747
+ if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1748
+ acpi_handle_warn(ACPI_HANDLE(gc->parent),
20801749 "Ignoring %d default trigger\n", type);
20811750 type = IRQ_TYPE_NONE;
20821751 }
20831752
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;
1753
+ gc->irq.chip = irqchip;
1754
+ gc->irq.handler = handler;
1755
+ gc->irq.default_type = type;
1756
+ gc->to_irq = gpiochip_to_irq;
1757
+ gc->irq.lock_key = lock_key;
1758
+ gc->irq.request_key = request_key;
1759
+ gc->irq.domain = irq_domain_add_simple(of_node,
1760
+ gc->ngpio, first_irq,
1761
+ &gpiochip_domain_ops, gc);
1762
+ if (!gc->irq.domain) {
1763
+ gc->irq.chip = NULL;
20951764 return -EINVAL;
20961765 }
20971766
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
- }
1767
+ gpiochip_set_irq_hooks(gc);
21071768
2108
- acpi_gpiochip_request_interrupts(gpiochip);
1769
+ acpi_gpiochip_request_interrupts(gc);
21091770
21101771 return 0;
21111772 }
21121773 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
21131774
1775
+/**
1776
+ * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1777
+ * @gc: the gpiochip to add the irqchip to
1778
+ * @domain: the irqdomain to add to the gpiochip
1779
+ *
1780
+ * This function adds an IRQ domain to the gpiochip.
1781
+ */
1782
+int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1783
+ struct irq_domain *domain)
1784
+{
1785
+ if (!domain)
1786
+ return -EINVAL;
1787
+
1788
+ gc->to_irq = gpiochip_to_irq;
1789
+ gc->irq.domain = domain;
1790
+
1791
+ return 0;
1792
+}
1793
+EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1794
+
21141795 #else /* CONFIG_GPIOLIB_IRQCHIP */
21151796
2116
-static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1797
+static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
21171798 struct lock_class_key *lock_key,
21181799 struct lock_class_key *request_key)
21191800 {
21201801 return 0;
21211802 }
1803
+static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
21221804
2123
-static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
2124
-static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1805
+static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
21251806 {
21261807 return 0;
21271808 }
2128
-static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1809
+
1810
+static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
1811
+{
1812
+ return 0;
1813
+}
1814
+static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
21291815 { }
21301816
21311817 #endif /* CONFIG_GPIOLIB_IRQCHIP */
21321818
21331819 /**
21341820 * gpiochip_generic_request() - request the gpio function for a pin
2135
- * @chip: the gpiochip owning the GPIO
1821
+ * @gc: the gpiochip owning the GPIO
21361822 * @offset: the offset of the GPIO to request for GPIO function
21371823 */
2138
-int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1824
+int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset)
21391825 {
2140
- return pinctrl_gpio_request(chip->gpiodev->base + offset);
1826
+#ifdef CONFIG_PINCTRL
1827
+ if (list_empty(&gc->gpiodev->pin_ranges))
1828
+ return 0;
1829
+#endif
1830
+
1831
+ return pinctrl_gpio_request(gc->gpiodev->base + offset);
21411832 }
21421833 EXPORT_SYMBOL_GPL(gpiochip_generic_request);
21431834
21441835 /**
21451836 * gpiochip_generic_free() - free the gpio function from a pin
2146
- * @chip: the gpiochip to request the gpio function for
1837
+ * @gc: the gpiochip to request the gpio function for
21471838 * @offset: the offset of the GPIO to free from GPIO function
21481839 */
2149
-void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1840
+void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset)
21501841 {
2151
- pinctrl_gpio_free(chip->gpiodev->base + offset);
1842
+#ifdef CONFIG_PINCTRL
1843
+ if (list_empty(&gc->gpiodev->pin_ranges))
1844
+ return;
1845
+#endif
1846
+
1847
+ pinctrl_gpio_free(gc->gpiodev->base + offset);
21521848 }
21531849 EXPORT_SYMBOL_GPL(gpiochip_generic_free);
21541850
21551851 /**
21561852 * gpiochip_generic_config() - apply configuration for a pin
2157
- * @chip: the gpiochip owning the GPIO
1853
+ * @gc: the gpiochip owning the GPIO
21581854 * @offset: the offset of the GPIO to apply the configuration
21591855 * @config: the configuration to be applied
21601856 */
2161
-int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1857
+int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset,
21621858 unsigned long config)
21631859 {
2164
- return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1860
+ return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
21651861 }
21661862 EXPORT_SYMBOL_GPL(gpiochip_generic_config);
21671863
....@@ -2169,7 +1865,7 @@
21691865
21701866 /**
21711867 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2172
- * @chip: the gpiochip to add the range for
1868
+ * @gc: the gpiochip to add the range for
21731869 * @pctldev: the pin controller to map to
21741870 * @gpio_offset: the start offset in the current gpio_chip number space
21751871 * @pin_group: name of the pin group inside the pin controller
....@@ -2179,24 +1875,24 @@
21791875 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
21801876 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
21811877 */
2182
-int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1878
+int gpiochip_add_pingroup_range(struct gpio_chip *gc,
21831879 struct pinctrl_dev *pctldev,
21841880 unsigned int gpio_offset, const char *pin_group)
21851881 {
21861882 struct gpio_pin_range *pin_range;
2187
- struct gpio_device *gdev = chip->gpiodev;
1883
+ struct gpio_device *gdev = gc->gpiodev;
21881884 int ret;
21891885
21901886 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
21911887 if (!pin_range) {
2192
- chip_err(chip, "failed to allocate pin ranges\n");
1888
+ chip_err(gc, "failed to allocate pin ranges\n");
21931889 return -ENOMEM;
21941890 }
21951891
21961892 /* Use local offset as range ID */
21971893 pin_range->range.id = gpio_offset;
2198
- pin_range->range.gc = chip;
2199
- pin_range->range.name = chip->label;
1894
+ pin_range->range.gc = gc;
1895
+ pin_range->range.name = gc->label;
22001896 pin_range->range.base = gdev->base + gpio_offset;
22011897 pin_range->pctldev = pctldev;
22021898
....@@ -2210,7 +1906,7 @@
22101906
22111907 pinctrl_add_gpio_range(pctldev, &pin_range->range);
22121908
2213
- chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1909
+ chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
22141910 gpio_offset, gpio_offset + pin_range->range.npins - 1,
22151911 pinctrl_dev_get_devname(pctldev), pin_group);
22161912
....@@ -2222,7 +1918,7 @@
22221918
22231919 /**
22241920 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2225
- * @chip: the gpiochip to add the range for
1921
+ * @gc: the gpiochip to add the range for
22261922 * @pinctl_name: the dev_name() of the pin controller to map to
22271923 * @gpio_offset: the start offset in the current gpio_chip number space
22281924 * @pin_offset: the start offset in the pin controller number space
....@@ -2237,24 +1933,24 @@
22371933 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
22381934 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
22391935 */
2240
-int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1936
+int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
22411937 unsigned int gpio_offset, unsigned int pin_offset,
22421938 unsigned int npins)
22431939 {
22441940 struct gpio_pin_range *pin_range;
2245
- struct gpio_device *gdev = chip->gpiodev;
1941
+ struct gpio_device *gdev = gc->gpiodev;
22461942 int ret;
22471943
22481944 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
22491945 if (!pin_range) {
2250
- chip_err(chip, "failed to allocate pin ranges\n");
1946
+ chip_err(gc, "failed to allocate pin ranges\n");
22511947 return -ENOMEM;
22521948 }
22531949
22541950 /* Use local offset as range ID */
22551951 pin_range->range.id = gpio_offset;
2256
- pin_range->range.gc = chip;
2257
- pin_range->range.name = chip->label;
1952
+ pin_range->range.gc = gc;
1953
+ pin_range->range.name = gc->label;
22581954 pin_range->range.base = gdev->base + gpio_offset;
22591955 pin_range->range.pin_base = pin_offset;
22601956 pin_range->range.npins = npins;
....@@ -2262,11 +1958,11 @@
22621958 &pin_range->range);
22631959 if (IS_ERR(pin_range->pctldev)) {
22641960 ret = PTR_ERR(pin_range->pctldev);
2265
- chip_err(chip, "could not create pin range\n");
1961
+ chip_err(gc, "could not create pin range\n");
22661962 kfree(pin_range);
22671963 return ret;
22681964 }
2269
- chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1965
+ chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
22701966 gpio_offset, gpio_offset + npins - 1,
22711967 pinctl_name,
22721968 pin_offset, pin_offset + npins - 1);
....@@ -2279,12 +1975,12 @@
22791975
22801976 /**
22811977 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2282
- * @chip: the chip to remove all the mappings for
1978
+ * @gc: the chip to remove all the mappings for
22831979 */
2284
-void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1980
+void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
22851981 {
22861982 struct gpio_pin_range *pin_range, *tmp;
2287
- struct gpio_device *gdev = chip->gpiodev;
1983
+ struct gpio_device *gdev = gc->gpiodev;
22881984
22891985 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
22901986 list_del(&pin_range->node);
....@@ -2303,8 +1999,8 @@
23031999 */
23042000 static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
23052001 {
2306
- struct gpio_chip *chip = desc->gdev->chip;
2307
- int status;
2002
+ struct gpio_chip *gc = desc->gdev->chip;
2003
+ int ret;
23082004 unsigned long flags;
23092005 unsigned offset;
23102006
....@@ -2322,39 +2018,39 @@
23222018
23232019 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
23242020 desc_set_label(desc, label ? : "?");
2325
- status = 0;
2021
+ ret = 0;
23262022 } else {
23272023 kfree_const(label);
2328
- status = -EBUSY;
2024
+ ret = -EBUSY;
23292025 goto done;
23302026 }
23312027
2332
- if (chip->request) {
2333
- /* chip->request may sleep */
2028
+ if (gc->request) {
2029
+ /* gc->request may sleep */
23342030 spin_unlock_irqrestore(&gpio_lock, flags);
23352031 offset = gpio_chip_hwgpio(desc);
2336
- if (gpiochip_line_is_valid(chip, offset))
2337
- status = chip->request(chip, offset);
2032
+ if (gpiochip_line_is_valid(gc, offset))
2033
+ ret = gc->request(gc, offset);
23382034 else
2339
- status = -EINVAL;
2035
+ ret = -EINVAL;
23402036 spin_lock_irqsave(&gpio_lock, flags);
23412037
2342
- if (status < 0) {
2038
+ if (ret < 0) {
23432039 desc_set_label(desc, NULL);
23442040 kfree_const(label);
23452041 clear_bit(FLAG_REQUESTED, &desc->flags);
23462042 goto done;
23472043 }
23482044 }
2349
- if (chip->get_direction) {
2350
- /* chip->get_direction may sleep */
2045
+ if (gc->get_direction) {
2046
+ /* gc->get_direction may sleep */
23512047 spin_unlock_irqrestore(&gpio_lock, flags);
23522048 gpiod_get_direction(desc);
23532049 spin_lock_irqsave(&gpio_lock, flags);
23542050 }
23552051 done:
23562052 spin_unlock_irqrestore(&gpio_lock, flags);
2357
- return status;
2053
+ return ret;
23582054 }
23592055
23602056 /*
....@@ -2397,31 +2093,31 @@
23972093
23982094 int gpiod_request(struct gpio_desc *desc, const char *label)
23992095 {
2400
- int status = -EPROBE_DEFER;
2096
+ int ret = -EPROBE_DEFER;
24012097 struct gpio_device *gdev;
24022098
24032099 VALIDATE_DESC(desc);
24042100 gdev = desc->gdev;
24052101
24062102 if (try_module_get(gdev->owner)) {
2407
- status = gpiod_request_commit(desc, label);
2408
- if (status < 0)
2103
+ ret = gpiod_request_commit(desc, label);
2104
+ if (ret < 0)
24092105 module_put(gdev->owner);
24102106 else
24112107 get_device(&gdev->dev);
24122108 }
24132109
2414
- if (status)
2415
- gpiod_dbg(desc, "%s: status %d\n", __func__, status);
2110
+ if (ret)
2111
+ gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
24162112
2417
- return status;
2113
+ return ret;
24182114 }
24192115
24202116 static bool gpiod_free_commit(struct gpio_desc *desc)
24212117 {
24222118 bool ret = false;
24232119 unsigned long flags;
2424
- struct gpio_chip *chip;
2120
+ struct gpio_chip *gc;
24252121
24262122 might_sleep();
24272123
....@@ -2429,12 +2125,12 @@
24292125
24302126 spin_lock_irqsave(&gpio_lock, flags);
24312127
2432
- chip = desc->gdev->chip;
2433
- if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2434
- if (chip->free) {
2128
+ gc = desc->gdev->chip;
2129
+ if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
2130
+ if (gc->free) {
24352131 spin_unlock_irqrestore(&gpio_lock, flags);
2436
- might_sleep_if(chip->can_sleep);
2437
- chip->free(chip, gpio_chip_hwgpio(desc));
2132
+ might_sleep_if(gc->can_sleep);
2133
+ gc->free(gc, gpio_chip_hwgpio(desc));
24382134 spin_lock_irqsave(&gpio_lock, flags);
24392135 }
24402136 kfree_const(desc->label);
....@@ -2443,11 +2139,25 @@
24432139 clear_bit(FLAG_REQUESTED, &desc->flags);
24442140 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
24452141 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
2142
+ clear_bit(FLAG_PULL_UP, &desc->flags);
2143
+ clear_bit(FLAG_PULL_DOWN, &desc->flags);
2144
+ clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
2145
+ clear_bit(FLAG_EDGE_RISING, &desc->flags);
2146
+ clear_bit(FLAG_EDGE_FALLING, &desc->flags);
24462147 clear_bit(FLAG_IS_HOGGED, &desc->flags);
2148
+#ifdef CONFIG_OF_DYNAMIC
2149
+ desc->hog = NULL;
2150
+#endif
2151
+#ifdef CONFIG_GPIO_CDEV
2152
+ WRITE_ONCE(desc->debounce_period_us, 0);
2153
+#endif
24472154 ret = true;
24482155 }
24492156
24502157 spin_unlock_irqrestore(&gpio_lock, flags);
2158
+ blocking_notifier_call_chain(&desc->gdev->notifier,
2159
+ GPIOLINE_CHANGED_RELEASED, desc);
2160
+
24512161 return ret;
24522162 }
24532163
....@@ -2463,7 +2173,7 @@
24632173
24642174 /**
24652175 * gpiochip_is_requested - return string iff signal was requested
2466
- * @chip: controller managing the signal
2176
+ * @gc: controller managing the signal
24672177 * @offset: of signal within controller's 0..(ngpio - 1) range
24682178 *
24692179 * Returns NULL if the GPIO is not currently requested, else a string.
....@@ -2474,14 +2184,16 @@
24742184 * help with diagnostics, and knowing that the signal is used as a GPIO
24752185 * can help avoid accidentally multiplexing it to another controller.
24762186 */
2477
-const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2187
+const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset)
24782188 {
24792189 struct gpio_desc *desc;
24802190
2481
- if (offset >= chip->ngpio)
2191
+ if (offset >= gc->ngpio)
24822192 return NULL;
24832193
2484
- desc = &chip->gpiodev->descs[offset];
2194
+ desc = gpiochip_get_desc(gc, offset);
2195
+ if (IS_ERR(desc))
2196
+ return NULL;
24852197
24862198 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
24872199 return NULL;
....@@ -2491,9 +2203,14 @@
24912203
24922204 /**
24932205 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2494
- * @chip: GPIO chip
2206
+ * @gc: GPIO chip
24952207 * @hwnum: hardware number of the GPIO for which to request the descriptor
24962208 * @label: label for the GPIO
2209
+ * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2210
+ * specify things like line inversion semantics with the machine flags
2211
+ * such as GPIO_OUT_LOW
2212
+ * @dflags: descriptor request flags for this GPIO or 0 if default, this
2213
+ * can be used to specify consumer semantics such as open drain
24972214 *
24982215 * Function allows GPIO chip drivers to request and use their own GPIO
24992216 * descriptors via gpiolib API. Difference to gpiod_request() is that this
....@@ -2505,20 +2222,30 @@
25052222 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
25062223 * code on failure.
25072224 */
2508
-struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2509
- const char *label)
2225
+struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
2226
+ unsigned int hwnum,
2227
+ const char *label,
2228
+ enum gpio_lookup_flags lflags,
2229
+ enum gpiod_flags dflags)
25102230 {
2511
- struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2512
- int err;
2231
+ struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
2232
+ int ret;
25132233
25142234 if (IS_ERR(desc)) {
2515
- chip_err(chip, "failed to get GPIO descriptor\n");
2235
+ chip_err(gc, "failed to get GPIO descriptor\n");
25162236 return desc;
25172237 }
25182238
2519
- err = gpiod_request_commit(desc, label);
2520
- if (err < 0)
2521
- return ERR_PTR(err);
2239
+ ret = gpiod_request_commit(desc, label);
2240
+ if (ret < 0)
2241
+ return ERR_PTR(ret);
2242
+
2243
+ ret = gpiod_configure_flags(desc, label, lflags, dflags);
2244
+ if (ret) {
2245
+ chip_err(gc, "setup of own GPIO %s failed\n", label);
2246
+ gpiod_free_commit(desc);
2247
+ return ERR_PTR(ret);
2248
+ }
25222249
25232250 return desc;
25242251 }
....@@ -2548,6 +2275,55 @@
25482275 * rely on gpio_request() having been called beforehand.
25492276 */
25502277
2278
+static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
2279
+ unsigned long config)
2280
+{
2281
+ if (!gc->set_config)
2282
+ return -ENOTSUPP;
2283
+
2284
+ return gc->set_config(gc, offset, config);
2285
+}
2286
+
2287
+static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2288
+{
2289
+ struct gpio_chip *gc = desc->gdev->chip;
2290
+ unsigned long config;
2291
+ unsigned arg;
2292
+
2293
+ switch (mode) {
2294
+ case PIN_CONFIG_BIAS_PULL_DOWN:
2295
+ case PIN_CONFIG_BIAS_PULL_UP:
2296
+ arg = 1;
2297
+ break;
2298
+
2299
+ default:
2300
+ arg = 0;
2301
+ }
2302
+
2303
+ config = PIN_CONF_PACKED(mode, arg);
2304
+ return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2305
+}
2306
+
2307
+static int gpio_set_bias(struct gpio_desc *desc)
2308
+{
2309
+ int bias = 0;
2310
+ int ret = 0;
2311
+
2312
+ if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2313
+ bias = PIN_CONFIG_BIAS_DISABLE;
2314
+ else if (test_bit(FLAG_PULL_UP, &desc->flags))
2315
+ bias = PIN_CONFIG_BIAS_PULL_UP;
2316
+ else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2317
+ bias = PIN_CONFIG_BIAS_PULL_DOWN;
2318
+
2319
+ if (bias) {
2320
+ ret = gpio_set_config(desc, bias);
2321
+ if (ret != -ENOTSUPP)
2322
+ return ret;
2323
+ }
2324
+ return 0;
2325
+}
2326
+
25512327 /**
25522328 * gpiod_direction_input - set the GPIO direction to input
25532329 * @desc: GPIO to set to input
....@@ -2559,44 +2335,49 @@
25592335 */
25602336 int gpiod_direction_input(struct gpio_desc *desc)
25612337 {
2562
- struct gpio_chip *chip;
2563
- int status = 0;
2338
+ struct gpio_chip *gc;
2339
+ int ret = 0;
25642340
25652341 VALIDATE_DESC(desc);
2566
- chip = desc->gdev->chip;
2342
+ gc = desc->gdev->chip;
25672343
2568
- if (!chip->get && chip->direction_input) {
2344
+ /*
2345
+ * It is legal to have no .get() and .direction_input() specified if
2346
+ * the chip is output-only, but you can't specify .direction_input()
2347
+ * and not support the .get() operation, that doesn't make sense.
2348
+ */
2349
+ if (!gc->get && gc->direction_input) {
25692350 gpiod_warn(desc,
2570
- "%s: missing get() and direction_input() operations\n",
2571
- __func__);
2351
+ "%s: missing get() but have direction_input()\n",
2352
+ __func__);
25722353 return -EIO;
25732354 }
25742355
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)) {
2356
+ /*
2357
+ * If we have a .direction_input() callback, things are simple,
2358
+ * just call it. Else we are some input-only chip so try to check the
2359
+ * direction (if .get_direction() is supported) else we silently
2360
+ * assume we are in input mode after this.
2361
+ */
2362
+ if (gc->direction_input) {
2363
+ ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2364
+ } else if (gc->get_direction &&
2365
+ (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
25792366 gpiod_warn(desc,
2580
- "%s: missing direction_input() operation\n",
2581
- __func__);
2367
+ "%s: missing direction_input() operation and line is output\n",
2368
+ __func__);
25822369 return -EIO;
25832370 }
2584
- if (status == 0)
2371
+ if (ret == 0) {
25852372 clear_bit(FLAG_IS_OUT, &desc->flags);
2373
+ ret = gpio_set_bias(desc);
2374
+ }
25862375
2587
- trace_gpio_direction(desc_to_gpio(desc), 1, status);
2376
+ trace_gpio_direction(desc_to_gpio(desc), 1, ret);
25882377
2589
- return status;
2378
+ return ret;
25902379 }
25912380 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
-}
26002381
26012382 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
26022383 {
....@@ -2604,16 +2385,22 @@
26042385 int val = !!value;
26052386 int ret = 0;
26062387
2388
+ /*
2389
+ * It's OK not to specify .direction_output() if the gpiochip is
2390
+ * output-only, but if there is then not even a .set() operation it
2391
+ * is pretty tricky to drive the output line.
2392
+ */
26072393 if (!gc->set && !gc->direction_output) {
26082394 gpiod_warn(desc,
2609
- "%s: missing set() and direction_output() operations\n",
2610
- __func__);
2395
+ "%s: missing set() and direction_output() operations\n",
2396
+ __func__);
26112397 return -EIO;
26122398 }
26132399
26142400 if (gc->direction_output) {
26152401 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
26162402 } else {
2403
+ /* Check that we are in output mode if we can */
26172404 if (gc->get_direction &&
26182405 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
26192406 gpiod_warn(desc,
....@@ -2621,6 +2408,10 @@
26212408 __func__);
26222409 return -EIO;
26232410 }
2411
+ /*
2412
+ * If we can't actively set the direction, we are some
2413
+ * output-only chip, so just drive the output as desired.
2414
+ */
26242415 gc->set(gc, gpio_chip_hwgpio(desc), val);
26252416 }
26262417
....@@ -2663,7 +2454,6 @@
26632454 */
26642455 int gpiod_direction_output(struct gpio_desc *desc, int value)
26652456 {
2666
- struct gpio_chip *gc;
26672457 int ret;
26682458
26692459 VALIDATE_DESC(desc);
....@@ -2672,19 +2462,18 @@
26722462 else
26732463 value = !!value;
26742464
2675
- /* GPIOs used for IRQs shall not be set as output */
2676
- if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2465
+ /* GPIOs used for enabled IRQs shall not be set as output */
2466
+ if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2467
+ test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
26772468 gpiod_err(desc,
26782469 "%s: tried to set a GPIO tied to an IRQ as output\n",
26792470 __func__);
26802471 return -EIO;
26812472 }
26822473
2683
- gc = desc->gdev->chip;
26842474 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
26852475 /* 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);
2476
+ ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
26882477 if (!ret)
26892478 goto set_output_value;
26902479 /* Emulate open drain by not actively driving the line high */
....@@ -2692,10 +2481,8 @@
26922481 ret = gpiod_direction_input(desc);
26932482 goto set_output_flag;
26942483 }
2695
- }
2696
- 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);
2484
+ } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2485
+ ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
26992486 if (!ret)
27002487 goto set_output_value;
27012488 /* Emulate open source by not actively driving the line low */
....@@ -2704,11 +2491,13 @@
27042491 goto set_output_flag;
27052492 }
27062493 } else {
2707
- gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2708
- PIN_CONFIG_DRIVE_PUSH_PULL);
2494
+ gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
27092495 }
27102496
27112497 set_output_value:
2498
+ ret = gpio_set_bias(desc);
2499
+ if (ret)
2500
+ return ret;
27122501 return gpiod_direction_output_raw_commit(desc, value);
27132502
27142503 set_output_flag:
....@@ -2725,6 +2514,26 @@
27252514 EXPORT_SYMBOL_GPL(gpiod_direction_output);
27262515
27272516 /**
2517
+ * gpiod_set_config - sets @config for a GPIO
2518
+ * @desc: descriptor of the GPIO for which to set the configuration
2519
+ * @config: Same packed config format as generic pinconf
2520
+ *
2521
+ * Returns:
2522
+ * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2523
+ * configuration.
2524
+ */
2525
+int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2526
+{
2527
+ struct gpio_chip *gc;
2528
+
2529
+ VALIDATE_DESC(desc);
2530
+ gc = desc->gdev->chip;
2531
+
2532
+ return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2533
+}
2534
+EXPORT_SYMBOL_GPL(gpiod_set_config);
2535
+
2536
+/**
27282537 * gpiod_set_debounce - sets @debounce time for a GPIO
27292538 * @desc: descriptor of the GPIO for which to set debounce time
27302539 * @debounce: debounce time in microseconds
....@@ -2735,20 +2544,10 @@
27352544 */
27362545 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
27372546 {
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
- }
2547
+ unsigned long config;
27492548
27502549 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2751
- return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2550
+ return gpiod_set_config(desc, config);
27522551 }
27532552 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
27542553
....@@ -2762,7 +2561,7 @@
27622561 */
27632562 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
27642563 {
2765
- struct gpio_chip *chip;
2564
+ struct gpio_chip *gc;
27662565 unsigned long packed;
27672566 int gpio;
27682567 int rc;
....@@ -2772,20 +2571,17 @@
27722571 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
27732572 * persistence state.
27742573 */
2775
- if (transitory)
2776
- set_bit(FLAG_TRANSITORY, &desc->flags);
2777
- else
2778
- clear_bit(FLAG_TRANSITORY, &desc->flags);
2574
+ assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
27792575
27802576 /* If the driver supports it, set the persistence state now */
2781
- chip = desc->gdev->chip;
2782
- if (!chip->set_config)
2577
+ gc = desc->gdev->chip;
2578
+ if (!gc->set_config)
27832579 return 0;
27842580
27852581 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
27862582 !transitory);
27872583 gpio = gpio_chip_hwgpio(desc);
2788
- rc = chip->set_config(chip, gpio, packed);
2584
+ rc = gpio_do_set_config(gc, gpio, packed);
27892585 if (rc == -ENOTSUPP) {
27902586 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
27912587 gpio);
....@@ -2808,6 +2604,17 @@
28082604 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
28092605 }
28102606 EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2607
+
2608
+/**
2609
+ * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2610
+ * @desc: the gpio descriptor to change
2611
+ */
2612
+void gpiod_toggle_active_low(struct gpio_desc *desc)
2613
+{
2614
+ VALIDATE_DESC_VOID(desc);
2615
+ change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2616
+}
2617
+EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
28112618
28122619 /* I/O calls are only valid after configuration completed; the relevant
28132620 * "is this a valid GPIO" error checks should already have been done.
....@@ -2833,28 +2640,28 @@
28332640
28342641 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
28352642 {
2836
- struct gpio_chip *chip;
2643
+ struct gpio_chip *gc;
28372644 int offset;
28382645 int value;
28392646
2840
- chip = desc->gdev->chip;
2647
+ gc = desc->gdev->chip;
28412648 offset = gpio_chip_hwgpio(desc);
2842
- value = chip->get ? chip->get(chip, offset) : -EIO;
2649
+ value = gc->get ? gc->get(gc, offset) : -EIO;
28432650 value = value < 0 ? value : !!value;
28442651 trace_gpio_value(desc_to_gpio(desc), 1, value);
28452652 return value;
28462653 }
28472654
2848
-static int gpio_chip_get_multiple(struct gpio_chip *chip,
2655
+static int gpio_chip_get_multiple(struct gpio_chip *gc,
28492656 unsigned long *mask, unsigned long *bits)
28502657 {
2851
- if (chip->get_multiple) {
2852
- return chip->get_multiple(chip, mask, bits);
2853
- } else if (chip->get) {
2658
+ if (gc->get_multiple)
2659
+ return gc->get_multiple(gc, mask, bits);
2660
+ if (gc->get) {
28542661 int i, value;
28552662
2856
- for_each_set_bit(i, mask, chip->ngpio) {
2857
- value = chip->get(chip, i);
2663
+ for_each_set_bit(i, mask, gc->ngpio) {
2664
+ value = gc->get(gc, i);
28582665 if (value < 0)
28592666 return value;
28602667 __assign_bit(i, bits, value);
....@@ -2867,31 +2674,60 @@
28672674 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
28682675 unsigned int array_size,
28692676 struct gpio_desc **desc_array,
2870
- int *value_array)
2677
+ struct gpio_array *array_info,
2678
+ unsigned long *value_bitmap)
28712679 {
2872
- int i = 0;
2680
+ int ret, i = 0;
2681
+
2682
+ /*
2683
+ * Validate array_info against desc_array and its size.
2684
+ * It should immediately follow desc_array if both
2685
+ * have been obtained from the same gpiod_get_array() call.
2686
+ */
2687
+ if (array_info && array_info->desc == desc_array &&
2688
+ array_size <= array_info->size &&
2689
+ (void *)array_info == desc_array + array_info->size) {
2690
+ if (!can_sleep)
2691
+ WARN_ON(array_info->chip->can_sleep);
2692
+
2693
+ ret = gpio_chip_get_multiple(array_info->chip,
2694
+ array_info->get_mask,
2695
+ value_bitmap);
2696
+ if (ret)
2697
+ return ret;
2698
+
2699
+ if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2700
+ bitmap_xor(value_bitmap, value_bitmap,
2701
+ array_info->invert_mask, array_size);
2702
+
2703
+ i = find_first_zero_bit(array_info->get_mask, array_size);
2704
+ if (i == array_size)
2705
+ return 0;
2706
+ } else {
2707
+ array_info = NULL;
2708
+ }
28732709
28742710 while (i < array_size) {
2875
- struct gpio_chip *chip = desc_array[i]->gdev->chip;
2711
+ struct gpio_chip *gc = desc_array[i]->gdev->chip;
28762712 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
28772713 unsigned long *mask, *bits;
28782714 int first, j, ret;
28792715
2880
- if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2716
+ if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
28812717 mask = fastpath;
28822718 } else {
2883
- mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2719
+ mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
28842720 sizeof(*mask),
28852721 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
28862722 if (!mask)
28872723 return -ENOMEM;
28882724 }
28892725
2890
- bits = mask + BITS_TO_LONGS(chip->ngpio);
2891
- bitmap_zero(mask, chip->ngpio);
2726
+ bits = mask + BITS_TO_LONGS(gc->ngpio);
2727
+ bitmap_zero(mask, gc->ngpio);
28922728
28932729 if (!can_sleep)
2894
- WARN_ON(chip->can_sleep);
2730
+ WARN_ON(gc->can_sleep);
28952731
28962732 /* collect all inputs belonging to the same chip */
28972733 first = i;
....@@ -2901,25 +2737,34 @@
29012737
29022738 __set_bit(hwgpio, mask);
29032739 i++;
2904
- } while ((i < array_size) &&
2905
- (desc_array[i]->gdev->chip == chip));
29062740
2907
- ret = gpio_chip_get_multiple(chip, mask, bits);
2741
+ if (array_info)
2742
+ i = find_next_zero_bit(array_info->get_mask,
2743
+ array_size, i);
2744
+ } while ((i < array_size) &&
2745
+ (desc_array[i]->gdev->chip == gc));
2746
+
2747
+ ret = gpio_chip_get_multiple(gc, mask, bits);
29082748 if (ret) {
29092749 if (mask != fastpath)
29102750 kfree(mask);
29112751 return ret;
29122752 }
29132753
2914
- for (j = first; j < i; j++) {
2754
+ for (j = first; j < i; ) {
29152755 const struct gpio_desc *desc = desc_array[j];
29162756 int hwgpio = gpio_chip_hwgpio(desc);
29172757 int value = test_bit(hwgpio, bits);
29182758
29192759 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
29202760 value = !value;
2921
- value_array[j] = value;
2761
+ __assign_bit(j, value_bitmap, value);
29222762 trace_gpio_value(desc_to_gpio(desc), 1, value);
2763
+ j++;
2764
+
2765
+ if (array_info)
2766
+ j = find_next_zero_bit(array_info->get_mask, i,
2767
+ j);
29232768 }
29242769
29252770 if (mask != fastpath)
....@@ -2935,7 +2780,7 @@
29352780 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
29362781 * its ACTIVE_LOW status, or negative errno on failure.
29372782 *
2938
- * This function should be called from contexts where we cannot sleep, and will
2783
+ * This function can be called from contexts where we cannot sleep, and will
29392784 * complain if the GPIO chip functions potentially sleep.
29402785 */
29412786 int gpiod_get_raw_value(const struct gpio_desc *desc)
....@@ -2954,7 +2799,7 @@
29542799 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
29552800 * account, or negative errno on failure.
29562801 *
2957
- * This function should be called from contexts where we cannot sleep, and will
2802
+ * This function can be called from contexts where we cannot sleep, and will
29582803 * complain if the GPIO chip functions potentially sleep.
29592804 */
29602805 int gpiod_get_value(const struct gpio_desc *desc)
....@@ -2978,46 +2823,54 @@
29782823
29792824 /**
29802825 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2981
- * @array_size: number of elements in the descriptor / value arrays
2826
+ * @array_size: number of elements in the descriptor array / value bitmap
29822827 * @desc_array: array of GPIO descriptors whose values will be read
2983
- * @value_array: array to store the read values
2828
+ * @array_info: information on applicability of fast bitmap processing path
2829
+ * @value_bitmap: bitmap to store the read values
29842830 *
29852831 * Read the raw values of the GPIOs, i.e. the values of the physical lines
29862832 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
29872833 * else an error code.
29882834 *
2989
- * This function should be called from contexts where we cannot sleep,
2835
+ * This function can be called from contexts where we cannot sleep,
29902836 * and it will complain if the GPIO chip functions potentially sleep.
29912837 */
29922838 int gpiod_get_raw_array_value(unsigned int array_size,
2993
- struct gpio_desc **desc_array, int *value_array)
2839
+ struct gpio_desc **desc_array,
2840
+ struct gpio_array *array_info,
2841
+ unsigned long *value_bitmap)
29942842 {
29952843 if (!desc_array)
29962844 return -EINVAL;
29972845 return gpiod_get_array_value_complex(true, false, array_size,
2998
- desc_array, value_array);
2846
+ desc_array, array_info,
2847
+ value_bitmap);
29992848 }
30002849 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
30012850
30022851 /**
30032852 * gpiod_get_array_value() - read values from an array of GPIOs
3004
- * @array_size: number of elements in the descriptor / value arrays
2853
+ * @array_size: number of elements in the descriptor array / value bitmap
30052854 * @desc_array: array of GPIO descriptors whose values will be read
3006
- * @value_array: array to store the read values
2855
+ * @array_info: information on applicability of fast bitmap processing path
2856
+ * @value_bitmap: bitmap to store the read values
30072857 *
30082858 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
30092859 * into account. Return 0 in case of success, else an error code.
30102860 *
3011
- * This function should be called from contexts where we cannot sleep,
2861
+ * This function can be called from contexts where we cannot sleep,
30122862 * and it will complain if the GPIO chip functions potentially sleep.
30132863 */
30142864 int gpiod_get_array_value(unsigned int array_size,
3015
- struct gpio_desc **desc_array, int *value_array)
2865
+ struct gpio_desc **desc_array,
2866
+ struct gpio_array *array_info,
2867
+ unsigned long *value_bitmap)
30162868 {
30172869 if (!desc_array)
30182870 return -EINVAL;
30192871 return gpiod_get_array_value_complex(false, false, array_size,
3020
- desc_array, value_array);
2872
+ desc_array, array_info,
2873
+ value_bitmap);
30212874 }
30222875 EXPORT_SYMBOL_GPL(gpiod_get_array_value);
30232876
....@@ -3028,22 +2881,22 @@
30282881 */
30292882 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
30302883 {
3031
- int err = 0;
3032
- struct gpio_chip *chip = desc->gdev->chip;
2884
+ int ret = 0;
2885
+ struct gpio_chip *gc = desc->gdev->chip;
30332886 int offset = gpio_chip_hwgpio(desc);
30342887
30352888 if (value) {
3036
- err = chip->direction_input(chip, offset);
2889
+ ret = gc->direction_input(gc, offset);
30372890 } else {
3038
- err = chip->direction_output(chip, offset, 0);
3039
- if (!err)
2891
+ ret = gc->direction_output(gc, offset, 0);
2892
+ if (!ret)
30402893 set_bit(FLAG_IS_OUT, &desc->flags);
30412894 }
3042
- trace_gpio_direction(desc_to_gpio(desc), value, err);
3043
- if (err < 0)
2895
+ trace_gpio_direction(desc_to_gpio(desc), value, ret);
2896
+ if (ret < 0)
30442897 gpiod_err(desc,
30452898 "%s: Error in set_value for open drain err %d\n",
3046
- __func__, err);
2899
+ __func__, ret);
30472900 }
30482901
30492902 /*
....@@ -3053,91 +2906,125 @@
30532906 */
30542907 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
30552908 {
3056
- int err = 0;
3057
- struct gpio_chip *chip = desc->gdev->chip;
2909
+ int ret = 0;
2910
+ struct gpio_chip *gc = desc->gdev->chip;
30582911 int offset = gpio_chip_hwgpio(desc);
30592912
30602913 if (value) {
3061
- err = chip->direction_output(chip, offset, 1);
3062
- if (!err)
2914
+ ret = gc->direction_output(gc, offset, 1);
2915
+ if (!ret)
30632916 set_bit(FLAG_IS_OUT, &desc->flags);
30642917 } else {
3065
- err = chip->direction_input(chip, offset);
2918
+ ret = gc->direction_input(gc, offset);
30662919 }
3067
- trace_gpio_direction(desc_to_gpio(desc), !value, err);
3068
- if (err < 0)
2920
+ trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2921
+ if (ret < 0)
30692922 gpiod_err(desc,
30702923 "%s: Error in set_value for open source err %d\n",
3071
- __func__, err);
2924
+ __func__, ret);
30722925 }
30732926
30742927 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
30752928 {
3076
- struct gpio_chip *chip;
2929
+ struct gpio_chip *gc;
30772930
3078
- chip = desc->gdev->chip;
2931
+ gc = desc->gdev->chip;
30792932 trace_gpio_value(desc_to_gpio(desc), 0, value);
3080
- chip->set(chip, gpio_chip_hwgpio(desc), value);
2933
+ gc->set(gc, gpio_chip_hwgpio(desc), value);
30812934 }
30822935
30832936 /*
30842937 * set multiple outputs on the same chip;
30852938 * use the chip's set_multiple function if available;
30862939 * otherwise set the outputs sequentially;
2940
+ * @chip: the GPIO chip we operate on
30872941 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
30882942 * defines which outputs are to be changed
30892943 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
30902944 * defines the values the outputs specified by mask are to be set to
30912945 */
3092
-static void gpio_chip_set_multiple(struct gpio_chip *chip,
2946
+static void gpio_chip_set_multiple(struct gpio_chip *gc,
30932947 unsigned long *mask, unsigned long *bits)
30942948 {
3095
- if (chip->set_multiple) {
3096
- chip->set_multiple(chip, mask, bits);
2949
+ if (gc->set_multiple) {
2950
+ gc->set_multiple(gc, mask, bits);
30972951 } else {
30982952 unsigned int i;
30992953
31002954 /* 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));
2955
+ for_each_set_bit(i, mask, gc->ngpio)
2956
+ gc->set(gc, i, test_bit(i, bits));
31032957 }
31042958 }
31052959
31062960 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)
2961
+ unsigned int array_size,
2962
+ struct gpio_desc **desc_array,
2963
+ struct gpio_array *array_info,
2964
+ unsigned long *value_bitmap)
31102965 {
31112966 int i = 0;
31122967
2968
+ /*
2969
+ * Validate array_info against desc_array and its size.
2970
+ * It should immediately follow desc_array if both
2971
+ * have been obtained from the same gpiod_get_array() call.
2972
+ */
2973
+ if (array_info && array_info->desc == desc_array &&
2974
+ array_size <= array_info->size &&
2975
+ (void *)array_info == desc_array + array_info->size) {
2976
+ if (!can_sleep)
2977
+ WARN_ON(array_info->chip->can_sleep);
2978
+
2979
+ if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2980
+ bitmap_xor(value_bitmap, value_bitmap,
2981
+ array_info->invert_mask, array_size);
2982
+
2983
+ gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2984
+ value_bitmap);
2985
+
2986
+ i = find_first_zero_bit(array_info->set_mask, array_size);
2987
+ if (i == array_size)
2988
+ return 0;
2989
+ } else {
2990
+ array_info = NULL;
2991
+ }
2992
+
31132993 while (i < array_size) {
3114
- struct gpio_chip *chip = desc_array[i]->gdev->chip;
2994
+ struct gpio_chip *gc = desc_array[i]->gdev->chip;
31152995 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
31162996 unsigned long *mask, *bits;
31172997 int count = 0;
31182998
3119
- if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2999
+ if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
31203000 mask = fastpath;
31213001 } else {
3122
- mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3002
+ mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
31233003 sizeof(*mask),
31243004 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
31253005 if (!mask)
31263006 return -ENOMEM;
31273007 }
31283008
3129
- bits = mask + BITS_TO_LONGS(chip->ngpio);
3130
- bitmap_zero(mask, chip->ngpio);
3009
+ bits = mask + BITS_TO_LONGS(gc->ngpio);
3010
+ bitmap_zero(mask, gc->ngpio);
31313011
31323012 if (!can_sleep)
3133
- WARN_ON(chip->can_sleep);
3013
+ WARN_ON(gc->can_sleep);
31343014
31353015 do {
31363016 struct gpio_desc *desc = desc_array[i];
31373017 int hwgpio = gpio_chip_hwgpio(desc);
3138
- int value = value_array[i];
3018
+ int value = test_bit(i, value_bitmap);
31393019
3140
- if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3020
+ /*
3021
+ * Pins applicable for fast input but not for
3022
+ * fast output processing may have been already
3023
+ * inverted inside the fast path, skip them.
3024
+ */
3025
+ if (!raw && !(array_info &&
3026
+ test_bit(i, array_info->invert_mask)) &&
3027
+ test_bit(FLAG_ACTIVE_LOW, &desc->flags))
31413028 value = !value;
31423029 trace_gpio_value(desc_to_gpio(desc), 0, value);
31433030 /*
....@@ -3150,18 +3037,19 @@
31503037 gpio_set_open_source_value_commit(desc, value);
31513038 } else {
31523039 __set_bit(hwgpio, mask);
3153
- if (value)
3154
- __set_bit(hwgpio, bits);
3155
- else
3156
- __clear_bit(hwgpio, bits);
3040
+ __assign_bit(hwgpio, bits, value);
31573041 count++;
31583042 }
31593043 i++;
3044
+
3045
+ if (array_info)
3046
+ i = find_next_zero_bit(array_info->set_mask,
3047
+ array_size, i);
31603048 } while ((i < array_size) &&
3161
- (desc_array[i]->gdev->chip == chip));
3049
+ (desc_array[i]->gdev->chip == gc));
31623050 /* push collected bits to outputs */
31633051 if (count != 0)
3164
- gpio_chip_set_multiple(chip, mask, bits);
3052
+ gpio_chip_set_multiple(gc, mask, bits);
31653053
31663054 if (mask != fastpath)
31673055 kfree(mask);
....@@ -3177,7 +3065,7 @@
31773065 * Set the raw value of the GPIO, i.e. the value of its physical line without
31783066 * regard for its ACTIVE_LOW status.
31793067 *
3180
- * This function should be called from contexts where we cannot sleep, and will
3068
+ * This function can be called from contexts where we cannot sleep, and will
31813069 * complain if the GPIO chip functions potentially sleep.
31823070 */
31833071 void gpiod_set_raw_value(struct gpio_desc *desc, int value)
....@@ -3218,7 +3106,7 @@
32183106 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
32193107 * OPEN_DRAIN and OPEN_SOURCE flags into account.
32203108 *
3221
- * This function should be called from contexts where we cannot sleep, and will
3109
+ * This function can be called from contexts where we cannot sleep, and will
32223110 * complain if the GPIO chip functions potentially sleep.
32233111 */
32243112 void gpiod_set_value(struct gpio_desc *desc, int value)
....@@ -3232,45 +3120,52 @@
32323120
32333121 /**
32343122 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
3235
- * @array_size: number of elements in the descriptor / value arrays
3123
+ * @array_size: number of elements in the descriptor array / value bitmap
32363124 * @desc_array: array of GPIO descriptors whose values will be assigned
3237
- * @value_array: array of values to assign
3125
+ * @array_info: information on applicability of fast bitmap processing path
3126
+ * @value_bitmap: bitmap of values to assign
32383127 *
32393128 * Set the raw values of the GPIOs, i.e. the values of the physical lines
32403129 * without regard for their ACTIVE_LOW status.
32413130 *
3242
- * This function should be called from contexts where we cannot sleep, and will
3131
+ * This function can be called from contexts where we cannot sleep, and will
32433132 * complain if the GPIO chip functions potentially sleep.
32443133 */
32453134 int gpiod_set_raw_array_value(unsigned int array_size,
3246
- struct gpio_desc **desc_array, int *value_array)
3135
+ struct gpio_desc **desc_array,
3136
+ struct gpio_array *array_info,
3137
+ unsigned long *value_bitmap)
32473138 {
32483139 if (!desc_array)
32493140 return -EINVAL;
32503141 return gpiod_set_array_value_complex(true, false, array_size,
3251
- desc_array, value_array);
3142
+ desc_array, array_info, value_bitmap);
32523143 }
32533144 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
32543145
32553146 /**
32563147 * gpiod_set_array_value() - assign values to an array of GPIOs
3257
- * @array_size: number of elements in the descriptor / value arrays
3148
+ * @array_size: number of elements in the descriptor array / value bitmap
32583149 * @desc_array: array of GPIO descriptors whose values will be assigned
3259
- * @value_array: array of values to assign
3150
+ * @array_info: information on applicability of fast bitmap processing path
3151
+ * @value_bitmap: bitmap of values to assign
32603152 *
32613153 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
32623154 * into account.
32633155 *
3264
- * This function should be called from contexts where we cannot sleep, and will
3156
+ * This function can be called from contexts where we cannot sleep, and will
32653157 * complain if the GPIO chip functions potentially sleep.
32663158 */
3267
-void gpiod_set_array_value(unsigned int array_size,
3268
- struct gpio_desc **desc_array, int *value_array)
3159
+int gpiod_set_array_value(unsigned int array_size,
3160
+ struct gpio_desc **desc_array,
3161
+ struct gpio_array *array_info,
3162
+ unsigned long *value_bitmap)
32693163 {
32703164 if (!desc_array)
3271
- return;
3272
- gpiod_set_array_value_complex(false, false, array_size, desc_array,
3273
- value_array);
3165
+ return -EINVAL;
3166
+ return gpiod_set_array_value_complex(false, false, array_size,
3167
+ desc_array, array_info,
3168
+ value_bitmap);
32743169 }
32753170 EXPORT_SYMBOL_GPL(gpiod_set_array_value);
32763171
....@@ -3316,7 +3211,7 @@
33163211 */
33173212 int gpiod_to_irq(const struct gpio_desc *desc)
33183213 {
3319
- struct gpio_chip *chip;
3214
+ struct gpio_chip *gc;
33203215 int offset;
33213216
33223217 /*
....@@ -3327,10 +3222,10 @@
33273222 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
33283223 return -EINVAL;
33293224
3330
- chip = desc->gdev->chip;
3225
+ gc = desc->gdev->chip;
33313226 offset = gpio_chip_hwgpio(desc);
3332
- if (chip->to_irq) {
3333
- int retirq = chip->to_irq(chip, offset);
3227
+ if (gc->to_irq) {
3228
+ int retirq = gc->to_irq(gc, offset);
33343229
33353230 /* Zero means NO_IRQ */
33363231 if (!retirq)
....@@ -3338,23 +3233,33 @@
33383233
33393234 return retirq;
33403235 }
3236
+#ifdef CONFIG_GPIOLIB_IRQCHIP
3237
+ if (gc->irq.chip) {
3238
+ /*
3239
+ * Avoid race condition with other code, which tries to lookup
3240
+ * an IRQ before the irqchip has been properly registered,
3241
+ * i.e. while gpiochip is still being brought up.
3242
+ */
3243
+ return -EPROBE_DEFER;
3244
+ }
3245
+#endif
33413246 return -ENXIO;
33423247 }
33433248 EXPORT_SYMBOL_GPL(gpiod_to_irq);
33443249
33453250 /**
33463251 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
3347
- * @chip: the chip the GPIO to lock belongs to
3252
+ * @gc: the chip the GPIO to lock belongs to
33483253 * @offset: the offset of the GPIO to lock as IRQ
33493254 *
33503255 * This is used directly by GPIO drivers that want to lock down
33513256 * a certain GPIO line to be used for IRQs.
33523257 */
3353
-int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
3258
+int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
33543259 {
33553260 struct gpio_desc *desc;
33563261
3357
- desc = gpiochip_get_desc(chip, offset);
3262
+ desc = gpiochip_get_desc(gc, offset);
33583263 if (IS_ERR(desc))
33593264 return PTR_ERR(desc);
33603265
....@@ -3362,24 +3267,27 @@
33623267 * If it's fast: flush the direction setting if something changed
33633268 * behind our back
33643269 */
3365
- if (!chip->can_sleep && chip->get_direction) {
3270
+ if (!gc->can_sleep && gc->get_direction) {
33663271 int dir = gpiod_get_direction(desc);
33673272
33683273 if (dir < 0) {
3369
- chip_err(chip, "%s: cannot get GPIO direction\n",
3274
+ chip_err(gc, "%s: cannot get GPIO direction\n",
33703275 __func__);
33713276 return dir;
33723277 }
33733278 }
33743279
3375
- if (test_bit(FLAG_IS_OUT, &desc->flags)) {
3376
- chip_err(chip,
3280
+ /* To be valid for IRQ the line needs to be input or open drain */
3281
+ if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3282
+ !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3283
+ chip_err(gc,
33773284 "%s: tried to flag a GPIO set as output for IRQ\n",
33783285 __func__);
33793286 return -EIO;
33803287 }
33813288
33823289 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
3290
+ set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
33833291
33843292 /*
33853293 * If the consumer has not set up a label (such as when the
....@@ -3395,21 +3303,22 @@
33953303
33963304 /**
33973305 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
3398
- * @chip: the chip the GPIO to lock belongs to
3306
+ * @gc: the chip the GPIO to lock belongs to
33993307 * @offset: the offset of the GPIO to lock as IRQ
34003308 *
34013309 * This is used directly by GPIO drivers that want to indicate
34023310 * that a certain GPIO is no longer used exclusively for IRQ.
34033311 */
3404
-void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
3312
+void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
34053313 {
34063314 struct gpio_desc *desc;
34073315
3408
- desc = gpiochip_get_desc(chip, offset);
3316
+ desc = gpiochip_get_desc(gc, offset);
34093317 if (IS_ERR(desc))
34103318 return;
34113319
34123320 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3321
+ clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
34133322
34143323 /* If we only had this marking, erase it */
34153324 if (desc->label && !strcmp(desc->label, "interrupt"))
....@@ -3417,39 +3326,90 @@
34173326 }
34183327 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
34193328
3420
-bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3329
+void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
34213330 {
3422
- if (offset >= chip->ngpio)
3331
+ struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3332
+
3333
+ if (!IS_ERR(desc) &&
3334
+ !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3335
+ clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3336
+}
3337
+EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3338
+
3339
+void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
3340
+{
3341
+ struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
3342
+
3343
+ if (!IS_ERR(desc) &&
3344
+ !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3345
+ /*
3346
+ * We must not be output when using IRQ UNLESS we are
3347
+ * open drain.
3348
+ */
3349
+ WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3350
+ !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
3351
+ set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3352
+ }
3353
+}
3354
+EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3355
+
3356
+bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
3357
+{
3358
+ if (offset >= gc->ngpio)
34233359 return false;
34243360
3425
- return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3361
+ return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
34263362 }
34273363 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
34283364
3429
-bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3365
+int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
34303366 {
3431
- if (offset >= chip->ngpio)
3367
+ int ret;
3368
+
3369
+ if (!try_module_get(gc->gpiodev->owner))
3370
+ return -ENODEV;
3371
+
3372
+ ret = gpiochip_lock_as_irq(gc, offset);
3373
+ if (ret) {
3374
+ chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3375
+ module_put(gc->gpiodev->owner);
3376
+ return ret;
3377
+ }
3378
+ return 0;
3379
+}
3380
+EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3381
+
3382
+void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
3383
+{
3384
+ gpiochip_unlock_as_irq(gc, offset);
3385
+ module_put(gc->gpiodev->owner);
3386
+}
3387
+EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3388
+
3389
+bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
3390
+{
3391
+ if (offset >= gc->ngpio)
34323392 return false;
34333393
3434
- return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3394
+ return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
34353395 }
34363396 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
34373397
3438
-bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3398
+bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
34393399 {
3440
- if (offset >= chip->ngpio)
3400
+ if (offset >= gc->ngpio)
34413401 return false;
34423402
3443
- return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3403
+ return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
34443404 }
34453405 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
34463406
3447
-bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3407
+bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
34483408 {
3449
- if (offset >= chip->ngpio)
3409
+ if (offset >= gc->ngpio)
34503410 return false;
34513411
3452
- return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
3412
+ return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
34533413 }
34543414 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
34553415
....@@ -3498,9 +3458,10 @@
34983458
34993459 /**
35003460 * 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
3461
+ * @array_size: number of elements in the descriptor array / value bitmap
35023462 * @desc_array: array of GPIO descriptors whose values will be read
3503
- * @value_array: array to store the read values
3463
+ * @array_info: information on applicability of fast bitmap processing path
3464
+ * @value_bitmap: bitmap to store the read values
35043465 *
35053466 * Read the raw values of the GPIOs, i.e. the values of the physical lines
35063467 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
....@@ -3510,21 +3471,24 @@
35103471 */
35113472 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
35123473 struct gpio_desc **desc_array,
3513
- int *value_array)
3474
+ struct gpio_array *array_info,
3475
+ unsigned long *value_bitmap)
35143476 {
35153477 might_sleep_if(extra_checks);
35163478 if (!desc_array)
35173479 return -EINVAL;
35183480 return gpiod_get_array_value_complex(true, true, array_size,
3519
- desc_array, value_array);
3481
+ desc_array, array_info,
3482
+ value_bitmap);
35203483 }
35213484 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
35223485
35233486 /**
35243487 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3525
- * @array_size: number of elements in the descriptor / value arrays
3488
+ * @array_size: number of elements in the descriptor array / value bitmap
35263489 * @desc_array: array of GPIO descriptors whose values will be read
3527
- * @value_array: array to store the read values
3490
+ * @array_info: information on applicability of fast bitmap processing path
3491
+ * @value_bitmap: bitmap to store the read values
35283492 *
35293493 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
35303494 * into account. Return 0 in case of success, else an error code.
....@@ -3533,13 +3497,15 @@
35333497 */
35343498 int gpiod_get_array_value_cansleep(unsigned int array_size,
35353499 struct gpio_desc **desc_array,
3536
- int *value_array)
3500
+ struct gpio_array *array_info,
3501
+ unsigned long *value_bitmap)
35373502 {
35383503 might_sleep_if(extra_checks);
35393504 if (!desc_array)
35403505 return -EINVAL;
35413506 return gpiod_get_array_value_complex(false, true, array_size,
3542
- desc_array, value_array);
3507
+ desc_array, array_info,
3508
+ value_bitmap);
35433509 }
35443510 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
35453511
....@@ -3581,9 +3547,10 @@
35813547
35823548 /**
35833549 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
3584
- * @array_size: number of elements in the descriptor / value arrays
3550
+ * @array_size: number of elements in the descriptor array / value bitmap
35853551 * @desc_array: array of GPIO descriptors whose values will be assigned
3586
- * @value_array: array of values to assign
3552
+ * @array_info: information on applicability of fast bitmap processing path
3553
+ * @value_bitmap: bitmap of values to assign
35873554 *
35883555 * Set the raw values of the GPIOs, i.e. the values of the physical lines
35893556 * without regard for their ACTIVE_LOW status.
....@@ -3591,14 +3558,15 @@
35913558 * This function is to be called from contexts that can sleep.
35923559 */
35933560 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3594
- struct gpio_desc **desc_array,
3595
- int *value_array)
3561
+ struct gpio_desc **desc_array,
3562
+ struct gpio_array *array_info,
3563
+ unsigned long *value_bitmap)
35963564 {
35973565 might_sleep_if(extra_checks);
35983566 if (!desc_array)
35993567 return -EINVAL;
36003568 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
3601
- value_array);
3569
+ array_info, value_bitmap);
36023570 }
36033571 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
36043572
....@@ -3621,24 +3589,27 @@
36213589
36223590 /**
36233591 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
3624
- * @array_size: number of elements in the descriptor / value arrays
3592
+ * @array_size: number of elements in the descriptor array / value bitmap
36253593 * @desc_array: array of GPIO descriptors whose values will be assigned
3626
- * @value_array: array of values to assign
3594
+ * @array_info: information on applicability of fast bitmap processing path
3595
+ * @value_bitmap: bitmap of values to assign
36273596 *
36283597 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
36293598 * into account.
36303599 *
36313600 * This function is to be called from contexts that can sleep.
36323601 */
3633
-void gpiod_set_array_value_cansleep(unsigned int array_size,
3634
- struct gpio_desc **desc_array,
3635
- int *value_array)
3602
+int gpiod_set_array_value_cansleep(unsigned int array_size,
3603
+ struct gpio_desc **desc_array,
3604
+ struct gpio_array *array_info,
3605
+ unsigned long *value_bitmap)
36363606 {
36373607 might_sleep_if(extra_checks);
36383608 if (!desc_array)
3639
- return;
3640
- gpiod_set_array_value_complex(false, true, array_size, desc_array,
3641
- value_array);
3609
+ return -EINVAL;
3610
+ return gpiod_set_array_value_complex(false, true, array_size,
3611
+ desc_array, array_info,
3612
+ value_bitmap);
36423613 }
36433614 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
36443615
....@@ -3676,7 +3647,7 @@
36763647 */
36773648 void gpiod_add_hogs(struct gpiod_hog *hogs)
36783649 {
3679
- struct gpio_chip *chip;
3650
+ struct gpio_chip *gc;
36803651 struct gpiod_hog *hog;
36813652
36823653 mutex_lock(&gpio_machine_hogs_mutex);
....@@ -3688,9 +3659,9 @@
36883659 * The chip may have been registered earlier, so check if it
36893660 * exists and, if so, try to hog the line now.
36903661 */
3691
- chip = find_chip_by_name(hog->chip_label);
3692
- if (chip)
3693
- gpiochip_machine_hog(chip, hog);
3662
+ gc = find_chip_by_name(hog->chip_label);
3663
+ if (gc)
3664
+ gpiochip_machine_hog(gc, hog);
36943665 }
36953666
36963667 mutex_unlock(&gpio_machine_hogs_mutex);
....@@ -3729,8 +3700,7 @@
37293700 }
37303701
37313702 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
3732
- unsigned int idx,
3733
- enum gpio_lookup_flags *flags)
3703
+ unsigned int idx, unsigned long *flags)
37343704 {
37353705 struct gpio_desc *desc = ERR_PTR(-ENOENT);
37363706 struct gpiod_lookup_table *table;
....@@ -3740,8 +3710,8 @@
37403710 if (!table)
37413711 return desc;
37423712
3743
- for (p = &table->table[0]; p->chip_label; p++) {
3744
- struct gpio_chip *chip;
3713
+ for (p = &table->table[0]; p->key; p++) {
3714
+ struct gpio_chip *gc;
37453715
37463716 /* idx must always match exactly */
37473717 if (p->idx != idx)
....@@ -3751,57 +3721,48 @@
37513721 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
37523722 continue;
37533723
3754
- chip = find_chip_by_name(p->chip_label);
3724
+ if (p->chip_hwnum == U16_MAX) {
3725
+ desc = gpio_name_to_desc(p->key);
3726
+ if (desc) {
3727
+ *flags = p->flags;
3728
+ return desc;
3729
+ }
37553730
3756
- if (!chip) {
3731
+ dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3732
+ p->key);
3733
+ return ERR_PTR(-EPROBE_DEFER);
3734
+ }
3735
+
3736
+ gc = find_chip_by_name(p->key);
3737
+
3738
+ if (!gc) {
37573739 /*
37583740 * As the lookup table indicates a chip with
3759
- * p->chip_label should exist, assume it may
3741
+ * p->key should exist, assume it may
37603742 * still appear later and let the interested
37613743 * consumer be probed again or let the Deferred
37623744 * Probe infrastructure handle the error.
37633745 */
37643746 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3765
- p->chip_label);
3747
+ p->key);
37663748 return ERR_PTR(-EPROBE_DEFER);
37673749 }
37683750
3769
- if (chip->ngpio <= p->chip_hwnum) {
3751
+ if (gc->ngpio <= p->chip_hwnum) {
37703752 dev_err(dev,
37713753 "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);
3754
+ idx, p->chip_hwnum, gc->ngpio - 1,
3755
+ gc->label);
37743756 return ERR_PTR(-EINVAL);
37753757 }
37763758
3777
- desc = gpiochip_get_desc(chip, p->chip_hwnum);
3759
+ desc = gpiochip_get_desc(gc, p->chip_hwnum);
37783760 *flags = p->flags;
37793761
37803762 return desc;
37813763 }
37823764
37833765 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;
38053766 }
38063767
38073768 static int platform_gpio_count(struct device *dev, const char *con_id)
....@@ -3814,7 +3775,7 @@
38143775 if (!table)
38153776 return -ENOENT;
38163777
3817
- for (p = &table->table[0]; p->chip_label; p++) {
3778
+ for (p = &table->table[0]; p->key; p++) {
38183779 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
38193780 (!con_id && !p->con_id))
38203781 count++;
....@@ -3824,6 +3785,54 @@
38243785
38253786 return count;
38263787 }
3788
+
3789
+/**
3790
+ * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3791
+ * @fwnode: handle of the firmware node
3792
+ * @con_id: function within the GPIO consumer
3793
+ * @index: index of the GPIO to obtain for the consumer
3794
+ * @flags: GPIO initialization flags
3795
+ * @label: label to attach to the requested GPIO
3796
+ *
3797
+ * This function can be used for drivers that get their configuration
3798
+ * from opaque firmware.
3799
+ *
3800
+ * The function properly finds the corresponding GPIO using whatever is the
3801
+ * underlying firmware interface and then makes sure that the GPIO
3802
+ * descriptor is requested before it is returned to the caller.
3803
+ *
3804
+ * Returns:
3805
+ * On successful request the GPIO pin is configured in accordance with
3806
+ * provided @flags.
3807
+ *
3808
+ * In case of error an ERR_PTR() is returned.
3809
+ */
3810
+struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3811
+ const char *con_id, int index,
3812
+ enum gpiod_flags flags,
3813
+ const char *label)
3814
+{
3815
+ struct gpio_desc *desc;
3816
+ char prop_name[32]; /* 32 is max size of property name */
3817
+ unsigned int i;
3818
+
3819
+ for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3820
+ if (con_id)
3821
+ snprintf(prop_name, sizeof(prop_name), "%s-%s",
3822
+ con_id, gpio_suffixes[i]);
3823
+ else
3824
+ snprintf(prop_name, sizeof(prop_name), "%s",
3825
+ gpio_suffixes[i]);
3826
+
3827
+ desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3828
+ label);
3829
+ if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
3830
+ break;
3831
+ }
3832
+
3833
+ return desc;
3834
+}
3835
+EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
38273836
38283837 /**
38293838 * gpiod_count - return the number of GPIOs associated with a device / function
....@@ -3836,7 +3845,7 @@
38363845 int count = -ENOENT;
38373846
38383847 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3839
- count = dt_gpio_count(dev, con_id);
3848
+ count = of_gpio_get_count(dev, con_id);
38403849 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
38413850 count = acpi_gpio_count(dev, con_id);
38423851
....@@ -3887,8 +3896,8 @@
38873896 * gpiod_configure_flags - helper function to configure a given GPIO
38883897 * @desc: gpio whose value will be assigned
38893898 * @con_id: function within the GPIO consumer
3890
- * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3891
- * of_get_gpio_hog()
3899
+ * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3900
+ * of_find_gpio() or of_get_gpio_hog()
38923901 * @dflags: gpiod_flags - optional GPIO initialization flags
38933902 *
38943903 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
....@@ -3898,7 +3907,7 @@
38983907 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
38993908 unsigned long lflags, enum gpiod_flags dflags)
39003909 {
3901
- int status;
3910
+ int ret;
39023911
39033912 if (lflags & GPIO_ACTIVE_LOW)
39043913 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
....@@ -3920,24 +3929,35 @@
39203929 if (lflags & GPIO_OPEN_SOURCE)
39213930 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
39223931
3923
- status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3924
- if (status < 0)
3925
- return status;
3932
+ if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3933
+ gpiod_err(desc,
3934
+ "both pull-up and pull-down enabled, invalid configuration\n");
3935
+ return -EINVAL;
3936
+ }
3937
+
3938
+ if (lflags & GPIO_PULL_UP)
3939
+ set_bit(FLAG_PULL_UP, &desc->flags);
3940
+ else if (lflags & GPIO_PULL_DOWN)
3941
+ set_bit(FLAG_PULL_DOWN, &desc->flags);
3942
+
3943
+ ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3944
+ if (ret < 0)
3945
+ return ret;
39263946
39273947 /* No particular flag request, return here... */
39283948 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3929
- pr_debug("no flags found for %s\n", con_id);
3949
+ gpiod_dbg(desc, "no flags found for %s\n", con_id);
39303950 return 0;
39313951 }
39323952
39333953 /* Process flags */
39343954 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3935
- status = gpiod_direction_output(desc,
3955
+ ret = gpiod_direction_output(desc,
39363956 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
39373957 else
3938
- status = gpiod_direction_input(desc);
3958
+ ret = gpiod_direction_input(desc);
39393959
3940
- return status;
3960
+ return ret;
39413961 }
39423962
39433963 /**
....@@ -3959,9 +3979,9 @@
39593979 unsigned int idx,
39603980 enum gpiod_flags flags)
39613981 {
3982
+ unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
39623983 struct gpio_desc *desc = NULL;
3963
- int status;
3964
- enum gpio_lookup_flags lookupflags = 0;
3984
+ int ret;
39653985 /* Maybe we have a device name, maybe not */
39663986 const char *devname = dev ? dev_name(dev) : "?";
39673987
....@@ -3996,91 +4016,38 @@
39964016 * If a connection label was passed use that, else attempt to use
39974017 * the device name as label
39984018 */
3999
- status = gpiod_request(desc, con_id ? con_id : devname);
4000
- if (status < 0)
4001
- return ERR_PTR(status);
4019
+ ret = gpiod_request(desc, con_id ? con_id : devname);
4020
+ if (ret < 0) {
4021
+ if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
4022
+ /*
4023
+ * This happens when there are several consumers for
4024
+ * the same GPIO line: we just return here without
4025
+ * further initialization. It is a bit if a hack.
4026
+ * This is necessary to support fixed regulators.
4027
+ *
4028
+ * FIXME: Make this more sane and safe.
4029
+ */
4030
+ dev_info(dev, "nonexclusive access to GPIO for %s\n",
4031
+ con_id ? con_id : devname);
4032
+ return desc;
4033
+ } else {
4034
+ return ERR_PTR(ret);
4035
+ }
4036
+ }
40024037
4003
- status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4004
- if (status < 0) {
4038
+ ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4039
+ if (ret < 0) {
40054040 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
40064041 gpiod_put(desc);
4007
- return ERR_PTR(status);
4042
+ return ERR_PTR(ret);
40084043 }
4044
+
4045
+ blocking_notifier_call_chain(&desc->gdev->notifier,
4046
+ GPIOLINE_CHANGED_REQUESTED, desc);
40094047
40104048 return desc;
40114049 }
40124050 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);
40844051
40854052 /**
40864053 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
....@@ -4108,8 +4075,8 @@
41084075 enum gpiod_flags dflags,
41094076 const char *label)
41104077 {
4078
+ unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
41114079 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4112
- unsigned long lflags = 0;
41134080 int ret;
41144081
41154082 if (!fwnode)
....@@ -4129,9 +4096,7 @@
41294096 return desc;
41304097
41314098 acpi_gpio_update_gpiod_flags(&dflags, &info);
4132
-
4133
- if (info.polarity == GPIO_ACTIVE_LOW)
4134
- lflags |= GPIO_ACTIVE_LOW;
4099
+ acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
41354100 }
41364101
41374102 /* Currently only ACPI takes this path */
....@@ -4144,6 +4109,9 @@
41444109 gpiod_put(desc);
41454110 return ERR_PTR(ret);
41464111 }
4112
+
4113
+ blocking_notifier_call_chain(&desc->gdev->notifier,
4114
+ GPIOLINE_CHANGED_REQUESTED, desc);
41474115
41484116 return desc;
41494117 }
....@@ -4182,62 +4150,52 @@
41824150 * gpiod_hog - Hog the specified GPIO desc given the provided flags
41834151 * @desc: gpio whose value will be assigned
41844152 * @name: gpio line name
4185
- * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4186
- * of_get_gpio_hog()
4153
+ * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4154
+ * of_find_gpio() or of_get_gpio_hog()
41874155 * @dflags: gpiod_flags - optional GPIO initialization flags
41884156 */
41894157 int gpiod_hog(struct gpio_desc *desc, const char *name,
41904158 unsigned long lflags, enum gpiod_flags dflags)
41914159 {
4192
- struct gpio_chip *chip;
4160
+ struct gpio_chip *gc;
41934161 struct gpio_desc *local_desc;
41944162 int hwnum;
4195
- int status;
4163
+ int ret;
41964164
4197
- chip = gpiod_to_chip(desc);
4165
+ gc = gpiod_to_chip(desc);
41984166 hwnum = gpio_chip_hwgpio(desc);
41994167
4200
- local_desc = gpiochip_request_own_desc(chip, hwnum, name);
4168
+ local_desc = gpiochip_request_own_desc(gc, hwnum, name,
4169
+ lflags, dflags);
42014170 if (IS_ERR(local_desc)) {
4202
- status = PTR_ERR(local_desc);
4171
+ ret = PTR_ERR(local_desc);
42034172 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;
4173
+ name, gc->label, hwnum, ret);
4174
+ return ret;
42144175 }
42154176
42164177 /* Mark GPIO as hogged so it can be identified and removed later */
42174178 set_bit(FLAG_IS_HOGGED, &desc->flags);
42184179
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":"");
4180
+ gpiod_info(desc, "hogged as %s%s\n",
4181
+ (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4182
+ (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4183
+ (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
42244184
42254185 return 0;
42264186 }
42274187
42284188 /**
42294189 * 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
4190
+ * @gc: gpio chip to act on
42334191 */
4234
-static void gpiochip_free_hogs(struct gpio_chip *chip)
4192
+static void gpiochip_free_hogs(struct gpio_chip *gc)
42354193 {
42364194 int id;
42374195
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]);
4196
+ for (id = 0; id < gc->ngpio; id++) {
4197
+ if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4198
+ gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
42414199 }
42424200 }
42434201
....@@ -4259,7 +4217,9 @@
42594217 {
42604218 struct gpio_desc *desc;
42614219 struct gpio_descs *descs;
4262
- int count;
4220
+ struct gpio_array *array_info = NULL;
4221
+ struct gpio_chip *gc;
4222
+ int count, bitmap_size;
42634223
42644224 count = gpiod_count(dev, con_id);
42654225 if (count < 0)
....@@ -4275,9 +4235,92 @@
42754235 gpiod_put_array(descs);
42764236 return ERR_CAST(desc);
42774237 }
4238
+
42784239 descs->desc[descs->ndescs] = desc;
4240
+
4241
+ gc = gpiod_to_chip(desc);
4242
+ /*
4243
+ * If pin hardware number of array member 0 is also 0, select
4244
+ * its chip as a candidate for fast bitmap processing path.
4245
+ */
4246
+ if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
4247
+ struct gpio_descs *array;
4248
+
4249
+ bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4250
+ gc->ngpio : count);
4251
+
4252
+ array = kzalloc(struct_size(descs, desc, count) +
4253
+ struct_size(array_info, invert_mask,
4254
+ 3 * bitmap_size), GFP_KERNEL);
4255
+ if (!array) {
4256
+ gpiod_put_array(descs);
4257
+ return ERR_PTR(-ENOMEM);
4258
+ }
4259
+
4260
+ memcpy(array, descs,
4261
+ struct_size(descs, desc, descs->ndescs + 1));
4262
+ kfree(descs);
4263
+
4264
+ descs = array;
4265
+ array_info = (void *)(descs->desc + count);
4266
+ array_info->get_mask = array_info->invert_mask +
4267
+ bitmap_size;
4268
+ array_info->set_mask = array_info->get_mask +
4269
+ bitmap_size;
4270
+
4271
+ array_info->desc = descs->desc;
4272
+ array_info->size = count;
4273
+ array_info->chip = gc;
4274
+ bitmap_set(array_info->get_mask, descs->ndescs,
4275
+ count - descs->ndescs);
4276
+ bitmap_set(array_info->set_mask, descs->ndescs,
4277
+ count - descs->ndescs);
4278
+ descs->info = array_info;
4279
+ }
4280
+ /* Unmark array members which don't belong to the 'fast' chip */
4281
+ if (array_info && array_info->chip != gc) {
4282
+ __clear_bit(descs->ndescs, array_info->get_mask);
4283
+ __clear_bit(descs->ndescs, array_info->set_mask);
4284
+ }
4285
+ /*
4286
+ * Detect array members which belong to the 'fast' chip
4287
+ * but their pins are not in hardware order.
4288
+ */
4289
+ else if (array_info &&
4290
+ gpio_chip_hwgpio(desc) != descs->ndescs) {
4291
+ /*
4292
+ * Don't use fast path if all array members processed so
4293
+ * far belong to the same chip as this one but its pin
4294
+ * hardware number is different from its array index.
4295
+ */
4296
+ if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4297
+ array_info = NULL;
4298
+ } else {
4299
+ __clear_bit(descs->ndescs,
4300
+ array_info->get_mask);
4301
+ __clear_bit(descs->ndescs,
4302
+ array_info->set_mask);
4303
+ }
4304
+ } else if (array_info) {
4305
+ /* Exclude open drain or open source from fast output */
4306
+ if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4307
+ gpiochip_line_is_open_source(gc, descs->ndescs))
4308
+ __clear_bit(descs->ndescs,
4309
+ array_info->set_mask);
4310
+ /* Identify 'fast' pins which require invertion */
4311
+ if (gpiod_is_active_low(desc))
4312
+ __set_bit(descs->ndescs,
4313
+ array_info->invert_mask);
4314
+ }
4315
+
42794316 descs->ndescs++;
42804317 }
4318
+ if (array_info)
4319
+ dev_dbg(dev,
4320
+ "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4321
+ array_info->chip->label, array_info->size,
4322
+ *array_info->get_mask, *array_info->set_mask,
4323
+ *array_info->invert_mask);
42814324 return descs;
42824325 }
42834326 EXPORT_SYMBOL_GPL(gpiod_get_array);
....@@ -4299,7 +4342,7 @@
42994342 struct gpio_descs *descs;
43004343
43014344 descs = gpiod_get_array(dev, con_id, flags);
4302
- if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4345
+ if (PTR_ERR(descs) == -ENOENT)
43034346 return NULL;
43044347
43054348 return descs;
....@@ -4314,7 +4357,8 @@
43144357 */
43154358 void gpiod_put(struct gpio_desc *desc)
43164359 {
4317
- gpiod_free(desc);
4360
+ if (desc)
4361
+ gpiod_free(desc);
43184362 }
43194363 EXPORT_SYMBOL_GPL(gpiod_put);
43204364
....@@ -4333,6 +4377,41 @@
43334377 }
43344378 EXPORT_SYMBOL_GPL(gpiod_put_array);
43354379
4380
+
4381
+static int gpio_bus_match(struct device *dev, struct device_driver *drv)
4382
+{
4383
+ /*
4384
+ * Only match if the fwnode doesn't already have a proper struct device
4385
+ * created for it.
4386
+ */
4387
+ if (dev->fwnode && dev->fwnode->dev != dev)
4388
+ return 0;
4389
+ return 1;
4390
+}
4391
+
4392
+static int gpio_stub_drv_probe(struct device *dev)
4393
+{
4394
+ /*
4395
+ * The DT node of some GPIO chips have a "compatible" property, but
4396
+ * never have a struct device added and probed by a driver to register
4397
+ * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause
4398
+ * the consumers of the GPIO chip to get probe deferred forever because
4399
+ * they will be waiting for a device associated with the GPIO chip
4400
+ * firmware node to get added and bound to a driver.
4401
+ *
4402
+ * To allow these consumers to probe, we associate the struct
4403
+ * gpio_device of the GPIO chip with the firmware node and then simply
4404
+ * bind it to this stub driver.
4405
+ */
4406
+ return 0;
4407
+}
4408
+
4409
+static struct device_driver gpio_stub_drv = {
4410
+ .name = "gpio_stub_drv",
4411
+ .bus = &gpio_bus_type,
4412
+ .probe = gpio_stub_drv_probe,
4413
+};
4414
+
43364415 static int __init gpiolib_dev_init(void)
43374416 {
43384417 int ret;
....@@ -4344,14 +4423,28 @@
43444423 return ret;
43454424 }
43464425
4347
- ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4426
+ ret = driver_register(&gpio_stub_drv);
4427
+ if (ret < 0) {
4428
+ pr_err("gpiolib: could not register GPIO stub driver\n");
4429
+ bus_unregister(&gpio_bus_type);
4430
+ return ret;
4431
+ }
4432
+
4433
+ ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
43484434 if (ret < 0) {
43494435 pr_err("gpiolib: failed to allocate char dev region\n");
4436
+ driver_unregister(&gpio_stub_drv);
43504437 bus_unregister(&gpio_bus_type);
4351
- } else {
4352
- gpiolib_initialized = true;
4353
- gpiochip_setup_devs();
4438
+ return ret;
43544439 }
4440
+
4441
+ gpiolib_initialized = true;
4442
+ gpiochip_setup_devs();
4443
+
4444
+#if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4445
+ WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4446
+#endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
4447
+
43554448 return ret;
43564449 }
43574450 core_initcall(gpiolib_dev_init);
....@@ -4361,11 +4454,12 @@
43614454 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
43624455 {
43634456 unsigned i;
4364
- struct gpio_chip *chip = gdev->chip;
4457
+ struct gpio_chip *gc = gdev->chip;
43654458 unsigned gpio = gdev->base;
43664459 struct gpio_desc *gdesc = &gdev->descs[0];
4367
- int is_out;
4368
- int is_irq;
4460
+ bool is_out;
4461
+ bool is_irq;
4462
+ bool active_low;
43694463
43704464 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
43714465 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
....@@ -4379,11 +4473,13 @@
43794473 gpiod_get_direction(gdesc);
43804474 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
43814475 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4382
- seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4476
+ active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4477
+ seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
43834478 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
43844479 is_out ? "out" : "in ",
4385
- chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
4386
- is_irq ? "IRQ" : " ");
4480
+ gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
4481
+ is_irq ? "IRQ " : "",
4482
+ active_low ? "ACTIVE LOW" : "");
43874483 seq_printf(s, "\n");
43884484 }
43894485 }
....@@ -4433,10 +4529,10 @@
44334529 static int gpiolib_seq_show(struct seq_file *s, void *v)
44344530 {
44354531 struct gpio_device *gdev = v;
4436
- struct gpio_chip *chip = gdev->chip;
4532
+ struct gpio_chip *gc = gdev->chip;
44374533 struct device *parent;
44384534
4439
- if (!chip) {
4535
+ if (!gc) {
44404536 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
44414537 dev_name(&gdev->dev));
44424538 return 0;
....@@ -4445,50 +4541,37 @@
44454541 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
44464542 dev_name(&gdev->dev),
44474543 gdev->base, gdev->base + gdev->ngpio - 1);
4448
- parent = chip->parent;
4544
+ parent = gc->parent;
44494545 if (parent)
44504546 seq_printf(s, ", parent: %s/%s",
44514547 parent->bus ? parent->bus->name : "no-bus",
44524548 dev_name(parent));
4453
- if (chip->label)
4454
- seq_printf(s, ", %s", chip->label);
4455
- if (chip->can_sleep)
4549
+ if (gc->label)
4550
+ seq_printf(s, ", %s", gc->label);
4551
+ if (gc->can_sleep)
44564552 seq_printf(s, ", can sleep");
44574553 seq_printf(s, ":\n");
44584554
4459
- if (chip->dbg_show)
4460
- chip->dbg_show(s, chip);
4555
+ if (gc->dbg_show)
4556
+ gc->dbg_show(s, gc);
44614557 else
44624558 gpiolib_dbg_show(s, gdev);
44634559
44644560 return 0;
44654561 }
44664562
4467
-static const struct seq_operations gpiolib_seq_ops = {
4563
+static const struct seq_operations gpiolib_sops = {
44684564 .start = gpiolib_seq_start,
44694565 .next = gpiolib_seq_next,
44704566 .stop = gpiolib_seq_stop,
44714567 .show = gpiolib_seq_show,
44724568 };
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
-};
4569
+DEFINE_SEQ_ATTRIBUTE(gpiolib);
44864570
44874571 static int __init gpiolib_debugfs_init(void)
44884572 {
44894573 /* /sys/kernel/debug/gpio */
4490
- (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4491
- NULL, NULL, &gpiolib_operations);
4574
+ debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
44924575 return 0;
44934576 }
44944577 subsys_initcall(gpiolib_debugfs_init);