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