hytp14.c revision 1.1.2.2 1 1.1.2.2 rmind /*-
2 1.1.2.2 rmind * Copyright (c) 2014 The NetBSD Foundation, Inc.
3 1.1.2.2 rmind * All rights reserved.
4 1.1.2.2 rmind *
5 1.1.2.2 rmind * This code is derived from software contributed to The NetBSD Foundation
6 1.1.2.2 rmind * by Frank Kardel.
7 1.1.2.2 rmind *
8 1.1.2.2 rmind * Redistribution and use in source and binary forms, with or without
9 1.1.2.2 rmind * modification, are permitted provided that the following conditions
10 1.1.2.2 rmind * are met:
11 1.1.2.2 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer.
13 1.1.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1.2.2 rmind * documentation and/or other materials provided with the distribution.
16 1.1.2.2 rmind *
17 1.1.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1.2.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1.2.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1.2.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1.2.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1.2.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1.2.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1.2.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1.2.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1.2.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1.2.2 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1.2.2 rmind */
29 1.1.2.2 rmind
30 1.1.2.2 rmind /*
31 1.1.2.2 rmind * IST-AG P14 calibrated Hygro-/Temperature sensor module
32 1.1.2.2 rmind * Devices: HYT-271, HYT-221 and HYT-939
33 1.1.2.2 rmind *
34 1.1.2.2 rmind * see:
35 1.1.2.2 rmind * http://www.ist-ag.com/eh/ist-ag/resource.nsf/imgref/Download_AHHYTM_E2.1.pdf/
36 1.1.2.2 rmind * $FILE/AHHYTM_E2.1.pdf
37 1.1.2.2 rmind */
38 1.1.2.2 rmind
39 1.1.2.2 rmind #include <sys/cdefs.h>
40 1.1.2.2 rmind __KERNEL_RCSID(0, "$NetBSD: hytp14.c,v 1.1.2.2 2014/05/18 17:45:37 rmind Exp $");
41 1.1.2.2 rmind
42 1.1.2.2 rmind #include <sys/param.h>
43 1.1.2.2 rmind #include <sys/systm.h>
44 1.1.2.2 rmind #include <sys/kernel.h>
45 1.1.2.2 rmind #include <sys/device.h>
46 1.1.2.2 rmind #include <sys/module.h>
47 1.1.2.2 rmind
48 1.1.2.2 rmind #include <dev/sysmon/sysmonvar.h>
49 1.1.2.2 rmind
50 1.1.2.2 rmind #include <dev/i2c/i2cvar.h>
51 1.1.2.2 rmind #include <dev/i2c/hytp14reg.h>
52 1.1.2.2 rmind #include <dev/i2c/hytp14var.h>
53 1.1.2.2 rmind
54 1.1.2.2 rmind static int hytp14_match(device_t, cfdata_t, void *);
55 1.1.2.2 rmind static void hytp14_attach(device_t, device_t, void *);
56 1.1.2.2 rmind static int hytp14_detach(device_t, int);
57 1.1.2.2 rmind static int hytp14_refresh_sensor(struct hytp14_sc *sc);
58 1.1.2.2 rmind static void hytp14_refresh(struct sysmon_envsys *, envsys_data_t *);
59 1.1.2.2 rmind static void hytp14_refresh_humidity(struct hytp14_sc *, envsys_data_t *);
60 1.1.2.2 rmind static void hytp14_refresh_temp(struct hytp14_sc *, envsys_data_t *);
61 1.1.2.2 rmind
62 1.1.2.2 rmind /* #define HYT_DEBUG 3 */
63 1.1.2.2 rmind #ifdef HYT_DEBUG
64 1.1.2.2 rmind volatile int hythygtemp_debug = HYT_DEBUG;
65 1.1.2.2 rmind
66 1.1.2.2 rmind #define DPRINTF(_L_, _X_) do { \
67 1.1.2.2 rmind if ((_L_) <= hythygtemp_debug) { \
68 1.1.2.2 rmind printf _X_; \
69 1.1.2.2 rmind } \
70 1.1.2.2 rmind } while (0)
71 1.1.2.2 rmind #else
72 1.1.2.2 rmind #define DPRINTF(_L_, _X_)
73 1.1.2.2 rmind #endif
74 1.1.2.2 rmind
75 1.1.2.2 rmind CFATTACH_DECL_NEW(hythygtemp, sizeof(struct hytp14_sc),
76 1.1.2.2 rmind hytp14_match, hytp14_attach, hytp14_detach, NULL);
77 1.1.2.2 rmind
78 1.1.2.2 rmind static struct hytp14_sensor hytp14_sensors[] = {
79 1.1.2.2 rmind {
80 1.1.2.2 rmind .desc = "humidity",
81 1.1.2.2 rmind .type = ENVSYS_SRELHUMIDITY,
82 1.1.2.2 rmind .refresh = hytp14_refresh_humidity
83 1.1.2.2 rmind },
84 1.1.2.2 rmind {
85 1.1.2.2 rmind .desc = "temperature",
86 1.1.2.2 rmind .type = ENVSYS_STEMP,
87 1.1.2.2 rmind .refresh = hytp14_refresh_temp
88 1.1.2.2 rmind }
89 1.1.2.2 rmind };
90 1.1.2.2 rmind
91 1.1.2.2 rmind static int
92 1.1.2.2 rmind hytp14_match(device_t parent, cfdata_t match, void *aux)
93 1.1.2.2 rmind {
94 1.1.2.2 rmind struct i2c_attach_args *ia = aux;
95 1.1.2.2 rmind
96 1.1.2.2 rmind if (ia->ia_name) {
97 1.1.2.2 rmind /* direct config - check name */
98 1.1.2.2 rmind if (strcmp(ia->ia_name, "hythygtemp") == 0)
99 1.1.2.2 rmind return 1;
100 1.1.2.2 rmind } else {
101 1.1.2.2 rmind /* indirect config - check typical address */
102 1.1.2.2 rmind if (ia->ia_addr == HYTP14_DEFAULT_ADDR)
103 1.1.2.2 rmind return 1;
104 1.1.2.2 rmind }
105 1.1.2.2 rmind return 0;
106 1.1.2.2 rmind }
107 1.1.2.2 rmind
108 1.1.2.2 rmind static void
109 1.1.2.2 rmind hytp14_attach(device_t parent, device_t self, void *aux)
110 1.1.2.2 rmind {
111 1.1.2.2 rmind struct hytp14_sc *sc = device_private(self);
112 1.1.2.2 rmind struct i2c_attach_args *ia = aux;
113 1.1.2.2 rmind int i;
114 1.1.2.2 rmind
115 1.1.2.2 rmind sc->sc_dev = self;
116 1.1.2.2 rmind sc->sc_tag = ia->ia_tag;
117 1.1.2.2 rmind sc->sc_addr = ia->ia_addr;
118 1.1.2.2 rmind sc->sc_refresh = 0;
119 1.1.2.2 rmind sc->sc_valid = ENVSYS_SINVALID;
120 1.1.2.2 rmind sc->sc_numsensors = __arraycount(hytp14_sensors);
121 1.1.2.2 rmind
122 1.1.2.2 rmind if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
123 1.1.2.2 rmind aprint_error_dev(sc->sc_dev,
124 1.1.2.2 rmind "unable to create sysmon structure\n");
125 1.1.2.2 rmind return;
126 1.1.2.2 rmind }
127 1.1.2.2 rmind
128 1.1.2.2 rmind for (i = 0; i < sc->sc_numsensors; i++) {
129 1.1.2.2 rmind strlcpy(sc->sc_sensors[i].desc,
130 1.1.2.2 rmind hytp14_sensors[i].desc,
131 1.1.2.2 rmind sizeof sc->sc_sensors[i].desc);
132 1.1.2.2 rmind
133 1.1.2.2 rmind sc->sc_sensors[i].units = hytp14_sensors[i].type;
134 1.1.2.2 rmind sc->sc_sensors[i].state = ENVSYS_SINVALID;
135 1.1.2.2 rmind
136 1.1.2.2 rmind DPRINTF(2, ("hytp14_attach: registering sensor %d (%s)\n", i,
137 1.1.2.2 rmind sc->sc_sensors[i].desc));
138 1.1.2.2 rmind
139 1.1.2.2 rmind if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[i])) {
140 1.1.2.2 rmind aprint_error_dev(sc->sc_dev,
141 1.1.2.2 rmind "unable to attach sensor\n");
142 1.1.2.2 rmind sysmon_envsys_destroy(sc->sc_sme);
143 1.1.2.2 rmind return;
144 1.1.2.2 rmind }
145 1.1.2.2 rmind }
146 1.1.2.2 rmind
147 1.1.2.2 rmind sc->sc_sme->sme_name = device_xname(sc->sc_dev);
148 1.1.2.2 rmind sc->sc_sme->sme_cookie = sc;
149 1.1.2.2 rmind sc->sc_sme->sme_refresh = hytp14_refresh;
150 1.1.2.2 rmind
151 1.1.2.2 rmind DPRINTF(2, ("hytp14_attach: registering with envsys\n"));
152 1.1.2.2 rmind
153 1.1.2.2 rmind if (sysmon_envsys_register(sc->sc_sme)) {
154 1.1.2.2 rmind aprint_error_dev(sc->sc_dev,
155 1.1.2.2 rmind "unable to register with sysmon\n");
156 1.1.2.2 rmind sysmon_envsys_destroy(sc->sc_sme);
157 1.1.2.2 rmind return;
158 1.1.2.2 rmind }
159 1.1.2.2 rmind
160 1.1.2.2 rmind aprint_normal(": HYT-221/271/939 humidity and temperature sensor\n");
161 1.1.2.2 rmind }
162 1.1.2.2 rmind
163 1.1.2.2 rmind static int hytp14_detach(device_t self, int flags)
164 1.1.2.2 rmind {
165 1.1.2.2 rmind struct hytp14_sc *sc = device_private(self);
166 1.1.2.2 rmind
167 1.1.2.2 rmind if (sc->sc_sme != NULL) {
168 1.1.2.2 rmind sysmon_envsys_unregister(sc->sc_sme);
169 1.1.2.2 rmind sc->sc_sme = NULL;
170 1.1.2.2 rmind }
171 1.1.2.2 rmind
172 1.1.2.2 rmind return 0;
173 1.1.2.2 rmind }
174 1.1.2.2 rmind
175 1.1.2.2 rmind static int
176 1.1.2.2 rmind hytp14_refresh_sensor(struct hytp14_sc *sc)
177 1.1.2.2 rmind {
178 1.1.2.2 rmind int error = 0;
179 1.1.2.2 rmind uint8_t buf[I2C_EXEC_MAX_BUFLEN];
180 1.1.2.2 rmind
181 1.1.2.2 rmind /* no more than once per second */
182 1.1.2.2 rmind if (hardclock_ticks - sc->sc_refresh < hz)
183 1.1.2.2 rmind return sc->sc_valid;
184 1.1.2.2 rmind
185 1.1.2.2 rmind DPRINTF(2, ("hytp14_refresh_sensor(%s)\n", device_xname(sc->sc_dev)));
186 1.1.2.2 rmind
187 1.1.2.2 rmind if ((error = iic_acquire_bus(sc->sc_tag, 0)) == 0) {
188 1.1.2.2 rmind DPRINTF(3, ("hytp14_refresh_sensor(%s): bus locked\n", device_xname(sc->sc_dev)));
189 1.1.2.2 rmind
190 1.1.2.2 rmind /* send MR command */
191 1.1.2.2 rmind /* avoid quick read/write by providing a result buffer */
192 1.1.2.2 rmind if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
193 1.1.2.2 rmind sc->sc_addr, NULL, 0, buf, sizeof buf, 0)) == 0) {
194 1.1.2.2 rmind DPRINTF(3, ("hytp14_refresh_sensor(%s): MR sent\n",
195 1.1.2.2 rmind device_xname(sc->sc_dev)));
196 1.1.2.2 rmind
197 1.1.2.2 rmind /* send DF command - read data from sensor */
198 1.1.2.2 rmind if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
199 1.1.2.2 rmind sc->sc_addr, NULL, 0, sc->sc_data,
200 1.1.2.2 rmind sizeof sc->sc_data, 0)) != 0) {
201 1.1.2.2 rmind DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n",
202 1.1.2.2 rmind device_xname(sc->sc_dev),
203 1.1.2.2 rmind __func__, sc->sc_addr, error));
204 1.1.2.2 rmind } else {
205 1.1.2.2 rmind DPRINTF(2, ("hytp14_refresh_sensor(%s): DF success : 0x%02x%02x%02x%02x\n",
206 1.1.2.2 rmind device_xname(sc->sc_dev),
207 1.1.2.2 rmind sc->sc_data[0],
208 1.1.2.2 rmind sc->sc_data[1],
209 1.1.2.2 rmind sc->sc_data[2],
210 1.1.2.2 rmind sc->sc_data[3]));
211 1.1.2.2 rmind }
212 1.1.2.2 rmind } else {
213 1.1.2.2 rmind DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n",
214 1.1.2.2 rmind device_xname(sc->sc_dev), __func__,
215 1.1.2.2 rmind sc->sc_addr, error));
216 1.1.2.2 rmind }
217 1.1.2.2 rmind
218 1.1.2.2 rmind iic_release_bus(sc->sc_tag, 0);
219 1.1.2.2 rmind DPRINTF(3, ("hytp14_refresh_sensor(%s): bus released\n", device_xname(sc->sc_dev)));
220 1.1.2.2 rmind } else {
221 1.1.2.2 rmind DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n",
222 1.1.2.2 rmind device_xname(sc->sc_dev), __func__, sc->sc_addr, error));
223 1.1.2.2 rmind }
224 1.1.2.2 rmind
225 1.1.2.2 rmind sc->sc_refresh = hardclock_ticks;
226 1.1.2.2 rmind
227 1.1.2.2 rmind if (error == 0) {
228 1.1.2.2 rmind sc->sc_valid = ENVSYS_SVALID;
229 1.1.2.2 rmind } else {
230 1.1.2.2 rmind sc->sc_valid = ENVSYS_SINVALID;
231 1.1.2.2 rmind }
232 1.1.2.2 rmind
233 1.1.2.2 rmind return sc->sc_valid;
234 1.1.2.2 rmind }
235 1.1.2.2 rmind
236 1.1.2.2 rmind
237 1.1.2.2 rmind static void
238 1.1.2.2 rmind hytp14_refresh_humidity(struct hytp14_sc *sc, envsys_data_t *edata)
239 1.1.2.2 rmind {
240 1.1.2.2 rmind uint16_t hyg;
241 1.1.2.2 rmind int status;
242 1.1.2.2 rmind
243 1.1.2.2 rmind status = hytp14_refresh_sensor(sc);
244 1.1.2.2 rmind
245 1.1.2.2 rmind if (status == ENVSYS_SVALID) {
246 1.1.2.2 rmind hyg = (sc->sc_data[0] << 8) | sc->sc_data[1];
247 1.1.2.2 rmind
248 1.1.2.2 rmind edata->value_cur = (1000000000 / HYTP14_HYG_SCALE) * (int32_t)HYTP14_HYG_RAWVAL(hyg);
249 1.1.2.2 rmind edata->value_cur /= 10;
250 1.1.2.2 rmind }
251 1.1.2.2 rmind
252 1.1.2.2 rmind edata->state = status;
253 1.1.2.2 rmind }
254 1.1.2.2 rmind
255 1.1.2.2 rmind static void
256 1.1.2.2 rmind hytp14_refresh_temp(struct hytp14_sc *sc, envsys_data_t *edata)
257 1.1.2.2 rmind {
258 1.1.2.2 rmind uint16_t temp;
259 1.1.2.2 rmind int status;
260 1.1.2.2 rmind
261 1.1.2.2 rmind status = hytp14_refresh_sensor(sc);
262 1.1.2.2 rmind
263 1.1.2.2 rmind if (status == ENVSYS_SVALID) {
264 1.1.2.2 rmind temp = HYTP14_TEMP_RAWVAL((sc->sc_data[2] << 8) | sc->sc_data[3]);
265 1.1.2.2 rmind
266 1.1.2.2 rmind edata->value_cur = (HYTP14_TEMP_FACTOR * 1000000) / HYTP14_TEMP_SCALE;
267 1.1.2.2 rmind edata->value_cur *= (int32_t)temp;
268 1.1.2.2 rmind edata->value_cur += HYTP14_TEMP_OFFSET * 1000000 + 273150000;
269 1.1.2.2 rmind }
270 1.1.2.2 rmind
271 1.1.2.2 rmind edata->state = status;
272 1.1.2.2 rmind }
273 1.1.2.2 rmind
274 1.1.2.2 rmind static void
275 1.1.2.2 rmind hytp14_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
276 1.1.2.2 rmind {
277 1.1.2.2 rmind struct hytp14_sc *sc = sme->sme_cookie;
278 1.1.2.2 rmind
279 1.1.2.2 rmind hytp14_sensors[edata->sensor].refresh(sc, edata);
280 1.1.2.2 rmind }
281 1.1.2.2 rmind
282 1.1.2.2 rmind
283 1.1.2.2 rmind MODULE(MODULE_CLASS_DRIVER, hythygtemp, "iic");
284 1.1.2.2 rmind
285 1.1.2.2 rmind #ifdef _MODULE
286 1.1.2.2 rmind #include "ioconf.c"
287 1.1.2.2 rmind #endif
288 1.1.2.2 rmind
289 1.1.2.2 rmind static int
290 1.1.2.2 rmind hythygtemp_modcmd(modcmd_t cmd, void *opaque)
291 1.1.2.2 rmind {
292 1.1.2.2 rmind int error = 0;
293 1.1.2.2 rmind
294 1.1.2.2 rmind switch (cmd) {
295 1.1.2.2 rmind case MODULE_CMD_INIT:
296 1.1.2.2 rmind #ifdef _MODULE
297 1.1.2.2 rmind error = config_init_component(cfdriver_ioconf_hythygtemp,
298 1.1.2.2 rmind cfattach_ioconf_hythygtemp, cfdata_ioconf_hythygtemp);
299 1.1.2.2 rmind #endif
300 1.1.2.2 rmind return error;
301 1.1.2.2 rmind
302 1.1.2.2 rmind case MODULE_CMD_FINI:
303 1.1.2.2 rmind #ifdef _MODULE
304 1.1.2.2 rmind error = config_fini_component(cfdriver_ioconf_hythygtemp,
305 1.1.2.2 rmind cfattach_ioconf_hythygtemp, cfdata_ioconf_hythygtemp);
306 1.1.2.2 rmind #endif
307 1.1.2.2 rmind return error;
308 1.1.2.2 rmind
309 1.1.2.2 rmind default:
310 1.1.2.2 rmind return ENOTTY;
311 1.1.2.2 rmind }
312 1.1.2.2 rmind }
313