fcu.c revision 1.6 1 1.6 macallan /* $NetBSD: fcu.c,v 1.6 2025/07/01 14:13:13 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2018 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/cdefs.h>
30 1.6 macallan __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.6 2025/07/01 14:13:13 macallan Exp $");
31 1.1 macallan
32 1.1 macallan #include <sys/param.h>
33 1.1 macallan #include <sys/systm.h>
34 1.1 macallan #include <sys/device.h>
35 1.1 macallan #include <sys/conf.h>
36 1.1 macallan #include <sys/bus.h>
37 1.1 macallan #include <sys/kthread.h>
38 1.4 macallan #include <sys/sysctl.h>
39 1.1 macallan
40 1.1 macallan #include <dev/i2c/i2cvar.h>
41 1.1 macallan
42 1.1 macallan #include <dev/sysmon/sysmonvar.h>
43 1.1 macallan
44 1.1 macallan #include <dev/ofw/openfirm.h>
45 1.1 macallan
46 1.2 macallan #include <macppc/dev/fancontrolvar.h>
47 1.2 macallan
48 1.6 macallan #include "opt_fcu.h"
49 1.6 macallan
50 1.1 macallan #ifdef FCU_DEBUG
51 1.1 macallan #define DPRINTF printf
52 1.1 macallan #else
53 1.1 macallan #define DPRINTF if (0) printf
54 1.1 macallan #endif
55 1.1 macallan
56 1.1 macallan /* FCU registers, from OpenBSD's fcu.c */
57 1.1 macallan #define FCU_FAN_FAIL 0x0b /* fans states in bits 0<1-6>7 */
58 1.1 macallan #define FCU_FAN_ACTIVE 0x0d
59 1.1 macallan #define FCU_FANREAD(x) 0x11 + (x)*2
60 1.1 macallan #define FCU_FANSET(x) 0x10 + (x)*2
61 1.1 macallan #define FCU_PWM_FAIL 0x2b
62 1.1 macallan #define FCU_PWM_ACTIVE 0x2d
63 1.1 macallan #define FCU_PWMREAD(x) 0x30 + (x)*2
64 1.1 macallan
65 1.1 macallan
66 1.1 macallan typedef struct _fcu_fan {
67 1.1 macallan int target;
68 1.1 macallan int reg;
69 1.1 macallan int base_rpm, max_rpm;
70 1.1 macallan int step;
71 1.1 macallan int duty; /* for pwm fans */
72 1.1 macallan } fcu_fan_t;
73 1.1 macallan
74 1.1 macallan #define FCU_ZONE_CPU 0
75 1.1 macallan #define FCU_ZONE_CASE 1
76 1.1 macallan #define FCU_ZONE_DRIVEBAY 2
77 1.1 macallan #define FCU_ZONE_COUNT 3
78 1.1 macallan
79 1.1 macallan struct fcu_softc {
80 1.1 macallan device_t sc_dev;
81 1.1 macallan i2c_tag_t sc_i2c;
82 1.1 macallan i2c_addr_t sc_addr;
83 1.4 macallan struct sysctlnode *sc_sysctl_me;
84 1.4 macallan struct sysmon_envsys *sc_sme;
85 1.2 macallan envsys_data_t sc_sensors[32];
86 1.2 macallan int sc_nsensors;
87 1.2 macallan fancontrol_zone_t sc_zones[FCU_ZONE_COUNT];
88 1.2 macallan fcu_fan_t sc_fans[FANCONTROL_MAX_FANS];
89 1.2 macallan int sc_nfans;
90 1.2 macallan lwp_t *sc_thread;
91 1.2 macallan bool sc_dying, sc_pwm;
92 1.2 macallan uint8_t sc_eeprom0[160];
93 1.2 macallan uint8_t sc_eeprom1[160];
94 1.1 macallan };
95 1.1 macallan
96 1.1 macallan static int fcu_match(device_t, cfdata_t, void *);
97 1.1 macallan static void fcu_attach(device_t, device_t, void *);
98 1.1 macallan
99 1.1 macallan static void fcu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
100 1.6 macallan static void fcu_configure_sensor(struct fcu_softc *, envsys_data_t *);
101 1.1 macallan
102 1.1 macallan static bool is_cpu(const envsys_data_t *);
103 1.1 macallan static bool is_case(const envsys_data_t *);
104 1.1 macallan static bool is_drive(const envsys_data_t *);
105 1.1 macallan
106 1.2 macallan static int fcu_set_rpm(void *, int, int);
107 1.2 macallan static int fcu_get_rpm(void *, int);
108 1.1 macallan static void fcu_adjust(void *);
109 1.1 macallan
110 1.1 macallan CFATTACH_DECL_NEW(fcu, sizeof(struct fcu_softc),
111 1.1 macallan fcu_match, fcu_attach, NULL, NULL);
112 1.1 macallan
113 1.1 macallan static const struct device_compatible_entry compat_data[] = {
114 1.1 macallan { .compat = "fcu" },
115 1.1 macallan DEVICE_COMPAT_EOL
116 1.1 macallan };
117 1.1 macallan
118 1.1 macallan static int
119 1.1 macallan fcu_match(device_t parent, cfdata_t match, void *aux)
120 1.1 macallan {
121 1.1 macallan struct i2c_attach_args *ia = aux;
122 1.1 macallan int match_result;
123 1.1 macallan
124 1.1 macallan if (iic_use_direct_match(ia, match, compat_data, &match_result))
125 1.1 macallan return match_result;
126 1.1 macallan
127 1.1 macallan if (ia->ia_addr == 0x2f)
128 1.1 macallan return I2C_MATCH_ADDRESS_ONLY;
129 1.1 macallan
130 1.1 macallan return 0;
131 1.1 macallan }
132 1.1 macallan
133 1.1 macallan static void
134 1.1 macallan fcu_attach(device_t parent, device_t self, void *aux)
135 1.1 macallan {
136 1.1 macallan struct fcu_softc *sc = device_private(self);
137 1.1 macallan struct i2c_attach_args *ia = aux;
138 1.6 macallan int i;
139 1.1 macallan
140 1.1 macallan sc->sc_dev = self;
141 1.1 macallan sc->sc_i2c = ia->ia_tag;
142 1.1 macallan sc->sc_addr = ia->ia_addr;
143 1.1 macallan
144 1.1 macallan aprint_naive("\n");
145 1.1 macallan aprint_normal(": Fan Control Unit\n");
146 1.1 macallan
147 1.4 macallan sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me,
148 1.4 macallan CTLFLAG_READWRITE,
149 1.4 macallan CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
150 1.4 macallan NULL, 0, NULL, 0,
151 1.4 macallan CTL_MACHDEP, CTL_CREATE, CTL_EOL);
152 1.4 macallan
153 1.1 macallan if (get_cpuid(0, sc->sc_eeprom0) < 160) {
154 1.1 macallan /*
155 1.1 macallan * XXX this should never happen, we depend on the EEPROM for
156 1.1 macallan * calibration data to make sense of temperature and voltage
157 1.1 macallan * sensors elsewhere, and fan parameters here.
158 1.1 macallan */
159 1.1 macallan aprint_error_dev(self, "no EEPROM data for CPU 0\n");
160 1.1 macallan return;
161 1.1 macallan }
162 1.1 macallan
163 1.1 macallan /* init zones */
164 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].name = "CPUs";
165 1.1 macallan sc->sc_zones[FCU_ZONE_CPU].filter = is_cpu;
166 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].cookie = sc;
167 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].get_rpm = fcu_get_rpm;
168 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].set_rpm = fcu_set_rpm;
169 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].Tmin = 50;
170 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].Tmax = 85;
171 1.1 macallan sc->sc_zones[FCU_ZONE_CPU].nfans = 0;
172 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].name = "Slots";
173 1.1 macallan sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
174 1.4 macallan sc->sc_zones[FCU_ZONE_CASE].cookie = sc;
175 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].Tmin = 50;
176 1.4 macallan sc->sc_zones[FCU_ZONE_CASE].Tmax = 75;
177 1.4 macallan sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
178 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].get_rpm = fcu_get_rpm;
179 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].set_rpm = fcu_set_rpm;
180 1.4 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].name = "Drivebays";
181 1.1 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
182 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].cookie = sc;
183 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].get_rpm = fcu_get_rpm;
184 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].set_rpm = fcu_set_rpm;
185 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmin = 30;
186 1.4 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmax = 50;
187 1.1 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
188 1.1 macallan
189 1.1 macallan sc->sc_sme = sysmon_envsys_create();
190 1.1 macallan sc->sc_sme->sme_name = device_xname(self);
191 1.1 macallan sc->sc_sme->sme_cookie = sc;
192 1.1 macallan sc->sc_sme->sme_refresh = fcu_sensors_refresh;
193 1.1 macallan
194 1.1 macallan sc->sc_sensors[0].units = ENVSYS_SFANRPM;
195 1.1 macallan sc->sc_sensors[1].state = ENVSYS_SINVALID;
196 1.1 macallan sc->sc_nfans = 0;
197 1.1 macallan
198 1.1 macallan /* round up sensors */
199 1.1 macallan int ch;
200 1.1 macallan
201 1.1 macallan sc->sc_nsensors = 0;
202 1.1 macallan ch = OF_child(ia->ia_cookie);
203 1.6 macallan if (ch == 0) {
204 1.6 macallan /* old style data, no individual nodes for fans, annoying */
205 1.6 macallan char loc[256], tp[256], descr[32], type[32];
206 1.6 macallan uint32_t reg_rpm = 0x10, reg_pwm = 0x32, reg;
207 1.6 macallan uint32_t id[16];
208 1.6 macallan int num, lidx = 0, tidx = 0;
209 1.6 macallan
210 1.6 macallan num = OF_getprop(ia->ia_cookie, "hwctrl-id", id, 64);
211 1.6 macallan OF_getprop(ia->ia_cookie, "hwctrl-location", loc, 1024);
212 1.6 macallan OF_getprop(ia->ia_cookie, "hwctrl-type", tp, 1024);
213 1.6 macallan while (num > 0) {
214 1.6 macallan envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
215 1.6 macallan
216 1.6 macallan s->state = ENVSYS_SINVALID;
217 1.6 macallan strcpy(descr, &loc[lidx]);
218 1.6 macallan strcpy(type, &tp[tidx]);
219 1.6 macallan if (strstr(type, "rpm") != NULL) {
220 1.6 macallan s->units = ENVSYS_SFANRPM;
221 1.6 macallan reg = reg_rpm;
222 1.6 macallan reg_rpm += 2;
223 1.6 macallan } else if (strstr(type, "pwm") != NULL) {
224 1.6 macallan s->units = ENVSYS_SFANRPM;
225 1.6 macallan reg = reg_pwm;
226 1.6 macallan reg_pwm += 2;
227 1.6 macallan } else goto skip;
228 1.6 macallan
229 1.6 macallan s->private = reg;
230 1.6 macallan strcpy(s->desc, descr);
231 1.6 macallan
232 1.6 macallan fcu_configure_sensor(sc, s);
233 1.6 macallan
234 1.6 macallan sysmon_envsys_sensor_attach(sc->sc_sme, s);
235 1.6 macallan sc->sc_nsensors++;
236 1.6 macallan skip:
237 1.6 macallan lidx += strlen(descr) + 1;
238 1.6 macallan tidx += strlen(type) + 1;
239 1.6 macallan num -= 4;
240 1.6 macallan }
241 1.6 macallan } else {
242 1.6 macallan /* new style, with individual nodes */
243 1.6 macallan while (ch != 0) {
244 1.6 macallan char type[32], descr[32];
245 1.6 macallan uint32_t reg;
246 1.6 macallan
247 1.6 macallan envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
248 1.6 macallan
249 1.6 macallan s->state = ENVSYS_SINVALID;
250 1.6 macallan
251 1.6 macallan if (OF_getprop(ch, "device_type", type, 32) <= 0)
252 1.6 macallan goto next;
253 1.6 macallan
254 1.6 macallan if (strcmp(type, "fan-rpm-control") == 0) {
255 1.6 macallan s->units = ENVSYS_SFANRPM;
256 1.6 macallan } else if (strcmp(type, "fan-pwm-control") == 0) {
257 1.6 macallan /* XXX we get the type from the register number */
258 1.6 macallan s->units = ENVSYS_SFANRPM;
259 1.1 macallan /* skip those for now since we don't really know how to interpret them */
260 1.1 macallan #if 0
261 1.6 macallan } else if (strcmp(type, "power-sensor") == 0) {
262 1.6 macallan s->units = ENVSYS_SVOLTS_DC;
263 1.1 macallan #endif
264 1.6 macallan } else if (strcmp(type, "gpi-sensor") == 0) {
265 1.6 macallan s->units = ENVSYS_INDICATOR;
266 1.6 macallan } else {
267 1.6 macallan /* ignore other types for now */
268 1.6 macallan goto next;
269 1.6 macallan }
270 1.1 macallan
271 1.6 macallan if (OF_getprop(ch, "reg", ®, sizeof(reg)) <= 0)
272 1.6 macallan goto next;
273 1.6 macallan s->private = reg;
274 1.6 macallan
275 1.6 macallan if (OF_getprop(ch, "location", descr, 32) <= 0)
276 1.6 macallan goto next;
277 1.6 macallan strcpy(s->desc, descr);
278 1.1 macallan
279 1.6 macallan fcu_configure_sensor(sc, s);
280 1.1 macallan
281 1.6 macallan sysmon_envsys_sensor_attach(sc->sc_sme, s);
282 1.6 macallan sc->sc_nsensors++;
283 1.6 macallan next:
284 1.6 macallan ch = OF_peer(ch);
285 1.1 macallan }
286 1.1 macallan }
287 1.1 macallan sysmon_envsys_register(sc->sc_sme);
288 1.1 macallan
289 1.4 macallan /* setup sysctls for our zones etc. */
290 1.4 macallan for (i = 0; i < FCU_ZONE_COUNT; i++) {
291 1.4 macallan fancontrol_init_zone(&sc->sc_zones[i], sc->sc_sysctl_me);
292 1.4 macallan }
293 1.4 macallan
294 1.1 macallan sc->sc_dying = FALSE;
295 1.1 macallan kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
296 1.1 macallan "fan control");
297 1.1 macallan }
298 1.1 macallan
299 1.1 macallan static void
300 1.6 macallan fcu_configure_sensor(struct fcu_softc *sc, envsys_data_t *s)
301 1.6 macallan {
302 1.6 macallan int have_eeprom1 = 1;
303 1.6 macallan
304 1.6 macallan if (get_cpuid(1, sc->sc_eeprom1) < 160)
305 1.6 macallan have_eeprom1 = 0;
306 1.6 macallan
307 1.6 macallan if (s->units == ENVSYS_SFANRPM) {
308 1.6 macallan fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
309 1.6 macallan uint8_t *eeprom = NULL;
310 1.6 macallan uint16_t rmin, rmax;
311 1.6 macallan
312 1.6 macallan if (strstr(s->desc, "CPU A") != NULL)
313 1.6 macallan eeprom = sc->sc_eeprom0;
314 1.6 macallan if (strstr(s->desc, "CPU B") != NULL) {
315 1.6 macallan /*
316 1.6 macallan * XXX
317 1.6 macallan * this should never happen
318 1.6 macallan */
319 1.6 macallan if (have_eeprom1 == 0) {
320 1.6 macallan eeprom = sc->sc_eeprom0;
321 1.6 macallan } else
322 1.6 macallan eeprom = sc->sc_eeprom1;
323 1.6 macallan }
324 1.6 macallan
325 1.6 macallan fan->reg = s->private;
326 1.6 macallan fan->target = 0;
327 1.6 macallan fan->duty = 0x80;
328 1.6 macallan
329 1.6 macallan /* speed settings from EEPROM */
330 1.6 macallan if (strstr(s->desc, "PUMP") != NULL) {
331 1.6 macallan KASSERT(eeprom != NULL);
332 1.6 macallan memcpy(&rmin, &eeprom[0x54], 2);
333 1.6 macallan memcpy(&rmax, &eeprom[0x56], 2);
334 1.6 macallan fan->base_rpm = rmin;
335 1.6 macallan fan->max_rpm = rmax;
336 1.6 macallan fan->step = (rmax - rmin) / 30;
337 1.6 macallan } else if (strstr(s->desc, "INTAKE") != NULL) {
338 1.6 macallan KASSERT(eeprom != NULL);
339 1.6 macallan memcpy(&rmin, &eeprom[0x4c], 2);
340 1.6 macallan memcpy(&rmax, &eeprom[0x4e], 2);
341 1.6 macallan fan->base_rpm = rmin;
342 1.6 macallan fan->max_rpm = rmax;
343 1.6 macallan fan->step = (rmax - rmin) / 30;
344 1.6 macallan } else if (strstr(s->desc, "EXHAUST") != NULL) {
345 1.6 macallan KASSERT(eeprom != NULL);
346 1.6 macallan memcpy(&rmin, &eeprom[0x50], 2);
347 1.6 macallan memcpy(&rmax, &eeprom[0x52], 2);
348 1.6 macallan fan->base_rpm = rmin;
349 1.6 macallan fan->max_rpm = rmax;
350 1.6 macallan fan->step = (rmax - rmin) / 30;
351 1.6 macallan } else if (strstr(s->desc, "DRIVE") != NULL ) {
352 1.6 macallan fan->base_rpm = 1000;
353 1.6 macallan fan->max_rpm = 3000;
354 1.6 macallan fan->step = 100;
355 1.6 macallan } else {
356 1.6 macallan fan->base_rpm = 1000;
357 1.6 macallan fan->max_rpm = 3000;
358 1.6 macallan fan->step = 100;
359 1.6 macallan }
360 1.6 macallan DPRINTF("fan %s: %d - %d rpm, step %d\n",
361 1.6 macallan s->desc, fan->base_rpm, fan->max_rpm, fan->step);
362 1.6 macallan
363 1.6 macallan /* now stuff them into zones */
364 1.6 macallan if (strstr(s->desc, "CPU") != NULL) {
365 1.6 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU];
366 1.6 macallan z->fans[z->nfans].num = sc->sc_nfans;
367 1.6 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
368 1.6 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
369 1.6 macallan z->fans[z->nfans].name = s->desc;
370 1.6 macallan z->nfans++;
371 1.6 macallan } else if ((strstr(s->desc, "BACKSIDE") != NULL) ||
372 1.6 macallan (strstr(s->desc, "SLOT") != NULL)) {
373 1.6 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
374 1.6 macallan z->fans[z->nfans].num = sc->sc_nfans;
375 1.6 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
376 1.6 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
377 1.6 macallan z->fans[z->nfans].name = s->desc;
378 1.6 macallan z->nfans++;
379 1.6 macallan } else if (strstr(s->desc, "DRIVE") != NULL) {
380 1.6 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
381 1.6 macallan z->fans[z->nfans].num = sc->sc_nfans;
382 1.6 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
383 1.6 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
384 1.6 macallan z->fans[z->nfans].name = s->desc;
385 1.6 macallan z->nfans++;
386 1.6 macallan }
387 1.6 macallan sc->sc_nfans++;
388 1.6 macallan }
389 1.6 macallan }
390 1.6 macallan static void
391 1.1 macallan fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
392 1.1 macallan {
393 1.1 macallan struct fcu_softc *sc = sme->sme_cookie;
394 1.1 macallan uint8_t cmd;
395 1.6 macallan uint16_t data = 0;
396 1.1 macallan int error;
397 1.1 macallan
398 1.1 macallan if (edata->units == ENVSYS_SFANRPM) {
399 1.1 macallan cmd = edata->private + 1;
400 1.1 macallan } else
401 1.1 macallan cmd = edata->private;
402 1.1 macallan
403 1.1 macallan /* fcu is a macppc only thing so we can safely assume big endian */
404 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
405 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
406 1.1 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
407 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
408 1.1 macallan
409 1.1 macallan if (error) {
410 1.1 macallan edata->state = ENVSYS_SINVALID;
411 1.1 macallan return;
412 1.1 macallan }
413 1.1 macallan
414 1.1 macallan edata->state = ENVSYS_SVALID;
415 1.1 macallan
416 1.1 macallan switch (edata->units) {
417 1.1 macallan case ENVSYS_SFANRPM:
418 1.1 macallan edata->value_cur = data >> 3;
419 1.1 macallan break;
420 1.1 macallan case ENVSYS_SVOLTS_DC:
421 1.1 macallan /* XXX this reads bogus */
422 1.1 macallan edata->value_cur = data * 1000;
423 1.1 macallan break;
424 1.1 macallan case ENVSYS_INDICATOR:
425 1.1 macallan /* guesswork for now */
426 1.1 macallan edata->value_cur = data >> 8;
427 1.1 macallan break;
428 1.1 macallan default:
429 1.1 macallan edata->state = ENVSYS_SINVALID;
430 1.1 macallan }
431 1.1 macallan }
432 1.1 macallan
433 1.1 macallan static bool
434 1.1 macallan is_cpu(const envsys_data_t *edata)
435 1.1 macallan {
436 1.1 macallan if (edata->units != ENVSYS_STEMP)
437 1.1 macallan return false;
438 1.1 macallan if (strstr(edata->desc, "CPU") != NULL)
439 1.1 macallan return TRUE;
440 1.1 macallan return false;
441 1.1 macallan }
442 1.1 macallan
443 1.1 macallan static bool
444 1.1 macallan is_case(const envsys_data_t *edata)
445 1.1 macallan {
446 1.1 macallan if (edata->units != ENVSYS_STEMP)
447 1.1 macallan return false;
448 1.1 macallan if ((strstr(edata->desc, "MLB") != NULL) ||
449 1.1 macallan (strstr(edata->desc, "BACKSIDE") != NULL) ||
450 1.1 macallan (strstr(edata->desc, "U3") != NULL))
451 1.1 macallan return TRUE;
452 1.1 macallan return false;
453 1.1 macallan }
454 1.1 macallan
455 1.1 macallan static bool
456 1.1 macallan is_drive(const envsys_data_t *edata)
457 1.1 macallan {
458 1.1 macallan if (edata->units != ENVSYS_STEMP)
459 1.1 macallan return false;
460 1.1 macallan if (strstr(edata->desc, "DRIVE") != NULL)
461 1.1 macallan return TRUE;
462 1.1 macallan return false;
463 1.1 macallan }
464 1.1 macallan
465 1.2 macallan static int
466 1.2 macallan fcu_get_rpm(void *cookie, int which)
467 1.1 macallan {
468 1.2 macallan struct fcu_softc *sc = cookie;
469 1.2 macallan fcu_fan_t *f = &sc->sc_fans[which];
470 1.1 macallan int error;
471 1.6 macallan uint16_t data = 0;
472 1.2 macallan uint8_t cmd;
473 1.2 macallan
474 1.2 macallan iic_acquire_bus(sc->sc_i2c, 0);
475 1.2 macallan cmd = f->reg + 1;
476 1.2 macallan error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
477 1.2 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
478 1.2 macallan iic_release_bus(sc->sc_i2c, 0);
479 1.6 macallan if (error != 0) return 0;
480 1.2 macallan data = data >> 3;
481 1.2 macallan return data;
482 1.2 macallan }
483 1.2 macallan
484 1.2 macallan static int
485 1.2 macallan fcu_set_rpm(void *cookie, int which, int speed)
486 1.2 macallan {
487 1.2 macallan struct fcu_softc *sc = cookie;
488 1.2 macallan fcu_fan_t *f = &sc->sc_fans[which];
489 1.2 macallan int error = 0;
490 1.1 macallan uint8_t cmd;
491 1.1 macallan
492 1.1 macallan if (speed > f->max_rpm) speed = f->max_rpm;
493 1.1 macallan if (speed < f->base_rpm) speed = f->base_rpm;
494 1.1 macallan
495 1.1 macallan if (f->reg < 0x30) {
496 1.1 macallan uint16_t data;
497 1.1 macallan /* simple rpm fan, just poke the register */
498 1.1 macallan
499 1.2 macallan if (f->target == speed) return 0;
500 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
501 1.1 macallan cmd = f->reg;
502 1.1 macallan data = (speed << 3);
503 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
504 1.1 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
505 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
506 1.1 macallan } else {
507 1.1 macallan int diff;
508 1.1 macallan int nduty = f->duty;
509 1.2 macallan int current_speed;
510 1.1 macallan /* pwm fan, measure speed, then adjust duty cycle */
511 1.1 macallan DPRINTF("pwm fan ");
512 1.2 macallan current_speed = fcu_get_rpm(sc, which);
513 1.2 macallan diff = current_speed - speed;
514 1.2 macallan DPRINTF("d %d s %d t %d diff %d ", f->duty, current_speed, speed, diff);
515 1.1 macallan if (diff > 100) {
516 1.1 macallan nduty = uimax(20, nduty - 1);
517 1.1 macallan }
518 1.1 macallan if (diff < -100) {
519 1.1 macallan nduty = uimin(0xd0, nduty + 1);
520 1.1 macallan }
521 1.1 macallan cmd = f->reg;
522 1.1 macallan DPRINTF("%s nduty %d", __func__, nduty);
523 1.1 macallan if (nduty != f->duty) {
524 1.1 macallan uint8_t arg = nduty;
525 1.2 macallan iic_acquire_bus(sc->sc_i2c, 0);
526 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
527 1.1 macallan sc->sc_addr, &cmd, 1, &arg, 1, 0);
528 1.2 macallan iic_release_bus(sc->sc_i2c, 0);
529 1.1 macallan f->duty = nduty;
530 1.1 macallan sc->sc_pwm = TRUE;
531 1.1 macallan
532 1.1 macallan }
533 1.1 macallan DPRINTF("ok\n");
534 1.1 macallan }
535 1.1 macallan if (error) printf("boo\n");
536 1.1 macallan f->target = speed;
537 1.2 macallan return 0;
538 1.1 macallan }
539 1.1 macallan
540 1.1 macallan static void
541 1.1 macallan fcu_adjust(void *cookie)
542 1.1 macallan {
543 1.1 macallan struct fcu_softc *sc = cookie;
544 1.1 macallan int i;
545 1.1 macallan uint8_t cmd, data;
546 1.1 macallan
547 1.1 macallan while (!sc->sc_dying) {
548 1.1 macallan /* poke the FCU so we don't go 747 */
549 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
550 1.1 macallan cmd = FCU_FAN_ACTIVE;
551 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
552 1.1 macallan sc->sc_addr, &cmd, 1, &data, 1, 0);
553 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
554 1.1 macallan sc->sc_pwm = FALSE;
555 1.1 macallan for (i = 0; i < FCU_ZONE_COUNT; i++)
556 1.2 macallan fancontrol_adjust_zone(&sc->sc_zones[i]);
557 1.3 macallan /*
558 1.5 andvar * take a shorter nap if we're in the process of adjusting a
559 1.3 macallan * PWM fan, which relies on measuring speed and then changing
560 1.3 macallan * its duty cycle until we're reasonable close to the target
561 1.3 macallan * speed
562 1.3 macallan */
563 1.3 macallan kpause("fanctrl", true, mstohz(sc->sc_pwm ? 1000 : 2000), NULL);
564 1.1 macallan }
565 1.1 macallan kthread_exit(0);
566 1.1 macallan }
567