Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Windfarm PowerMac thermal control.
  4 * Control loops for machines with SMU and PPC970MP processors.
  5 *
  6 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
  7 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  8 */
  9#include <linux/types.h>
 10#include <linux/errno.h>
 11#include <linux/kernel.h>
 12#include <linux/device.h>
 13#include <linux/platform_device.h>
 14#include <linux/reboot.h>
 15#include <asm/prom.h>
 16#include <asm/smu.h>
 17
 18#include "windfarm.h"
 19#include "windfarm_pid.h"
 20
 21#define VERSION "0.2"
 22
 23#define DEBUG
 24#undef LOTSA_DEBUG
 25
 26#ifdef DEBUG
 27#define DBG(args...)	printk(args)
 28#else
 29#define DBG(args...)	do { } while(0)
 30#endif
 31
 32#ifdef LOTSA_DEBUG
 33#define DBG_LOTS(args...)	printk(args)
 34#else
 35#define DBG_LOTS(args...)	do { } while(0)
 36#endif
 37
 38/* define this to force CPU overtemp to 60 degree, useful for testing
 39 * the overtemp code
 40 */
 41#undef HACKED_OVERTEMP
 42
 43/* We currently only handle 2 chips, 4 cores... */
 44#define NR_CHIPS	2
 45#define NR_CORES	4
 46#define NR_CPU_FANS	3 * NR_CHIPS
 47
 48/* Controls and sensors */
 49static struct wf_sensor *sens_cpu_temp[NR_CORES];
 50static struct wf_sensor *sens_cpu_power[NR_CORES];
 51static struct wf_sensor *hd_temp;
 52static struct wf_sensor *slots_power;
 53static struct wf_sensor *u4_temp;
 54
 55static struct wf_control *cpu_fans[NR_CPU_FANS];
 56static char *cpu_fan_names[NR_CPU_FANS] = {
 57	"cpu-rear-fan-0",
 58	"cpu-rear-fan-1",
 59	"cpu-front-fan-0",
 60	"cpu-front-fan-1",
 61	"cpu-pump-0",
 62	"cpu-pump-1",
 63};
 64static struct wf_control *cpufreq_clamp;
 65
 66/* Second pump isn't required (and isn't actually present) */
 67#define CPU_FANS_REQD		(NR_CPU_FANS - 2)
 68#define FIRST_PUMP		4
 69#define LAST_PUMP		5
 70
 71/* We keep a temperature history for average calculation of 180s */
 72#define CPU_TEMP_HIST_SIZE	180
 73
 74/* Scale factor for fan speed, *100 */
 75static int cpu_fan_scale[NR_CPU_FANS] = {
 76	100,
 77	100,
 78	97,		/* inlet fans run at 97% of exhaust fan */
 79	97,
 80	100,		/* updated later */
 81	100,		/* updated later */
 82};
 83
 84static struct wf_control *backside_fan;
 85static struct wf_control *slots_fan;
 86static struct wf_control *drive_bay_fan;
 87
 88/* PID loop state */
 89static struct wf_cpu_pid_state cpu_pid[NR_CORES];
 90static u32 cpu_thist[CPU_TEMP_HIST_SIZE];
 91static int cpu_thist_pt;
 92static s64 cpu_thist_total;
 93static s32 cpu_all_tmax = 100 << 16;
 94static int cpu_last_target;
 95static struct wf_pid_state backside_pid;
 96static int backside_tick;
 97static struct wf_pid_state slots_pid;
 98static bool slots_started;
 99static struct wf_pid_state drive_bay_pid;
