fcu.c revision 1.7 1 1.7 thorpej /* $NetBSD: fcu.c,v 1.7 2025/09/17 14:15:59 thorpej 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.7 thorpej __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.7 2025/09/17 14:15:59 thorpej 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.7 thorpej int phandle = devhandle_to_of(device_handle(self));
139 1.6 macallan int i;
140 1.1 macallan
141 1.1 macallan sc->sc_dev = self;
142 1.1 macallan sc->sc_i2c = ia->ia_tag;
143 1.1 macallan sc->sc_addr = ia->ia_addr;
144 1.1 macallan
145 1.1 macallan aprint_naive("\n");
146 1.1 macallan aprint_normal(": Fan Control Unit\n");
147 1.1 macallan
148 1.4 macallan sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me,
149 1.4 macallan CTLFLAG_READWRITE,
150 1.4 macallan CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
151 1.4 macallan NULL, 0, NULL, 0,
152 1.4 macallan CTL_MACHDEP, CTL_CREATE, CTL_EOL);
153 1.4 macallan
154 1.1 macallan if (get_cpuid(0, sc->sc_eeprom0) < 160) {
155 1.1 macallan /*
156 1.1 macallan * XXX this should never happen, we depend on the EEPROM for
157 1.1 macallan * calibration data to make sense of temperature and voltage
158 1.1 macallan * sensors elsewhere, and fan parameters here.
159 1.1 macallan */
160 1.1 macallan aprint_error_dev(self, "no EEPROM data for CPU 0\n");
161 1.1 macallan return;
162 1.1 macallan }
163 1.1 macallan
164 1.1 macallan /* init zones */
165 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].name = "CPUs";
166 1.1 macallan sc->sc_zones[FCU_ZONE_CPU].filter = is_cpu;
167 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].cookie = sc;
168 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].get_rpm = fcu_get_rpm;
169 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].set_rpm = fcu_set_rpm;
170 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].Tmin = 50;
171 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].Tmax = 85;
172 1.1 macallan sc->sc_zones[FCU_ZONE_CPU].nfans = 0;
173 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].name = "Slots";
174 1.1 macallan sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
175 1.4 macallan sc->sc_zones[FCU_ZONE_CASE].cookie = sc;
176 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].Tmin = 50;
177 1.4 macallan sc->sc_zones[FCU_ZONE_CASE].Tmax = 75;
178 1.4 macallan sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
179 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].get_rpm = fcu_get_rpm;
180 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].set_rpm = fcu_set_rpm;
181 1.4 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].name = "Drivebays";
182 1.1 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
183 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].cookie = sc;
184 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].get_rpm = fcu_get_rpm;
185 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].set_rpm = fcu_set_rpm;
186 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmin = 30;
187 1.4 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmax = 50;
188 1.1 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
189 1.1 macallan
190 1.1 macallan sc->sc_sme = sysmon_envsys_create();
191 1.1 macallan sc->sc_sme->sme_name = device_xname(self);
192 1.1 macallan sc->sc_sme->sme_cookie = sc;
193 1.1 macallan sc->sc_sme->sme_refresh = fcu_sensors_refresh;
194 1.1 macallan
195 1.1 macallan sc->sc_sensors[0].units = ENVSYS_SFANRPM;
196 1.1 macallan sc->sc_sensors[1].state = ENVSYS_SINVALID;
197 1.1 macallan sc->sc_nfans = 0;
198 1.1 macallan
199 1.1 macallan /* round up sensors */
200 1.1 macallan int ch;
201 1.1 macallan
202 1.1 macallan sc->sc_nsensors = 0;
203 1.7 thorpej ch = OF_child(phandle);
204 1.6 macallan if (ch == 0) {
205 1.6 macallan /* old style data, no individual nodes for fans, annoying */
206 1.6 macallan char loc[256], tp[256], descr[32], type[32];
207 1.6 macallan uint32_t reg_rpm = 0x10, reg_pwm = 0x32, reg;
208 1.6 macallan uint32_t id[16];
209 1.6 macallan int num, lidx = 0, tidx = 0;
210 1.6 macallan
211 1.7 thorpej num = OF_getprop(phandle, "hwctrl-id", id, 64);
212 1.7 thorpej OF_getprop(phandle, "hwctrl-location", loc, 1024);
213 1.7 thorpej OF_getprop(phandle, "hwctrl-type", tp, 1024);
214 1.6 macallan while (num > 0) {
215 1.6 macallan envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
216 1.6 macallan
217 1.6 macallan s->state = ENVSYS_SINVALID;
218 1.6 macallan strcpy(descr, &loc[lidx]);
219 1.6 macallan strcpy(type, &tp[tidx]);
220 1.6 macallan if (strstr(type, "rpm") != NULL) {
221 1.6 macallan s->units = ENVSYS_SFANRPM;
222 1.6 macallan reg = reg_rpm;
223 1.6 macallan reg_rpm += 2;
224 1.6 macallan } else if (strstr(type, "pwm") != NULL) {
225 1.6 macallan s->units = ENVSYS_SFANRPM;
226 1.6 macallan reg = reg_pwm;
227 1.6 macallan reg_pwm += 2;
228 1.6 macallan } else goto skip;
229 1.6 macallan
230 1.6 macallan s->private = reg;
231 1.6 macallan strcpy(s->desc, descr);
232 1.6 macallan
233 1.6 macallan fcu_configure_sensor(sc, s);
234 1.6 macallan
235 1.6 macallan sysmon_envsys_sensor_attach(sc->sc_sme, s);
236 1.6 macallan sc->sc_nsensors++;
237 1.6 macallan skip:
238 1.6 macallan lidx += strlen(descr) + 1;
239 1.6 macallan tidx += strlen(type) + 1;
240 1.6 macallan num -= 4;
241 1.6 macallan }
242 1.6 macallan } else {
243 1.6 macallan /* new style, with individual nodes */
244 1.6 macallan while (ch != 0) {
245 1.6 macallan char type[32], descr[32];
246 1.6 macallan uint32_t reg;
247 1.6 macallan
248 1.6 macallan envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
249 1.6 macallan
250 1.6 macallan s->state = ENVSYS_SINVALID;
251 1.6 macallan
252 1.6 macallan if (OF_getprop(ch, "device_type", type, 32) <= 0)
253 1.6 macallan goto next;
254 1.6 macallan
255 1.6 macallan if (strcmp(type, "fan-rpm-control") == 0) {
256 1.6 macallan s->units = ENVSYS_SFANRPM;
257 1.6 macallan } else if (strcmp(type, "fan-pwm-control") == 0) {
258 1.6 macallan /* XXX we get the type from the register number */
259 1.6 macallan s->units = ENVSYS_SFANRPM;
260 1.1 macallan /* skip those for now since we don't really know how to interpret them */
261 1.1 macallan #if 0
262 1.6 macallan } else if (strcmp(type, "power-sensor") == 0) {
263 1.6 macallan s->units = ENVSYS_SVOLTS_DC;
264 1.1 macallan #endif
265 1.6 macallan } else if (strcmp(type, "gpi-sensor") == 0) {
266 1.6 macallan s->units = ENVSYS_INDICATOR;
267 1.6 macallan } else {
268 1.6 macallan /* ignore other types for now */
269 1.6 macallan goto next;
270 1.6 macallan }
271 1.1 macallan
272 1.6 macallan if (OF_getprop(ch, "reg", ®, sizeof(reg)) <= 0)
273 1.6 macallan goto next;
274 1.6 macallan s->private = reg;
275 1.6 macallan
276 1.6 macallan if (OF_getprop(ch, "location", descr, 32) <= 0)
277 1.6 macallan goto next;
278 1.6 macallan strcpy(s->desc, descr);
279 1.1 macallan
280 1.6 macallan fcu_configure_sensor(sc, s);
281 1.1 macallan
282 1.6 macallan sysmon_envsys_sensor_attach(sc->sc_sme, s);
283 1.6 macallan sc->sc_nsensors++;
284 1.6 macallan next:
285 1.6 macallan ch = OF_peer(ch);
286 1.1 macallan }
287 1.1 macallan }
288 1.1 macallan sysmon_envsys_register(sc->sc_sme);
289 1.1 macallan
290 1.4 macallan /* setup sysctls for our zones etc. */
291 1.4 macallan for (i = 0; i < FCU_ZONE_COUNT; i++) {
292 1.4 macallan fancontrol_init_zone(&sc->sc_zones[i], sc->sc_sysctl_me);
293 1.4 macallan }
294 1.4 macallan
295 1.1 macallan sc->sc_dying = FALSE;
296 1.1 macallan kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
297 1.1 macallan "fan control");
298 1.1 macallan }
299 1.1 macallan
300 1.1 macallan static void
301 1.6 macallan fcu_configure_sensor(struct fcu_softc *sc, envsys_data_t *s)
302 1.6 macallan {
303 1.6 macallan int have_eeprom1 = 1;
304 1.6 macallan
305 1.6 macallan if (get_cpuid(1, sc->sc_eeprom1) < 160)
306 1.6 macallan have_eeprom1 = 0;
307 1.6 macallan
308 1.6 macallan if (s->units == ENVSYS_SFANRPM) {
309 1.6 macallan fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
310 1.6 macallan uint8_t *eeprom = NULL;
311 1.6 macallan uint16_t rmin, rmax;
312 1.6 macallan
313 1.6 macallan if (strstr(s->desc, "CPU A") != NULL)
314 1.6 macallan eeprom = sc->sc_eeprom0;
315 1.6 macallan if (strstr(s->desc, "CPU B") != NULL) {
316 1.6 macallan /*
317 1.6 macallan * XXX
318 1.6 macallan * this should never happen
319 1.6 macallan */
320 1.6 macallan if (have_eeprom1 == 0) {
321 1.6 macallan eeprom = sc->sc_eeprom0;
322 1.6 macallan } else
323 1.6 macallan eeprom = sc->sc_eeprom1;
324 1.6 macallan }
325 1.6 macallan
326 1.6 macallan fan->reg = s->private;
327 1.6 macallan fan->target = 0;
328 1.6 macallan fan->duty = 0x80;
329 1.6 macallan
330 1.6 macallan /* speed settings from EEPROM */
331 1.6 macallan if (strstr(s->desc, "PUMP") != NULL) {
332 1.6 macallan KASSERT(eeprom != NULL);
333 1.6 macallan memcpy(&rmin, &eeprom[0x54], 2);
334 1.6 macallan memcpy(&rmax, &eeprom[0x56], 2);
335 1.6 macallan fan->base_rpm = rmin;
336 1.6 macallan fan->max_rpm = rmax;
337 1.6 macallan fan->step = (rmax - rmin) / 30;
338 1.6 macallan } else if (strstr(s->desc, "INTAKE") != NULL) {
339 1.6 macallan KASSERT(eeprom != NULL);
340 1.6 macallan memcpy(&rmin, &eeprom[0x4c], 2);
341 1.6 macallan memcpy(&rmax, &eeprom[0x4e], 2);
342 1.6 macallan fan->base_rpm = rmin;
343 1.6 macallan fan->max_rpm = rmax;
344 1.6 macallan fan->step = (rmax - rmin) / 30;
345 1.6 macallan } else if (strstr(s->desc, "EXHAUST") != NULL) {
346 1.6 macallan KASSERT(eeprom != NULL);
347 1.6 macallan memcpy(&rmin, &eeprom[0x50], 2);
348 1.6 macallan memcpy(&rmax, &eeprom[0x52], 2);
349 1.6 macallan fan->base_rpm = rmin;
350 1.6 macallan fan->max_rpm = rmax;
351 1.6 macallan fan->step = (rmax - rmin) / 30;
352 1.6 macallan } else if (strstr(s->desc, "DRIVE") != NULL ) {
353 1.6 macallan fan->base_rpm = 1000;
354 1.6 macallan fan->max_rpm = 3000;
355 1.6 macallan fan->step = 100;
356 1.6 macallan } else {
357 1.6 macallan fan->base_rpm = 1000;
358 1.6 macallan fan->max_rpm = 3000;
359 1.6 macallan fan->step = 100;
360 1.6 macallan }
361 1.6 macallan DPRINTF("fan %s: %d - %d rpm, step %d\n",
362 1.6 macallan s->desc, fan->base_rpm, fan->max_rpm, fan->step);
363 1.6 macallan
364 1.6 macallan /* now stuff them into zones */
365 1.6 macallan if (strstr(s->desc, "CPU") != NULL) {
366 1.6 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU];
367 1.6 macallan z->fans[z->nfans].num = sc->sc_nfans;
368 1.6 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
369 1.6 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
370 1.6 macallan z->fans[z->nfans].name = s->desc;
371 1.6 macallan z->nfans++;
372 1.6 macallan } else if ((strstr(s->desc, "BACKSIDE") != NULL) ||
373 1.6 macallan (strstr(s->desc, "SLOT") != NULL)) {
374 1.6 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
375 1.6 macallan z->fans[z->nfans].num = sc->sc_nfans;
376 1.6 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
377 1.6 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
378 1.6 macallan z->fans[z->nfans].name = s->desc;
379 1.6 macallan z->nfans++;
380 1.6 macallan } else if (strstr(s->desc, "DRIVE") != NULL) {
381 1.6 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
382 1.6 macallan z->fans[z->nfans].num = sc->sc_nfans;
383 1.6 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
384 1.6 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
385 1.6 macallan z->fans[z->nfans].name = s->desc;
386 1.6 macallan z->nfans++;
387 1.6 macallan }
388 1.6 macallan sc->sc_nfans++;
389 1.6 macallan }
390 1.6 macallan }
391 1.6 macallan static void
392 1.1 macallan fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
393 1.1 macallan {
394 1.1 macallan struct fcu_softc *sc = sme->sme_cookie;
395 1.1 macallan uint8_t cmd;
396 1.6 macallan uint16_t data = 0;
397 1.1 macallan int error;
398 1.1 macallan
399 1.1 macallan if (edata->units == ENVSYS_SFANRPM) {
400 1.1 macallan cmd = edata->private + 1;
401 1.1 macallan } else
402 1.1 macallan cmd = edata->private;
403 1.1 macallan
404 1.1 macallan /* fcu is a macppc only thing so we can safely assume big endian */
405 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
406 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
407 1.1 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
408 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
409 1.1 macallan
410 1.1 macallan if (error) {
411 1.1 macallan edata->state = ENVSYS_SINVALID;
412 1.1 macallan return;
413 1.1 macallan }
414 1.1 macallan
415 1.1 macallan edata->state = ENVSYS_SVALID;
416 1.1 macallan
417 1.1 macallan switch (edata->units) {
418 1.1 macallan case ENVSYS_SFANRPM:
419 1.1 macallan edata->value_cur = data >> 3;
420 1.1 macallan break;
421 1.1 macallan case ENVSYS_SVOLTS_DC:
422 1.1 macallan /* XXX this reads bogus */
423 1.1 macallan edata->value_cur = data * 1000;
424 1.1 macallan break;
425 1.1 macallan case ENVSYS_INDICATOR:
426 1.1 macallan /* guesswork for now */
427 1.1 macallan edata->value_cur = data >> 8;
428 1.1 macallan break;
429 1.1 macallan default:
430 1.1 macallan edata->state = ENVSYS_SINVALID;
431 1.1 macallan }
432 1.1 macallan }
433 1.1 macallan
434 1.1 macallan static bool
435 1.1 macallan is_cpu(const envsys_data_t *edata)
436 1.1 macallan {
437 1.1 macallan if (edata->units != ENVSYS_STEMP)
438 1.1 macallan return false;
439 1.1 macallan if (strstr(edata->desc, "CPU") != NULL)
440 1.1 macallan return TRUE;
441 1.1 macallan return false;
442 1.1 macallan }
443 1.1 macallan
444 1.1 macallan static bool
445 1.1 macallan is_case(const envsys_data_t *edata)
446 1.1 macallan {
447 1.1 macallan if (edata->units != ENVSYS_STEMP)
448 1.1 macallan return false;
449 1.1 macallan if ((strstr(edata->desc, "MLB") != NULL) ||
450 1.1 macallan (strstr(edata->desc, "BACKSIDE") != NULL) ||
451 1.1 macallan (strstr(edata->desc, "U3") != NULL))
452 1.1 macallan return TRUE;
453 1.1 macallan return false;
454 1.1 macallan }
455 1.1 macallan
456 1.1 macallan static bool
457 1.1 macallan is_drive(const envsys_data_t *edata)
458 1.1 macallan {
459 1.1 macallan if (edata->units != ENVSYS_STEMP)
460 1.1 macallan return false;
461 1.1 macallan if (strstr(edata->desc, "DRIVE") != NULL)
462 1.1 macallan return TRUE;
463 1.1 macallan return false;
464 1.1 macallan }
465 1.1 macallan
466 1.2 macallan static int
467 1.2 macallan fcu_get_rpm(void *cookie, int which)
468 1.1 macallan {
469 1.2 macallan struct fcu_softc *sc = cookie;
470 1.2 macallan fcu_fan_t *f = &sc->sc_fans[which];
471 1.1 macallan int error;
472 1.6 macallan uint16_t data = 0;
473 1.2 macallan uint8_t cmd;
474 1.2 macallan
475 1.2 macallan iic_acquire_bus(sc->sc_i2c, 0);
476 1.2 macallan cmd = f->reg + 1;
477 1.2 macallan error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
478 1.2 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
479 1.2 macallan iic_release_bus(sc->sc_i2c, 0);
480 1.6 macallan if (error != 0) return 0;
481 1.2 macallan data = data >> 3;
482 1.2 macallan return data;
483 1.2 macallan }
484 1.2 macallan
485 1.2 macallan static int
486 1.2 macallan fcu_set_rpm(void *cookie, int which, int speed)
487 1.2 macallan {
488 1.2 macallan struct fcu_softc *sc = cookie;
489 1.2 macallan fcu_fan_t *f = &sc->sc_fans[which];
490 1.2 macallan int error = 0;
491 1.1 macallan uint8_t cmd;
492 1.1 macallan
493 1.1 macallan if (speed > f->max_rpm) speed = f->max_rpm;
494 1.1 macallan if (speed < f->base_rpm) speed = f->base_rpm;
495 1.1 macallan
496 1.1 macallan if (f->reg < 0x30) {
497 1.1 macallan uint16_t data;
498 1.1 macallan /* simple rpm fan, just poke the register */
499 1.1 macallan
500 1.2 macallan if (f->target == speed) return 0;
501 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
502 1.1 macallan cmd = f->reg;
503 1.1 macallan data = (speed << 3);
504 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
505 1.1 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
506 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
507 1.1 macallan } else {
508 1.1 macallan int diff;
509 1.1 macallan int nduty = f->duty;
510 1.2 macallan int current_speed;
511 1.1 macallan /* pwm fan, measure speed, then adjust duty cycle */
512 1.1 macallan DPRINTF("pwm fan ");
513 1.2 macallan current_speed = fcu_get_rpm(sc, which);
514 1.2 macallan diff = current_speed - speed;
515 1.2 macallan DPRINTF("d %d s %d t %d diff %d ", f->duty, current_speed, speed, diff);
516 1.1 macallan if (diff > 100) {
517 1.1 macallan nduty = uimax(20, nduty - 1);
518 1.1 macallan }
519 1.1 macallan if (diff < -100) {
520 1.1 macallan nduty = uimin(0xd0, nduty + 1);
521 1.1 macallan }
522 1.1 macallan cmd = f->reg;
523 1.1 macallan DPRINTF("%s nduty %d", __func__, nduty);
524 1.1 macallan if (nduty != f->duty) {
525 1.1 macallan uint8_t arg = nduty;
526 1.2 macallan iic_acquire_bus(sc->sc_i2c, 0);
527 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
528 1.1 macallan sc->sc_addr, &cmd, 1, &arg, 1, 0);
529 1.2 macallan iic_release_bus(sc->sc_i2c, 0);
530 1.1 macallan f->duty = nduty;
531 1.1 macallan sc->sc_pwm = TRUE;
532 1.1 macallan
533 1.1 macallan }
534 1.1 macallan DPRINTF("ok\n");
535 1.1 macallan }
536 1.1 macallan if (error) printf("boo\n");
537 1.1 macallan f->target = speed;
538 1.2 macallan return 0;
539 1.1 macallan }
540 1.1 macallan
541 1.1 macallan static void
542 1.1 macallan fcu_adjust(void *cookie)
543 1.1 macallan {
544 1.1 macallan struct fcu_softc *sc = cookie;
545 1.1 macallan int i;
546 1.1 macallan uint8_t cmd, data;
547 1.1 macallan
548 1.1 macallan while (!sc->sc_dying) {
549 1.1 macallan /* poke the FCU so we don't go 747 */
550 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
551 1.1 macallan cmd = FCU_FAN_ACTIVE;
552 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
553 1.1 macallan sc->sc_addr, &cmd, 1, &data, 1, 0);
554 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
555 1.1 macallan sc->sc_pwm = FALSE;
556 1.1 macallan for (i = 0; i < FCU_ZONE_COUNT; i++)
557 1.2 macallan fancontrol_adjust_zone(&sc->sc_zones[i]);
558 1.3 macallan /*
559 1.5 andvar * take a shorter nap if we're in the process of adjusting a
560 1.3 macallan * PWM fan, which relies on measuring speed and then changing
561 1.3 macallan * its duty cycle until we're reasonable close to the target
562 1.3 macallan * speed
563 1.3 macallan */
564 1.3 macallan kpause("fanctrl", true, mstohz(sc->sc_pwm ? 1000 : 2000), NULL);
565 1.1 macallan }
566 1.1 macallan kthread_exit(0);
567 1.1 macallan }
568