Source-Changes-HG archive

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

[src/trunk]: src MI driver for the Microchip 3x0x series of SAR analog to dig...



details:   https://anonhg.NetBSD.org/src/rev/9b9e598f5f8b
branches:  trunk
changeset: 339970:9b9e598f5f8b
user:      phx <phx%NetBSD.org@localhost>
date:      Tue Aug 18 15:54:20 2015 +0000

description:
MI driver for the Microchip 3x0x series of SAR analog to digital converters.

diffstat:

 distrib/sets/lists/man/mi |    5 +-
 doc/CHANGES               |    3 +-
 share/man/man4/Makefile   |    4 +-
 share/man/man4/mcp3kadc.4 |   94 +++++++++++
 sys/arch/evbarm/conf/RPI  |    6 +-
 sys/dev/spi/files.spi     |    7 +-
 sys/dev/spi/mcp3k.c       |  368 ++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 481 insertions(+), 6 deletions(-)

diffs (truncated from 574 to 300 lines):

diff -r 1b4c56dea725 -r 9b9e598f5f8b distrib/sets/lists/man/mi
--- a/distrib/sets/lists/man/mi Tue Aug 18 15:38:02 2015 +0000
+++ b/distrib/sets/lists/man/mi Tue Aug 18 15:54:20 2015 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1504 2015/08/17 06:50:03 knakahara Exp $
+# $NetBSD: mi,v 1.1505 2015/08/18 15:54:20 phx Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1391,6 +1391,7 @@
 ./usr/share/man/cat4/mcclock.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/mcd.0                     man-sys-catman          .cat
 ./usr/share/man/cat4/mcp23s17gpio.0            man-sys-catman          .cat
+./usr/share/man/cat4/mcp3kadc.0                        man-sys-catman          .cat
 ./usr/share/man/cat4/mcp980x.0                 man-sys-catman          .cat
 ./usr/share/man/cat4/md.0                      man-sys-catman          .cat
 ./usr/share/man/cat4/mfb.0                     man-sys-catman          .cat
@@ -4415,6 +4416,7 @@
 ./usr/share/man/html4/mcclock.html             man-sys-htmlman         html
 ./usr/share/man/html4/mcd.html                 man-sys-htmlman         html
 ./usr/share/man/html4/mcp23s17gpio.html                man-sys-htmlman         html
+./usr/share/man/html4/mcp3kadc.html            man-sys-htmlman         html
 ./usr/share/man/html4/mcp980x.html             man-sys-htmlman         html
 ./usr/share/man/html4/md.html                  man-sys-htmlman         html
 ./usr/share/man/html4/mfb.html                 man-sys-htmlman         html
@@ -7289,6 +7291,7 @@
 ./usr/share/man/man4/mcclock.4                 man-sys-man             .man
 ./usr/share/man/man4/mcd.4                     man-sys-man             .man
 ./usr/share/man/man4/mcp23s17gpio.4            man-sys-man             .man
+./usr/share/man/man4/mcp3kadc.4                        man-sys-man             .man
 ./usr/share/man/man4/mcp980x.4                 man-sys-man             .man
 ./usr/share/man/man4/md.4                      man-sys-man             .man
 ./usr/share/man/man4/mfb.4                     man-sys-man             .man
diff -r 1b4c56dea725 -r 9b9e598f5f8b doc/CHANGES
--- a/doc/CHANGES       Tue Aug 18 15:38:02 2015 +0000
+++ b/doc/CHANGES       Tue Aug 18 15:54:20 2015 +0000
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:                   <$Revision: 1.2094 $>
+# LIST OF CHANGES FROM LAST RELEASE:                   <$Revision: 1.2095 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -187,3 +187,4 @@
        libc: Update to tzcode2015f. [christos 20150813]
        gdb(1): Updated to 7.9.1.  [christos 20150818]
        acpi(4): Updated ACPICA to 20150717. [christos 20150818]
+       mcp3kadc(4): Driver for Microchip 3x0x SAR ADC chips. [phx 20150818]
diff -r 1b4c56dea725 -r 9b9e598f5f8b share/man/man4/Makefile
--- a/share/man/man4/Makefile   Tue Aug 18 15:38:02 2015 +0000
+++ b/share/man/man4/Makefile   Tue Aug 18 15:54:20 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.620 2015/05/13 07:28:49 mlelstv Exp $
+#      $NetBSD: Makefile,v 1.621 2015/08/18 15:54:20 phx Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/18/93
 
 MAN=   aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