100static int drive_bay_tick;
101
102static int nr_cores;
103static int have_all_controls;
104static int have_all_sensors;
105static bool started;
106
107static int failure_state;
108#define FAILURE_SENSOR		1
109#define FAILURE_FAN		2
110#define FAILURE_PERM		4
111#define FAILURE_LOW_OVERTEMP	8
112#define FAILURE_HIGH_OVERTEMP	16
113
114/* Overtemp values */
115#define LOW_OVER_AVERAGE	0
116#define LOW_OVER_IMMEDIATE	(10 << 16)
117#define LOW_OVER_CLEAR		((-10) << 16)
118#define HIGH_OVER_IMMEDIATE	(14 << 16)
119#define HIGH_OVER_AVERAGE	(10 << 16)
120#define HIGH_OVER_IMMEDIATE	(14 << 16)
121
122
123/* Implementation... */
124static int create_cpu_loop(int cpu)
125{
126	int chip = cpu / 2;
127	int core = cpu & 1;
128	struct smu_sdbp_header *hdr;
129	struct smu_sdbp_cpupiddata *piddata;
130	struct wf_cpu_pid_param pid;
131	struct wf_control *main_fan = cpu_fans[0];
132	s32 tmax;
133	int fmin;
134
 
 
 
 
 
 
 
 
135	/* Get FVT params to get Tmax; if not found, assume default */
136	hdr = smu_sat_get_sdb_partition(chip, 0xC4 + core, NULL);
137	if (hdr) {
138		struct smu_sdbp_fvt *fvt = (struct smu_sdbp_fvt *)&hdr[1];
139		tmax = fvt->maxtemp << 16;
140	} else
141		tmax = 95 << 16;	/* default to 95 degrees C */
142
143	/* We keep a global tmax for overtemp calculations */
144	if (tmax < cpu_all_tmax)
145		cpu_all_tmax = tmax;
146
147	kfree(hdr);
148
149	/* Get PID params from the appropriate SAT */
150	hdr = smu_sat_get_sdb_partition(chip, 0xC8 + core, NULL);
151	if (hdr == NULL) {
152		printk(KERN_WARNING"windfarm: can't get CPU PID fan config\n");
153		return -EINVAL;
154	}
155	piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
156
157	/*
158	 * Darwin has a minimum fan speed of 1000 rpm for the 4-way and
159	 * 515 for the 2-way.  That appears to be overkill, so for now,
160	 * impose a minimum of 750 or 515.
161	 */
162	fmin = (nr_cores > 2) ? 750 : 515;
163
164	/* Initialize PID loop */
165	pid.interval = 1;	/* seconds */
166	pid.history_len = piddata->history_len;
167	pid.gd = piddata->gd;
168	pid.gp = piddata->gp;
169	pid.gr = piddata->gr / piddata->history_len;
170	pid.pmaxadj = (piddata->max_power << 16) - (piddata->power_adj << 8);
171	pid.ttarget = tmax - (piddata->target_temp_delta << 16);
172	pid.tmax = tmax;
173	pid.min = main_fan->ops->get_min(main_fan);
174	pid.max = main_fan->ops->get_max(main_fan);
175	if (pid.min < fmin)
176		pid.min = fmin;
177
178	wf_cpu_pid_init(&cpu_pid[cpu], &pid);
179
180	kfree(hdr);
181
182	return 0;
183}
184
185static void cpu_max_all_fans(void)
186{
187	int i;
188
189	/* We max all CPU fans in case of a sensor error. We also do the
190	 * cpufreq clamping now, even if it's supposedly done later by the
191	 * generic code anyway, we do it earlier here to react faster
192	 */
193	if (cpufreq_clamp)
194		wf_control_set_max(cpufreq_clamp);
195	for (i = 0; i < NR_CPU_FANS; ++i)
196		if (cpu_fans[i])
197			wf_control_set_max(cpu_fans[i]);
198}
199
200static int cpu_check_overtemp(s32 temp)
201{
202	int new_state = 0;
203	s32 t_avg, t_old;
204
205	/* First check for immediate overtemps */
206	if (temp >= (cpu_all_tmax + LOW_OVER_IMMEDIATE)) {
207		new_state |= FAILURE_LOW_OVERTEMP;
208		if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
209			printk(KERN_ERR "windfarm: Overtemp due to immediate CPU"
210			       " temperature !\n");
211	}
212	if (temp >= (cpu_all_tmax + HIGH_OVER_IMMEDIATE)) {
213		new_state |= FAILURE_HIGH_OVERTEMP;
214		if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
215			printk(KERN_ERR "windfarm: Critical overtemp due to"
216			       " immediate CPU temperature !\n");
217	}
218
219	/* We calculate a history of max temperatures and use that for the
220	 * overtemp management
221	 */
222	t_old = cpu_thist[cpu_thist_pt];
223	cpu_thist[cpu_thist_pt] = temp;
224	cpu_thist_pt = (cpu_thist_pt + 1) % CPU_TEMP_HIST_SIZE;
225	cpu_thist_total -= t_old;
226	cpu_thist_total += temp;
227	t_avg = cpu_thist_total / CPU_TEMP_HIST_SIZE;
228
229	DBG_LOTS("t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
230		 FIX32TOPRINT(t_avg), FIX32TOPRINT(t_old), FIX32TOPRINT(temp));
231
232	/* Now check for average overtemps */
233	if (t_avg >= (cpu_all_tmax + LOW_OVER_AVERAGE)) {
234		new_state |= FAILURE_LOW_OVERTEMP;
235		if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
236			printk(KERN_ERR "windfarm: Overtemp due to average CPU"
237			       " temperature !\n");
238	}
239	if (t_avg >= (cpu_all_tmax + HIGH_OVER_AVERAGE)) {
240		new_state |= FAILURE_HIGH_OVERTEMP;
241		if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
242			printk(KERN_ERR "windfarm: Critical overtemp due to"
243			       " average CPU temperature !\n");
244	}
245
246	/* Now handle overtemp conditions. We don't currently use the windfarm
247	 * overtemp handling core as it's not fully suited to the needs of those
248	 * new machine. This will be fixed later.
249	 */
250	if (new_state) {
251		/* High overtemp -> immediate shutdown */
252		if (new_state & FAILURE_HIGH_OVERTEMP)
253			machine_power_off();
254		if ((failure_state & new_state) != new_state)
255			cpu_max_all_fans();
256		failure_state |= new_state;
257	} else if ((failure_state & FAILURE_LOW_OVERTEMP) &&
258		   (temp < (cpu_all_tmax + LOW_OVER_CLEAR))) {
259		printk(KERN_ERR "windfarm: Overtemp condition cleared !\n");
260		failure_state &= ~FAILURE_LOW_OVERTEMP;
261	}
262
263	return failure_state & (FAILURE_LOW_OVERTEMP | FAILURE_HIGH_OVERTEMP);
264}
265
266static void cpu_fans_tick(void)
267{
268	int err, cpu;
269	s32 greatest_delta = 0;
270	s32 temp, power, t_max = 0;
271	int i, t, target = 0;
272	struct wf_sensor *sr;
273	struct wf_control *ct;
274	struct wf_cpu_pid_state *sp;
275
276	DBG_LOTS(KERN_DEBUG);
277	for (cpu = 0; cpu < nr_cores; ++cpu) {
278		/* Get CPU core temperature */
279		sr = sens_cpu_temp[cpu];
280		err = sr->ops->get_value(sr, &temp);
281		if (err) {
282			DBG("\n");
283			printk(KERN_WARNING "windfarm: CPU %d temperature "
284			       "sensor error %d\n", cpu, err);
285			failure_state |= FAILURE_SENSOR;
286			cpu_max_all_fans();
287			return;
288		}
289
290		/* Keep track of highest temp */
291		t_max = max(t_max, temp);
292
293		/* Get CPU power */
294		sr = sens_cpu_power[cpu];
295		err = sr->ops->get_value(sr, &power);
296		if (err) {
297			DBG("\n");
298			printk(KERN_WARNING "windfarm: CPU %d power "
299			       "sensor error %d\n", cpu, err);
300			failure_state |= FAILURE_SENSOR;
301			cpu_max_all_fans();
302			return;
303		}
304
305		/* Run PID */
306		sp = &cpu_pid[cpu];
307		t = wf_cpu_pid_run(sp, power, temp);
308
309		if (cpu == 0 || sp->last_delta > greatest_delta) {
310			greatest_delta = sp->last_delta;
311			target = t;
312		}
313		DBG_LOTS("[%d] P=%d.%.3d T=%d.%.3d ",
314		    cpu, FIX32TOPRINT(power), FIX32TOPRINT(temp));
315	}
316	DBG_LOTS("fans = %d, t_max = %d.%03d\n", target, FIX32TOPRINT(t_max));
317
318	/* Darwin limits decrease to 20 per iteration */
319	if (target < (cpu_last_target - 20))
320		target = cpu_last_target - 20;
321	cpu_last_target = target;
322	for (cpu = 0; cpu < nr_cores; ++cpu)
323		cpu_pid[cpu].target = target;
324
325	/* Handle possible overtemps */
326	if (cpu_check_overtemp(t_max))
327		return;
328
329	/* Set fans */
330	for (i = 0; i < NR_CPU_FANS; ++i) {
331		ct = cpu_fans[i];
332		if (ct == NULL)
333			continue;
334		err = ct->ops->set_value(ct, target * cpu_fan_scale[i] / 100);
335		if (err) {
336			printk(KERN_WARNING "windfarm: fan %s reports "
337			       "error %d\n", ct->name, err);
338			failure_state |= FAILURE_FAN;
339			break;
340		}
341	}
342}
343
344/* Backside/U4 fan */
345static struct wf_pid_param backside_param = {
346	.interval	= 5,
347	.history_len	= 2,
348	.gd		= 48 << 20,
349	.gp		= 5 << 20,
350	.gr		= 0,
351	.itarget	= 64 << 16,
352	.additive	= 1,
353};
354
355static void backside_fan_tick(void)
356{
357	s32 temp;
358	int speed;
359	int err;
360
361	if (!backside_fan || !u4_temp)
362		return;
363	if (!backside_tick) {
364		/* first time; initialize things */
365		printk(KERN_INFO "windfarm: Backside control loop started.\n");
366		backside_param.min = backside_fan->ops->get_min(backside_fan);
367		backside_param.max = backside_fan->ops->get_max(backside_fan);
368		wf_pid_init(&backside_pid, &backside_param);
369		backside_tick = 1;
370	}
371	if (--backside_tick > 0)
372		return;
373	backside_tick = backside_pid.param.interval;
374
375	err = u4_temp->ops->get_value(u4_temp, &temp);
376	if (err) {
377		printk(KERN_WARNING "windfarm: U4 temp sensor error %d\n",
378		       err);
379		failure_state |= FAILURE_SENSOR;
380		wf_control_set_max(backside_fan);
381		return;
382	}
383	speed = wf_pid_run(&backside_pid, temp);
384	DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
385		 FIX32TOPRINT(temp), speed);
386
387	err = backside_fan->ops->set_value(backside_fan, speed);
388	if (err) {
389		printk(KERN_WARNING "windfarm: backside fan error %d\n", err);
390		failure_state |= FAILURE_FAN;
391	}
392}
393
394/* Drive bay fan */
395static struct wf_pid_param drive_bay_prm = {
396	.interval	= 5,
397	.history_len	= 2,
398	.gd		= 30 << 20,
399	.gp		= 5 << 20,
400	.gr		= 0,
401	.itarget	= 40 << 16,
402	.additive	= 1,
403};
404
405static void drive_bay_fan_tick(void)
406{
407	s32 temp;
408	int speed;
409	int err;
410
411	if (!drive_bay_fan || !hd_temp)
412		return;
413	if (!drive_bay_tick) {
414		/* first time; initialize things */
415		printk(KERN_INFO "windfarm: Drive bay control loop started.\n");
416		drive_bay_prm.min = drive_bay_fan->ops->get_min(drive_bay_fan);
417		drive_bay_prm.max = drive_bay_fan->ops->get_max(drive_bay_fan);
418		wf_pid_init(&drive_bay_pid, &drive_bay_prm);
419		drive_bay_tick = 1;
420	}
421	if (--drive_bay_tick > 0)
422		return;
423	drive_bay_tick = drive_bay_pid.param.interval;
424
425	err = hd_temp->ops->get_value(hd_temp, &temp);
426	if (err) {
427		printk(KERN_WARNING "windfarm: drive bay temp sensor "
428		       "error %d\n", err);
429		failure_state |= FAILURE_SENSOR;
430		wf_control_set_max(drive_bay_fan);
431		return;
432	}
433	speed = wf_pid_run(&drive_bay_pid, temp);
434	DBG_LOTS("drive_bay PID temp=%d.%.3d speed=%d\n",
435		 FIX32TOPRINT(temp), speed);
436
437	err = drive_bay_fan->ops->set_value(drive_bay_fan, speed);
438	if (err) {
439		printk(KERN_WARNING "windfarm: drive bay fan error %d\n", err);
440		failure_state |= FAILURE_FAN;
441	}
442}
443
444/* PCI slots area fan */
445/* This makes the fan speed proportional to the power consumed */
446static struct wf_pid_param slots_param = {
447	.interval	= 1,
448	.history_len	= 2,
449	.gd		= 0,
450	.gp		= 0,
451	.gr		= 0x1277952,
452	.itarget	= 0,
453	.min		= 1560,
454	.max		= 3510,
455};
456
457static void slots_fan_tick(void)
458{
459	s32 power;
460	int speed;
461	int err;
462
463	if (!slots_fan || !slots_power)
464		return;
465	if (!slots_started) {
466		/* first time; initialize things */
467		printk(KERN_INFO "windfarm: Slots control loop started.\n");
468		wf_pid_init(&slots_pid, &slots_param);
469		slots_started = true;
470	}
471
472	err = slots_power->ops->get_value(slots_power, &power);
473	if (err) {
474		printk(KERN_WARNING "windfarm: slots power sensor error %d\n",
475		       err);
476		failure_state |= FAILURE_SENSOR;
477		wf_control_set_max(slots_fan);
478		return;
479	}
480	speed = wf_pid_run(&slots_pid, power);
481	DBG_LOTS("slots PID power=%d.%.3d speed=%d\n",
482		 FIX32TOPRINT(power), speed);
483
484	err = slots_fan->ops->set_value(slots_fan, speed);
485	if (err) {
486		printk(KERN_WARNING "windfarm: slots fan error %d\n", err);
487		failure_state |= FAILURE_FAN;
488	}
489}
490
491static void set_fail_state(void)
492{
493	int i;
494
495	if (cpufreq_clamp)
496		wf_control_set_max(cpufreq_clamp);
497	for (i = 0; i < NR_CPU_FANS; ++i)
498		if (cpu_fans[i])
499			wf_control_set_max(cpu_fans[i]);
500	if (backside_fan)
501		wf_control_set_max(backside_fan);
502	if (slots_fan)
503		wf_control_set_max(slots_fan);
504	if (drive_bay_fan)
505		wf_control_set_max(drive_bay_fan);
506}
507
508static void pm112_tick(void)
509{
510	int i, last_failure;
511
512	if (!started) {
513		started = true;
514		printk(KERN_INFO "windfarm: CPUs control loops started.\n");
515		for (i = 0; i < nr_cores; ++i) {
516			if (create_cpu_loop(i) < 0) {
517				failure_state = FAILURE_PERM;
518				set_fail_state();
519				break;
520			}
521		}
522		DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax));
523
524#ifdef HACKED_OVERTEMP
525		cpu_all_tmax = 60 << 16;
526#endif
527	}
528
529	/* Permanent failure, bail out */
530	if (failure_state & FAILURE_PERM)
531		return;
532	/* Clear all failure bits except low overtemp which will be eventually
533	 * cleared by the control loop itself
534	 */
535	last_failure = failure_state;
536	failure_state &= FAILURE_LOW_OVERTEMP;
537	cpu_fans_tick();
538	backside_fan_tick();
539	slots_fan_tick();
540	drive_bay_fan_tick();
541
542	DBG_LOTS("last_failure: 0x%x, failure_state: %x\n",
543		 last_failure, failure_state);
544
545	/* Check for failures. Any failure causes cpufreq clamping */
546	if (failure_state && last_failure == 0 && cpufreq_clamp)
547		wf_control_set_max(cpufreq_clamp);
548	if (failure_state == 0 && last_failure && cpufreq_clamp)
549		wf_control_set_min(cpufreq_clamp);
550
551	/* That's it for now, we might want to deal with other failures
552	 * differently in the future though
553	 */
554}
555
556static void pm112_new_control(struct wf_control *ct)
557{
558	int i, max_exhaust;
559
560	if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
561		if (wf_get_control(ct) == 0)
562			cpufreq_clamp = ct;
563	}
564
565	for (i = 0; i < NR_CPU_FANS; ++i) {
566		if (!strcmp(ct->name, cpu_fan_names[i])) {
567			if (cpu_fans[i] == NULL && wf_get_control(ct) == 0)
568				cpu_fans[i] = ct;
569			break;
570		}
571	}
572	if (i >= NR_CPU_FANS) {
573		/* not a CPU fan, try the others */
574		if (!strcmp(ct->name, "backside-fan")) {
575			if (backside_fan == NULL && wf_get_control(ct) == 0)
576				backside_fan = ct;
577		} else if (!strcmp(ct->name, "slots-fan")) {
578			if (slots_fan == NULL && wf_get_control(ct) == 0)
579				slots_fan = ct;
580		} else if (!strcmp(ct->name, "drive-bay-fan")) {
581			if (drive_bay_fan == NULL && wf_get_control(ct) == 0)
582				drive_bay_fan = ct;
583		}
584		return;
585	}
586
587	for (i = 0; i < CPU_FANS_REQD; ++i)
588		if (cpu_fans[i] == NULL)
589			return;
590
591	/* work out pump scaling factors */
592	max_exhaust = cpu_fans[0]->ops->get_max(cpu_fans[0]);
593	for (i = FIRST_PUMP; i <= LAST_PUMP; ++i)
594		if ((ct = cpu_fans[i]) != NULL)
595			cpu_fan_scale[i] =
596				ct->ops->get_max(ct) * 100 / max_exhaust;
597
598	have_all_controls = 1;
599}
600
601static void pm112_new_sensor(struct wf_sensor *sr)
602{
603	unsigned int i;
604
605	if (!strncmp(sr->name, "cpu-temp-", 9)) {
606		i = sr->name[9] - '0';
607		if (sr->name[10] == 0 && i < NR_CORES &&
608		    sens_cpu_temp[i] == NULL && wf_get_sensor(sr) == 0)
609			sens_cpu_temp[i] = sr;
610
611	} else if (!strncmp(sr->name, "cpu-power-", 10)) {
612		i = sr->name[10] - '0';
613		if (sr->name[11] == 0 && i < NR_CORES &&
614		    sens_cpu_power[i] == NULL && wf_get_sensor(sr) == 0)
615			sens_cpu_power[i] = sr;
616	} else if (!strcmp(sr->name, "hd-temp")) {
617		if (hd_temp == NULL && wf_get_sensor(sr) == 0)
618			hd_temp = sr;
619	} else if (!strcmp(sr->name, "slots-power")) {
620		if (slots_power == NULL && wf_get_sensor(sr) == 0)
621			slots_power = sr;
622	} else if (!strcmp(sr->name, "backside-temp")) {
623		if (u4_temp == NULL && wf_get_sensor(sr) == 0)
624			u4_temp = sr;
625	} else
626		return;
627
628	/* check if we have all the sensors we need */
629	for (i = 0; i < nr_cores; ++i)
630		if (sens_cpu_temp[i] == NULL || sens_cpu_power[i] == NULL)
631			return;
632
633	have_all_sensors = 1;
634}
635
636static int pm112_wf_notify(struct notifier_block *self,
637			   unsigned long event, void *data)
638{
639	switch (event) {
640	case WF_EVENT_NEW_SENSOR:
641		pm112_new_sensor(data);
642		break;
643	case WF_EVENT_NEW_CONTROL:
644		pm112_new_control(data);
645		break;
646	case WF_EVENT_TICK:
647		if (have_all_controls && have_all_sensors)
648			pm112_tick();
649	}
650	return 0;
651}
652
653static struct notifier_block pm112_events = {
654	.notifier_call = pm112_wf_notify,
655};
656
657static int wf_pm112_probe(struct platform_device *dev)
658{
659	wf_register_client(&pm112_events);
660	return 0;
661}
662
663static int wf_pm112_remove(struct platform_device *dev)
664{
665	wf_unregister_client(&pm112_events);
666	/* should release all sensors and controls */
667	return 0;
668}
669
670static struct platform_driver wf_pm112_driver = {
671	.probe = wf_pm112_probe,
672	.remove = wf_pm112_remove,
673	.driver = {
674		.name = "windfarm",
675	},
676};
677
678static int __init wf_pm112_init(void)
679{
680	struct device_node *cpu;
681
682	if (!of_machine_is_compatible("PowerMac11,2"))
683		return -ENODEV;
684
685	/* Count the number of CPU cores */
686	nr_cores = 0;
687	for_each_node_by_type(cpu, "cpu")
688		++nr_cores;
689
690	printk(KERN_INFO "windfarm: initializing for dual-core desktop G5\n");
691
692#ifdef MODULE
693	request_module("windfarm_smu_controls");
694	request_module("windfarm_smu_sensors");
695	request_module("windfarm_smu_sat");
696	request_module("windfarm_lm75_sensor");
697	request_module("windfarm_max6690_sensor");
698	request_module("windfarm_cpufreq_clamp");
699
700#endif /* MODULE */
701
702	platform_driver_register(&wf_pm112_driver);
703	return 0;
704}
705
706static void __exit wf_pm112_exit(void)
707{
708	platform_driver_unregister(&wf_pm112_driver);
709}
710
711module_init(wf_pm112_init);
712module_exit(wf_pm112_exit);
713
714MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
715MODULE_DESCRIPTION("Thermal control for PowerMac11,2");
716MODULE_LICENSE("GPL");
717MODULE_ALIAS("platform:windfarm");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Windfarm PowerMac thermal control.
  4 * Control loops for machines with SMU and PPC970MP processors.
  5 *
  6 * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
  7 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  8 */
  9#include <linux/types.h>
 10#include <linux/errno.h>
 11#include <linux/kernel.h>
 12#include <linux/device.h>
 13#include <linux/platform_device.h>
 14#include <linux/reboot.h>
 15#include <asm/prom.h>
 16#include <asm/smu.h>
 17
 18#include "windfarm.h"
 19#include "windfarm_pid.h"
 20
 21#define VERSION "0.2"
 22
 23#define DEBUG
 24#undef LOTSA_DEBUG
 25
 26#ifdef DEBUG
 27#define DBG(args...)	printk(args)
 28#else
 29#define DBG(args...)	do { } while(0)
 30#endif
 31
 32#ifdef LOTSA_DEBUG
 33#define DBG_LOTS(args...)	printk(args)
 34#else
 35#define DBG_LOTS(args...)	do { } while(0)
 36#endif
 37
 38/* define this to force CPU overtemp to 60 degree, useful for testing
 39 * the overtemp code
 40 */
 41#undef HACKED_OVERTEMP
 42
 43/* We currently only handle 2 chips, 4 cores... */
 44#define NR_CHIPS	2
 45#define NR_CORES	4
 46#define NR_CPU_FANS	3 * NR_CHIPS
 47
 48/* Controls and sensors */
 49static struct wf_sensor *sens_cpu_temp[NR_CORES];
 50static struct wf_sensor *sens_cpu_power[NR_CORES];
 51static struct wf_sensor *hd_temp;
 52static struct wf_sensor *slots_power;
 53static struct wf_sensor *u4_temp;
 54
 55static struct wf_control *cpu_fans[NR_CPU_FANS];
 56static char *cpu_fan_names[NR_CPU_FANS] = {
 57	"cpu-rear-fan-0",
 58	"cpu-rear-fan-1",
 59	"cpu-front-fan-0",
 60	"cpu-front-fan-1",
 61	"cpu-pump-0",
 62	"cpu-pump-1",
 63};
 64static struct wf_control *cpufreq_clamp;
 65
 66/* Second pump isn't required (and isn't actually present) */
 67#define CPU_FANS_REQD		(NR_CPU_FANS - 2)
 68#define FIRST_PUMP		4
 69#define LAST_PUMP		5
 70
 71/* We keep a temperature history for average calculation of 180s */
 72#define CPU_TEMP_HIST_SIZE	180
 73
 74/* Scale factor for fan speed, *100 */
 75static int cpu_fan_scale[NR_CPU_FANS] = {
 76	100,
 77	100,
 78	97,		/* inlet fans run at 97% of exhaust fan */
 79	97,
 80	100,		/* updated later */
 81	100,		/* updated later */
 82};
 83
 84static struct wf_control *backside_fan;
 85static struct wf_control *slots_fan;
 86static struct wf_control *drive_bay_fan;
 87
 88/* PID loop state */
 89static struct wf_cpu_pid_state cpu_pid[NR_CORES];
 90static u32 cpu_thist[CPU_TEMP_HIST_SIZE];
 91static int cpu_thist_pt;
 92static s64 cpu_thist_total;
 93static s32 cpu_all_tmax = 100 << 16;
 94static int cpu_last_target;
 95static struct wf_pid_state backside_pid;
 96static int backside_tick;
 97static struct wf_pid_state slots_pid;
 98static bool slots_started;
 99static struct wf_pid_state drive_bay_pid;
