Tue Oct 8 21:16:11 2019 UTC ()
enable FDT direct configuration.
remove address test as the i2c address can be changed on this device.


(kardel)
diff -r1.8 -r1.9 src/sys/dev/i2c/hytp14.c

cvs diff -r1.8 -r1.9 src/sys/dev/i2c/hytp14.c (switch to unified diff)

--- src/sys/dev/i2c/hytp14.c 2018/06/16 21:24:36 1.8
+++ src/sys/dev/i2c/hytp14.c 2019/10/08 21:16:11 1.9
@@ -1,496 +1,521 @@ @@ -1,496 +1,521 @@
1/*- 1/*-
2 * Copyright (c) 2014,2016 The NetBSD Foundation, Inc. 2 * Copyright (c) 2014,2016 The NetBSD Foundation, Inc.
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * This code is derived from software contributed to The NetBSD Foundation 5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Frank Kardel. 6 * by Frank Kardel.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE. 27 * POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30/* 30/*
31 * IST-AG P14 calibrated Hygro-/Temperature sensor module 31 * IST-AG P14 calibrated Hygro-/Temperature sensor module
32 * Devices: HYT-271, HYT-221 and HYT-939  32 * Devices: HYT-271, HYT-221 and HYT-939
33 * 33 *
34 * see: 34 * see:
35 * http://www.ist-ag.com/eh/ist-ag/resource.nsf/imgref/Download_AHHYTM_E2.1.pdf/ 35 * http://www.ist-ag.com/eh/ist-ag/resource.nsf/imgref/Download_AHHYTM_E2.1.pdf/
36 * $FILE/AHHYTM_E2.1.pdf 36 * $FILE/AHHYTM_E2.1.pdf
37 */  37 */
38 38
 39/*
 40 * FDT direct configuration fragment to be added to i2cX definition in dtsi file
 41 * like in bcm2835-rpi.dtsi or another specific file.
 42 *
 43 * &i2c1 {
 44 * pinctrl-names = "default";
 45 * pinctrl-0 = <&i2c1_gpio2>;
 46 * status = "okay";
 47 * clock-frequency = <100000>;
 48 * #address-cells = <1>;
 49 * #size-cells = <0>;
 50 * hythygtemp@28 {
 51 * compatible = "ist-ag,i2c-hytp14";
 52 * reg = <0x28>;
 53 * status = "okay";
 54 * };
 55 * hythygtemp@29 {
 56 * compatible = "ist-ag,i2c-hytp14";
 57 * reg = <0x29>;
 58 * status = "okay";
 59 * };
 60 * };
 61 */
 62