@@ -127,7 +127,7 @@
        smscmon.4 spdmem.4 tps65217pmic.4
 
 # machine-independent SPI devices
-MAN += m25p.4 mcp23s17gpio.4 tm121temp.4
+MAN += m25p.4 mcp23s17gpio.4 mcp3kadc.4 tm121temp.4
 
 # machine-independent SD/MMC devices
 MAN += sbt.4 sdhc.4 sdmmc.4
diff -r 1b4c56dea725 -r 9b9e598f5f8b share/man/man4/mcp3kadc.4
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man4/mcp3kadc.4 Tue Aug 18 15:54:20 2015 +0000
@@ -0,0 +1,94 @@
+.\" $NetBSD: mcp3kadc.4,v 1.1 2015/08/18 15:54:20 phx Exp $
+.\"
+.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Frank Wille.
+.\"
+.\" 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.
+.\"
+.Dd August 18, 2015
+.Dt MCP3KADC 4
+.Os
+.Sh NAME
+.Nm mcp3kadc
+.Nd Microchip 3x0x SAR analog to digital converter
+.Sh SYNOPSIS
+.Cd "mcp3kadc* at spi? slave ? flags N"
+.Sh DESCRIPTION
+The
+.Nm
+driver reports the current voltage on the chip's ADC channels through the
+.Xr envsys 4
+API. The driver calculates these values according to the currently selected
+reference voltage (
+.Li Vref
+). It can be changed
+through the
+.Xr sysctl 8
+node
+.Li hw.mcp3kadc0.vref .
+.Pp
+The following table shows the supported chips. The type of the chip can
+be selected with the
+.Ar flags
+argument in the config file.
+.Bl -column "Designation" "Resolution" "Input Channels" "flags" -offset indent
+.It Sy "Designation" Ta Sy "Resolution" Ta Sy "Input Channels" Ta Sy "flags"
+.It Li "MCP3001" Ta "10 bits" Ta "1" Ta "0"
+.It Li "MCP3002" Ta "10 bits" Ta "2" Ta "1"
+.It Li "MCP3004" Ta "10 bits" Ta "4" Ta "2"
+.It Li "MCP3008" Ta "10 bits" Ta "8" Ta "3"
+.It Li "MCP3201" Ta "12 bits" Ta "1" Ta "4"
+.It Li "MCP3202" Ta "12 bits" Ta "2" Ta "5"
+.It Li "MCP3204" Ta "12 bits" Ta "4" Ta "6"
+.It Li "MCP3208" Ta "12 bits" Ta "8" Ta "7"
+.It Li "MCP3301" Ta "13 bits" Ta "1" Ta "8"
+.It Li "MCP3302" Ta "13 bits" Ta "4" Ta "9"
+.It Li "MCP3304" Ta "13 bits" Ta "8" Ta "10"
+.El
+.Sh SYSCTL VARIABLES
+The following
+.Xr sysctl 3
+variables are provided:
+.Bl -tag -width indent
+.It hw.mcp3kadc0.vref
+Defines the reference voltage on the chip's
+.Li Vref
+pin in millivolts (mV). It defaults to the ADC's maximum output value + 1
+in millivolts (e.g., 4096 for a 12-bit ADC).
+.El
+.Sh SEE ALSO
+.Xr spi 4 ,
+.Xr envsys 4 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Nx 8.0 .
+.Sh AUTHORS
+The
+.Nm
+driver was written by
+.An Frank Wille .
diff -r 1b4c56dea725 -r 9b9e598f5f8b sys/arch/evbarm/conf/RPI
--- a/sys/arch/evbarm/conf/RPI  Tue Aug 18 15:38:02 2015 +0000
+++ b/sys/arch/evbarm/conf/RPI  Tue Aug 18 15:54:20 2015 +0000
@@ -1,5 +1,5 @@
 #
-#      $NetBSD: RPI,v 1.63 2015/04/18 13:43:45 skrll Exp $
+#      $NetBSD: RPI,v 1.64 2015/08/18 15:54:20 phx Exp $
 #
 #      RPi -- Raspberry Pi
 #
@@ -144,6 +144,10 @@
 bcmspi* at obio?
 spi* at spibus?
 
