Source-Changes-HG archive

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

[src/trunk]: src add Microchip MCP23S17 GPIO driver



details:   https://anonhg.NetBSD.org/src/rev/1a348bb1a061
branches:  trunk
changeset: 328544:1a348bb1a061
user:      kardel <kardel%NetBSD.org@localhost>
date:      Sun Apr 06 17:59:39 2014 +0000

description:
add Microchip MCP23S17 GPIO driver
(via SPI interface)

diffstat:

 distrib/sets/lists/man/mi     |    5 +-
 doc/CHANGES                   |    4 +-
 share/man/man4/Makefile       |    5 +-
 share/man/man4/mcp23s17gpio.4 |   61 ++++++++
 sys/arch/evbarm/conf/RPI      |   11 +-
 sys/dev/spi/files.spi         |    6 +-
 sys/dev/spi/mcp23s17.c        |  301 ++++++++++++++++++++++++++++++++++++++++++
 sys/dev/spi/mcp23s17.h        |   96 +++++++++++++
 8 files changed, 483 insertions(+), 6 deletions(-)

diffs (truncated from 581 to 300 lines):

diff -r eb8e0921d151 -r 1a348bb1a061 distrib/sets/lists/man/mi
--- a/distrib/sets/lists/man/mi Sun Apr 06 17:13:57 2014 +0000
+++ b/distrib/sets/lists/man/mi Sun Apr 06 17:59:39 2014 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1467 2014/04/01 21:40:46 christos Exp $
+# $NetBSD: mi,v 1.1468 2014/04/06 17:59:39 kardel Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1391,6 +1391,7 @@
 ./usr/share/man/cat4/mlx.0                     man-sys-catman          .cat
 ./usr/share/man/cat4/mly.0                     man-sys-catman          .cat
 ./usr/share/man/cat4/module.0                  man-obsolete            obsolete
+./usr/share/man/cat4/mcp23s17gpio.0            man-sys-catman          .cat
 ./usr/share/man/cat4/mpii.0                    man-sys-catman          .cat
 ./usr/share/man/cat4/mpl115a.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/mpls.0                    man-sys-catman          .cat
@@ -4387,6 +4388,7 @@
 ./usr/share/man/html4/mlx.html                 man-sys-htmlman         html
 ./usr/share/man/html4/mly.html                 man-sys-htmlman         html
 ./usr/share/man/html4/module.html              man-obsolete            obsolete
+./usr/share/man/html4/mcp23s17gpio.html                man-sys-htmlman         html
 ./usr/share/man/html4/mpii.html                        man-sys-htmlman         html
 ./usr/share/man/html4/mpl115a.html             man-sys-htmlman         html
 ./usr/share/man/html4/mpls.html                        man-sys-htmlman         html
@@ -7231,6 +7233,7 @@
 ./usr/share/man/man4/mlx.4                     man-sys-man             .man
 ./usr/share/man/man4/mly.4                     man-sys-man             .man
 ./usr/share/man/man4/module.4                  man-obsolete            obsolete
+./usr/share/man/man4/mcp23s17gpio.4            man-sys-man             .man
 ./usr/share/man/man4/mpii.4                    man-sys-man             .man
 ./usr/share/man/man4/mpl115a.4                 man-sys-man             .man
 ./usr/share/man/man4/mpls.4                    man-sys-man             .man
diff -r eb8e0921d151 -r 1a348bb1a061 doc/CHANGES
--- a/doc/CHANGES       Sun Apr 06 17:13:57 2014 +0000
+++ b/doc/CHANGES       Sun Apr 06 17:59:39 2014 +0000
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:                   <$Revision: 1.1910 $>
+# LIST OF CHANGES FROM LAST RELEASE:                   <$Revision: 1.1911 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -382,3 +382,5 @@
        ptyfs: Allow multiple mounts and support proper device visibility
             in chroots (Ilya Zykov) [christos 20140404]
        i386: Switched to GCC 4.8.  [christos 20140404]
+       mcp23s17gpio: Add Microchip MCP23S17 16-bit GPIO chip driver.
+               e.g. Rasberry Pi interface boards [kardel 20140406]
\ No newline at end of file
diff -r eb8e0921d151 -r 1a348bb1a061 share/man/man4/Makefile
--- a/share/man/man4/Makefile   Sun Apr 06 17:13:57 2014 +0000
+++ b/share/man/man4/Makefile   Sun Apr 06 17:59:39 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.611 2014/03/19 15:26:41 nonaka Exp $
+#      $NetBSD: Makefile,v 1.612 2014/04/06 17:59:39 kardel Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/18/93
 
 MAN=   aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
