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