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