hrtimer: create a "timer_slack" field in the task struct
[linux-2.6/mini2440.git] / drivers / macintosh / therm_pm72.c
blob817607e2af6a9c89193cc390ab16d2455dc7a0f7
1 /*
2 * Device driver for the thermostats & fan controller of the
3 * Apple G5 "PowerMac7,2" desktop machines.
5 * (c) Copyright IBM Corp. 2003-2004
7 * Maintained by: Benjamin Herrenschmidt
8 * <benh@kernel.crashing.org>
9 *
11 * The algorithm used is the PID control algorithm, used the same
12 * way the published Darwin code does, using the same values that
13 * are present in the Darwin 7.0 snapshot property lists.
15 * As far as the CPUs control loops are concerned, I use the
16 * calibration & PID constants provided by the EEPROM,
17 * I do _not_ embed any value from the property lists, as the ones
18 * provided by Darwin 7.0 seem to always have an older version that
19 * what I've seen on the actual computers.
20 * It would be interesting to verify that though. Darwin has a
21 * version code of 1.0.0d11 for all control loops it seems, while
22 * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
24 * Darwin doesn't provide source to all parts, some missing
25 * bits like the AppleFCU driver or the actual scale of some
26 * of the values returned by sensors had to be "guessed" some
27 * way... or based on what Open Firmware does.
29 * I didn't yet figure out how to get the slots power consumption
30 * out of the FCU, so that part has not been implemented yet and
31 * the slots fan is set to a fixed 50% PWM, hoping this value is
32 * safe enough ...
34 * Note: I have observed strange oscillations of the CPU control
35 * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
36 * oscillates slowly (over several minutes) between the minimum
37 * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
38 * this, it could be some incorrect constant or an error in the
39 * way I ported the algorithm, or it could be just normal. I
40 * don't have full understanding on the way Apple tweaked the PID
41 * algorithm for the CPU control, it is definitely not a standard
42 * implementation...
44 * TODO: - Check MPU structure version/signature
45 * - Add things like /sbin/overtemp for non-critical
46 * overtemp conditions so userland can take some policy
47 * decisions, like slewing down CPUs
48 * - Deal with fan and i2c failures in a better way
49 * - Maybe do a generic PID based on params used for
50 * U3 and Drives ? Definitely need to factor code a bit
51 * bettter... also make sensor detection more robust using
52 * the device-tree to probe for them
53 * - Figure out how to get the slots consumption and set the
54 * slots fan accordingly
56 * History:
58 * Nov. 13, 2003 : 0.5
59 * - First release
61 * Nov. 14, 2003 : 0.6
62 * - Read fan speed from FCU, low level fan routines now deal
63 * with errors & check fan status, though higher level don't
64 * do much.
65 * - Move a bunch of definitions to .h file
67 * Nov. 18, 2003 : 0.7
68 * - Fix build on ppc64 kernel
69 * - Move back statics definitions to .c file
70 * - Avoid calling schedule_timeout with a negative number
72 * Dec. 18, 2003 : 0.8
73 * - Fix typo when reading back fan speed on 2 CPU machines
75 * Mar. 11, 2004 : 0.9
76 * - Rework code accessing the ADC chips, make it more robust and
77 * closer to the chip spec. Also make sure it is configured properly,
78 * I've seen yet unexplained cases where on startup, I would have stale
79 * values in the configuration register
80 * - Switch back to use of target fan speed for PID, thus lowering
81 * pressure on i2c
83 * Oct. 20, 2004 : 1.1
84 * - Add device-tree lookup for fan IDs, should detect liquid cooling
85 * pumps when present
86 * - Enable driver for PowerMac7,3 machines
87 * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
88 * - Add new CPU cooling algorithm for machines with liquid cooling
89 * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
90 * - Fix a signed/unsigned compare issue in some PID loops
92 * Mar. 10, 2005 : 1.2
93 * - Add basic support for Xserve G5
94 * - Retreive pumps min/max from EEPROM image in device-tree (broken)
95 * - Use min/max macros here or there
96 * - Latest darwin updated U3H min fan speed to 20% PWM
98 * July. 06, 2006 : 1.3
99 * - Fix setting of RPM fans on Xserve G5 (they were going too fast)
100 * - Add missing slots fan control loop for Xserve G5
101 * - Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
102 * still can't properly implement the control loop for these, so let's
103 * reduce the noise a little bit, it appears that 40% still gives us
104 * a pretty good air flow
105 * - Add code to "tickle" the FCU regulary so it doesn't think that
106 * we are gone while in fact, the machine just didn't need any fan
107 * speed change lately
111 #include <linux/types.h>
112 #include <linux/module.h>
113 #include <linux/errno.h>
114 #include <linux/kernel.h>
115 #include <linux/delay.h>
116 #include <linux/sched.h>
117 #include <linux/slab.h>
118 #include <linux/init.h>
119 #include <linux/spinlock.h>
120 #include <linux/wait.h>
121 #include <linux/reboot.h>
122 #include <linux/kmod.h>
123 #include <linux/i2c.h>
124 #include <linux/kthread.h>
125 #include <linux/mutex.h>
126 #include <linux/of_device.h>
127 #include <linux/of_platform.h>
128 #include <asm/prom.h>
129 #include <asm/machdep.h>
130 #include <asm/io.h>
131 #include <asm/system.h>
132 #include <asm/sections.h>
133 #include <asm/macio.h>
135 #include "therm_pm72.h"
137 #define VERSION "1.3"
139 #undef DEBUG
141 #ifdef DEBUG
142 #define DBG(args...) printk(args)
143 #else
144 #define DBG(args...) do { } while(0)
145 #endif
149 * Driver statics
152 static struct of_device * of_dev;
153 static struct i2c_adapter * u3_0;
154 static struct i2c_adapter * u3_1;
155 static struct i2c_adapter * k2;
156 static struct i2c_client * fcu;
157 static struct cpu_pid_state cpu_state[2];
158 static struct basckside_pid_params backside_params;
159 static struct backside_pid_state backside_state;
160 static struct drives_pid_state drives_state;
161 static struct dimm_pid_state dimms_state;
162 static struct slots_pid_state slots_state;
163 static int state;
164 static int cpu_count;
165 static int cpu_pid_type;
166 static struct task_struct *ctrl_task;
167 static struct completion ctrl_complete;
168 static int critical_state;
169 static int rackmac;
170 static s32 dimm_output_clamp;
171 static int fcu_rpm_shift;
172 static int fcu_tickle_ticks;
173 static DEFINE_MUTEX(driver_lock);
176 * We have 3 types of CPU PID control. One is "split" old style control
177 * for intake & exhaust fans, the other is "combined" control for both
178 * CPUs that also deals with the pumps when present. To be "compatible"
179 * with OS X at this point, we only use "COMBINED" on the machines that
180 * are identified as having the pumps (though that identification is at
181 * least dodgy). Ultimately, we could probably switch completely to this
182 * algorithm provided we hack it to deal with the UP case
184 #define CPU_PID_TYPE_SPLIT 0
185 #define CPU_PID_TYPE_COMBINED 1
186 #define CPU_PID_TYPE_RACKMAC 2
189 * This table describes all fans in the FCU. The "id" and "type" values
190 * are defaults valid for all earlier machines. Newer machines will
191 * eventually override the table content based on the device-tree
193 struct fcu_fan_table
195 char* loc; /* location code */
196 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
197 int id; /* id or -1 */
200 #define FCU_FAN_RPM 0
201 #define FCU_FAN_PWM 1
203 #define FCU_FAN_ABSENT_ID -1
205 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
207 struct fcu_fan_table fcu_fans[] = {
208 [BACKSIDE_FAN_PWM_INDEX] = {
209 .loc = "BACKSIDE,SYS CTRLR FAN",
210 .type = FCU_FAN_PWM,
211 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
213 [DRIVES_FAN_RPM_INDEX] = {
214 .loc = "DRIVE BAY",
215 .type = FCU_FAN_RPM,
216 .id = DRIVES_FAN_RPM_DEFAULT_ID,
218 [SLOTS_FAN_PWM_INDEX] = {
219 .loc = "SLOT,PCI FAN",
220 .type = FCU_FAN_PWM,
221 .id = SLOTS_FAN_PWM_DEFAULT_ID,
223 [CPUA_INTAKE_FAN_RPM_INDEX] = {
224 .loc = "CPU A INTAKE",
225 .type = FCU_FAN_RPM,
226 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
228 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
229 .loc = "CPU A EXHAUST",
230 .type = FCU_FAN_RPM,
231 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
233 [CPUB_INTAKE_FAN_RPM_INDEX] = {
234 .loc = "CPU B INTAKE",
235 .type = FCU_FAN_RPM,
236 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
238 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
239 .loc = "CPU B EXHAUST",
240 .type = FCU_FAN_RPM,
241 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
243 /* pumps aren't present by default, have to be looked up in the
244 * device-tree
246 [CPUA_PUMP_RPM_INDEX] = {
247 .loc = "CPU A PUMP",
248 .type = FCU_FAN_RPM,
249 .id = FCU_FAN_ABSENT_ID,
251 [CPUB_PUMP_RPM_INDEX] = {
252 .loc = "CPU B PUMP",
253 .type = FCU_FAN_RPM,
254 .id = FCU_FAN_ABSENT_ID,
256 /* Xserve fans */
257 [CPU_A1_FAN_RPM_INDEX] = {
258 .loc = "CPU A 1",
259 .type = FCU_FAN_RPM,
260 .id = FCU_FAN_ABSENT_ID,
262 [CPU_A2_FAN_RPM_INDEX] = {
263 .loc = "CPU A 2",
264 .type = FCU_FAN_RPM,
265 .id = FCU_FAN_ABSENT_ID,
267 [CPU_A3_FAN_RPM_INDEX] = {
268 .loc = "CPU A 3",
269 .type = FCU_FAN_RPM,
270 .id = FCU_FAN_ABSENT_ID,
272 [CPU_B1_FAN_RPM_INDEX] = {
273 .loc = "CPU B 1",
274 .type = FCU_FAN_RPM,
275 .id = FCU_FAN_ABSENT_ID,
277 [CPU_B2_FAN_RPM_INDEX] = {
278 .loc = "CPU B 2",
279 .type = FCU_FAN_RPM,
280 .id = FCU_FAN_ABSENT_ID,
282 [CPU_B3_FAN_RPM_INDEX] = {
283 .loc = "CPU B 3",
284 .type = FCU_FAN_RPM,
285 .id = FCU_FAN_ABSENT_ID,
290 * i2c_driver structure to attach to the host i2c controller
293 static int therm_pm72_attach(struct i2c_adapter *adapter);
294 static int therm_pm72_detach(struct i2c_adapter *adapter);
296 static struct i2c_driver therm_pm72_driver =
298 .driver = {
299 .name = "therm_pm72",
301 .attach_adapter = therm_pm72_attach,
302 .detach_adapter = therm_pm72_detach,
306 * Utility function to create an i2c_client structure and
307 * attach it to one of u3 adapters
309 static struct i2c_client *attach_i2c_chip(int id, const char *name)
311 struct i2c_client *clt;
312 struct i2c_adapter *adap;
314 if (id & 0x200)
315 adap = k2;
316 else if (id & 0x100)
317 adap = u3_1;
318 else
319 adap = u3_0;
320 if (adap == NULL)
321 return NULL;
323 clt = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
324 if (clt == NULL)
325 return NULL;
327 clt->addr = (id >> 1) & 0x7f;
328 clt->adapter = adap;
329 clt->driver = &therm_pm72_driver;
330 strncpy(clt->name, name, I2C_NAME_SIZE-1);
332 if (i2c_attach_client(clt)) {
333 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
334 kfree(clt);
335 return NULL;
337 return clt;
341 * Utility function to get rid of the i2c_client structure
342 * (will also detach from the adapter hopepfully)
344 static void detach_i2c_chip(struct i2c_client *clt)
346 i2c_detach_client(clt);
347 kfree(clt);
351 * Here are the i2c chip access wrappers
354 static void initialize_adc(struct cpu_pid_state *state)
356 int rc;
357 u8 buf[2];
359 /* Read ADC the configuration register and cache it. We
360 * also make sure Config2 contains proper values, I've seen
361 * cases where we got stale grabage in there, thus preventing
362 * proper reading of conv. values
365 /* Clear Config2 */
366 buf[0] = 5;
367 buf[1] = 0;
368 i2c_master_send(state->monitor, buf, 2);
370 /* Read & cache Config1 */
371 buf[0] = 1;
372 rc = i2c_master_send(state->monitor, buf, 1);
373 if (rc > 0) {
374 rc = i2c_master_recv(state->monitor, buf, 1);
375 if (rc > 0) {
376 state->adc_config = buf[0];
377 DBG("ADC config reg: %02x\n", state->adc_config);
378 /* Disable shutdown mode */
379 state->adc_config &= 0xfe;
380 buf[0] = 1;
381 buf[1] = state->adc_config;
382 rc = i2c_master_send(state->monitor, buf, 2);
385 if (rc <= 0)
386 printk(KERN_ERR "therm_pm72: Error reading ADC config"
387 " register !\n");
390 static int read_smon_adc(struct cpu_pid_state *state, int chan)
392 int rc, data, tries = 0;
393 u8 buf[2];
395 for (;;) {
396 /* Set channel */
397 buf[0] = 1;
398 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
399 rc = i2c_master_send(state->monitor, buf, 2);
400 if (rc <= 0)
401 goto error;
402 /* Wait for convertion */
403 msleep(1);
404 /* Switch to data register */
405 buf[0] = 4;
406 rc = i2c_master_send(state->monitor, buf, 1);
407 if (rc <= 0)
408 goto error;
409 /* Read result */
410 rc = i2c_master_recv(state->monitor, buf, 2);
411 if (rc < 0)
412 goto error;
413 data = ((u16)buf[0]) << 8 | (u16)buf[1];
414 return data >> 6;
415 error:
416 DBG("Error reading ADC, retrying...\n");
417 if (++tries > 10) {
418 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
419 return -1;
421 msleep(10);
425 static int read_lm87_reg(struct i2c_client * chip, int reg)
427 int rc, tries = 0;
428 u8 buf;
430 for (;;) {
431 /* Set address */
432 buf = (u8)reg;
433 rc = i2c_master_send(chip, &buf, 1);
434 if (rc <= 0)
435 goto error;
436 rc = i2c_master_recv(chip, &buf, 1);
437 if (rc <= 0)
438 goto error;
439 return (int)buf;
440 error:
441 DBG("Error reading LM87, retrying...\n");
442 if (++tries > 10) {
443 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
444 return -1;
446 msleep(10);
450 static int fan_read_reg(int reg, unsigned char *buf, int nb)
452 int tries, nr, nw;
454 buf[0] = reg;
455 tries = 0;
456 for (;;) {
457 nw = i2c_master_send(fcu, buf, 1);
458 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
459 break;
460 msleep(10);
461 ++tries;
463 if (nw <= 0) {
464 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
465 return -EIO;
467 tries = 0;
468 for (;;) {
469 nr = i2c_master_recv(fcu, buf, nb);
470 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
471 break;
472 msleep(10);
473 ++tries;
475 if (nr <= 0)
476 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
477 return nr;
480 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
482 int tries, nw;
483 unsigned char buf[16];
485 buf[0] = reg;
486 memcpy(buf+1, ptr, nb);
487 ++nb;
488 tries = 0;
489 for (;;) {
490 nw = i2c_master_send(fcu, buf, nb);
491 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
492 break;
493 msleep(10);
494 ++tries;
496 if (nw < 0)
497 printk(KERN_ERR "Failure writing to FCU: %d", nw);
498 return nw;
501 static int start_fcu(void)
503 unsigned char buf = 0xff;
504 int rc;
506 rc = fan_write_reg(0xe, &buf, 1);
507 if (rc < 0)
508 return -EIO;
509 rc = fan_write_reg(0x2e, &buf, 1);
510 if (rc < 0)
511 return -EIO;
512 rc = fan_read_reg(0, &buf, 1);
513 if (rc < 0)
514 return -EIO;
515 fcu_rpm_shift = (buf == 1) ? 2 : 3;
516 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
517 fcu_rpm_shift);
519 return 0;
522 static int set_rpm_fan(int fan_index, int rpm)
524 unsigned char buf[2];
525 int rc, id, min, max;
527 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
528 return -EINVAL;
529 id = fcu_fans[fan_index].id;
530 if (id == FCU_FAN_ABSENT_ID)
531 return -EINVAL;
533 min = 2400 >> fcu_rpm_shift;
534 max = 56000 >> fcu_rpm_shift;
536 if (rpm < min)
537 rpm = min;
538 else if (rpm > max)
539 rpm = max;
540 buf[0] = rpm >> (8 - fcu_rpm_shift);
541 buf[1] = rpm << fcu_rpm_shift;
542 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
543 if (rc < 0)
544 return -EIO;
545 return 0;
548 static int get_rpm_fan(int fan_index, int programmed)
550 unsigned char failure;
551 unsigned char active;
552 unsigned char buf[2];
553 int rc, id, reg_base;
555 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
556 return -EINVAL;
557 id = fcu_fans[fan_index].id;
558 if (id == FCU_FAN_ABSENT_ID)
559 return -EINVAL;
561 rc = fan_read_reg(0xb, &failure, 1);
562 if (rc != 1)
563 return -EIO;
564 if ((failure & (1 << id)) != 0)
565 return -EFAULT;
566 rc = fan_read_reg(0xd, &active, 1);
567 if (rc != 1)
568 return -EIO;
569 if ((active & (1 << id)) == 0)
570 return -ENXIO;
572 /* Programmed value or real current speed */
573 reg_base = programmed ? 0x10 : 0x11;
574 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
575 if (rc != 2)
576 return -EIO;
578 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
581 static int set_pwm_fan(int fan_index, int pwm)
583 unsigned char buf[2];
584 int rc, id;
586 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
587 return -EINVAL;
588 id = fcu_fans[fan_index].id;
589 if (id == FCU_FAN_ABSENT_ID)
590 return -EINVAL;
592 if (pwm < 10)
593 pwm = 10;
594 else if (pwm > 100)
595 pwm = 100;
596 pwm = (pwm * 2559) / 1000;
597 buf[0] = pwm;
598 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
599 if (rc < 0)
600 return rc;
601 return 0;
604 static int get_pwm_fan(int fan_index)
606 unsigned char failure;
607 unsigned char active;
608 unsigned char buf[2];
609 int rc, id;
611 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
612 return -EINVAL;
613 id = fcu_fans[fan_index].id;
614 if (id == FCU_FAN_ABSENT_ID)
615 return -EINVAL;
617 rc = fan_read_reg(0x2b, &failure, 1);
618 if (rc != 1)
619 return -EIO;
620 if ((failure & (1 << id)) != 0)
621 return -EFAULT;
622 rc = fan_read_reg(0x2d, &active, 1);
623 if (rc != 1)
624 return -EIO;
625 if ((active & (1 << id)) == 0)
626 return -ENXIO;
628 /* Programmed value or real current speed */
629 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
630 if (rc != 1)
631 return -EIO;
633 return (buf[0] * 1000) / 2559;
636 static void tickle_fcu(void)
638 int pwm;
640 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
642 DBG("FCU Tickle, slots fan is: %d\n", pwm);
643 if (pwm < 0)
644 pwm = 100;
646 if (!rackmac) {
647 pwm = SLOTS_FAN_DEFAULT_PWM;
648 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
649 pwm = SLOTS_PID_OUTPUT_MIN;
651 /* That is hopefully enough to make the FCU happy */
652 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
657 * Utility routine to read the CPU calibration EEPROM data
658 * from the device-tree
660 static int read_eeprom(int cpu, struct mpu_data *out)
662 struct device_node *np;
663 char nodename[64];
664 const u8 *data;
665 int len;
667 /* prom.c routine for finding a node by path is a bit brain dead
668 * and requires exact @xxx unit numbers. This is a bit ugly but
669 * will work for these machines
671 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
672 np = of_find_node_by_path(nodename);
673 if (np == NULL) {
674 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
675 return -ENODEV;
677 data = of_get_property(np, "cpuid", &len);
678 if (data == NULL) {
679 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
680 of_node_put(np);
681 return -ENODEV;
683 memcpy(out, data, sizeof(struct mpu_data));
684 of_node_put(np);
686 return 0;
689 static void fetch_cpu_pumps_minmax(void)
691 struct cpu_pid_state *state0 = &cpu_state[0];
692 struct cpu_pid_state *state1 = &cpu_state[1];
693 u16 pump_min = 0, pump_max = 0xffff;
694 u16 tmp[4];
696 /* Try to fetch pumps min/max infos from eeprom */
698 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
699 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
700 pump_min = max(pump_min, tmp[0]);
701 pump_max = min(pump_max, tmp[1]);
703 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
704 pump_min = max(pump_min, tmp[2]);
705 pump_max = min(pump_max, tmp[3]);
708 /* Double check the values, this _IS_ needed as the EEPROM on
709 * some dual 2.5Ghz G5s seem, at least, to have both min & max
710 * same to the same value ... (grrrr)
712 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
713 pump_min = CPU_PUMP_OUTPUT_MIN;
714 pump_max = CPU_PUMP_OUTPUT_MAX;
717 state0->pump_min = state1->pump_min = pump_min;
718 state0->pump_max = state1->pump_max = pump_max;
722 * Now, unfortunately, sysfs doesn't give us a nice void * we could
723 * pass around to the attribute functions, so we don't really have
724 * choice but implement a bunch of them...
726 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
727 * the input twice... I accept patches :)
729 #define BUILD_SHOW_FUNC_FIX(name, data) \
730 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
732 ssize_t r; \
733 mutex_lock(&driver_lock); \
734 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
735 mutex_unlock(&driver_lock); \
736 return r; \
738 #define BUILD_SHOW_FUNC_INT(name, data) \
739 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
741 return sprintf(buf, "%d", data); \
744 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
745 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
746 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
747 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
748 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
750 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
751 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
752 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
753 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
754 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
756 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
757 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
759 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
760 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
762 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
763 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
765 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
767 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
768 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
769 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
770 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
771 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
773 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
774 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
775 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
776 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
777 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
779 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
780 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
782 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
783 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
785 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
786 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
788 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
791 * CPUs fans control loop
794 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
796 s32 ltemp, volts, amps;
797 int index, rc = 0;
799 /* Default (in case of error) */
800 *temp = state->cur_temp;
801 *power = state->cur_power;
803 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
804 index = (state->index == 0) ?
805 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
806 else
807 index = (state->index == 0) ?
808 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
810 /* Read current fan status */
811 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
812 if (rc < 0) {
813 /* XXX What do we do now ? Nothing for now, keep old value, but
814 * return error upstream
816 DBG(" cpu %d, fan reading error !\n", state->index);
817 } else {
818 state->rpm = rc;
819 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
822 /* Get some sensor readings and scale it */
823 ltemp = read_smon_adc(state, 1);
824 if (ltemp == -1) {
825 /* XXX What do we do now ? */
826 state->overtemp++;
827 if (rc == 0)
828 rc = -EIO;
829 DBG(" cpu %d, temp reading error !\n", state->index);
830 } else {
831 /* Fixup temperature according to diode calibration
833 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
834 state->index,
835 ltemp, state->mpu.mdiode, state->mpu.bdiode);
836 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
837 state->last_temp = *temp;
838 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
842 * Read voltage & current and calculate power
844 volts = read_smon_adc(state, 3);
845 amps = read_smon_adc(state, 4);
847 /* Scale voltage and current raw sensor values according to fixed scales
848 * obtained in Darwin and calculate power from I and V
850 volts *= ADC_CPU_VOLTAGE_SCALE;
851 amps *= ADC_CPU_CURRENT_SCALE;
852 *power = (((u64)volts) * ((u64)amps)) >> 16;
853 state->voltage = volts;
854 state->current_a = amps;
855 state->last_power = *power;
857 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
858 state->index, FIX32TOPRINT(state->current_a),
859 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
861 return 0;
864 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
866 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
867 s64 integ_p, deriv_p, prop_p, sum;
868 int i;
870 /* Calculate power target value (could be done once for all)
871 * and convert to a 16.16 fp number
873 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
874 DBG(" power target: %d.%03d, error: %d.%03d\n",
875 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
877 /* Store temperature and power in history array */
878 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
879 state->temp_history[state->cur_temp] = temp;
880 state->cur_power = (state->cur_power + 1) % state->count_power;
881 state->power_history[state->cur_power] = power;
882 state->error_history[state->cur_power] = power_target - power;
884 /* If first loop, fill the history table */
885 if (state->first) {
886 for (i = 0; i < (state->count_power - 1); i++) {
887 state->cur_power = (state->cur_power + 1) % state->count_power;
888 state->power_history[state->cur_power] = power;
889 state->error_history[state->cur_power] = power_target - power;
891 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
892 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
893 state->temp_history[state->cur_temp] = temp;
895 state->first = 0;
898 /* Calculate the integral term normally based on the "power" values */
899 sum = 0;
900 integral = 0;
901 for (i = 0; i < state->count_power; i++)
902 integral += state->error_history[i];
903 integral *= CPU_PID_INTERVAL;
904 DBG(" integral: %08x\n", integral);
906 /* Calculate the adjusted input (sense value).
907 * G_r is 12.20
908 * integ is 16.16
909 * so the result is 28.36
911 * input target is mpu.ttarget, input max is mpu.tmax
913 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
914 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
915 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
916 adj_in_target = (state->mpu.ttarget << 16);
917 if (adj_in_target > sval)
918 adj_in_target = sval;
919 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
920 state->mpu.ttarget);
922 /* Calculate the derivative term */
923 derivative = state->temp_history[state->cur_temp] -
924 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
925 % CPU_TEMP_HISTORY_SIZE];
926 derivative /= CPU_PID_INTERVAL;
927 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
928 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
929 sum += deriv_p;
931 /* Calculate the proportional term */
932 proportional = temp - adj_in_target;
933 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
934 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
935 sum += prop_p;
937 /* Scale sum */
938 sum >>= 36;
940 DBG(" sum: %d\n", (int)sum);
941 state->rpm += (s32)sum;
944 static void do_monitor_cpu_combined(void)
946 struct cpu_pid_state *state0 = &cpu_state[0];
947 struct cpu_pid_state *state1 = &cpu_state[1];
948 s32 temp0, power0, temp1, power1;
949 s32 temp_combi, power_combi;
950 int rc, intake, pump;
952 rc = do_read_one_cpu_values(state0, &temp0, &power0);
953 if (rc < 0) {
954 /* XXX What do we do now ? */
956 state1->overtemp = 0;
957 rc = do_read_one_cpu_values(state1, &temp1, &power1);
958 if (rc < 0) {
959 /* XXX What do we do now ? */
961 if (state1->overtemp)
962 state0->overtemp++;
964 temp_combi = max(temp0, temp1);
965 power_combi = max(power0, power1);
967 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
968 * full blown immediately and try to trigger a shutdown
970 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
971 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
972 temp_combi >> 16);
973 state0->overtemp += CPU_MAX_OVERTEMP / 4;
974 } else if (temp_combi > (state0->mpu.tmax << 16))
975 state0->overtemp++;
976 else
977 state0->overtemp = 0;
978 if (state0->overtemp >= CPU_MAX_OVERTEMP)
979 critical_state = 1;
980 if (state0->overtemp > 0) {
981 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
982 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
983 pump = state0->pump_max;
984 goto do_set_fans;
987 /* Do the PID */
988 do_cpu_pid(state0, temp_combi, power_combi);
990 /* Range check */
991 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
992 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
994 /* Calculate intake fan speed */
995 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
996 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
997 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
998 state0->intake_rpm = intake;
1000 /* Calculate pump speed */
1001 pump = (state0->rpm * state0->pump_max) /
1002 state0->mpu.rmaxn_exhaust_fan;
1003 pump = min(pump, state0->pump_max);
1004 pump = max(pump, state0->pump_min);
1006 do_set_fans:
1007 /* We copy values from state 0 to state 1 for /sysfs */
1008 state1->rpm = state0->rpm;
1009 state1->intake_rpm = state0->intake_rpm;
1011 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
1012 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
1014 /* We should check for errors, shouldn't we ? But then, what
1015 * do we do once the error occurs ? For FCU notified fan
1016 * failures (-EFAULT) we probably want to notify userland
1017 * some way...
1019 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1020 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1021 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1022 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1024 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1025 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1026 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1027 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1030 static void do_monitor_cpu_split(struct cpu_pid_state *state)
1032 s32 temp, power;
1033 int rc, intake;
1035 /* Read current fan status */
1036 rc = do_read_one_cpu_values(state, &temp, &power);
1037 if (rc < 0) {
1038 /* XXX What do we do now ? */
1041 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1042 * full blown immediately and try to trigger a shutdown
1044 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1045 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1046 " (%d) !\n",
1047 state->index, temp >> 16);
1048 state->overtemp += CPU_MAX_OVERTEMP / 4;
1049 } else if (temp > (state->mpu.tmax << 16))
1050 state->overtemp++;
1051 else
1052 state->overtemp = 0;
1053 if (state->overtemp >= CPU_MAX_OVERTEMP)
1054 critical_state = 1;
1055 if (state->overtemp > 0) {
1056 state->rpm = state->mpu.rmaxn_exhaust_fan;
1057 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1058 goto do_set_fans;
1061 /* Do the PID */
1062 do_cpu_pid(state, temp, power);
1064 /* Range check */
1065 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1066 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1068 /* Calculate intake fan */
1069 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1070 intake = max(intake, (int)state->mpu.rminn_intake_fan);
1071 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1072 state->intake_rpm = intake;
1074 do_set_fans:
1075 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1076 state->index, (int)state->rpm, intake, state->overtemp);
1078 /* We should check for errors, shouldn't we ? But then, what
1079 * do we do once the error occurs ? For FCU notified fan
1080 * failures (-EFAULT) we probably want to notify userland
1081 * some way...
1083 if (state->index == 0) {
1084 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1085 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1086 } else {
1087 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1088 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1092 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1094 s32 temp, power, fan_min;
1095 int rc;
1097 /* Read current fan status */
1098 rc = do_read_one_cpu_values(state, &temp, &power);
1099 if (rc < 0) {
1100 /* XXX What do we do now ? */
1103 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1104 * full blown immediately and try to trigger a shutdown
1106 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1107 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1108 " (%d) !\n",
1109 state->index, temp >> 16);
1110 state->overtemp = CPU_MAX_OVERTEMP / 4;
1111 } else if (temp > (state->mpu.tmax << 16))
1112 state->overtemp++;
1113 else
1114 state->overtemp = 0;
1115 if (state->overtemp >= CPU_MAX_OVERTEMP)
1116 critical_state = 1;
1117 if (state->overtemp > 0) {
1118 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1119 goto do_set_fans;
1122 /* Do the PID */
1123 do_cpu_pid(state, temp, power);
1125 /* Check clamp from dimms */
1126 fan_min = dimm_output_clamp;
1127 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1129 DBG(" CPU min mpu = %d, min dimm = %d\n",
1130 state->mpu.rminn_intake_fan, dimm_output_clamp);
1132 state->rpm = max(state->rpm, (int)fan_min);
1133 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1134 state->intake_rpm = state->rpm;
1136 do_set_fans:
1137 DBG("** CPU %d RPM: %d overtemp: %d\n",
1138 state->index, (int)state->rpm, state->overtemp);
1140 /* We should check for errors, shouldn't we ? But then, what
1141 * do we do once the error occurs ? For FCU notified fan
1142 * failures (-EFAULT) we probably want to notify userland
1143 * some way...
1145 if (state->index == 0) {
1146 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1147 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1148 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1149 } else {
1150 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1151 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1152 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1157 * Initialize the state structure for one CPU control loop
1159 static int init_cpu_state(struct cpu_pid_state *state, int index)
1161 int err;
1163 state->index = index;
1164 state->first = 1;
1165 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1166 state->overtemp = 0;
1167 state->adc_config = 0x00;
1170 if (index == 0)
1171 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1172 else if (index == 1)
1173 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1174 if (state->monitor == NULL)
1175 goto fail;
1177 if (read_eeprom(index, &state->mpu))
1178 goto fail;
1180 state->count_power = state->mpu.tguardband;
1181 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1182 printk(KERN_WARNING "Warning ! too many power history slots\n");
1183 state->count_power = CPU_POWER_HISTORY_SIZE;
1185 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1187 if (index == 0) {
1188 err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1189 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1190 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1191 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1192 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1193 } else {
1194 err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1195 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1196 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1197 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1198 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1200 if (err)
1201 printk(KERN_WARNING "Failed to create some of the atribute"
1202 "files for CPU %d\n", index);
1204 return 0;
1205 fail:
1206 if (state->monitor)
1207 detach_i2c_chip(state->monitor);
1208 state->monitor = NULL;
1210 return -ENODEV;
1214 * Dispose of the state data for one CPU control loop
1216 static void dispose_cpu_state(struct cpu_pid_state *state)
1218 if (state->monitor == NULL)
1219 return;
1221 if (state->index == 0) {
1222 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1223 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1224 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1225 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1226 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1227 } else {
1228 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1229 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1230 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1231 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1232 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1235 detach_i2c_chip(state->monitor);
1236 state->monitor = NULL;
1240 * Motherboard backside & U3 heatsink fan control loop
1242 static void do_monitor_backside(struct backside_pid_state *state)
1244 s32 temp, integral, derivative, fan_min;
1245 s64 integ_p, deriv_p, prop_p, sum;
1246 int i, rc;
1248 if (--state->ticks != 0)
1249 return;
1250 state->ticks = backside_params.interval;
1252 DBG("backside:\n");
1254 /* Check fan status */
1255 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1256 if (rc < 0) {
1257 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1258 /* XXX What do we do now ? */
1259 } else
1260 state->pwm = rc;
1261 DBG(" current pwm: %d\n", state->pwm);
1263 /* Get some sensor readings */
1264 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1265 state->last_temp = temp;
1266 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1267 FIX32TOPRINT(backside_params.input_target));
1269 /* Store temperature and error in history array */
1270 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1271 state->sample_history[state->cur_sample] = temp;
1272 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1274 /* If first loop, fill the history table */
1275 if (state->first) {
1276 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1277 state->cur_sample = (state->cur_sample + 1) %
1278 BACKSIDE_PID_HISTORY_SIZE;
1279 state->sample_history[state->cur_sample] = temp;
1280 state->error_history[state->cur_sample] =
1281 temp - backside_params.input_target;
1283 state->first = 0;
1286 /* Calculate the integral term */
1287 sum = 0;
1288 integral = 0;
1289 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1290 integral += state->error_history[i];
1291 integral *= backside_params.interval;
1292 DBG(" integral: %08x\n", integral);
1293 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1294 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1295 sum += integ_p;
1297 /* Calculate the derivative term */
1298 derivative = state->error_history[state->cur_sample] -
1299 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1300 % BACKSIDE_PID_HISTORY_SIZE];
1301 derivative /= backside_params.interval;
1302 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1303 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1304 sum += deriv_p;
1306 /* Calculate the proportional term */
1307 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1308 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1309 sum += prop_p;
1311 /* Scale sum */
1312 sum >>= 36;
1314 DBG(" sum: %d\n", (int)sum);
1315 if (backside_params.additive)
1316 state->pwm += (s32)sum;
1317 else
1318 state->pwm = sum;
1320 /* Check for clamp */
1321 fan_min = (dimm_output_clamp * 100) / 14000;
1322 fan_min = max(fan_min, backside_params.output_min);
1324 state->pwm = max(state->pwm, fan_min);
1325 state->pwm = min(state->pwm, backside_params.output_max);
1327 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1328 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1332 * Initialize the state structure for the backside fan control loop
1334 static int init_backside_state(struct backside_pid_state *state)
1336 struct device_node *u3;
1337 int u3h = 1; /* conservative by default */
1338 int err;
1341 * There are different PID params for machines with U3 and machines
1342 * with U3H, pick the right ones now
1344 u3 = of_find_node_by_path("/u3@0,f8000000");
1345 if (u3 != NULL) {
1346 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1347 if (vers)
1348 if (((*vers) & 0x3f) < 0x34)
1349 u3h = 0;
1350 of_node_put(u3);
1353 if (rackmac) {
1354 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1355 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1356 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1357 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1358 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1359 backside_params.G_r = BACKSIDE_PID_G_r;
1360 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1361 backside_params.additive = 0;
1362 } else if (u3h) {
1363 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1364 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1365 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1366 backside_params.interval = BACKSIDE_PID_INTERVAL;
1367 backside_params.G_p = BACKSIDE_PID_G_p;
1368 backside_params.G_r = BACKSIDE_PID_G_r;
1369 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1370 backside_params.additive = 1;
1371 } else {
1372 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1373 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1374 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1375 backside_params.interval = BACKSIDE_PID_INTERVAL;
1376 backside_params.G_p = BACKSIDE_PID_G_p;
1377 backside_params.G_r = BACKSIDE_PID_G_r;
1378 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1379 backside_params.additive = 1;
1382 state->ticks = 1;
1383 state->first = 1;
1384 state->pwm = 50;
1386 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1387 if (state->monitor == NULL)
1388 return -ENODEV;
1390 err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1391 err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1392 if (err)
1393 printk(KERN_WARNING "Failed to create attribute file(s)"
1394 " for backside fan\n");
1396 return 0;
1400 * Dispose of the state data for the backside control loop
1402 static void dispose_backside_state(struct backside_pid_state *state)
1404 if (state->monitor == NULL)
1405 return;
1407 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1408 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1410 detach_i2c_chip(state->monitor);
1411 state->monitor = NULL;
1415 * Drives bay fan control loop
1417 static void do_monitor_drives(struct drives_pid_state *state)
1419 s32 temp, integral, derivative;
1420 s64 integ_p, deriv_p, prop_p, sum;
1421 int i, rc;
1423 if (--state->ticks != 0)
1424 return;
1425 state->ticks = DRIVES_PID_INTERVAL;
1427 DBG("drives:\n");
1429 /* Check fan status */
1430 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1431 if (rc < 0) {
1432 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1433 /* XXX What do we do now ? */
1434 } else
1435 state->rpm = rc;
1436 DBG(" current rpm: %d\n", state->rpm);
1438 /* Get some sensor readings */
1439 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1440 DS1775_TEMP)) << 8;
1441 state->last_temp = temp;
1442 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1443 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1445 /* Store temperature and error in history array */
1446 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1447 state->sample_history[state->cur_sample] = temp;
1448 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1450 /* If first loop, fill the history table */
1451 if (state->first) {
1452 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1453 state->cur_sample = (state->cur_sample + 1) %
1454 DRIVES_PID_HISTORY_SIZE;
1455 state->sample_history[state->cur_sample] = temp;
1456 state->error_history[state->cur_sample] =
1457 temp - DRIVES_PID_INPUT_TARGET;
1459 state->first = 0;
1462 /* Calculate the integral term */
1463 sum = 0;
1464 integral = 0;
1465 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1466 integral += state->error_history[i];
1467 integral *= DRIVES_PID_INTERVAL;
1468 DBG(" integral: %08x\n", integral);
1469 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1470 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1471 sum += integ_p;
1473 /* Calculate the derivative term */
1474 derivative = state->error_history[state->cur_sample] -
1475 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1476 % DRIVES_PID_HISTORY_SIZE];
1477 derivative /= DRIVES_PID_INTERVAL;
1478 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1479 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1480 sum += deriv_p;
1482 /* Calculate the proportional term */
1483 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1484 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1485 sum += prop_p;
1487 /* Scale sum */
1488 sum >>= 36;
1490 DBG(" sum: %d\n", (int)sum);
1491 state->rpm += (s32)sum;
1493 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1494 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1496 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1497 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1501 * Initialize the state structure for the drives bay fan control loop
1503 static int init_drives_state(struct drives_pid_state *state)
1505 int err;
1507 state->ticks = 1;
1508 state->first = 1;
1509 state->rpm = 1000;
1511 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1512 if (state->monitor == NULL)
1513 return -ENODEV;
1515 err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1516 err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1517 if (err)
1518 printk(KERN_WARNING "Failed to create attribute file(s)"
1519 " for drives bay fan\n");
1521 return 0;
1525 * Dispose of the state data for the drives control loop
1527 static void dispose_drives_state(struct drives_pid_state *state)
1529 if (state->monitor == NULL)
1530 return;
1532 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1533 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1535 detach_i2c_chip(state->monitor);
1536 state->monitor = NULL;
1540 * DIMMs temp control loop
1542 static void do_monitor_dimms(struct dimm_pid_state *state)
1544 s32 temp, integral, derivative, fan_min;
1545 s64 integ_p, deriv_p, prop_p, sum;
1546 int i;
1548 if (--state->ticks != 0)
1549 return;
1550 state->ticks = DIMM_PID_INTERVAL;
1552 DBG("DIMM:\n");
1554 DBG(" current value: %d\n", state->output);
1556 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1557 if (temp < 0)
1558 return;
1559 temp <<= 16;
1560 state->last_temp = temp;
1561 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1562 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1564 /* Store temperature and error in history array */
1565 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1566 state->sample_history[state->cur_sample] = temp;
1567 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1569 /* If first loop, fill the history table */
1570 if (state->first) {
1571 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1572 state->cur_sample = (state->cur_sample + 1) %
1573 DIMM_PID_HISTORY_SIZE;
1574 state->sample_history[state->cur_sample] = temp;
1575 state->error_history[state->cur_sample] =
1576 temp - DIMM_PID_INPUT_TARGET;
1578 state->first = 0;
1581 /* Calculate the integral term */
1582 sum = 0;
1583 integral = 0;
1584 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1585 integral += state->error_history[i];
1586 integral *= DIMM_PID_INTERVAL;
1587 DBG(" integral: %08x\n", integral);
1588 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1589 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1590 sum += integ_p;
1592 /* Calculate the derivative term */
1593 derivative = state->error_history[state->cur_sample] -
1594 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1595 % DIMM_PID_HISTORY_SIZE];
1596 derivative /= DIMM_PID_INTERVAL;
1597 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1598 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1599 sum += deriv_p;
1601 /* Calculate the proportional term */
1602 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1603 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1604 sum += prop_p;
1606 /* Scale sum */
1607 sum >>= 36;
1609 DBG(" sum: %d\n", (int)sum);
1610 state->output = (s32)sum;
1611 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1612 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1613 dimm_output_clamp = state->output;
1615 DBG("** DIMM clamp value: %d\n", (int)state->output);
1617 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1618 fan_min = (dimm_output_clamp * 100) / 14000;
1619 fan_min = max(fan_min, backside_params.output_min);
1620 if (backside_state.pwm < fan_min) {
1621 backside_state.pwm = fan_min;
1622 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1623 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1628 * Initialize the state structure for the DIMM temp control loop
1630 static int init_dimms_state(struct dimm_pid_state *state)
1632 state->ticks = 1;
1633 state->first = 1;
1634 state->output = 4000;
1636 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1637 if (state->monitor == NULL)
1638 return -ENODEV;
1640 if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1641 printk(KERN_WARNING "Failed to create attribute file"
1642 " for DIMM temperature\n");
1644 return 0;
1648 * Dispose of the state data for the DIMM control loop
1650 static void dispose_dimms_state(struct dimm_pid_state *state)
1652 if (state->monitor == NULL)
1653 return;
1655 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1657 detach_i2c_chip(state->monitor);
1658 state->monitor = NULL;
1662 * Slots fan control loop
1664 static void do_monitor_slots(struct slots_pid_state *state)
1666 s32 temp, integral, derivative;
1667 s64 integ_p, deriv_p, prop_p, sum;
1668 int i, rc;
1670 if (--state->ticks != 0)
1671 return;
1672 state->ticks = SLOTS_PID_INTERVAL;
1674 DBG("slots:\n");
1676 /* Check fan status */
1677 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1678 if (rc < 0) {
1679 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1680 /* XXX What do we do now ? */
1681 } else
1682 state->pwm = rc;
1683 DBG(" current pwm: %d\n", state->pwm);
1685 /* Get some sensor readings */
1686 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1687 DS1775_TEMP)) << 8;
1688 state->last_temp = temp;
1689 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1690 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1692 /* Store temperature and error in history array */
1693 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1694 state->sample_history[state->cur_sample] = temp;
1695 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1697 /* If first loop, fill the history table */
1698 if (state->first) {
1699 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1700 state->cur_sample = (state->cur_sample + 1) %
1701 SLOTS_PID_HISTORY_SIZE;
1702 state->sample_history[state->cur_sample] = temp;
1703 state->error_history[state->cur_sample] =
1704 temp - SLOTS_PID_INPUT_TARGET;
1706 state->first = 0;
1709 /* Calculate the integral term */
1710 sum = 0;
1711 integral = 0;
1712 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1713 integral += state->error_history[i];
1714 integral *= SLOTS_PID_INTERVAL;
1715 DBG(" integral: %08x\n", integral);
1716 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1717 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1718 sum += integ_p;
1720 /* Calculate the derivative term */
1721 derivative = state->error_history[state->cur_sample] -
1722 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1723 % SLOTS_PID_HISTORY_SIZE];
1724 derivative /= SLOTS_PID_INTERVAL;
1725 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1726 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1727 sum += deriv_p;
1729 /* Calculate the proportional term */
1730 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1731 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1732 sum += prop_p;
1734 /* Scale sum */
1735 sum >>= 36;
1737 DBG(" sum: %d\n", (int)sum);
1738 state->pwm = (s32)sum;
1740 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1741 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1743 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1744 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1748 * Initialize the state structure for the slots bay fan control loop
1750 static int init_slots_state(struct slots_pid_state *state)
1752 int err;
1754 state->ticks = 1;
1755 state->first = 1;
1756 state->pwm = 50;
1758 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1759 if (state->monitor == NULL)
1760 return -ENODEV;
1762 err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1763 err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1764 if (err)
1765 printk(KERN_WARNING "Failed to create attribute file(s)"
1766 " for slots bay fan\n");
1768 return 0;
1772 * Dispose of the state data for the slots control loop
1774 static void dispose_slots_state(struct slots_pid_state *state)
1776 if (state->monitor == NULL)
1777 return;
1779 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1780 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1782 detach_i2c_chip(state->monitor);
1783 state->monitor = NULL;
1787 static int call_critical_overtemp(void)
1789 char *argv[] = { critical_overtemp_path, NULL };
1790 static char *envp[] = { "HOME=/",
1791 "TERM=linux",
1792 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1793 NULL };
1795 return call_usermodehelper(critical_overtemp_path,
1796 argv, envp, UMH_WAIT_EXEC);
1801 * Here's the kernel thread that calls the various control loops
1803 static int main_control_loop(void *x)
1805 DBG("main_control_loop started\n");
1807 mutex_lock(&driver_lock);
1809 if (start_fcu() < 0) {
1810 printk(KERN_ERR "kfand: failed to start FCU\n");
1811 mutex_unlock(&driver_lock);
1812 goto out;
1815 /* Set the PCI fan once for now on non-RackMac */
1816 if (!rackmac)
1817 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1819 /* Initialize ADCs */
1820 initialize_adc(&cpu_state[0]);
1821 if (cpu_state[1].monitor != NULL)
1822 initialize_adc(&cpu_state[1]);
1824 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1826 mutex_unlock(&driver_lock);
1828 while (state == state_attached) {
1829 unsigned long elapsed, start;
1831 start = jiffies;
1833 mutex_lock(&driver_lock);
1835 /* Tickle the FCU just in case */
1836 if (--fcu_tickle_ticks < 0) {
1837 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1838 tickle_fcu();
1841 /* First, we always calculate the new DIMMs state on an Xserve */
1842 if (rackmac)
1843 do_monitor_dimms(&dimms_state);
1845 /* Then, the CPUs */
1846 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1847 do_monitor_cpu_combined();
1848 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1849 do_monitor_cpu_rack(&cpu_state[0]);
1850 if (cpu_state[1].monitor != NULL)
1851 do_monitor_cpu_rack(&cpu_state[1]);
1852 // better deal with UP
1853 } else {
1854 do_monitor_cpu_split(&cpu_state[0]);
1855 if (cpu_state[1].monitor != NULL)
1856 do_monitor_cpu_split(&cpu_state[1]);
1857 // better deal with UP
1859 /* Then, the rest */
1860 do_monitor_backside(&backside_state);
1861 if (rackmac)
1862 do_monitor_slots(&slots_state);
1863 else
1864 do_monitor_drives(&drives_state);
1865 mutex_unlock(&driver_lock);
1867 if (critical_state == 1) {
1868 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1869 printk(KERN_WARNING "Attempting to shut down...\n");
1870 if (call_critical_overtemp()) {
1871 printk(KERN_WARNING "Can't call %s, power off now!\n",
1872 critical_overtemp_path);
1873 machine_power_off();
1876 if (critical_state > 0)
1877 critical_state++;
1878 if (critical_state > MAX_CRITICAL_STATE) {
1879 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1880 machine_power_off();
1883 // FIXME: Deal with signals
1884 elapsed = jiffies - start;
1885 if (elapsed < HZ)
1886 schedule_timeout_interruptible(HZ - elapsed);
1889 out:
1890 DBG("main_control_loop ended\n");
1892 ctrl_task = 0;
1893 complete_and_exit(&ctrl_complete, 0);
1897 * Dispose the control loops when tearing down
1899 static void dispose_control_loops(void)
1901 dispose_cpu_state(&cpu_state[0]);
1902 dispose_cpu_state(&cpu_state[1]);
1903 dispose_backside_state(&backside_state);
1904 dispose_drives_state(&drives_state);
1905 dispose_slots_state(&slots_state);
1906 dispose_dimms_state(&dimms_state);
1910 * Create the control loops. U3-0 i2c bus is up, so we can now
1911 * get to the various sensors
1913 static int create_control_loops(void)
1915 struct device_node *np;
1917 /* Count CPUs from the device-tree, we don't care how many are
1918 * actually used by Linux
1920 cpu_count = 0;
1921 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1922 cpu_count++;
1924 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1926 /* Decide the type of PID algorithm to use based on the presence of
1927 * the pumps, though that may not be the best way, that is good enough
1928 * for now
1930 if (rackmac)
1931 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1932 else if (machine_is_compatible("PowerMac7,3")
1933 && (cpu_count > 1)
1934 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1935 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1936 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1937 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1938 } else
1939 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1941 /* Create control loops for everything. If any fail, everything
1942 * fails
1944 if (init_cpu_state(&cpu_state[0], 0))
1945 goto fail;
1946 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1947 fetch_cpu_pumps_minmax();
1949 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1950 goto fail;
1951 if (init_backside_state(&backside_state))
1952 goto fail;
1953 if (rackmac && init_dimms_state(&dimms_state))
1954 goto fail;
1955 if (rackmac && init_slots_state(&slots_state))
1956 goto fail;
1957 if (!rackmac && init_drives_state(&drives_state))
1958 goto fail;
1960 DBG("all control loops up !\n");
1962 return 0;
1964 fail:
1965 DBG("failure creating control loops, disposing\n");
1967 dispose_control_loops();
1969 return -ENODEV;
1973 * Start the control loops after everything is up, that is create
1974 * the thread that will make them run
1976 static void start_control_loops(void)
1978 init_completion(&ctrl_complete);
1980 ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1984 * Stop the control loops when tearing down
1986 static void stop_control_loops(void)
1988 if (ctrl_task)
1989 wait_for_completion(&ctrl_complete);
1993 * Attach to the i2c FCU after detecting U3-1 bus
1995 static int attach_fcu(void)
1997 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1998 if (fcu == NULL)
1999 return -ENODEV;
2001 DBG("FCU attached\n");
2003 return 0;
2007 * Detach from the i2c FCU when tearing down
2009 static void detach_fcu(void)
2011 if (fcu)
2012 detach_i2c_chip(fcu);
2013 fcu = NULL;
2017 * Attach to the i2c controller. We probe the various chips based
2018 * on the device-tree nodes and build everything for the driver to
2019 * run, we then kick the driver monitoring thread
2021 static int therm_pm72_attach(struct i2c_adapter *adapter)
2023 mutex_lock(&driver_lock);
2025 /* Check state */
2026 if (state == state_detached)
2027 state = state_attaching;
2028 if (state != state_attaching) {
2029 mutex_unlock(&driver_lock);
2030 return 0;
2033 /* Check if we are looking for one of these */
2034 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2035 u3_0 = adapter;
2036 DBG("found U3-0\n");
2037 if (k2 || !rackmac)
2038 if (create_control_loops())
2039 u3_0 = NULL;
2040 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2041 u3_1 = adapter;
2042 DBG("found U3-1, attaching FCU\n");
2043 if (attach_fcu())
2044 u3_1 = NULL;
2045 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2046 k2 = adapter;
2047 DBG("Found K2\n");
2048 if (u3_0 && rackmac)
2049 if (create_control_loops())
2050 k2 = NULL;
2052 /* We got all we need, start control loops */
2053 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2054 DBG("everything up, starting control loops\n");
2055 state = state_attached;
2056 start_control_loops();
2058 mutex_unlock(&driver_lock);
2060 return 0;
2064 * Called on every adapter when the driver or the i2c controller
2065 * is going away.
2067 static int therm_pm72_detach(struct i2c_adapter *adapter)
2069 mutex_lock(&driver_lock);
2071 if (state != state_detached)
2072 state = state_detaching;
2074 /* Stop control loops if any */
2075 DBG("stopping control loops\n");
2076 mutex_unlock(&driver_lock);
2077 stop_control_loops();
2078 mutex_lock(&driver_lock);
2080 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2081 DBG("lost U3-0, disposing control loops\n");
2082 dispose_control_loops();
2083 u3_0 = NULL;
2086 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2087 DBG("lost U3-1, detaching FCU\n");
2088 detach_fcu();
2089 u3_1 = NULL;
2091 if (u3_0 == NULL && u3_1 == NULL)
2092 state = state_detached;
2094 mutex_unlock(&driver_lock);
2096 return 0;
2099 static int fan_check_loc_match(const char *loc, int fan)
2101 char tmp[64];
2102 char *c, *e;
2104 strlcpy(tmp, fcu_fans[fan].loc, 64);
2106 c = tmp;
2107 for (;;) {
2108 e = strchr(c, ',');
2109 if (e)
2110 *e = 0;
2111 if (strcmp(loc, c) == 0)
2112 return 1;
2113 if (e == NULL)
2114 break;
2115 c = e + 1;
2117 return 0;
2120 static void fcu_lookup_fans(struct device_node *fcu_node)
2122 struct device_node *np = NULL;
2123 int i;
2125 /* The table is filled by default with values that are suitable
2126 * for the old machines without device-tree informations. We scan
2127 * the device-tree and override those values with whatever is
2128 * there
2131 DBG("Looking up FCU controls in device-tree...\n");
2133 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2134 int type = -1;
2135 const char *loc;
2136 const u32 *reg;
2138 DBG(" control: %s, type: %s\n", np->name, np->type);
2140 /* Detect control type */
2141 if (!strcmp(np->type, "fan-rpm-control") ||
2142 !strcmp(np->type, "fan-rpm"))
2143 type = FCU_FAN_RPM;
2144 if (!strcmp(np->type, "fan-pwm-control") ||
2145 !strcmp(np->type, "fan-pwm"))
2146 type = FCU_FAN_PWM;
2147 /* Only care about fans for now */
2148 if (type == -1)
2149 continue;
2151 /* Lookup for a matching location */
2152 loc = of_get_property(np, "location", NULL);
2153 reg = of_get_property(np, "reg", NULL);
2154 if (loc == NULL || reg == NULL)
2155 continue;
2156 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2158 for (i = 0; i < FCU_FAN_COUNT; i++) {
2159 int fan_id;
2161 if (!fan_check_loc_match(loc, i))
2162 continue;
2163 DBG(" location match, index: %d\n", i);
2164 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2165 if (type != fcu_fans[i].type) {
2166 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2167 "in device-tree for %s\n", np->full_name);
2168 break;
2170 if (type == FCU_FAN_RPM)
2171 fan_id = ((*reg) - 0x10) / 2;
2172 else
2173 fan_id = ((*reg) - 0x30) / 2;
2174 if (fan_id > 7) {
2175 printk(KERN_WARNING "therm_pm72: Can't parse "
2176 "fan ID in device-tree for %s\n", np->full_name);
2177 break;
2179 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2180 fcu_fans[i].id = fan_id;
2184 /* Now dump the array */
2185 printk(KERN_INFO "Detected fan controls:\n");
2186 for (i = 0; i < FCU_FAN_COUNT; i++) {
2187 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2188 continue;
2189 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2190 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2191 fcu_fans[i].id, fcu_fans[i].loc);
2195 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
2197 state = state_detached;
2199 /* Lookup the fans in the device tree */
2200 fcu_lookup_fans(dev->node);
2202 /* Add the driver */
2203 return i2c_add_driver(&therm_pm72_driver);
2206 static int fcu_of_remove(struct of_device* dev)
2208 i2c_del_driver(&therm_pm72_driver);
2210 return 0;
2213 static struct of_device_id fcu_match[] =
2216 .type = "fcu",
2221 static struct of_platform_driver fcu_of_platform_driver =
2223 .name = "temperature",
2224 .match_table = fcu_match,
2225 .probe = fcu_of_probe,
2226 .remove = fcu_of_remove
2230 * Check machine type, attach to i2c controller
2232 static int __init therm_pm72_init(void)
2234 struct device_node *np;
2236 rackmac = machine_is_compatible("RackMac3,1");
2238 if (!machine_is_compatible("PowerMac7,2") &&
2239 !machine_is_compatible("PowerMac7,3") &&
2240 !rackmac)
2241 return -ENODEV;
2243 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2245 np = of_find_node_by_type(NULL, "fcu");
2246 if (np == NULL) {
2247 /* Some machines have strangely broken device-tree */
2248 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2249 if (np == NULL) {
2250 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2251 return -ENODEV;
2254 of_dev = of_platform_device_create(np, "temperature", NULL);
2255 if (of_dev == NULL) {
2256 printk(KERN_ERR "Can't register FCU platform device !\n");
2257 return -ENODEV;
2260 of_register_platform_driver(&fcu_of_platform_driver);
2262 return 0;
2265 static void __exit therm_pm72_exit(void)
2267 of_unregister_platform_driver(&fcu_of_platform_driver);
2269 if (of_dev)
2270 of_device_unregister(of_dev);
2273 module_init(therm_pm72_init);
2274 module_exit(therm_pm72_exit);
2276 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2277 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2278 MODULE_LICENSE("GPL");