Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add an example "gpioirq" driver that demonstrates interrupts...



details:   https://anonhg.NetBSD.org/src/rev/e20ff8ef9c9e
branches:  trunk
changeset: 361941:e20ff8ef9c9e
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sat May 19 14:15:39 2018 +0000

description:
Add an example "gpioirq" driver that demonstrates interrupts on GPIO
pins.  Will be enhanced in the future to support sending events to
user space on edge-triggered interrupt events.

Based on initial work by Brad Spencer.
PR kern/51676

diffstat:

 distrib/sets/lists/man/mi          |    5 +-
 distrib/sets/lists/modules/mi      |    4 +-
 share/man/man4/Makefile            |    6 +-
 share/man/man4/gpioirq.4           |   97 +++++++++++++++
 sys/dev/gpio/files.gpio            |    7 +-
 sys/dev/gpio/gpioirq.c             |  228 +++++++++++++++++++++++++++++++++++++
 sys/modules/Makefile               |    3 +-
 sys/modules/gpioirq/Makefile       |   13 ++
 sys/modules/gpioirq/gpioirq.ioconf |    8 +
 9 files changed, 364 insertions(+), 7 deletions(-)

diffs (truncated from 468 to 300 lines):

diff -r 5e8cb8ddc66b -r e20ff8ef9c9e distrib/sets/lists/man/mi
--- a/distrib/sets/lists/man/mi Sat May 19 14:11:30 2018 +0000
+++ b/distrib/sets/lists/man/mi Sat May 19 14:15:39 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1586 2018/05/15 00:54:01 nat Exp $
+# $NetBSD: mi,v 1.1587 2018/05/19 14:15:39 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1123,6 +1123,7 @@
 ./usr/share/man/cat4/gpioiic.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/gpiolock.0                        man-sys-catman          .cat
 ./usr/share/man/cat4/gpioow.0                  man-sys-catman          .cat
+./usr/share/man/cat4/gpioirq.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/gpiopwm.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/gpiosim.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/gre.0                     man-sys-catman          .cat
@@ -4263,6 +4264,7 @@
 ./usr/share/man/html4/gpioiic.html             man-sys-htmlman         html
 ./usr/share/man/html4/gpiolock.html            man-sys-htmlman         html
 ./usr/share/man/html4/gpioow.html              man-sys-htmlman         html
+./usr/share/man/html4/gpioirq.html             man-sys-htmlman         html
 ./usr/share/man/html4/gpiopwm.html             man-sys-htmlman         html
 ./usr/share/man/html4/gpiosim.html             man-sys-htmlman         html
 ./usr/share/man/html4/gre.html                 man-sys-htmlman         html
@@ -7177,6 +7179,7 @@
 ./usr/share/man/man4/gpioiic.4                 man-sys-man             .man
 ./usr/share/man/man4/gpiolock.4                        man-sys-man             .man
 ./usr/share/man/man4/gpioow.4                  man-sys-man             .man
+./usr/share/man/man4/gpioirq.4                 man-sys-man             .man
 ./usr/share/man/man4/gpiopwm.4                 man-sys-man             .man
 ./usr/share/man/man4/gpiosim.4                 man-sys-man             .man
 ./usr/share/man/man4/gre.4                     man-sys-man             .man
diff -r 5e8cb8ddc66b -r e20ff8ef9c9e distrib/sets/lists/modules/mi
--- a/distrib/sets/lists/modules/mi     Sat May 19 14:11:30 2018 +0000
+++ b/distrib/sets/lists/modules/mi     Sat May 19 14:15:39 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.114 2018/01/09 03:31:14 christos Exp $
+# $NetBSD: mi,v 1.115 2018/05/19 14:15:39 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -132,6 +132,8 @@
 ./@MODULEDIR@/gpio/gpio.kmod                   base-kernel-modules     kmod
 ./@MODULEDIR@/gpioiic                          base-kernel-modules     kmod
 ./@MODULEDIR@/gpioiic/gpioiic.kmod             base-kernel-modules     kmod
+./@MODULEDIR@/gpioirq                          base-kernel-modules     kmod
+./@MODULEDIR@/gpioirq/gpioirq.kmod             base-kernel-modules     kmod
 ./@MODULEDIR@/gpioow                           base-kernel-modules     kmod
 ./@MODULEDIR@/gpioow/gpioow.kmod               base-kernel-modules     kmod
 ./@MODULEDIR@/gpiosim                          base-kernel-modules     kmod
