blob: 93faf298a3c56e39a5703eb014ea1512af06fc28 [file] [log] [blame]
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +11001/*
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
Adrian Bunk943ffb52006-01-10 00:10:13 +010029 * - the value of the target of the CPU Fan control loop is retrieved,
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110030 * 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>
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100110#include <asm/sections.h>
111#include <asm/smu.h>
112
113#include "windfarm.h"
114#include "windfarm_pid.h"
115
116#define VERSION "0.4"
117
118#undef DEBUG
119
120#ifdef DEBUG
121#define DBG(args...) printk(args)
122#else
123#define DBG(args...) do { } while(0)
124#endif
125
126/* define this to force CPU overtemp to 74 degree, useful for testing
127 * the overtemp code
128 */
129#undef HACKED_OVERTEMP
130
131static int wf_smu_mach_model; /* machine model id */
132
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100133/* Controls & sensors */
134static struct wf_sensor *sensor_cpu_power;
135static struct wf_sensor *sensor_cpu_temp;
136static struct wf_sensor *sensor_hd_temp;
137static struct wf_control *fan_cpu_main;
138static struct wf_control *fan_hd;
139static struct wf_control *fan_system;
140static struct wf_control *cpufreq_clamp;
141
142/* Set to kick the control loop into life */
143static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, 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
150static unsigned int wf_smu_failure_state;
151static int wf_smu_readjust, wf_smu_skipping;
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300152static bool wf_smu_overtemp;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100153
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 */
163struct 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 */
179struct 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/*
Thomas Weber88393162010-03-16 11:47:56 +0100191 * Configs for SMU System Fan control loop
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100192 */
193static 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 {
Michal Ostrowski326743e2005-12-08 16:56:17 +1100208 .model_id = 3,
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100209 .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 {
Michal Ostrowski326743e2005-12-08 16:56:17 +1100220 .model_id = 5,
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100221 .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
233static 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 */
248struct 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
256static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
257
258
259
260/*
261 * ***** Implementation *****
262 *
263 */
264
265static 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(sizeof(struct wf_smu_sys_fans_state),
287 GFP_KERNEL);
288 if (wf_smu_sys_fans == NULL) {
289 printk(KERN_WARNING "windfarm: Memory allocation error"
290 " max fan speed\n");
291 goto fail;
292 }
293 wf_smu_sys_fans->ticks = 1;
294 wf_smu_sys_fans->scale0 = param->scale0;
295 wf_smu_sys_fans->offset0 = param->offset0;
296 wf_smu_sys_fans->scale1 = param->scale1;
297 wf_smu_sys_fans->offset1 = param->offset1;
298
299 /* Fill PID params */
300 pid_param.gd = param->gd;
301 pid_param.gp = param->gp;
302 pid_param.gr = param->gr;
303 pid_param.interval = WF_SMU_SYS_FANS_INTERVAL;
304 pid_param.history_len = WF_SMU_SYS_FANS_HISTORY_SIZE;
305 pid_param.itarget = param->itarget;
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000306 pid_param.min = wf_control_get_min(fan_system);
307 pid_param.max = wf_control_get_max(fan_system);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100308 if (fan_hd) {
309 pid_param.min =
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000310 max(pid_param.min, wf_control_get_min(fan_hd));
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100311 pid_param.max =
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000312 min(pid_param.max, wf_control_get_max(fan_hd));
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100313 }
314 wf_pid_init(&wf_smu_sys_fans->pid, &pid_param);
315
316 DBG("wf: System Fan control initialized.\n");
317 DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
318 FIX32TOPRINT(pid_param.itarget), pid_param.min, pid_param.max);
319 return;
320
321 fail:
322
323 if (fan_system)
324 wf_control_set_max(fan_system);
325 if (fan_hd)
326 wf_control_set_max(fan_hd);
327}
328
329static void wf_smu_sys_fans_tick(struct wf_smu_sys_fans_state *st)
330{
331 s32 new_setpoint, temp, scaled, cputarget;
332 int rc;
333
334 if (--st->ticks != 0) {
335 if (wf_smu_readjust)
336 goto readjust;
337 return;
338 }
339 st->ticks = WF_SMU_SYS_FANS_INTERVAL;
340
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000341 rc = wf_sensor_get(sensor_hd_temp, &temp);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100342 if (rc) {
343 printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
344 rc);
345 wf_smu_failure_state |= FAILURE_SENSOR;
346 return;
347 }
348
349 DBG("wf_smu: System Fans tick ! HD temp: %d.%03d\n",
350 FIX32TOPRINT(temp));
351
352 if (temp > (st->pid.param.itarget + 0x50000))
353 wf_smu_failure_state |= FAILURE_OVERTEMP;
354
355 new_setpoint = wf_pid_run(&st->pid, temp);
356
357 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
358
359 scaled = ((((s64)new_setpoint) * (s64)st->scale0) >> 12) + st->offset0;
360
361 DBG("wf_smu: scaled setpoint: %d RPM\n", (int)scaled);
362
363 cputarget = wf_smu_cpu_fans ? wf_smu_cpu_fans->pid.target : 0;
364 cputarget = ((((s64)cputarget) * (s64)st->scale1) >> 12) + st->offset1;
365 scaled = max(scaled, cputarget);
366 scaled = max(scaled, st->pid.param.min);
367 scaled = min(scaled, st->pid.param.max);
368
369 DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)scaled);
370
371 if (st->sys_setpoint == scaled && new_setpoint == st->hd_setpoint)
372 return;
373 st->sys_setpoint = scaled;
374 st->hd_setpoint = new_setpoint;
375 readjust:
376 if (fan_system && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000377 rc = wf_control_set(fan_system, st->sys_setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100378 if (rc) {
379 printk(KERN_WARNING "windfarm: Sys fan error %d\n",
380 rc);
381 wf_smu_failure_state |= FAILURE_FAN;
382 }
383 }
384 if (fan_hd && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000385 rc = wf_control_set(fan_hd, st->hd_setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100386 if (rc) {
387 printk(KERN_WARNING "windfarm: HD fan error %d\n",
388 rc);
389 wf_smu_failure_state |= FAILURE_FAN;
390 }
391 }
392}
393
394static void wf_smu_create_cpu_fans(void)
395{
396 struct wf_cpu_pid_param pid_param;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000397 const struct smu_sdbp_header *hdr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100398 struct smu_sdbp_cpupiddata *piddata;
399 struct smu_sdbp_fvt *fvt;
400 s32 tmax, tdelta, maxpow, powadj;
401
402 /* First, locate the PID params in SMU SBD */
403 hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
404 if (hdr == 0) {
405 printk(KERN_WARNING "windfarm: CPU PID fan config not found "
406 "max fan speed\n");
407 goto fail;
408 }
409 piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
410
411 /* Get the FVT params for operating point 0 (the only supported one
412 * for now) in order to get tmax
413 */
414 hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
415 if (hdr) {
416 fvt = (struct smu_sdbp_fvt *)&hdr[1];
417 tmax = ((s32)fvt->maxtemp) << 16;
418 } else
419 tmax = 0x5e0000; /* 94 degree default */
420
421 /* Alloc & initialize state */
422 wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
423 GFP_KERNEL);
424 if (wf_smu_cpu_fans == NULL)
425 goto fail;
426 wf_smu_cpu_fans->ticks = 1;
427
428 wf_smu_cpu_fans->scale = WF_SMU_CPU_FANS_SIBLING_SCALE;
429 wf_smu_cpu_fans->offset = WF_SMU_CPU_FANS_SIBLING_OFFSET;
430
431 /* Fill PID params */
432 pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
433 pid_param.history_len = piddata->history_len;
434 if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
435 printk(KERN_WARNING "windfarm: History size overflow on "
436 "CPU control loop (%d)\n", piddata->history_len);
437 pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
438 }
439 pid_param.gd = piddata->gd;
440 pid_param.gp = piddata->gp;
441 pid_param.gr = piddata->gr / pid_param.history_len;
442
443 tdelta = ((s32)piddata->target_temp_delta) << 16;
444 maxpow = ((s32)piddata->max_power) << 16;
445 powadj = ((s32)piddata->power_adj) << 16;
446
447 pid_param.tmax = tmax;
448 pid_param.ttarget = tmax - tdelta;
449 pid_param.pmaxadj = maxpow - powadj;
450
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000451 pid_param.min = wf_control_get_min(fan_cpu_main);
452 pid_param.max = wf_control_get_max(fan_cpu_main);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100453
454 wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
455
456 DBG("wf: CPU Fan control initialized.\n");
457 DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
458 FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
459 pid_param.min, pid_param.max);
460
461 return;
462
463 fail:
464 printk(KERN_WARNING "windfarm: CPU fan config not found\n"
465 "for this machine model, max fan speed\n");
466
467 if (cpufreq_clamp)
468 wf_control_set_max(cpufreq_clamp);
469 if (fan_cpu_main)
470 wf_control_set_max(fan_cpu_main);
471}
472
473static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
474{
475 s32 new_setpoint, temp, power, systarget;
476 int rc;
477
478 if (--st->ticks != 0) {
479 if (wf_smu_readjust)
480 goto readjust;
481 return;
482 }
483 st->ticks = WF_SMU_CPU_FANS_INTERVAL;
484
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000485 rc = wf_sensor_get(sensor_cpu_temp, &temp);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100486 if (rc) {
487 printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
488 rc);
489 wf_smu_failure_state |= FAILURE_SENSOR;
490 return;
491 }
492
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000493 rc = wf_sensor_get(sensor_cpu_power, &power);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100494 if (rc) {
495 printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
496 rc);
497 wf_smu_failure_state |= FAILURE_SENSOR;
498 return;
499 }
500
501 DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
502 FIX32TOPRINT(temp), FIX32TOPRINT(power));
503
504#ifdef HACKED_OVERTEMP
505 if (temp > 0x4a0000)
506 wf_smu_failure_state |= FAILURE_OVERTEMP;
507#else
508 if (temp > st->pid.param.tmax)
509 wf_smu_failure_state |= FAILURE_OVERTEMP;
510#endif
511 new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
512
513 DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
514
515 systarget = wf_smu_sys_fans ? wf_smu_sys_fans->pid.target : 0;
516 systarget = ((((s64)systarget) * (s64)st->scale) >> 12)
517 + st->offset;
518 new_setpoint = max(new_setpoint, systarget);
519 new_setpoint = max(new_setpoint, st->pid.param.min);
520 new_setpoint = min(new_setpoint, st->pid.param.max);
521
522 DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)new_setpoint);
523
524 if (st->cpu_setpoint == new_setpoint)
525 return;
526 st->cpu_setpoint = new_setpoint;
527 readjust:
528 if (fan_cpu_main && wf_smu_failure_state == 0) {
Benjamin Herrenschmidt33e68202012-04-18 22:16:51 +0000529 rc = wf_control_set(fan_cpu_main, st->cpu_setpoint);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100530 if (rc) {
531 printk(KERN_WARNING "windfarm: CPU main fan"
532 " error %d\n", rc);
533 wf_smu_failure_state |= FAILURE_FAN;
534 }
535 }
536}
537
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100538/*
539 * ****** Setup / Init / Misc ... ******
540 *
541 */
542
543static void wf_smu_tick(void)
544{
545 unsigned int last_failure = wf_smu_failure_state;
546 unsigned int new_failure;
547
548 if (!wf_smu_started) {
549 DBG("wf: creating control loops !\n");
550 wf_smu_create_sys_fans();
551 wf_smu_create_cpu_fans();
552 wf_smu_started = 1;
553 }
554
555 /* Skipping ticks */
556 if (wf_smu_skipping && --wf_smu_skipping)
557 return;
558
559 wf_smu_failure_state = 0;
560 if (wf_smu_sys_fans)
561 wf_smu_sys_fans_tick(wf_smu_sys_fans);
562 if (wf_smu_cpu_fans)
563 wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
564
565 wf_smu_readjust = 0;
566 new_failure = wf_smu_failure_state & ~last_failure;
567
568 /* If entering failure mode, clamp cpufreq and ramp all
569 * fans to full speed.
570 */
571 if (wf_smu_failure_state && !last_failure) {
572 if (cpufreq_clamp)
573 wf_control_set_max(cpufreq_clamp);
574 if (fan_system)
575 wf_control_set_max(fan_system);
576 if (fan_cpu_main)
577 wf_control_set_max(fan_cpu_main);
578 if (fan_hd)
579 wf_control_set_max(fan_hd);
580 }
581
582 /* If leaving failure mode, unclamp cpufreq and readjust
583 * all fans on next iteration
584 */
585 if (!wf_smu_failure_state && last_failure) {
586 if (cpufreq_clamp)
587 wf_control_set_min(cpufreq_clamp);
588 wf_smu_readjust = 1;
589 }
590
591 /* Overtemp condition detected, notify and start skipping a couple
592 * ticks to let the temperature go down
593 */
594 if (new_failure & FAILURE_OVERTEMP) {
595 wf_set_overtemp();
596 wf_smu_skipping = 2;
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300597 wf_smu_overtemp = true;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100598 }
599
600 /* We only clear the overtemp condition if overtemp is cleared
601 * _and_ no other failure is present. Since a sensor error will
602 * clear the overtemp condition (can't measure temperature) at
603 * the control loop levels, but we don't want to keep it clear
604 * here in this case
605 */
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300606 if (!wf_smu_failure_state && wf_smu_overtemp) {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100607 wf_clear_overtemp();
Aaro Koskinen4bb29712013-06-30 22:00:42 +0300608 wf_smu_overtemp = false;
609 }
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100610}
611
612static void wf_smu_new_control(struct wf_control *ct)
613{
614 if (wf_smu_all_controls_ok)
615 return;
616
617 if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-fan")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100618 if (wf_get_control(ct) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100619 fan_cpu_main = ct;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100620 }
621
622 if (fan_system == NULL && !strcmp(ct->name, "system-fan")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100623 if (wf_get_control(ct) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100624 fan_system = ct;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100625 }
626
627 if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
628 if (wf_get_control(ct) == 0)
629 cpufreq_clamp = ct;
630 }
631
632 /* Darwin property list says the HD fan is only for model ID
633 * 0, 1, 2 and 3
634 */
635
636 if (wf_smu_mach_model > 3) {
637 if (fan_system && fan_cpu_main && cpufreq_clamp)
638 wf_smu_all_controls_ok = 1;
639 return;
640 }
641
642 if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100643 if (wf_get_control(ct) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100644 fan_hd = ct;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100645 }
646
647 if (fan_system && fan_hd && fan_cpu_main && cpufreq_clamp)
648 wf_smu_all_controls_ok = 1;
649}
650
651static void wf_smu_new_sensor(struct wf_sensor *sr)
652{
653 if (wf_smu_all_sensors_ok)
654 return;
655
656 if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100657 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100658 sensor_cpu_power = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100659 }
660
661 if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100662 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100663 sensor_cpu_temp = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100664 }
665
666 if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100667 if (wf_get_sensor(sr) == 0)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100668 sensor_hd_temp = sr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100669 }
670
671 if (sensor_cpu_power && sensor_cpu_temp && sensor_hd_temp)
672 wf_smu_all_sensors_ok = 1;
673}
674
675
676static int wf_smu_notify(struct notifier_block *self,
677 unsigned long event, void *data)
678{
679 switch(event) {
680 case WF_EVENT_NEW_CONTROL:
681 DBG("wf: new control %s detected\n",
682 ((struct wf_control *)data)->name);
683 wf_smu_new_control(data);
684 wf_smu_readjust = 1;
685 break;
686 case WF_EVENT_NEW_SENSOR:
687 DBG("wf: new sensor %s detected\n",
688 ((struct wf_sensor *)data)->name);
689 wf_smu_new_sensor(data);
690 break;
691 case WF_EVENT_TICK:
692 if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
693 wf_smu_tick();
694 }
695
696 return 0;
697}
698
699static struct notifier_block wf_smu_events = {
700 .notifier_call = wf_smu_notify,
701};
702
703static int wf_init_pm(void)
704{
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000705 const struct smu_sdbp_header *hdr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100706
707 hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
708 if (hdr != 0) {
709 struct smu_sdbp_sensortree *st =
710 (struct smu_sdbp_sensortree *)&hdr[1];
711 wf_smu_mach_model = st->model_id;
712 }
713
714 printk(KERN_INFO "windfarm: Initializing for iMacG5 model ID %d\n",
715 wf_smu_mach_model);
716
717 return 0;
718}
719
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000720static int wf_smu_probe(struct platform_device *ddev)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100721{
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100722 wf_register_client(&wf_smu_events);
723
724 return 0;
725}
726
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800727static int wf_smu_remove(struct platform_device *ddev)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100728{
729 wf_unregister_client(&wf_smu_events);
730
731 /* XXX We don't have yet a guarantee that our callback isn't
732 * in progress when returning from wf_unregister_client, so
733 * we add an arbitrary delay. I'll have to fix that in the core
734 */
735 msleep(1000);
736
737 /* Release all sensors */
738 /* One more crappy race: I don't think we have any guarantee here
739 * that the attribute callback won't race with the sensor beeing
740 * disposed of, and I'm not 100% certain what best way to deal
741 * with that except by adding locks all over... I'll do that
742 * eventually but heh, who ever rmmod this module anyway ?
743 */
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100744 if (sensor_cpu_power)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100745 wf_put_sensor(sensor_cpu_power);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100746 if (sensor_cpu_temp)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100747 wf_put_sensor(sensor_cpu_temp);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100748 if (sensor_hd_temp)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100749 wf_put_sensor(sensor_hd_temp);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100750
751 /* Release all controls */
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100752 if (fan_cpu_main)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100753 wf_put_control(fan_cpu_main);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100754 if (fan_hd)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100755 wf_put_control(fan_hd);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100756 if (fan_system)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100757 wf_put_control(fan_system);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100758 if (cpufreq_clamp)
759 wf_put_control(cpufreq_clamp);
760
761 /* Destroy control loops state structures */
d binderman6d1bdd22010-03-18 23:01:42 +0000762 kfree(wf_smu_sys_fans);
763 kfree(wf_smu_cpu_fans);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100764
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100765 return 0;
766}
767
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000768static struct platform_driver wf_smu_driver = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100769 .probe = wf_smu_probe,
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800770 .remove = wf_smu_remove,
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000771 .driver = {
772 .name = "windfarm",
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000773 },
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100774};
775
776
777static int __init wf_smu_init(void)
778{
779 int rc = -ENODEV;
780
Grant Likely71a157e2010-02-01 21:34:14 -0700781 if (of_machine_is_compatible("PowerMac8,1") ||
782 of_machine_is_compatible("PowerMac8,2"))
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100783 rc = wf_init_pm();
784
785 if (rc == 0) {
786#ifdef MODULE
787 request_module("windfarm_smu_controls");
788 request_module("windfarm_smu_sensors");
789 request_module("windfarm_lm75_sensor");
Benjamin Herrenschmidtd31e8172006-11-16 14:00:57 +1100790 request_module("windfarm_cpufreq_clamp");
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100791
792#endif /* MODULE */
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000793 platform_driver_register(&wf_smu_driver);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100794 }
795
796 return rc;
797}
798
799static void __exit wf_smu_exit(void)
800{
801
Benjamin Herrenschmidt10270612006-10-10 11:47:31 +1000802 platform_driver_unregister(&wf_smu_driver);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100803}
804
805
806module_init(wf_smu_init);
807module_exit(wf_smu_exit);
808
809MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
810MODULE_DESCRIPTION("Thermal control logic for iMac G5");
811MODULE_LICENSE("GPL");
Kay Sievers23386fe2008-04-18 13:33:53 -0700812MODULE_ALIAS("platform:windfarm");