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