diff -r 5e8cb8ddc66b -r e20ff8ef9c9e share/man/man4/Makefile
--- a/share/man/man4/Makefile   Sat May 19 14:11:30 2018 +0000
+++ b/share/man/man4/Makefile   Sat May 19 14:15:39 2018 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.652 2018/05/09 08:22:53 msaitoh Exp $
+#      $NetBSD: Makefile,v 1.653 2018/05/19 14:15:39 thorpej Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/18/93
 
 MAN=   aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
@@ -26,8 +26,8 @@
        fast_ipsec.4 fd.4 filemon.4 finsio.4 flash.4 fpa.4 fms.4 fss.4 \
        fujbp.4 full.4 fxp.4 \
        gcscaudio.4 gem.4 genfb.4 gentbi.4 geodeide.4 \
-       glxtphy.4 gpib.4 gpio.4 gpiolock.4 gpiopwm.4 gpiosim.4 gre.4 \
-       gphyter.4 gsip.4 \
+       glxtphy.4 gpib.4 gpio.4 gpioirq.4 gpiolock.4 gpiopwm.4 \
+       gpiosim.4 gre.4 gphyter.4 gsip.4 \
        hdaudio.4 hifn.4 hme.4 hpacel.4 hpqlb.4 hptide.4 hythygtemp.4 \
        ibmcd.4 ibmhawk.4 ichsmb.4 icmp.4 icp.4 icsphy.4 iee.4 ieee80211.4 \
        ifmedia.4 igmafb.4 igphy.4 igsfb.4 iha.4 ihidev.4 ihphy.4 iic.4 ims.4 \
diff -r 5e8cb8ddc66b -r e20ff8ef9c9e share/man/man4/gpioirq.4
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man4/gpioirq.4  Sat May 19 14:15:39 2018 +0000
@@ -0,0 +1,97 @@
+.\" $NetBSD: gpioirq.4,v 1.1 2018/05/19 14:15:39 thorpej Exp $
+.\"
+.\" Copyright (c) 2016 Brad Spencer <brad%anduin.eldar.org@localhost>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd May 11, 2018
+.Dt GPIOIRQ 4
+.Os
+.Sh NAME
+.Nm gpioirq
+.Nd Install an interrupt handler on a GPIO pin
+.Sh SYNOPSIS
+.Cd "gpioirq* at gpio? offset 0 mask 0x1 flag 0x00"
+.Sh DESCRIPTION
+The
+.Nm
+driver attaches an interrupt handler to a single GPIO pin.
+.Pp
+The pin number is specified in the kernel configuration file with the
+.Ar offset
+locator.
+The
+.Ar mask
+locator should always be 0x1.
+.Pp
+The
+.Ar flag
+locator specifies the interrupt mode to use:
+.Bl -tag -width "XXXXXXXX"
+.It Dv 0x01
+Interrupt on the positive
+.Pq rising
+edge of the pin.
+.It Dv 0x02
+Interrupt on the negative
+.Pq falling
+edge of the pin.
+.It Dv 0x04
+Interrupt on both edges of the pin.
+.It Dv 0x08
+Assert the intrerrupt as long as the pin is high.
+.It Dv 0x10
+Assert the interrupt as long as the pin is low.
+.El
+.Pp
+Note that the interrupts modes are mutually-exclusive, and exactly one
+interrupt mode must be specified.
+These flags correspond to the
+.Dv GPIO_INTR
+mode bits defined in
+.Pa sys/gpio.h .
+In addition to the interrupt mode, setting
+.Dv 0x1000
+in
+.Ar flags
+will enable the printing of a message to the console whenever the
+interrupt handler is called.
+.Pp
+The
+.Ar offset ,
+.Ar mask ,
+and
+.Ar flag
+locators can also be specified when
+.Nm
+is attached at runtime using the
+.Dv GPIOATTACH
+.Xr ioctl 2
+on the
+.Xr gpio 4
+device.
+.Sh SEE ALSO
+.Xr gpio 4 ,
+.Xr drvctl 8 ,
+.Xr gpioctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Nx 9.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Brad Spencer Aq Mt brad%anduin.eldar.org@localhost
diff -r 5e8cb8ddc66b -r e20ff8ef9c9e sys/dev/gpio/files.gpio
--- a/sys/dev/gpio/files.gpio   Sat May 19 14:11:30 2018 +0000
+++ b/sys/dev/gpio/files.gpio   Sat May 19 14:15:39 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.gpio,v 1.14 2015/10/15 09:07:49 jmcneill Exp $
+# $NetBSD: files.gpio,v 1.15 2018/05/19 14:15:39 thorpej Exp $
 
 define gpio {[offset = -1], [mask = 0], [flag = 0]}
 
