1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Windfarm PowerMac thermal control. iMac G5
4 *
5 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
6 * <benh@kernel.crashing.org>
7 *
8 * The algorithm used is the PID control algorithm, used the same
9 * way the published Darwin code does, using the same values that
10 * are present in the Darwin 8.2 snapshot property lists (note however
11 * that none of the code has been re-used, it's a complete re-implementation
12 *
13 * The various control loops found in Darwin config file are:
14 *
15 * PowerMac8,1 and PowerMac8,2
16 * ===========================
17 *
18 * System Fans control loop. Different based on models. In addition to the
19 * usual PID algorithm, the control loop gets 2 additional pairs of linear
20 * scaling factors (scale/offsets) expressed as 4.12 fixed point values
21 * signed offset, unsigned scale)
22 *
23 * The targets are modified such as:
24 * - the linked control (second control) gets the target value as-is
25 * (typically the drive fan)
26 * - the main control (first control) gets the target value scaled with
27 * the first pair of factors, and is then modified as below
28 * - the value of the target of the CPU Fan control loop is retrieved,
29 * scaled with the second pair of factors, and the max of that and
30 * the scaled target is applied to the main control.
31 *
32 * # model_id: 2
33 * controls : system-fan, drive-bay-fan
34 * sensors : hd-temp
35 * PID params : G_d = 0x15400000
36 * G_p = 0x00200000
37 * G_r = 0x000002fd
38 * History = 2 entries
39 * Input target = 0x3a0000
40 * Interval = 5s
41 * linear-factors : offset = 0xff38 scale = 0x0ccd
42 * offset = 0x0208 scale = 0x07ae
43 *
44 * # model_id: 3
45 * controls : system-fan, drive-bay-fan
46 * sensors : hd-temp
47 * PID params : G_d = 0x08e00000
48 * G_p = 0x00566666
49 * G_r = 0x0000072b
50 * History = 2 entries
51 * Input target = 0x350000
52 * Interval = 5s
53 * linear-factors : offset = 0xff38 scale = 0x0ccd
54 * offset = 0x0000 scale = 0x0000
55 *
56 * # model_id: 5
57 * controls : system-fan
58 * sensors : hd-temp
59 * PID params : G_d = 0x15400000
60 * G_p = 0x00233333
61 * G_r = 0x000002fd
62 * History = 2 entries
63 * Input target = 0x3a0000
64 * Interval = 5s
65 * linear-factors : offset = 0x0000 scale = 0x1000
66 * offset = 0x0091 scale = 0x0bae
67 *
68 * CPU Fan control loop. The loop is identical for all models. it
69 * has an additional pair of scaling factor. This is used to scale the
70 * systems fan control loop target result (the one before it gets scaled
71 * by the System Fans control loop itself). Then, the max value of the
72 * calculated target value and system fan value is sent to the fans
73 *
74 * controls : cpu-fan
75 * sensors : cpu-temp cpu-power
76 * PID params : From SMU sdb partition
77 * linear-factors : offset = 0xfb50 scale = 0x1000
78 *
79 * CPU Slew control loop. Not implemented. The cpufreq driver in linux is
80 * completely separate for now, though we could find a way to link it, either
81 * as a client reacting to overtemp notifications, or directling monitoring
82 * the CPU temperature
83 *
84 * WARNING ! The CPU control loop requires the CPU tmax for the current
85 * operating point. However, we currently are completely separated from
86 * the cpufreq driver and thus do not know what the current operating
87 * point is. Fortunately, we also do not have any hardware supporting anything
88 * but operating point 0 at the moment, thus we just peek that value directly
89 * from the SDB partition. If we ever end up with actually slewing the system
90 * clock and thus changing operating points, we'll have to find a way to
91 * communicate with the CPU freq driver;
92 */
93
94 #include <linux/types.h>
95 #include <linux/errno.h>
96 #include <linux/kernel.h>
97 #include <linux/delay.h>
98 #include <linux/slab.h>
99 #include <linux/init.h>
100 #include <linux/spinlock.h>
101 #include <linux/wait.h>
102 #include <linux/kmod.h>
103 #include <linux/device.h>
104 #include <linux/platform_device.h>
105 #include <linux/of.h>
106
107 #include <asm/machdep.h>
108 #include <asm/io.h>
109 #include <asm/sections.h>
110 #include <asm/smu.h>
111
112 #include "windfarm.h"
113 #include "windfarm_pid.h"
114
115 #define VERSION "0.4"
116
117 #undef DEBUG
118
119 #ifdef DEBUG
120 #define DBG(args...) printk(args)
121 #else
122 #define DBG(args...) do { } while(0)
123 #endif
124
125 /* define this to force CPU overtemp to 74 degree, useful for testing
126 * the overtemp code
127 */
128 #undef HACKED_OVERTEMP
129
130 static int wf_smu_mach_model; /* machine model id */
131
132 /* Controls & sensors */
133 static struct wf_sensor *sensor_cpu_power;
134 static struct wf_sensor *sensor_cpu_temp;
135 static struct wf_sensor *sensor_hd_temp;
136 static struct wf_control *fan_cpu_main;
137 static struct wf_control *fan_hd;
138 static struct wf_control *fan_system;
139 static struct wf_control *cpufreq_clamp;
140
141 /* Set to kick the control loop into life */
142 static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok;
143 static bool wf_smu_started;
144
145 /* Failure handling.. could be nicer */
146 #define FAILURE_FAN 0x01
147 #define FAILURE_SENSOR 0x02
148 #define FAILURE_OVERTEMP 0x04
149
150 static unsigned int wf_smu_failure_state;
151 static int wf_smu_readjust, wf_smu_skipping;
152 static bool wf_smu_overtemp;
153
154 /*
155 * ****** System Fans Control Loop ******
156 *
157 */
158
159 /* Parameters for the System Fans control loop. Parameters
160 * not in this table such as interval, history size, ...
161 * are common to all versions and thus hard coded for now.
162 */
163 struct wf_smu_sys_fans_param {
164 int model_id;
165 s32 itarget;
166 s32 gd, gp, gr;
167
168 s16 offset0;
169 u16 scale0;
170 s16 offset1;
171 u16 scale1;
172 };
173
174 #define WF_SMU_SYS_FANS_INTERVAL 5
175 #define WF_SMU_SYS_FANS_HISTORY_SIZE 2
176
177 /* State data used by the system fans control loop
178 */
179 struct wf_smu_sys_fans_state {
180 int ticks;
181 s32 sys_setpoint;
182 s32 hd_setpoint;
183 s16 offset0;
184 u16 scale0;
185 s16 offset1;
186 u16 scale1;
187 struct wf_pid_state pid;
188 };
189
190 /*
191 * Configs for SMU System Fan control loop
192 */
193 static struct wf_smu_sys_fans_param wf_smu_sys_all_params[] = {
194 /* Model ID 2 */
195 {
196 .model_id = 2,
197 .itarget = 0x3a0000,
198 .gd = 0x15400000,
199 .gp = 0x00200000,
200 .gr = 0x000002fd,
201 .offset0 = 0xff38,
202 .scale0 = 0x0ccd,
203 .offset1 = 0x0208,
204 .scale1 = 0x07ae,
205 },
206 /* Model ID 3 */
207 {
208 .model_id = 3,
209 .itarget = 0x350000,
210 .gd = 0x08e00000,
211 .gp = 0x00566666,
212 .gr = 0x0000072b,
213 .offset0 = 0xff38,
214 .scale0 = 0x0ccd,
215 .offset1 = 0x0000,
216 .scale1 = 0x0000,
217 },
218 /* Model ID 5 */
219 {
220 .model_id = 5,
221 .itarget = 0x3a0000,
222 .gd = 0x15400000,
223 .gp = 0x00233333,
224 .gr = 0x000002fd,
225 .offset0 = 0x0000,
226 .scale0 = 0x1000,
227 .offset1 = 0x0091,
228 .scale1 = 0x0bae,
229 },
230 };
231 #define WF_SMU_SYS_FANS_NUM_CONFIGS ARRAY_SIZE(wf_smu_sys_all_params)
232
233 static struct wf_smu_sys_fans_state *wf_smu_sys_fans;
234
235 /*
236 * ****** CPU Fans Control Loop ******
237 *
238 */
239
240
241 #define WF_SMU_CPU_FANS_INTERVAL 1
242 #define WF_SMU_CPU_FANS_MAX_HISTORY 16
243 #define WF_SMU_CPU_FANS_SIBLING_SCALE 0x00001000
244 #define WF_SMU_CPU_FANS_SIBLING_OFFSET 0xfffffb50
245
246 /* State data used by the cpu fans control loop
247 */
248 struct wf_smu_cpu_fans_state {
249 int ticks;
250 s32 cpu_setpoint;
251 s32 scale;
252 s32 offset;
253 struct wf_cpu_pid_state pid;
254 };
255
256 static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
257
258
259
260 /*
261 * ***** Implementation *****
262 *
263 */
264
wf_smu_create_sys_fans(void)265 static void wf_smu_create_sys_fans(void)
266 {
267 struct wf_smu_sys_fans_param *param = NULL;
268 struct wf_pid_param pid_param;
269 int i;
270
271 /* First, locate the params for this model */
272 for (i = 0; i < WF_SMU_SYS_FANS_NUM_CONFIGS; i++)
273 if (wf_smu_sys_all_params[i].model_id == wf_smu_mach_model) {
274 param = &wf_smu_sys_all_params[i];
275 break;
276 }
277
278 /* No params found, put fans to max */
279 if (param == NULL) {
280 printk(KERN_WARNING "windfarm: System fan config not found "
281 "for this machine model, max fan speed\n");
282 goto fail;
283 }
284
285 /* Alloc & initialize state */
286 wf_smu_sys_fans = kmalloc_obj(struct wf_smu_sys_fans_state);
287 if (wf_smu_sys_fans == NULL) {
288 printk(KERN_WARNING "windfarm: Memory allocation error"
289 " max fan speed\n");
290 goto fail;
291 }
292 wf_smu_sys_fans->ticks = 1;
293 wf_smu_sys_fans->scale0 = param->scale0;
294 wf_smu_sys_fans->offset0 = param->offset0;
295 wf_smu_sys_fans->scale1 = param->scale1;
296 wf_smu_sys_fans->offset1 = param->offset1;
297
298 /* Fill PID params */
299 pid_param.gd = param->gd;
300 pid_param.gp = param->gp;
301 pid_param.gr = param->gr;
302 pid_param.interval = WF_SMU_SYS_FANS_INTERVAL;
303 pid_param.history_len = WF_SMU_SYS_FANS_HISTORY_SIZE;
304 pid_param.itarget = param->itarget;
305 pid_param.min = wf_control_get_min(fan_system);
306 pid_param.max = wf_control_get_max(fan_system);
307 if (fan_hd) {
308 pid_param.min =
309 max(pid_param.min, wf_control_get_min(fan_hd));
310 pid_param.max =
311 min(pid_param.max, wf_control_get_max(fan_hd));
312 }
313 wf_pid_init(&wf_smu_sys_fans->pid, &pid_param);
314
315 DBG("wf: System Fan control initialized.\n");
316 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
317 FIX32TOPRINT(pid_param.itarget), pid_param.min, pid_param.max);
318 return;
319
320 fail:
321
322 if (fan_system)
323 wf_control_set_max(fan_system);
324 if (fan_hd)
325 wf_control_set_max(fan_hd);
326 }
327
wf_smu_sys_fans_tick(struct wf_smu_sys_fans_state * st)328 static void wf_smu_sys_fans_tick(struct wf_smu_sys_fans_state *st)
329 {
330 s32 new_setpoint, temp, scaled, cputarget;
331 int rc;
332
333 if (--st->ticks != 0) {
334 if (wf_smu_readjust)
335 goto readjust;
336 return;
337 }
338 st->ticks = WF_SMU_SYS_FANS_INTERVAL;
339
340 rc = wf_sensor_get(sensor_hd_temp, &temp);
341 if (rc) {
342 printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
343 rc);
344 wf_smu_failure_state |= FAILURE_SENSOR;
345 return;
346 }
347
348 DBG("wf_smu: System Fans tick ! HD temp: %d.%03d\n",
349 FIX32TOPRINT(temp));
350
351 if (temp > (st->pid.param.itarget + 0x50000))
352 wf_smu_failure_state |= FAILURE_OVERTEMP;
353
354 new_setpoint = wf_pid_run(&st->pid, temp);
355
356 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
357
358 scaled = ((((s64)new_setpoint) * (s64)st->scale0) >> 12) + st->offset0;
359
360 DBG("wf_smu: scaled setpoint: %d RPM\n", (int)scaled);
361
362 cputarget = wf_smu_cpu_fans ? wf_smu_cpu_fans->pid.target : 0;
363 cputarget = ((((s64)cputarget) * (s64)st->scale1) >> 12) + st->offset1;
364 scaled = max(scaled, cputarget);
365 scaled = max(scaled, st->pid.param.min);
366 scaled = min(scaled, st->pid.param.max);
367
368 DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)scaled);
369
370 if (st->sys_setpoint == scaled && new_setpoint == st->hd_setpoint)
371 return;
372 st->sys_setpoint = scaled;
373 st->hd_setpoint = new_setpoint;
374 readjust:
375 if (fan_system && wf_smu_failure_state == 0) {
376 rc = wf_control_set(fan_system, st->sys_setpoint);
377 if (rc) {
378 printk(KERN_WARNING "windfarm: Sys fan error %d\n",
379 rc);
380 wf_smu_failure_state |= FAILURE_FAN;
381 }
382 }
383 if (fan_hd && wf_smu_failure_state == 0) {
384 rc = wf_control_set(fan_hd, st->hd_setpoint);
385 if (rc) {
386 printk(KERN_WARNING "windfarm: HD fan error %d\n",
387 rc);
388 wf_smu_failure_state |= FAILURE_FAN;
389 }
390 }
391 }
392
wf_smu_create_cpu_fans(void)393 static void wf_smu_create_cpu_fans(void)
394 {
395 struct wf_cpu_pid_param pid_param;
396 const struct smu_sdbp_header *hdr;
397 struct smu_sdbp_cpupiddata *piddata;
398 struct smu_sdbp_fvt *fvt;
399 s32 tmax, tdelta, maxpow, powadj;
400
401 /* First, locate the PID params in SMU SBD */
402 hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
403 if (!hdr) {
404 printk(KERN_WARNING "windfarm: CPU PID fan config not found "
405 "max fan speed\n");
406 goto fail;
407 }
408 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
409
410 /* Get the FVT params for operating point 0 (the only supported one
411 * for now) in order to get tmax
412 */
413 hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
414 if (hdr) {
415 fvt = (struct smu_sdbp_fvt *)&hdr[1];
416 tmax = ((s32)fvt->maxtemp) << 16;
417 } else
418 tmax = 0x5e0000; /* 94 degree default */
419
420 /* Alloc & initialize state */
421 wf_smu_cpu_fans = kmalloc_obj(struct wf_smu_cpu_fans_state);
422 if (wf_smu_cpu_fans == NULL)
423 goto fail;
424 wf_smu_cpu_fans->ticks = 1;
425
426 wf_smu_cpu_fans->scale = WF_SMU_CPU_FANS_SIBLING_SCALE;
427 wf_smu_cpu_fans->offset = WF_SMU_CPU_FANS_SIBLING_OFFSET;
428
429 /* Fill PID params */
430 pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
431 pid_param.history_len = piddata->history_len;
432 if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
433 printk(KERN_WARNING "windfarm: History size overflow on "
434 "CPU control loop (%d)\n", piddata->history_len);
435 pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
436 }
437 pid_param.gd = piddata->gd;
438 pid_param.gp = piddata->gp;
439 pid_param.gr = piddata->gr / pid_param.history_len;
440
441 tdelta = ((s32)piddata->target_temp_delta) << 16;
442 maxpow = ((s32)piddata->max_power) << 16;
443 powadj = ((s32)piddata->power_adj) << 16;
444
445 pid_param.tmax = tmax;
446 pid_param.ttarget = tmax - tdelta;
447 pid_param.pmaxadj = maxpow - powadj;
448
449 pid_param.min = wf_control_get_min(fan_cpu_main);
450 pid_param.max = wf_control_get_max(fan_cpu_main);
451
452 wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
453
454 DBG("wf: CPU Fan control initialized.\n");
455 DBG(" ttarget=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
456 FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
457 pid_param.min, pid_param.max);
458
459 return;
460
461 fail:
462 printk(KERN_WARNING "windfarm: CPU fan config not found\n"
463 "for this machine model, max fan speed\n");
464
465 if (cpufreq_clamp)
466 wf_control_set_max(cpufreq_clamp);
467 if (fan_cpu_main)
468 wf_control_set_max(fan_cpu_main);
469 }
470
wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state * st)471 static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
472 {
473 s32 new_setpoint, temp, power, systarget;
474 int rc;
475
476 if (--st->ticks != 0) {
477 if (wf_smu_readjust)
478 goto readjust;
479 return;
480 }
481 st->ticks = WF_SMU_CPU_FANS_INTERVAL;
482
483 rc = wf_sensor_get(sensor_cpu_temp, &temp);
484 if (rc) {
485 printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
486 rc);
487 wf_smu_failure_state |= FAILURE_SENSOR;
488 return;
489 }
490
491 rc = wf_sensor_get(sensor_cpu_power, &power);
492 if (rc) {
493 printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
494 rc);
495 wf_smu_failure_state |= FAILURE_SENSOR;
496 return;
497 }
498
499 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
500 FIX32TOPRINT(temp), FIX32TOPRINT(power));
501
502 #ifdef HACKED_OVERTEMP
503 if (temp > 0x4a0000)
504 wf_smu_failure_state |= FAILURE_OVERTEMP;
505 #else
506 if (temp > st->pid.param.tmax)
507 wf_smu_failure_state |= FAILURE_OVERTEMP;
508 #endif
509 new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
510
511 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
512
513 systarget = wf_smu_sys_fans ? wf_smu_sys_fans->pid.target : 0;
514 systarget = ((((s64)systarget) * (s64)st->scale) >> 12)
515 + st->offset;
516 new_setpoint = max(new_setpoint, systarget);
517 new_setpoint = max(new_setpoint, st->pid.param.min);
518 new_setpoint = min(new_setpoint, st->pid.param.max);
519
520 DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)new_setpoint);
521
522 if (st->cpu_setpoint == new_setpoint)
523 return;
524 st->cpu_setpoint = new_setpoint;
525 readjust:
526 if (fan_cpu_main && wf_smu_failure_state == 0) {
527 rc = wf_control_set(fan_cpu_main, st->cpu_setpoint);
528 if (rc) {
529 printk(KERN_WARNING "windfarm: CPU main fan"
530 " error %d\n", rc);
531 wf_smu_failure_state |= FAILURE_FAN;
532 }
533 }
534 }
535
536 /*
537 * ****** Setup / Init / Misc ... ******
538 *
539 */
540
wf_smu_tick(void)541 static void wf_smu_tick(void)
542 {
543 unsigned int last_failure = wf_smu_failure_state;
544 unsigned int new_failure;
545
546 if (!wf_smu_started) {
547 DBG("wf: creating control loops !\n");
548 wf_smu_create_sys_fans();
549 wf_smu_create_cpu_fans();
550 wf_smu_started = true;
551 }
552
553 /* Skipping ticks */
554 if (wf_smu_skipping && --wf_smu_skipping)
555 return;
556
557 wf_smu_failure_state = 0;
558 if (wf_smu_sys_fans)
559 wf_smu_sys_fans_tick(wf_smu_sys_fans);
560 if (wf_smu_cpu_fans)
561 wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
562
563 wf_smu_readjust = 0;
564 new_failure = wf_smu_failure_state & ~last_failure;
565
566 /* If entering failure mode, clamp cpufreq and ramp all
567 * fans to full speed.
568 */
569 if (wf_smu_failure_state && !last_failure) {
570 if (cpufreq_clamp)
571 wf_control_set_max(cpufreq_clamp);
572 if (fan_system)
573 wf_control_set_max(fan_system);
574 if (fan_cpu_main)
575 wf_control_set_max(fan_cpu_main);
576 if (fan_hd)
577 wf_control_set_max(fan_hd);
578 }
579
580 /* If leaving failure mode, unclamp cpufreq and readjust
581 * all fans on next iteration
582 */
583 if (!wf_smu_failure_state && last_failure) {
584 if (cpufreq_clamp)
585 wf_control_set_min(cpufreq_clamp);
586 wf_smu_readjust = 1;
587 }
588
589 /* Overtemp condition detected, notify and start skipping a couple
590 * ticks to let the temperature go down
591 */
592 if (new_failure & FAILURE_OVERTEMP) {
593 wf_set_overtemp();
594 wf_smu_skipping = 2;
595 wf_smu_overtemp = true;
596 }
597
598 /* We only clear the overtemp condition if overtemp is cleared
599 * _and_ no other failure is present. Since a sensor error will
600 * clear the overtemp condition (can't measure temperature) at
601 * the control loop levels, but we don't want to keep it clear
602 * here in this case
603 */
604 if (!wf_smu_failure_state && wf_smu_overtemp) {
605 wf_clear_overtemp();
606 wf_smu_overtemp = false;
607 }
608 }
609
wf_smu_new_control(struct wf_control * ct)610 static void wf_smu_new_control(struct wf_control *ct)
611 {
612 if (wf_smu_all_controls_ok)
613 return;
614
615 if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-fan")) {
616 if (wf_get_control(ct) == 0)
617 fan_cpu_main = ct;
618 }
619
620 if (fan_system == NULL && !strcmp(ct->name, "system-fan")) {
621 if (wf_get_control(ct) == 0)
622 fan_system = ct;
623 }
624
625 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
626 if (wf_get_control(ct) == 0)
627 cpufreq_clamp = ct;
628 }
629
630 /* Darwin property list says the HD fan is only for model ID
631 * 0, 1, 2 and 3
632 */
633
634 if (wf_smu_mach_model > 3) {
635 if (fan_system && fan_cpu_main && cpufreq_clamp)
636 wf_smu_all_controls_ok = 1;
637 return;
638 }
639
640 if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
641 if (wf_get_control(ct) == 0)
642 fan_hd = ct;
643 }
644
645 if (fan_system && fan_hd && fan_cpu_main && cpufreq_clamp)
646 wf_smu_all_controls_ok = 1;
647 }
648
wf_smu_new_sensor(struct wf_sensor * sr)649 static void wf_smu_new_sensor(struct wf_sensor *sr)
650 {
651 if (wf_smu_all_sensors_ok)
652 return;
653
654 if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
655 if (wf_get_sensor(sr) == 0)
656 sensor_cpu_power = sr;
657 }
658
659 if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
660 if (wf_get_sensor(sr) == 0)
661 sensor_cpu_temp = sr;
662 }
663
664 if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
665 if (wf_get_sensor(sr) == 0)
666 sensor_hd_temp = sr;
667 }
668
669 if (sensor_cpu_power && sensor_cpu_temp && sensor_hd_temp)
670 wf_smu_all_sensors_ok = 1;
671 }
672
673
wf_smu_notify(struct notifier_block * self,unsigned long event,void * data)674 static int wf_smu_notify(struct notifier_block *self,
675 unsigned long event, void *data)
676 {
677 switch(event) {
678 case WF_EVENT_NEW_CONTROL:
679 DBG("wf: new control %s detected\n",
680 ((struct wf_control *)data)->name);
681 wf_smu_new_control(data);
682 wf_smu_readjust = 1;
683 break;
684 case WF_EVENT_NEW_SENSOR:
685 DBG("wf: new sensor %s detected\n",
686 ((struct wf_sensor *)data)->name);
687 wf_smu_new_sensor(data);
688 break;
689 case WF_EVENT_TICK:
690 if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
691 wf_smu_tick();
692 }
693
694 return 0;
695 }
696
697 static struct notifier_block wf_smu_events = {
698 .notifier_call = wf_smu_notify,
699 };
700
wf_init_pm(void)701 static int wf_init_pm(void)
702 {
703 const struct smu_sdbp_header *hdr;
704
705 hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
706 if (hdr) {
707 struct smu_sdbp_sensortree *st =
708 (struct smu_sdbp_sensortree *)&hdr[1];
709 wf_smu_mach_model = st->model_id;
710 }
711
712 printk(KERN_INFO "windfarm: Initializing for iMacG5 model ID %d\n",
713 wf_smu_mach_model);
714
715 return 0;
716 }
717
wf_smu_probe(struct platform_device * ddev)718 static int wf_smu_probe(struct platform_device *ddev)
719 {
720 wf_register_client(&wf_smu_events);
721
722 return 0;
723 }
724
wf_smu_remove(struct platform_device * ddev)725 static void wf_smu_remove(struct platform_device *ddev)
726 {
727 wf_unregister_client(&wf_smu_events);
728
729 /* XXX We don't have yet a guarantee that our callback isn't
730 * in progress when returning from wf_unregister_client, so
731 * we add an arbitrary delay. I'll have to fix that in the core
732 */
733 msleep(1000);
734
735 /* Release all sensors */
736 /* One more crappy race: I don't think we have any guarantee here
737 * that the attribute callback won't race with the sensor beeing
738 * disposed of, and I'm not 100% certain what best way to deal
739 * with that except by adding locks all over... I'll do that
740 * eventually but heh, who ever rmmod this module anyway ?
741 */
742 if (sensor_cpu_power)
743 wf_put_sensor(sensor_cpu_power);
744 if (sensor_cpu_temp)
745 wf_put_sensor(sensor_cpu_temp);
746 if (sensor_hd_temp)
747 wf_put_sensor(sensor_hd_temp);
748
749 /* Release all controls */
750 if (fan_cpu_main)
751 wf_put_control(fan_cpu_main);
752 if (fan_hd)
753 wf_put_control(fan_hd);
754 if (fan_system)
755 wf_put_control(fan_system);
756 if (cpufreq_clamp)
757 wf_put_control(cpufreq_clamp);
758
759 /* Destroy control loops state structures */
760 kfree(wf_smu_sys_fans);
761 kfree(wf_smu_cpu_fans);
762 }
763
764 static struct platform_driver wf_smu_driver = {
765 .probe = wf_smu_probe,
766 .remove = wf_smu_remove,
767 .driver = {
768 .name = "windfarm",
769 },
770 };
771
772
wf_smu_init(void)773 static int __init wf_smu_init(void)
774 {
775 int rc = -ENODEV;
776
777 if (of_machine_is_compatible("PowerMac8,1") ||
778 of_machine_is_compatible("PowerMac8,2"))
779 rc = wf_init_pm();
780
781 if (rc == 0) {
782 #ifdef MODULE
783 request_module("windfarm_smu_controls");
784 request_module("windfarm_smu_sensors");
785 request_module("windfarm_lm75_sensor");
786 request_module("windfarm_cpufreq_clamp");
787
788 #endif /* MODULE */
789 platform_driver_register(&wf_smu_driver);
790 }
791
792 return rc;
793 }
794
wf_smu_exit(void)795 static void __exit wf_smu_exit(void)
796 {
797
798 platform_driver_unregister(&wf_smu_driver);
799 }
800
801
802 module_init(wf_smu_init);
803 module_exit(wf_smu_exit);
804
805 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
806 MODULE_DESCRIPTION("Thermal control logic for iMac G5");
807 MODULE_LICENSE("GPL");
808 MODULE_ALIAS("platform:windfarm");
809