xref: /linux/drivers/macintosh/windfarm_pm121.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Windfarm PowerMac thermal control. iMac G5 iSight
4  *
5  * (c) Copyright 2007 Étienne Bersac <bersace@gmail.com>
6  *
7  * Bits & pieces from windfarm_pm81.c by (c) Copyright 2005 Benjamin
8  * Herrenschmidt, IBM Corp. <benh@kernel.crashing.org>
9  *
10  * PowerMac12,1
11  * ============
12  *
13  * The algorithm used is the PID control algorithm, used the same way
14  * the published Darwin code does, using the same values that are
15  * present in the Darwin 8.10 snapshot property lists (note however
16  * that none of the code has been re-used, it's a complete
17  * re-implementation
18  *
19  * There is two models using PowerMac12,1. Model 2 is iMac G5 iSight
20  * 17" while Model 3 is iMac G5 20". They do have both the same
21  * controls with a tiny difference. The control-ids of hard-drive-fan
22  * and cpu-fan is swapped.
23  *
24  * Target Correction :
25  *
26  * controls have a target correction calculated as :
27  *
28  * new_min = ((((average_power * slope) >> 16) + offset) >> 16) + min_value
29  * new_value = max(new_value, max(new_min, 0))
30  *
31  * OD Fan control correction.
32  *
33  * # model_id: 2
34  *   offset		: -19563152
35  *   slope		:  1956315
36  *
37  * # model_id: 3
38  *   offset		: -15650652
39  *   slope		:  1565065
40  *
41  * HD Fan control correction.
42  *
43  * # model_id: 2
44  *   offset		: -15650652
45  *   slope		:  1565065
46  *
47  * # model_id: 3
48  *   offset		: -19563152
49  *   slope		:  1956315
50  *
51  * CPU Fan control correction.
52  *
53  * # model_id: 2
54  *   offset		: -25431900
55  *   slope		:  2543190
56  *
57  * # model_id: 3
58  *   offset		: -15650652
59  *   slope		:  1565065
60  *
61  * Target rubber-banding :
62  *
63  * Some controls have a target correction which depends on another
64  * control value. The correction is computed in the following way :
65  *
66  * new_min = ref_value * slope + offset
67  *
68  * ref_value is the value of the reference control. If new_min is
69  * greater than 0, then we correct the target value using :
70  *
71  * new_target = max (new_target, new_min >> 16)
72  *
73  * # model_id : 2
74  *   control	: cpu-fan
75  *   ref	: optical-drive-fan
76  *   offset	: -15650652
77  *   slope	: 1565065
78  *
79  * # model_id : 3
80  *   control	: optical-drive-fan
81  *   ref	: hard-drive-fan
82  *   offset	: -32768000
83  *   slope	: 65536
84  *
85  * In order to have the moste efficient correction with those
86  * dependencies, we must trigger HD loop before OD loop before CPU
87  * loop.
88  *
89  * The various control loops found in Darwin config file are:
90  *
91  * HD Fan control loop.
92  *
93  * # model_id: 2
94  *   control        : hard-drive-fan
95  *   sensor         : hard-drive-temp
96  *   PID params     : G_d = 0x00000000
97  *                    G_p = 0x002D70A3
98  *                    G_r = 0x00019999
99  *                    History = 2 entries
100  *                    Input target = 0x370000
101  *                    Interval = 5s
102  *
103  * # model_id: 3
104  *   control        : hard-drive-fan
105  *   sensor         : hard-drive-temp
106  *   PID params     : G_d = 0x00000000
107  *                    G_p = 0x002170A3
108  *                    G_r = 0x00019999
109  *                    History = 2 entries
110  *                    Input target = 0x370000
111  *                    Interval = 5s
112  *
113  * OD Fan control loop.
114  *
115  * # model_id: 2
116  *   control        : optical-drive-fan
117  *   sensor         : optical-drive-temp
118  *   PID params     : G_d = 0x00000000
119  *                    G_p = 0x001FAE14
120  *                    G_r = 0x00019999
121  *                    History = 2 entries
122  *                    Input target = 0x320000
123  *                    Interval = 5s
124  *
125  * # model_id: 3
126  *   control        : optical-drive-fan
127  *   sensor         : optical-drive-temp
128  *   PID params     : G_d = 0x00000000
129  *                    G_p = 0x001FAE14
130  *                    G_r = 0x00019999
131  *                    History = 2 entries
132  *                    Input target = 0x320000
133  *                    Interval = 5s
134  *
135  * GPU Fan control loop.
136  *
137  * # model_id: 2
138  *   control        : hard-drive-fan
139  *   sensor         : gpu-temp
140  *   PID params     : G_d = 0x00000000
141  *                    G_p = 0x002A6666
142  *                    G_r = 0x00019999
143  *                    History = 2 entries
144  *                    Input target = 0x5A0000
145  *                    Interval = 5s
146  *
147  * # model_id: 3
148  *   control        : cpu-fan
149  *   sensor         : gpu-temp
150  *   PID params     : G_d = 0x00000000
151  *                    G_p = 0x0010CCCC
152  *                    G_r = 0x00019999
153  *                    History = 2 entries
154  *                    Input target = 0x500000
155  *                    Interval = 5s
156  *
157  * KODIAK (aka northbridge) Fan control loop.
158  *
159  * # model_id: 2
160  *   control        : optical-drive-fan
161  *   sensor         : north-bridge-temp
162  *   PID params     : G_d = 0x00000000
163  *                    G_p = 0x003BD70A
164  *                    G_r = 0x00019999
165  *                    History = 2 entries
166  *                    Input target = 0x550000
167  *                    Interval = 5s
168  *
169  * # model_id: 3
170  *   control        : hard-drive-fan
171  *   sensor         : north-bridge-temp
172  *   PID params     : G_d = 0x00000000
173  *                    G_p = 0x0030F5C2
174  *                    G_r = 0x00019999
175  *                    History = 2 entries
176  *                    Input target = 0x550000
177  *                    Interval = 5s
178  *
179  * CPU Fan control loop.
180  *
181  *   control        : cpu-fan
182  *   sensors        : cpu-temp, cpu-power
183  *   PID params     : from SDB partition
184  *
185  * CPU Slew control loop.
186  *
187  *   control        : cpufreq-clamp
188  *   sensor         : cpu-temp
189  */
190 
191 #undef	DEBUG
192 
193 #include <linux/types.h>
194 #include <linux/errno.h>
195 #include <linux/kernel.h>
196 #include <linux/delay.h>
197 #include <linux/slab.h>
198 #include <linux/init.h>
199 #include <linux/spinlock.h>
200 #include <linux/wait.h>
201 #include <linux/kmod.h>
202 #include <linux/device.h>
203 #include <linux/platform_device.h>
204 #include <linux/of.h>
205 
206 #include <asm/machdep.h>
207 #include <asm/io.h>
208 #include <asm/sections.h>
209 #include <asm/smu.h>
210 
211 #include "windfarm.h"
212 #include "windfarm_pid.h"
213 
214 #define VERSION "0.3"
215 
216 static int pm121_mach_model;	/* machine model id */
217 
218 /* Controls & sensors */
219 static struct wf_sensor	*sensor_cpu_power;
220 static struct wf_sensor	*sensor_cpu_temp;
221 static struct wf_sensor	*sensor_cpu_voltage;
222 static struct wf_sensor	*sensor_cpu_current;
223 static struct wf_sensor	*sensor_gpu_temp;
224 static struct wf_sensor	*sensor_north_bridge_temp;
225 static struct wf_sensor	*sensor_hard_drive_temp;
226 static struct wf_sensor	*sensor_optical_drive_temp;
227 static struct wf_sensor	*sensor_incoming_air_temp; /* unused ! */
228 
229 enum {
230 	FAN_CPU,
231 	FAN_HD,
232 	FAN_OD,
233 	CPUFREQ,
234 	N_CONTROLS
235 };
236 static struct wf_control *controls[N_CONTROLS] = {};
237 
238 /* Set to kick the control loop into life */
239 static int pm121_all_controls_ok, pm121_all_sensors_ok;
240 static bool pm121_started;
241 
242 enum {
243 	FAILURE_FAN		= 1 << 0,
244 	FAILURE_SENSOR		= 1 << 1,
245 	FAILURE_OVERTEMP	= 1 << 2
246 };
247 
248 /* All sys loops. Note the HD before the OD loop in order to have it
249    run before. */
250 enum {
251 	LOOP_GPU,		/* control = hd or cpu, but luckily,
252 				   it doesn't matter */
253 	LOOP_HD,		/* control = hd */
254 	LOOP_KODIAK,		/* control = hd or od */
255 	LOOP_OD,		/* control = od */
256 	N_LOOPS
257 };
258 
259 static const char *loop_names[N_LOOPS] = {
260 	"GPU",
261 	"HD",
262 	"KODIAK",
263 	"OD",
264 };
265 
266 #define	PM121_NUM_CONFIGS	2
267 
268 static unsigned int pm121_failure_state;
269 static int pm121_readjust, pm121_skipping;
270 static bool pm121_overtemp;
271 static s32 average_power;
272 
273 struct pm121_correction {
274 	int	offset;
275 	int	slope;
276 };
277 
278 static struct pm121_correction corrections[N_CONTROLS][PM121_NUM_CONFIGS] = {
279 	/* FAN_OD */
280 	{
281 		/* MODEL 2 */
282 		{ .offset	= -19563152,
283 		  .slope	=  1956315
284 		},
285 		/* MODEL 3 */
286 		{ .offset	= -15650652,
287 		  .slope	=  1565065
288 		},
289 	},
290 	/* FAN_HD */
291 	{
292 		/* MODEL 2 */
293 		{ .offset	= -15650652,
294 		  .slope	=  1565065
295 		},
296 		/* MODEL 3 */
297 		{ .offset	= -19563152,
298 		  .slope	=  1956315
299 		},
300 	},
301 	/* FAN_CPU */
302 	{
303 		/* MODEL 2 */
304 		{ .offset	= -25431900,
305 		  .slope	=  2543190
306 		},
307 		/* MODEL 3 */
308 		{ .offset	= -15650652,
309 		  .slope	=  1565065
310 		},
311 	},
312 	/* CPUFREQ has no correction (and is not implemented at all) */
313 };
314 
315 struct pm121_connection {
316 	unsigned int	control_id;
317 	unsigned int	ref_id;
318 	struct pm121_correction	correction;
319 };
320 
321 static struct pm121_connection pm121_connections[] = {
322 	/* MODEL 2 */
323 	{ .control_id	= FAN_CPU,
324 	  .ref_id	= FAN_OD,
325 	  { .offset	= -32768000,
326 	    .slope	=  65536
327 	  }
328 	},
329 	/* MODEL 3 */
330 	{ .control_id	= FAN_OD,
331 	  .ref_id	= FAN_HD,
332 	  { .offset	= -32768000,
333 	    .slope	=  65536
334 	  }
335 	},
336 };
337 
338 /* pointer to the current model connection */
339 static struct pm121_connection *pm121_connection;
340 
341 /*
342  * ****** System Fans Control Loop ******
343  *
344  */
345 
346 /* Since each loop handles only one control and we want to avoid
347  * writing virtual control, we store the control correction with the
348  * loop params. Some data are not set, there are common to all loop
349  * and thus, hardcoded.
350  */
351 struct pm121_sys_param {
352 	/* purely informative since we use mach_model-2 as index */
353 	int			model_id;
354 	struct wf_sensor	**sensor; /* use sensor_id instead ? */
355 	s32			gp, itarget;
356 	unsigned int		control_id;
357 };
358 
359 static struct pm121_sys_param
360 pm121_sys_all_params[N_LOOPS][PM121_NUM_CONFIGS] = {
361 	/* GPU Fan control loop */
362 	{
363 		{ .model_id	= 2,
364 		  .sensor	= &sensor_gpu_temp,
365 		  .gp		= 0x002A6666,
366 		  .itarget	= 0x5A0000,
367 		  .control_id	= FAN_HD,
368 		},
369 		{ .model_id	= 3,
370 		  .sensor	= &sensor_gpu_temp,
371 		  .gp		= 0x0010CCCC,
372 		  .itarget	= 0x500000,
373 		  .control_id	= FAN_CPU,
374 		},
375 	},
376 	/* HD Fan control loop */
377 	{
378 		{ .model_id	= 2,
379 		  .sensor	= &sensor_hard_drive_temp,
380 		  .gp		= 0x002D70A3,
381 		  .itarget	= 0x370000,
382 		  .control_id	= FAN_HD,
383 		},
384 		{ .model_id	= 3,
385 		  .sensor	= &sensor_hard_drive_temp,
386 		  .gp		= 0x002170A3,
387 		  .itarget	= 0x370000,
388 		  .control_id	= FAN_HD,
389 		},
390 	},
391 	/* KODIAK Fan control loop */
392 	{
393 		{ .model_id	= 2,
394 		  .sensor	= &sensor_north_bridge_temp,
395 		  .gp		= 0x003BD70A,
396 		  .itarget	= 0x550000,
397 		  .control_id	= FAN_OD,
398 		},
399 		{ .model_id	= 3,
400 		  .sensor	= &sensor_north_bridge_temp,
401 		  .gp		= 0x0030F5C2,
402 		  .itarget	= 0x550000,
403 		  .control_id	= FAN_HD,
404 		},
405 	},
406 	/* OD Fan control loop */
407 	{
408 		{ .model_id	= 2,
409 		  .sensor	= &sensor_optical_drive_temp,
410 		  .gp		= 0x001FAE14,
411 		  .itarget	= 0x320000,
412 		  .control_id	= FAN_OD,
413 		},
414 		{ .model_id	= 3,
415 		  .sensor	= &sensor_optical_drive_temp,
416 		  .gp		= 0x001FAE14,
417 		  .itarget	= 0x320000,
418 		  .control_id	= FAN_OD,
419 		},
420 	},
421 };
422 
423 /* the hardcoded values */
424 #define	PM121_SYS_GD		0x00000000
425 #define	PM121_SYS_GR		0x00019999
426 #define	PM121_SYS_HISTORY_SIZE	2
427 #define	PM121_SYS_INTERVAL	5
428 
429 /* State data used by the system fans control loop
430  */
431 struct pm121_sys_state {
432 	int			ticks;
433 	s32			setpoint;
434 	struct wf_pid_state	pid;
435 };
436 
437 static struct pm121_sys_state *pm121_sys_state[N_LOOPS] = {};
438 
439 /*
440  * ****** CPU Fans Control Loop ******
441  *
442  */
443 
444 #define PM121_CPU_INTERVAL	1
445 
446 /* State data used by the cpu fans control loop
447  */
448 struct pm121_cpu_state {
449 	int			ticks;
450 	s32			setpoint;
451 	struct wf_cpu_pid_state	pid;
452 };
453 
454 static struct pm121_cpu_state *pm121_cpu_state;
455 
456 
457 
458 /*
459  * ***** Implementation *****
460  *
461  */
462 
463 /* correction the value using the output-low-bound correction algo */
pm121_correct(s32 new_setpoint,unsigned int control_id,s32 min)464 static s32 pm121_correct(s32 new_setpoint,
465 			 unsigned int control_id,
466 			 s32 min)
467 {
468 	s32 new_min;
469 	struct pm121_correction *correction;
470 	correction = &corrections[control_id][pm121_mach_model - 2];
471 
472 	new_min = (average_power * correction->slope) >> 16;
473 	new_min += correction->offset;
474 	new_min = (new_min >> 16) + min;
475 
476 	return max3(new_setpoint, new_min, 0);
477 }
478 
pm121_connect(unsigned int control_id,s32 setpoint)479 static s32 pm121_connect(unsigned int control_id, s32 setpoint)
480 {
481 	s32 new_min, value, new_setpoint;
482 
483 	if (pm121_connection->control_id == control_id) {
484 		controls[control_id]->ops->get_value(controls[control_id],
485 						     &value);
486 		new_min = value * pm121_connection->correction.slope;
487 		new_min += pm121_connection->correction.offset;
488 		if (new_min > 0) {
489 			new_setpoint = max(setpoint, (new_min >> 16));
490 			if (new_setpoint != setpoint) {
491 				pr_debug("pm121: %s depending on %s, "
492 					 "corrected from %d to %d RPM\n",
493 					 controls[control_id]->name,
494 					 controls[pm121_connection->ref_id]->name,
495 					 (int) setpoint, (int) new_setpoint);
496 			}
497 		} else
498 			new_setpoint = setpoint;
499 	}
500 	/* no connection */
501 	else
502 		new_setpoint = setpoint;
503 
504 	return new_setpoint;
505 }
506 
507 /* FAN LOOPS */
pm121_create_sys_fans(int loop_id)508 static void pm121_create_sys_fans(int loop_id)
509 {
510 	struct pm121_sys_param *param = NULL;
511 	struct wf_pid_param pid_param;
512 	struct wf_control *control = NULL;
513 	int i;
514 
515 	/* First, locate the params for this model */
516 	for (i = 0; i < PM121_NUM_CONFIGS; i++) {
517 		if (pm121_sys_all_params[loop_id][i].model_id == pm121_mach_model) {
518 			param = &(pm121_sys_all_params[loop_id][i]);
519 			break;
520 		}
521 	}
522 
523 	/* No params found, put fans to max */
524 	if (param == NULL) {
525 		printk(KERN_WARNING "pm121: %s fan config not found "
526 		       " for this machine model\n",
527 		       loop_names[loop_id]);
528 		goto fail;
529 	}
530 
531 	control = controls[param->control_id];
532 
533 	/* Alloc & initialize state */
534 	pm121_sys_state[loop_id] = kmalloc_obj(struct pm121_sys_state);
535 	if (pm121_sys_state[loop_id] == NULL) {
536 		printk(KERN_WARNING "pm121: Memory allocation error\n");
537 		goto fail;
538 	}
539 	pm121_sys_state[loop_id]->ticks = 1;
540 
541 	/* Fill PID params */
542 	pid_param.gd		= PM121_SYS_GD;
543 	pid_param.gp		= param->gp;
544 	pid_param.gr		= PM121_SYS_GR;
545 	pid_param.interval	= PM121_SYS_INTERVAL;
546 	pid_param.history_len	= PM121_SYS_HISTORY_SIZE;
547 	pid_param.itarget	= param->itarget;
548 	if(control)
549 	{
550 		pid_param.min		= control->ops->get_min(control);
551 		pid_param.max		= control->ops->get_max(control);
552 	} else {
553 		/*
554 		 * This is probably not the right!?
555 		 * Perhaps goto fail  if control == NULL  above?
556 		 */
557 		pid_param.min		= 0;
558 		pid_param.max		= 0;
559 	}
560 
561 	wf_pid_init(&pm121_sys_state[loop_id]->pid, &pid_param);
562 
563 	pr_debug("pm121: %s Fan control loop initialized.\n"
564 		 "       itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
565 		 loop_names[loop_id], FIX32TOPRINT(pid_param.itarget),
566 		 pid_param.min, pid_param.max);
567 	return;
568 
569  fail:
570 	/* note that this is not optimal since another loop may still
571 	   control the same control */
572 	printk(KERN_WARNING "pm121: failed to set up %s loop "
573 	       "setting \"%s\" to max speed.\n",
574 	       loop_names[loop_id], control ? control->name : "uninitialized value");
575 
576 	if (control)
577 		wf_control_set_max(control);
578 }
579 
pm121_sys_fans_tick(int loop_id)580 static void pm121_sys_fans_tick(int loop_id)
581 {
582 	struct pm121_sys_param *param;
583 	struct pm121_sys_state *st;
584 	struct wf_sensor *sensor;
585 	struct wf_control *control;
586 	s32 temp, new_setpoint;
587 	int rc;
588 
589 	param = &(pm121_sys_all_params[loop_id][pm121_mach_model-2]);
590 	st = pm121_sys_state[loop_id];
591 	sensor = *(param->sensor);
592 	control = controls[param->control_id];
593 
594 	if (--st->ticks != 0) {
595 		if (pm121_readjust)
596 			goto readjust;
597 		return;
598 	}
599 	st->ticks = PM121_SYS_INTERVAL;
600 
601 	rc = sensor->ops->get_value(sensor, &temp);
602 	if (rc) {
603 		printk(KERN_WARNING "windfarm: %s sensor error %d\n",
604 		       sensor->name, rc);
605 		pm121_failure_state |= FAILURE_SENSOR;
606 		return;
607 	}
608 
609 	pr_debug("pm121: %s Fan tick ! %s: %d.%03d\n",
610 		 loop_names[loop_id], sensor->name,
611 		 FIX32TOPRINT(temp));
612 
613 	new_setpoint = wf_pid_run(&st->pid, temp);
614 
615 	/* correction */
616 	new_setpoint = pm121_correct(new_setpoint,
617 				     param->control_id,
618 				     st->pid.param.min);
619 	/* linked corretion */
620 	new_setpoint = pm121_connect(param->control_id, new_setpoint);
621 
622 	if (new_setpoint == st->setpoint)
623 		return;
624 	st->setpoint = new_setpoint;
625 	pr_debug("pm121: %s corrected setpoint: %d RPM\n",
626 		 control->name, (int)new_setpoint);
627  readjust:
628 	if (control && pm121_failure_state == 0) {
629 		rc = control->ops->set_value(control, st->setpoint);
630 		if (rc) {
631 			printk(KERN_WARNING "windfarm: %s fan error %d\n",
632 			       control->name, rc);
633 			pm121_failure_state |= FAILURE_FAN;
634 		}
635 	}
636 }
637 
638 
639 /* CPU LOOP */
pm121_create_cpu_fans(void)640 static void pm121_create_cpu_fans(void)
641 {
642 	struct wf_cpu_pid_param pid_param;
643 	const struct smu_sdbp_header *hdr;
644 	struct smu_sdbp_cpupiddata *piddata;
645 	struct smu_sdbp_fvt *fvt;
646 	struct wf_control *fan_cpu;
647 	s32 tmax, tdelta, maxpow, powadj;
648 
649 	fan_cpu = controls[FAN_CPU];
650 
651 	/* First, locate the PID params in SMU SBD */
652 	hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
653 	if (!hdr) {
654 		printk(KERN_WARNING "pm121: CPU PID fan config not found.\n");
655 		goto fail;
656 	}
657 	piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
658 
659 	/* Get the FVT params for operating point 0 (the only supported one
660 	 * for now) in order to get tmax
661 	 */
662 	hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
663 	if (hdr) {
664 		fvt = (struct smu_sdbp_fvt *)&hdr[1];
665 		tmax = ((s32)fvt->maxtemp) << 16;
666 	} else
667 		tmax = 0x5e0000; /* 94 degree default */
668 
669 	/* Alloc & initialize state */
670 	pm121_cpu_state = kmalloc_obj(struct pm121_cpu_state);
671 	if (pm121_cpu_state == NULL)
672 		goto fail;
673 	pm121_cpu_state->ticks = 1;
674 
675 	/* Fill PID params */
676 	pid_param.interval = PM121_CPU_INTERVAL;
677 	pid_param.history_len = piddata->history_len;
678 	if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
679 		printk(KERN_WARNING "pm121: History size overflow on "
680 		       "CPU control loop (%d)\n", piddata->history_len);
681 		pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
682 	}
683 	pid_param.gd = piddata->gd;
684 	pid_param.gp = piddata->gp;
685 	pid_param.gr = piddata->gr / pid_param.history_len;
686 
687 	tdelta = ((s32)piddata->target_temp_delta) << 16;
688 	maxpow = ((s32)piddata->max_power) << 16;
689 	powadj = ((s32)piddata->power_adj) << 16;
690 
691 	pid_param.tmax = tmax;
692 	pid_param.ttarget = tmax - tdelta;
693 	pid_param.pmaxadj = maxpow - powadj;
694 
695 	pid_param.min = fan_cpu->ops->get_min(fan_cpu);
696 	pid_param.max = fan_cpu->ops->get_max(fan_cpu);
697 
698 	wf_cpu_pid_init(&pm121_cpu_state->pid, &pid_param);
699 
700 	pr_debug("pm121: CPU Fan control initialized.\n");
701 	pr_debug("       ttarget=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM,\n",
702 		 FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
703 		 pid_param.min, pid_param.max);
704 
705 	return;
706 
707  fail:
708 	printk(KERN_WARNING "pm121: CPU fan config not found, max fan speed\n");
709 
710 	if (controls[CPUFREQ])
711 		wf_control_set_max(controls[CPUFREQ]);
712 	if (fan_cpu)
713 		wf_control_set_max(fan_cpu);
714 }
715 
716 
pm121_cpu_fans_tick(struct pm121_cpu_state * st)717 static void pm121_cpu_fans_tick(struct pm121_cpu_state *st)
718 {
719 	s32 new_setpoint, temp, power;
720 	struct wf_control *fan_cpu = NULL;
721 	int rc;
722 
723 	if (--st->ticks != 0) {
724 		if (pm121_readjust)
725 			goto readjust;
726 		return;
727 	}
728 	st->ticks = PM121_CPU_INTERVAL;
729 
730 	fan_cpu = controls[FAN_CPU];
731 
732 	rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);
733 	if (rc) {
734 		printk(KERN_WARNING "pm121: CPU temp sensor error %d\n",
735 		       rc);
736 		pm121_failure_state |= FAILURE_SENSOR;
737 		return;
738 	}
739 
740 	rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);
741 	if (rc) {
742 		printk(KERN_WARNING "pm121: CPU power sensor error %d\n",
743 		       rc);
744 		pm121_failure_state |= FAILURE_SENSOR;
745 		return;
746 	}
747 
748 	pr_debug("pm121: CPU Fans tick ! CPU temp: %d.%03d°C, power: %d.%03d\n",
749 		 FIX32TOPRINT(temp), FIX32TOPRINT(power));
750 
751 	if (temp > st->pid.param.tmax)
752 		pm121_failure_state |= FAILURE_OVERTEMP;
753 
754 	new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
755 
756 	/* correction */
757 	new_setpoint = pm121_correct(new_setpoint,
758 				     FAN_CPU,
759 				     st->pid.param.min);
760 
761 	/* connected correction */
762 	new_setpoint = pm121_connect(FAN_CPU, new_setpoint);
763 
764 	if (st->setpoint == new_setpoint)
765 		return;
766 	st->setpoint = new_setpoint;
767 	pr_debug("pm121: CPU corrected setpoint: %d RPM\n", (int)new_setpoint);
768 
769  readjust:
770 	if (fan_cpu && pm121_failure_state == 0) {
771 		rc = fan_cpu->ops->set_value(fan_cpu, st->setpoint);
772 		if (rc) {
773 			printk(KERN_WARNING "pm121: %s fan error %d\n",
774 			       fan_cpu->name, rc);
775 			pm121_failure_state |= FAILURE_FAN;
776 		}
777 	}
778 }
779 
780 /*
781  * ****** Common ******
782  *
783  */
784 
pm121_tick(void)785 static void pm121_tick(void)
786 {
787 	unsigned int last_failure = pm121_failure_state;
788 	unsigned int new_failure;
789 	s32 total_power;
790 	int i;
791 
792 	if (!pm121_started) {
793 		pr_debug("pm121: creating control loops !\n");
794 		for (i = 0; i < N_LOOPS; i++)
795 			pm121_create_sys_fans(i);
796 
797 		pm121_create_cpu_fans();
798 		pm121_started = true;
799 	}
800 
801 	/* skipping ticks */
802 	if (pm121_skipping && --pm121_skipping)
803 		return;
804 
805 	/* compute average power */
806 	total_power = 0;
807 	for (i = 0; i < pm121_cpu_state->pid.param.history_len; i++)
808 		total_power += pm121_cpu_state->pid.powers[i];
809 
810 	average_power = total_power / pm121_cpu_state->pid.param.history_len;
811 
812 
813 	pm121_failure_state = 0;
814 	for (i = 0 ; i < N_LOOPS; i++) {
815 		if (pm121_sys_state[i])
816 			pm121_sys_fans_tick(i);
817 	}
818 
819 	if (pm121_cpu_state)
820 		pm121_cpu_fans_tick(pm121_cpu_state);
821 
822 	pm121_readjust = 0;
823 	new_failure = pm121_failure_state & ~last_failure;
824 
825 	/* If entering failure mode, clamp cpufreq and ramp all
826 	 * fans to full speed.
827 	 */
828 	if (pm121_failure_state && !last_failure) {
829 		for (i = 0; i < N_CONTROLS; i++) {
830 			if (controls[i])
831 				wf_control_set_max(controls[i]);
832 		}
833 	}
834 
835 	/* If leaving failure mode, unclamp cpufreq and readjust
836 	 * all fans on next iteration
837 	 */
838 	if (!pm121_failure_state && last_failure) {
839 		if (controls[CPUFREQ])
840 			wf_control_set_min(controls[CPUFREQ]);
841 		pm121_readjust = 1;
842 	}
843 
844 	/* Overtemp condition detected, notify and start skipping a couple
845 	 * ticks to let the temperature go down
846 	 */
847 	if (new_failure & FAILURE_OVERTEMP) {
848 		wf_set_overtemp();
849 		pm121_skipping = 2;
850 		pm121_overtemp = true;
851 	}
852 
853 	/* We only clear the overtemp condition if overtemp is cleared
854 	 * _and_ no other failure is present. Since a sensor error will
855 	 * clear the overtemp condition (can't measure temperature) at
856 	 * the control loop levels, but we don't want to keep it clear
857 	 * here in this case
858 	 */
859 	if (!pm121_failure_state && pm121_overtemp) {
860 		wf_clear_overtemp();
861 		pm121_overtemp = false;
862 	}
863 }
864 
865 
pm121_register_control(struct wf_control * ct,const char * match,unsigned int id)866 static struct wf_control* pm121_register_control(struct wf_control *ct,
867 						 const char *match,
868 						 unsigned int id)
869 {
870 	if (controls[id] == NULL && !strcmp(ct->name, match)) {
871 		if (wf_get_control(ct) == 0)
872 			controls[id] = ct;
873 	}
874 	return controls[id];
875 }
876 
pm121_new_control(struct wf_control * ct)877 static void pm121_new_control(struct wf_control *ct)
878 {
879 	int all = 1;
880 
881 	if (pm121_all_controls_ok)
882 		return;
883 
884 	all = pm121_register_control(ct, "optical-drive-fan", FAN_OD) && all;
885 	all = pm121_register_control(ct, "hard-drive-fan", FAN_HD) && all;
886 	all = pm121_register_control(ct, "cpu-fan", FAN_CPU) && all;
887 	all = pm121_register_control(ct, "cpufreq-clamp", CPUFREQ) && all;
888 
889 	if (all)
890 		pm121_all_controls_ok = 1;
891 }
892 
893 
894 
895 
pm121_register_sensor(struct wf_sensor * sensor,const char * match,struct wf_sensor ** var)896 static struct wf_sensor* pm121_register_sensor(struct wf_sensor *sensor,
897 					       const char *match,
898 					       struct wf_sensor **var)
899 {
900 	if (*var == NULL && !strcmp(sensor->name, match)) {
901 		if (wf_get_sensor(sensor) == 0)
902 			*var = sensor;
903 	}
904 	return *var;
905 }
906 
pm121_new_sensor(struct wf_sensor * sr)907 static void pm121_new_sensor(struct wf_sensor *sr)
908 {
909 	int all = 1;
910 
911 	if (pm121_all_sensors_ok)
912 		return;
913 
914 	all = pm121_register_sensor(sr, "cpu-temp",
915 				    &sensor_cpu_temp) && all;
916 	all = pm121_register_sensor(sr, "cpu-current",
917 				    &sensor_cpu_current) && all;
918 	all = pm121_register_sensor(sr, "cpu-voltage",
919 				    &sensor_cpu_voltage) && all;
920 	all = pm121_register_sensor(sr, "cpu-power",
921 				    &sensor_cpu_power) && all;
922 	all = pm121_register_sensor(sr, "hard-drive-temp",
923 				    &sensor_hard_drive_temp) && all;
924 	all = pm121_register_sensor(sr, "optical-drive-temp",
925 				    &sensor_optical_drive_temp) && all;
926 	all = pm121_register_sensor(sr, "incoming-air-temp",
927 				    &sensor_incoming_air_temp) && all;
928 	all = pm121_register_sensor(sr, "north-bridge-temp",
929 				    &sensor_north_bridge_temp) && all;
930 	all = pm121_register_sensor(sr, "gpu-temp",
931 				    &sensor_gpu_temp) && all;
932 
933 	if (all)
934 		pm121_all_sensors_ok = 1;
935 }
936 
937 
938 
pm121_notify(struct notifier_block * self,unsigned long event,void * data)939 static int pm121_notify(struct notifier_block *self,
940 			unsigned long event, void *data)
941 {
942 	switch (event) {
943 	case WF_EVENT_NEW_CONTROL:
944 		pr_debug("pm121: new control %s detected\n",
945 			 ((struct wf_control *)data)->name);
946 		pm121_new_control(data);
947 		break;
948 	case WF_EVENT_NEW_SENSOR:
949 		pr_debug("pm121: new sensor %s detected\n",
950 			 ((struct wf_sensor *)data)->name);
951 		pm121_new_sensor(data);
952 		break;
953 	case WF_EVENT_TICK:
954 		if (pm121_all_controls_ok && pm121_all_sensors_ok)
955 			pm121_tick();
956 		break;
957 	}
958 
959 	return 0;
960 }
961 
962 static struct notifier_block pm121_events = {
963 	.notifier_call	= pm121_notify,
964 };
965 
pm121_init_pm(void)966 static int pm121_init_pm(void)
967 {
968 	const struct smu_sdbp_header *hdr;
969 
970 	hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
971 	if (hdr) {
972 		struct smu_sdbp_sensortree *st =
973 			(struct smu_sdbp_sensortree *)&hdr[1];
974 		pm121_mach_model = st->model_id;
975 	}
976 
977 	pm121_connection = &pm121_connections[pm121_mach_model - 2];
978 
979 	printk(KERN_INFO "pm121: Initializing for iMac G5 iSight model ID %d\n",
980 	       pm121_mach_model);
981 
982 	return 0;
983 }
984 
985 
pm121_probe(struct platform_device * ddev)986 static int pm121_probe(struct platform_device *ddev)
987 {
988 	wf_register_client(&pm121_events);
989 
990 	return 0;
991 }
992 
pm121_remove(struct platform_device * ddev)993 static void pm121_remove(struct platform_device *ddev)
994 {
995 	wf_unregister_client(&pm121_events);
996 }
997 
998 static struct platform_driver pm121_driver = {
999 	.probe = pm121_probe,
1000 	.remove = pm121_remove,
1001 	.driver = {
1002 		.name = "windfarm",
1003 		.bus = &platform_bus_type,
1004 	},
1005 };
1006 
1007 
pm121_init(void)1008 static int __init pm121_init(void)
1009 {
1010 	int rc = -ENODEV;
1011 
1012 	if (of_machine_is_compatible("PowerMac12,1"))
1013 		rc = pm121_init_pm();
1014 
1015 	if (rc == 0) {
1016 		request_module("windfarm_smu_controls");
1017 		request_module("windfarm_smu_sensors");
1018 		request_module("windfarm_smu_sat");
1019 		request_module("windfarm_lm75_sensor");
1020 		request_module("windfarm_max6690_sensor");
1021 		request_module("windfarm_cpufreq_clamp");
1022 		platform_driver_register(&pm121_driver);
1023 	}
1024 
1025 	return rc;
1026 }
1027 
pm121_exit(void)1028 static void __exit pm121_exit(void)
1029 {
1030 
1031 	platform_driver_unregister(&pm121_driver);
1032 }
1033 
1034 
1035 module_init(pm121_init);
1036 module_exit(pm121_exit);
1037 
1038 MODULE_AUTHOR("Étienne Bersac <bersace@gmail.com>");
1039 MODULE_DESCRIPTION("Thermal control logic for iMac G5 (iSight)");
1040 MODULE_LICENSE("GPL");
1041 
1042