mcp3k.c revision 1.2.36.1 1 /* $NetBSD: mcp3k.c,v 1.2.36.1 2021/05/19 03:33: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 static const struct device_compatible_entry compat_data[] = {
183 { .compat = "microchip,mcp3001", .data = &mcp3001 },
184 { .compat = "microchip,mcp3002", .data = &mcp3002 },
185 { .compat = "microchip,mcp3004", .data = &mcp3004 },
186 { .compat = "microchip,mcp3008", .data = &mcp3008 },
187 { .compat = "microchip,mcp3201", .data = &mcp3201 },
188 { .compat = "microchip,mcp3202", .data = &mcp3202 },
189 { .compat = "microchip,mcp3204", .data = &mcp3204 },
190 { .compat = "microchip,mcp3208", .data = &mcp3208 },
191 { .compat = "microchip,mcp3301", .data = &mcp3301 },
192 { .compat = "microchip,mcp3302", .data = &mcp3302 },
193 { .compat = "microchip,mcp3304", .data = &mcp3304 },
194
195 #if 0 /* We should also add support for these: */
196 { .compat = "microchip,mcp3550-50" },
197 { .compat = "microchip,mcp3550-60" },
198 { .compat = "microchip,mcp3551" },
199 { .compat = "microchip,mcp3553" },
200 #endif
201
202 DEVICE_COMPAT_EOL
203 };
204
205 static const struct mcp3kadc_model *
206 mcp3kadc_lookup(const struct spi_attach_args *sa, const cfdata_t cf)
207 {
208 const struct device_compatible_entry *dce;
209
210 if (sa->sa_clist != NULL) {
211 dce = device_compatible_lookup_strlist(sa->sa_clist,
212 sa->sa_clist_size, compat_data);
213 if (dce == NULL) {
214 return NULL;
215 }
216 return dce->data;
217 } else {
218 const struct mcp3kadc_model *model;
219
220 for (dce = compat_data; dce->compat != NULL; dce++) {
221 model = dce->data;
222 if (model->name == cf->cf_flags) {
223 return model;
224 }
225 }
226 return NULL;
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 rv;
235
236 rv = spi_compatible_match(sa, cf, compat_data);
237 if (rv != 0) {
238 /*
239 * If we're doing indirect config, the user must
240 * have specified the correct model.
241 */
242 if (sa->sa_clist == NULL && mcp3kadc_lookup(sa, cf) == NULL) {
243 return 0;
244 }
245
246 /* configure for 1MHz */
247 if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
248 return 0;
249 }
250
251 return rv;
252 }
253
254 static void
255 mcp3kadc_attach(device_t parent, device_t self, void *aux)
256 {
257 const struct sysctlnode *rnode, *node;
258 struct spi_attach_args *sa = aux;
259 struct mcp3kadc_softc *sc = device_private(self);
260 const struct mcp3kadc_model *model;
261 int ch, i;
262
263 sc->sc_dev = self;
264 sc->sc_sh = sa->sa_handle;
265
266 model = mcp3kadc_lookup(sa, device_cfdata(self));
267 KASSERT(model != NULL);
268
269 sc->sc_model = model;
270
271 aprint_naive(": Analog to Digital converter\n");
272 aprint_normal(": MCP%u %u-channel %u-bit ADC\n",
273 (unsigned)model->name, (unsigned)model->channels,
274 (unsigned)model->bits);
275
276 /*
277 * XXX Get vref-supply from device tree and make the sysctl
278 * XXX read-only in that case.
279 */
280 /* set a default Vref in mV according to the chip's ADC resolution */
281 sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
282 model->bits - 1 : model->bits);
283
284 /* remember maximum value for this ADC - also used for masking */
285 sc->sc_adc_max = (1 << model->bits) - 1;
286
287 /* attach voltage sensors to envsys */
288 sc->sc_sme = sysmon_envsys_create();
289
290 /* adc difference from two neighbouring channels */
291 for (ch = 0; ch < model->channels; ch++) {
292 KASSERT(ch < M3K_MAX_SENSORS);
293 sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
294 sc->sc_sensors[ch].state = ENVSYS_SINVALID;
295 if (model->channels == 1)
296 strlcpy(sc->sc_sensors[ch].desc, "adc diff ch0",
297 sizeof(sc->sc_sensors[ch].desc));
298 else
299 snprintf(sc->sc_sensors[ch].desc,
300 sizeof(sc->sc_sensors[ch].desc),
301 "adc diff ch%d-ch%d", ch, ch ^ 1);
302 sc->sc_sensors[ch].private = ch;
303 sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[ch]);
304 }
305
306 if (model->flags & M3K_SGLDIFF) {
307 /* adc from single ended channels */
308 for (i = 0; i < model->channels; i++, ch++) {
309 KASSERT(ch < M3K_MAX_SENSORS);
310 sc->sc_sensors[ch].units = ENVSYS_SVOLTS_DC;
311 sc->sc_sensors[ch].state = ENVSYS_SINVALID;
312 snprintf(sc->sc_sensors[ch].desc,
313 sizeof(sc->sc_sensors[ch].desc),
314 "adc single ch%d", i);
315 sc->sc_sensors[ch].private = ch;
316 sysmon_envsys_sensor_attach(sc->sc_sme,
317 &sc->sc_sensors[ch]);
318 }
319 }
320
321 sc->sc_sme->sme_name = device_xname(self);
322 sc->sc_sme->sme_refresh = mcp3kadc_envsys_refresh;
323 sc->sc_sme->sme_cookie = sc;
324 if (sysmon_envsys_register(sc->sc_sme)) {
325 aprint_error_dev(self, "unable to register with sysmon\n");
326 sysmon_envsys_destroy(sc->sc_sme);
327 }
328
329 /* create a sysctl node for adjusting the ADC's reference voltage */
330 rnode = node = NULL;
331 sysctl_createv(NULL, 0, NULL, &rnode,
332 CTLFLAG_READWRITE,
333 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
334 NULL, 0, NULL, 0,
335 CTL_HW, CTL_CREATE, CTL_EOL);
336
337 if (rnode != NULL)
338 sysctl_createv(NULL, 0, NULL, &node,
339 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
340 CTLTYPE_INT, "vref",
341 SYSCTL_DESCR("ADC reference voltage"),
342 sysctl_mcp3kadc_vref, 0, (void *)sc, 0,
343 CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL);
344 }
345
346 static void
347 mcp3kadc_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
348 {
349 struct mcp3kadc_softc *sc;
350 const struct mcp3kadc_model *model;
351 uint8_t buf[2], ctrl;
352 int32_t val, scale;
353
354 sc = sme->sme_cookie;
355 model = sc->sc_model;
356 scale = sc->sc_adc_max + 1;
357
358 if (model->flags & M3K_CTRL_NEEDED) {
359 /* we need to send some control bits first */
360 ctrl = 1; /* start bit */
361
362 if (model->flags & M3K_SGLDIFF) {
363 /* bit set to select single-ended mode */
364 ctrl <<= 1;
365 ctrl |= edata->private >= model->channels;
366 }
367
368 if (model->flags & M3K_D2D1D0) {
369 /* 3 bits select the channel */
370 ctrl <<= 3;
371 ctrl |= edata->private & (model->channels - 1);
372 } else {
373 /* 1 bit selects between two channels */
374 ctrl <<= 1;
375 ctrl |= edata->private & 1;
376 }
377
378 if (model->flags & M3K_MSBF) {
379 /* bit select MSB first format */
380 ctrl <<= 1;
381 ctrl |= 1;
382 }
383
384 /* send control bits, receive ADC data */
385 if (spi_send_recv(sc->sc_sh, 1, &ctrl, 2, buf) != 0) {
386 edata->state = ENVSYS_SINVALID;
387 return;
388 }
389 } else {
390
391 /* just read data from the ADC */
392 if (spi_recv(sc->sc_sh, 2, buf) != 0) {
393 edata->state = ENVSYS_SINVALID;
394 return;
395 }
396 }
397
398 /* extract big-endian ADC data from buffer */
399 val = (buf[0] << 8) | buf[1];
400 val = (val >> (16 - (model->bits + model->lead))) & sc->sc_adc_max;
401
402 /* sign-extend the result, when needed */
403 if (model->flags & M3K_SIGNED) {
404 if (val & (1 << (model->bits - 1)))
405 val -= sc->sc_adc_max + 1;
406 scale >>= 1; /* MSB is the sign */
407 }
408
409 /* scale the value for Vref and convert to mV */
410 edata->value_cur = (sc->sc_vref_mv * val / scale) * 1000;
411 edata->state = ENVSYS_SVALID;
412 }
413
414 static int
415 sysctl_mcp3kadc_vref(SYSCTLFN_ARGS)
416 {
417 struct sysctlnode node;
418 struct mcp3kadc_softc *sc;
419 int32_t t;
420 int error;
421
422 node = *rnode;
423 sc = node.sysctl_data;
424
425 t = sc->sc_vref_mv;
426 node.sysctl_data = &t;
427
428 error = sysctl_lookup(SYSCTLFN_CALL(&node));
429 if (error || newp == NULL)
430 return error;
431 if (t <= 0)
432 return EINVAL;
433
434 sc->sc_vref_mv = t;
435 return 0;
436 }
437