@@ -37,7 +37,8 @@
        jme.4 jmide.4 joy.4 \
        kloader.4 kse.4 ksyms.4 kttcp.4 \
        lc.4 ld.4 lii.4 lo.4 lua.4 lxtphy.4 \
-       mainbus.4 makphy.4 malo.4 mbe.4 mca.4 mcclock.4 md.4 mfb.4 mfi.4 mhzc.4 \
+       mainbus.4 makphy.4 malo.4 mbe.4 mca.4 mcclock.4 mcp23s17gpio.4 md.4 mfb.4 \
+       mfi.4 mhzc.4 \
        micphy.4 midi.4 mii.4 mk48txx.4 mlx.4 mly.4 mpls.4 mpii.4 mpt.4 mpu.4 mtd.4 \
        mtio.4 msm6242b.4 multicast.4 mvsata.4 \
        nadb.4 ne.4 neo.4 netintro.4 nfe.4 nfsmb.4 njata.4 njs.4 \
diff -r eb8e0921d151 -r 1a348bb1a061 share/man/man4/mcp23s17gpio.4
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man4/mcp23s17gpio.4     Sun Apr 06 17:59:39 2014 +0000
@@ -0,0 +1,61 @@
+.\"    $NetBSD: mcp23s17gpio.4,v 1.1 2014/04/06 17:59:39 kardel Exp $
+.\"
+.\"Copyright (c) 2014 Frank Kardel
+.\"All rights reserved.
+.\"
+.\"Redistribution and use in source and binary forms, with or without
+.\"modification, are permitted provided that the following conditions
+.\"are met:
+.\"1. Redistributions of source code must retain the above copyright
+.\"   notice, this list of conditions and the following disclaimer.
+.\"2. Redistributions in binary form must reproduce the above copyright
+.\"   notice, this list of conditions and the following disclaimer in the
+.\"   documentation and/or other materials provided with the distribution.
+.\"
+.\"THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
+.\"``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\"TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\"PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\"BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\"CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\"SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\"INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\"CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\"ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\"POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd April 6th, 2014
+.Dt MCP23S17GPIO 4
+.Os
+.Sh NAME
+.Nm mcp23s17gpio
+.Nd Driver for MCP23S17 gpio chip via SPI bus
+.Sh SYNOPSIS
+.Cd "mcp23s17gpio0 at spi? slave 0 flags 0"
+.Cd "mcp23s17gpio1 at spi? slave 0 flags 1"
+.Cd "mcp23s17gpio2 at spi? slave 0 flags 2"
+.Cd "mcp23s17gpio3 at spi? slave 0 flags 3"
+.Cd "gpio* at gpiobus?"
+.Sh DESCRIPTION
+The
+.Nm
+driver supports up to 8 instances of the MCP23S17 16-bit GPIO chips. Access to
+the pins is provided by the
+.Xr gpio 4
+interface. The
+.Nm flags argument selects the hardware address of the chip instance.
+.Sh SEE ALSO
+.Xr spi 4 ,
+.Xr intro 4 ,
+.Xr gpio 4
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Nx 7.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Frank Kardel Aq Mt kardel%netbsd.org@localhost .
diff -r eb8e0921d151 -r 1a348bb1a061 sys/arch/evbarm/conf/RPI
--- a/sys/arch/evbarm/conf/RPI  Sun Apr 06 17:13:57 2014 +0000
+++ b/sys/arch/evbarm/conf/RPI  Sun Apr 06 17:59:39 2014 +0000
@@ -1,5 +1,5 @@
 #
-#      $NetBSD: RPI,v 1.42 2014/01/31 08:18:03 skrll Exp $
+#      $NetBSD: RPI,v 1.43 2014/04/06 17:59:39 kardel Exp $
 #
 #      RPi -- Raspberry Pi
 #
@@ -225,6 +225,15 @@
 bcmspi* at obio?
 spi* at spibus?
 
+# PIFace or other boards using that chip (needs gpio)
+#mcp23s17gpio0 at spi? slave 0 flags 0
+#mcp23s17gpio1 at spi? slave 0 flags 1 
+#mcp23s17gpio2 at spi? slave 0 flags 2
+#mcp23s17gpio3 at spi? slave 0 flags 3
+
+# gpio support (e. g. mcp23s17gpio)
+#gpio*         at gpiobus?
+
 # various options for wscons - we try to look as much like a standard
 # sun console as possible
 options        WSEMUL_VT100            # sun terminal emulation
