mcp3k.c revision 1.2.46.1 1 /* $NetBSD: mcp3k.c,v 1.2.46.1 2021/08/09 00:30:09 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 /* FDT */
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 static const struct device_compatible_entry compat_data[] = {
192 { .compat = "microchip,mcp3001", .data = &mcp3001 },
193 { .compat = "microchip,mcp3002", .data = &mcp3002 },
194 { .compat = "microchip,mcp3004", .data = &mcp3004 },
195 { .compat = "microchip,mcp3008", .data = &mcp3008 },
196 { .compat = "microchip,mcp3201", .data = &mcp3201 },
197 { .compat = "microchip,mcp3202", .data = &mcp3202 },
198 { .compat = "microchip,mcp3204", .data = &mcp3204 },
199 { .compat = "microchip,mcp3208", .data = &mcp3208 },
200 { .compat = "microchip,mcp3301", .data = &mcp3301 },
201 { .compat = "microchip,mcp3302", .data = &mcp3302 },
202 { .compat = "microchip,mcp3304", .data = &mcp3304 },
203
204 #if 0 /* We should also add support for these: */
205 { .compat = "microchip,mcp3550-50" },
206 { .compat = "microchip,mcp3550-60" },
207 { .compat = "microchip,mcp3551" },
208 { .compat = "microchip,mcp3553" },
209 #endif
210
211 DEVICE_COMPAT_EOL
212 };
213
214 static const struct mcp3kadc_model *
215 mcp3kadc_lookup(const struct spi_attach_args *sa, const cfdata_t cf)
216 {
217 const struct device_compatible_entry *dce;
218
219 if (sa->sa_clist != NULL) {
220 dce = device_compatible_lookup_strlist(sa->sa_clist,
221 sa->sa_clist_size, compat_data);
222 if (dce == NULL) {
223 return NULL;
224 }
225 return dce->data;
226 } else {
227 const struct mcp3kadc_model *model;
228
229 for (dce = compat_data; dce->compat != NULL; dce++) {
230 model = dce->data;
231 if (model->name == cf->cf_flags) {
232 return model;
233 }
234 }
235 return NULL;
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 rv;
244
245 rv = spi_compatible_match(sa, cf, compat_data);
246 if (rv != 0) {
247 /*
248 * If we're doing indirect config, the user must
249 * have specified the correct model.
250 */
251 if (sa->sa_clist == NULL && mcp3kadc_lookup(sa, cf) == NULL) {
252 return 0;
253 }
254 }
255
256 return rv;
257 }
258
259 #ifdef FDT
260 static bool
261 mcp3kadc_vref_fdt(struct mcp3kadc_softc *sc)
262 {
263 devhandle_t devhandle = device_handle(sc->sc_dev);
264 int phandle = devhandle_to_of(devhandle);
265 int error;
266 u_int uvolts;
267
268 sc->sc_vref_supply = fdtbus_regulator_acquire(phandle, "vref-supply");
269 if (sc->sc_vref_supply == NULL) {
270 aprint_error_dev(sc->sc_dev,
271 "unable to acquire \"vref-supply\"\n");
272 return false;
273 }
274
275 error = fdtbus_regulator_enable(sc->sc_vref_supply);
276 if (error) {
277 aprint_error_dev(sc->sc_dev,
278 "failed to enable \"vref-supply\" (error = %d)\n",
279 error);
280 return false;
281 }
282
283 error = fdtbus_regulator_get_voltage(sc->sc_vref_supply, &uvolts);
284 if (error) {
285 aprint_error_dev(sc->sc_dev,
286 "unable to get \"vref-supply\" voltage (error = %d)\n",
287 error);
288 (void) fdtbus_regulator_disable(sc->sc_vref_supply);
289 return false;
290 }
291
292 /*
293 * Device tree property is uV, convert to mV that we use
294 * internally.
295 */
296 sc->sc_vref_mv = uvolts / 1000;
297 return true;
298 }
299 #endif /* FDT */
300
301 static void
302 mcp3kadc_attach(device_t parent, device_t self, void *aux)
303 {
304 const struct sysctlnode *rnode, *node;
305 struct spi_attach_args *sa = aux;
306 struct mcp3kadc_softc *sc = device_private(self);
307 devhandle_t devhandle = device_handle(self);
308 const struct mcp3kadc_model *model;
309 int ch, i, error;
310 bool vref_read_only;
311
312 sc->sc_dev = self;
313 sc->sc_sh = sa->sa_handle;
314
315 model = mcp3kadc_lookup(sa, device_cfdata(self));
316 KASSERT(model != NULL);
317
318 sc->sc_model = model;
319
320 aprint_naive(": Analog to Digital converter\n");
321 aprint_normal(": MCP%u %u-channel %u-bit ADC\n",
322 (unsigned)model->name, (unsigned)model->channels,
323 (unsigned)model->bits);
324
325 /* configure for 1MHz */
326 error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
327 if (error) {
328 aprint_error_dev(self, "spi_configure failed (error = %d)\n",
329 error);
330 return;
331 }
332
333 vref_read_only = false;
334 switch (devhandle_type(devhandle)) {
335 #ifdef FDT
336 case DEVHANDLE_TYPE_OF:
337 vref_read_only = mcp3kadc_vref_fdt(sc);
338 if (! vref_read_only) {
339 /* Error already displayed. */
340 return;
341 }
342 break;
343 #endif /* FDT */
344 default:
345 /*
346 * set a default Vref in mV according to the chip's ADC
347 * resolution
348 */
349 sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
350 model->bits - 1 : model->bits);
351 break;
352 }
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 if (rnode != NULL)
411 sysctl_createv(NULL, 0, NULL, &node,
412 ctlflag | CTLFLAG_OWNDESC,
413 CTLTYPE_INT, "vref",
414 SYSCTL_DESCR("ADC reference voltage"),
415 sysctl_mcp3kadc_vref, 0, (void *)sc, 0,
416 CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL);
417 }
418
419 static void
420 mcp3kadc_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
421 {
422 struct mcp3kadc_softc *sc;
423 const struct mcp3kadc_model *model;
424 uint8_t buf[2], ctrl;
425 int32_t val, scale;
426
427 sc = sme->sme_cookie;
428 model = sc->sc_model;
429 scale = sc->sc_adc_max + 1;
430
431 if (model->flags & M3K_CTRL_NEEDED) {
432 /* we need to send some control bits first */
433 ctrl = 1; /* start bit */
434
435 if (model->flags & M3K_SGLDIFF) {
436 /* bit set to select single-ended mode */
437 ctrl <<= 1;
438 ctrl |= edata->private >= model->channels;
439 }
440
441 if (model->flags & M3K_D2D1D0) {
442 /* 3 bits select the channel */
443 ctrl <<= 3;
444 ctrl |= edata->private & (model->channels - 1);
445 } else {
446 /* 1 bit selects between two channels */
447 ctrl <<= 1;
448 ctrl |= edata->private & 1;
449 }
450
451 if (model->flags & M3K_MSBF) {
452 /* bit select MSB first format */
453 ctrl <<= 1;
454 ctrl |= 1;
455 }
456
457 /* send control bits, receive ADC data */
458 if (spi_send_recv(sc->sc_sh, 1, &ctrl, 2, buf) != 0) {
459 edata->state = ENVSYS_SINVALID;
460 return;
461 }
462 } else {
463
464 /* just read data from the ADC */
465 if (spi_recv(sc->sc_sh, 2, buf) != 0) {
466 edata->state = ENVSYS_SINVALID;
467 return;
468 }
469 }
470
471 /* extract big-endian ADC data from buffer */
472 val = (buf[0] << 8) | buf[1];
473 val = (val >> (16 - (model->bits + model->lead))) & sc->sc_adc_max;
474
475 /* sign-extend the result, when needed */
476 if (model->flags & M3K_SIGNED) {
477 if (val & (1 << (model->bits - 1)))
478 val -= sc->sc_adc_max + 1;
479 scale >>= 1; /* MSB is the sign */
480 }
481
482 /* scale the value for Vref and convert to mV */
483 edata->value_cur = (sc->sc_vref_mv * val / scale) * 1000;
484 edata->state = ENVSYS_SVALID;
485 }
486
487 static int
488 sysctl_mcp3kadc_vref(SYSCTLFN_ARGS)
489 {
490 struct sysctlnode node;
491 struct mcp3kadc_softc *sc;
492 int32_t t;
493 int error;
494
495 node = *rnode;
496 sc = node.sysctl_data;
497
498 t = sc->sc_vref_mv;
499 node.sysctl_data = &t;
500
501 error = sysctl_lookup(SYSCTLFN_CALL(&node));
502 if (error || newp == NULL)
503 return error;
504 if (t <= 0)
505 return EINVAL;
506
507 sc->sc_vref_mv = t;
508 return 0;
509 }
510