fcu.c revision 1.2 1 1.2 macallan /* $NetBSD: fcu.c,v 1.2 2021/07/27 23:38:42 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.2 macallan __KERNEL_RCSID(0, "$NetBSD: fcu.c,v 1.2 2021/07/27 23:38:42 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.1 macallan
39 1.1 macallan #include <dev/i2c/i2cvar.h>
40 1.1 macallan
41 1.1 macallan #include <dev/sysmon/sysmonvar.h>
42 1.1 macallan
43 1.1 macallan #include <dev/ofw/openfirm.h>
44 1.1 macallan
45 1.2 macallan #include <macppc/dev/fancontrolvar.h>
46 1.2 macallan
47 1.1 macallan //#define FCU_DEBUG
48 1.1 macallan #ifdef FCU_DEBUG
49 1.1 macallan #define DPRINTF printf
50 1.1 macallan #else
51 1.1 macallan #define DPRINTF if (0) printf
52 1.1 macallan #endif
53 1.1 macallan
54 1.1 macallan /* FCU registers, from OpenBSD's fcu.c */
55 1.1 macallan #define FCU_FAN_FAIL 0x0b /* fans states in bits 0<1-6>7 */
56 1.1 macallan #define FCU_FAN_ACTIVE 0x0d
57 1.1 macallan #define FCU_FANREAD(x) 0x11 + (x)*2
58 1.1 macallan #define FCU_FANSET(x) 0x10 + (x)*2
59 1.1 macallan #define FCU_PWM_FAIL 0x2b
60 1.1 macallan #define FCU_PWM_ACTIVE 0x2d
61 1.1 macallan #define FCU_PWMREAD(x) 0x30 + (x)*2
62 1.1 macallan
63 1.1 macallan
64 1.1 macallan typedef struct _fcu_fan {
65 1.1 macallan int target;
66 1.1 macallan int reg;
67 1.1 macallan int base_rpm, max_rpm;
68 1.1 macallan int step;
69 1.1 macallan int duty; /* for pwm fans */
70 1.1 macallan } fcu_fan_t;
71 1.1 macallan
72 1.1 macallan #define FCU_ZONE_CPU 0
73 1.1 macallan #define FCU_ZONE_CASE 1
74 1.1 macallan #define FCU_ZONE_DRIVEBAY 2
75 1.1 macallan #define FCU_ZONE_COUNT 3
76 1.1 macallan
77 1.1 macallan struct fcu_softc {
78 1.1 macallan device_t sc_dev;
79 1.1 macallan i2c_tag_t sc_i2c;
80 1.1 macallan i2c_addr_t sc_addr;
81 1.1 macallan
82 1.1 macallan struct sysmon_envsys *sc_sme;
83 1.2 macallan envsys_data_t sc_sensors[32];
84 1.2 macallan int sc_nsensors;
85 1.2 macallan fancontrol_zone_t sc_zones[FCU_ZONE_COUNT];
86 1.2 macallan fcu_fan_t sc_fans[FANCONTROL_MAX_FANS];
87 1.2 macallan int sc_nfans;
88 1.2 macallan lwp_t *sc_thread;
89 1.2 macallan bool sc_dying, sc_pwm;
90 1.2 macallan uint8_t sc_eeprom0[160];
91 1.2 macallan uint8_t sc_eeprom1[160];
92 1.1 macallan };
93 1.1 macallan
94 1.1 macallan static int fcu_match(device_t, cfdata_t, void *);
95 1.1 macallan static void fcu_attach(device_t, device_t, void *);
96 1.1 macallan
97 1.1 macallan static void fcu_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
98 1.1 macallan
99 1.1 macallan static bool is_cpu(const envsys_data_t *);
100 1.1 macallan static bool is_case(const envsys_data_t *);
101 1.1 macallan static bool is_drive(const envsys_data_t *);
102 1.1 macallan
103 1.2 macallan static int fcu_set_rpm(void *, int, int);
104 1.2 macallan static int fcu_get_rpm(void *, int);
105 1.1 macallan static void fcu_adjust(void *);
106 1.1 macallan
107 1.1 macallan CFATTACH_DECL_NEW(fcu, sizeof(struct fcu_softc),
108 1.1 macallan fcu_match, fcu_attach, NULL, NULL);
109 1.1 macallan
110 1.1 macallan static const struct device_compatible_entry compat_data[] = {
111 1.1 macallan { .compat = "fcu" },
112 1.1 macallan DEVICE_COMPAT_EOL
113 1.1 macallan };
114 1.1 macallan
115 1.1 macallan static int
116 1.1 macallan fcu_match(device_t parent, cfdata_t match, void *aux)
117 1.1 macallan {
118 1.1 macallan struct i2c_attach_args *ia = aux;
119 1.1 macallan int match_result;
120 1.1 macallan
121 1.1 macallan if (iic_use_direct_match(ia, match, compat_data, &match_result))
122 1.1 macallan return match_result;
123 1.1 macallan
124 1.1 macallan if (ia->ia_addr == 0x2f)
125 1.1 macallan return I2C_MATCH_ADDRESS_ONLY;
126 1.1 macallan
127 1.1 macallan return 0;
128 1.1 macallan }
129 1.1 macallan
130 1.1 macallan static void
131 1.1 macallan fcu_attach(device_t parent, device_t self, void *aux)
132 1.1 macallan {
133 1.1 macallan struct fcu_softc *sc = device_private(self);
134 1.1 macallan struct i2c_attach_args *ia = aux;
135 1.1 macallan int have_eeprom1 = 1;
136 1.1 macallan
137 1.1 macallan sc->sc_dev = self;
138 1.1 macallan sc->sc_i2c = ia->ia_tag;
139 1.1 macallan sc->sc_addr = ia->ia_addr;
140 1.1 macallan
141 1.1 macallan aprint_naive("\n");
142 1.1 macallan aprint_normal(": Fan Control Unit\n");
143 1.1 macallan
144 1.1 macallan if (get_cpuid(0, sc->sc_eeprom0) < 160) {
145 1.1 macallan /*
146 1.1 macallan * XXX this should never happen, we depend on the EEPROM for
147 1.1 macallan * calibration data to make sense of temperature and voltage
148 1.1 macallan * sensors elsewhere, and fan parameters here.
149 1.1 macallan */
150 1.1 macallan aprint_error_dev(self, "no EEPROM data for CPU 0\n");
151 1.1 macallan return;
152 1.1 macallan }
153 1.1 macallan if (get_cpuid(1, sc->sc_eeprom1) < 160)
154 1.1 macallan have_eeprom1 = 0;
155 1.1 macallan
156 1.1 macallan /* init zones */
157 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].name = "CPUs";
158 1.1 macallan sc->sc_zones[FCU_ZONE_CPU].filter = is_cpu;
159 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].cookie = sc;
160 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].get_rpm = fcu_get_rpm;
161 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].set_rpm = fcu_set_rpm;
162 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].Tmin = 50;
163 1.2 macallan sc->sc_zones[FCU_ZONE_CPU].Tmax = 85;
164 1.1 macallan sc->sc_zones[FCU_ZONE_CPU].nfans = 0;
165 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].name = "Slots";
166 1.1 macallan sc->sc_zones[FCU_ZONE_CASE].filter = is_case;
167 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].Tmin = 50;
168 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].cookie = sc;
169 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].get_rpm = fcu_get_rpm;
170 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].set_rpm = fcu_set_rpm;
171 1.2 macallan sc->sc_zones[FCU_ZONE_CASE].Tmax = 75;
172 1.1 macallan sc->sc_zones[FCU_ZONE_CASE].nfans = 0;
173 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].name = "Drive bays";
174 1.1 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].filter = is_drive;
175 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].cookie = sc;
176 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].get_rpm = fcu_get_rpm;
177 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].set_rpm = fcu_set_rpm;
178 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmin = 30;
179 1.2 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].Tmax = 60;
180 1.1 macallan sc->sc_zones[FCU_ZONE_DRIVEBAY].nfans = 0;
181 1.1 macallan
182 1.1 macallan sc->sc_sme = sysmon_envsys_create();
183 1.1 macallan sc->sc_sme->sme_name = device_xname(self);
184 1.1 macallan sc->sc_sme->sme_cookie = sc;
185 1.1 macallan sc->sc_sme->sme_refresh = fcu_sensors_refresh;
186 1.1 macallan
187 1.1 macallan sc->sc_sensors[0].units = ENVSYS_SFANRPM;
188 1.1 macallan sc->sc_sensors[1].state = ENVSYS_SINVALID;
189 1.1 macallan sc->sc_nfans = 0;
190 1.1 macallan
191 1.1 macallan /* round up sensors */
192 1.1 macallan int ch;
193 1.1 macallan
194 1.1 macallan sc->sc_nsensors = 0;
195 1.1 macallan ch = OF_child(ia->ia_cookie);
196 1.1 macallan while (ch != 0) {
197 1.1 macallan char type[32], descr[32];
198 1.1 macallan uint32_t reg;
199 1.1 macallan
200 1.1 macallan envsys_data_t *s = &sc->sc_sensors[sc->sc_nsensors];
201 1.1 macallan
202 1.1 macallan s->state = ENVSYS_SINVALID;
203 1.1 macallan
204 1.1 macallan if (OF_getprop(ch, "device_type", type, 32) <= 0)
205 1.1 macallan goto next;
206 1.1 macallan
207 1.1 macallan if (strcmp(type, "fan-rpm-control") == 0) {
208 1.1 macallan s->units = ENVSYS_SFANRPM;
209 1.1 macallan } else if (strcmp(type, "fan-pwm-control") == 0) {
210 1.1 macallan /* XXX we get the type from the register number */
211 1.1 macallan s->units = ENVSYS_SFANRPM;
212 1.1 macallan /* skip those for now since we don't really know how to interpret them */
213 1.1 macallan #if 0
214 1.1 macallan } else if (strcmp(type, "power-sensor") == 0) {
215 1.1 macallan s->units = ENVSYS_SVOLTS_DC;
216 1.1 macallan #endif
217 1.1 macallan } else if (strcmp(type, "gpi-sensor") == 0) {
218 1.1 macallan s->units = ENVSYS_INDICATOR;
219 1.1 macallan } else {
220 1.1 macallan /* ignore other types for now */
221 1.1 macallan goto next;
222 1.1 macallan }
223 1.1 macallan
224 1.1 macallan if (OF_getprop(ch, "reg", ®, sizeof(reg)) <= 0)
225 1.1 macallan goto next;
226 1.1 macallan s->private = reg;
227 1.1 macallan
228 1.1 macallan if (OF_getprop(ch, "location", descr, 32) <= 0)
229 1.1 macallan goto next;
230 1.1 macallan strcpy(s->desc, descr);
231 1.1 macallan
232 1.1 macallan if (s->units == ENVSYS_SFANRPM) {
233 1.1 macallan fcu_fan_t *fan = &sc->sc_fans[sc->sc_nfans];
234 1.1 macallan uint8_t *eeprom = NULL;
235 1.1 macallan uint16_t rmin, rmax;
236 1.1 macallan
237 1.1 macallan if (strstr(descr, "CPU A") != NULL)
238 1.1 macallan eeprom = sc->sc_eeprom0;
239 1.1 macallan if (strstr(descr, "CPU B") != NULL) {
240 1.1 macallan /*
241 1.1 macallan * XXX
242 1.1 macallan * this should never happen
243 1.1 macallan */
244 1.1 macallan if (have_eeprom1 == 0) {
245 1.1 macallan eeprom = sc->sc_eeprom0;
246 1.1 macallan } else
247 1.1 macallan eeprom = sc->sc_eeprom1;
248 1.1 macallan }
249 1.1 macallan
250 1.1 macallan fan->reg = reg;
251 1.1 macallan fan->target = 0;
252 1.1 macallan fan->duty = 0x80;
253 1.1 macallan
254 1.1 macallan /* speed settings from EEPROM */
255 1.1 macallan if (strstr(descr, "PUMP") != NULL) {
256 1.1 macallan KASSERT(eeprom != NULL);
257 1.1 macallan memcpy(&rmin, &eeprom[0x54], 2);
258 1.1 macallan memcpy(&rmax, &eeprom[0x56], 2);
259 1.1 macallan fan->base_rpm = rmin;
260 1.1 macallan fan->max_rpm = rmax;
261 1.1 macallan fan->step = (rmax - rmin) / 30;
262 1.1 macallan } else if (strstr(descr, "INTAKE") != NULL) {
263 1.1 macallan KASSERT(eeprom != NULL);
264 1.1 macallan memcpy(&rmin, &eeprom[0x4c], 2);
265 1.1 macallan memcpy(&rmax, &eeprom[0x4e], 2);
266 1.1 macallan fan->base_rpm = rmin;
267 1.1 macallan fan->max_rpm = rmax;
268 1.1 macallan fan->step = (rmax - rmin) / 30;
269 1.1 macallan } else if (strstr(descr, "EXHAUST") != NULL) {
270 1.1 macallan KASSERT(eeprom != NULL);
271 1.1 macallan memcpy(&rmin, &eeprom[0x50], 2);
272 1.1 macallan memcpy(&rmax, &eeprom[0x52], 2);
273 1.1 macallan fan->base_rpm = rmin;
274 1.1 macallan fan->max_rpm = rmax;
275 1.1 macallan fan->step = (rmax - rmin) / 30;
276 1.1 macallan } else if (strstr(descr, "DRIVE") != NULL ) {
277 1.1 macallan fan->base_rpm = 1000;
278 1.1 macallan fan->max_rpm = 3000;
279 1.1 macallan fan->step = 100;
280 1.1 macallan } else {
281 1.1 macallan fan->base_rpm = 1000;
282 1.1 macallan fan->max_rpm = 3000;
283 1.1 macallan fan->step = 100;
284 1.1 macallan }
285 1.1 macallan DPRINTF("fan %s: %d - %d rpm, step %d\n",
286 1.1 macallan descr, fan->base_rpm, fan->max_rpm, fan->step);
287 1.1 macallan
288 1.1 macallan /* now stuff them into zones */
289 1.1 macallan if (strstr(descr, "CPU") != NULL) {
290 1.2 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CPU];
291 1.2 macallan z->fans[z->nfans].num = sc->sc_nfans;
292 1.2 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
293 1.2 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
294 1.2 macallan z->fans[z->nfans].name = s->desc;
295 1.1 macallan z->nfans++;
296 1.1 macallan } else if ((strstr(descr, "BACKSIDE") != NULL) ||
297 1.1 macallan (strstr(descr, "SLOT") != NULL)) {
298 1.2 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_CASE];
299 1.2 macallan z->fans[z->nfans].num = sc->sc_nfans;
300 1.2 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
301 1.2 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
302 1.2 macallan z->fans[z->nfans].name = s->desc;
303 1.1 macallan z->nfans++;
304 1.1 macallan } else if (strstr(descr, "DRIVE") != NULL) {
305 1.2 macallan fancontrol_zone_t *z = &sc->sc_zones[FCU_ZONE_DRIVEBAY];
306 1.2 macallan z->fans[z->nfans].num = sc->sc_nfans;
307 1.2 macallan z->fans[z->nfans].min_rpm = fan->base_rpm;
308 1.2 macallan z->fans[z->nfans].max_rpm = fan->max_rpm;
309 1.2 macallan z->fans[z->nfans].name = s->desc;
310 1.1 macallan z->nfans++;
311 1.1 macallan }
312 1.1 macallan sc->sc_nfans++;
313 1.1 macallan }
314 1.1 macallan sysmon_envsys_sensor_attach(sc->sc_sme, s);
315 1.1 macallan sc->sc_nsensors++;
316 1.1 macallan next:
317 1.1 macallan ch = OF_peer(ch);
318 1.1 macallan }
319 1.1 macallan sysmon_envsys_register(sc->sc_sme);
320 1.1 macallan
321 1.1 macallan sc->sc_dying = FALSE;
322 1.1 macallan kthread_create(PRI_NONE, 0, curcpu(), fcu_adjust, sc, &sc->sc_thread,
323 1.1 macallan "fan control");
324 1.1 macallan }
325 1.1 macallan
326 1.1 macallan static void
327 1.1 macallan fcu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
328 1.1 macallan {
329 1.1 macallan struct fcu_softc *sc = sme->sme_cookie;
330 1.1 macallan uint8_t cmd;
331 1.1 macallan uint16_t data = -1;
332 1.1 macallan int error;
333 1.1 macallan
334 1.1 macallan if (edata->units == ENVSYS_SFANRPM) {
335 1.1 macallan cmd = edata->private + 1;
336 1.1 macallan } else
337 1.1 macallan cmd = edata->private;
338 1.1 macallan
339 1.1 macallan /* fcu is a macppc only thing so we can safely assume big endian */
340 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
341 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
342 1.1 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
343 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
344 1.1 macallan
345 1.1 macallan if (error) {
346 1.1 macallan edata->state = ENVSYS_SINVALID;
347 1.1 macallan return;
348 1.1 macallan }
349 1.1 macallan
350 1.1 macallan edata->state = ENVSYS_SVALID;
351 1.1 macallan
352 1.1 macallan switch (edata->units) {
353 1.1 macallan case ENVSYS_SFANRPM:
354 1.1 macallan edata->value_cur = data >> 3;
355 1.1 macallan break;
356 1.1 macallan case ENVSYS_SVOLTS_DC:
357 1.1 macallan /* XXX this reads bogus */
358 1.1 macallan edata->value_cur = data * 1000;
359 1.1 macallan break;
360 1.1 macallan case ENVSYS_INDICATOR:
361 1.1 macallan /* guesswork for now */
362 1.1 macallan edata->value_cur = data >> 8;
363 1.1 macallan break;
364 1.1 macallan default:
365 1.1 macallan edata->state = ENVSYS_SINVALID;
366 1.1 macallan }
367 1.1 macallan }
368 1.1 macallan
369 1.1 macallan static bool
370 1.1 macallan is_cpu(const envsys_data_t *edata)
371 1.1 macallan {
372 1.1 macallan if (edata->units != ENVSYS_STEMP)
373 1.1 macallan return false;
374 1.1 macallan if (strstr(edata->desc, "CPU") != NULL)
375 1.1 macallan return TRUE;
376 1.1 macallan return false;
377 1.1 macallan }
378 1.1 macallan
379 1.1 macallan static bool
380 1.1 macallan is_case(const envsys_data_t *edata)
381 1.1 macallan {
382 1.1 macallan if (edata->units != ENVSYS_STEMP)
383 1.1 macallan return false;
384 1.1 macallan if ((strstr(edata->desc, "MLB") != NULL) ||
385 1.1 macallan (strstr(edata->desc, "BACKSIDE") != NULL) ||
386 1.1 macallan (strstr(edata->desc, "U3") != NULL))
387 1.1 macallan return TRUE;
388 1.1 macallan return false;
389 1.1 macallan }
390 1.1 macallan
391 1.1 macallan static bool
392 1.1 macallan is_drive(const envsys_data_t *edata)
393 1.1 macallan {
394 1.1 macallan if (edata->units != ENVSYS_STEMP)
395 1.1 macallan return false;
396 1.1 macallan if (strstr(edata->desc, "DRIVE") != NULL)
397 1.1 macallan return TRUE;
398 1.1 macallan return false;
399 1.1 macallan }
400 1.1 macallan
401 1.2 macallan static int
402 1.2 macallan fcu_get_rpm(void *cookie, int which)
403 1.1 macallan {
404 1.2 macallan struct fcu_softc *sc = cookie;
405 1.2 macallan fcu_fan_t *f = &sc->sc_fans[which];
406 1.1 macallan int error;
407 1.2 macallan uint16_t data;
408 1.2 macallan uint8_t cmd;
409 1.2 macallan
410 1.2 macallan iic_acquire_bus(sc->sc_i2c, 0);
411 1.2 macallan cmd = f->reg + 1;
412 1.2 macallan error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
413 1.2 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
414 1.2 macallan iic_release_bus(sc->sc_i2c, 0);
415 1.2 macallan if (error != 0) return -1;
416 1.2 macallan data = data >> 3;
417 1.2 macallan return data;
418 1.2 macallan }
419 1.2 macallan
420 1.2 macallan static int
421 1.2 macallan fcu_set_rpm(void *cookie, int which, int speed)
422 1.2 macallan {
423 1.2 macallan struct fcu_softc *sc = cookie;
424 1.2 macallan fcu_fan_t *f = &sc->sc_fans[which];
425 1.2 macallan int error = 0;
426 1.1 macallan uint8_t cmd;
427 1.1 macallan
428 1.1 macallan if (speed > f->max_rpm) speed = f->max_rpm;
429 1.1 macallan if (speed < f->base_rpm) speed = f->base_rpm;
430 1.1 macallan
431 1.1 macallan if (f->reg < 0x30) {
432 1.1 macallan uint16_t data;
433 1.1 macallan /* simple rpm fan, just poke the register */
434 1.1 macallan
435 1.2 macallan if (f->target == speed) return 0;
436 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
437 1.1 macallan cmd = f->reg;
438 1.1 macallan data = (speed << 3);
439 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
440 1.1 macallan sc->sc_addr, &cmd, 1, &data, 2, 0);
441 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
442 1.1 macallan } else {
443 1.1 macallan int diff;
444 1.1 macallan int nduty = f->duty;
445 1.2 macallan int current_speed;
446 1.1 macallan /* pwm fan, measure speed, then adjust duty cycle */
447 1.1 macallan DPRINTF("pwm fan ");
448 1.2 macallan current_speed = fcu_get_rpm(sc, which);
449 1.2 macallan diff = current_speed - speed;
450 1.2 macallan DPRINTF("d %d s %d t %d diff %d ", f->duty, current_speed, speed, diff);
451 1.1 macallan if (diff > 100) {
452 1.1 macallan nduty = uimax(20, nduty - 1);
453 1.1 macallan }
454 1.1 macallan if (diff < -100) {
455 1.1 macallan nduty = uimin(0xd0, nduty + 1);
456 1.1 macallan }
457 1.1 macallan cmd = f->reg;
458 1.1 macallan DPRINTF("%s nduty %d", __func__, nduty);
459 1.1 macallan if (nduty != f->duty) {
460 1.1 macallan uint8_t arg = nduty;
461 1.2 macallan iic_acquire_bus(sc->sc_i2c, 0);
462 1.1 macallan error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
463 1.1 macallan sc->sc_addr, &cmd, 1, &arg, 1, 0);
464 1.2 macallan iic_release_bus(sc->sc_i2c, 0);
465 1.1 macallan f->duty = nduty;
466 1.1 macallan sc->sc_pwm = TRUE;
467 1.1 macallan
468 1.1 macallan }
469 1.1 macallan DPRINTF("ok\n");
470 1.1 macallan }
471 1.1 macallan if (error) printf("boo\n");
472 1.1 macallan f->target = speed;
473 1.2 macallan return 0;
474 1.1 macallan }
475 1.1 macallan
476 1.1 macallan static void
477 1.1 macallan fcu_adjust(void *cookie)
478 1.1 macallan {
479 1.1 macallan struct fcu_softc *sc = cookie;
480 1.1 macallan int i;
481 1.1 macallan uint8_t cmd, data;
482 1.1 macallan
483 1.1 macallan while (!sc->sc_dying) {
484 1.1 macallan /* poke the FCU so we don't go 747 */
485 1.1 macallan iic_acquire_bus(sc->sc_i2c, 0);
486 1.1 macallan cmd = FCU_FAN_ACTIVE;
487 1.1 macallan iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
488 1.1 macallan sc->sc_addr, &cmd, 1, &data, 1, 0);
489 1.1 macallan iic_release_bus(sc->sc_i2c, 0);
490 1.1 macallan sc->sc_pwm = FALSE;
491 1.1 macallan for (i = 0; i < FCU_ZONE_COUNT; i++)
492 1.2 macallan fancontrol_adjust_zone(&sc->sc_zones[i]);
493 1.1 macallan kpause("fanctrl", true, mstohz(sc->sc_pwm ? 1000 : 5000), NULL);
494 1.1 macallan }
495 1.1 macallan kthread_exit(0);
496 1.1 macallan }
497