diff -r eb8e0921d151 -r 1a348bb1a061 sys/dev/spi/files.spi
--- a/sys/dev/spi/files.spi     Sun Apr 06 17:13:57 2014 +0000
+++ b/sys/dev/spi/files.spi     Sun Apr 06 17:59:39 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.spi,v 1.3 2014/02/25 20:09:37 rkujawa Exp $
+#      $NetBSD: files.spi,v 1.4 2014/04/06 17:59:39 kardel Exp $
 
 define spibus { }
 
@@ -31,3 +31,7 @@
 attach mcp48x1dac at spi
 file   dev/spi/mcp48x1.c               mcp48x1dac      
 
+# MCP23S17 16-bit GPIO
+device mcp23s17gpio: gpiobus
+attach mcp23s17gpio at spi
+file   dev/spi/mcp23s17.c              mcp23s17gpio
diff -r eb8e0921d151 -r 1a348bb1a061 sys/dev/spi/mcp23s17.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/spi/mcp23s17.c    Sun Apr 06 17:59:39 2014 +0000
@@ -0,0 +1,301 @@
+/*      $NetBSD: mcp23s17.c,v 1.1 2014/04/06 17:59:39 kardel Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Frank Kardel.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: mcp23s17.c,v 1.1 2014/04/06 17:59:39 kardel Exp $");
+
+/* 
+ * Driver for Microchip MCP23S17 GPIO
+ *
+ * see: http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
+ */
+
+#include "gpio.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/gpio.h>
+#include <sys/sysctl.h>
+
+#include <dev/gpio/gpiovar.h>
+
+#include <dev/spi/spivar.h>
+
+#include <dev/spi/mcp23s17.h>
+
+/* #define MCP23S17_DEBUG */
+#ifdef MCP23S17_DEBUG
+int mcp23S17debug = 3;
+#define DPRINTF(l, x)  do { if (l <= mcp23S17debug) { printf x; } } while (0)
+#else
+#define DPRINTF(l, x)
+#endif
+
+struct mcp23s17gpio_softc {
+       device_t                sc_dev;
+       struct spi_handle      *sc_sh; 
+       uint8_t                 sc_ha;   /* hardware address */
+       uint8_t                 sc_bank; /* addressing scheme */
+       struct gpio_chipset_tag sc_gpio_gc;
+       gpio_pin_t              sc_gpio_pins[MCP23x17_GPIO_NPINS];
+};
+
+static int     mcp23s17gpio_match(device_t, cfdata_t, void *);
+static void    mcp23s17gpio_attach(device_t, device_t, void *);
+
+static void     mcp23s17gpio_write(struct mcp23s17gpio_softc *, uint8_t, uint8_t);
+
+#if NGPIO > 0
+static uint8_t  mcp23s17gpio_read(struct mcp23s17gpio_softc *, uint8_t);
+
+static int      mcp23s17gpio_gpio_pin_read(void *, int);
+static void     mcp23s17gpio_gpio_pin_write(void *, int, int);
+static void     mcp23s17gpio_gpio_pin_ctl(void *, int, int);
+#endif
+
+CFATTACH_DECL_NEW(mcp23s17gpio, sizeof(struct mcp23s17gpio_softc),
+                 mcp23s17gpio_match, mcp23s17gpio_attach, NULL, NULL);
+
+static int
+mcp23s17gpio_match(device_t parent, cfdata_t cf, void *aux)
+{
+       struct spi_attach_args *sa = aux;
+
+       /* MCP23S17 has no way to detect it! */
+
+       /* run at 10MHz */
+       if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
+               return 0;
+
+       return 1;
+}
+
+static void
+mcp23s17gpio_attach(device_t parent, device_t self, void *aux)
+{
+       struct mcp23s17gpio_softc *sc;
+       struct spi_attach_args *sa;
+#if NGPIO > 0
+       int i;
+       struct gpiobus_attach_args gba;
+#endif
+
+       sa = aux;
+       sc = device_private(self);
+       sc->sc_dev = self;
+       sc->sc_sh = sa->sa_handle;
+       sc->sc_bank = 0;
+       sc->sc_ha = device_cfdata(sc->sc_dev)->cf_flags & 0x7;
+



Home | Main Index | Thread Index | Old Index