Home | History | Annotate | Line # | Download | only in spi
mcp3k.c revision 1.7
      1 /*	$NetBSD: mcp3k.c,v 1.7 2025/09/11 14:19:05 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank Wille.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Microchip MCP3x0x SAR analog to digital converters.
     34  * The driver supports various ADCs with different resolutions, operation
     35  * modes and number of input channels.
     36  * The reference voltage Vref defaults to the maximum output value in mV,
     37  * but can be changed via sysctl(3).
     38  *
     39  * MCP3001: http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf
     40  * MCP3002: http://ww1.microchip.com/downloads/en/DeviceDoc/21294E.pdf
     41  * MCP3004/3008: http://ww1.microchip.com/downloads/en/DeviceDoc/21295C.pdf
     42  * MCP3201: http://ww1.microchip.com/downloads/en/DeviceDoc/21290D.pdf
     43  * MCP3204/3208: http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf
     44  * MCP3301: http://ww1.microchip.com/downloads/en/DeviceDoc/21700E.pdf
     45  * MPC3302/3304: http://ww1.microchip.com/downloads/en/DeviceDoc/21697F.pdf
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/device.h>
     51 #include <sys/kernel.h>
     52 #include <sys/types.h>
     53 #include <sys/sysctl.h>
     54 
     55 #include <dev/sysmon/sysmonvar.h>
     56 #include <dev/spi/spivar.h>
     57 
     58 #define M3K_MAX_SENSORS		16		/* 8 single-ended & 8 diff. */
     59 
     60 /* mcp3x0x model description */
     61 struct mcp3kadc_model {
     62 	uint32_t		name;
     63 	uint8_t			bits;
     64 	uint8_t			channels;
     65 	uint8_t			lead;		/* leading bits to ignore */
     66 	uint8_t			flags;
     67 #define M3K_SGLDIFF		0x01		/* single-ended/differential */
     68 #define M3K_D2D1D0		0x02		/* 3 channel select bits */
     69 #define M3K_MSBF		0x04		/* MSBF select bit */
     70 #define M3K_SIGNED		0x80		/* result is signed */
     71 #define M3K_CTRL_NEEDED		(M3K_SGLDIFF | M3K_D2D1D0 | M3K_MSBF)
     72 };
     73 
     74 struct mcp3kadc_softc {
     75 	device_t			sc_dev;
     76 	struct spi_handle 		*sc_sh;
     77 	const struct mcp3kadc_model	*sc_model;
     78 	uint32_t			sc_adc_max;
     79 	int32_t				sc_vref_mv;
     80 
     81 	struct sysmon_envsys 		*sc_sme;
     82 	envsys_data_t 			sc_sensors[M3K_MAX_SENSORS];
     83 };
     84 
     85 static int	mcp3kadc_match(device_t, cfdata_t, void *);
     86 static void	mcp3kadc_attach(device_t, device_t, void *);
     87 static void	mcp3kadc_envsys_refresh(struct sysmon_envsys *,
     88 		    envsys_data_t *);
     89 static int	sysctl_mcp3kadc_vref(SYSCTLFN_ARGS);
     90 
     91 CFATTACH_DECL_NEW(mcp3kadc, sizeof(struct mcp3kadc_softc),
     92     mcp3kadc_match,  mcp3kadc_attach, NULL, NULL);
     93 
     94 static const struct mcp3kadc_model mcp3001 = {
     95 	.name = 3001,
     96 	.bits = 10,
     97 	.channels = 1,
     98 	.lead = 3,
     99 	.flags = 0
    100 };
    101 
    102 static const struct mcp3kadc_model mcp3002 = {
    103 	.name = 3002,
    104 	.bits = 10,
    105 	.channels = 2,
    106 	.lead = 2,
    107 	.flags = M3K_SGLDIFF | M3K_MSBF
    108 };
    109 
    110 static const struct mcp3kadc_model mcp3004 = {
    111 	.name = 3004,
    112 	.bits = 10,
    113 	.channels = 4,
    114 	.lead = 2,
    115 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    116 };
    117 
    118 static const struct mcp3kadc_model mcp3008 = {
    119 	.name = 3008,
    120 	.bits = 10,
    121 	.channels = 8,
    122 	.lead = 2,
    123 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    124 };
    125 
    126 static const struct mcp3kadc_model mcp3201 = {
    127 	.name = 3201,
    128 	.bits = 12,
    129 	.channels = 1,
    130 	.lead = 3,
    131 	.flags = 0
    132 };
    133 
    134 static const struct mcp3kadc_model mcp3202 = {
    135 	.name = 3202,
    136 	.bits = 12,
    137 	.channels = 2,
    138 	.lead = 2,
    139 	.flags = M3K_SGLDIFF | M3K_MSBF
    140 };
    141 
    142 static const struct mcp3kadc_model mcp3204 = {
    143 	.name = 3204,
    144 	.bits = 12,
    145 	.channels = 4,
    146 	.lead = 2,
    147 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    148 };
    149 
    150 static const struct mcp3kadc_model mcp3208 = {
    151 	.name = 3208,
    152 	.bits = 12,
    153 	.channels = 8,
    154 	.lead = 2,
    155 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    156 };
    157 
    158 static const struct mcp3kadc_model mcp3301 = {
    159 	.name = 3301,
    160 	.bits = 13,
    161 	.channels = 1,
    162 	.lead = 3,
    163 	.flags = M3K_SIGNED
    164 };
    165 
    166 static const struct mcp3kadc_model mcp3302 = {
    167 	.name = 3302,
    168 	.bits = 13,
    169 	.channels = 4,
    170 	.lead = 2,
    171 	.flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
    172 };
    173 
    174 static const struct mcp3kadc_model mcp3304 = {
    175 	.name = 3304,
    176 	.bits = 13,
    177 	.channels = 8,
    178 	.lead = 2,
    179 	.flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
    180 };
    181 
    182 /*
    183  * N.B. The order of this table is important!  It matches the order
    184  * of the legacy mcp3k_models[] array, which is used to manually
    185  * select the device type in the kernel configuration file when
    186  * direct configuration is not available.
    187  */
    188 static const struct device_compatible_entry compat_data[] = {
    189 	{ .compat = "microchip,mcp3001",	.data = &mcp3001 },
    190 	{ .compat = "microchip,mcp3002",	.data = &mcp3002 },
    191 	{ .compat = "microchip,mcp3004",	.data = &mcp3004 },
    192 	{ .compat = "microchip,mcp3008",	.data = &mcp3008 },
    193 	{ .compat = "microchip,mcp3201",	.data = &mcp3201 },
    194 	{ .compat = "microchip,mcp3202",	.data = &mcp3202 },
    195 	{ .compat = "microchip,mcp3204",	.data = &mcp3204 },
    196 	{ .compat = "microchip,mcp3208",	.data = &mcp3208 },
    197 	{ .compat = "microchip,mcp3301",	.data = &mcp3301 },
    198 	{ .compat = "microchip,mcp3302",	.data = &mcp3302 },
    199 	{ .compat = "microchip,mcp3304",	.data = &mcp3304 },
    200 
    201 #if 0	/* We should also add support for these: */
    202 	{ .compat = "microchip,mcp3550-50" },
    203 	{ .compat = "microchip,mcp3550-60" },
    204 	{ .compat = "microchip,mcp3551" },
    205 	{ .compat = "microchip,mcp3553" },
    206 #endif
    207 
    208 	DEVICE_COMPAT_EOL
    209 };
    210 static const int mcp3k_nmodels = __arraycount(compat_data) - 1;
    211 
    212 static const struct mcp3kadc_model *
    213 mcp3kadc_lookup(const struct spi_attach_args *sa, const cfdata_t cf)
    214 {
    215 	if (sa->sa_ncompat > 0) {
    216 		const struct device_compatible_entry *dce =
    217 		    spi_compatible_lookup(sa, compat_data);
    218 		if (dce == NULL) {
    219 			return NULL;
    220 		}
    221 		return dce->data;
    222 	} else {
    223 		if (cf->cf_flags < 0 || cf->cf_flags >= mcp3k_nmodels) {
    224 			return NULL;
    225 		}
    226 		return compat_data[cf->cf_flags].data;
    227 	}
    228 }
    229 
    230 static int
    231 mcp3kadc_match(device_t parent, cfdata_t cf, void *aux)
    232 {
    233 	struct spi_attach_args *sa = aux;
    234 	int match_result;
    235 
    236 	if (spi_use_direct_match(sa, compat_data, &match_result)) {
    237 		return match_result;
    238 	}
    239 
    240 	/*
    241 	 * If we're doing indirect config, the user must
    242 	 * have specified a valid model.
    243 	 */
    244 	if (sa->sa_ncompat == 0 && mcp3kadc_lookup(sa, cf) == NULL) {
    245 		return 0;
    246 	}
    247 
    248 	return SPI_MATCH_DEFAULT;
    249 }
    250 
    251 static void
    252 mcp3kadc_attach(device_t parent, device_t self, void *aux)
    253 {
    254 	const struct sysctlnode *rnode, *node;
    255 	struct spi_attach_args *sa = aux;
    256 	struct mcp3kadc_softc *sc = device_private(self);;
    257 	const struct mcp3kadc_model *model;
    258 	int error, ch, i;
    259 
    260 	sc->sc_dev = self;
    261 	sc->sc_sh = sa->sa_handle;
    262 
    263 	model = mcp3kadc_lookup(sa, device_cfdata(self));
    264 	KASSERT(model != NULL);
    265 
    266 	sc->sc_model = model;
    267 
    268 	aprint_naive(": Analog to Digital converter\n");
    269 	aprint_normal(": MCP%u %u-channel %u-bit ADC\n",
    270 	    (unsigned)model->name, (unsigned)model->channels,
    271 	    (unsigned)model->bits);
    272 
    273 	/* configure for 1MHz */
    274 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, SPI_FREQ_MHz(1));
    275 	if (error) {
    276 		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
    277 		    error);
    278 		return;
    279 	}
    280 
    281 	/* set a default Vref in mV according to the chip's ADC resolution */
    282 	sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
    283 	    model->bits - 1 : model->bits);
    284 
    285 	/* remember maximum value for this ADC - also used for masking */
    286 	sc->sc_adc_max = (1 << model->bits) - 1;
    287 
    288 	/* attach voltage sensors to envsys */
    289 	sc->sc_sme = sysmon_envsys_create();
    290 
    291 	/* adc difference from two neighbouring channels */
    292 	for (ch = 0; ch < model->channels; ch++) {
    293 		KASSERT(ch < M3K_MAX_SENSORS);
    294 		sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
    295 		sc->sc_sensors[ch].state = ENVSYS_SINVALID;
    296 		if (model->channels == 1)
    297 			strlcpy(sc->sc_sensors[ch].desc, "adc diff ch0",
    298 			    sizeof(sc->sc_sensors[ch].desc));
    299 		else
    300 			snprintf(sc->sc_sensors[ch].desc,
    301 			    sizeof(sc->sc_sensors[ch].desc),
    302 			    "adc diff ch%d-ch%d", ch, ch ^ 1);
    303 		sc->sc_sensors[ch].private = ch;
    304 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[ch]);
    305 	}
    306 
    307 	if (model->flags & M3K_SGLDIFF) {
    308 		/* adc from single ended channels */
    309 		for (i = 0; i < model->channels; i++, ch++) {
    310 			KASSERT(ch < M3K_MAX_SENSORS);
    311 			sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
    312 			sc->sc_sensors[ch].state = ENVSYS_SINVALID;
    313 			snprintf(sc->sc_sensors[ch].desc,
    314 			    sizeof(sc->sc_sensors[ch].desc),
    315 			    "adc single ch%d", i);
    316 			sc->sc_sensors[ch].private = ch;
    317 			sysmon_envsys_sensor_attach(sc->sc_sme,
    318 			    &sc->sc_sensors[ch]);
    319 		}
    320 	}
    321 
    322 	sc->sc_sme->sme_name = device_xname(self);
    323 	sc->sc_sme->sme_refresh = mcp3kadc_envsys_refresh;
    324 	sc->sc_sme->sme_cookie = sc;
    325 	if (sysmon_envsys_register(sc->sc_sme)) {
    326 		aprint_error_dev(self, "unable to register with sysmon\n");
    327 		sysmon_envsys_destroy(sc->sc_sme);
    328 	}
    329 
    330 	/* create a sysctl node for adjusting the ADC's reference voltage */
    331 	rnode = node = NULL;
    332 	sysctl_createv(NULL, 0, NULL, &rnode,
    333 	    CTLFLAG_READWRITE,
    334 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    335 	    NULL, 0, NULL, 0,
    336 	    CTL_HW, CTL_CREATE, CTL_EOL);
    337 
    338 	if (rnode != NULL)
    339 		sysctl_createv(NULL, 0, NULL, &node,
    340 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    341 		    CTLTYPE_INT, "vref",
    342 		    SYSCTL_DESCR("ADC reference voltage"),
    343 		    sysctl_mcp3kadc_vref, 0, (void *)sc, 0,
    344 		    CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL);
    345 }
    346 
    347 static void
    348 mcp3kadc_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    349 {
    350 	struct mcp3kadc_softc *sc;
    351 	const struct mcp3kadc_model *model;
    352 	uint8_t buf[2], ctrl;
    353 	int32_t val, scale;
    354 
    355 	sc = sme->sme_cookie;
    356 	model = sc->sc_model;
    357 	scale = sc->sc_adc_max + 1;
    358 
    359 	if (model->flags & M3K_CTRL_NEEDED) {
    360 		/* we need to send some control bits first */
    361 		ctrl = 1;	/* start bit */
    362 
    363 		if (model->flags & M3K_SGLDIFF) {
    364 			/* bit set to select single-ended mode */
    365 			ctrl <<= 1;
    366 			ctrl |= edata->private >= model->channels;
    367 		}
    368 
    369 		if (model->flags & M3K_D2D1D0) {
    370 			/* 3 bits select the channel */
    371 			ctrl <<= 3;
    372 			ctrl |= edata->private & (model->channels - 1);
    373 		} else {
    374 			/* 1 bit selects between two channels */
    375 			ctrl <<= 1;
    376 			ctrl |= edata->private & 1;
    377 		}
    378 
    379 		if (model->flags & M3K_MSBF) {
    380 			/* bit select MSB first format */
    381 			ctrl <<= 1;
    382 			ctrl |= 1;
    383 		}
    384 
    385 		/* send control bits, receive ADC data */
    386 		if (spi_send_recv(sc->sc_sh, 1, &ctrl, 2, buf) != 0) {
    387 			edata->state = ENVSYS_SINVALID;
    388 			return;
    389 		}
    390 	} else {
    391 
    392 		/* just read data from the ADC */
    393 		if (spi_recv(sc->sc_sh, 2, buf) != 0) {
    394 			edata->state = ENVSYS_SINVALID;
    395 			return;
    396 		}
    397 	}
    398 
    399 	/* extract big-endian ADC data from buffer */
    400 	val = (buf[0] << 8) | buf[1];
    401 	val = (val >> (16 - (model->bits + model->lead))) & sc->sc_adc_max;
    402 
    403 	/* sign-extend the result, when needed */
    404 	if (model->flags & M3K_SIGNED) {
    405 		if (val & (1 << (model->bits - 1)))
    406 			val -= sc->sc_adc_max + 1;
    407 		scale >>= 1;	/* MSB is the sign */
    408 	}
    409 
    410 	/* scale the value for Vref and convert to mV */
    411 	edata->value_cur = (sc->sc_vref_mv * val / scale) * 1000;
    412 	edata->state = ENVSYS_SVALID;
    413 }
    414 
    415 static int
    416 sysctl_mcp3kadc_vref(SYSCTLFN_ARGS)
    417 {
    418 	struct sysctlnode node;
    419 	struct mcp3kadc_softc *sc;
    420 	int32_t t;
    421 	int error;
    422 
    423 	node = *rnode;
    424 	sc = node.sysctl_data;
    425 
    426 	t = sc->sc_vref_mv;
    427 	node.sysctl_data = &t;
    428 
    429 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    430 	if (error || newp == NULL)
    431 		return error;
    432 	if (t <= 0)
    433 		return EINVAL;
    434 
    435 	sc->sc_vref_mv = t;
    436 	return 0;
    437 }
    438