Home | History | Annotate | Line # | Download | only in spi
mcp3k.c revision 1.6
      1 /*	$NetBSD: mcp3k.c,v 1.6 2025/09/11 01:07:24 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 struct mcp3kadc_model mcp3k_models[] = {
     95 	{
     96 		.name = 3001,
     97 		.bits = 10,
     98 		.channels = 1,
     99 		.lead = 3,
    100 		.flags = 0
    101 	},
    102 	{
    103 		.name = 3002,
    104 		.bits = 10,
    105 		.channels = 2,
    106 		.lead = 2,
    107 		.flags = M3K_SGLDIFF | M3K_MSBF
    108 	},
    109 	{
    110 		.name = 3004,
    111 		.bits = 10,
    112 		.channels = 4,
    113 		.lead = 2,
    114 		.flags = M3K_SGLDIFF | M3K_D2D1D0
    115 	},
    116 	{
    117 		.name = 3008,
    118 		.bits = 10,
    119 		.channels = 8,
    120 		.lead = 2,
    121 		.flags = M3K_SGLDIFF | M3K_D2D1D0
    122 	},
    123 	{
    124 		.name = 3201,
    125 		.bits = 12,
    126 		.channels = 1,
    127 		.lead = 3,
    128 		.flags = 0
    129 	},
    130 	{
    131 		.name = 3202,
    132 		.bits = 12,
    133 		.channels = 2,
    134 		.lead = 2,
    135 		.flags = M3K_SGLDIFF | M3K_MSBF
    136 	},
    137 	{
    138 		.name = 3204,
    139 		.bits = 12,
    140 		.channels = 4,
    141 		.lead = 2,
    142 		.flags = M3K_SGLDIFF | M3K_D2D1D0
    143 	},
    144 	{
    145 		.name = 3208,
    146 		.bits = 12,
    147 		.channels = 8,
    148 		.lead = 2,
    149 		.flags = M3K_SGLDIFF | M3K_D2D1D0
    150 	},
    151 	{
    152 		.name = 3301,
    153 		.bits = 13,
    154 		.channels = 1,
    155 		.lead = 3,
    156 		.flags = M3K_SIGNED
    157 	},
    158 	{
    159 		.name = 3302,
    160 		.bits = 13,
    161 		.channels = 4,
    162 		.lead = 2,
    163 		.flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
    164 	},
    165 	{
    166 		.name = 3304,
    167 		.bits = 13,
    168 		.channels = 8,
    169 		.lead = 2,
    170 		.flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
    171 	},
    172 };
    173 static const int mcp3k_nmodels = __arraycount(mcp3k_models);
    174 
    175 static const struct mcp3kadc_model *
    176 mcp3kadc_lookup(const cfdata_t cf)
    177 {
    178 	if (cf->cf_flags < 0 || cf->cf_flags >= mcp3k_nmodels) {
    179 		return NULL;
    180 	}
    181 	return &mcp3k_models[cf->cf_flags];
    182 }
    183 
    184 static int
    185 mcp3kadc_match(device_t parent, cfdata_t cf, void *aux)
    186 {
    187 
    188 	if (strcmp(cf->cf_name, "mcp3kadc") != 0)
    189 		return 0;
    190 
    191 	/*
    192 	 * If we're doing indirect config, the user must
    193 	 * have specified a valid model.
    194 	 */
    195 	if (mcp3kadc_lookup(cf) == NULL) {
    196 		return 0;
    197 	}
    198 
    199 	return 1;
    200 }
    201 
    202 static void
    203 mcp3kadc_attach(device_t parent, device_t self, void *aux)
    204 {
    205 	const struct sysctlnode *rnode, *node;
    206 	struct spi_attach_args *sa = aux;
    207 	struct mcp3kadc_softc *sc = device_private(self);;
    208 	const struct mcp3kadc_model *model;
    209 	int error, ch, i;
    210 
    211 	sc->sc_dev = self;
    212 	sc->sc_sh = sa->sa_handle;
    213 
    214 	model = mcp3kadc_lookup(device_cfdata(self));
    215 	KASSERT(model != NULL);
    216 
    217 	sc->sc_model = model;
    218 
    219 	aprint_naive(": Analog to Digital converter\n");
    220 	aprint_normal(": MCP%u %u-channel %u-bit ADC\n",
    221 	    (unsigned)model->name, (unsigned)model->channels,
    222 	    (unsigned)model->bits);
    223 
    224 	/* configure for 1MHz */
    225 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, SPI_FREQ_MHz(1));
    226 	if (error) {
    227 		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
    228 		    error);
    229 		return;
    230 	}
    231 
    232 	/* set a default Vref in mV according to the chip's ADC resolution */
    233 	sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
    234 	    model->bits - 1 : model->bits);
    235 
    236 	/* remember maximum value for this ADC - also used for masking */
    237 	sc->sc_adc_max = (1 << model->bits) - 1;
    238 
    239 	/* attach voltage sensors to envsys */
    240 	sc->sc_sme = sysmon_envsys_create();
    241 
    242 	/* adc difference from two neighbouring channels */
    243 	for (ch = 0; ch < model->channels; ch++) {
    244 		KASSERT(ch < M3K_MAX_SENSORS);
    245 		sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
    246 		sc->sc_sensors[ch].state = ENVSYS_SINVALID;
    247 		if (model->channels == 1)
    248 			strlcpy(sc->sc_sensors[ch].desc, "adc diff ch0",
    249 			    sizeof(sc->sc_sensors[ch].desc));
    250 		else
    251 			snprintf(sc->sc_sensors[ch].desc,
    252 			    sizeof(sc->sc_sensors[ch].desc),
    253 			    "adc diff ch%d-ch%d", ch, ch ^ 1);
    254 		sc->sc_sensors[ch].private = ch;
    255 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[ch]);
    256 	}
    257 
    258 	if (model->flags & M3K_SGLDIFF) {
    259 		/* adc from single ended channels */
    260 		for (i = 0; i < model->channels; i++, ch++) {
    261 			KASSERT(ch < M3K_MAX_SENSORS);
    262 			sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
    263 			sc->sc_sensors[ch].state = ENVSYS_SINVALID;
    264 			snprintf(sc->sc_sensors[ch].desc,
    265 			    sizeof(sc->sc_sensors[ch].desc),
    266 			    "adc single ch%d", i);
    267 			sc->sc_sensors[ch].private = ch;
    268 			sysmon_envsys_sensor_attach(sc->sc_sme,
    269 			    &sc->sc_sensors[ch]);
    270 		}
    271 	}
    272 
    273 	sc->sc_sme->sme_name = device_xname(self);
    274 	sc->sc_sme->sme_refresh = mcp3kadc_envsys_refresh;
    275 	sc->sc_sme->sme_cookie = sc;
    276 	if (sysmon_envsys_register(sc->sc_sme)) {
    277 		aprint_error_dev(self, "unable to register with sysmon\n");
    278 		sysmon_envsys_destroy(sc->sc_sme);
    279 	}
    280 
    281 	/* create a sysctl node for adjusting the ADC's reference voltage */
    282 	rnode = node = NULL;
    283 	sysctl_createv(NULL, 0, NULL, &rnode,
    284 	    CTLFLAG_READWRITE,
    285 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    286 	    NULL, 0, NULL, 0,
    287 	    CTL_HW, CTL_CREATE, CTL_EOL);
    288 
    289 	if (rnode != NULL)
    290 		sysctl_createv(NULL, 0, NULL, &node,
    291 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    292 		    CTLTYPE_INT, "vref",
    293 		    SYSCTL_DESCR("ADC reference voltage"),
    294 		    sysctl_mcp3kadc_vref, 0, (void *)sc, 0,
    295 		    CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL);
    296 }
    297 
    298 static void
    299 mcp3kadc_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    300 {
    301 	struct mcp3kadc_softc *sc;
    302 	const struct mcp3kadc_model *model;
    303 	uint8_t buf[2], ctrl;
    304 	int32_t val, scale;
    305 
    306 	sc = sme->sme_cookie;
    307 	model = sc->sc_model;
    308 	scale = sc->sc_adc_max + 1;
    309 
    310 	if (model->flags & M3K_CTRL_NEEDED) {
    311 		/* we need to send some control bits first */
    312 		ctrl = 1;	/* start bit */
    313 
    314 		if (model->flags & M3K_SGLDIFF) {
    315 			/* bit set to select single-ended mode */
    316 			ctrl <<= 1;
    317 			ctrl |= edata->private >= model->channels;
    318 		}
    319 
    320 		if (model->flags & M3K_D2D1D0) {
    321 			/* 3 bits select the channel */
    322 			ctrl <<= 3;
    323 			ctrl |= edata->private & (model->channels - 1);
    324 		} else {
    325 			/* 1 bit selects between two channels */
    326 			ctrl <<= 1;
    327 			ctrl |= edata->private & 1;
    328 		}
    329 
    330 		if (model->flags & M3K_MSBF) {
    331 			/* bit select MSB first format */
    332 			ctrl <<= 1;
    333 			ctrl |= 1;
    334 		}
    335 
    336 		/* send control bits, receive ADC data */
    337 		if (spi_send_recv(sc->sc_sh, 1, &ctrl, 2, buf) != 0) {
    338 			edata->state = ENVSYS_SINVALID;
    339 			return;
    340 		}
    341 	} else {
    342 
    343 		/* just read data from the ADC */
    344 		if (spi_recv(sc->sc_sh, 2, buf) != 0) {
    345 			edata->state = ENVSYS_SINVALID;
    346 			return;
    347 		}
    348 	}
    349 
    350 	/* extract big-endian ADC data from buffer */
    351 	val = (buf[0] << 8) | buf[1];
    352 	val = (val >> (16 - (model->bits + model->lead))) & sc->sc_adc_max;
    353 
    354 	/* sign-extend the result, when needed */
    355 	if (model->flags & M3K_SIGNED) {
    356 		if (val & (1 << (model->bits - 1)))
    357 			val -= sc->sc_adc_max + 1;
    358 		scale >>= 1;	/* MSB is the sign */
    359 	}
    360 
    361 	/* scale the value for Vref and convert to mV */
    362 	edata->value_cur = (sc->sc_vref_mv * val / scale) * 1000;
    363 	edata->state = ENVSYS_SVALID;
    364 }
    365 
    366 static int
    367 sysctl_mcp3kadc_vref(SYSCTLFN_ARGS)
    368 {
    369 	struct sysctlnode node;
    370 	struct mcp3kadc_softc *sc;
    371 	int32_t t;
    372 	int error;
    373 
    374 	node = *rnode;
    375 	sc = node.sysctl_data;
    376 
    377 	t = sc->sc_vref_mv;
    378 	node.sysctl_data = &t;
    379 
    380 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    381 	if (error || newp == NULL)
    382 		return error;
    383 	if (t <= 0)
    384 		return EINVAL;
    385 
    386 	sc->sc_vref_mv = t;
    387 	return 0;
    388 }
    389