39#include <sys/cdefs.h> 63#include <sys/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: hytp14.c,v 1.8 2018/06/16 21:24:36 thorpej Exp $"); 64__KERNEL_RCSID(0, "$NetBSD: hytp14.c,v 1.9 2019/10/08 21:16:11 kardel Exp $");
41 65
42#include <sys/param.h> 66#include <sys/param.h>
43#include <sys/systm.h> 67#include <sys/systm.h>
44#include <sys/kernel.h> 68#include <sys/kernel.h>
45#include <sys/device.h> 69#include <sys/device.h>
46#include <sys/module.h> 70#include <sys/module.h>
47#include <sys/sysctl.h> 71#include <sys/sysctl.h>
48#include <sys/mutex.h> 72#include <sys/mutex.h>
49#include <sys/condvar.h> 73#include <sys/condvar.h>
50#include <sys/kthread.h> 74#include <sys/kthread.h>
51 75
52#include <dev/sysmon/sysmonvar.h> 76#include <dev/sysmon/sysmonvar.h>
53#include <dev/i2c/i2cvar.h> 77#include <dev/i2c/i2cvar.h>
54#include <dev/i2c/hytp14reg.h> 78#include <dev/i2c/hytp14reg.h>
55#include <dev/i2c/hytp14var.h> 79#include <dev/i2c/hytp14var.h>
56 80
57static int hytp14_match(device_t, cfdata_t, void *); 81static int hytp14_match(device_t, cfdata_t, void *);
58static void hytp14_attach(device_t, device_t, void *); 82static void hytp14_attach(device_t, device_t, void *);
59static int hytp14_detach(device_t, int); 83static int hytp14_detach(device_t, int);
60static void hytp14_measurement_request(void *); 84static void hytp14_measurement_request(void *);
61static int hytp14_refresh_sensor(struct hytp14_sc *sc); 85static int hytp14_refresh_sensor(struct hytp14_sc *sc);
62static void hytp14_refresh(struct sysmon_envsys *, envsys_data_t *); 86static void hytp14_refresh(struct sysmon_envsys *, envsys_data_t *);
63static void hytp14_refresh_humidity(struct hytp14_sc *, envsys_data_t *); 87static void hytp14_refresh_humidity(struct hytp14_sc *, envsys_data_t *);
64static void hytp14_refresh_temp(struct hytp14_sc *, envsys_data_t *); 88static void hytp14_refresh_temp(struct hytp14_sc *, envsys_data_t *);
65static void hytp14_thread(void *); 89static void hytp14_thread(void *);
66static int sysctl_hytp14_interval(SYSCTLFN_ARGS); 90static int sysctl_hytp14_interval(SYSCTLFN_ARGS);
67 91
68/* #define HYT_DEBUG 3 */ 92/* #define HYT_DEBUG 3 */
69 93
70#ifdef HYT_DEBUG 94#ifdef HYT_DEBUG
71volatile int hythygtemp_debug = HYT_DEBUG; 95volatile int hythygtemp_debug = HYT_DEBUG;
72 96
73#define DPRINTF(_L_, _X_) do { \ 97#define DPRINTF(_L_, _X_) do { \
74 if ((_L_) <= hythygtemp_debug) { \ 98 if ((_L_) <= hythygtemp_debug) { \
75 printf _X_; \ 99 printf _X_; \
76 } \ 100 } \
77 } while (0) 101 } while (0)
78#else 102#else
79#define DPRINTF(_L_, _X_) 103#define DPRINTF(_L_, _X_)
80#endif 104#endif
81 105
82CFATTACH_DECL_NEW(hythygtemp, sizeof(struct hytp14_sc), 106CFATTACH_DECL_NEW(hythygtemp, sizeof(struct hytp14_sc),
83 hytp14_match, hytp14_attach, hytp14_detach, NULL); 107 hytp14_match, hytp14_attach, hytp14_detach, NULL);
84 108
85static struct hytp14_sensor hytp14_sensors[] = { 109static struct hytp14_sensor hytp14_sensors[] = {
86 { 110 {
87 .desc = "humidity", 111 .desc = "humidity",
88 .type = ENVSYS_SRELHUMIDITY, 112 .type = ENVSYS_SRELHUMIDITY,
89 .refresh = hytp14_refresh_humidity 113 .refresh = hytp14_refresh_humidity
90 }, 114 },
91 { 115 {
92 .desc = "temperature", 116 .desc = "temperature",
93 .type = ENVSYS_STEMP, 117 .type = ENVSYS_STEMP,
94 .refresh = hytp14_refresh_temp 118 .refresh = hytp14_refresh_temp
95 } 119 }
96}; 120};
97 121
 122static const struct device_compatible_entry compat_data[] = {
 123 { "i2c-hytp14", 0 },
 124 { NULL, 0 }
 125};
 126
