emcfan.c revision 1.1 1 1.1 brad /* $NetBSD: emcfan.c,v 1.1 2025/03/11 13:56:46 brad Exp $ */
2 1.1 brad
3 1.1 brad /*
4 1.1 brad * Copyright (c) 2025 Brad Spencer <brad (at) anduin.eldar.org>
5 1.1 brad *
6 1.1 brad * Permission to use, copy, modify, and distribute this software for any
7 1.1 brad * purpose with or without fee is hereby granted, provided that the above
8 1.1 brad * copyright notice and this permission notice appear in all copies.
9 1.1 brad *
10 1.1 brad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 brad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 brad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 brad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 brad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 brad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 brad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 brad */
18 1.1 brad
19 1.1 brad #include <sys/cdefs.h>
20 1.1 brad __KERNEL_RCSID(0, "$NetBSD: emcfan.c,v 1.1 2025/03/11 13:56:46 brad Exp $");
21 1.1 brad
22 1.1 brad /*
23 1.1 brad * Driver for the EMC-210x and EMC-230x fan controllers on a
24 1.1 brad * I2C bus.
25 1.1 brad */
26 1.1 brad
27 1.1 brad #include <sys/param.h>
28 1.1 brad #include <sys/systm.h>
29 1.1 brad #include <sys/kernel.h>
30 1.1 brad #include <sys/device.h>
31 1.1 brad #include <sys/module.h>
32 1.1 brad #include <sys/conf.h>
33 1.1 brad #include <sys/sysctl.h>
34 1.1 brad #include <sys/mutex.h>
35 1.1 brad #include <sys/condvar.h>
36 1.1 brad #include <sys/kthread.h>
37 1.1 brad #include <sys/pool.h>
38 1.1 brad #include <sys/kmem.h>
39 1.1 brad
40 1.1 brad #include <dev/sysmon/sysmonvar.h>
41 1.1 brad #include <dev/i2c/i2cvar.h>
42 1.1 brad #include <dev/i2c/emcfanreg.h>
43 1.1 brad #include <dev/i2c/emcfanvar.h>
44 1.1 brad #include <dev/i2c/emcfaninfo.h>
45 1.1 brad
46 1.1 brad static int emcfan_poke(i2c_tag_t, i2c_addr_t, bool);
47 1.1 brad static int emcfan_match(device_t, cfdata_t, void *);
48 1.1 brad static void emcfan_attach(device_t, device_t, void *);
49 1.1 brad static int emcfan_detach(device_t, int);
50 1.1 brad static void emcfan_refresh(struct sysmon_envsys *, envsys_data_t *);
51 1.1 brad static int emcfan_activate(device_t, enum devact);
52 1.1 brad static int emcfan_verify_sysctl(SYSCTLFN_ARGS);
53 1.1 brad static void emcfan_attach_gpio(struct emcfan_sc *, uint8_t);
54 1.1 brad
55 1.1 brad #define EMCFAN_DEBUG
56 1.1 brad #ifdef EMCFAN_DEBUG
57 1.1 brad #define DPRINTF(s, l, x) \
58 1.1 brad do { \
59 1.1 brad if (l <= s->sc_emcfandebug) \
60 1.1 brad printf x; \
61 1.1 brad } while (/*CONSTCOND*/0)
62 1.1 brad #else
63 1.1 brad #define DPRINTF(s, l, x)
64 1.1 brad #endif
65 1.1 brad
66 1.1 brad CFATTACH_DECL_NEW(emcfan, sizeof(struct emcfan_sc),
67 1.1 brad emcfan_match, emcfan_attach, emcfan_detach, emcfan_activate);
68 1.1 brad
69 1.1 brad extern struct cfdriver emcfan_cd;
70 1.1 brad
71 1.1 brad static dev_type_open(emcfanopen);
72 1.1 brad static dev_type_read(emcfanread);
73 1.1 brad static dev_type_write(emcfanwrite);
74 1.1 brad static dev_type_close(emcfanclose);
75 1.1 brad const struct cdevsw emcfan_cdevsw = {
76 1.1 brad .d_open = emcfanopen,
77 1.1 brad .d_close = emcfanclose,
78 1.1 brad .d_read = emcfanread,
79 1.1 brad .d_write = emcfanwrite,
80 1.1 brad .d_ioctl = noioctl,
81 1.1 brad .d_stop = nostop,
82 1.1 brad .d_tty = notty,
83 1.1 brad .d_poll = nopoll,
84 1.1 brad .d_mmap = nommap,
85 1.1 brad .d_kqfilter = nokqfilter,
86 1.1 brad .d_discard = nodiscard,
87 1.1 brad .d_flag = D_OTHER
88 1.1 brad };
89 1.1 brad
90 1.1 brad static bool
91 1.1 brad emcfan_reg_is_real(struct emcfan_sc *sc, uint8_t reg)
92 1.1 brad {
93 1.1 brad int segment;
94 1.1 brad uint64_t index;
95 1.1 brad
96 1.1 brad segment = reg / 64;
97 1.1 brad index = reg % 64;
98 1.1 brad
99 1.1 brad DPRINTF(sc, 10, ("%s: void check 1: reg=%02x, segment=%d, index=%jd, sc_info_info=%d\n", __func__, reg,
100 1.1 brad segment, index, sc->sc_info_index));
101 1.1 brad DPRINTF(sc, 10, ("%s: void check 2: register_void=%jx, shift=%jx\n", __func__,
102 1.1 brad emcfan_chip_infos[sc->sc_info_index].register_void[segment], ((uint64_t)1 << index)));
103 1.1 brad
104 1.1 brad return(emcfan_chip_infos[sc->sc_info_index].register_void[segment] & ((uint64_t)1 << index));
105 1.1 brad }
106 1.1 brad
107 1.1 brad static int
108 1.1 brad emcfan_read_registerr(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
109 1.1 brad uint8_t *res)
110 1.1 brad {
111 1.1 brad int error = 0;
112 1.1 brad
113 1.1 brad error = iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, ®, 1, res, 1, 0);
114 1.1 brad
115 1.1 brad return error;
116 1.1 brad }
117 1.1 brad
118 1.1 brad static int
119 1.1 brad emcfan_read_register(struct emcfan_sc *sc, uint8_t reg, uint8_t *res)
120 1.1 brad {
121 1.1 brad if (emcfan_reg_is_real(sc,reg))
122 1.1 brad return(emcfan_read_registerr(sc->sc_tag, sc->sc_addr, reg, res));
123 1.1 brad else
124 1.1 brad *res = EMCFAN_VOID_READ;
125 1.1 brad return 0;
126 1.1 brad }
127 1.1 brad
128 1.1 brad static int
129 1.1 brad emcfan_write_registerr(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
130 1.1 brad uint8_t value)
131 1.1 brad {
132 1.1 brad int error = 0;
133 1.1 brad
134 1.1 brad error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, ®, 1, &value, 1, 0);
135 1.1 brad
136 1.1 brad return error;
137 1.1 brad }
138 1.1 brad
139 1.1 brad static int
140 1.1 brad emcfan_write_register(struct emcfan_sc *sc, uint8_t reg, uint8_t value)
141 1.1 brad {
142 1.1 brad if (emcfan_reg_is_real(sc,reg))
143 1.1 brad return(emcfan_write_registerr(sc->sc_tag, sc->sc_addr, reg, value));
144 1.1 brad else
145 1.1 brad return EACCES;
146 1.1 brad }
147 1.1 brad
148 1.1 brad static int
149 1.1 brad emcfan_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
150 1.1 brad {
151 1.1 brad int error;
152 1.1 brad uint8_t res;
153 1.1 brad
154 1.1 brad error = emcfan_read_registerr(tag, addr, EMCFAN_MANUFACTURER_ID, &res);
155 1.1 brad if (matchdebug) {
156 1.1 brad printf("poke X 1: %d %d\n", addr, error);
157 1.1 brad }
158 1.1 brad
159 1.1 brad /* Ok.. something was there, but the ID did not match what was expected.
160 1.1 brad * We get away with doing this because the poke is just getting the Manufacturer
161 1.1 brad * ID, which is a fixed value.
162 1.1 brad */
163 1.1 brad
164 1.1 brad if (!error) {
165 1.1 brad if (res != EMCFAN_VALID_MANUFACTURER_ID)
166 1.1 brad error = EIO;
167 1.1 brad }
168 1.1 brad
169 1.1 brad return error;
170 1.1 brad }
171 1.1 brad
172 1.1 brad static bool
173 1.1 brad emcfan_check_i2c_addr(i2c_addr_t addr)
174 1.1 brad {
175 1.1 brad bool r = false;
176 1.1 brad
177 1.1 brad for(int i = 0;i < __arraycount(emcfan_typical_addrs); i++)
178 1.1 brad if (addr == emcfan_typical_addrs[i]) {
179 1.1 brad r = true;
180 1.1 brad break;
181 1.1 brad }
182 1.1 brad
183 1.1 brad return(r);
184 1.1 brad }
185 1.1 brad
186 1.1 brad static int
187 1.1 brad emcfan_match(device_t parent, cfdata_t match, void *aux)
188 1.1 brad {
189 1.1 brad struct i2c_attach_args *ia = aux;
190 1.1 brad int error, match_result;
191 1.1 brad const bool matchdebug = false;
192 1.1 brad
193 1.1 brad if (iic_use_direct_match(ia, match, NULL, &match_result))
194 1.1 brad return match_result;
195 1.1 brad
196 1.1 brad if (matchdebug) {
197 1.1 brad printf("Looking at ia_addr: %x\n",ia->ia_addr);
198 1.1 brad }
199 1.1 brad
200 1.1 brad /* Look to see if there is a device indirectly */
201 1.1 brad
202 1.1 brad if (! emcfan_check_i2c_addr(ia->ia_addr))
203 1.1 brad return 0;
204 1.1 brad
205 1.1 brad /*
206 1.1 brad * Check to see if something is really at this i2c address.
207 1.1 brad * This will keep phantom devices from appearing
208 1.1 brad */
209 1.1 brad if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
210 1.1 brad if (matchdebug)
211 1.1 brad printf("in match acquire bus failed\n");
212 1.1 brad return 0;
213 1.1 brad }
214 1.1 brad
215 1.1 brad error = emcfan_poke(ia->ia_tag, ia->ia_addr, matchdebug);
216 1.1 brad iic_release_bus(ia->ia_tag, 0);
217 1.1 brad
218 1.1 brad return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
219 1.1 brad }
220 1.1 brad
221 1.1 brad static int
222 1.1 brad emcfan_find_info(uint8_t product)
223 1.1 brad {
224 1.1 brad for(int i = 0;i < __arraycount(emcfan_chip_infos); i++)
225 1.1 brad if (product == emcfan_chip_infos[i].product_id)
226 1.1 brad return(i);
227 1.1 brad
228 1.1 brad return(-1);
229 1.1 brad }
230 1.1 brad
231 1.1 brad static const char *
232 1.1 brad emcfan_product_to_name(uint8_t info_index)
233 1.1 brad {
234 1.1 brad KASSERT(info_index >= 0);
235 1.1 brad
236 1.1 brad return(emcfan_chip_infos[info_index].name);
237 1.1 brad }
238 1.1 brad
239 1.1 brad int
240 1.1 brad emcfan_verify_sysctl(SYSCTLFN_ARGS)
241 1.1 brad {
242 1.1 brad int error, t;
243 1.1 brad struct sysctlnode node;
244 1.1 brad
245 1.1 brad node = *rnode;
246 1.1 brad t = *(int *)rnode->sysctl_data;
247 1.1 brad node.sysctl_data = &t;
248 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
249 1.1 brad if (error || newp == NULL)
250 1.1 brad return error;
251 1.1 brad
252 1.1 brad if (t < 0)
253 1.1 brad return EINVAL;
254 1.1 brad
255 1.1 brad *(int *)rnode->sysctl_data = t;
256 1.1 brad
257 1.1 brad return 0;
258 1.1 brad }
259 1.1 brad
260 1.1 brad static int
261 1.1 brad emcfan_sysctl_init(struct emcfan_sc *sc)
262 1.1 brad {
263 1.1 brad int error;
264 1.1 brad const struct sysctlnode *cnode;
265 1.1 brad int sysctlroot_num;
266 1.1 brad char pole_name[8];
267 1.1 brad
268 1.1 brad if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode,
269 1.1 brad 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
270 1.1 brad SYSCTL_DESCR("emcfan controls"), NULL, 0, NULL, 0, CTL_HW,
271 1.1 brad CTL_CREATE, CTL_EOL)) != 0)
272 1.1 brad return error;
273 1.1 brad
274 1.1 brad sysctlroot_num = cnode->sysctl_num;
275 1.1 brad
276 1.1 brad #ifdef EMCFAN_DEBUG
277 1.1 brad if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode,
278 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
279 1.1 brad SYSCTL_DESCR("Debug level"), emcfan_verify_sysctl, 0,
280 1.1 brad &sc->sc_emcfandebug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
281 1.1 brad CTL_EOL)) != 0)
282 1.1 brad return error;
283 1.1 brad
284 1.1 brad #endif
285 1.1 brad
286 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].family == EMCFAN_FAMILY_230X ||
287 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_1 ||
288 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_24 ||
289 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2104 ||
290 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2106) {
291 1.1 brad for(int i=0;i < emcfan_chip_infos[sc->sc_info_index].num_tachs;i++) {
292 1.1 brad snprintf(pole_name,sizeof(pole_name),"poles%d",i+1);
293 1.1 brad if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode,
294 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, pole_name,
295 1.1 brad SYSCTL_DESCR("Number of poles"), emcfan_verify_sysctl, 0,
296 1.1 brad &sc->sc_num_poles[i], 0, CTL_HW, sysctlroot_num, CTL_CREATE,
297 1.1 brad CTL_EOL)) != 0)
298 1.1 brad return error;
299 1.1 brad }
300 1.1 brad }
301 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_1 ||
302 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id == EMCFAN_PRODUCT_2103_24) {
303 1.1 brad if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode,
304 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "ftach",
305 1.1 brad SYSCTL_DESCR("ftach frequency"), emcfan_verify_sysctl, 0,
306 1.1 brad &sc->sc_ftach, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
307 1.1 brad CTL_EOL)) != 0)
308 1.1 brad return error;
309 1.1 brad }
310 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].vin4_temp_zone) {
311 1.1 brad if ((error = sysctl_createv(&sc->sc_emcfanlog, 0, NULL, &cnode,
312 1.1 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "vin4",
313 1.1 brad SYSCTL_DESCR("Use VIN4 pin as a temperature sensor input"), NULL, 0,
314 1.1 brad &sc->sc_vin4_temp, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
315 1.1 brad CTL_EOL)) != 0)
316 1.1 brad return error;
317 1.1 brad }
318 1.1 brad
319 1.1 brad return 0;
320 1.1 brad }
321 1.1 brad
322 1.1 brad static void
323 1.1 brad emcfan_attach(device_t parent, device_t self, void *aux)
324 1.1 brad {
325 1.1 brad struct emcfan_sc *sc;
326 1.1 brad struct i2c_attach_args *ia;
327 1.1 brad uint8_t product_id, revision;
328 1.1 brad int error;
329 1.1 brad
330 1.1 brad ia = aux;
331 1.1 brad sc = device_private(self);
332 1.1 brad
333 1.1 brad sc->sc_dev = self;
334 1.1 brad sc->sc_tag = ia->ia_tag;
335 1.1 brad sc->sc_addr = ia->ia_addr;
336 1.1 brad sc->sc_opened = false;
337 1.1 brad sc->sc_dying = false;
338 1.1 brad sc->sc_ftach = 32000;
339 1.1 brad sc->sc_vin4_temp = false;
340 1.1 brad for(int i=0;i < EMCFAN_NUM_FANS;i++)
341 1.1 brad sc->sc_num_poles[i] = 2;
342 1.1 brad sc->sc_emcfandebug = 0;
343 1.1 brad
344 1.1 brad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
345 1.1 brad
346 1.1 brad aprint_normal("\n");
347 1.1 brad
348 1.1 brad if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
349 1.1 brad aprint_error_dev(self,
350 1.1 brad "Unable to create sysmon structure\n");
351 1.1 brad sc->sc_sme = NULL;
352 1.1 brad return;
353 1.1 brad }
354 1.1 brad
355 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
356 1.1 brad if (error) {
357 1.1 brad aprint_error_dev(self, "Could not acquire iic bus: %d\n",
358 1.1 brad error);
359 1.1 brad goto out;
360 1.1 brad }
361 1.1 brad
362 1.1 brad error = emcfan_read_registerr(sc->sc_tag, sc->sc_addr, EMCFAN_PRODUCT_ID, &product_id);
363 1.1 brad if (error != 0) {
364 1.1 brad aprint_error_dev(self, "Could not get the product id\n");
365 1.1 brad } else {
366 1.1 brad error = emcfan_read_registerr(sc->sc_tag, sc->sc_addr, EMCFAN_REVISION, &revision);
367 1.1 brad if (error != 0) {
368 1.1 brad aprint_error_dev(self, "Could not get the revision of the chip\n");
369 1.1 brad }
370 1.1 brad }
371 1.1 brad
372 1.1 brad iic_release_bus(sc->sc_tag, 0);
373 1.1 brad if (error != 0) {
374 1.1 brad aprint_error_dev(self, "Unable to setup device\n");
375 1.1 brad goto out;
376 1.1 brad }
377 1.1 brad
378 1.1 brad sc->sc_info_index = emcfan_find_info(product_id);
379 1.1 brad if (sc->sc_info_index < 0) {
380 1.1 brad aprint_error_dev(self, "Unknown product id: %02x\n",product_id);
381 1.1 brad goto out;
382 1.1 brad }
383 1.1 brad
384 1.1 brad if ((error = emcfan_sysctl_init(sc)) != 0) {
385 1.1 brad sc->sc_emcfanlog = NULL;
386 1.1 brad aprint_error_dev(self, "Can't setup sysctl tree (%d)\n", error);
387 1.1 brad goto out;
388 1.1 brad }
389 1.1 brad
390 1.1 brad for(int i=0;i < EMCFAN_NUM_SENSORS;i++)
391 1.1 brad sc->sc_sensor_instances[i].sc_i_member = -1;
392 1.1 brad
393 1.1 brad int sensor_instance = 0;
394 1.1 brad /* Set up the tachs */
395 1.1 brad for(int i = 0;i < emcfan_chip_infos[sc->sc_info_index].num_tachs &&
396 1.1 brad sensor_instance < EMCFAN_NUM_SENSORS;
397 1.1 brad i++) {
398 1.1 brad snprintf(sc->sc_sensors[sensor_instance].desc,
399 1.1 brad sizeof(sc->sc_sensors[sensor_instance].desc),
400 1.1 brad "FAN %d",i+1);
401 1.1 brad
402 1.1 brad DPRINTF(sc, 2, ("%s: TACH registering fan sensor %d (%s)\n", __func__,
403 1.1 brad sensor_instance, sc->sc_sensors[sensor_instance].desc));
404 1.1 brad
405 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_flags = 0;
406 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_member = i + 1;
407 1.1 brad sc->sc_sensors[sensor_instance].units = ENVSYS_SFANRPM;
408 1.1 brad sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID;
409 1.1 brad
410 1.1 brad error = sysmon_envsys_sensor_attach(sc->sc_sme,
411 1.1 brad &sc->sc_sensors[sensor_instance]);
412 1.1 brad if (error) {
413 1.1 brad aprint_error_dev(self,
414 1.1 brad "Unable to attach sensor %d: %d\n", i, error);
415 1.1 brad goto out;
416 1.1 brad }
417 1.1 brad
418 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_envnum =
419 1.1 brad sc->sc_sensors[sensor_instance].sensor;
420 1.1 brad
421 1.1 brad DPRINTF(sc, 2, ("%s: TACH recorded sensor instance number %d->%d\n", __func__,
422 1.1 brad sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum));
423 1.1 brad
424 1.1 brad sensor_instance++;
425 1.1 brad }
426 1.1 brad
427 1.1 brad /* Set up internal temperature sensor */
428 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].internal_temp_zone) {
429 1.1 brad snprintf(sc->sc_sensors[sensor_instance].desc,
430 1.1 brad sizeof(sc->sc_sensors[sensor_instance].desc),
431 1.1 brad "internal temperature");
432 1.1 brad
433 1.1 brad DPRINTF(sc, 2, ("%s: IT registering internal temperature sensor %d (%s)\n", __func__,
434 1.1 brad sensor_instance, sc->sc_sensors[sensor_instance].desc));
435 1.1 brad
436 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_flags = EMCFAN_INTERNAL_TEMP;
437 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_member = 1;
438 1.1 brad sc->sc_sensors[sensor_instance].units = ENVSYS_STEMP;
439 1.1 brad sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID;
440 1.1 brad
441 1.1 brad error = sysmon_envsys_sensor_attach(sc->sc_sme,
442 1.1 brad &sc->sc_sensors[sensor_instance]);
443 1.1 brad if (error) {
444 1.1 brad aprint_error_dev(self,
445 1.1 brad "Unable to attach internal sensor: %d\n", error);
446 1.1 brad goto out;
447 1.1 brad }
448 1.1 brad
449 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_envnum =
450 1.1 brad sc->sc_sensors[sensor_instance].sensor;
451 1.1 brad
452 1.1 brad DPRINTF(sc, 2, ("%s: IT recorded sensor instance number %d->%d\n", __func__,
453 1.1 brad sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum));
454 1.1 brad
455 1.1 brad sensor_instance++;
456 1.1 brad }
457 1.1 brad
458 1.1 brad /* Set up VIN4 temperature sensor */
459 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].vin4_temp_zone) {
460 1.1 brad snprintf(sc->sc_sensors[sensor_instance].desc,
461 1.1 brad sizeof(sc->sc_sensors[sensor_instance].desc),
462 1.1 brad "VIN4 temperature");
463 1.1 brad
464 1.1 brad DPRINTF(sc, 2, ("%s: registering VIN4 temperature sensor %d (%s)\n", __func__,
465 1.1 brad sensor_instance, sc->sc_sensors[sensor_instance].desc));
466 1.1 brad
467 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_flags = EMCFAN_VIN4_TEMP;
468 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_member = 1;
469 1.1 brad sc->sc_sensors[sensor_instance].units = ENVSYS_STEMP;
470 1.1 brad sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID;
471 1.1 brad
472 1.1 brad error = sysmon_envsys_sensor_attach(sc->sc_sme,
473 1.1 brad &sc->sc_sensors[sensor_instance]);
474 1.1 brad if (error) {
475 1.1 brad aprint_error_dev(self,
476 1.1 brad "Unable to attach VIN4 sensor: %d\n", error);
477 1.1 brad goto out;
478 1.1 brad }
479 1.1 brad
480 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_envnum =
481 1.1 brad sc->sc_sensors[sensor_instance].sensor;
482 1.1 brad
483 1.1 brad DPRINTF(sc, 2, ("%s: VIN4 recorded sensor instance number %d->%d\n", __func__,
484 1.1 brad sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum));
485 1.1 brad
486 1.1 brad sensor_instance++;
487 1.1 brad }
488 1.1 brad
489 1.1 brad /* Set up external temperature sensors */
490 1.1 brad for(int i = 0;i < emcfan_chip_infos[sc->sc_info_index].num_external_temp_zones &&
491 1.1 brad sensor_instance < EMCFAN_NUM_SENSORS;
492 1.1 brad i++) {
493 1.1 brad snprintf(sc->sc_sensors[sensor_instance].desc,
494 1.1 brad sizeof(sc->sc_sensors[sensor_instance].desc),
495 1.1 brad "temperature zone %d",i+1);
496 1.1 brad
497 1.1 brad DPRINTF(sc, 2, ("%s: ET registering fan sensor %d (%s)\n", __func__,
498 1.1 brad sensor_instance, sc->sc_sensors[sensor_instance].desc));
499 1.1 brad
500 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_flags = 0;
501 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_member = i + 1;
502 1.1 brad sc->sc_sensors[sensor_instance].units = ENVSYS_STEMP;
503 1.1 brad sc->sc_sensors[sensor_instance].state = ENVSYS_SINVALID;
504 1.1 brad
505 1.1 brad error = sysmon_envsys_sensor_attach(sc->sc_sme,
506 1.1 brad &sc->sc_sensors[sensor_instance]);
507 1.1 brad if (error) {
508 1.1 brad aprint_error_dev(self,
509 1.1 brad "Unable to attach sensor %d: %d\n", i, error);
510 1.1 brad goto out;
511 1.1 brad }
512 1.1 brad
513 1.1 brad sc->sc_sensor_instances[sensor_instance].sc_i_envnum =
514 1.1 brad sc->sc_sensors[sensor_instance].sensor;
515 1.1 brad
516 1.1 brad DPRINTF(sc, 2, ("%s: ET recorded sensor instance number %d->%d\n", __func__,
517 1.1 brad sensor_instance, sc->sc_sensor_instances[sensor_instance].sc_i_envnum));
518 1.1 brad
519 1.1 brad sensor_instance++;
520 1.1 brad }
521 1.1 brad
522 1.1 brad sc->sc_sme->sme_name = device_xname(sc->sc_dev);
523 1.1 brad sc->sc_sme->sme_cookie = sc;
524 1.1 brad sc->sc_sme->sme_refresh = emcfan_refresh;
525 1.1 brad
526 1.1 brad if (sysmon_envsys_register(sc->sc_sme)) {
527 1.1 brad aprint_error_dev(self, "unable to register with sysmon\n");
528 1.1 brad sysmon_envsys_destroy(sc->sc_sme);
529 1.1 brad sc->sc_sme = NULL;
530 1.1 brad return;
531 1.1 brad }
532 1.1 brad
533 1.1 brad aprint_normal_dev(self, "Microchip Technology %s fan controller, "
534 1.1 brad "Revision: %02x\n",
535 1.1 brad emcfan_product_to_name(sc->sc_info_index),
536 1.1 brad revision);
537 1.1 brad int e = emcfan_chip_infos[sc->sc_info_index].num_external_temp_zones;
538 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].vin4_temp_zone)
539 1.1 brad e++;
540 1.1 brad aprint_normal_dev(self, "Fans: %d, Tachometers: %d, Internal Temperature: %s, External Sensors: %d, GPIO: %s\n",
541 1.1 brad emcfan_chip_infos[sc->sc_info_index].num_fans,
542 1.1 brad emcfan_chip_infos[sc->sc_info_index].num_tachs,
543 1.1 brad (emcfan_chip_infos[sc->sc_info_index].internal_temp_zone) ? "Yes" : "No",
544 1.1 brad e,
545 1.1 brad (emcfan_chip_infos[sc->sc_info_index].num_gpio_pins > 0) ? "Yes" : "No");
546 1.1 brad
547 1.1 brad if (emcfan_chip_infos[sc->sc_info_index].num_gpio_pins > 0)
548 1.1 brad emcfan_attach_gpio(sc, product_id);
549 1.1 brad return;
550 1.1 brad out:
551 1.1 brad sysmon_envsys_destroy(sc->sc_sme);
552 1.1 brad sc->sc_sme = NULL;
553 1.1 brad }
554 1.1 brad
555 1.1 brad /* The EMC-2101 is quite a bit different than the other EMC fan controllers.
556 1.1 brad * Handle it differently.
557 1.1 brad */
558 1.1 brad
559 1.1 brad static void
560 1.1 brad emcfan_refresh_2101_tach(struct sysmon_envsys *sme, envsys_data_t *edata, int instance)
561 1.1 brad {
562 1.1 brad struct emcfan_sc *sc = sme->sme_cookie;
563 1.1 brad
564 1.1 brad int error;
565 1.1 brad uint8_t tach_high_reg;
566 1.1 brad uint8_t tach_low_reg;
567 1.1 brad uint8_t tach_high;
568 1.1 brad uint8_t tach_low;
569 1.1 brad
570 1.1 brad switch(sc->sc_sensor_instances[instance].sc_i_member) {
571 1.1 brad case 1:
572 1.1 brad tach_high_reg = EMCFAN_2101_TACH_HIGH;
573 1.1 brad tach_low_reg = EMCFAN_2101_TACH_LOW;
574 1.1 brad break;
575 1.1 brad default:
576 1.1 brad panic("A 2101 can not have more than one tach\n");
577 1.1 brad break;
578 1.1 brad };
579 1.1 brad
580 1.1 brad DPRINTF(sc, 2, ("%s: dev=%s, instance=%d, sc_i_member=%d, tach_high_reg=0x%02X, tach_low_reg=0x%02X\n", __func__,
581 1.1 brad device_xname(sc->sc_dev), instance,
582 1.1 brad sc->sc_sensor_instances[instance].sc_i_member,
583 1.1 brad tach_high_reg, tach_low_reg));
584 1.1 brad
585 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
586 1.1 brad if (error) {
587 1.1 brad device_printf(sc->sc_dev,"%s: could not acquire I2C bus: %d\n",__func__, error);
588 1.1 brad return;
589 1.1 brad }
590 1.1 brad
591 1.1 brad /* There is a interlock thing with the low and high bytes. Read the
592 1.1 brad * low byte first.
593 1.1 brad */
594 1.1 brad
595 1.1 brad error = emcfan_read_register(sc, tach_low_reg, &tach_low);
596 1.1 brad if (error) {
597 1.1 brad device_printf(sc->sc_dev,"%s: could not read tach low register: %d\n",__func__, error);
598 1.1 brad iic_release_bus(sc->sc_tag, 0);
599 1.1 brad return;
600 1.1 brad }
601 1.1 brad error = emcfan_read_register(sc, tach_high_reg, &tach_high);
602 1.1 brad if (error) {
603 1.1 brad device_printf(sc->sc_dev,"%s: could not read tach high register: %d\n",__func__, error);
604 1.1 brad iic_release_bus(sc->sc_tag, 0);
605 1.1 brad return;
606 1.1 brad }
607 1.1 brad
608 1.1 brad iic_release_bus(sc->sc_tag, 0);
609 1.1 brad
610 1.1 brad uint16_t count;
611 1.1 brad count = tach_high << 8;
612 1.1 brad count |= tach_low;
613 1.1 brad
614 1.1 brad DPRINTF(sc, 2, ("%s: instance=%d, tach_high=%d 0x%02X, tach_low=%d 0x%02X, count=%d\n", __func__,
615 1.1 brad instance, tach_high, tach_high, tach_low, tach_low, count));
616 1.1 brad
617 1.1 brad /* 0xffff indicates that the fan is not present, stopped / stalled
618 1.1 brad * or below the RPM that can be measured or the chip is not configured
619 1.1 brad * to read tach signals on the pin, but is being used for an alert
620 1.1 brad */
621 1.1 brad
622 1.1 brad if (count == 0xffff)
623 1.1 brad return;
624 1.1 brad
625 1.1 brad /* The formula is:
626 1.1 brad *
627 1.1 brad * rpm = 5400000 / count
628 1.1 brad *
629 1.1 brad */
630 1.1 brad
631 1.1 brad uint64_t irpm;
632 1.1 brad
633 1.1 brad irpm = 5400000 / count;
634 1.1 brad
635 1.1 brad edata->value_cur = (uint32_t) irpm;
636 1.1 brad edata->state = ENVSYS_SVALID;
637 1.1 brad }
638 1.1 brad
639 1.1 brad static void
640 1.1 brad emcfan_refresh_210_346_230x_tach(int product_family, uint8_t product_id,
641 1.1 brad struct sysmon_envsys *sme, envsys_data_t *edata, int instance)
642 1.1 brad {
643 1.1 brad struct emcfan_sc *sc = sme->sme_cookie;
644 1.1 brad
645 1.1 brad int error;
646 1.1 brad uint8_t tach_high_reg;
647 1.1 brad uint8_t tach_low_reg;
648 1.1 brad uint8_t fan_config_reg;
649 1.1 brad uint8_t chip_config;
650 1.1 brad uint8_t fan_config;
651 1.1 brad uint8_t tach_high;
652 1.1 brad uint8_t tach_low;
653 1.1 brad int ftach = 32000;
654 1.1 brad int edges;
655 1.1 brad int poles;
656 1.1 brad int m;
657 1.1 brad
658 1.1 brad if (product_family == EMCFAN_FAMILY_210X) {
659 1.1 brad switch(sc->sc_sensor_instances[instance].sc_i_member) {
660 1.1 brad case 1:
661 1.1 brad fan_config_reg = EMCFAN_210_346_CONFIG_1;
662 1.1 brad tach_high_reg = EMCFAN_210_346_TACH_1_HIGH;
663 1.1 brad tach_low_reg = EMCFAN_210_346_TACH_1_LOW;
664 1.1 brad break;
665 1.1 brad case 2:
666 1.1 brad fan_config_reg = EMCFAN_210_346_CONFIG_2;
667 1.1 brad tach_high_reg = EMCFAN_210_346_TACH_2_HIGH;
668 1.1 brad tach_low_reg = EMCFAN_210_346_TACH_2_LOW;
669 1.1 brad break;
670 1.1 brad default:
671 1.1 brad panic("210X family do not know how to deal with member: %d\n",
672 1.1 brad sc->sc_sensor_instances[instance].sc_i_member);
673 1.1 brad break;
674 1.1 brad };
675 1.1 brad } else {
676 1.1 brad switch(sc->sc_sensor_instances[instance].sc_i_member) {
677 1.1 brad case 1:
678 1.1 brad fan_config_reg = EMCFAN_230X_CONFIG_1;
679 1.1 brad tach_high_reg = EMCFAN_230X_TACH_1_HIGH;
680 1.1 brad tach_low_reg = EMCFAN_230X_TACH_1_LOW;
681 1.1 brad break;
682 1.1 brad case 2:
683 1.1 brad fan_config_reg = EMCFAN_230X_CONFIG_2;
684 1.1 brad tach_high_reg = EMCFAN_230X_TACH_2_HIGH;
685 1.1 brad tach_low_reg = EMCFAN_230X_TACH_2_LOW;
686 1.1 brad break;
687 1.1 brad case 3:
688 1.1 brad fan_config_reg = EMCFAN_230X_CONFIG_3;
689 1.1 brad tach_high_reg = EMCFAN_230X_TACH_3_HIGH;
690 1.1 brad tach_low_reg = EMCFAN_230X_TACH_3_LOW;
691 1.1 brad break;
692 1.1 brad case 4:
693 1.1 brad fan_config_reg = EMCFAN_230X_CONFIG_4;
694 1.1 brad tach_high_reg = EMCFAN_230X_TACH_4_HIGH;
695 1.1 brad tach_low_reg = EMCFAN_230X_TACH_4_LOW;
696 1.1 brad break;
697 1.1 brad case 5:
698 1.1 brad fan_config_reg = EMCFAN_230X_CONFIG_5;
699 1.1 brad tach_high_reg = EMCFAN_230X_TACH_5_HIGH;
700 1.1 brad tach_low_reg = EMCFAN_230X_TACH_5_LOW;
701 1.1 brad break;
702 1.1 brad default:
703 1.1 brad panic("230X family do not know how to deal with member: %d\n",
704 1.1 brad sc->sc_sensor_instances[instance].sc_i_member);
705 1.1 brad break;
706 1.1 brad };
707 1.1 brad }
708 1.1 brad
709 1.1 brad DPRINTF(sc, 2, ("%s: dev=%s, instance=%d, sc_i_member=%d, fan_config_reg=0x%02X, tach_high_reg=0x%02X, tach_low_reg=0x%02X\n", __func__,
710 1.1 brad device_xname(sc->sc_dev), instance,
711 1.1 brad sc->sc_sensor_instances[instance].sc_i_member,
712 1.1 brad fan_config_reg, tach_high_reg, tach_low_reg));
713 1.1 brad
714 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
715 1.1 brad if (error) {
716 1.1 brad device_printf(sc->sc_dev,"%s: could not acquire I2C bus: %d\n",__func__, error);
717 1.1 brad return;
718 1.1 brad }
719 1.1 brad
720 1.1 brad if (product_id == EMCFAN_PRODUCT_2103_1 ||
721 1.1 brad product_id == EMCFAN_PRODUCT_2103_24) {
722 1.1 brad ftach = sc->sc_ftach;
723 1.1 brad } else {
724 1.1 brad chip_config = 0x00;
725 1.1 brad if (product_family == EMCFAN_FAMILY_230X) {
726 1.1 brad error = emcfan_read_register(sc, EMCFAN_CHIP_CONFIG, &chip_config);
727 1.1 brad } else {
728 1.1 brad if (product_id == EMCFAN_PRODUCT_2104 ||
729 1.1 brad product_id == EMCFAN_PRODUCT_2106) {
730 1.1 brad error = emcfan_read_register(sc, EMCFAN_MUX_PINS, &chip_config);
731 1.1 brad }
732 1.1 brad }
733 1.1 brad if (error) {
734 1.1 brad device_printf(sc->sc_dev,"%s: could not read chip config: %d\n",__func__, error);
735 1.1 brad iic_release_bus(sc->sc_tag, 0);
736 1.1 brad return;
737 1.1 brad }
738 1.1 brad
739 1.1 brad /* Figure out if there is an external clock involved */
740 1.1 brad if (product_family == EMCFAN_FAMILY_230X) {
741 1.1 brad if (chip_config & 0x02)
742 1.1 brad ftach = 32000;
743 1.1 brad else
744 1.1 brad if (chip_config & 0x01)
745 1.1 brad ftach = 32768;
746 1.1 brad else
747 1.1 brad ftach = 32000;
748 1.1 brad } else {
749 1.1 brad if (product_id == EMCFAN_PRODUCT_2104 ||
750 1.1 brad product_id == EMCFAN_PRODUCT_2106) {
751 1.1 brad if (chip_config & 0x01)
752 1.1 brad ftach = 32768;
753 1.1 brad else
754 1.1 brad ftach = 32000;
755 1.1 brad }
756 1.1 brad }
757 1.1 brad
758 1.1 brad }
759 1.1 brad
760 1.1 brad error = emcfan_read_register(sc, fan_config_reg, &fan_config);
761 1.1 brad if (error) {
762 1.1 brad device_printf(sc->sc_dev,"%s: could not read fan config: %d\n",__func__, error);
763 1.1 brad iic_release_bus(sc->sc_tag, 0);
764 1.1 brad return;
765 1.1 brad }
766 1.1 brad
767 1.1 brad /* There is a interlock thing with the low and high bytes. Read the
768 1.1 brad * low byte first.
769 1.1 brad */
770 1.1 brad
771 1.1 brad error = emcfan_read_register(sc, tach_low_reg, &tach_low);
772 1.1 brad if (error) {
773 1.1 brad device_printf(sc->sc_dev,"%s: could not read tach low register: %d\n",__func__, error);
774 1.1 brad iic_release_bus(sc->sc_tag, 0);
775 1.1 brad return;
776 1.1 brad }
777 1.1 brad error = emcfan_read_register(sc, tach_high_reg, &tach_high);
778 1.1 brad if (error) {
779 1.1 brad device_printf(sc->sc_dev,"%s: could not read tach high register: %d\n",__func__, error);
780 1.1 brad iic_release_bus(sc->sc_tag, 0);
781 1.1 brad return;
782 1.1 brad }
783 1.1 brad
784 1.1 brad iic_release_bus(sc->sc_tag, 0);
785 1.1 brad
786 1.1 brad /* Return early if the fan is stalled or not hooked up. It might be better to look at
787 1.1 brad * the stalled fan status register, but that works differently depending on which chip
788 1.1 brad * you are looking at.
789 1.1 brad */
790 1.1 brad
791 1.1 brad if (product_family == EMCFAN_FAMILY_210X) {
792 1.1 brad /* The datasheet is not at all clear as to what will be set in the low byte of the tach
793 1.1 brad * 0xc0, 0xe0 and 0xf0 all seem to depend on the minimum expected rpm and 0xf8 appears
794 1.1 brad * to mean that the fan is stalled in some way.
795 1.1 brad *
796 1.1 brad * Further to confuse matters, some chips may be able to adjust what invalid means.
797 1.1 brad * See the fan config register (0x4A) on the EMC2101 for an example of that. We check
798 1.1 brad * tach_low here just in case these chips can do that too.
799 1.1 brad */
800 1.1 brad if (tach_high == 0xff &&
801 1.1 brad (tach_low == 0xc0 || tach_low == 0xe0 ||
802 1.1 brad tach_low == 0xf0 || tach_low == 0xf8 ||
803 1.1 brad tach_low == 0xff))
804 1.1 brad return;
805 1.1 brad } else {
806 1.1 brad /* The datasheet for the 230X family was a little clearer. In that one, if the high byte is
807 1.1 brad * 0xff the tach reading is invalid.
808 1.1 brad */
809 1.1 brad if (tach_high == 0xff)
810 1.1 brad return;
811 1.1 brad }
812 1.1 brad
813 1.1 brad /* Extract the M value, also known as the tach multiplier */
814 1.1 brad m = fan_config & 0b01100000;
815 1.1 brad m = m >> 5;
816 1.1 brad
817 1.1 brad DPRINTF(sc, 2, ("%s: fan_config=%d 0x%02X, raw m=%d 0x%02X\n",
818 1.1 brad __func__, fan_config, fan_config, m, m));
819 1.1 brad
820 1.1 brad m = (1 << m);
821 1.1 brad
822 1.1 brad /* Extract the number of configured edges */
823 1.1 brad edges = fan_config & 0b00011000;
824 1.1 brad edges = edges >> 3;
825 1.1 brad
826 1.1 brad DPRINTF(sc, 2, ("%s: fan_config=%d 0x%02X, raw edges=%d 0x%02X\n",
827 1.1 brad __func__, fan_config, fan_config, edges, edges));
828 1.1 brad
829 1.1 brad edges = ((edges + 1) * 2) + 1;
830 1.1 brad
831 1.1 brad /* Calculate the tach count, which needs to use bit weights */
832 1.1 brad int count = 0;
833 1.1 brad count = (tach_high << 5) | tach_low;
834 1.1 brad
835 1.1 brad /* The number of poles is a sysctl setting */
836 1.1 brad poles = sc->sc_num_poles[sc->sc_sensor_instances[instance].sc_i_member - 1];
837 1.1 brad
838 1.1 brad DPRINTF(sc, 2, ("%s: instance=%d, ftach=%d, m=%d, edges=%d, poles=%d, tach_high=%d 0x%02X, tach_low=%d 0x%02X, count=%d\n", __func__,
839 1.1 brad instance, ftach, m, edges, poles, tach_high, tach_high, tach_low, tach_low, count));
840 1.1 brad
841 1.1 brad /* The formula is:
842 1.1 brad *
843 1.1 brad * rpm = 1/poles * ((edges - 1) / count * 1/m) * ftach * 60
844 1.1 brad *
845 1.1 brad * ftach is either 32.000khz or 32.768khz
846 1.1 brad *
847 1.1 brad */
848 1.1 brad
849 1.1 brad int64_t irpm;
850 1.1 brad int ip1, ip2;
851 1.1 brad int64_t ip3;
852 1.1 brad
853 1.1 brad ip1 = 10000 / poles;
854 1.1 brad /*
855 1.1 brad printf("poles: %d ; ip1: %d\n",poles,ip1);
856 1.1 brad */
857 1.1 brad ip2 = 10000 / m;
858 1.1 brad /*
859 1.1 brad printf("m: %d ; ip2: %d\n",m,ip2);
860 1.1 brad */
861 1.1 brad ip2 = count * ip2;
862 1.1 brad /*
863 1.1 brad printf("count: %d ; ip2: %d\n",count,ip2);
864 1.1 brad */
865 1.1 brad ip3 = (int64_t)((edges - 1) * (int64_t)100000000000) / (int64_t)ip2;
866 1.1 brad /*
867 1.1 brad printf("edges: %d ; ip3: %d\n",edges,ip3);
868 1.1 brad */
869 1.1 brad
870 1.1 brad irpm = (ip1 * ip3 * ftach * 60) / 100000000000;
871 1.1 brad
872 1.1 brad edata->value_cur = (uint32_t) irpm;
873 1.1 brad edata->state = ENVSYS_SVALID;
874 1.1 brad }
875 1.1 brad
876 1.1 brad /* These two tables are taken from Appendix A in the 2104 and 2106 datasheet.
877 1.1 brad * The index into the array is the ADC value and the value of the array is a
878 1.1 brad * precomputed kelvin1000 (i.e celcius to kelvin * 1000) temperature.
879 1.1 brad *
880 1.1 brad * There are unusual holes as not all of the ADC values are present in the
881 1.1 brad * *center* of the table these were made into xx.5 temperature values.
882 1.1 brad *
883 1.1 brad * Another quirk is that the table in the datasheets have multiple temperatures
884 1.1 brad * for a particular ADC. This behavior seems more common on the edges of the
885 1.1 brad * table and may make sense. What these tables do, is just take the first
886 1.1 brad * temperature for any ADC value.
887 1.1 brad *
888 1.1 brad */
889 1.1 brad
890 1.1 brad #define EMCFAN_VIN_NO_TEMP -1
891 1.1 brad
892 1.1 brad static const int32_t emcfan_vin_temps[] = {
893 1.1 brad EMCFAN_VIN_NO_TEMP,
894 1.1 brad EMCFAN_VIN_NO_TEMP,
895 1.1 brad EMCFAN_VIN_NO_TEMP,
896 1.1 brad EMCFAN_VIN_NO_TEMP,
897 1.1 brad EMCFAN_VIN_NO_TEMP,
898 1.1 brad EMCFAN_VIN_NO_TEMP,
899 1.1 brad EMCFAN_VIN_NO_TEMP,
900 1.1 brad EMCFAN_VIN_NO_TEMP,
901 1.1 brad EMCFAN_VIN_NO_TEMP,
902 1.1 brad EMCFAN_VIN_NO_TEMP,
903 1.1 brad EMCFAN_VIN_NO_TEMP,
904 1.1 brad EMCFAN_VIN_NO_TEMP,
905 1.1 brad EMCFAN_VIN_NO_TEMP,
906 1.1 brad EMCFAN_VIN_NO_TEMP,
907 1.1 brad EMCFAN_VIN_NO_TEMP,
908 1.1 brad EMCFAN_VIN_NO_TEMP,
909 1.1 brad EMCFAN_VIN_NO_TEMP,
910 1.1 brad EMCFAN_VIN_NO_TEMP,
911 1.1 brad EMCFAN_VIN_NO_TEMP,
912 1.1 brad EMCFAN_VIN_NO_TEMP,
913 1.1 brad EMCFAN_VIN_NO_TEMP,
914 1.1 brad EMCFAN_VIN_NO_TEMP,
915 1.1 brad EMCFAN_VIN_NO_TEMP,
916 1.1 brad EMCFAN_VIN_NO_TEMP,
917 1.1 brad EMCFAN_VIN_NO_TEMP,
918 1.1 brad EMCFAN_VIN_NO_TEMP,
919 1.1 brad EMCFAN_VIN_NO_TEMP,
920 1.1 brad EMCFAN_VIN_NO_TEMP,
921 1.1 brad EMCFAN_VIN_NO_TEMP,
922 1.1 brad EMCFAN_VIN_NO_TEMP,
923 1.1 brad EMCFAN_VIN_NO_TEMP,
924 1.1 brad EMCFAN_VIN_NO_TEMP,
925 1.1 brad 463150,
926 1.1 brad 461150,
927 1.1 brad 459150,
928 1.1 brad 457150,
929 1.1 brad 455150,
930 1.1 brad 453150,
931 1.1 brad 451150,
932 1.1 brad 450150,
933 1.1 brad 448150,
934 1.1 brad 446150,
935 1.1 brad 445150,
936 1.1 brad 443150,
937 1.1 brad 441150,
938 1.1 brad 440150,
939 1.1 brad 438150,
940 1.1 brad 437150,
941 1.1 brad 435150,
942 1.1 brad 434150,
943 1.1 brad 433150,
944 1.1 brad 431150,
945 1.1 brad 430150,
946 1.1 brad 429150,
947 1.1 brad 427150,
948 1.1 brad 426150,
949 1.1 brad 425150,
950 1.1 brad 424150,
951 1.1 brad 423150,
952 1.1 brad 421150,
953 1.1 brad 420150,
954 1.1 brad 419150,
955 1.1 brad 418150,
956 1.1 brad 417150,
957 1.1 brad 416150,
958 1.1 brad 415150,
959 1.1 brad 414150,
960 1.1 brad 413150,
961 1.1 brad 412150,
962 1.1 brad 411150,
963 1.1 brad 410150,
964 1.1 brad 409150,
965 1.1 brad 408150,
966 1.1 brad 407150,
967 1.1 brad 406150,
968 1.1 brad 405150,
969 1.1 brad 404150,
970 1.1 brad 403150,
971 1.1 brad 402150,
972 1.1 brad 398150,
973 1.1 brad 397150,
974 1.1 brad 396150,
975 1.1 brad 395150,
976 1.1 brad 394650,
977 1.1 brad 394150,
978 1.1 brad 393150,
979 1.1 brad 392150,
980 1.1 brad 391650,
981 1.1 brad 391150,
982 1.1 brad 390150,
983 1.1 brad 389150,
984 1.1 brad 388650,
985 1.1 brad 388150,
986 1.1 brad 387150,
987 1.1 brad 386650,
988 1.1 brad 386150,
989 1.1 brad 385150,
990 1.1 brad 384150,
991 1.1 brad 383650,
992 1.1 brad 383150,
993 1.1 brad 382150,
994 1.1 brad 381650,
995 1.1 brad 381150,
996 1.1 brad 380150,
997 1.1 brad 379650,
998 1.1 brad 379150,
999 1.1 brad 378150,
1000 1.1 brad 377650,
1001 1.1 brad 377150,
1002 1.1 brad 376650,
1003 1.1 brad 376150,
1004 1.1 brad 375150,
1005 1.1 brad 374650,
1006 1.1 brad 374150,
1007 1.1 brad 373150,
1008 1.1 brad 372650,
1009 1.1 brad 372150,
1010 1.1 brad 371650,
1011 1.1 brad 371150,
1012 1.1 brad 370150,
1013 1.1 brad 369650,
1014 1.1 brad 369150,
1015 1.1 brad 368650,
1016 1.1 brad 368150,
1017 1.1 brad 367150,
1018 1.1 brad 366650,
1019 1.1 brad 366150,
1020 1.1 brad 365650,
1021 1.1 brad 365150,
1022 1.1 brad 364150,
1023 1.1 brad 363650,
1024 1.1 brad 363150,
1025 1.1 brad 362650,
1026 1.1 brad 362150,
1027 1.1 brad 361650,
1028 1.1 brad 361150,
1029 1.1 brad 360150,
1030 1.1 brad 359650,
1031 1.1 brad 359150,
1032 1.1 brad 358150,
1033 1.1 brad 357650,
1034 1.1 brad 357150,
1035 1.1 brad 356650,
1036 1.1 brad 356150,
1037 1.1 brad 355650,
1038 1.1 brad 355150,
1039 1.1 brad 354150,
1040 1.1 brad 353650,
1041 1.1 brad 353150,
1042 1.1 brad 352650,
1043 1.1 brad 352150,
1044 1.1 brad 351650,
1045 1.1 brad 351150,
1046 1.1 brad 350650,
1047 1.1 brad 350150,
1048 1.1 brad 349150,
1049 1.1 brad 348650,
1050 1.1 brad 348150,
1051 1.1 brad 347650,
1052 1.1 brad 347150,
1053 1.1 brad 346150,
1054 1.1 brad 345650,
1055 1.1 brad 345150,
1056 1.1 brad 344650,
1057 1.1 brad 344150,
1058 1.1 brad 343650,
1059 1.1 brad 343150,
1060 1.1 brad 342150,
1061 1.1 brad 341650,
1062 1.1 brad 341150,
1063 1.1 brad 340650,
1064 1.1 brad 340150,
1065 1.1 brad 339150,
1066 1.1 brad 338650,
1067 1.1 brad 338150,
1068 1.1 brad 337650,
1069 1.1 brad 337150,
1070 1.1 brad 336150,
1071 1.1 brad 335650,
1072 1.1 brad 335150,
1073 1.1 brad 334650,
1074 1.1 brad 334150,
1075 1.1 brad 333150,
1076 1.1 brad 332650,
1077 1.1 brad 332150,
1078 1.1 brad 331150,
1079 1.1 brad 330650,
1080 1.1 brad 330150,
1081 1.1 brad 329650,
1082 1.1 brad 329150,
1083 1.1 brad 328150,
1084 1.1 brad 327650,
1085 1.1 brad 327150,
1086 1.1 brad 326150,
1087 1.1 brad 325650,
1088 1.1 brad 325150,
1089 1.1 brad 324150,
1090 1.1 brad 323650,
1091 1.1 brad 323150,
1092 1.1 brad 322150,
1093 1.1 brad 321150,
1094 1.1 brad 320650,
1095 1.1 brad 320150,
1096 1.1 brad 319150,
1097 1.1 brad 318650,
1098 1.1 brad 318150,
1099 1.1 brad 317150,
1100 1.1 brad 316150,
1101 1.1 brad 315150,
1102 1.1 brad 314650,
1103 1.1 brad 314150,
1104 1.1 brad 313150,
1105 1.1 brad 312150,
1106 1.1 brad 311150,
1107 1.1 brad 310650,
1108 1.1 brad 310150,
1109 1.1 brad 309150,
1110 1.1 brad 308150,
1111 1.1 brad 307150,
1112 1.1 brad 306150,
1113 1.1 brad 305150,
1114 1.1 brad 304150,
1115 1.1 brad 303150,
1116 1.1 brad 302150,
1117 1.1 brad 301150,
1118 1.1 brad 300150,
1119 1.1 brad 299150,
1120 1.1 brad 298150,
1121 1.1 brad 297150,
1122 1.1 brad 296150,
1123 1.1 brad 295150,
1124 1.1 brad 293150,
1125 1.1 brad 292150,
1126 1.1 brad 291150,
1127 1.1 brad 290150,
1128 1.1 brad 288150,
1129 1.1 brad 287150,
1130 1.1 brad 285150,
1131 1.1 brad 283150,
1132 1.1 brad 282150,
1133 1.1 brad 280150,
1134 1.1 brad 278150,
1135 1.1 brad 276150,
1136 1.1 brad 273150,
1137 1.1 brad 271150,
1138 1.1 brad 268150,
1139 1.1 brad 265150,
1140 1.1 brad 262150,
1141 1.1 brad 259150,
1142 1.1 brad 255150,
1143 1.1 brad 250150,
1144 1.1 brad 244150,
1145 1.1 brad 236150,
1146 1.1 brad 229150,
1147 1.1 brad 228150,
1148 1.1 brad EMCFAN_VIN_NO_TEMP
1149 1.1 brad };
1150 1.1 brad
1151 1.1 brad static const int32_t emcfan_vin_temps_i[] = {
1152 1.1 brad 228150,
1153 1.1 brad 229150,
1154 1.1 brad 236150,
1155 1.1 brad 244150,
1156 1.1 brad 250150,
1157 1.1 brad 255150,
1158 1.1 brad 259150,
1159 1.1 brad 262150,
1160 1.1 brad 265150,
1161 1.1 brad 268150,
1162 1.1 brad 271150,
1163 1.1 brad 273150,
1164 1.1 brad 276150,
1165 1.1 brad 278150,
1166 1.1 brad 280150,
1167 1.1 brad 281150,
1168 1.1 brad 283150,
1169 1.1 brad 285150,
1170 1.1 brad 286150,
1171 1.1 brad 288150,
1172 1.1 brad 289150,
1173 1.1 brad 291150,
1174 1.1 brad 292150,
1175 1.1 brad 293150,
1176 1.1 brad 295150,
1177 1.1 brad 296150,
1178 1.1 brad 297150,
1179 1.1 brad 298150,
1180 1.1 brad 299150,
1181 1.1 brad 300150,
1182 1.1 brad 301150,
1183 1.1 brad 302150,
1184 1.1 brad 303150,
1185 1.1 brad 304150,
1186 1.1 brad 305150,
1187 1.1 brad 306150,
1188 1.1 brad 307150,
1189 1.1 brad 308150,
1190 1.1 brad 309150,
1191 1.1 brad 310150,
1192 1.1 brad 310650,
1193 1.1 brad 311150,
1194 1.1 brad 312150,
1195 1.1 brad 313150,
1196 1.1 brad 314150,
1197 1.1 brad 314650,
1198 1.1 brad 315150,
1199 1.1 brad 316150,
1200 1.1 brad 317150,
1201 1.1 brad 317650,
1202 1.1 brad 318150,
1203 1.1 brad 319150,
1204 1.1 brad 320150,
1205 1.1 brad 320650,
1206 1.1 brad 321150,
1207 1.1 brad 322150,
1208 1.1 brad 323150,
1209 1.1 brad 323650,
1210 1.1 brad 324150,
1211 1.1 brad 325150,
1212 1.1 brad 325650,
1213 1.1 brad 326150,
1214 1.1 brad 327150,
1215 1.1 brad 327650,
1216 1.1 brad 328150,
1217 1.1 brad 329150,
1218 1.1 brad 329650,
1219 1.1 brad 330150,
1220 1.1 brad 330650,
1221 1.1 brad 331150,
1222 1.1 brad 332150,
1223 1.1 brad 332650,
1224 1.1 brad 333150,
1225 1.1 brad 334150,
1226 1.1 brad 334650,
1227 1.1 brad 335150,
1228 1.1 brad 335650,
1229 1.1 brad 336150,
1230 1.1 brad 337150,
1231 1.1 brad 337650,
1232 1.1 brad 338150,
1233 1.1 brad 338650,
1234 1.1 brad 339150,
1235 1.1 brad 340150,
1236 1.1 brad 340650,
1237 1.1 brad 341150,
1238 1.1 brad 341650,
1239 1.1 brad 342150,
1240 1.1 brad 343150,
1241 1.1 brad 343650,
1242 1.1 brad 344150,
1243 1.1 brad 344650,
1244 1.1 brad 345150,
1245 1.1 brad 345650,
1246 1.1 brad 346150,
1247 1.1 brad 347150,
1248 1.1 brad 347650,
1249 1.1 brad 348150,
1250 1.1 brad 348650,
1251 1.1 brad 349150,
1252 1.1 brad 350150,
1253 1.1 brad 350650,
1254 1.1 brad 351150,
1255 1.1 brad 351650,
1256 1.1 brad 352150,
1257 1.1 brad 352650,
1258 1.1 brad 353150,
1259 1.1 brad 353650,
1260 1.1 brad 354150,
1261 1.1 brad 355150,
1262 1.1 brad 355650,
1263 1.1 brad 356150,
1264 1.1 brad 356650,
1265 1.1 brad 357150,
1266 1.1 brad 357650,
1267 1.1 brad 358150,
1268 1.1 brad 359150,
1269 1.1 brad 359650,
1270 1.1 brad 360150,
1271 1.1 brad 360650,
1272 1.1 brad 361150,
1273 1.1 brad 362150,
1274 1.1 brad 362650,
1275 1.1 brad 363150,
1276 1.1 brad 363650,
1277 1.1 brad 364150,
1278 1.1 brad 365150,
1279 1.1 brad 365650,
1280 1.1 brad 366150,
1281 1.1 brad 366650,
1282 1.1 brad 367150,
1283 1.1 brad 368150,
1284 1.1 brad 368650,
1285 1.1 brad 369150,
1286 1.1 brad 369650,
1287 1.1 brad 370150,
1288 1.1 brad 371150,
1289 1.1 brad 371650,
1290 1.1 brad 372150,
1291 1.1 brad 372650,
1292 1.1 brad 373150,
1293 1.1 brad 374150,
1294 1.1 brad 374650,
1295 1.1 brad 375150,
1296 1.1 brad 376150,
1297 1.1 brad 376650,
1298 1.1 brad 377150,
1299 1.1 brad 377650,
1300 1.1 brad 378150,
1301 1.1 brad 379150,
1302 1.1 brad 379650,
1303 1.1 brad 380150,
1304 1.1 brad 381150,
1305 1.1 brad 381650,
1306 1.1 brad 382150,
1307 1.1 brad 383150,
1308 1.1 brad 383650,
1309 1.1 brad 384150,
1310 1.1 brad 385150,
1311 1.1 brad 386150,
1312 1.1 brad 386650,
1313 1.1 brad 387150,
1314 1.1 brad 388150,
1315 1.1 brad 388650,
1316 1.1 brad 389150,
1317 1.1 brad 390150,
1318 1.1 brad 391150,
1319 1.1 brad 391650,
1320 1.1 brad 392150,
1321 1.1 brad 393150,
1322 1.1 brad 394150,
1323 1.1 brad 394650,
1324 1.1 brad 395150,
1325 1.1 brad 396150,
1326 1.1 brad 397150,
1327 1.1 brad 398150,
1328 1.1 brad 402150,
1329 1.1 brad 403150,
1330 1.1 brad 404150,
1331 1.1 brad 405150,
1332 1.1 brad 406150,
1333 1.1 brad 407150,
1334 1.1 brad 408150,
1335 1.1 brad 409150,
1336 1.1 brad 410150,
1337 1.1 brad 411150,
1338 1.1 brad 412150,
1339 1.1 brad 413150,
1340 1.1 brad 414150,
1341 1.1 brad 415150,
1342 1.1 brad 416150,
1343 1.1 brad 417150,
1344 1.1 brad 418150,
1345 1.1 brad 419150,
1346 1.1 brad 420150,
1347 1.1 brad 421150,
1348 1.1 brad 423150,
1349 1.1 brad 424150,
1350 1.1 brad 425150,
1351 1.1 brad 426150,
1352 1.1 brad 427150,
1353 1.1 brad 429150,
1354 1.1 brad 430150,
1355 1.1 brad 431150,
1356 1.1 brad 433150,
1357 1.1 brad 434150,
1358 1.1 brad 435150,
1359 1.1 brad 437150,
1360 1.1 brad 438150,
1361 1.1 brad 440150,
1362 1.1 brad 441150,
1363 1.1 brad 443150,
1364 1.1 brad 445150,
1365 1.1 brad 446150,
1366 1.1 brad 448150,
1367 1.1 brad 450150,
1368 1.1 brad 451150,
1369 1.1 brad 453150,
1370 1.1 brad 455150,
1371 1.1 brad 457150,
1372 1.1 brad 459150,
1373 1.1 brad 461150,
1374 1.1 brad 463150,
1375 1.1 brad EMCFAN_VIN_NO_TEMP,
1376 1.1 brad EMCFAN_VIN_NO_TEMP,
1377 1.1 brad EMCFAN_VIN_NO_TEMP,
1378 1.1 brad EMCFAN_VIN_NO_TEMP,
1379 1.1 brad EMCFAN_VIN_NO_TEMP,
1380 1.1 brad EMCFAN_VIN_NO_TEMP,
1381 1.1 brad EMCFAN_VIN_NO_TEMP,
1382 1.1 brad EMCFAN_VIN_NO_TEMP,
1383 1.1 brad EMCFAN_VIN_NO_TEMP,
1384 1.1 brad EMCFAN_VIN_NO_TEMP,
1385 1.1 brad EMCFAN_VIN_NO_TEMP,
1386 1.1 brad EMCFAN_VIN_NO_TEMP,
1387 1.1 brad EMCFAN_VIN_NO_TEMP,
1388 1.1 brad EMCFAN_VIN_NO_TEMP,
1389 1.1 brad EMCFAN_VIN_NO_TEMP,
1390 1.1 brad EMCFAN_VIN_NO_TEMP,
1391 1.1 brad EMCFAN_VIN_NO_TEMP,
1392 1.1 brad EMCFAN_VIN_NO_TEMP,
1393 1.1 brad EMCFAN_VIN_NO_TEMP,
1394 1.1 brad EMCFAN_VIN_NO_TEMP,
1395 1.1 brad EMCFAN_VIN_NO_TEMP,
1396 1.1 brad EMCFAN_VIN_NO_TEMP,
1397 1.1 brad EMCFAN_VIN_NO_TEMP,
1398 1.1 brad EMCFAN_VIN_NO_TEMP,
1399 1.1 brad EMCFAN_VIN_NO_TEMP,
1400 1.1 brad EMCFAN_VIN_NO_TEMP,
1401 1.1 brad EMCFAN_VIN_NO_TEMP,
1402 1.1 brad EMCFAN_VIN_NO_TEMP,
1403 1.1 brad EMCFAN_VIN_NO_TEMP,
1404 1.1 brad EMCFAN_VIN_NO_TEMP,
1405 1.1 brad EMCFAN_VIN_NO_TEMP,
1406 1.1 brad EMCFAN_VIN_NO_TEMP,
1407 1.1 brad EMCFAN_VIN_NO_TEMP
1408 1.1 brad };
1409 1.1 brad
1410 1.1 brad static void
1411 1.1 brad emcfan_refresh_temp(int product_family, uint8_t product_id,
1412 1.1 brad struct sysmon_envsys *sme, envsys_data_t *edata, int instance)
1413 1.1 brad {
1414 1.1 brad struct emcfan_sc *sc = sme->sme_cookie;
1415 1.1 brad
1416 1.1 brad int error;
1417 1.1 brad uint8_t temp_config;
1418 1.1 brad uint8_t raw_temp_config_3;
1419 1.1 brad uint8_t temp_config_3;
1420 1.1 brad uint8_t temp_high;
1421 1.1 brad uint8_t temp_low;
1422 1.1 brad uint8_t external_temp_high_reg;
1423 1.1 brad uint8_t external_temp_low_reg;
1424 1.1 brad bool is_internal = false;
1425 1.1 brad bool is_vin4 = false;
1426 1.1 brad bool using_apd = false;
1427 1.1 brad bool using_vin = false;
1428 1.1 brad bool inverted = false;
1429 1.1 brad
1430 1.1 brad is_internal = sc->sc_sensor_instances[instance].sc_i_flags & EMCFAN_INTERNAL_TEMP;
1431 1.1 brad is_vin4 = sc->sc_sensor_instances[instance].sc_i_flags & EMCFAN_VIN4_TEMP;
1432 1.1 brad
1433 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
1434 1.1 brad if (error) {
1435 1.1 brad device_printf(sc->sc_dev,"%s: could not acquire I2C bus: %d\n",__func__, error);
1436 1.1 brad return;
1437 1.1 brad }
1438 1.1 brad
1439 1.1 brad if (is_internal) {
1440 1.1 brad /* There might be a data interlock thing going on with the high and low
1441 1.1 brad * registers, to make sure, read the high one first. This works in the
1442 1.1 brad * opposite of the tach.
1443 1.1 brad */
1444 1.1 brad error = emcfan_read_register(sc, EMCFAN_INTERNAL_TEMP_HIGH, &temp_high);
1445 1.1 brad if (error) {
1446 1.1 brad device_printf(sc->sc_dev,"%s: could not read internal temp high: %d\n",__func__, error);
1447 1.1 brad iic_release_bus(sc->sc_tag, 0);
1448 1.1 brad return;
1449 1.1 brad }
1450 1.1 brad /* The 2101 does not have fractions on the internal temperature sensor */
1451 1.1 brad if (product_id == EMCFAN_PRODUCT_2101) {
1452 1.1 brad temp_low = 0;
1453 1.1 brad } else {
1454 1.1 brad error = emcfan_read_register(sc, EMCFAN_INTERNAL_TEMP_LOW, &temp_low);
1455 1.1 brad if (error) {
1456 1.1 brad device_printf(sc->sc_dev,"%s: could not read internal temp low: %d\n",__func__, error);
1457 1.1 brad iic_release_bus(sc->sc_tag, 0);
1458 1.1 brad return;
1459 1.1 brad }
1460 1.1 brad }
1461 1.1 brad } else {
1462 1.1 brad if (is_vin4) {
1463 1.1 brad if (sc->sc_vin4_temp) {
1464 1.1 brad using_vin = true;
1465 1.1 brad
1466 1.1 brad error = emcfan_read_register(sc, EMCFAN_CHIP_CONFIG, &temp_config);
1467 1.1 brad if (error) {
1468 1.1 brad device_printf(sc->sc_dev,"%s: could not read chip config register: %d\n",__func__, error);
1469 1.1 brad iic_release_bus(sc->sc_tag, 0);
1470 1.1 brad return;
1471 1.1 brad }
1472 1.1 brad inverted = temp_config & 0x80;
1473 1.1 brad
1474 1.1 brad error = emcfan_read_register(sc, EMCFAN_VIN4_VOLTAGE, &temp_high);
1475 1.1 brad if (error) {
1476 1.1 brad device_printf(sc->sc_dev,"%s: could not read external temp high: %d\n",__func__, error);
1477 1.1 brad iic_release_bus(sc->sc_tag, 0);
1478 1.1 brad return;
1479 1.1 brad }
1480 1.1 brad } else {
1481 1.1 brad iic_release_bus(sc->sc_tag, 0);
1482 1.1 brad return;
1483 1.1 brad }
1484 1.1 brad } else {
1485 1.1 brad /* The 2101 has its external sensor on a different set of registers
1486 1.1 brad * than the rest.
1487 1.1 brad */
1488 1.1 brad if (product_id == EMCFAN_PRODUCT_2101) {
1489 1.1 brad error = emcfan_read_register(sc, EMCFAN_2101_EXTERNAL_TEMP_LOW, &temp_low);
1490 1.1 brad if (error) {
1491 1.1 brad device_printf(sc->sc_dev,"%s: could not read external temp low: %d\n",__func__, error);
1492 1.1 brad iic_release_bus(sc->sc_tag, 0);
1493 1.1 brad return;
1494 1.1 brad }
1495 1.1 brad error = emcfan_read_register(sc, EMCFAN_2101_EXTERNAL_TEMP_HIGH, &temp_high);
1496 1.1 brad if (error) {
1497 1.1 brad device_printf(sc->sc_dev,"%s: could not read external temp high: %d\n",__func__, error);
1498 1.1 brad iic_release_bus(sc->sc_tag, 0);
1499 1.1 brad return;
1500 1.1 brad }
1501 1.1 brad } else {
1502 1.1 brad switch(sc->sc_sensor_instances[instance].sc_i_member) {
1503 1.1 brad case 1:
1504 1.1 brad external_temp_high_reg = EMCFAN_EXTERNAL_1_TEMP_HIGH;
1505 1.1 brad external_temp_low_reg = EMCFAN_EXTERNAL_1_TEMP_LOW;
1506 1.1 brad break;
1507 1.1 brad case 2:
1508 1.1 brad external_temp_high_reg = EMCFAN_EXTERNAL_2_TEMP_HIGH;
1509 1.1 brad external_temp_low_reg = EMCFAN_EXTERNAL_2_TEMP_LOW;
1510 1.1 brad break;
1511 1.1 brad case 3:
1512 1.1 brad external_temp_high_reg = EMCFAN_EXTERNAL_3_TEMP_HIGH;
1513 1.1 brad external_temp_low_reg = EMCFAN_EXTERNAL_3_TEMP_LOW;
1514 1.1 brad break;
1515 1.1 brad case 4:
1516 1.1 brad external_temp_high_reg = EMCFAN_EXTERNAL_4_TEMP_HIGH;
1517 1.1 brad external_temp_low_reg = EMCFAN_EXTERNAL_4_TEMP_LOW;
1518 1.1 brad break;
1519 1.1 brad default:
1520 1.1 brad panic("Unknown member: %d\n",
1521 1.1 brad sc->sc_sensor_instances[instance].sc_i_member);
1522 1.1 brad break;
1523 1.1 brad };
1524 1.1 brad
1525 1.1 brad /* The 2103-2, 2103-4, 2104 and 2106 can use APD mode. This is a method
1526 1.1 brad * of using two sensors in parallel on a single set of pins. The way one
1527 1.1 brad * wires this up is in the datasheets for the chip.
1528 1.1 brad */
1529 1.1 brad
1530 1.1 brad if (product_id == EMCFAN_PRODUCT_2103_24 ||
1531 1.1 brad product_id == EMCFAN_PRODUCT_2104 ||
1532 1.1 brad product_id == EMCFAN_PRODUCT_2106) {
1533 1.1 brad error = emcfan_read_register(sc, EMCFAN_CHIP_CONFIG, &temp_config);
1534 1.1 brad if (error) {
1535 1.1 brad device_printf(sc->sc_dev,"%s: could not read chip config register: %d\n",__func__, error);
1536 1.1 brad iic_release_bus(sc->sc_tag, 0);
1537 1.1 brad return;
1538 1.1 brad }
1539 1.1 brad
1540 1.1 brad using_apd = temp_config & 0x01;
1541 1.1 brad }
1542 1.1 brad
1543 1.1 brad /* The 2104, 2105 and 2106 has some other special abilities, such as being
1544 1.1 brad * able to use thermistors.
1545 1.1 brad */
1546 1.1 brad
1547 1.1 brad if (product_id == EMCFAN_PRODUCT_2104 ||
1548 1.1 brad product_id == EMCFAN_PRODUCT_2106) {
1549 1.1 brad error = emcfan_read_register(sc, EMCFAN_TEMP_CONFIG_3, &raw_temp_config_3);
1550 1.1 brad if (error) {
1551 1.1 brad device_printf(sc->sc_dev,"%s: could not read temperature config register: %d\n",__func__, error);
1552 1.1 brad iic_release_bus(sc->sc_tag, 0);
1553 1.1 brad return;
1554 1.1 brad }
1555 1.1 brad switch(sc->sc_sensor_instances[instance].sc_i_member) {
1556 1.1 brad case 1:
1557 1.1 brad temp_config_3 = raw_temp_config_3 & 0x03;
1558 1.1 brad break;
1559 1.1 brad case 2:
1560 1.1 brad temp_config_3 = raw_temp_config_3 >> 2;
1561 1.1 brad temp_config_3 = temp_config_3 & 0x03;
1562 1.1 brad break;
1563 1.1 brad case 3:
1564 1.1 brad temp_config_3 = raw_temp_config_3 >> 4;
1565 1.1 brad temp_config_3 = temp_config_3 & 0x03;
1566 1.1 brad break;
1567 1.1 brad default:
1568 1.1 brad temp_config_3 = 0;
1569 1.1 brad break;
1570 1.1 brad };
1571 1.1 brad
1572 1.1 brad using_vin = temp_config_3 & 0x02;
1573 1.1 brad inverted = temp_config_3 & 0x01;
1574 1.1 brad
1575 1.1 brad
1576 1.1 brad /* there is a strange situation if sensor 3 is being used as a VIN
1577 1.1 brad * sensor, then sensor 4 is not available at all. Note that this
1578 1.1 brad * sensor 4 is *NOT* the sensor that might be attached to the VIN4
1579 1.1 brad * pin.
1580 1.1 brad */
1581 1.1 brad
1582 1.1 brad if (sc->sc_sensor_instances[instance].sc_i_member == 4 &&
1583 1.1 brad raw_temp_config_3 & 0x20) {
1584 1.1 brad iic_release_bus(sc->sc_tag, 0);
1585 1.1 brad return;
1586 1.1 brad }
1587 1.1 brad }
1588 1.1 brad
1589 1.1 brad if (product_id == EMCFAN_PRODUCT_2103_24) {
1590 1.1 brad /* The anti-parallel mode, apd, must be enabled before sensor 3 will
1591 1.1 brad * be available.
1592 1.1 brad */
1593 1.1 brad if (!using_apd &&
1594 1.1 brad sc->sc_sensor_instances[instance].sc_i_member == 3) {
1595 1.1 brad iic_release_bus(sc->sc_tag, 0);
1596 1.1 brad return;
1597 1.1 brad }
1598 1.1 brad }
1599 1.1 brad
1600 1.1 brad if (product_id == EMCFAN_PRODUCT_2104 ||
1601 1.1 brad product_id == EMCFAN_PRODUCT_2106) {
1602 1.1 brad /* The anti-parallel mode, apd, must be enabled before sensor 4 will
1603 1.1 brad * be available. This, of course, might conflict if sensor 3 is a VIN
1604 1.1 brad * sensor. Don't do that....
1605 1.1 brad */
1606 1.1 brad if (!using_apd &&
1607 1.1 brad sc->sc_sensor_instances[instance].sc_i_member == 4) {
1608 1.1 brad iic_release_bus(sc->sc_tag, 0);
1609 1.1 brad return;
1610 1.1 brad }
1611 1.1 brad }
1612 1.1 brad
1613 1.1 brad /* There is a data interlock thing going on with the high and low
1614 1.1 brad * registers, but it works the opposite from the tach.
1615 1.1 brad */
1616 1.1 brad
1617 1.1 brad error = emcfan_read_register(sc, external_temp_high_reg, &temp_high);
1618 1.1 brad if (error) {
1619 1.1 brad device_printf(sc->sc_dev,"%s: could not read external temp high: %d\n",__func__, error);
1620 1.1 brad iic_release_bus(sc->sc_tag, 0);
1621 1.1 brad return;
1622 1.1 brad }
1623 1.1 brad if (using_vin) {
1624 1.1 brad temp_low = 0;
1625 1.1 brad } else {
1626 1.1 brad error = emcfan_read_register(sc, external_temp_low_reg, &temp_low);
1627 1.1 brad if (error) {
1628 1.1 brad device_printf(sc->sc_dev,"%s: could not read external temp low: %d\n",__func__, error);
1629 1.1 brad iic_release_bus(sc->sc_tag, 0);
1630 1.1 brad return;
1631 1.1 brad }
1632 1.1 brad }
1633 1.1 brad }
1634 1.1 brad }
1635 1.1 brad }
1636 1.1 brad
1637 1.1 brad iic_release_bus(sc->sc_tag, 0);
1638 1.1 brad
1639 1.1 brad /* It appears that on the 2101, if the high byte is 0x7f and the low byte is 0,\
1640 1.1 brad * then there is a fault or no sensor.
1641 1.1 brad */
1642 1.1 brad
1643 1.1 brad if (product_id == EMCFAN_PRODUCT_2101 &&
1644 1.1 brad !is_internal) {
1645 1.1 brad if (temp_high == 0x7f &&
1646 1.1 brad temp_low == 0) {
1647 1.1 brad return;
1648 1.1 brad }
1649 1.1 brad }
1650 1.1 brad
1651 1.1 brad /* For everyone else, if the external sensor read 0x80 on the high byte and
1652 1.1 brad * the fraction is 0, then there is a fault or no sensor.
1653 1.1 brad */
1654 1.1 brad
1655 1.1 brad if (!is_internal && !using_vin) {
1656 1.1 brad if (temp_high == 0x80 &&
1657 1.1 brad (temp_low >> 5) == 0x00) {
1658 1.1 brad return;
1659 1.1 brad }
1660 1.1 brad }
1661 1.1 brad
1662 1.1 brad int32_t kelvin1000 = 0;
1663 1.1 brad int32_t frac = 0;
1664 1.1 brad uint8_t tl;
1665 1.1 brad
1666 1.1 brad if (!using_vin) {
1667 1.1 brad kelvin1000 = (int8_t)temp_high * 1000;
1668 1.1 brad tl = temp_low >> 5;
1669 1.1 brad if (temp_high & 0x80) {
1670 1.1 brad tl = (~tl) & 0x07;
1671 1.1 brad tl++;
1672 1.1 brad }
1673 1.1 brad frac = 125 * tl;
1674 1.1 brad if (temp_high & 0x80) {
1675 1.1 brad kelvin1000 -= frac;
1676 1.1 brad } else {
1677 1.1 brad kelvin1000 += frac;
1678 1.1 brad }
1679 1.1 brad kelvin1000 += 273150;
1680 1.1 brad } else {
1681 1.1 brad int32_t vin1000 = EMCFAN_VIN_NO_TEMP;
1682 1.1 brad
1683 1.1 brad if (inverted) {
1684 1.1 brad if (emcfan_vin_temps_i[temp_high] != EMCFAN_VIN_NO_TEMP) {
1685 1.1 brad vin1000 = emcfan_vin_temps_i[temp_high];
1686 1.1 brad }
1687 1.1 brad } else {
1688 1.1 brad if (emcfan_vin_temps[temp_high] != EMCFAN_VIN_NO_TEMP) {
1689 1.1 brad vin1000 = emcfan_vin_temps[temp_high];
1690 1.1 brad }
1691 1.1 brad }
1692 1.1 brad
1693 1.1 brad if (vin1000 != EMCFAN_VIN_NO_TEMP)
1694 1.1 brad kelvin1000 = vin1000;
1695 1.1 brad else
1696 1.1 brad return;
1697 1.1 brad }
1698 1.1 brad
1699 1.1 brad edata->value_cur = (uint32_t) kelvin1000 * 1000;
1700 1.1 brad edata->state = ENVSYS_SVALID;
1701 1.1 brad }
1702 1.1 brad
1703 1.1 brad static void
1704 1.1 brad emcfan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
1705 1.1 brad {
1706 1.1 brad struct emcfan_sc *sc = sme->sme_cookie;
1707 1.1 brad int instance = -1;
1708 1.1 brad
1709 1.1 brad /* Hunt down the instance for this sensor.
1710 1.1 brad *
1711 1.1 brad * The type is held in sc_sensors, but the actual hardware
1712 1.1 brad * instance is held in sc_sensor_instances in the member
1713 1.1 brad * field. It would be nice if the edata structure had a
1714 1.1 brad * field that could be used as an opaque value.
1715 1.1 brad */
1716 1.1 brad for(int i = 0;i < EMCFAN_NUM_SENSORS;i++)
1717 1.1 brad if (sc->sc_sensor_instances[i].sc_i_envnum == edata->sensor) {
1718 1.1 brad instance = i;
1719 1.1 brad break;
1720 1.1 brad }
1721 1.1 brad
1722 1.1 brad KASSERT(instance > -1);
1723 1.1 brad
1724 1.1 brad DPRINTF(sc, 2, ("%s: using sensor instance %d\n", __func__,
1725 1.1 brad instance));
1726 1.1 brad
1727 1.1 brad edata->state = ENVSYS_SINVALID;
1728 1.1 brad
1729 1.1 brad /* Unlike manor of the refresh functions in other drivers, this
1730 1.1 brad * one will select the sensor based upon the type and instance.
1731 1.1 brad *
1732 1.1 brad * Due to the fact that the order will vary depending on which
1733 1.1 brad * chip you are using.
1734 1.1 brad */
1735 1.1 brad
1736 1.1 brad switch(edata->units) {
1737 1.1 brad case ENVSYS_SFANRPM:
1738 1.1 brad switch(emcfan_chip_infos[sc->sc_info_index].family) {
1739 1.1 brad case EMCFAN_FAMILY_210X:
1740 1.1 brad switch(emcfan_chip_infos[sc->sc_info_index].product_id) {
1741 1.1 brad case EMCFAN_PRODUCT_2101:
1742 1.1 brad emcfan_refresh_2101_tach(sme, edata, instance);
1743 1.1 brad break;
1744 1.1 brad /* 2103, 2104 and 2106 use nearly the same algorithm as the 230x family */
1745 1.1 brad default:
1746 1.1 brad emcfan_refresh_210_346_230x_tach(emcfan_chip_infos[sc->sc_info_index].family,
1747 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id,
1748 1.1 brad sme, edata, instance);
1749 1.1 brad break;
1750 1.1 brad };
1751 1.1 brad break;
1752 1.1 brad case EMCFAN_FAMILY_230X:
1753 1.1 brad emcfan_refresh_210_346_230x_tach(emcfan_chip_infos[sc->sc_info_index].family,
1754 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id,
1755 1.1 brad sme, edata, instance);
1756 1.1 brad break;
1757 1.1 brad default:
1758 1.1 brad panic("Unknown family: %d\n",emcfan_chip_infos[sc->sc_info_index].family);
1759 1.1 brad break;
1760 1.1 brad }
1761 1.1 brad break;
1762 1.1 brad case ENVSYS_STEMP:
1763 1.1 brad emcfan_refresh_temp(emcfan_chip_infos[sc->sc_info_index].family,
1764 1.1 brad emcfan_chip_infos[sc->sc_info_index].product_id,
1765 1.1 brad sme, edata, instance);
1766 1.1 brad break;
1767 1.1 brad default:
1768 1.1 brad panic("Unknown edata units value: %d\n",edata->units);
1769 1.1 brad break;
1770 1.1 brad };
1771 1.1 brad }
1772 1.1 brad
1773 1.1 brad static int
1774 1.1 brad emcfanopen(dev_t dev, int flags, int fmt, struct lwp *l)
1775 1.1 brad {
1776 1.1 brad struct emcfan_sc *sc;
1777 1.1 brad
1778 1.1 brad sc = device_lookup_private(&emcfan_cd, minor(dev));
1779 1.1 brad if (!sc)
1780 1.1 brad return ENXIO;
1781 1.1 brad
1782 1.1 brad if (sc->sc_opened)
1783 1.1 brad return EBUSY;
1784 1.1 brad
1785 1.1 brad mutex_enter(&sc->sc_mutex);
1786 1.1 brad sc->sc_opened = true;
1787 1.1 brad mutex_exit(&sc->sc_mutex);
1788 1.1 brad
1789 1.1 brad return 0;
1790 1.1 brad }
1791 1.1 brad
1792 1.1 brad static int
1793 1.1 brad emcfanread(dev_t dev, struct uio *uio, int flags)
1794 1.1 brad {
1795 1.1 brad struct emcfan_sc *sc;
1796 1.1 brad int error;
1797 1.1 brad
1798 1.1 brad if ((sc = device_lookup_private(&emcfan_cd, minor(dev))) == NULL)
1799 1.1 brad return ENXIO;
1800 1.1 brad
1801 1.1 brad /* We do not make this an error. There is nothing wrong with running
1802 1.1 brad * off the end here, just return EOF.
1803 1.1 brad */
1804 1.1 brad if (uio->uio_offset > 0xff)
1805 1.1 brad return 0;
1806 1.1 brad
1807 1.1 brad if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
1808 1.1 brad return error;
1809 1.1 brad
1810 1.1 brad while (uio->uio_resid &&
1811 1.1 brad uio->uio_offset <= 0xff &&
1812 1.1 brad !sc->sc_dying) {
1813 1.1 brad uint8_t buf;
1814 1.1 brad int reg_addr = uio->uio_offset;
1815 1.1 brad
1816 1.1 brad if ((error = emcfan_read_register(sc, reg_addr, &buf)) != 0) {
1817 1.1 brad iic_release_bus(sc->sc_tag, 0);
1818 1.1 brad aprint_error_dev(sc->sc_dev,
1819 1.1 brad "%s: read failed at 0x%02x: %d\n",
1820 1.1 brad __func__, reg_addr, error);
1821 1.1 brad return error;
1822 1.1 brad }
1823 1.1 brad
1824 1.1 brad if (sc->sc_dying)
1825 1.1 brad break;
1826 1.1 brad
1827 1.1 brad if ((error = uiomove(&buf, 1, uio)) != 0) {
1828 1.1 brad iic_release_bus(sc->sc_tag, 0);
1829 1.1 brad return error;
1830 1.1 brad }
1831 1.1 brad }
1832 1.1 brad
1833 1.1 brad iic_release_bus(sc->sc_tag, 0);
1834 1.1 brad
1835 1.1 brad if (sc->sc_dying) {
1836 1.1 brad return EIO;
1837 1.1 brad }
1838 1.1 brad
1839 1.1 brad return 0;
1840 1.1 brad }
1841 1.1 brad
1842 1.1 brad static int
1843 1.1 brad emcfanwrite(dev_t dev, struct uio *uio, int flags)
1844 1.1 brad {
1845 1.1 brad struct emcfan_sc *sc;
1846 1.1 brad int error;
1847 1.1 brad
1848 1.1 brad if ((sc = device_lookup_private(&emcfan_cd, minor(dev))) == NULL)
1849 1.1 brad return ENXIO;
1850 1.1 brad
1851 1.1 brad /* Same thing as read, this is not considered an error */
1852 1.1 brad if (uio->uio_offset > 0xff)
1853 1.1 brad return 0;
1854 1.1 brad
1855 1.1 brad if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
1856 1.1 brad return error;
1857 1.1 brad
1858 1.1 brad while (uio->uio_resid &&
1859 1.1 brad uio->uio_offset <= 0xff &&
1860 1.1 brad !sc->sc_dying) {
1861 1.1 brad uint8_t buf;
1862 1.1 brad int reg_addr = uio->uio_offset;
1863 1.1 brad
1864 1.1 brad if ((error = uiomove(&buf, 1, uio)) != 0)
1865 1.1 brad break;
1866 1.1 brad
1867 1.1 brad if (sc->sc_dying)
1868 1.1 brad break;
1869 1.1 brad
1870 1.1 brad if ((error = emcfan_write_register(sc, (uint8_t)reg_addr, buf)) != 0) {
1871 1.1 brad iic_release_bus(sc->sc_tag, 0);
1872 1.1 brad aprint_error_dev(sc->sc_dev,
1873 1.1 brad "%s: write failed at 0x%02x: %d\n",
1874 1.1 brad __func__, reg_addr, error);
1875 1.1 brad return error;
1876 1.1 brad }
1877 1.1 brad }
1878 1.1 brad
1879 1.1 brad iic_release_bus(sc->sc_tag, 0);
1880 1.1 brad
1881 1.1 brad if (sc->sc_dying) {
1882 1.1 brad return EIO;
1883 1.1 brad }
1884 1.1 brad
1885 1.1 brad return error;
1886 1.1 brad }
1887 1.1 brad
1888 1.1 brad static int
1889 1.1 brad emcfanclose(dev_t dev, int flags, int fmt, struct lwp *l)
1890 1.1 brad {
1891 1.1 brad struct emcfan_sc *sc;
1892 1.1 brad
1893 1.1 brad sc = device_lookup_private(&emcfan_cd, minor(dev));
1894 1.1 brad
1895 1.1 brad mutex_enter(&sc->sc_mutex);
1896 1.1 brad sc->sc_opened = false;
1897 1.1 brad mutex_exit(&sc->sc_mutex);
1898 1.1 brad
1899 1.1 brad return(0);
1900 1.1 brad }
1901 1.1 brad
1902 1.1 brad static int
1903 1.1 brad emcfan_detach(device_t self, int flags)
1904 1.1 brad {
1905 1.1 brad int err;
1906 1.1 brad struct emcfan_sc *sc;
1907 1.1 brad
1908 1.1 brad sc = device_private(self);
1909 1.1 brad
1910 1.1 brad mutex_enter(&sc->sc_mutex);
1911 1.1 brad sc->sc_dying = true;
1912 1.1 brad
1913 1.1 brad err = config_detach_children(self, flags);
1914 1.1 brad if (err)
1915 1.1 brad return err;
1916 1.1 brad
1917 1.1 brad /* Remove the sysctl tree */
1918 1.1 brad if (sc->sc_emcfanlog != NULL)
1919 1.1 brad sysctl_teardown(&sc->sc_emcfanlog);
1920 1.1 brad
1921 1.1 brad /* Remove the sensors */
1922 1.1 brad if (sc->sc_sme != NULL) {
1923 1.1 brad sysmon_envsys_unregister(sc->sc_sme);
1924 1.1 brad sc->sc_sme = NULL;
1925 1.1 brad }
1926 1.1 brad
1927 1.1 brad mutex_exit(&sc->sc_mutex);
1928 1.1 brad
1929 1.1 brad mutex_destroy(&sc->sc_mutex);
1930 1.1 brad
1931 1.1 brad return 0;
1932 1.1 brad }
1933 1.1 brad
1934 1.1 brad int
1935 1.1 brad emcfan_activate(device_t self, enum devact act)
1936 1.1 brad {
1937 1.1 brad struct emcfan_sc *sc = device_private(self);
1938 1.1 brad
1939 1.1 brad switch (act) {
1940 1.1 brad case DVACT_DEACTIVATE:
1941 1.1 brad sc->sc_dying = true;
1942 1.1 brad return 0;
1943 1.1 brad default:
1944 1.1 brad return EOPNOTSUPP;
1945 1.1 brad }
1946 1.1 brad }
1947 1.1 brad
1948 1.1 brad /* --- GPIO --- */
1949 1.1 brad
1950 1.1 brad static int
1951 1.1 brad emcfan_current_gpio_flags(struct emcfan_sc *sc,
1952 1.1 brad uint8_t product_id,
1953 1.1 brad int pin)
1954 1.1 brad {
1955 1.1 brad int error = 0;
1956 1.1 brad int f = 0;
1957 1.1 brad uint8_t mux_reg = 0;
1958 1.1 brad uint8_t dir_reg;
1959 1.1 brad uint8_t out_config;
1960 1.1 brad uint8_t pin_mask;
1961 1.1 brad uint8_t pin_maska1;
1962 1.1 brad uint8_t pin_maska2;
1963 1.1 brad
1964 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
1965 1.1 brad if (error) {
1966 1.1 brad return 0;
1967 1.1 brad }
1968 1.1 brad
1969 1.1 brad if (product_id != EMCFAN_PRODUCT_2103_24) {
1970 1.1 brad error = emcfan_read_register(sc, EMCFAN_MUX_PINS, &mux_reg);
1971 1.1 brad if (error != 0) {
1972 1.1 brad return 0;
1973 1.1 brad }
1974 1.1 brad }
1975 1.1 brad error = emcfan_read_register(sc, EMCFAN_DIR_PINS, &dir_reg);
1976 1.1 brad if (error != 0) {
1977 1.1 brad return 0;
1978 1.1 brad }
1979 1.1 brad error = emcfan_read_register(sc, EMCFAN_OUTPUT_PIN_CONFIG, &out_config);
1980 1.1 brad if (error != 0) {
1981 1.1 brad return 0;
1982 1.1 brad }
1983 1.1 brad iic_release_bus(sc->sc_tag, 0);
1984 1.1 brad
1985 1.1 brad pin_mask = 1 << pin;
1986 1.1 brad
1987 1.1 brad if (product_id != EMCFAN_PRODUCT_2103_24) {
1988 1.1 brad if (pin <= 4) {
1989 1.1 brad if (pin <= 2) {
1990 1.1 brad if ((mux_reg & pin_mask) == 0)
1991 1.1 brad f = GPIO_PIN_ALT0;
1992 1.1 brad } else {
1993 1.1 brad if (pin == 3) {
1994 1.1 brad pin_maska1 = 0x08;
1995 1.1 brad pin_maska2 = 0x10;
1996 1.1 brad } else {
1997 1.1 brad pin_maska1 = 0x20;
1998 1.1 brad pin_maska2 = 0x40;
1999 1.1 brad }
2000 1.1 brad if (mux_reg & pin_maska1 &&
2001 1.1 brad mux_reg & pin_maska2) {
2002 1.1 brad f = GPIO_PIN_ALT1;
2003 1.1 brad } else {
2004 1.1 brad if (((mux_reg & pin_maska1) == 0) &&
2005 1.1 brad ((mux_reg & pin_maska2) == 0)) {
2006 1.1 brad f = GPIO_PIN_ALT0;
2007 1.1 brad }
2008 1.1 brad }
2009 1.1 brad }
2010 1.1 brad }
2011 1.1 brad }
2012 1.1 brad
2013 1.1 brad if (f == 0) {
2014 1.1 brad if (dir_reg & pin_mask) {
2015 1.1 brad f = GPIO_PIN_OUTPUT;
2016 1.1 brad } else {
2017 1.1 brad f = GPIO_PIN_INPUT;
2018 1.1 brad }
2019 1.1 brad
2020 1.1 brad if (out_config & pin_mask) {
2021 1.1 brad f |= GPIO_PIN_PUSHPULL;
2022 1.1 brad } else {
2023 1.1 brad f |= GPIO_PIN_OPENDRAIN;
2024 1.1 brad }
2025 1.1 brad }
2026 1.1 brad
2027 1.1 brad return f;
2028 1.1 brad }
2029 1.1 brad
2030 1.1 brad static int
2031 1.1 brad emcfan_gpio_pin_read(void *arg, int pin)
2032 1.1 brad {
2033 1.1 brad struct emcfan_sc *sc = arg;
2034 1.1 brad int error = 0;
2035 1.1 brad int r = GPIO_PIN_LOW;
2036 1.1 brad uint8_t input_reg;
2037 1.1 brad uint8_t pin_mask;
2038 1.1 brad
2039 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
2040 1.1 brad if (!error) {
2041 1.1 brad error = emcfan_read_register(sc, EMCFAN_PINS_INPUT, &input_reg);
2042 1.1 brad if (!error) {
2043 1.1 brad pin_mask = 1 << pin;
2044 1.1 brad if (input_reg & pin_mask)
2045 1.1 brad r = GPIO_PIN_HIGH;
2046 1.1 brad }
2047 1.1 brad
2048 1.1 brad }
2049 1.1 brad iic_release_bus(sc->sc_tag, 0);
2050 1.1 brad
2051 1.1 brad return r;
2052 1.1 brad }
2053 1.1 brad
2054 1.1 brad static void
2055 1.1 brad emcfan_gpio_pin_write(void *arg, int pin, int value)
2056 1.1 brad {
2057 1.1 brad struct emcfan_sc *sc = arg;
2058 1.1 brad int error = 0;
2059 1.1 brad uint8_t output_reg;
2060 1.1 brad uint8_t pin_mask;
2061 1.1 brad
2062 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
2063 1.1 brad if (!error) {
2064 1.1 brad error = emcfan_read_register(sc, EMCFAN_PINS_OUTPUT, &output_reg);
2065 1.1 brad if (!error) {
2066 1.1 brad pin_mask = 1 << pin;
2067 1.1 brad
2068 1.1 brad if (value == 0) {
2069 1.1 brad pin_mask = ~pin_mask;
2070 1.1 brad output_reg &= pin_mask;
2071 1.1 brad } else {
2072 1.1 brad output_reg |= pin_mask;
2073 1.1 brad }
2074 1.1 brad emcfan_write_register(sc, EMCFAN_PINS_OUTPUT, output_reg);
2075 1.1 brad }
2076 1.1 brad
2077 1.1 brad }
2078 1.1 brad iic_release_bus(sc->sc_tag, 0);
2079 1.1 brad }
2080 1.1 brad
2081 1.1 brad static void
2082 1.1 brad emcfan_gpio_pin_ctl(void *arg, int pin, int flags)
2083 1.1 brad {
2084 1.1 brad struct emcfan_sc *sc = arg;
2085 1.1 brad int error = 0;
2086 1.1 brad uint8_t product_id = emcfan_chip_infos[sc->sc_info_index].product_id;
2087 1.1 brad uint8_t pin_mask = 1 << pin;
2088 1.1 brad uint8_t pin_maska1;
2089 1.1 brad uint8_t pin_maska2;
2090 1.1 brad uint8_t mux_reg = 0;
2091 1.1 brad uint8_t dir_reg;
2092 1.1 brad uint8_t out_config;
2093 1.1 brad
2094 1.1 brad error = iic_acquire_bus(sc->sc_tag, 0);
2095 1.1 brad if (error) {
2096 1.1 brad return;
2097 1.1 brad }
2098 1.1 brad
2099 1.1 brad if (product_id != EMCFAN_PRODUCT_2103_24) {
2100 1.1 brad error = emcfan_read_register(sc, EMCFAN_MUX_PINS, &mux_reg);
2101 1.1 brad if (error != 0) {
2102 1.1 brad return;
2103 1.1 brad }
2104 1.1 brad }
2105 1.1 brad error = emcfan_read_register(sc, EMCFAN_DIR_PINS, &dir_reg);
2106 1.1 brad if (error != 0) {
2107 1.1 brad return;
2108 1.1 brad }
2109 1.1 brad error = emcfan_read_register(sc, EMCFAN_OUTPUT_PIN_CONFIG, &out_config);
2110 1.1 brad if (error != 0) {
2111 1.1 brad return;
2112 1.1 brad }
2113 1.1 brad iic_release_bus(sc->sc_tag, 0);
2114 1.1 brad
2115 1.1 brad if (flags & GPIO_PIN_ALT0 ||
2116 1.1 brad flags & GPIO_PIN_ALT1) {
2117 1.1 brad if (product_id != EMCFAN_PRODUCT_2103_24) {
2118 1.1 brad if (pin <= 4) {
2119 1.1 brad if (pin <= 2) {
2120 1.1 brad mux_reg &= ~pin_mask;
2121 1.1 brad } else {
2122 1.1 brad if (pin == 3) {
2123 1.1 brad pin_maska2 = 0x18;
2124 1.1 brad } else {
2125 1.1 brad pin_maska2 = 0x60;
2126 1.1 brad }
2127 1.1 brad if (flags & GPIO_PIN_ALT0) {
2128 1.1 brad mux_reg &= ~pin_maska2;
2129 1.1 brad } else {
2130 1.1 brad if (flags & GPIO_PIN_ALT1) {
2131 1.1 brad mux_reg |= pin_maska2;
2132 1.1 brad }
2133 1.1 brad }
2134 1.1 brad }
2135 1.1 brad emcfan_write_register(sc, EMCFAN_MUX_PINS, mux_reg);
2136 1.1 brad }
2137 1.1 brad }
2138 1.1 brad } else {
2139 1.1 brad if (product_id != EMCFAN_PRODUCT_2103_24) {
2140 1.1 brad if (pin <= 4) {
2141 1.1 brad if (pin <= 2) {
2142 1.1 brad mux_reg |= pin_mask;
2143 1.1 brad } else {
2144 1.1 brad if (pin == 3) {
2145 1.1 brad pin_maska1 = 0x08;
2146 1.1 brad pin_maska2 = 0x18;
2147 1.1 brad } else {
2148 1.1 brad pin_maska1 = 0x20;
2149 1.1 brad pin_maska2 = 0x60;
2150 1.1 brad }
2151 1.1 brad mux_reg &= ~pin_maska2;
2152 1.1 brad mux_reg |= pin_maska1;
2153 1.1 brad }
2154 1.1 brad emcfan_write_register(sc, EMCFAN_MUX_PINS, mux_reg);
2155 1.1 brad }
2156 1.1 brad }
2157 1.1 brad
2158 1.1 brad if (flags & GPIO_PIN_OPENDRAIN) {
2159 1.1 brad out_config &= ~pin_mask;
2160 1.1 brad } else {
2161 1.1 brad if (flags & GPIO_PIN_PUSHPULL)
2162 1.1 brad out_config |= pin_mask;
2163 1.1 brad }
2164 1.1 brad emcfan_write_register(sc, EMCFAN_OUTPUT_PIN_CONFIG, out_config);
2165 1.1 brad
2166 1.1 brad if (flags & GPIO_PIN_INPUT) {
2167 1.1 brad dir_reg &= ~pin_mask;
2168 1.1 brad } else {
2169 1.1 brad if (flags & GPIO_PIN_OUTPUT)
2170 1.1 brad dir_reg |= pin_mask;
2171 1.1 brad }
2172 1.1 brad emcfan_write_register(sc, EMCFAN_DIR_PINS, dir_reg);
2173 1.1 brad }
2174 1.1 brad }
2175 1.1 brad
2176 1.1 brad static void
2177 1.1 brad emcfan_attach_gpio(struct emcfan_sc *sc, uint8_t product_id)
2178 1.1 brad {
2179 1.1 brad struct gpiobus_attach_args gba;
2180 1.1 brad
2181 1.1 brad for(int i = 0; i < emcfan_chip_infos[sc->sc_info_index].num_gpio_pins;i++) {
2182 1.1 brad sc->sc_gpio_pins[i].pin_num = i;
2183 1.1 brad sc->sc_gpio_pins[i].pin_caps = emcfan_chip_infos[sc->sc_info_index].gpio_pin_ability[i];
2184 1.1 brad sc->sc_gpio_pins[i].pin_flags = emcfan_current_gpio_flags(sc, emcfan_chip_infos[sc->sc_info_index].product_id, i);
2185 1.1 brad sc->sc_gpio_pins[i].pin_intrcaps = 0;
2186 1.1 brad strncpy(sc->sc_gpio_pins[i].pin_defname,
2187 1.1 brad emcfan_chip_infos[sc->sc_info_index].gpio_names[i],
2188 1.1 brad strlen(emcfan_chip_infos[sc->sc_info_index].gpio_names[i]) + 1);
2189 1.1 brad }
2190 1.1 brad
2191 1.1 brad sc->sc_gpio_gc.gp_cookie = sc;
2192 1.1 brad sc->sc_gpio_gc.gp_pin_read = emcfan_gpio_pin_read;
2193 1.1 brad sc->sc_gpio_gc.gp_pin_write = emcfan_gpio_pin_write;
2194 1.1 brad sc->sc_gpio_gc.gp_pin_ctl = emcfan_gpio_pin_ctl;
2195 1.1 brad
2196 1.1 brad gba.gba_gc = &sc->sc_gpio_gc;
2197 1.1 brad gba.gba_pins = sc->sc_gpio_pins;
2198 1.1 brad gba.gba_npins = emcfan_chip_infos[sc->sc_info_index].num_gpio_pins;
2199 1.1 brad
2200 1.1 brad sc->sc_gpio_dev = config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS(.iattr = "gpiobus"));
2201 1.1 brad
2202 1.1 brad return;
2203 1.1 brad }
2204 1.1 brad
2205 1.1 brad MODULE(MODULE_CLASS_DRIVER, emcfan, "iic,sysmon_envsys,gpio");
2206 1.1 brad
2207 1.1 brad #ifdef _MODULE
2208 1.1 brad #include "ioconf.c"
2209 1.1 brad #endif
2210 1.1 brad
2211 1.1 brad static int
2212 1.1 brad emcfan_modcmd(modcmd_t cmd, void *opaque)
2213 1.1 brad {
2214 1.1 brad int error;
2215 1.1 brad #ifdef _MODULE
2216 1.1 brad int bmaj = -1, cmaj = -1;
2217 1.1 brad #endif
2218 1.1 brad
2219 1.1 brad switch (cmd) {
2220 1.1 brad case MODULE_CMD_INIT:
2221 1.1 brad #ifdef _MODULE
2222 1.1 brad error = devsw_attach("emcfan", NULL, &bmaj,
2223 1.1 brad &emcfan_cdevsw, &cmaj);
2224 1.1 brad if (error) {
2225 1.1 brad aprint_error("%s: unable to attach devsw\n",
2226 1.1 brad emcfan_cd.cd_name);
2227 1.1 brad return error;
2228 1.1 brad }
2229 1.1 brad
2230 1.1 brad error = config_init_component(cfdriver_ioconf_emcfan,
2231 1.1 brad cfattach_ioconf_emcfan, cfdata_ioconf_emcfan);
2232 1.1 brad if (error) {
2233 1.1 brad aprint_error("%s: unable to init component\n",
2234 1.1 brad emcfan_cd.cd_name);
2235 1.1 brad devsw_detach(NULL, &emcfan_cdevsw);
2236 1.1 brad }
2237 1.1 brad return error;
2238 1.1 brad #else
2239 1.1 brad return 0;
2240 1.1 brad #endif
2241 1.1 brad case MODULE_CMD_FINI:
2242 1.1 brad #ifdef _MODULE
2243 1.1 brad error = config_fini_component(cfdriver_ioconf_emcfan,
2244 1.1 brad cfattach_ioconf_emcfan, cfdata_ioconf_emcfan);
2245 1.1 brad devsw_detach(NULL, &emcfan_cdevsw);
2246 1.1 brad return error;
2247 1.1 brad #else
2248 1.1 brad return 0;
2249 1.1 brad #endif
2250 1.1 brad default:
2251 1.1 brad return ENOTTY;
2252 1.1 brad }
2253 1.1 brad }
2254