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