1 /* $NetBSD: mpl115a.c,v 1.1 2013/09/08 14:59:42 rkujawa Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Radoslaw Kujawa.
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 * Freescale MPL115A2 miniature digital barometer driver.
34 *
35 * This driver could be split into bus-indepented driver and I2C-specific
36 * attachment, as SPI variant of this chip also exist.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: mpl115a.c,v 1.1 2013/09/08 14:59:42 rkujawa Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/endian.h>
48
49 #include <sys/bus.h>
50 #include <dev/i2c/i2cvar.h>
51
52 #include <dev/sysmon/sysmonvar.h>
53
54 #include <dev/i2c/mpl115areg.h>
55
56 #define MPL115A_DEBUG 1
57
58 struct mpl115a_softc {
59 device_t sc_dev;
60
61 i2c_tag_t sc_tag;
62 i2c_addr_t sc_addr;
63
64 /* raw coefficients */
65 int16_t sc_a0;
66 int16_t sc_b1;
67 int16_t sc_b2;
68 int16_t sc_c12;
69
70 /* envsys(4) stuff */
71 struct sysmon_envsys *sc_sme;
72 envsys_data_t sc_sensor;
73 kmutex_t sc_lock;
74 };
75
76
77 static int mpl115a_match(device_t, cfdata_t, void *);
78 static void mpl115a_attach(device_t, device_t, void *);
79
80 static uint8_t mpl115a_reg_read_1(struct mpl115a_softc *sc, uint8_t);
81 static void mpl115a_reg_write_1(struct mpl115a_softc *sc, uint8_t, uint8_t);
82
83 static void mpl115a_load_coeffs(struct mpl115a_softc *sc);
84 static uint16_t mpl115a_make_coeff(uint8_t msb, uint8_t lsb);
85 static uint32_t mpl115a_pressure(struct mpl115a_softc *sc);
86 static uint32_t mpl115a_calc(struct mpl115a_softc *sc, uint16_t padc, uint16_t tadc) ;
87
88 static void mpl115a_envsys_register(struct mpl115a_softc *);
89 static void mpl115a_envsys_refresh(struct sysmon_envsys *, envsys_data_t *);
90
91 CFATTACH_DECL_NEW(mpl115a, sizeof (struct mpl115a_softc),
92 mpl115a_match, mpl115a_attach, NULL, NULL);
93
94 static int
mpl115a_match(device_t parent,cfdata_t cf,void * aux)95 mpl115a_match(device_t parent, cfdata_t cf, void *aux)
96 {
97 struct i2c_attach_args *ia = aux;
98
99 if (ia->ia_addr == MPL115A_ADDR)
100 return 1;
101 return 0;
102 }
103
104 static void
mpl115a_attach(device_t parent,device_t self,void * aux)105 mpl115a_attach(device_t parent, device_t self, void *aux)
106 {
107 struct mpl115a_softc *sc = device_private(self);
108 struct i2c_attach_args *ia = aux;
109
110 sc->sc_dev = self;
111 sc->sc_addr = ia->ia_addr;
112 sc->sc_tag = ia->ia_tag;
113
114 aprint_normal(": Freescale MPL115A2 Pressure Sensor\n");
115
116 /* Since coefficients do not change load them once. */
117 mpl115a_load_coeffs(sc);
118
119 mpl115a_pressure(sc);
120
121 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
122
123 mpl115a_envsys_register(sc);
124 }
125
126 /* Construct a coefficients from MSB and LSB bytes. */
127 static uint16_t
mpl115a_make_coeff(uint8_t msb,uint8_t lsb)128 mpl115a_make_coeff(uint8_t msb, uint8_t lsb)
129 {
130 uint16_t rv;
131
132 rv = le16toh((msb << 8) | lsb);
133
134 #ifdef MPL115A_DEBUG
135 aprint_normal("msb %x, lsb %x, ret val %x\n", msb, lsb, rv);
136 #endif /* MPL115A_DEBUG */
137
138 return rv;
139 }
140
141 static void
mpl115a_load_coeffs(struct mpl115a_softc * sc)142 mpl115a_load_coeffs(struct mpl115a_softc *sc)
143 {
144 sc->sc_a0 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_A0_MSB),
145 mpl115a_reg_read_1(sc, MPL115A_A0_LSB));
146 sc->sc_b1 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_B1_MSB),
147 mpl115a_reg_read_1(sc, MPL115A_B1_LSB));
148 sc->sc_b2 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_B2_MSB),
149 mpl115a_reg_read_1(sc, MPL115A_B2_LSB));
150 sc->sc_c12 = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_C12_MSB),
151 mpl115a_reg_read_1(sc, MPL115A_C12_LSB));
152 }
153
154 static void
mpl115a_reg_write_1(struct mpl115a_softc * sc,uint8_t reg,uint8_t val)155 mpl115a_reg_write_1(struct mpl115a_softc *sc, uint8_t reg, uint8_t val)
156 {
157 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL) != 0) {
158 aprint_error_dev(sc->sc_dev, "cannot acquire bus for write\n");
159 return;
160 }
161
162 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, ®, 1,
163 &val, 1, I2C_F_POLL)) {
164 aprint_error_dev(sc->sc_dev, "cannot execute write\n");
165 }
166 iic_release_bus(sc->sc_tag, I2C_F_POLL);
167 }
168
169 static uint8_t
mpl115a_reg_read_1(struct mpl115a_softc * sc,uint8_t reg)170 mpl115a_reg_read_1(struct mpl115a_softc *sc, uint8_t reg)
171 {
172 uint8_t rv, wbuf[2];
173
174 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL) != 0) {
175 #ifdef MPL115A_DEBUG
176 aprint_error_dev(sc->sc_dev, "cannot acquire bus for read\n");
177 #endif /* MPL115A_DEBUG */
178 return 0;
179 }
180
181 wbuf[0] = reg;
182
183 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, wbuf,
184 1, &rv, 1, I2C_F_POLL)) {
185 aprint_error_dev(sc->sc_dev, "cannot execute read\n");
186 iic_release_bus(sc->sc_tag, I2C_F_POLL);
187 return 0;
188 }
189 iic_release_bus(sc->sc_tag, I2C_F_POLL);
190
191 return rv;
192 }
193
194
195
196 /* Get pressure in pascals. */
197 static uint32_t
mpl115a_pressure(struct mpl115a_softc * sc)198 mpl115a_pressure(struct mpl115a_softc *sc)
199 {
200 uint32_t rv;
201
202 uint16_t padc, tadc;
203
204 rv = 0;
205
206 mpl115a_reg_write_1(sc, MPL115A_CONVERT, 1);
207 delay(20); /* even 3 should be enough */
208
209 padc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_PADC_MSB),
210 mpl115a_reg_read_1(sc, MPL115A_PADC_LSB)); /* XXX, this fails */
211 padc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_PADC_MSB),
212 mpl115a_reg_read_1(sc, MPL115A_PADC_LSB));
213
214 tadc = mpl115a_make_coeff(mpl115a_reg_read_1(sc, MPL115A_TADC_MSB),
215 mpl115a_reg_read_1(sc, MPL115A_TADC_LSB));
216
217 #ifdef MPL115A_DEBUG
218 aprint_normal_dev(sc->sc_dev, "padc %x, tadc %x\n", padc, tadc);
219 #endif /* MPL115A_DEBUG */
220
221 rv = mpl115a_calc(sc, padc, tadc);
222
223 #ifdef MPL115A_DEBUG
224 aprint_normal_dev(sc->sc_dev, "%d Pa\n", rv);
225 #endif /* MPL115A_DEBUG */
226
227 return rv;
228 }
229
230 /* The following calculation routine was suggested by Matt Thomas. */
231 static uint32_t
mpl115a_calc(struct mpl115a_softc * sc,uint16_t padc,uint16_t tadc)232 mpl115a_calc(struct mpl115a_softc *sc, uint16_t padc, uint16_t tadc)
233 {
234 int32_t fp_a0, fp_b1, fp_b2, fp_c12, fp_padc, fp_tadc;
235 int32_t c12x2, a1, a1x2, y1, a2x2, pcomp;
236 uint32_t pre_kpa1, pre_kpa2, pa;
237
238 /* convert coefficients to common fixed point format */
239 fp_a0 = sc->sc_a0 << 13; /* 12.3 -> 15.16 */
240 fp_b1 = sc->sc_b1 << 3; /* 2.13 -> 15.16 */
241 fp_b2 = sc->sc_b2 << 2; /* 1.14 -> 15.16 */
242 fp_c12 = sc->sc_c12 >> 2; /* 0.22 */
243
244 fp_padc = padc >> 6;
245 fp_tadc = tadc >> 6;
246
247 c12x2 = (fp_c12 * fp_tadc + 0x3f) >> 6;
248 a1 = fp_b1 + c12x2;
249 a1x2 = a1 * fp_padc;
250 y1 = fp_a0 + a1x2;
251 a2x2 = fp_b2 * fp_tadc;
252 pcomp = y1 + a2x2;
253
254 /* pre_kpa has 16 fractional digits so it's accurate to 1/65536 kPa */
255 pre_kpa1 = pcomp * (115 - 50);
256 pre_kpa2 = pre_kpa1 / 1023 + (50 << 16);
257
258 /* but the real accuracy of the sensor is around +/- 1 kPa... */
259 pa = ((pre_kpa2 + 32768) >> 16) * 1000;
260
261 return pa;
262 }
263
264 static void
mpl115a_envsys_register(struct mpl115a_softc * sc)265 mpl115a_envsys_register(struct mpl115a_softc *sc)
266 {
267 sc->sc_sme = sysmon_envsys_create();
268
269 strlcpy(sc->sc_sensor.desc, "Absolute pressure",
270 sizeof(sc->sc_sensor.desc));
271 /* sc->sc_sensor.units = ENVSYS_SPRESSURE; */
272 sc->sc_sensor.units = ENVSYS_INTEGER;
273 sc->sc_sensor.state = ENVSYS_SINVALID;
274
275 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
276 aprint_error_dev(sc->sc_dev,
277 "error attaching sensor\n");
278 return;
279 }
280
281 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
282 sc->sc_sme->sme_cookie = sc;
283 sc->sc_sme->sme_refresh = mpl115a_envsys_refresh;
284
285 if (sysmon_envsys_register(sc->sc_sme)) {
286 aprint_error_dev(sc->sc_dev, "unable to register in sysmon\n");
287 sysmon_envsys_destroy(sc->sc_sme);
288 }
289 }
290
291 static void
mpl115a_envsys_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)292 mpl115a_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
293 {
294 struct mpl115a_softc *sc = sme->sme_cookie;
295
296 mutex_enter(&sc->sc_lock);
297
298 edata->value_cur = mpl115a_pressure(sc);
299 edata->state = ENVSYS_SVALID;
300
301 mutex_exit(&sc->sc_lock);
302 }
303
304