blob: a8ac66cd3b13b3b9e92f514e1ee5d70221dcd331 [file] [log] [blame]
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +11001/*
2 * Windfarm PowerMac thermal control. SMU based 1 CPU desktop control loops
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 * PowerMac9,1
17 * ===========
18 *
19 * Has 3 control loops: CPU fans is similar to PowerMac8,1 (though it doesn't
20 * try to play with other control loops fans). Drive bay is rather basic PID
21 * with one sensor and one fan. Slots area is a bit different as the Darwin
22 * driver is supposed to be capable of working in a special "AGP" mode which
23 * involves the presence of an AGP sensor and an AGP fan (possibly on the
24 * AGP card itself). I can't deal with that special mode as I don't have
25 * access to those additional sensor/fans for now (though ultimately, it would
26 * be possible to add sensor objects for them) so I'm only implementing the
27 * basic PCI slot control loop
28 */
29
30#include <linux/types.h>
31#include <linux/errno.h>
32#include <linux/kernel.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/spinlock.h>
37#include <linux/wait.h>
38#include <linux/kmod.h>
39#include <linux/device.h>
40#include <linux/platform_device.h>
41#include <asm/prom.h>
42#include <asm/machdep.h>
43#include <asm/io.h>
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110044#include <asm/sections.h>
45#include <asm/smu.h>
46
47#include "windfarm.h"
48#include "windfarm_pid.h"
49
50#define VERSION "0.4"
51
52#undef DEBUG
53
54#ifdef DEBUG
55#define DBG(args...) printk(args)
56#else
57#define DBG(args...) do { } while(0)
58#endif
59
60/* define this to force CPU overtemp to 74 degree, useful for testing
61 * the overtemp code
62 */
63#undef HACKED_OVERTEMP
64
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110065/* Controls & sensors */
66static struct wf_sensor *sensor_cpu_power;
67static struct wf_sensor *sensor_cpu_temp;
68static struct wf_sensor *sensor_hd_temp;
69static struct wf_sensor *sensor_slots_power;
70static struct wf_control *fan_cpu_main;
71static struct wf_control *fan_cpu_second;
72static struct wf_control *fan_cpu_third;
73static struct wf_control *fan_hd;
74static struct wf_control *fan_slots;
75static struct wf_control *cpufreq_clamp;
76
77/* Set to kick the control loop into life */
78static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, wf_smu_started;
Aaro Koskinen4bb29712013-06-30 22:00:42 +030079static bool wf_smu_overtemp;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110080
81/* Failure handling.. could be nicer */
82#define FAILURE_FAN 0x01
83#define FAILURE_SENSOR 0x02
84#define FAILURE_OVERTEMP 0x04
85
86static unsigned int wf_smu_failure_state;
87static int wf_smu_readjust, wf_smu_skipping;
88
89/*
90 * ****** CPU Fans Control Loop ******
91 *
92 */
93
94
95#define WF_SMU_CPU_FANS_INTERVAL 1
96#define WF_SMU_CPU_FANS_MAX_HISTORY 16
97
98/* State data used by the cpu fans control loop
99 */
100struct wf_smu_cpu_fans_state {
101 int ticks;
102 s32 cpu_setpoint;
103 struct wf_cpu_pid_state pid;
104};
105
106static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
107
108
109
110/*
111 * ****** Drive Fan Control Loop ******
112 *
113 */
114
115struct wf_smu_drive_fans_state {
116 int ticks;
117 s32 setpoint;
118 struct wf_pid_state pid;
119};
120
121static struct wf_smu_drive_fans_state *wf_smu_drive_fans;
122
123/*
124 * ****** Slots Fan Control Loop ******
125 *
126 */
127
128struct wf_smu_slots_fans_state {
129 int ticks;
130 s32 setpoint;
131 struct wf_pid_state pid;
132};
133
134static struct wf_smu_slots_fans_state *wf_smu_slots_fans;
135
136/*
137 * ***** Implementation *****
138 *
139 */
140
141
142static void wf_smu_create_cpu_fans(void)
143{
144 struct wf_cpu_pid_param pid_param;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000145 const struct smu_sdbp_header *hdr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100146 struct smu_sdbp_cpupiddata *piddata;
147 struct smu_sdbp_fvt *fvt;
148 s32 tmax, tdelta, maxpow, powadj;
149
150 /* First, locate the PID params in SMU SBD */
151 hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
152 if (hdr == 0) {
153 printk(KERN_WARNING "windfarm: CPU PID fan config not found "
154 "max fan speed\n");
155 goto fail;
156 }
157 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
158
159 /* Get the FVT params for operating point 0 (the only supported one
160 * for now) in order to get tmax
161 */
162 hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
163 if (hdr) {
164 fvt = (struct smu_sdbp_fvt *)&hdr[1];
165 tmax = ((s32)fvt->maxtemp) << 16;
166 } else
167 tmax = 0x5e0000; /* 94 degree default */
168
169 /* Alloc & initialize state */
170 wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
171 GFP_KERNEL);
172 if (wf_smu_cpu_fans == NULL)
173 goto fail;
174 wf_smu_cpu_fans->ticks = 1;
175
176 /* Fill PID params */
177 pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
178 pid_param.history_len = piddata->history_len;
179 if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
180 printk(KERN_WARNING "windfarm: History size overflow on "
181 "CPU control loop (%d)\n", piddata->history_len);
182 pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
183 }
184 pid_param.gd = piddata->gd;
185 pid_param.gp = piddata->gp;
186 pid_param.gr = piddata->gr / pid_param.history_len;
187
188 tdelta = ((s32)piddata->target_temp_delta) << 16;
189 maxpow = ((s32)piddata->max_power) << 16;
190 powadj = ((s32)piddata->power_adj) << 16;
191
192 pid_param.tmax = tmax;
193 pid_param.ttarget = tmax - tdelta;
194 pid_param.pmaxadj = maxpow - powadj;
195
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000196 pid_param.min = wf_control_get_min(fan_cpu_main);
197 pid_param.max = wf_control_get_max(fan_cpu_main);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100198
199 wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
200
201 DBG("wf: CPU Fan control initialized.\n");
202 DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
203 FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
204 pid_param.min, pid_param.max);
205
206 return;
207
208 fail:
209 printk(KERN_WARNING "windfarm: CPU fan config not found\n"
210 "for this machine model, max fan speed\n");
211
212 if (cpufreq_clamp)
213 wf_control_set_max(cpufreq_clamp);
214 if (fan_cpu_main)
215 wf_control_set_max(fan_cpu_main);
216}
217
218static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
219{
220 s32 new_setpoint, temp, power;
221 int rc;
222
223 if (--st->ticks != 0) {
224 if (wf_smu_readjust)
225 goto readjust;
226 return;
227 }
228 st->ticks = WF_SMU_CPU_FANS_INTERVAL;
229
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000230 rc = wf_sensor_get(sensor_cpu_temp, &temp);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100231 if (rc) {
232 printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
233 rc);
234 wf_smu_failure_state |= FAILURE_SENSOR;
235 return;
236 }
237
Stephen Rothwellca7d5932012-05-02 18:34:52 +0000238 rc = wf_sensor_get(sensor_cpu_power, &power);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100239 if (rc) {
240 printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
241 rc);
242 wf_smu_failure_state |= FAILURE_SENSOR;
243 return;
244 }
245
246 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
247 FIX32TOPRINT(temp), FIX32TOPRINT(power));
248
249#ifdef HACKED_OVERTEMP
250 if (temp > 0x4a0000)
251 wf_smu_failure_state |= FAILURE_OVERTEMP;
252#else
253 if (temp > st->pid.param.tmax)
254 wf_smu_failure_state |= FAILURE_OVERTEMP;
255#endif
256 new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
257
258 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
259
260 if (st->cpu_setpoint == new_setpoint)
261 return;
262 st->cpu_setpoint = new_setpoint;
263 readjust:
264 if (fan_cpu_main && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000265 rc = wf_control_set(fan_cpu_main, st->cpu_setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100266 if (rc) {
267 printk(KERN_WARNING "windfarm: CPU main fan"
268 " error %d\n", rc);
269 wf_smu_failure_state |= FAILURE_FAN;
270 }
271 }
272 if (fan_cpu_second && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000273 rc = wf_control_set(fan_cpu_second, st->cpu_setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100274 if (rc) {
275 printk(KERN_WARNING "windfarm: CPU second fan"
276 " error %d\n", rc);
277 wf_smu_failure_state |= FAILURE_FAN;
278 }
279 }
280 if (fan_cpu_third && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000281 rc = wf_control_set(fan_cpu_third, st->cpu_setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100282 if (rc) {
283 printk(KERN_WARNING "windfarm: CPU third fan"
284 " error %d\n", rc);
285 wf_smu_failure_state |= FAILURE_FAN;
286 }
287 }
288}
289
290static void wf_smu_create_drive_fans(void)
291{
292 struct wf_pid_param param = {
293 .interval = 5,
294 .history_len = 2,
295 .gd = 0x01e00000,
296 .gp = 0x00500000,
297 .gr = 0x00000000,
298 .itarget = 0x00200000,
299 };
300
301 /* Alloc & initialize state */
302 wf_smu_drive_fans = kmalloc(sizeof(struct wf_smu_drive_fans_state),
303 GFP_KERNEL);
304 if (wf_smu_drive_fans == NULL) {
305 printk(KERN_WARNING "windfarm: Memory allocation error"
306 " max fan speed\n");
307 goto fail;
308 }
309 wf_smu_drive_fans->ticks = 1;
310
311 /* Fill PID params */
312 param.additive = (fan_hd->type == WF_CONTROL_RPM_FAN);
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000313 param.min = wf_control_get_min(fan_hd);
314 param.max = wf_control_get_max(fan_hd);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100315 wf_pid_init(&wf_smu_drive_fans->pid, &param);
316
317 DBG("wf: Drive Fan control initialized.\n");
318 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
319 FIX32TOPRINT(param.itarget), param.min, param.max);
320 return;
321
322 fail:
323 if (fan_hd)
324 wf_control_set_max(fan_hd);
325}
326
327static void wf_smu_drive_fans_tick(struct wf_smu_drive_fans_state *st)
328{
329 s32 new_setpoint, temp;
330 int rc;
331
332 if (--st->ticks != 0) {
333 if (wf_smu_readjust)
334 goto readjust;
335 return;
336 }
337 st->ticks = st->pid.param.interval;
338
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000339 rc = wf_sensor_get(sensor_hd_temp, &temp);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100340 if (rc) {
341 printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
342 rc);
343 wf_smu_failure_state |= FAILURE_SENSOR;
344 return;
345 }
346
347 DBG("wf_smu: Drive Fans tick ! HD temp: %d.%03d\n",
348 FIX32TOPRINT(temp));
349
350 if (temp > (st->pid.param.itarget + 0x50000))
351 wf_smu_failure_state |= FAILURE_OVERTEMP;
352
353 new_setpoint = wf_pid_run(&st->pid, temp);
354
355 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
356
357 if (st->setpoint == new_setpoint)
358 return;
359 st->setpoint = new_setpoint;
360 readjust:
361 if (fan_hd && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000362 rc = wf_control_set(fan_hd, st->setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100363 if (rc) {
364 printk(KERN_WARNING "windfarm: HD fan error %d\n",
365 rc);
366 wf_smu_failure_state |= FAILURE_FAN;
367 }
368 }
369}
370
371static void wf_smu_create_slots_fans(void)
372{
373 struct wf_pid_param param = {
374 .interval = 1,
375 .history_len = 8,
376 .gd = 0x00000000,
377 .gp = 0x00000000,
378 .gr = 0x00020000,
379 .itarget = 0x00000000
380 };
381
382 /* Alloc & initialize state */
383 wf_smu_slots_fans = kmalloc(sizeof(struct wf_smu_slots_fans_state),
384 GFP_KERNEL);
385 if (wf_smu_slots_fans == NULL) {
386 printk(KERN_WARNING "windfarm: Memory allocation error"
387 " max fan speed\n");
388 goto fail;
389 }
390 wf_smu_slots_fans->ticks = 1;
391
392 /* Fill PID params */
393 param.additive = (fan_slots->type == WF_CONTROL_RPM_FAN);
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000394 param.min = wf_control_get_min(fan_slots);
395 param.max = wf_control_get_max(fan_slots);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100396 wf_pid_init(&wf_smu_slots_fans->pid, &param);
397
398 DBG("wf: Slots Fan control initialized.\n");
399 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
400 FIX32TOPRINT(param.itarget), param.min, param.max);
401 return;
402
403 fail:
404 if (fan_slots)
405 wf_control_set_max(fan_slots);
406}
407
408static void wf_smu_slots_fans_tick(struct wf_smu_slots_fans_state *st)
409{
410 s32 new_setpoint, power;
411 int rc;
412
413 if (--st->ticks != 0) {
414 if (wf_smu_readjust)
415 goto readjust;
416 return;
417 }
418 st->ticks = st->pid.param.interval;
419
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000420 rc = wf_sensor_get(sensor_slots_power, &power);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100421 if (rc) {
422 printk(KERN_WARNING "windfarm: Slots power sensor error %d\n",
423 rc);
424 wf_smu_failure_state |= FAILURE_SENSOR;
425 return;
426 }
427
428 DBG("wf_smu: Slots Fans tick ! Slots power: %d.%03d\n",
429 FIX32TOPRINT(power));
430
431#if 0 /* Check what makes a good overtemp condition */
432 if (power > (st->pid.param.itarget + 0x50000))
433 wf_smu_failure_state |= FAILURE_OVERTEMP;
434#endif
435
436 new_setpoint = wf_pid_run(&st->pid, power);
437
438 DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
439
440 if (st->setpoint == new_setpoint)
441 return;
442 st->setpoint = new_setpoint;
443 readjust:
444 if (fan_slots && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000445 rc = wf_control_set(fan_slots, st->setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100446 if (rc) {
447 printk(KERN_WARNING "windfarm: Slots fan error %d\n",
448 rc);
449 wf_smu_failure_state |= FAILURE_FAN;
450 }
451 }
452}
453
454
455/*
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100456 * ****** Setup / Init / Misc ... ******
457 *
458 */
459
460static void wf_smu_tick(void)
461{
462 unsigned int last_failure = wf_smu_failure_state;
463 unsigned int new_failure;
464
465 if (!wf_smu_started) {
466 DBG("wf: creating control loops !\n");
467 wf_smu_create_drive_fans();
468 wf_smu_create_slots_fans();
469 wf_smu_create_cpu_fans();
470 wf_smu_started = 1;
471 }
472
473 /* Skipping ticks */
474 if (wf_smu_skipping && --wf_smu_skipping)
475 return;
476
477 wf_smu_failure_state = 0;
478 if (wf_smu_drive_fans)
479 wf_smu_drive_fans_tick(wf_smu_drive_fans);
480 if (wf_smu_slots_fans)
481 wf_smu_slots_fans_tick(wf_smu_slots_fans);
482 if (wf_smu_cpu_fans)
483 wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
484
485 wf_smu_readjust = 0;
486 new_failure = wf_smu_failure_state & ~last_failure;
487
488 /* If entering failure mode, clamp cpufreq and ramp all
489 * fans to full speed.
490 */
491 if (wf_smu_failure_state && !last_failure) {
492 if (cpufreq_clamp)
493 wf_control_set_max(cpufreq_clamp);
494 if (fan_cpu_main)
495 wf_control_set_max(fan_cpu_main);
496 if (fan_cpu_second)
497 wf_control_set_max(fan_cpu_second);
498 if (fan_cpu_third)
499 wf_control_set_max(fan_cpu_third);
500 if (fan_hd)
501 wf_control_set_max(fan_hd);
502 if (fan_slots)
503 wf_control_set_max(fan_slots);
504 }
505
506 /* If leaving failure mode, unclamp cpufreq and readjust
507 * all fans on next iteration
508 */
509 if (!wf_smu_failure_state && last_failure) {
510 if (cpufreq_clamp)
511 wf_control_set_min(cpufreq_clamp);
512 wf_smu_readjust = 1;
513 }
514
515 /* Overtemp condition detected, notify and start skipping a couple
516 * ticks to let the temperature go down
517 */
518 if (new_failure & FAILURE_OVERTEMP) {
519 wf_set_overtemp();
520 wf_smu_skipping = 2;
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300521 wf_smu_overtemp = true;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100522 }
523
524 /* We only clear the overtemp condition if overtemp is cleared
525 * _and_ no other failure is present. Since a sensor error will
526 * clear the overtemp condition (can't measure temperature) at
527 * the control loop levels, but we don't want to keep it clear
528 * here in this case
529 */
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300530 if (!wf_smu_failure_state && wf_smu_overtemp) {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100531 wf_clear_overtemp();
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300532 wf_smu_overtemp = false;
533 }
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100534}
535
536
537static void wf_smu_new_control(struct wf_control *ct)
538{
539 if (wf_smu_all_controls_ok)
540 return;
541
542 if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-rear-fan-0")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100543 if (wf_get_control(ct) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100544 fan_cpu_main = ct;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100545 }
546
547 if (fan_cpu_second == NULL && !strcmp(ct->name, "cpu-rear-fan-1")) {
548 if (wf_get_control(ct) == 0)
549 fan_cpu_second = ct;
550 }
551
552 if (fan_cpu_third == NULL && !strcmp(ct->name, "cpu-front-fan-0")) {
553 if (wf_get_control(ct) == 0)
554 fan_cpu_third = ct;
555 }
556
557 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
558 if (wf_get_control(ct) == 0)
559 cpufreq_clamp = ct;
560 }
561
562 if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100563 if (wf_get_control(ct) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100564 fan_hd = ct;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100565 }
566
567 if (fan_slots == NULL && !strcmp(ct->name, "slots-fan")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100568 if (wf_get_control(ct) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100569 fan_slots = ct;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100570 }
571
572 if (fan_cpu_main && (fan_cpu_second || fan_cpu_third) && fan_hd &&
573 fan_slots && cpufreq_clamp)
574 wf_smu_all_controls_ok = 1;
575}
576
577static void wf_smu_new_sensor(struct wf_sensor *sr)
578{
579 if (wf_smu_all_sensors_ok)
580 return;
581
582 if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100583 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100584 sensor_cpu_power = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100585 }
586
587 if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100588 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100589 sensor_cpu_temp = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100590 }
591
592 if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100593 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100594 sensor_hd_temp = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100595 }
596
597 if (sensor_slots_power == NULL && !strcmp(sr->name, "slots-power")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100598 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100599 sensor_slots_power = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100600 }
601
602 if (sensor_cpu_power && sensor_cpu_temp &&
603 sensor_hd_temp && sensor_slots_power)
604 wf_smu_all_sensors_ok = 1;
605}
606
607
608static int wf_smu_notify(struct notifier_block *self,
609 unsigned long event, void *data)
610{
611 switch(event) {
612 case WF_EVENT_NEW_CONTROL:
613 DBG("wf: new control %s detected\n",
614 ((struct wf_control *)data)->name);
615 wf_smu_new_control(data);
616 wf_smu_readjust = 1;
617 break;
618 case WF_EVENT_NEW_SENSOR:
619 DBG("wf: new sensor %s detected\n",
620 ((struct wf_sensor *)data)->name);
621 wf_smu_new_sensor(data);
622 break;
623 case WF_EVENT_TICK:
624 if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
625 wf_smu_tick();
626 }
627
628 return 0;
629}
630
631static struct notifier_block wf_smu_events = {
632 .notifier_call = wf_smu_notify,
633};
634
635static int wf_init_pm(void)
636{
637 printk(KERN_INFO "windfarm: Initializing for Desktop G5 model\n");
638
639 return 0;
640}
641
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000642static int wf_smu_probe(struct platform_device *ddev)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100643{
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100644 wf_register_client(&wf_smu_events);
645
646 return 0;
647}
648
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800649static int wf_smu_remove(struct platform_device *ddev)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100650{
651 wf_unregister_client(&wf_smu_events);
652
653 /* XXX We don't have yet a guarantee that our callback isn't
654 * in progress when returning from wf_unregister_client, so
655 * we add an arbitrary delay. I'll have to fix that in the core
656 */
657 msleep(1000);
658
659 /* Release all sensors */
660 /* One more crappy race: I don't think we have any guarantee here
661 * that the attribute callback won't race with the sensor beeing
662 * disposed of, and I'm not 100% certain what best way to deal
663 * with that except by adding locks all over... I'll do that
664 * eventually but heh, who ever rmmod this module anyway ?
665 */
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100666 if (sensor_cpu_power)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100667 wf_put_sensor(sensor_cpu_power);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100668 if (sensor_cpu_temp)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100669 wf_put_sensor(sensor_cpu_temp);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100670 if (sensor_hd_temp)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100671 wf_put_sensor(sensor_hd_temp);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100672 if (sensor_slots_power)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100673 wf_put_sensor(sensor_slots_power);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100674
675 /* Release all controls */
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100676 if (fan_cpu_main)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100677 wf_put_control(fan_cpu_main);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100678 if (fan_cpu_second)
679 wf_put_control(fan_cpu_second);
680 if (fan_cpu_third)
681 wf_put_control(fan_cpu_third);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100682 if (fan_hd)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100683 wf_put_control(fan_hd);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100684 if (fan_slots)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100685 wf_put_control(fan_slots);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100686 if (cpufreq_clamp)
687 wf_put_control(cpufreq_clamp);
688
689 /* Destroy control loops state structures */
Julia Lawallab30f782010-03-28 23:39:22 +0000690 kfree(wf_smu_slots_fans);
691 kfree(wf_smu_drive_fans);
692 kfree(wf_smu_cpu_fans);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100693
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100694 return 0;
695}
696
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000697static struct platform_driver wf_smu_driver = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100698 .probe = wf_smu_probe,
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800699 .remove = wf_smu_remove,
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000700 .driver = {
701 .name = "windfarm",
Kay Sievers23386fe2008-04-18 13:33:53 -0700702 .owner = THIS_MODULE,
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000703 },
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100704};
705
706
707static int __init wf_smu_init(void)
708{
709 int rc = -ENODEV;
710
Grant Likely71a157e2010-02-01 21:34:14 -0700711 if (of_machine_is_compatible("PowerMac9,1"))
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100712 rc = wf_init_pm();
713
714 if (rc == 0) {
715#ifdef MODULE
716 request_module("windfarm_smu_controls");
717 request_module("windfarm_smu_sensors");
718 request_module("windfarm_lm75_sensor");
Benjamin Herrenschmidtd31e8172006-11-16 14:00:57 +1100719 request_module("windfarm_cpufreq_clamp");
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100720
721#endif /* MODULE */
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000722 platform_driver_register(&wf_smu_driver);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100723 }
724
725 return rc;
726}
727
728static void __exit wf_smu_exit(void)
729{
730
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000731 platform_driver_unregister(&wf_smu_driver);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100732}
733
734
735module_init(wf_smu_init);
736module_exit(wf_smu_exit);
737
738MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
739MODULE_DESCRIPTION("Thermal control logic for PowerMac9,1");
740MODULE_LICENSE("GPL");
741
Kay Sievers23386fe2008-04-18 13:33:53 -0700742MODULE_ALIAS("platform:windfarm");