100static int drive_bay_tick;
101
102static int nr_cores;
103static int have_all_controls;
104static int have_all_sensors;
105static bool started;
106
107static int failure_state;
108#define FAILURE_SENSOR		1
109#define FAILURE_FAN		2
110#define FAILURE_PERM		4
111#define FAILURE_LOW_OVERTEMP	8
112#define FAILURE_HIGH_OVERTEMP	16
113
114/* Overtemp values */
115#define LOW_OVER_AVERAGE	0
116#define LOW_OVER_IMMEDIATE	(10 << 16)
117#define LOW_OVER_CLEAR		((-10) << 16)
118#define HIGH_OVER_IMMEDIATE	(14 << 16)
119#define HIGH_OVER_AVERAGE	(10 << 16)
120#define HIGH_OVER_IMMEDIATE	(14 << 16)
121
122
123/* Implementation... */
124static int create_cpu_loop(int cpu)
125{
126	int chip = cpu / 2;
127	int core = cpu & 1;
128	struct smu_sdbp_header *hdr;
129	struct smu_sdbp_cpupiddata *piddata;
130	struct wf_cpu_pid_param pid;
131	struct wf_control *main_fan = cpu_fans[0];
132	s32 tmax;
133	int fmin;
134
135	/* Get PID params from the appropriate SAT */
136	hdr = smu_sat_get_sdb_partition(chip, 0xC8 + core, NULL);
137	if (hdr == NULL) {
138		printk(KERN_WARNING"windfarm: can't get CPU PID fan config\n");
139		return -EINVAL;
140	}
141	piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
142
143	/* Get FVT params to get Tmax; if not found, assume default */
144	hdr = smu_sat_get_sdb_partition(chip, 0xC4 + core, NULL);
145	if (hdr) {
146		struct smu_sdbp_fvt *fvt = (struct smu_sdbp_fvt *)&hdr[1];
147		tmax = fvt->maxtemp << 16;
148	} else
149		tmax = 95 << 16;	/* default to 95 degrees C */
150
151	/* We keep a global tmax for overtemp calculations */
152	if (tmax < cpu_all_tmax)
153		cpu_all_tmax = tmax;
154
 
 
 
 
 
 
 
 
 
 
155	/*
156	 * Darwin has a minimum fan speed of 1000 rpm for the 4-way and
157	 * 515 for the 2-way.  That appears to be overkill, so for now,
158	 * impose a minimum of 750 or 515.
159	 */
160	fmin = (nr_cores > 2) ? 750 : 515;
161
162	/* Initialize PID loop */
163	pid.interval = 1;	/* seconds */
164	pid.history_len = piddata->history_len;
165	pid.gd = piddata->gd;
166	pid.gp = piddata->gp;
167	pid.gr = piddata->gr / piddata->history_len;
168	pid.pmaxadj = (piddata->max_power << 16) - (piddata->power_adj << 8);
169	pid.ttarget = tmax - (piddata->target_temp_delta << 16);
170	pid.tmax = tmax;
171	pid.min = main_fan->ops->get_min(main_fan);
172	pid.max = main_fan->ops->get_max(main_fan);
173	if (pid.min < fmin)
174		pid.min = fmin;
175
176	wf_cpu_pid_init(&cpu_pid[cpu], &pid);
 
 
 
177	return 0;
178}
179
180static void cpu_max_all_fans(void)
181{
182	int i;
183
184	/* We max all CPU fans in case of a sensor error. We also do the
185	 * cpufreq clamping now, even if it's supposedly done later by the
186	 * generic code anyway, we do it earlier here to react faster
187	 */
188	if (cpufreq_clamp)
189		wf_control_set_max(cpufreq_clamp);
190	for (i = 0; i < NR_CPU_FANS; ++i)
191		if (cpu_fans[i])
192			wf_control_set_max(cpu_fans[i]);
193}
194
195static int cpu_check_overtemp(s32 temp)
196{
197	int new_state = 0;
198	s32 t_avg, t_old;
199
200	/* First check for immediate overtemps */
201	if (temp >= (cpu_all_tmax + LOW_OVER_IMMEDIATE)) {
202		new_state |= FAILURE_LOW_OVERTEMP;
203		if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
204			printk(KERN_ERR "windfarm: Overtemp due to immediate CPU"
205			       " temperature !\n");
206	}
207	if (temp >= (cpu_all_tmax + HIGH_OVER_IMMEDIATE)) {
208		new_state |= FAILURE_HIGH_OVERTEMP;
209		if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
210			printk(KERN_ERR "windfarm: Critical overtemp due to"
211			       " immediate CPU temperature !\n");
212	}
213
214	/* We calculate a history of max temperatures and use that for the
215	 * overtemp management
216	 */
217	t_old = cpu_thist[cpu_thist_pt];
218	cpu_thist[cpu_thist_pt] = temp;
219	cpu_thist_pt = (cpu_thist_pt + 1) % CPU_TEMP_HIST_SIZE;
220	cpu_thist_total -= t_old;
221	cpu_thist_total += temp;
222	t_avg = cpu_thist_total / CPU_TEMP_HIST_SIZE;
223
224	DBG_LOTS("t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
225		 FIX32TOPRINT(t_avg), FIX32TOPRINT(t_old), FIX32TOPRINT(temp));
226
227	/* Now check for average overtemps */
228	if (t_avg >= (cpu_all_tmax + LOW_OVER_AVERAGE)) {
229		new_state |= FAILURE_LOW_OVERTEMP;
230		if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
231			printk(KERN_ERR "windfarm: Overtemp due to average CPU"
232			       " temperature !\n");
233	}
234	if (t_avg >= (cpu_all_tmax + HIGH_OVER_AVERAGE)) {
235		new_state |= FAILURE_HIGH_OVERTEMP;
236		if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
237			printk(KERN_ERR "windfarm: Critical overtemp due to"
238			       " average CPU temperature !\n");
239	}
240
241	/* Now handle overtemp conditions. We don't currently use the windfarm
242	 * overtemp handling core as it's not fully suited to the needs of those
243	 * new machine. This will be fixed later.
244	 */
245	if (new_state) {
246		/* High overtemp -> immediate shutdown */
247		if (new_state & FAILURE_HIGH_OVERTEMP)
248			machine_power_off();
249		if ((failure_state & new_state) != new_state)
250			cpu_max_all_fans();
251		failure_state |= new_state;
252	} else if ((failure_state & FAILURE_LOW_OVERTEMP) &&
253		   (temp < (cpu_all_tmax + LOW_OVER_CLEAR))) {
254		printk(KERN_ERR "windfarm: Overtemp condition cleared !\n");
255		failure_state &= ~FAILURE_LOW_OVERTEMP;
256	}
257
258	return failure_state & (FAILURE_LOW_OVERTEMP | FAILURE_HIGH_OVERTEMP);
259}
260
261static void cpu_fans_tick(void)
262{
263	int err, cpu;
264	s32 greatest_delta = 0;
265	s32 temp, power, t_max = 0;
266	int i, t, target = 0;
267	struct wf_sensor *sr;
268	struct wf_control *ct;
269	struct wf_cpu_pid_state *sp;
270
271	DBG_LOTS(KERN_DEBUG);
272	for (cpu = 0; cpu < nr_cores; ++cpu) {
273		/* Get CPU core temperature */
274		sr = sens_cpu_temp[cpu];
275		err = sr->ops->get_value(sr, &temp);
276		if (err) {
277			DBG("\n");
278			printk(KERN_WARNING "windfarm: CPU %d temperature "
279			       "sensor error %d\n", cpu, err);
280			failure_state |= FAILURE_SENSOR;
281			cpu_max_all_fans();
282			return;
283		}
284
285		/* Keep track of highest temp */
286		t_max = max(t_max, temp);
287
288		/* Get CPU power */
289		sr = sens_cpu_power[cpu];
290		err = sr->ops->get_value(sr, &power);
291		if (err) {
292			DBG("\n");
293			printk(KERN_WARNING "windfarm: CPU %d power "
294			       "sensor error %d\n", cpu, err);
295			failure_state |= FAILURE_SENSOR;
296			cpu_max_all_fans();
297			return;
298		}
299
300		/* Run PID */
301		sp = &cpu_pid[cpu];
302		t = wf_cpu_pid_run(sp, power, temp);
303
304		if (cpu == 0 || sp->last_delta > greatest_delta) {
305			greatest_delta = sp->last_delta;
306			target = t;
307		}
308		DBG_LOTS("[%d] P=%d.%.3d T=%d.%.3d ",
309		    cpu, FIX32TOPRINT(power), FIX32TOPRINT(temp));
310	}
311	DBG_LOTS("fans = %d, t_max = %d.%03d\n", target, FIX32TOPRINT(t_max));
312
313	/* Darwin limits decrease to 20 per iteration */
314	if (target < (cpu_last_target - 20))
315		target = cpu_last_target - 20;
316	cpu_last_target = target;
317	for (cpu = 0; cpu < nr_cores; ++cpu)
318		cpu_pid[cpu].target = target;
319
320	/* Handle possible overtemps */
321	if (cpu_check_overtemp(t_max))
322		return;
323
324	/* Set fans */
325	for (i = 0; i < NR_CPU_FANS; ++i) {
326		ct = cpu_fans[i];
327		if (ct == NULL)
328			continue;
329		err = ct->ops->set_value(ct, target * cpu_fan_scale[i] / 100);
330		if (err) {
331			printk(KERN_WARNING "windfarm: fan %s reports "
332			       "error %d\n", ct->name, err);
333			failure_state |= FAILURE_FAN;
334			break;
335		}
336	}
337}
338
339/* Backside/U4 fan */
340static struct wf_pid_param backside_param = {
341	.interval	= 5,
342	.history_len	= 2,
343	.gd		= 48 << 20,
344	.gp		= 5 << 20,
345	.gr		= 0,
346	.itarget	= 64 << 16,
347	.additive	= 1,
348};
349
350static void backside_fan_tick(void)
351{
352	s32 temp;
353	int speed;
354	int err;
355
356	if (!backside_fan || !u4_temp)
357		return;
358	if (!backside_tick) {
359		/* first time; initialize things */
360		printk(KERN_INFO "windfarm: Backside control loop started.\n");
361		backside_param.min = backside_fan->ops->get_min(backside_fan);
362		backside_param.max = backside_fan->ops->get_max(backside_fan);
363		wf_pid_init(&backside_pid, &backside_param);
364		backside_tick = 1;
365	}
366	if (--backside_tick > 0)
367		return;
368	backside_tick = backside_pid.param.interval;
369
370	err = u4_temp->ops->get_value(u4_temp, &temp);
371	if (err) {
372		printk(KERN_WARNING "windfarm: U4 temp sensor error %d\n",
373		       err);
374		failure_state |= FAILURE_SENSOR;
375		wf_control_set_max(backside_fan);
376		return;
377	}
378	speed = wf_pid_run(&backside_pid, temp);
379	DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
380		 FIX32TOPRINT(temp), speed);
381
382	err = backside_fan->ops->set_value(backside_fan, speed);
383	if (err) {
384		printk(KERN_WARNING "windfarm: backside fan error %d\n", err);
385		failure_state |= FAILURE_FAN;
386	}
387}
388
389/* Drive bay fan */
390static struct wf_pid_param drive_bay_prm = {
391	.interval	= 5,
392	.history_len	= 2,
393	.gd		= 30 << 20,
394	.gp		= 5 << 20,
395	.gr		= 0,
396	.itarget	= 40 << 16,
397	.additive	= 1,
398};
399
400static void drive_bay_fan_tick(void)
401{
402	s32 temp;
403	int speed;
404	int err;
405
406	if (!drive_bay_fan || !hd_temp)
407		return;
408	if (!drive_bay_tick) {
409		/* first time; initialize things */
410		printk(KERN_INFO "windfarm: Drive bay control loop started.\n");
411		drive_bay_prm.min = drive_bay_fan->ops->get_min(drive_bay_fan);
412		drive_bay_prm.max = drive_bay_fan->ops->get_max(drive_bay_fan);
413		wf_pid_init(&drive_bay_pid, &drive_bay_prm);
414		drive_bay_tick = 1;
415	}
416	if (--drive_bay_tick > 0)
417		return;
418	drive_bay_tick = drive_bay_pid.param.interval;
419
420	err = hd_temp->ops->get_value(hd_temp, &temp);
421	if (err) {
422		printk(KERN_WARNING "windfarm: drive bay temp sensor "
423		       "error %d\n", err);
424		failure_state |= FAILURE_SENSOR;
425		wf_control_set_max(drive_bay_fan);
426		return;
427	}
428	speed = wf_pid_run(&drive_bay_pid, temp);
429	DBG_LOTS("drive_bay PID temp=%d.%.3d speed=%d\n",
430		 FIX32TOPRINT(temp), speed);
431
432	err = drive_bay_fan->ops->set_value(drive_bay_fan, speed);
433	if (err) {
434		printk(KERN_WARNING "windfarm: drive bay fan error %d\n", err);
435		failure_state |= FAILURE_FAN;
436	}
437}
438
439/* PCI slots area fan */
440/* This makes the fan speed proportional to the power consumed */
441static struct wf_pid_param slots_param = {
442	.interval	= 1,
443	.history_len	= 2,
444	.gd		= 0,
445	.gp		= 0,
446	.gr		= 0x1277952,
447	.itarget	= 0,
448	.min		= 1560,
449	.max		= 3510,
450};
451
452static void slots_fan_tick(void)
453{
454	s32 power;
455	int speed;
456	int err;
457
458	if (!slots_fan || !slots_power)
459		return;
460	if (!slots_started) {
461		/* first time; initialize things */
462		printk(KERN_INFO "windfarm: Slots control loop started.\n");
463		wf_pid_init(&slots_pid, &slots_param);
464		slots_started = true;
465	}
466
467	err = slots_power->ops->get_value(slots_power, &power);
468	if (err) {
469		printk(KERN_WARNING "windfarm: slots power sensor error %d\n",
470		       err);
471		failure_state |= FAILURE_SENSOR;
472		wf_control_set_max(slots_fan);
473		return;
474	}
475	speed = wf_pid_run(&slots_pid, power);
476	DBG_LOTS("slots PID power=%d.%.3d speed=%d\n",
477		 FIX32TOPRINT(power), speed);
478
479	err = slots_fan->ops->set_value(slots_fan, speed);
480	if (err) {
481		printk(KERN_WARNING "windfarm: slots fan error %d\n", err);
482		failure_state |= FAILURE_FAN;
483	}
484}
485
486static void set_fail_state(void)
487{
488	int i;
489
490	if (cpufreq_clamp)
491		wf_control_set_max(cpufreq_clamp);
492	for (i = 0; i < NR_CPU_FANS; ++i)
493		if (cpu_fans[i])
494			wf_control_set_max(cpu_fans[i]);
495	if (backside_fan)
496		wf_control_set_max(backside_fan);
497	if (slots_fan)
498		wf_control_set_max(slots_fan);
499	if (drive_bay_fan)
500		wf_control_set_max(drive_bay_fan);
501}
502
503static void pm112_tick(void)
504{
505	int i, last_failure;
506
507	if (!started) {
508		started = true;
509		printk(KERN_INFO "windfarm: CPUs control loops started.\n");
510		for (i = 0; i < nr_cores; ++i) {
511			if (create_cpu_loop(i) < 0) {
512				failure_state = FAILURE_PERM;
513				set_fail_state();
514				break;
515			}
516		}
517		DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax));
518
519#ifdef HACKED_OVERTEMP
520		cpu_all_tmax = 60 << 16;
521#endif
522	}
523
524	/* Permanent failure, bail out */
525	if (failure_state & FAILURE_PERM)
526		return;
527	/* Clear all failure bits except low overtemp which will be eventually
528	 * cleared by the control loop itself
529	 */
530	last_failure = failure_state;
531	failure_state &= FAILURE_LOW_OVERTEMP;
532	cpu_fans_tick();
533	backside_fan_tick();
534	slots_fan_tick();
535	drive_bay_fan_tick();
536
537	DBG_LOTS("last_failure: 0x%x, failure_state: %x\n",
538		 last_failure, failure_state);
539
540	/* Check for failures. Any failure causes cpufreq clamping */
541	if (failure_state && last_failure == 0 && cpufreq_clamp)
542		wf_control_set_max(cpufreq_clamp);
543	if (failure_state == 0 && last_failure && cpufreq_clamp)
544		wf_control_set_min(cpufreq_clamp);
545
546	/* That's it for now, we might want to deal with other failures
547	 * differently in the future though
548	 */
549}
550
551static void pm112_new_control(struct wf_control *ct)
552{
553	int i, max_exhaust;
554
555	if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
556		if (wf_get_control(ct) == 0)
557			cpufreq_clamp = ct;
558	}
559
560	for (i = 0; i < NR_CPU_FANS; ++i) {
561		if (!strcmp(ct->name, cpu_fan_names[i])) {
562			if (cpu_fans[i] == NULL && wf_get_control(ct) == 0)
563				cpu_fans[i] = ct;
564			break;
565		}
566	}
567	if (i >= NR_CPU_FANS) {
568		/* not a CPU fan, try the others */
569		if (!strcmp(ct->name, "backside-fan")) {
570			if (backside_fan == NULL && wf_get_control(ct) == 0)
571				backside_fan = ct;
572		} else if (!strcmp(ct->name, "slots-fan")) {
573			if (slots_fan == NULL && wf_get_control(ct) == 0)
574				slots_fan = ct;
575		} else if (!strcmp(ct->name, "drive-bay-fan")) {
576			if (drive_bay_fan == NULL && wf_get_control(ct) == 0)
577				drive_bay_fan = ct;
578		}
579		return;
580	}
581
582	for (i = 0; i < CPU_FANS_REQD; ++i)
583		if (cpu_fans[i] == NULL)
584			return;
585
586	/* work out pump scaling factors */
587	max_exhaust = cpu_fans[0]->ops->get_max(cpu_fans[0]);
588	for (i = FIRST_PUMP; i <= LAST_PUMP; ++i)
589		if ((ct = cpu_fans[i]) != NULL)
590			cpu_fan_scale[i] =
591				ct->ops->get_max(ct) * 100 / max_exhaust;
592
593	have_all_controls = 1;
594}
595
596static void pm112_new_sensor(struct wf_sensor *sr)
597{
598	unsigned int i;
599
600	if (!strncmp(sr->name, "cpu-temp-", 9)) {
601		i = sr->name[9] - '0';
602		if (sr->name[10] == 0 && i < NR_CORES &&
603		    sens_cpu_temp[i] == NULL && wf_get_sensor(sr) == 0)
604			sens_cpu_temp[i] = sr;
605
606	} else if (!strncmp(sr->name, "cpu-power-", 10)) {
607		i = sr->name[10] - '0';
608		if (sr->name[11] == 0 && i < NR_CORES &&
609		    sens_cpu_power[i] == NULL && wf_get_sensor(sr) == 0)
610			sens_cpu_power[i] = sr;
611	} else if (!strcmp(sr->name, "hd-temp")) {
612		if (hd_temp == NULL && wf_get_sensor(sr) == 0)
613			hd_temp = sr;
614	} else if (!strcmp(sr->name, "slots-power")) {
615		if (slots_power == NULL && wf_get_sensor(sr) == 0)
616			slots_power = sr;
617	} else if (!strcmp(sr->name, "backside-temp")) {
618		if (u4_temp == NULL && wf_get_sensor(sr) == 0)
619			u4_temp = sr;
620	} else
621		return;
622
623	/* check if we have all the sensors we need */
624	for (i = 0; i < nr_cores; ++i)
625		if (sens_cpu_temp[i] == NULL || sens_cpu_power[i] == NULL)
626			return;
627
628	have_all_sensors = 1;
629}
630
631static int pm112_wf_notify(struct notifier_block *self,
632			   unsigned long event, void *data)
633{
634	switch (event) {
635	case WF_EVENT_NEW_SENSOR:
636		pm112_new_sensor(data);
637		break;
638	case WF_EVENT_NEW_CONTROL:
639		pm112_new_control(data);
640		break;
641	case WF_EVENT_TICK:
642		if (have_all_controls && have_all_sensors)
643			pm112_tick();
644	}
645	return 0;
646}
647
648static struct notifier_block pm112_events = {
649	.notifier_call = pm112_wf_notify,
650};
651
652static int wf_pm112_probe(struct platform_device *dev)
653{
654	wf_register_client(&pm112_events);
655	return 0;
656}
657
658static int wf_pm112_remove(struct platform_device *dev)
659{
660	wf_unregister_client(&pm112_events);
661	/* should release all sensors and controls */
662	return 0;
663}
664
665static struct platform_driver wf_pm112_driver = {
666	.probe = wf_pm112_probe,
667	.remove = wf_pm112_remove,
668	.driver = {
669		.name = "windfarm",
670	},
671};
672
673static int __init wf_pm112_init(void)
674{
675	struct device_node *cpu;
676
677	if (!of_machine_is_compatible("PowerMac11,2"))
678		return -ENODEV;
679
680	/* Count the number of CPU cores */
681	nr_cores = 0;
682	for_each_node_by_type(cpu, "cpu")
683		++nr_cores;
684
685	printk(KERN_INFO "windfarm: initializing for dual-core desktop G5\n");
686
687#ifdef MODULE
688	request_module("windfarm_smu_controls");
689	request_module("windfarm_smu_sensors");
690	request_module("windfarm_smu_sat");
691	request_module("windfarm_lm75_sensor");
692	request_module("windfarm_max6690_sensor");
693	request_module("windfarm_cpufreq_clamp");
694
695#endif /* MODULE */
696
697	platform_driver_register(&wf_pm112_driver);
698	return 0;
699}
700
701static void __exit wf_pm112_exit(void)
702{
703	platform_driver_unregister(&wf_pm112_driver);
704}
705
706module_init(wf_pm112_init);
707module_exit(wf_pm112_exit);
708
709MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
710MODULE_DESCRIPTION("Thermal control for PowerMac11,2");
711MODULE_LICENSE("GPL");
712MODULE_ALIAS("platform:windfarm");