Linux Audio

Check our new training course

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