Source-Changes-HG archive

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

[src/thorpej-i2c-spi-conf]: src/sys/dev/spi Use spi_compatible_match().



details:   https://anonhg.NetBSD.org/src/rev/181b2a04530a
branches:  thorpej-i2c-spi-conf
changeset: 1020829:181b2a04530a
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Wed May 19 03:32:27 2021 +0000

description:
Use spi_compatible_match().

XXX More work to do for proper FDT integration.

diffstat:

 sys/dev/spi/mcp23s17.c |   42 +++++-
 sys/dev/spi/mcp3k.c    |  274 ++++++++++++++++++++++++++++++------------------
 2 files changed, 204 insertions(+), 112 deletions(-)

diffs (truncated from 415 to 300 lines):

diff -r d87089037e62 -r 181b2a04530a sys/dev/spi/mcp23s17.c
--- a/sys/dev/spi/mcp23s17.c    Wed May 19 03:14:24 2021 +0000
+++ b/sys/dev/spi/mcp23s17.c    Wed May 19 03:32:27 2021 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: mcp23s17.c,v 1.2 2021/04/24 23:36:59 thorpej Exp $ */
+/*      $NetBSD: mcp23s17.c,v 1.2.2.1 2021/05/19 03:32:27 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mcp23s17.c,v 1.2 2021/04/24 23:36:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mcp23s17.c,v 1.2.2.1 2021/05/19 03:32:27 thorpej Exp $");
 
 /* 
  * Driver for Microchip MCP23S17 GPIO
@@ -87,18 +87,34 @@
 CFATTACH_DECL_NEW(mcp23s17gpio, sizeof(struct mcp23s17gpio_softc),
                  mcp23s17gpio_match, mcp23s17gpio_attach, NULL, NULL);
 
+static const struct device_compatible_entry compat_data[] = {
+       { .compat = "mcp,mcp23s17" },
+       { .compat = "microchip,mcp23s17" },
+
+#if 0  /* We should also add support for these: */
+       { .compat = "mcp,mcp23s08" },
+       { .compat = "microchip,mcp23s08" },
+
+       { .compat = "microchip,mcp23s18" },
+#endif
+
+       DEVICE_COMPAT_EOL
+};
+
 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! */
+       int rv;
 
-       /* run at 10MHz */
-       if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
-               return 0;
+       rv = spi_compatible_match(sa, cf, compat_data);
+       if (rv != 0) {
+               /* run at 10MHz */
+               if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
+                       return 0;
+       }
 
-       return 1;
+       return rv;
 }
 
 static void
@@ -116,6 +132,11 @@
        sc->sc_dev = self;
        sc->sc_sh = sa->sa_handle;
        sc->sc_bank = 0;
+
+       /*
+        * XXX Initialize sc_ha from microchip,spi-present-mask
+        * XXX property for FDT.  Only consult cf_flags for indirect.
+        */
        sc->sc_ha = device_cfdata(sc->sc_dev)->cf_flags & 0x7;
 
        aprint_naive(": GPIO\n");       
@@ -124,7 +145,10 @@
        DPRINTF(1, ("%s: initialize (HAEN|SEQOP)\n", device_xname(sc->sc_dev)));
 
        /* basic setup */