@@ -39,3 +39,8 @@
 device gpiobutton: gpiobus, sysmon_power, sysmon_taskq
 attach gpiobutton at gpio
 file   dev/gpio/gpiobutton.c                   gpiobutton
+
+# GPIO interrupt example
+device gpioirq: gpiobus
+attach gpioirq at gpio
+file   dev/gpio/gpioirq.c                      gpioirq
diff -r 5e8cb8ddc66b -r e20ff8ef9c9e sys/dev/gpio/gpioirq.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/gpio/gpioirq.c    Sat May 19 14:15:39 2018 +0000
@@ -0,0 +1,228 @@
+/* $NetBSD: gpioirq.c,v 1.1 2018/05/19 14:15:39 thorpej Exp $ */
+
+/*
+ * Copyright (c) 2016 Brad Spencer <brad%anduin.eldar.org@localhost>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: gpioirq.c,v 1.1 2018/05/19 14:15:39 thorpej Exp $");
+
+/*
+ * Example GPIO driver that uses interrupts.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/gpio.h>
+#include <sys/module.h>
+
+#include <dev/gpio/gpiovar.h>
+
+#define        GPIOIRQ_NPINS           1
+
+struct gpioirq_softc {
+       device_t                sc_dev;
+       void *                  sc_gpio;
+       struct gpio_pinmap      sc_map;
+       int                     _map[GPIOIRQ_NPINS];
+       char                    sc_intrstr[128];
+       void *                  sc_ih;
+       kmutex_t                sc_lock;
+       bool                    sc_verbose;
+       bool                    sc_functional;
+};
+
+#define        GPIOIRQ_FLAGS_IRQMODE   GPIO_INTR_MODE_MASK
+#define        GPIOIRQ_FLAGS_VERBOSE   0x1000
+
+static int     gpioirq_match(device_t, cfdata_t, void *);
+static void    gpioirq_attach(device_t, device_t, void *);
+static int     gpioirq_detach(device_t, int);
+static int     gpioirq_activate(device_t, enum devact);
+
+static int     gpioirq_intr(void *);
+
+CFATTACH_DECL_NEW(gpioirq, sizeof(struct gpioirq_softc),
+                 gpioirq_match, gpioirq_attach,
+                 gpioirq_detach, gpioirq_activate);
+
+extern struct cfdriver gpioirq_cd;
+
+static int
+gpioirq_match(device_t parent, cfdata_t cf, void *aux)
+{
+       struct gpio_attach_args *ga = aux;
+       int npins;
+
+       if (strcmp(ga->ga_dvname, cf->cf_name))
+               return (0);
+       
+       if (ga->ga_offset == -1)
+               return (0);
+
+       npins = gpio_npins(ga->ga_mask);
+       if (npins > 1)
+               return (0);
+
+       return (1);
+}
+
+static void
+gpioirq_attach(device_t parent, device_t self, void *aux)
+{
+       struct gpioirq_softc *sc = device_private(self);
+       struct gpio_attach_args *ga = aux;
+       int npins = gpio_npins(ga->ga_mask);
+       int irqmode, flags;
+
+       sc->sc_dev = self;
+
+       /* Map pins */
+       sc->sc_gpio = ga->ga_gpio;
+       sc->sc_map.pm_map = sc->_map;
+
+       /* We always map just 1 pin. */
+       if (gpio_pin_map(sc->sc_gpio, ga->ga_offset,
+                        npins ? ga->ga_mask : 0x1, &sc->sc_map)) {
+               aprint_error(": can't map pins\n");
+               return;
+       }
+
+       aprint_normal("\n");
+
+       if (ga->ga_flags & GPIOIRQ_FLAGS_VERBOSE)



Home | Main Index | Thread Index | Old Index