98static int 127static int
99hytp14_match(device_t parent, cfdata_t match, void *aux) 128hytp14_match(device_t parent, cfdata_t match, void *aux)
100{ 129{
101 struct i2c_attach_args *ia = aux; 130 struct i2c_attach_args *ia = aux;
102 int match_result; 131 int match_result;
103 132
104 if (iic_use_direct_match(ia, match, NULL, &match_result)) 133 if (iic_use_direct_match(ia, match, compat_data, &match_result))
105 return match_result; 134 return match_result;
106 135
107 if (ia->ia_addr == 0x28) 
108 return I2C_MATCH_ADDRESS_ONLY; 
109  
110 /* 136 /*
111 * XXXJRT 137 * This device can be reprogrammed to use a different
112 * This device is an odd-ball; the i2c address can be changed 138 * I2C address, thus checking for specific addresses
113 * at run-time using a command sequence documented in the 139 * is not helpful here.
114 * application note, but the timing is critical (within 10ms 140 * reprogramming is done via setting new values in
115 * after power-on of the device), and the device always starts 141 * the device EEPROM via the hytctl utility and
116 * up at address 0x28. 142 * a special GPIO setup - see hythygtemp(4) for more
117 * 143 * information.
118 * How should we handle this? 
119 */ 144 */
120 return 0; 145 return I2C_MATCH_ADDRESS_ONLY;
121} 146}
122 147
123static void 148static void
124hytp14_attach(device_t parent, device_t self, void *aux) 149hytp14_attach(device_t parent, device_t self, void *aux)
125{ 150{
126 const struct sysctlnode *rnode, *node; 151 const struct sysctlnode *rnode, *node;
127 struct hytp14_sc *sc; 152 struct hytp14_sc *sc;
128 struct i2c_attach_args *ia; 153 struct i2c_attach_args *ia;
129 int i, rv; 154 int i, rv;
130 155
131 ia = aux; 156 ia = aux;
132 sc = device_private(self); 157 sc = device_private(self);
133 158
134 sc->sc_dev = self; 159 sc->sc_dev = self;
135 sc->sc_tag = ia->ia_tag; 160 sc->sc_tag = ia->ia_tag;
136 sc->sc_addr = ia->ia_addr; 161 sc->sc_addr = ia->ia_addr;
137 162
138 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE); 163 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
139 cv_init(&sc->sc_condvar, "hytcv"); 164 cv_init(&sc->sc_condvar, "hytcv");
140 165
141 sc->sc_state = HYTP14_THR_INIT; 166 sc->sc_state = HYTP14_THR_INIT;
142  167
143 sc->sc_valid = ENVSYS_SINVALID; 168 sc->sc_valid = ENVSYS_SINVALID;
144 sc->sc_numsensors = __arraycount(hytp14_sensors); 169 sc->sc_numsensors = __arraycount(hytp14_sensors);
145 170
146 if ((sc->sc_sme = sysmon_envsys_create()) == NULL) { 171 if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
147 aprint_error_dev(sc->sc_dev, 172 aprint_error_dev(sc->sc_dev,
148 "unable to create sysmon structure\n"); 173 "unable to create sysmon structure\n");
149 return; 174 return;
150 } 175 }
151  176
152 for (i = 0; i < sc->sc_numsensors; i++) { 177 for (i = 0; i < sc->sc_numsensors; i++) {
153 strlcpy(sc->sc_sensors[i].desc, 178 strlcpy(sc->sc_sensors[i].desc,
154 hytp14_sensors[i].desc, 179 hytp14_sensors[i].desc,
155 sizeof sc->sc_sensors[i].desc); 180 sizeof sc->sc_sensors[i].desc);
156  181
157 sc->sc_sensors[i].units = hytp14_sensors[i].type; 182 sc->sc_sensors[i].units = hytp14_sensors[i].type;
158 sc->sc_sensors[i].state = ENVSYS_SINVALID; 183 sc->sc_sensors[i].state = ENVSYS_SINVALID;
159  184
160 DPRINTF(2, ("hytp14_attach: registering sensor %d (%s)\n", i, 185 DPRINTF(2, ("hytp14_attach: registering sensor %d (%s)\n", i,
161 sc->sc_sensors[i].desc)); 186 sc->sc_sensors[i].desc));
162  187
163 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[i])) { 188 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[i])) {
164 aprint_error_dev(sc->sc_dev, 189 aprint_error_dev(sc->sc_dev,
165 "unable to attach sensor\n"); 190 "unable to attach sensor\n");
166 sysmon_envsys_destroy(sc->sc_sme); 191 sysmon_envsys_destroy(sc->sc_sme);
167 return; 192 return;
168 } 193 }
169 } 194 }
170 195
171 sc->sc_sme->sme_name = device_xname(sc->sc_dev); 196 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
172 sc->sc_sme->sme_cookie = sc; 197 sc->sc_sme->sme_cookie = sc;
173 sc->sc_sme->sme_refresh = hytp14_refresh; 198 sc->sc_sme->sme_refresh = hytp14_refresh;
174 199
175 DPRINTF(2, ("hytp14_attach: registering with envsys\n")); 200 DPRINTF(2, ("hytp14_attach: registering with envsys\n"));
176 201
177 if (sysmon_envsys_register(sc->sc_sme)) { 202 if (sysmon_envsys_register(sc->sc_sme)) {
178 aprint_error_dev(sc->sc_dev, 203 aprint_error_dev(sc->sc_dev,
179 "unable to register with sysmon\n"); 204 "unable to register with sysmon\n");
180 sysmon_envsys_destroy(sc->sc_sme); 205 sysmon_envsys_destroy(sc->sc_sme);
181 return; 206 return;
182 } 207 }
183 208
184 /* create a sysctl node for setting the measurement interval */ 209 /* create a sysctl node for setting the measurement interval */
185 rnode = node = NULL; 210 rnode = node = NULL;
186 sysctl_createv(NULL, 0, NULL, &rnode, 211 sysctl_createv(NULL, 0, NULL, &rnode,
187 CTLFLAG_READWRITE, 212 CTLFLAG_READWRITE,
188 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, 213 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
189 NULL, 0, NULL, 0, 214 NULL, 0, NULL, 0,
190 CTL_HW, CTL_CREATE, CTL_EOL); 215 CTL_HW, CTL_CREATE, CTL_EOL);
191 216
192 if (rnode != NULL) 217 if (rnode != NULL)
193 sysctl_createv(NULL, 0, NULL, &node, 218 sysctl_createv(NULL, 0, NULL, &node,
194 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 219 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
195 CTLTYPE_INT, "interval", 220 CTLTYPE_INT, "interval",
196 SYSCTL_DESCR("Sensor sampling interval in seconds"), 221 SYSCTL_DESCR("Sensor sampling interval in seconds"),
197 sysctl_hytp14_interval, 0, (void *)sc, 0, 222 sysctl_hytp14_interval, 0, (void *)sc, 0,
198 CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL); 223 CTL_HW, rnode->sysctl_num, CTL_CREATE, CTL_EOL);
199 224
200 225
201 /* set up the default measurement interval for worker thread */ 226 /* set up the default measurement interval for worker thread */
202 sc->sc_mrinterval = HYTP14_MR_INTERVAL; 227 sc->sc_mrinterval = HYTP14_MR_INTERVAL;
203 228
204 /* create worker kthread */ 229 /* create worker kthread */
205 rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 230 rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
206 hytp14_thread, sc, &sc->sc_thread, 231 hytp14_thread, sc, &sc->sc_thread,
207 "%s", device_xname(sc->sc_dev)); 232 "%s", device_xname(sc->sc_dev));
208 if (rv) 233 if (rv)
209 aprint_error_dev(self, "unable to create intr thread\n"); 234 aprint_error_dev(self, "unable to create intr thread\n");
210 235
211 aprint_normal(": HYT-221/271/939 humidity and temperature sensor\n"); 236 aprint_normal(": HYT-221/271/939 humidity and temperature sensor\n");
212} 237}
213 238
214static int 239static int
215hytp14_detach(device_t self, int flags) 240hytp14_detach(device_t self, int flags)
216{ 241{
217 struct hytp14_sc *sc; 242 struct hytp14_sc *sc;
218 243
219 sc = device_private(self); 244 sc = device_private(self);
220 245
221 if (sc->sc_sme != NULL) { 246 if (sc->sc_sme != NULL) {
222 sysmon_envsys_unregister(sc->sc_sme); 247 sysmon_envsys_unregister(sc->sc_sme);
223 sc->sc_sme = NULL; 248 sc->sc_sme = NULL;
224 } 249 }
225 250
226 /* stop measurement thread */ 251 /* stop measurement thread */
227 mutex_enter(&sc->sc_mutex); 252 mutex_enter(&sc->sc_mutex);
228 sc->sc_state = HYTP14_THR_STOP; 253 sc->sc_state = HYTP14_THR_STOP;
229 cv_signal(&sc->sc_condvar); 254 cv_signal(&sc->sc_condvar);
230 mutex_exit(&sc->sc_mutex); 255 mutex_exit(&sc->sc_mutex);
231 256
232 /* await thread completion */ 257 /* await thread completion */
233 kthread_join(sc->sc_thread); 258 kthread_join(sc->sc_thread);
234 259
235 /* cleanup */ 260 /* cleanup */
236 cv_destroy(&sc->sc_condvar); 261 cv_destroy(&sc->sc_condvar);
237 mutex_destroy(&sc->sc_mutex); 262 mutex_destroy(&sc->sc_mutex);
238  263
239 return 0; 264 return 0;
240} 265}
241 266
242static void 267static void
243hytp14_thread(void *aux) 268hytp14_thread(void *aux)
244{ 269{
245 struct hytp14_sc *sc = aux; 270 struct hytp14_sc *sc = aux;
246 int rv; 271 int rv;
247  272
248 mutex_enter(&sc->sc_mutex); 273 mutex_enter(&sc->sc_mutex);
249 274
250 DPRINTF(2, ("%s(%s): thread start - state=%d\n", 275 DPRINTF(2, ("%s(%s): thread start - state=%d\n",
251 __func__, device_xname(sc->sc_dev), 276 __func__, device_xname(sc->sc_dev),
252 sc->sc_state)); 277 sc->sc_state));
253  278
254 while (sc->sc_state != HYTP14_THR_STOP) { 279 while (sc->sc_state != HYTP14_THR_STOP) {
255 sc->sc_state = HYTP14_THR_RUN; 280 sc->sc_state = HYTP14_THR_RUN;
256 281
257 DPRINTF(2, ("%s(%s): waiting %d seconds\n", 282 DPRINTF(2, ("%s(%s): waiting %d seconds\n",
258 __func__, device_xname(sc->sc_dev), 283 __func__, device_xname(sc->sc_dev),
259 sc->sc_mrinterval)); 284 sc->sc_mrinterval));
260  285
261 rv = cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz * sc->sc_mrinterval); 286 rv = cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz * sc->sc_mrinterval);
262 287
263 if (rv == EWOULDBLOCK) { 288 if (rv == EWOULDBLOCK) {
264 /* timeout - run measurement */ 289 /* timeout - run measurement */
265 DPRINTF(2, ("%s(%s): timeout -> measurement\n", 290 DPRINTF(2, ("%s(%s): timeout -> measurement\n",
266 __func__, device_xname(sc->sc_dev))); 291 __func__, device_xname(sc->sc_dev)));
267 292
268 hytp14_measurement_request(sc); 293 hytp14_measurement_request(sc);
269 } else { 294 } else {
270 DPRINTF(2, ("%s(%s): condvar signalled - state=%d\n", 295 DPRINTF(2, ("%s(%s): condvar signalled - state=%d\n",
271 __func__, device_xname(sc->sc_dev), 296 __func__, device_xname(sc->sc_dev),
272 sc->sc_state)); 297 sc->sc_state));
273 } 298 }
274 } 299 }
275  300
276 mutex_exit(&sc->sc_mutex); 301 mutex_exit(&sc->sc_mutex);
277 302
278 DPRINTF(2, ("%s(%s): thread exit\n", 303 DPRINTF(2, ("%s(%s): thread exit\n",
279 __func__, device_xname(sc->sc_dev))); 304 __func__, device_xname(sc->sc_dev)));
280 305
281 kthread_exit(0); 306 kthread_exit(0);
282} 307}
283 308
284static void 309static void
285hytp14_measurement_request(void *aux) 310hytp14_measurement_request(void *aux)
286{ 311{
287 uint8_t buf[I2C_EXEC_MAX_BUFLEN]; 312 uint8_t buf[I2C_EXEC_MAX_BUFLEN];
288 struct hytp14_sc *sc; 313 struct hytp14_sc *sc;
289 int error; 314 int error;
290 315
291 sc = aux; 316 sc = aux;
292 DPRINTF(2, ("%s(%s)\n", __func__, device_xname(sc->sc_dev))); 317 DPRINTF(2, ("%s(%s)\n", __func__, device_xname(sc->sc_dev)));
293 318
294 error = iic_acquire_bus(sc->sc_tag, 0); 319 error = iic_acquire_bus(sc->sc_tag, 0);
295 if (error == 0) { 320 if (error == 0) {
296 321
297 /* send DF command - read last data from sensor */ 322 /* send DF command - read last data from sensor */
298 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 323 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
299 sc->sc_addr, NULL, 0, sc->sc_data, sizeof(sc->sc_data), 0); 324 sc->sc_addr, NULL, 0, sc->sc_data, sizeof(sc->sc_data), 0);
300 if (error != 0) { 325 if (error != 0) {
301 DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n", 326 DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n",
302 device_xname(sc->sc_dev), __func__, 327 device_xname(sc->sc_dev), __func__,
303 sc->sc_addr, error)); 328 sc->sc_addr, error));
304 sc->sc_valid = ENVSYS_SINVALID; 329 sc->sc_valid = ENVSYS_SINVALID;
305 } else { 330 } else {
306 DPRINTF(3, ("%s(%s): DF success : " 331 DPRINTF(3, ("%s(%s): DF success : "
307 "0x%02x%02x%02x%02x\n", 332 "0x%02x%02x%02x%02x\n",
308 __func__, device_xname(sc->sc_dev), 333 __func__, device_xname(sc->sc_dev),
309 sc->sc_data[0], sc->sc_data[1], 334 sc->sc_data[0], sc->sc_data[1],
310 sc->sc_data[2], sc->sc_data[3])); 335 sc->sc_data[2], sc->sc_data[3]));
311 336
312 /* remember last data, when valid */ 337 /* remember last data, when valid */
313 if (!(sc->sc_data[0] & 338 if (!(sc->sc_data[0] &
314 (HYTP14_RESP_CMDMODE | HYTP14_RESP_STALE))) { 339 (HYTP14_RESP_CMDMODE | HYTP14_RESP_STALE))) {
315 memcpy(sc->sc_last, sc->sc_data, 340 memcpy(sc->sc_last, sc->sc_data,
316 sizeof(sc->sc_last)); 341 sizeof(sc->sc_last));
317 sc->sc_valid = ENVSYS_SVALID; 342 sc->sc_valid = ENVSYS_SVALID;
318 } 343 }
319 } 344 }
320 345
321 /* send MR command to request a new measurement */ 346 /* send MR command to request a new measurement */
322 error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, 347 error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
323 sc->sc_addr, NULL, 0, buf, sizeof(buf), 0); 348 sc->sc_addr, NULL, 0, buf, sizeof(buf), 0);
324 349
325 if (error == 0) { 350 if (error == 0) {
326 DPRINTF(3, ("%s(%s): MR sent\n", 351 DPRINTF(3, ("%s(%s): MR sent\n",
327 __func__, device_xname(sc->sc_dev))); 352 __func__, device_xname(sc->sc_dev)));
328 } else { 353 } else {
329 DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n", 354 DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n",
330 device_xname(sc->sc_dev), __func__, 355 device_xname(sc->sc_dev), __func__,
331 sc->sc_addr, error)); 356 sc->sc_addr, error));
332 } 357 }
333 358
334 iic_release_bus(sc->sc_tag, 0);  359 iic_release_bus(sc->sc_tag, 0);
335 DPRINTF(3, ("%s(%s): bus released\n", 360 DPRINTF(3, ("%s(%s): bus released\n",
336 __func__, device_xname(sc->sc_dev))); 361 __func__, device_xname(sc->sc_dev)));
337 } else { 362 } else {
338 DPRINTF(2, ("%s: %s: failed acquire i2c bus - error %d\n", 363 DPRINTF(2, ("%s: %s: failed acquire i2c bus - error %d\n",
339 device_xname(sc->sc_dev), __func__, error)); 364 device_xname(sc->sc_dev), __func__, error));
340 } 365 }
341} 366}
342 367
343static int 368static int
344hytp14_refresh_sensor(struct hytp14_sc *sc) 369hytp14_refresh_sensor(struct hytp14_sc *sc)
345{ 370{
346 int error; 371 int error;
347 372
348 DPRINTF(2, ("%s(%s)\n", __func__, device_xname(sc->sc_dev))); 373 DPRINTF(2, ("%s(%s)\n", __func__, device_xname(sc->sc_dev)));
349 374
350 error = iic_acquire_bus(sc->sc_tag, 0); 375 error = iic_acquire_bus(sc->sc_tag, 0);
351 if (error == 0) { 376 if (error == 0) {
352 377
353 /* send DF command - read last data from sensor */ 378 /* send DF command - read last data from sensor */
354 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 379 error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
355 sc->sc_addr, NULL, 0, sc->sc_data, sizeof(sc->sc_data), 0); 380 sc->sc_addr, NULL, 0, sc->sc_data, sizeof(sc->sc_data), 0);
356 if (error != 0) { 381 if (error != 0) {
357 DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n", 382 DPRINTF(2, ("%s: %s: failed read from 0x%02x - error %d\n",
358 device_xname(sc->sc_dev), __func__, 383 device_xname(sc->sc_dev), __func__,
359 sc->sc_addr, error)); 384 sc->sc_addr, error));
360 sc->sc_valid = ENVSYS_SINVALID; 385 sc->sc_valid = ENVSYS_SINVALID;
361 } else { 386 } else {
362 DPRINTF(3, ("%s(%s): DF success : " 387 DPRINTF(3, ("%s(%s): DF success : "
363 "0x%02x%02x%02x%02x\n", 388 "0x%02x%02x%02x%02x\n",
364 __func__, device_xname(sc->sc_dev), 389 __func__, device_xname(sc->sc_dev),
365 sc->sc_data[0], sc->sc_data[1], 390 sc->sc_data[0], sc->sc_data[1],
366 sc->sc_data[2], sc->sc_data[3])); 391 sc->sc_data[2], sc->sc_data[3]));
367 392
368 /* 393 /*
369 * Use old data from sc_last[] when new data 394 * Use old data from sc_last[] when new data
370 * is not yet valid (i.e. DF command came too 395 * is not yet valid (i.e. DF command came too
371 * quickly after the last command). 396 * quickly after the last command).
372 */ 397 */
373 if (!(sc->sc_data[0] & 398 if (!(sc->sc_data[0] &
374 (HYTP14_RESP_CMDMODE | HYTP14_RESP_STALE))) { 399 (HYTP14_RESP_CMDMODE | HYTP14_RESP_STALE))) {
375 memcpy(sc->sc_last, sc->sc_data, 400 memcpy(sc->sc_last, sc->sc_data,
376 sizeof(sc->sc_last)); 401 sizeof(sc->sc_last));
377 sc->sc_valid = ENVSYS_SVALID; 402 sc->sc_valid = ENVSYS_SVALID;
378 } else 403 } else
379 memcpy(sc->sc_data, sc->sc_last, 404 memcpy(sc->sc_data, sc->sc_last,
380 sizeof(sc->sc_data)); 405 sizeof(sc->sc_data));
381 } 406 }
382 407
383 iic_release_bus(sc->sc_tag, 0);  408 iic_release_bus(sc->sc_tag, 0);
384 DPRINTF(3, ("%s(%s): bus released\n", 409 DPRINTF(3, ("%s(%s): bus released\n",
385 __func__, device_xname(sc->sc_dev))); 410 __func__, device_xname(sc->sc_dev)));
386 } else { 411 } else {
387 DPRINTF(2, ("%s: %s: failed acquire i2c bus - error %d\n", 412 DPRINTF(2, ("%s: %s: failed acquire i2c bus - error %d\n",
388 device_xname(sc->sc_dev), __func__, error)); 413 device_xname(sc->sc_dev), __func__, error));
389 } 414 }
390 415
391 return sc->sc_valid; 416 return sc->sc_valid;
392} 417}
393 418
394 419
395static void 420static void
396hytp14_refresh_humidity(struct hytp14_sc *sc, envsys_data_t *edata) 421hytp14_refresh_humidity(struct hytp14_sc *sc, envsys_data_t *edata)
397{ 422{
398 uint16_t hyg; 423 uint16_t hyg;
399 int status; 424 int status;
400  425
401 status = hytp14_refresh_sensor(sc); 426 status = hytp14_refresh_sensor(sc);
402  427
403 if (status == ENVSYS_SVALID) { 428 if (status == ENVSYS_SVALID) {
404 hyg = (sc->sc_data[0] << 8) | sc->sc_data[1]; 429 hyg = (sc->sc_data[0] << 8) | sc->sc_data[1];
405  430
406 edata->value_cur = (1000000000 / HYTP14_HYG_SCALE) * (int32_t)HYTP14_HYG_RAWVAL(hyg); 431 edata->value_cur = (1000000000 / HYTP14_HYG_SCALE) * (int32_t)HYTP14_HYG_RAWVAL(hyg);
407 edata->value_cur /= 10; 432 edata->value_cur /= 10;
408 } 433 }
409 434
410 edata->state = status; 435 edata->state = status;
411} 436}
412 437
413static void 438static void
414hytp14_refresh_temp(struct hytp14_sc *sc, envsys_data_t *edata) 439hytp14_refresh_temp(struct hytp14_sc *sc, envsys_data_t *edata)
415{ 440{
416 uint16_t temp; 441 uint16_t temp;
417 int status; 442 int status;
418  443
419 status = hytp14_refresh_sensor(sc); 444 status = hytp14_refresh_sensor(sc);
420  445
421 if (status == ENVSYS_SVALID) { 446 if (status == ENVSYS_SVALID) {
422 temp = HYTP14_TEMP_RAWVAL((sc->sc_data[2] << 8) | sc->sc_data[3]); 447 temp = HYTP14_TEMP_RAWVAL((sc->sc_data[2] << 8) | sc->sc_data[3]);
423 448
424 edata->value_cur = (HYTP14_TEMP_FACTOR * 1000000) / HYTP14_TEMP_SCALE; 449 edata->value_cur = (HYTP14_TEMP_FACTOR * 1000000) / HYTP14_TEMP_SCALE;
425 edata->value_cur *= (int32_t)temp; 450 edata->value_cur *= (int32_t)temp;
426 edata->value_cur += HYTP14_TEMP_OFFSET * 1000000 + 273150000; 451 edata->value_cur += HYTP14_TEMP_OFFSET * 1000000 + 273150000;
427 } 452 }
428 453
429 edata->state = status; 454 edata->state = status;
430} 455}
431 456
432static void 457static void
433hytp14_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 458hytp14_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
434{ 459{
435 struct hytp14_sc *sc; 460 struct hytp14_sc *sc;
436 461
437 sc = sme->sme_cookie; 462 sc = sme->sme_cookie;
438 hytp14_sensors[edata->sensor].refresh(sc, edata); 463 hytp14_sensors[edata->sensor].refresh(sc, edata);
439} 464}
440 465
441static int 466static int
442sysctl_hytp14_interval(SYSCTLFN_ARGS) 467sysctl_hytp14_interval(SYSCTLFN_ARGS)
443{ 468{
444 struct sysctlnode node; 469 struct sysctlnode node;
445 struct hytp14_sc *sc; 470 struct hytp14_sc *sc;
446 int32_t t; 471 int32_t t;
447 int error; 472 int error;
448 473
449 node = *rnode; 474 node = *rnode;
450 sc = node.sysctl_data; 475 sc = node.sysctl_data;
451 476
452 t = sc->sc_mrinterval; 477 t = sc->sc_mrinterval;
453 node.sysctl_data = &t; 478 node.sysctl_data = &t;
454 479
455 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 480 error = sysctl_lookup(SYSCTLFN_CALL(&node));
456 if (error || newp == NULL) 481 if (error || newp == NULL)
457 return error; 482 return error;
458 if (t <= 0) 483 if (t <= 0)
459 return EINVAL; 484 return EINVAL;
460 485
461 sc->sc_mrinterval = t; 486 sc->sc_mrinterval = t;
462 return 0; 487 return 0;
463} 488}
464 489
465MODULE(MODULE_CLASS_DRIVER, hythygtemp, "i2cexec,sysmon_envsys"); 490MODULE(MODULE_CLASS_DRIVER, hythygtemp, "i2cexec,sysmon_envsys");
466 491
467#ifdef _MODULE 492#ifdef _MODULE
468#include "ioconf.c" 493#include "ioconf.c"
469#endif 494#endif
470 495
471static int 496static int
472hythygtemp_modcmd(modcmd_t cmd, void *opaque) 497hythygtemp_modcmd(modcmd_t cmd, void *opaque)
473{ 498{
474 int error; 499 int error;
475 500
476 error = 0; 501 error = 0;
477 502
478 switch (cmd) { 503 switch (cmd) {
479 case MODULE_CMD_INIT: 504 case MODULE_CMD_INIT:
480#ifdef _MODULE 505#ifdef _MODULE
481 error = config_init_component(cfdriver_ioconf_hythygtemp, 506 error = config_init_component(cfdriver_ioconf_hythygtemp,
482 cfattach_ioconf_hythygtemp, cfdata_ioconf_hythygtemp); 507 cfattach_ioconf_hythygtemp, cfdata_ioconf_hythygtemp);
483#endif 508#endif
484 return error; 509 return error;
485 510
486 case MODULE_CMD_FINI: 511 case MODULE_CMD_FINI:
487#ifdef _MODULE 512#ifdef _MODULE
488 error = config_fini_component(cfdriver_ioconf_hythygtemp, 513 error = config_fini_component(cfdriver_ioconf_hythygtemp,
489 cfattach_ioconf_hythygtemp, cfdata_ioconf_hythygtemp); 514 cfattach_ioconf_hythygtemp, cfdata_ioconf_hythygtemp);
490#endif 515#endif
491 return error; 516 return error;
492 517
493 default: 518 default:
494 return ENOTTY; 519 return ENOTTY;
495 } 520 }
496} 521}