Home | History | Annotate | Line # | Download | only in spi
mcp3k.c revision 1.8
      1 /*	$NetBSD: mcp3k.c,v 1.8 2025/09/11 14:32:49 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 "opt_fdt.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/device.h>
     53 #include <sys/kernel.h>
     54 #include <sys/types.h>
     55 #include <sys/sysctl.h>
     56 
     57 #include <dev/sysmon/sysmonvar.h>
     58 #include <dev/spi/spivar.h>
     59 
     60 #ifdef FDT
     61 #include <dev/fdt/fdtvar.h>
     62 #endif
     63 
     64 #define M3K_MAX_SENSORS		16		/* 8 single-ended & 8 diff. */
     65 
     66 /* mcp3x0x model description */
     67 struct mcp3kadc_model {
     68 	uint32_t		name;
     69 	uint8_t			bits;
     70 	uint8_t			channels;
     71 	uint8_t			lead;		/* leading bits to ignore */
     72 	uint8_t			flags;
     73 #define M3K_SGLDIFF		0x01		/* single-ended/differential */
     74 #define M3K_D2D1D0		0x02		/* 3 channel select bits */
     75 #define M3K_MSBF		0x04		/* MSBF select bit */
     76 #define M3K_SIGNED		0x80		/* result is signed */
     77 #define M3K_CTRL_NEEDED		(M3K_SGLDIFF | M3K_D2D1D0 | M3K_MSBF)
     78 };
     79 
     80 struct mcp3kadc_softc {
     81 	device_t			sc_dev;
     82 	struct spi_handle 		*sc_sh;
     83 	const struct mcp3kadc_model	*sc_model;
     84 	uint32_t			sc_adc_max;
     85 	int32_t				sc_vref_mv;
     86 #ifdef FDT
     87 	struct fdtbus_regulator		*sc_vref_supply;
     88 #endif
     89 
     90 	struct sysmon_envsys 		*sc_sme;
     91 	envsys_data_t 			sc_sensors[M3K_MAX_SENSORS];
     92 };
     93 
     94 static int	mcp3kadc_match(device_t, cfdata_t, void *);
     95 static void	mcp3kadc_attach(device_t, device_t, void *);
     96 static void	mcp3kadc_envsys_refresh(struct sysmon_envsys *,
     97 		    envsys_data_t *);
     98 static int	sysctl_mcp3kadc_vref(SYSCTLFN_ARGS);
     99 
    100 CFATTACH_DECL_NEW(mcp3kadc, sizeof(struct mcp3kadc_softc),
    101     mcp3kadc_match,  mcp3kadc_attach, NULL, NULL);
    102 
    103 static const struct mcp3kadc_model mcp3001 = {
    104 	.name = 3001,
    105 	.bits = 10,
    106 	.channels = 1,
    107 	.lead = 3,
    108 	.flags = 0
    109 };
    110 
    111 static const struct mcp3kadc_model mcp3002 = {
    112 	.name = 3002,
    113 	.bits = 10,
    114 	.channels = 2,
    115 	.lead = 2,
    116 	.flags = M3K_SGLDIFF | M3K_MSBF
    117 };
    118 
    119 static const struct mcp3kadc_model mcp3004 = {
    120 	.name = 3004,
    121 	.bits = 10,
    122 	.channels = 4,
    123 	.lead = 2,
    124 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    125 };
    126 
    127 static const struct mcp3kadc_model mcp3008 = {
    128 	.name = 3008,
    129 	.bits = 10,
    130 	.channels = 8,
    131 	.lead = 2,
    132 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    133 };
    134 
    135 static const struct mcp3kadc_model mcp3201 = {
    136 	.name = 3201,
    137 	.bits = 12,
    138 	.channels = 1,
    139 	.lead = 3,
    140 	.flags = 0
    141 };
    142 
    143 static const struct mcp3kadc_model mcp3202 = {
    144 	.name = 3202,
    145 	.bits = 12,
    146 	.channels = 2,
    147 	.lead = 2,
    148 	.flags = M3K_SGLDIFF | M3K_MSBF
    149 };
    150 
    151 static const struct mcp3kadc_model mcp3204 = {
    152 	.name = 3204,
    153 	.bits = 12,
    154 	.channels = 4,
    155 	.lead = 2,
    156 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    157 };
    158 
    159 static const struct mcp3kadc_model mcp3208 = {
    160 	.name = 3208,
    161 	.bits = 12,
    162 	.channels = 8,
    163 	.lead = 2,
    164 	.flags = M3K_SGLDIFF | M3K_D2D1D0
    165 };
    166 
    167 static const struct mcp3kadc_model mcp3301 = {
    168 	.name = 3301,
    169 	.bits = 13,
    170 	.channels = 1,
    171 	.lead = 3,
    172 	.flags = M3K_SIGNED
    173 };
    174 
    175 static const struct mcp3kadc_model mcp3302 = {
    176 	.name = 3302,
    177 	.bits = 13,
    178 	.channels = 4,
    179 	.lead = 2,
    180 	.flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
    181 };
    182 
    183 static const struct mcp3kadc_model mcp3304 = {
    184 	.name = 3304,
    185 	.bits = 13,
    186 	.channels = 8,
    187 	.lead = 2,
    188 	.flags = M3K_SIGNED | M3K_SGLDIFF | M3K_D2D1D0
    189 };
    190 
    191 /*
    192  * N.B. The order of this table is important!  It matches the order
    193  * of the legacy mcp3k_models[] array, which is used to manually
    194  * select the device type in the kernel configuration file when
    195  * direct configuration is not available.
    196  */
    197 static const struct device_compatible_entry compat_data[] = {
    198 	{ .compat = "microchip,mcp3001",	.data = &mcp3001 },
    199 	{ .compat = "microchip,mcp3002",	.data = &mcp3002 },
    200 	{ .compat = "microchip,mcp3004",	.data = &mcp3004 },
    201 	{ .compat = "microchip,mcp3008",	.data = &mcp3008 },
    202 	{ .compat = "microchip,mcp3201",	.data = &mcp3201 },
    203 	{ .compat = "microchip,mcp3202",	.data = &mcp3202 },
    204 	{ .compat = "microchip,mcp3204",	.data = &mcp3204 },
    205 	{ .compat = "microchip,mcp3208",	.data = &mcp3208 },
    206 	{ .compat = "microchip,mcp3301",	.data = &mcp3301 },
    207 	{ .compat = "microchip,mcp3302",	.data = &mcp3302 },
    208 	{ .compat = "microchip,mcp3304",	.data = &mcp3304 },
    209 
    210 #if 0	/* We should also add support for these: */
    211 	{ .compat = "microchip,mcp3550-50" },
    212 	{ .compat = "microchip,mcp3550-60" },
    213 	{ .compat = "microchip,mcp3551" },
    214 	{ .compat = "microchip,mcp3553" },
    215 #endif
    216 
    217 	DEVICE_COMPAT_EOL
    218 };
    219 static const int mcp3k_nmodels = __arraycount(compat_data) - 1;
    220 
    221 static const struct mcp3kadc_model *
    222 mcp3kadc_lookup(const struct spi_attach_args *sa, const cfdata_t cf)
    223 {
    224 	if (sa->sa_ncompat > 0) {
    225 		const struct device_compatible_entry *dce =
    226 		    spi_compatible_lookup(sa, compat_data);
    227 		if (dce == NULL) {
    228 			return NULL;
    229 		}
    230 		return dce->data;
    231 	} else {
    232 		if (cf->cf_flags < 0 || cf->cf_flags >= mcp3k_nmodels) {
    233 			return NULL;
    234 		}
    235 		return compat_data[cf->cf_flags].data;
    236 	}
    237 }
    238 
    239 static int
    240 mcp3kadc_match(device_t parent, cfdata_t cf, void *aux)
    241 {
    242 	struct spi_attach_args *sa = aux;
    243 	int match_result;
    244 
    245 	if (spi_use_direct_match(sa, compat_data, &match_result)) {
    246 		return match_result;
    247 	}
    248 
    249 	/*
    250 	 * If we're doing indirect config, the user must
    251 	 * have specified a valid model.
    252 	 */
    253 	if (sa->sa_ncompat == 0 && mcp3kadc_lookup(sa, cf) == NULL) {
    254 		return 0;
    255 	}
    256 
    257 	return SPI_MATCH_DEFAULT;
    258 }
    259 
    260 #ifdef FDT
    261 static bool
    262 mcp3kadc_vref_fdt(struct mcp3kadc_softc *sc)
    263 {
    264 	devhandle_t devhandle = device_handle(sc->sc_dev);
    265 	int phandle = devhandle_to_of(devhandle);
    266 	int error;
    267 	u_int uvolts;
    268 
    269 	sc->sc_vref_supply = fdtbus_regulator_acquire(phandle, "vref-supply");
    270 	if (sc->sc_vref_supply == NULL) {
    271 		aprint_error_dev(sc->sc_dev,
    272 		    "unable to acquire \"vref-supply\"\n");
    273 		return false;
    274 	}
    275 
    276 	error = fdtbus_regulator_enable(sc->sc_vref_supply);
    277 	if (error) {
    278 		aprint_error_dev(sc->sc_dev,
    279 		    "failed to enable \"vref-supply\" (error = %d)\n",
    280 		    error);
    281 		return false;
    282 	}
    283 
    284 	error = fdtbus_regulator_get_voltage(sc->sc_vref_supply, &uvolts);
    285 	if (error) {
    286 		aprint_error_dev(sc->sc_dev,
    287 		    "unable to get \"vref-supply\" voltage (error = %d)\n",
    288 		    error);
    289 		(void) fdtbus_regulator_disable(sc->sc_vref_supply);
    290 		return false;
    291 	}
    292 
    293 	/*
    294 	 * Device tree property is uV, convert to mV that we use
    295 	 * internally.
    296 	 */
    297 	sc->sc_vref_mv = uvolts / 1000;
    298 	return true;
    299 }
    300 #endif /* FDT */
    301 
    302 static void
    303 mcp3kadc_attach(device_t parent, device_t self, void *aux)
    304 {
    305 	const struct sysctlnode *rnode, *node;
    306 	struct spi_attach_args *sa = aux;
    307 	struct mcp3kadc_softc *sc = device_private(self);;
    308 	devhandle_t devhandle = device_handle(self);
    309 	const struct mcp3kadc_model *model;
    310 	int error, ch, i;
    311 	bool vref_read_only;
    312 
    313 	sc->sc_dev = self;
    314 	sc->sc_sh = sa->sa_handle;
    315 
    316 	model = mcp3kadc_lookup(sa, device_cfdata(self));
    317 	KASSERT(model != NULL);
    318 
    319 	sc->sc_model = model;
    320 
    321 	aprint_naive(": Analog to Digital converter\n");
    322 	aprint_normal(": MCP%u %u-channel %u-bit ADC\n",
    323 	    (unsigned)model->name, (unsigned)model->channels,
    324 	    (unsigned)model->bits);
    325 
    326 	/* configure for 1MHz */
    327 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, SPI_FREQ_MHz(1));
    328 	if (error) {
    329 		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
    330 		    error);
    331 		return;
    332 	}
    333 
    334 	vref_read_only = false;
    335 	switch (devhandle_type(devhandle)) {
    336 #ifdef FDT
    337 	case DEVHANDLE_TYPE_OF:
    338 		vref_read_only = mcp3kadc_vref_fdt(sc);
    339 		if (! vref_read_only) {
    340 			/* Error already displayed. */
    341 			return;
    342 		}
    343 		break;
    344 #endif /* FDT */
    345 	default:
    346 		/*
    347 		 * Set a default Vref in mV according to the chip's ADC
    348 		 * resolution.
    349 		 */
    350 		sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
    351 		    model->bits - 1 : model->bits);
    352 		break;
    353 	}
    354 
    355 	/* remember maximum value for this ADC - also used for masking */
    356 	sc->sc_adc_max = (1 << model->bits) - 1;
    357 
    358 	/* attach voltage sensors to envsys */
    359 	sc->sc_sme = sysmon_envsys_create();
    360 
    361 	/* adc difference from two neighbouring channels */
    362 	for (ch = 0; ch < model->channels; ch++) {
    363 		KASSERT(ch < M3K_MAX_SENSORS);
    364 		sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
    365 		sc->sc_sensors[ch].state = ENVSYS_SINVALID;
    366 		if (model->channels == 1)
    367 			strlcpy(sc->sc_sensors[ch].desc, "adc diff ch0",
    368 			    sizeof(sc->sc_sensors[ch].desc));
    369 		else
    370 			snprintf(sc->sc_sensors[ch].desc,
    371 			    sizeof(sc->sc_sensors[ch].desc),
    372 			    "adc diff ch%d-ch%d", ch, ch ^ 1);
    373 		sc->sc_sensors[ch].private = ch;
    374 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[ch]);
    375 	}
    376 
    377 	if (model->flags & M3K_SGLDIFF) {
    378 		/* adc from single ended channels */
    379 		for (i = 0; i < model->channels; i++, ch++) {
    380 			KASSERT(ch < M3K_MAX_SENSORS);
    381 			sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
    382 			sc->sc_sensors[ch].state = ENVSYS_SINVALID;
    383 			snprintf(sc->sc_sensors[ch].desc,
    384 			    sizeof(sc->sc_sensors[ch].desc),
    385 			    "adc single ch%d", i);
    386 			sc->sc_sensors[ch].private = ch;
    387 			sysmon_envsys_sensor_attach(sc->sc_sme,
    388 			    &sc->sc_sensors[ch]);
    389 		}
    390 	}
    391 
    392 	sc->sc_sme->sme_name = device_xname(self);
    393 	sc->sc_sme->sme_refresh = mcp3kadc_envsys_refresh;
    394 	sc->sc_sme->sme_cookie = sc;
    395 	if (sysmon_envsys_register(sc->sc_sme)) {
    396 		aprint_error_dev(self, "unable to register with sysmon\n");
    397 		sysmon_envsys_destroy(sc->sc_sme);
    398 	}
    399 
    400 	/* create a sysctl node for adjusting the ADC's reference voltage */
    401 	rnode = node = NULL;
    402 	sysctl_createv(NULL, 0, NULL, &rnode,
    403 	    CTLFLAG_READWRITE,
    404 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    405 	    NULL, 0, NULL, 0,
    406 	    CTL_HW, CTL_CREATE, CTL_EOL);
    407 
    408 	const int ctlflag = vref_read_only ? CTLFLAG_READONLY
    409 					   : CTLFLAG_READWRITE;
    410 
    411 	if (rnode != NULL)
    412 		sysctl_createv(NULL, 0, NULL, &node,
    413 		    ctlflag | CTLFLAG_OWNDESC,
    414 		    CTLTYPE_INT, "vref",
    415 		    SYSCTL_DESCR("ADC reference voltage"),
    416 		    sysctl_mcp3kadc_vref, 0, (void *)sc, 0,
    417 		    CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL);
    418 }
    419 
    420 static void
    421 mcp3kadc_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    422 {
    423 	struct mcp3kadc_softc *sc;
    424 	const struct mcp3kadc_model *model;
    425 	uint8_t buf[2], ctrl;
    426 	int32_t val, scale;
    427 
    428 	sc = sme->sme_cookie;
    429 	model = sc->sc_model;
    430 	scale = sc->sc_adc_max + 1;
    431 
    432 	if (model->flags & M3K_CTRL_NEEDED) {
    433 		/* we need to send some control bits first */
    434 		ctrl = 1;	/* start bit */
    435 
    436 		if (model->flags & M3K_SGLDIFF) {
    437 			/* bit set to select single-ended mode */
    438 			ctrl <<= 1;
    439 			ctrl |= edata->private >= model->channels;
    440 		}
    441 
    442 		if (model->flags & M3K_D2D1D0) {
    443 			/* 3 bits select the channel */
    444 			ctrl <<= 3;
    445 			ctrl |= edata->private & (model->channels - 1);
    446 		} else {
    447 			/* 1 bit selects between two channels */
    448 			ctrl <<= 1;
    449 			ctrl |= edata->private & 1;
    450 		}
    451 
    452 		if (model->flags & M3K_MSBF) {
    453 			/* bit select MSB first format */
    454 			ctrl <<= 1;
    455 			ctrl |= 1;
    456 		}
    457 
    458 		/* send control bits, receive ADC data */
    459 		if (spi_send_recv(sc->sc_sh, 1, &ctrl, 2, buf) != 0) {
    460 			edata->state = ENVSYS_SINVALID;
    461 			return;
    462 		}
    463 	} else {
    464 
    465 		/* just read data from the ADC */
    466 		if (spi_recv(sc->sc_sh, 2, buf) != 0) {
    467 			edata->state = ENVSYS_SINVALID;
    468 			return;
    469 		}
    470 	}
    471 
    472 	/* extract big-endian ADC data from buffer */
    473 	val = (buf[0] << 8) | buf[1];
    474 	val = (val >> (16 - (model->bits + model->lead))) & sc->sc_adc_max;
    475 
    476 	/* sign-extend the result, when needed */
    477 	if (model->flags & M3K_SIGNED) {
    478 		if (val & (1 << (model->bits - 1)))
    479 			val -= sc->sc_adc_max + 1;
    480 		scale >>= 1;	/* MSB is the sign */
    481 	}
    482 
    483 	/* scale the value for Vref and convert to mV */
    484 	edata->value_cur = (sc->sc_vref_mv * val / scale) * 1000;
    485 	edata->state = ENVSYS_SVALID;
    486 }
    487 
    488 static int
    489 sysctl_mcp3kadc_vref(SYSCTLFN_ARGS)
    490 {
    491 	struct sysctlnode node;
    492 	struct mcp3kadc_softc *sc;
    493 	int32_t t;
    494 	int error;
    495 
    496 	node = *rnode;
    497 	sc = node.sysctl_data;
    498 
    499 	t = sc->sc_vref_mv;
    500 	node.sysctl_data = &t;
    501 
    502 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    503 	if (error || newp == NULL)
    504 		return error;
    505 	if (t <= 0)
    506 		return EINVAL;
    507 
    508 	sc->sc_vref_mv = t;
    509 	return 0;
    510 }
    511