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
|
/* $NetBSD: mpl115a.c,v 1.3 2019/12/23 14:50:43 thorpej Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Radoslaw Kujawa.
*
* 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.
*/
/*
* Freescale MPL115A2 miniature digital barometer driver.
*
* This driver could be split into bus-indepented driver and I2C-specific
* attachment, as SPI variant of this chip also exist.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mpl115a.c,v 1.3 2019/12/23 14:50:43 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/endian.h>
#include <sys/bus.h>
#include <dev/i2c/i2cvar.h>
#include <dev/sysmon/sysmonvar.h>
#include <dev/i2c/mpl115areg.h>
#define MPL115A_DEBUG 1
struct mpl115a_softc {
device_t sc_dev;
i2c_tag_t sc_tag;
i2c_addr_t sc_addr;
/* raw coefficients */
int16_t sc_a0;
int16_t sc_b1;
int16_t sc_b2;
int16_t sc_c12;
/* envsys(4) stuff */
struct sysmon_envsys *sc_sme;
envsys_data_t sc_sensor;
kmutex_t sc_lock;
};
static int mpl115a_match(device_t, cfdata_t, void *);
static void mpl115a_attach(device_t, device_t, void *);
static uint8_t mpl115a_reg_read_1(struct mpl115a_softc *sc, uint8_t);
static void mpl115a_reg_write_1(struct mpl115a_softc *sc, uint8_t, uint8_t);
static void mpl115a_load_coeffs(struct mpl115a_softc *sc);
static uint16_t mpl115a_make_coeff(uint8_t msb, uint8_t lsb);
static uint32_t mpl115a_pressure(struct mpl115a_softc *sc);
static uint32_t mpl115a_calc(struct mpl115a_softc *sc, uint16_t padc, uint16_t tadc) ;
static void mpl115a_envsys_register(struct mpl115a_softc *);
static void mpl115a_envsys_refresh(struct sysmon_envsys *, envsys_data_t *);
CFATTACH_DECL_NEW(mpl115a, sizeof (struct mpl115a_softc),
mpl115a_match, mpl115a_attach, NULL, NULL);
static int
mpl115a_match(device_t parent, cfdata_t cf, void *aux)
{
struct i2c_attach_args *ia = aux;
if (ia->ia_addr == MPL115A_ADDR)
return I2C_MATCH_ADDRESS_ONLY;
return 0;
}
static void
mpl115a_attach(device_t parent, device_t self, void *aux)
{
struct mpl115a_softc *sc = device_private(self);
struct i2c_attach_args *ia = aux;
sc->sc_dev = self;
sc->sc_addr = ia->ia_addr;
sc->sc_tag = ia->ia_tag;
aprint_normal(": Freescale MPL115A2 Pressure Sensor\n");
/* Since coefficients do not change load them once. */
mpl115a_load_coeffs(sc);
mpl115a_pressure(sc);
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
mpl115a_envsys_register(sc);
}
/* Construct a coefficients from MSB and LSB bytes. */
static uint16_t
mpl115a_make_coeff(uint8_t msb, uint8_t lsb)
{
uint16_t rv;
rv = le16toh((msb << 8) | lsb);
#ifdef MPL115A_DEBUG
aprint_normal("msb %x, lsb %x, ret val %x\n", msb, lsb, rv);
#endif /* MPL115A_DEBUG */
return rv;
}
static void
mpl115a_load_coeffs(struct mpl115a_softc *sc)
{
sc->sc_a0 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_A0_MSB),
mpl115a_reg_read_1(sc, MPL115A_A0_LSB));
sc->sc_b1 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_B1_MSB),
mpl115a_reg_read_1(sc, MPL115A_B1_LSB));
sc->sc_b2 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_B2_MSB),
mpl115a_reg_read_1(sc, MPL115A_B2_LSB));
sc->sc_c12 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_C12_MSB),
mpl115a_reg_read_1(sc, MPL115A_C12_LSB));
}
static void
mpl115a_reg_write_1(struct mpl115a_softc *sc, uint8_t reg, uint8_t val)
{
if (iic_acquire_bus(sc->sc_tag, 0) != 0) {
aprint_error_dev(sc->sc_dev, "cannot acquire bus for write\n");
return;
}
if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, ®, 1,
&val, 1, 0)) {
aprint_error_dev(sc->sc_dev, "cannot execute write\n");
}
iic_release_bus(sc->sc_tag, 0);
}
static uint8_t
mpl115a_reg_read_1(struct mpl115a_softc *sc, uint8_t reg)
{
uint8_t rv, wbuf[2];
if (iic_acquire_bus(sc->sc_tag, 0) != 0) {
#ifdef MPL115A_DEBUG
aprint_error_dev(sc->sc_dev, "cannot acquire bus for read\n");
#endif /* MPL115A_DEBUG */
return 0;
}
wbuf[0] = reg;
if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, wbuf,
1, &rv, 1, 0)) {
iic_release_bus(sc->sc_tag, 0);
aprint_error_dev(sc->sc_dev, "cannot execute read\n");
return 0;
}
iic_release_bus(sc->sc_tag, 0);
return rv;
}
/* Get pressure in pascals. */
static uint32_t
mpl115a_pressure(struct mpl115a_softc *sc)
{
uint32_t rv;
uint16_t padc, tadc;
rv = 0;
mpl115a_reg_write_1(sc, MPL115A_CONVERT, 1);
delay(20); /* even 3 should be enough */
padc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_PADC_MSB),
mpl115a_reg_read_1(sc, MPL115A_PADC_LSB)); /* XXX, this fails */
padc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_PADC_MSB),
mpl115a_reg_read_1(sc, MPL115A_PADC_LSB));
tadc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_TADC_MSB),
mpl115a_reg_read_1(sc, MPL115A_TADC_LSB));
#ifdef MPL115A_DEBUG
aprint_normal_dev(sc->sc_dev, "padc %x, tadc %x\n", padc, tadc);
#endif /* MPL115A_DEBUG */
rv = mpl115a_calc(sc, padc, tadc);
#ifdef MPL115A_DEBUG
aprint_normal_dev(sc->sc_dev, "%d Pa\n", rv);
#endif /* MPL115A_DEBUG */
return rv;
}
/* The following calculation routine was suggested by Matt Thomas. */
static uint32_t
mpl115a_calc(struct mpl115a_softc *sc, uint16_t padc, uint16_t tadc)
{
int32_t fp_a0, fp_b1, fp_b2, fp_c12, fp_padc, fp_tadc;
int32_t c12x2, a1, a1x2, y1, a2x2, pcomp;
uint32_t pre_kpa1, pre_kpa2, pa;
/* convert coefficients to common fixed point format */
fp_a0 = sc->sc_a0 << 13; /* 12.3 -> 15.16 */
fp_b1 = sc->sc_b1 << 3; /* 2.13 -> 15.16 */
fp_b2 = sc->sc_b2 << 2; /* 1.14 -> 15.16 */
fp_c12 = sc->sc_c12 >> 2; /* 0.22 */
fp_padc = padc >> 6;
fp_tadc = tadc >> 6;
c12x2 = (fp_c12 * fp_tadc + 0x3f) >> 6;
a1 = fp_b1 + c12x2;
a1x2 = a1 * fp_padc;
y1 = fp_a0 + a1x2;
a2x2 = fp_b2 * fp_tadc;
pcomp = y1 + a2x2;
/* pre_kpa has 16 fractional digits so it's accurate to 1/65536 kPa */
pre_kpa1 = pcomp * (115 - 50);
pre_kpa2 = pre_kpa1 / 1023 + (50 << 16);
/* but the real accuracy of the sensor is around +/- 1 kPa... */
pa = ((pre_kpa2 + 32768) >> 16) * 1000;
return pa;
}
static void
mpl115a_envsys_register(struct mpl115a_softc *sc)
{
sc->sc_sme = sysmon_envsys_create();
strlcpy(sc->sc_sensor.desc, "Absolute pressure",
sizeof(sc->sc_sensor.desc));
/* sc->sc_sensor.units = ENVSYS_SPRESSURE; */
sc->sc_sensor.units = ENVSYS_INTEGER;
sc->sc_sensor.state = ENVSYS_SINVALID;
if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
aprint_error_dev(sc->sc_dev,
"error attaching sensor\n");
return;
}
sc->sc_sme->sme_name = device_xname(sc->sc_dev);
sc->sc_sme->sme_cookie = sc;
sc->sc_sme->sme_refresh = mpl115a_envsys_refresh;
if (sysmon_envsys_register(sc->sc_sme)) {
aprint_error_dev(sc->sc_dev, "unable to register in sysmon\n");
sysmon_envsys_destroy(sc->sc_sme);
}
}
static void
mpl115a_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
struct mpl115a_softc *sc = sme->sme_cookie;
mutex_enter(&sc->sc_lock);
edata->value_cur = mpl115a_pressure(sc);
edata->state = ENVSYS_SVALID;
mutex_exit(&sc->sc_lock);
}
|