aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpiolib.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-07-26 22:16:01 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-07-26 22:16:01 -0400
commit1cd04d293c818687795b83cd8f2626bd4662feeb (patch)
treedcbaadd82c02204114b99c418bfae1ee57b2c4ca /drivers/gpio/gpiolib.c
parent9c1958fc326a0a0a533ec8e86ea6fa30977207de (diff)
parent224f9e6d538c4cfb2fa8dc4206fceb9431271388 (diff)
Merge tag 'gpio-v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio
Pull GPIO updates from Linus Walleij: "This is the bulk of GPIO changes for the v4.8 kernel cycle. The big news is the completion of the chardev ABI which I'm very happy about and apart from that it's an ordinary, quite busy cycle. The details are below. The patches are tested in linux-next for some time, patches to other subsystem mostly have ACKs. I got overly ambitious with configureing lines as input for IRQ lines but it turns out that some controllers have their interrupt-enable and input-enabling in orthogonal settings so the assumption that all IRQ lines are input lines does not hold. Oh well, revert and back to the drawing board with that. Core changes: - The big item is of course the completion of the character device ABI. It has now replaced and surpassed the former unmaintainable sysfs ABI: we can now hammer (bitbang) individual lines or sets of lines and read individual lines or sets of lines from userspace, and we can also register to listen to GPIO events from userspace. As a tie-in we have two new tools in tools/gpio: gpio-hammer and gpio-event-mon that illustrate the proper use of the new ABI. As someone said: the wild west days of GPIO are now over. - Continued to remove the pointless ARCH_[WANT_OPTIONAL|REQUIRE]_GPIOLIB Kconfig symbols. I'm patching hexagon, openrisc, powerpc, sh, unicore, ia64 and microblaze. These are either ACKed by their maintainers or patched anyways after a grace period and no response from maintainers. Some archs (ARM) come in from their trees, and others (x86) are still not fixed, so I might send a second pull request to root it out later in this merge window, or just defer to v4.9. - The GPIO tools are moved to the tools build system. New drivers: - New driver for the MAX77620/MAX20024. - New driver for the Intel Merrifield. - Enabled PCA953x for the TI PCA9536. - Enabled PCA953x for the Intel Edison. - Enabled R8A7792 in the RCAR driver. Driver improvements: - The STMPE and F7188x now supports the .get_direction() callback. - The Xilinx driver supports setting multiple lines at once. - ACPI support for the Vulcan GPIO controller. - The MMIO GPIO driver supports device tree probing. - The Acer One 10 is supported through the _DEP ACPI attribute. Cleanups: - A major cleanup of the OF/DT support code. It is way easier to read and understand now, probably this improves performance too. - Drop a few redundant .owner assignments. - Remove CLPS711x boardfile support: we are 100% DT" * tag 'gpio-v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: (67 commits) MAINTAINERS: Add INTEL MERRIFIELD GPIO entry gpio: dwapb: add missing fwnode_handle_put() in dwapb_gpio_get_pdata() gpio: merrifield: Protect irq_ack() and gpio_set() by lock gpio: merrifield: Introduce GPIO driver to support Merrifield gpio: intel-mid: Make it depend to X86_INTEL_MID gpio: intel-mid: Sort header block alphabetically gpio: intel-mid: Remove potentially harmful code gpio: rcar: add R8A7792 support gpiolib: remove duplicated include from gpiolib.c Revert "gpio: convince line to become input in irq helper" gpiolib: of_find_gpio(): Don't discard errors gpio: of: Allow overriding the device node gpio: free handles in fringe cases gpio: tps65218: Add platform_device_id table gpio: max77620: get gpio value based on direction gpio: lynxpoint: avoid potential warning on error path tools/gpio: add install section tools/gpio: move to tools buildsystem gpio: intel-mid: switch to devm_gpiochip_add_data() gpio: 74x164: Use spi_write() helper instead of open coding ...
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r--drivers/gpio/gpiolib.c511
1 files changed, 505 insertions, 6 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index be74bd370f1f..53ff25ac66d8 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -16,11 +16,14 @@
16#include <linux/gpio/driver.h> 16#include <linux/gpio/driver.h>
17#include <linux/gpio/machine.h> 17#include <linux/gpio/machine.h>
18#include <linux/pinctrl/consumer.h> 18#include <linux/pinctrl/consumer.h>
19#include <linux/idr.h>
20#include <linux/cdev.h> 19#include <linux/cdev.h>
21#include <linux/fs.h> 20#include <linux/fs.h>
22#include <linux/uaccess.h> 21#include <linux/uaccess.h>
23#include <linux/compat.h> 22#include <linux/compat.h>
23#include <linux/anon_inodes.h>
24#include <linux/kfifo.h>
25#include <linux/poll.h>
26#include <linux/timekeeping.h>
24#include <uapi/linux/gpio.h> 27#include <uapi/linux/gpio.h>
25 28
26#include "gpiolib.h" 29#include "gpiolib.h"
@@ -310,6 +313,497 @@ static int gpiochip_set_desc_names(struct gpio_chip *gc)
310 return 0; 313 return 0;
311} 314}
312 315
316/*
317 * GPIO line handle management
318 */
319
320/**
321 * struct linehandle_state - contains the state of a userspace handle
322 * @gdev: the GPIO device the handle pertains to
323 * @label: consumer label used to tag descriptors
324 * @descs: the GPIO descriptors held by this handle
325 * @numdescs: the number of descriptors held in the descs array
326 */
327struct linehandle_state {
328 struct gpio_device *gdev;
329 const char *label;
330 struct gpio_desc *descs[GPIOHANDLES_MAX];
331 u32 numdescs;
332};
333
334static long linehandle_ioctl(struct file *filep, unsigned int cmd,
335 unsigned long arg)
336{
337 struct linehandle_state *lh = filep->private_data;
338 void __user *ip = (void __user *)arg;
339 struct gpiohandle_data ghd;
340 int i;
341
342 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
343 int val;
344
345 /* TODO: check if descriptors are really input */
346 for (i = 0; i < lh->numdescs; i++) {
347 val = gpiod_get_value_cansleep(lh->descs[i]);
348 if (val < 0)
349 return val;
350 ghd.values[i] = val;
351 }
352
353 if (copy_to_user(ip, &ghd, sizeof(ghd)))
354 return -EFAULT;
355
356 return 0;
357 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
358 int vals[GPIOHANDLES_MAX];
359
360 /* TODO: check if descriptors are really output */
361 if (copy_from_user(&ghd, ip, sizeof(ghd)))
362 return -EFAULT;
363
364 /* Clamp all values to [0,1] */
365 for (i = 0; i < lh->numdescs; i++)
366 vals[i] = !!ghd.values[i];
367
368 /* Reuse the array setting function */
369 gpiod_set_array_value_complex(false,
370 true,
371 lh->numdescs,
372 lh->descs,
373 vals);
374 return 0;
375 }
376 return -EINVAL;
377}
378
379#ifdef CONFIG_COMPAT
380static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
381 unsigned long arg)
382{
383 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
384}
385#endif
386
387static int linehandle_release(struct inode *inode, struct file *filep)
388{
389 struct linehandle_state *lh = filep->private_data;
390 struct gpio_device *gdev = lh->gdev;
391 int i;
392
393 for (i = 0; i < lh->numdescs; i++)
394 gpiod_free(lh->descs[i]);
395 kfree(lh->label);
396 kfree(lh);
397 put_device(&gdev->dev);
398 return 0;
399}
400
401static const struct file_operations linehandle_fileops = {
402 .release = linehandle_release,
403 .owner = THIS_MODULE,
404 .llseek = noop_llseek,
405 .unlocked_ioctl = linehandle_ioctl,
406#ifdef CONFIG_COMPAT
407 .compat_ioctl = linehandle_ioctl_compat,
408#endif
409};
410
411static int linehandle_create(struct gpio_device *gdev, void __user *ip)
412{
413 struct gpiohandle_request handlereq;
414 struct linehandle_state *lh;
415 int fd, i, ret;
416
417 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
418 return -EFAULT;
419 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
420 return -EINVAL;
421
422 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
423 if (!lh)
424 return -ENOMEM;
425 lh->gdev = gdev;
426 get_device(&gdev->dev);
427
428 /* Make sure this is terminated */
429 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
430 if (strlen(handlereq.consumer_label)) {
431 lh->label = kstrdup(handlereq.consumer_label,
432 GFP_KERNEL);
433 if (!lh->label) {
434 ret = -ENOMEM;
435 goto out_free_lh;
436 }
437 }
438
439 /* Request each GPIO */
440 for (i = 0; i < handlereq.lines; i++) {
441 u32 offset = handlereq.lineoffsets[i];
442 u32 lflags = handlereq.flags;
443 struct gpio_desc *desc;
444
445 desc = &gdev->descs[offset];
446 ret = gpiod_request(desc, lh->label);
447 if (ret)
448 goto out_free_descs;
449 lh->descs[i] = desc;
450
451 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
452 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
453 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
454 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
455 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
456 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
457
458 /*
459 * Lines have to be requested explicitly for input
460 * or output, else the line will be treated "as is".
461 */
462 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
463 int val = !!handlereq.default_values[i];
464
465 ret = gpiod_direction_output(desc, val);
466 if (ret)
467 goto out_free_descs;
468 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
469 ret = gpiod_direction_input(desc);
470 if (ret)
471 goto out_free_descs;
472 }
473 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
474 offset);
475 }
476 /* Let i point at the last handle */
477 i--;
478 lh->numdescs = handlereq.lines;
479
480 fd = anon_inode_getfd("gpio-linehandle",
481 &linehandle_fileops,
482 lh,
483 O_RDONLY | O_CLOEXEC);
484 if (fd < 0) {
485 ret = fd;
486 goto out_free_descs;
487 }
488
489 handlereq.fd = fd;
490 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
491 ret = -EFAULT;
492 goto out_free_descs;
493 }
494
495 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
496 lh->numdescs);
497
498 return 0;
499
500out_free_descs:
501 for (; i >= 0; i--)
502 gpiod_free(lh->descs[i]);
503 kfree(lh->label);
504out_free_lh:
505 kfree(lh);
506 put_device(&gdev->dev);
507 return ret;
508}
509
510/*
511 * GPIO line event management
512 */
513
514/**
515 * struct lineevent_state - contains the state of a userspace event
516 * @gdev: the GPIO device the event pertains to
517 * @label: consumer label used to tag descriptors
518 * @desc: the GPIO descriptor held by this event
519 * @eflags: the event flags this line was requested with
520 * @irq: the interrupt that trigger in response to events on this GPIO
521 * @wait: wait queue that handles blocking reads of events
522 * @events: KFIFO for the GPIO events
523 * @read_lock: mutex lock to protect reads from colliding with adding
524 * new events to the FIFO
525 */
526struct lineevent_state {
527 struct gpio_device *gdev;
528 const char *label;
529 struct gpio_desc *desc;
530 u32 eflags;
531 int irq;
532 wait_queue_head_t wait;
533 DECLARE_KFIFO(events, struct gpioevent_data, 16);
534 struct mutex read_lock;
535};
536
537static unsigned int lineevent_poll(struct file *filep,
538 struct poll_table_struct *wait)
539{
540 struct lineevent_state *le = filep->private_data;
541 unsigned int events = 0;
542
543 poll_wait(filep, &le->wait, wait);
544
545 if (!kfifo_is_empty(&le->events))
546 events = POLLIN | POLLRDNORM;
547
548 return events;
549}
550
551
552static ssize_t lineevent_read(struct file *filep,
553 char __user *buf,
554 size_t count,
555 loff_t *f_ps)
556{
557 struct lineevent_state *le = filep->private_data;
558 unsigned int copied;
559 int ret;
560
561 if (count < sizeof(struct gpioevent_data))
562 return -EINVAL;
563
564 do {
565 if (kfifo_is_empty(&le->events)) {
566 if (filep->f_flags & O_NONBLOCK)
567 return -EAGAIN;
568
569 ret = wait_event_interruptible(le->wait,
570 !kfifo_is_empty(&le->events));
571 if (ret)
572 return ret;
573 }
574
575 if (mutex_lock_interruptible(&le->read_lock))
576 return -ERESTARTSYS;
577 ret = kfifo_to_user(&le->events, buf, count, &copied);
578 mutex_unlock(&le->read_lock);
579
580 if (ret)
581 return ret;
582
583 /*
584 * If we couldn't read anything from the fifo (a different
585 * thread might have been faster) we either return -EAGAIN if
586 * the file descriptor is non-blocking, otherwise we go back to
587 * sleep and wait for more data to arrive.
588 */
589 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
590 return -EAGAIN;
591
592 } while (copied == 0);
593
594 return copied;
595}
596
597static int lineevent_release(struct inode *inode, struct file *filep)
598{
599 struct lineevent_state *le = filep->private_data;
600 struct gpio_device *gdev = le->gdev;
601
602 free_irq(le->irq, le);
603 gpiod_free(le->desc);
604 kfree(le->label);
605 kfree(le);
606 put_device(&gdev->dev);
607 return 0;
608}
609
610static long lineevent_ioctl(struct file *filep, unsigned int cmd,
611 unsigned long arg)
612{
613 struct lineevent_state *le = filep->private_data;
614 void __user *ip = (void __user *)arg;
615 struct gpiohandle_data ghd;
616
617 /*
618 * We can get the value for an event line but not set it,
619 * because it is input by definition.
620 */
621 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
622 int val;
623
624 val = gpiod_get_value_cansleep(le->desc);
625 if (val < 0)
626 return val;
627 ghd.values[0] = val;
628
629 if (copy_to_user(ip, &ghd, sizeof(ghd)))
630 return -EFAULT;
631
632 return 0;
633 }
634 return -EINVAL;
635}
636
637#ifdef CONFIG_COMPAT
638static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
639 unsigned long arg)
640{
641 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
642}
643#endif
644
645static const struct file_operations lineevent_fileops = {
646 .release = lineevent_release,
647 .read = lineevent_read,
648 .poll = lineevent_poll,
649 .owner = THIS_MODULE,
650 .llseek = noop_llseek,
651 .unlocked_ioctl = lineevent_ioctl,
652#ifdef CONFIG_COMPAT
653 .compat_ioctl = lineevent_ioctl_compat,
654#endif
655};
656
657static irqreturn_t lineevent_irq_thread(int irq, void *p)
658{
659 struct lineevent_state *le = p;
660 struct gpioevent_data ge;
661 int ret;
662
663 ge.timestamp = ktime_get_real_ns();
664
665 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
666 int level = gpiod_get_value_cansleep(le->desc);
667
668 if (level)
669 /* Emit low-to-high event */
670 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
671 else
672 /* Emit high-to-low event */
673 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
674 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
675 /* Emit low-to-high event */
676 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
677 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
678 /* Emit high-to-low event */
679 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
680 } else {
681 return IRQ_NONE;
682 }
683
684 ret = kfifo_put(&le->events, ge);
685 if (ret != 0)
686 wake_up_poll(&le->wait, POLLIN);
687
688 return IRQ_HANDLED;
689}
690
691static int lineevent_create(struct gpio_device *gdev, void __user *ip)
692{
693 struct gpioevent_request eventreq;
694 struct lineevent_state *le;
695 struct gpio_desc *desc;
696 u32 offset;
697 u32 lflags;
698 u32 eflags;
699 int fd;
700 int ret;
701 int irqflags = 0;
702
703 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
704 return -EFAULT;
705
706 le = kzalloc(sizeof(*le), GFP_KERNEL);
707 if (!le)
708 return -ENOMEM;
709 le->gdev = gdev;
710 get_device(&gdev->dev);
711
712 /* Make sure this is terminated */
713 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
714 if (strlen(eventreq.consumer_label)) {
715 le->label = kstrdup(eventreq.consumer_label,
716 GFP_KERNEL);
717 if (!le->label) {
718 ret = -ENOMEM;
719 goto out_free_le;
720 }
721 }
722
723 offset = eventreq.lineoffset;
724 lflags = eventreq.handleflags;
725 eflags = eventreq.eventflags;
726
727 /* This is just wrong: we don't look for events on output lines */
728 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
729 ret = -EINVAL;
730 goto out_free_label;
731 }
732
733 desc = &gdev->descs[offset];
734 ret = gpiod_request(desc, le->label);
735 if (ret)
736 goto out_free_desc;
737 le->desc = desc;
738 le->eflags = eflags;
739
740 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
741 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
742 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
743 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
744 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
745 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
746
747 ret = gpiod_direction_input(desc);
748 if (ret)
749 goto out_free_desc;
750
751 le->irq = gpiod_to_irq(desc);
752 if (le->irq <= 0) {
753 ret = -ENODEV;
754 goto out_free_desc;
755 }
756
757 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
758 irqflags |= IRQF_TRIGGER_RISING;
759 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
760 irqflags |= IRQF_TRIGGER_FALLING;
761 irqflags |= IRQF_ONESHOT;
762 irqflags |= IRQF_SHARED;
763
764 INIT_KFIFO(le->events);
765 init_waitqueue_head(&le->wait);
766 mutex_init(&le->read_lock);
767
768 /* Request a thread to read the events */
769 ret = request_threaded_irq(le->irq,
770 NULL,
771 lineevent_irq_thread,
772 irqflags,
773 le->label,
774 le);
775 if (ret)
776 goto out_free_desc;
777
778 fd = anon_inode_getfd("gpio-event",
779 &lineevent_fileops,
780 le,
781 O_RDONLY | O_CLOEXEC);
782 if (fd < 0) {
783 ret = fd;
784 goto out_free_irq;
785 }
786
787 eventreq.fd = fd;
788 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
789 ret = -EFAULT;
790 goto out_free_irq;
791 }
792
793 return 0;
794
795out_free_irq:
796 free_irq(le->irq, le);
797out_free_desc:
798 gpiod_free(le->desc);
799out_free_label:
800 kfree(le->label);
801out_free_le:
802 kfree(le);
803 put_device(&gdev->dev);
804 return ret;
805}
806
313/** 807/**
314 * gpio_ioctl() - ioctl handler for the GPIO chardev 808 * gpio_ioctl() - ioctl handler for the GPIO chardev
315 */ 809 */
@@ -385,6 +879,10 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
385 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) 879 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
386 return -EFAULT; 880 return -EFAULT;
387 return 0; 881 return 0;
882 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
883 return linehandle_create(gdev, ip);
884 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
885 return lineevent_create(gdev, ip);
388 } 886 }
389 return -EINVAL; 887 return -EINVAL;
390} 888}
@@ -548,13 +1046,14 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
548 if (chip->parent) { 1046 if (chip->parent) {
549 gdev->dev.parent = chip->parent; 1047 gdev->dev.parent = chip->parent;
550 gdev->dev.of_node = chip->parent->of_node; 1048 gdev->dev.of_node = chip->parent->of_node;
551 } else { 1049 }
1050
552#ifdef CONFIG_OF_GPIO 1051#ifdef CONFIG_OF_GPIO
553 /* If the gpiochip has an assigned OF node this takes precedence */ 1052 /* If the gpiochip has an assigned OF node this takes precedence */
554 if (chip->of_node) 1053 if (chip->of_node)
555 gdev->dev.of_node = chip->of_node; 1054 gdev->dev.of_node = chip->of_node;
556#endif 1055#endif
557 } 1056
558 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); 1057 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
559 if (gdev->id < 0) { 1058 if (gdev->id < 0) {
560 status = gdev->id; 1059 status = gdev->id;
@@ -2333,7 +2832,7 @@ static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2333 2832
2334 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, 2833 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2335 &of_flags); 2834 &of_flags);
2336 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) 2835 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
2337 break; 2836 break;
2338 } 2837 }
2339 2838