-       mcp23s17gpio_write(sc, MCP23x17_IOCONA(sc->sc_bank), MCP23x17_IOCON_HAEN|MCP23x17_IOCON_SEQOP);
+       mcp23s17gpio_write(sc, MCP23x17_IOCONA(sc->sc_bank),
+           MCP23x17_IOCON_HAEN|MCP23x17_IOCON_SEQOP);
+
+       /* XXX Hook up to FDT GPIO. */
 
 #if NGPIO > 0
        for (i = 0; i < MCP23x17_GPIO_NPINS; i++) {
diff -r d87089037e62 -r 181b2a04530a sys/dev/spi/mcp3k.c
--- a/sys/dev/spi/mcp3k.c       Wed May 19 03:14:24 2021 +0000
+++ b/sys/dev/spi/mcp3k.c       Wed May 19 03:32:27 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mcp3k.c,v 1.2 2016/11/20 12:38:04 phx Exp $ */
+/*     $NetBSD: mcp3k.c,v 1.2.36.1 2021/05/19 03:33:05 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -72,14 +72,14 @@
 };
 
 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;
+       device_t                        sc_dev;
+       struct spi_handle               *sc_sh;
+       const struct mcp3kadc_model     *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];
+       struct sysmon_envsys            *sc_sme;
+       envsys_data_t                   sc_sensors[M3K_MAX_SENSORS];
 };
 
 static int     mcp3kadc_match(device_t, cfdata_t, void *);
@@ -91,124 +91,192 @@
 CFATTACH_DECL_NEW(mcp3kadc, sizeof(struct mcp3kadc_softc),
     mcp3kadc_match,  mcp3kadc_attach, NULL, NULL);
 
-static struct mcp3kadc_model mcp3k_models[] = {
-       {
-               .name = 3001,
-               .bits = 10,
-               .channels = 1,
-               .lead = 3,
-               .flags = 0
-       },
-       {
-               .name = 3002,
-               .bits = 10,
-               .channels = 2,
-               .lead = 2,
-               .flags = M3K_SGLDIFF | M3K_MSBF
-       },
-       {
-               .name = 3004,
-               .bits = 10,
-               .channels = 4,
-               .lead = 2,
-               .flags = M3K_SGLDIFF | M3K_D2D1D0
-       },
-       {
-               .name = 3008,
-               .bits = 10,
-               .channels = 8,
-               .lead = 2,
-               .flags = M3K_SGLDIFF | M3K_D2D1D0
-       },
-       {
-               .name = 3201,
-               .bits = 12,
-               .channels = 1,
-               .lead = 3,
-               .flags = 0
-       },
-       {
-               .name = 3202,
-               .bits = 12,
-               .channels = 2,
-               .lead = 2,
-               .flags = M3K_SGLDIFF | M3K_MSBF
-       },
-       {
-               .name = 3204,
-               .bits = 12,
-               .channels = 4,
-               .lead = 2,
-               .flags = M3K_SGLDIFF | M3K_D2D1D0
-       },
-       {
-               .name = 3208,
-               .bits = 12,
-               .channels = 8,
-               .lead = 2,
-               .flags = M3K_SGLDIFF | M3K_D2D1D0
-       },
-       {
-               .name = 3301,
-               .bits = 13,
-               .channels = 1,
-               .lead = 3,
-               .flags = M3K_SIGNED
-       },
-       {
-               .name = 3302,
-               .bits = 13,
-               .channels = 4,
-               .lead = 2,
-               .flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
-       },
-       {
-               .name = 3304,
-               .bits = 13,
-               .channels = 8,
-               .lead = 2,
-               .flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
-       },
+static const struct mcp3kadc_model mcp3001 = {
+       .name = 3001,
+       .bits = 10,
+       .channels = 1,
+       .lead = 3,
+       .flags = 0
+};
+
+static const struct mcp3kadc_model mcp3002 = {
+       .name = 3002,
+       .bits = 10,
+       .channels = 2,
+       .lead = 2,
+       .flags = M3K_SGLDIFF | M3K_MSBF
+};
+
+static const struct mcp3kadc_model mcp3004 = {
+       .name = 3004,
+       .bits = 10,
+       .channels = 4,
+       .lead = 2,
+       .flags = M3K_SGLDIFF | M3K_D2D1D0
+};
+
+static const struct mcp3kadc_model mcp3008 = {
+       .name = 3008,
+       .bits = 10,
+       .channels = 8,
+       .lead = 2,
+       .flags = M3K_SGLDIFF | M3K_D2D1D0
+};
+
+static const struct mcp3kadc_model mcp3201 = {
+       .name = 3201,
+       .bits = 12,
+       .channels = 1,
+       .lead = 3,
+       .flags = 0
+};
+
+static const struct mcp3kadc_model mcp3202 = {
+       .name = 3202,
+       .bits = 12,
+       .channels = 2,
+       .lead = 2,
+       .flags = M3K_SGLDIFF | M3K_MSBF
+};
+
+static const struct mcp3kadc_model mcp3204 = {
+       .name = 3204,
+       .bits = 12,
+       .channels = 4,
+       .lead = 2,
+       .flags = M3K_SGLDIFF | M3K_D2D1D0
+};
+
+static const struct mcp3kadc_model mcp3208 = {
+       .name = 3208,
+       .bits = 12,
+       .channels = 8,
+       .lead = 2,
+       .flags = M3K_SGLDIFF | M3K_D2D1D0
 };
 
+static const struct mcp3kadc_model mcp3301 = {
+       .name = 3301,
+       .bits = 13,
+       .channels = 1,
+       .lead = 3,
+       .flags = M3K_SIGNED
+};
+
+static const struct mcp3kadc_model mcp3302 = {
+       .name = 3302,
+       .bits = 13,
+       .channels = 4,
+       .lead = 2,
+       .flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
+};
+
+static const struct mcp3kadc_model mcp3304 = {
+       .name = 3304,
+       .bits = 13,
+       .channels = 8,
+       .lead = 2,
+       .flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
+};
+
+static const struct device_compatible_entry compat_data[] = {
+       { .compat = "microchip,mcp3001",        .data = &mcp3001 },
+       { .compat = "microchip,mcp3002",        .data = &mcp3002 },
+       { .compat = "microchip,mcp3004",        .data = &mcp3004 },
+       { .compat = "microchip,mcp3008",        .data = &mcp3008 },
+       { .compat = "microchip,mcp3201",        .data = &mcp3201 },
+       { .compat = "microchip,mcp3202",        .data = &mcp3202 },
+       { .compat = "microchip,mcp3204",        .data = &mcp3204 },
+       { .compat = "microchip,mcp3208",        .data = &mcp3208 },
+       { .compat = "microchip,mcp3301",        .data = &mcp3301 },
+       { .compat = "microchip,mcp3302",        .data = &mcp3302 },
+       { .compat = "microchip,mcp3304",        .data = &mcp3304 },
+
+#if 0  /* We should also add support for these: */
+       { .compat = "microchip,mcp3550-50" },
+       { .compat = "microchip,mcp3550-60" },



Home | Main Index | Thread Index | Old Index