+# MCP3x0x ADC
+# flags selects the actual chip, refer to mcp3kadc(4)
+#mcp3kadc0 at spi? slave 0 flags 0
+
 # PIFace or other boards using that chip (needs gpio)
 #mcp23s17gpio0 at spi? slave 0 flags 0
 #mcp23s17gpio1 at spi? slave 0 flags 1
diff -r 1b4c56dea725 -r 9b9e598f5f8b sys/dev/spi/files.spi
--- a/sys/dev/spi/files.spi     Tue Aug 18 15:38:02 2015 +0000
+++ b/sys/dev/spi/files.spi     Tue Aug 18 15:54:20 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.spi,v 1.4 2014/04/06 17:59:39 kardel Exp $
+#      $NetBSD: files.spi,v 1.5 2015/08/18 15:54:20 phx Exp $
 
 define spibus { }
 
@@ -35,3 +35,8 @@
 device mcp23s17gpio: gpiobus
 attach mcp23s17gpio at spi
 file   dev/spi/mcp23s17.c              mcp23s17gpio
+
+# MCP3x0x ADC
+device mcp3kadc: sysmon_envsys
+attach mcp3kadc at spi
+file   dev/spi/mcp3k.c                 mcp3kadc
diff -r 1b4c56dea725 -r 9b9e598f5f8b sys/dev/spi/mcp3k.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/spi/mcp3k.c       Tue Aug 18 15:54:20 2015 +0000
@@ -0,0 +1,368 @@
+/*     $NetBSD: mcp3k.c,v 1.1 2015/08/18 15:54:20 phx Exp $ */
+
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Frank Wille.
+ *
+ * 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.
+ */
+
+/*
+ * Microchip MCP3x0x SAR analog to digital converters.
+ * The driver supports various ADCs with different resolutions, operation
+ * modes and number of input channels.
+ * The reference voltage Vref defaults to the maximum output value in mV,
+ * but can be changed via sysctl(3).
+ *
+ * MCP3001: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
+ * MCP3002: http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
+ * MCP3004/3008: http://ww1.microchip.com/downloads/en/DeviceDoc/21295C.pdf
+ * MCP3201: http://ww1.microchip.com/downloads/en/DeviceDoc/21290D.pdf
+ * MCP3204/3208: http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf
+ * MCP3301: http://ww1.microchip.com/downloads/en/DeviceDoc/21700E.pdf
+ * MPC3302/3304: http://ww1.microchip.com/downloads/en/DeviceDoc/21697F.pdf
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/types.h> 
+#include <sys/sysctl.h>
+
+#include <dev/sysmon/sysmonvar.h>
+#include <dev/spi/spivar.h>
+
+#define M3K_MAX_SENSORS                16              /* 8 single-ended & 8 diff. */
+
+/* mcp3x0x model description */
+struct mcp3kadc_model {
+       uint32_t                name;
+       uint8_t                 bits;
+       uint8_t                 channels;
+       uint8_t                 lead;           /* leading bits to ignore */
+       uint8_t                 flags;
+#define M3K_SGLDIFF            0x01            /* single-ended/differential */
+#define M3K_D2D1D0             0x02            /* 3 channel select bits */
+#define M3K_MSBF               0x04            /* MSBF select bit */
+#define M3K_SIGNED             0x80            /* result is signed */
+#define M3K_CTRL_NEEDED                (M3K_SGLDIFF | M3K_D2D1D0 | M3K_MSBF)
+};
+
+struct mcp3kadc_softc {
+       device_t                sc_dev;
+       struct spi_handle       *sc_sh;
+       int                     sc_model;
+       uint32_t                sc_adc_max;
+       int32_t                 sc_vref_mv;
+
+       struct sysmon_envsys    *sc_sme;
+       envsys_data_t           sc_sensors[M3K_MAX_SENSORS];
+};
+
+static int     mcp3kadc_match(device_t, cfdata_t, void *);
+static void    mcp3kadc_attach(device_t, device_t, void *);
+static void    mcp3kadc_envsys_refresh(struct sysmon_envsys *,
+                   envsys_data_t *);
+static int     sysctl_mcp3kadc_vref(SYSCTLFN_ARGS);
+
+CFATTACH_DECL_NEW(mcp3kadc, sizeof(struct mcp3kadc_softc),
+    mcp3kadc_match,  mcp3kadc_attach, NULL, NULL);
+
+static struct mcp3kadc_model mcp3k_models[] = {



Home | Main Index | Thread Index | Old Index