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