Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / macintosh / therm_pm72.c
blob1e0a69a5e8150664c7240c9793f76a8563a63687
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 <asm/prom.h>
126 #include <asm/machdep.h>
127 #include <asm/io.h>
128 #include <asm/system.h>
129 #include <asm/sections.h>
130 #include <asm/of_device.h>
131 #include <asm/macio.h>
132 #include <asm/of_platform.h>
134 #include "therm_pm72.h"
136 #define VERSION "1.3"
138 #undef DEBUG
140 #ifdef DEBUG
141 #define DBG(args...) printk(args)
142 #else
143 #define DBG(args...) do { } while(0)
144 #endif
148 * Driver statics
151 static struct of_device * of_dev;
152 static struct i2c_adapter * u3_0;
153 static struct i2c_adapter * u3_1;
154 static struct i2c_adapter * k2;
155 static struct i2c_client * fcu;
156 static struct cpu_pid_state cpu_state[2];
157 static struct basckside_pid_params backside_params;
158 static struct backside_pid_state backside_state;
159 static struct drives_pid_state drives_state;
160 static struct dimm_pid_state dimms_state;
161 static struct slots_pid_state slots_state;
162 static int state;
163 static int cpu_count;
164 static int cpu_pid_type;
165 static struct task_struct *ctrl_task;
166 static struct completion ctrl_complete;
167 static int critical_state;
168 static int rackmac;
169 static s32 dimm_output_clamp;
170 static int fcu_rpm_shift;
171 static int fcu_tickle_ticks;
172 static DECLARE_MUTEX(driver_lock);
175 * We have 3 types of CPU PID control. One is "split" old style control
176 * for intake & exhaust fans, the other is "combined" control for both
177 * CPUs that also deals with the pumps when present. To be "compatible"
178 * with OS X at this point, we only use "COMBINED" on the machines that
179 * are identified as having the pumps (though that identification is at
180 * least dodgy). Ultimately, we could probably switch completely to this
181 * algorithm provided we hack it to deal with the UP case
183 #define CPU_PID_TYPE_SPLIT 0
184 #define CPU_PID_TYPE_COMBINED 1
185 #define CPU_PID_TYPE_RACKMAC 2
188 * This table describes all fans in the FCU. The "id" and "type" values
189 * are defaults valid for all earlier machines. Newer machines will
190 * eventually override the table content based on the device-tree
192 struct fcu_fan_table
194 char* loc; /* location code */
195 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
196 int id; /* id or -1 */
199 #define FCU_FAN_RPM 0
200 #define FCU_FAN_PWM 1
202 #define FCU_FAN_ABSENT_ID -1
204 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
206 struct fcu_fan_table fcu_fans[] = {
207 [BACKSIDE_FAN_PWM_INDEX] = {
208 .loc = "BACKSIDE,SYS CTRLR FAN",
209 .type = FCU_FAN_PWM,
210 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
212 [DRIVES_FAN_RPM_INDEX] = {
213 .loc = "DRIVE BAY",
214 .type = FCU_FAN_RPM,
215 .id = DRIVES_FAN_RPM_DEFAULT_ID,
217 [SLOTS_FAN_PWM_INDEX] = {
218 .loc = "SLOT,PCI FAN",
219 .type = FCU_FAN_PWM,
220 .id = SLOTS_FAN_PWM_DEFAULT_ID,
222 [CPUA_INTAKE_FAN_RPM_INDEX] = {
223 .loc = "CPU A INTAKE",
224 .type = FCU_FAN_RPM,
225 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
227 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
228 .loc = "CPU A EXHAUST",
229 .type = FCU_FAN_RPM,
230 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
232 [CPUB_INTAKE_FAN_RPM_INDEX] = {
233 .loc = "CPU B INTAKE",
234 .type = FCU_FAN_RPM,
235 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
237 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
238 .loc = "CPU B EXHAUST",
239 .type = FCU_FAN_RPM,
240 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
242 /* pumps aren't present by default, have to be looked up in the
243 * device-tree
245 [CPUA_PUMP_RPM_INDEX] = {
246 .loc = "CPU A PUMP",
247 .type = FCU_FAN_RPM,
248 .id = FCU_FAN_ABSENT_ID,
250 [CPUB_PUMP_RPM_INDEX] = {
251 .loc = "CPU B PUMP",
252 .type = FCU_FAN_RPM,
253 .id = FCU_FAN_ABSENT_ID,
255 /* Xserve fans */
256 [CPU_A1_FAN_RPM_INDEX] = {
257 .loc = "CPU A 1",
258 .type = FCU_FAN_RPM,
259 .id = FCU_FAN_ABSENT_ID,
261 [CPU_A2_FAN_RPM_INDEX] = {
262 .loc = "CPU A 2",
263 .type = FCU_FAN_RPM,
264 .id = FCU_FAN_ABSENT_ID,
266 [CPU_A3_FAN_RPM_INDEX] = {
267 .loc = "CPU A 3",
268 .type = FCU_FAN_RPM,
269 .id = FCU_FAN_ABSENT_ID,
271 [CPU_B1_FAN_RPM_INDEX] = {
272 .loc = "CPU B 1",
273 .type = FCU_FAN_RPM,
274 .id = FCU_FAN_ABSENT_ID,
276 [CPU_B2_FAN_RPM_INDEX] = {
277 .loc = "CPU B 2",
278 .type = FCU_FAN_RPM,
279 .id = FCU_FAN_ABSENT_ID,
281 [CPU_B3_FAN_RPM_INDEX] = {
282 .loc = "CPU B 3",
283 .type = FCU_FAN_RPM,
284 .id = FCU_FAN_ABSENT_ID,
289 * i2c_driver structure to attach to the host i2c controller
292 static int therm_pm72_attach(struct i2c_adapter *adapter);
293 static int therm_pm72_detach(struct i2c_adapter *adapter);
295 static struct i2c_driver therm_pm72_driver =
297 .driver = {
298 .name = "therm_pm72",
300 .attach_adapter = therm_pm72_attach,
301 .detach_adapter = therm_pm72_detach,
305 * Utility function to create an i2c_client structure and
306 * attach it to one of u3 adapters
308 static struct i2c_client *attach_i2c_chip(int id, const char *name)
310 struct i2c_client *clt;
311 struct i2c_adapter *adap;
313 if (id & 0x200)
314 adap = k2;
315 else if (id & 0x100)
316 adap = u3_1;
317 else
318 adap = u3_0;
319 if (adap == NULL)
320 return NULL;
322 clt = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
323 if (clt == NULL)
324 return NULL;
326 clt->addr = (id >> 1) & 0x7f;
327 clt->adapter = adap;
328 clt->driver = &therm_pm72_driver;
329 strncpy(clt->name, name, I2C_NAME_SIZE-1);
331 if (i2c_attach_client(clt)) {
332 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
333 kfree(clt);
334 return NULL;
336 return clt;
340 * Utility function to get rid of the i2c_client structure
341 * (will also detach from the adapter hopepfully)
343 static void detach_i2c_chip(struct i2c_client *clt)
345 i2c_detach_client(clt);
346 kfree(clt);
350 * Here are the i2c chip access wrappers
353 static void initialize_adc(struct cpu_pid_state *state)
355 int rc;
356 u8 buf[2];
358 /* Read ADC the configuration register and cache it. We
359 * also make sure Config2 contains proper values, I've seen
360 * cases where we got stale grabage in there, thus preventing
361 * proper reading of conv. values
364 /* Clear Config2 */
365 buf[0] = 5;
366 buf[1] = 0;
367 i2c_master_send(state->monitor, buf, 2);
369 /* Read & cache Config1 */
370 buf[0] = 1;
371 rc = i2c_master_send(state->monitor, buf, 1);
372 if (rc > 0) {
373 rc = i2c_master_recv(state->monitor, buf, 1);
374 if (rc > 0) {
375 state->adc_config = buf[0];
376 DBG("ADC config reg: %02x\n", state->adc_config);
377 /* Disable shutdown mode */
378 state->adc_config &= 0xfe;
379 buf[0] = 1;
380 buf[1] = state->adc_config;
381 rc = i2c_master_send(state->monitor, buf, 2);
384 if (rc <= 0)
385 printk(KERN_ERR "therm_pm72: Error reading ADC config"
386 " register !\n");
389 static int read_smon_adc(struct cpu_pid_state *state, int chan)
391 int rc, data, tries = 0;
392 u8 buf[2];
394 for (;;) {
395 /* Set channel */
396 buf[0] = 1;
397 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
398 rc = i2c_master_send(state->monitor, buf, 2);
399 if (rc <= 0)
400 goto error;
401 /* Wait for convertion */
402 msleep(1);
403 /* Switch to data register */
404 buf[0] = 4;
405 rc = i2c_master_send(state->monitor, buf, 1);
406 if (rc <= 0)
407 goto error;
408 /* Read result */
409 rc = i2c_master_recv(state->monitor, buf, 2);
410 if (rc < 0)
411 goto error;
412 data = ((u16)buf[0]) << 8 | (u16)buf[1];
413 return data >> 6;
414 error:
415 DBG("Error reading ADC, retrying...\n");
416 if (++tries > 10) {
417 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
418 return -1;
420 msleep(10);
424 static int read_lm87_reg(struct i2c_client * chip, int reg)
426 int rc, tries = 0;
427 u8 buf;
429 for (;;) {
430 /* Set address */
431 buf = (u8)reg;
432 rc = i2c_master_send(chip, &buf, 1);
433 if (rc <= 0)
434 goto error;
435 rc = i2c_master_recv(chip, &buf, 1);
436 if (rc <= 0)
437 goto error;
438 return (int)buf;
439 error:
440 DBG("Error reading LM87, retrying...\n");
441 if (++tries > 10) {
442 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
443 return -1;
445 msleep(10);
449 static int fan_read_reg(int reg, unsigned char *buf, int nb)
451 int tries, nr, nw;
453 buf[0] = reg;
454 tries = 0;
455 for (;;) {
456 nw = i2c_master_send(fcu, buf, 1);
457 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
458 break;
459 msleep(10);
460 ++tries;
462 if (nw <= 0) {
463 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
464 return -EIO;
466 tries = 0;
467 for (;;) {
468 nr = i2c_master_recv(fcu, buf, nb);
469 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
470 break;
471 msleep(10);
472 ++tries;
474 if (nr <= 0)
475 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
476 return nr;
479 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
481 int tries, nw;
482 unsigned char buf[16];
484 buf[0] = reg;
485 memcpy(buf+1, ptr, nb);
486 ++nb;
487 tries = 0;
488 for (;;) {
489 nw = i2c_master_send(fcu, buf, nb);
490 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
491 break;
492 msleep(10);
493 ++tries;
495 if (nw < 0)
496 printk(KERN_ERR "Failure writing to FCU: %d", nw);
497 return nw;
500 static int start_fcu(void)
502 unsigned char buf = 0xff;
503 int rc;
505 rc = fan_write_reg(0xe, &buf, 1);
506 if (rc < 0)
507 return -EIO;
508 rc = fan_write_reg(0x2e, &buf, 1);
509 if (rc < 0)
510 return -EIO;
511 rc = fan_read_reg(0, &buf, 1);
512 if (rc < 0)
513 return -EIO;
514 fcu_rpm_shift = (buf == 1) ? 2 : 3;
515 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
516 fcu_rpm_shift);
518 return 0;
521 static int set_rpm_fan(int fan_index, int rpm)
523 unsigned char buf[2];
524 int rc, id, min, max;
526 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
527 return -EINVAL;
528 id = fcu_fans[fan_index].id;
529 if (id == FCU_FAN_ABSENT_ID)
530 return -EINVAL;
532 min = 2400 >> fcu_rpm_shift;
533 max = 56000 >> fcu_rpm_shift;
535 if (rpm < min)
536 rpm = min;
537 else if (rpm > max)
538 rpm = max;
539 buf[0] = rpm >> (8 - fcu_rpm_shift);
540 buf[1] = rpm << fcu_rpm_shift;
541 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
542 if (rc < 0)
543 return -EIO;
544 return 0;
547 static int get_rpm_fan(int fan_index, int programmed)
549 unsigned char failure;
550 unsigned char active;
551 unsigned char buf[2];
552 int rc, id, reg_base;
554 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
555 return -EINVAL;
556 id = fcu_fans[fan_index].id;
557 if (id == FCU_FAN_ABSENT_ID)
558 return -EINVAL;
560 rc = fan_read_reg(0xb, &failure, 1);
561 if (rc != 1)
562 return -EIO;
563 if ((failure & (1 << id)) != 0)
564 return -EFAULT;
565 rc = fan_read_reg(0xd, &active, 1);
566 if (rc != 1)
567 return -EIO;
568 if ((active & (1 << id)) == 0)
569 return -ENXIO;
571 /* Programmed value or real current speed */
572 reg_base = programmed ? 0x10 : 0x11;
573 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
574 if (rc != 2)
575 return -EIO;
577 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
580 static int set_pwm_fan(int fan_index, int pwm)
582 unsigned char buf[2];
583 int rc, id;
585 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
586 return -EINVAL;
587 id = fcu_fans[fan_index].id;
588 if (id == FCU_FAN_ABSENT_ID)
589 return -EINVAL;
591 if (pwm < 10)
592 pwm = 10;
593 else if (pwm > 100)
594 pwm = 100;
595 pwm = (pwm * 2559) / 1000;
596 buf[0] = pwm;
597 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
598 if (rc < 0)
599 return rc;
600 return 0;
603 static int get_pwm_fan(int fan_index)
605 unsigned char failure;
606 unsigned char active;
607 unsigned char buf[2];
608 int rc, id;
610 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
611 return -EINVAL;
612 id = fcu_fans[fan_index].id;
613 if (id == FCU_FAN_ABSENT_ID)
614 return -EINVAL;
616 rc = fan_read_reg(0x2b, &failure, 1);
617 if (rc != 1)
618 return -EIO;
619 if ((failure & (1 << id)) != 0)
620 return -EFAULT;
621 rc = fan_read_reg(0x2d, &active, 1);
622 if (rc != 1)
623 return -EIO;
624 if ((active & (1 << id)) == 0)
625 return -ENXIO;
627 /* Programmed value or real current speed */
628 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
629 if (rc != 1)
630 return -EIO;
632 return (buf[0] * 1000) / 2559;
635 static void tickle_fcu(void)
637 int pwm;
639 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
641 DBG("FCU Tickle, slots fan is: %d\n", pwm);
642 if (pwm < 0)
643 pwm = 100;
645 if (!rackmac) {
646 pwm = SLOTS_FAN_DEFAULT_PWM;
647 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
648 pwm = SLOTS_PID_OUTPUT_MIN;
650 /* That is hopefully enough to make the FCU happy */
651 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
656 * Utility routine to read the CPU calibration EEPROM data
657 * from the device-tree
659 static int read_eeprom(int cpu, struct mpu_data *out)
661 struct device_node *np;
662 char nodename[64];
663 const u8 *data;
664 int len;
666 /* prom.c routine for finding a node by path is a bit brain dead
667 * and requires exact @xxx unit numbers. This is a bit ugly but
668 * will work for these machines
670 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
671 np = of_find_node_by_path(nodename);
672 if (np == NULL) {
673 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
674 return -ENODEV;
676 data = of_get_property(np, "cpuid", &len);
677 if (data == NULL) {
678 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
679 of_node_put(np);
680 return -ENODEV;
682 memcpy(out, data, sizeof(struct mpu_data));
683 of_node_put(np);
685 return 0;
688 static void fetch_cpu_pumps_minmax(void)
690 struct cpu_pid_state *state0 = &cpu_state[0];
691 struct cpu_pid_state *state1 = &cpu_state[1];
692 u16 pump_min = 0, pump_max = 0xffff;
693 u16 tmp[4];
695 /* Try to fetch pumps min/max infos from eeprom */
697 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
698 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
699 pump_min = max(pump_min, tmp[0]);
700 pump_max = min(pump_max, tmp[1]);
702 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
703 pump_min = max(pump_min, tmp[2]);
704 pump_max = min(pump_max, tmp[3]);
707 /* Double check the values, this _IS_ needed as the EEPROM on
708 * some dual 2.5Ghz G5s seem, at least, to have both min & max
709 * same to the same value ... (grrrr)
711 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
712 pump_min = CPU_PUMP_OUTPUT_MIN;
713 pump_max = CPU_PUMP_OUTPUT_MAX;
716 state0->pump_min = state1->pump_min = pump_min;
717 state0->pump_max = state1->pump_max = pump_max;
721 * Now, unfortunately, sysfs doesn't give us a nice void * we could
722 * pass around to the attribute functions, so we don't really have
723 * choice but implement a bunch of them...
725 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
726 * the input twice... I accept patches :)
728 #define BUILD_SHOW_FUNC_FIX(name, data) \
729 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
731 ssize_t r; \
732 down(&driver_lock); \
733 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
734 up(&driver_lock); \
735 return r; \
737 #define BUILD_SHOW_FUNC_INT(name, data) \
738 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
740 return sprintf(buf, "%d", data); \
743 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
744 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
745 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
746 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
747 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
749 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
750 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
751 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
752 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
753 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
755 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
756 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
758 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
759 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
761 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
762 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
764 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
766 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
767 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
768 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
769 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
770 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
772 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
773 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
774 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
775 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
776 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
778 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
779 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
781 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
782 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
784 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
785 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
787 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
790 * CPUs fans control loop
793 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
795 s32 ltemp, volts, amps;
796 int index, rc = 0;
798 /* Default (in case of error) */
799 *temp = state->cur_temp;
800 *power = state->cur_power;
802 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
803 index = (state->index == 0) ?
804 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
805 else
806 index = (state->index == 0) ?
807 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
809 /* Read current fan status */
810 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
811 if (rc < 0) {
812 /* XXX What do we do now ? Nothing for now, keep old value, but
813 * return error upstream
815 DBG(" cpu %d, fan reading error !\n", state->index);
816 } else {
817 state->rpm = rc;
818 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
821 /* Get some sensor readings and scale it */
822 ltemp = read_smon_adc(state, 1);
823 if (ltemp == -1) {
824 /* XXX What do we do now ? */
825 state->overtemp++;
826 if (rc == 0)
827 rc = -EIO;
828 DBG(" cpu %d, temp reading error !\n", state->index);
829 } else {
830 /* Fixup temperature according to diode calibration
832 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
833 state->index,
834 ltemp, state->mpu.mdiode, state->mpu.bdiode);
835 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
836 state->last_temp = *temp;
837 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
841 * Read voltage & current and calculate power
843 volts = read_smon_adc(state, 3);
844 amps = read_smon_adc(state, 4);
846 /* Scale voltage and current raw sensor values according to fixed scales
847 * obtained in Darwin and calculate power from I and V
849 volts *= ADC_CPU_VOLTAGE_SCALE;
850 amps *= ADC_CPU_CURRENT_SCALE;
851 *power = (((u64)volts) * ((u64)amps)) >> 16;
852 state->voltage = volts;
853 state->current_a = amps;
854 state->last_power = *power;
856 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
857 state->index, FIX32TOPRINT(state->current_a),
858 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
860 return 0;
863 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
865 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
866 s64 integ_p, deriv_p, prop_p, sum;
867 int i;
869 /* Calculate power target value (could be done once for all)
870 * and convert to a 16.16 fp number
872 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
873 DBG(" power target: %d.%03d, error: %d.%03d\n",
874 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
876 /* Store temperature and power in history array */
877 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
878 state->temp_history[state->cur_temp] = temp;
879 state->cur_power = (state->cur_power + 1) % state->count_power;
880 state->power_history[state->cur_power] = power;
881 state->error_history[state->cur_power] = power_target - power;
883 /* If first loop, fill the history table */
884 if (state->first) {
885 for (i = 0; i < (state->count_power - 1); i++) {
886 state->cur_power = (state->cur_power + 1) % state->count_power;
887 state->power_history[state->cur_power] = power;
888 state->error_history[state->cur_power] = power_target - power;
890 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
891 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
892 state->temp_history[state->cur_temp] = temp;
894 state->first = 0;
897 /* Calculate the integral term normally based on the "power" values */
898 sum = 0;
899 integral = 0;
900 for (i = 0; i < state->count_power; i++)
901 integral += state->error_history[i];
902 integral *= CPU_PID_INTERVAL;
903 DBG(" integral: %08x\n", integral);
905 /* Calculate the adjusted input (sense value).
906 * G_r is 12.20
907 * integ is 16.16
908 * so the result is 28.36
910 * input target is mpu.ttarget, input max is mpu.tmax
912 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
913 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
914 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
915 adj_in_target = (state->mpu.ttarget << 16);
916 if (adj_in_target > sval)
917 adj_in_target = sval;
918 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
919 state->mpu.ttarget);
921 /* Calculate the derivative term */
922 derivative = state->temp_history[state->cur_temp] -
923 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
924 % CPU_TEMP_HISTORY_SIZE];
925 derivative /= CPU_PID_INTERVAL;
926 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
927 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
928 sum += deriv_p;
930 /* Calculate the proportional term */
931 proportional = temp - adj_in_target;
932 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
933 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
934 sum += prop_p;
936 /* Scale sum */
937 sum >>= 36;
939 DBG(" sum: %d\n", (int)sum);
940 state->rpm += (s32)sum;
943 static void do_monitor_cpu_combined(void)
945 struct cpu_pid_state *state0 = &cpu_state[0];
946 struct cpu_pid_state *state1 = &cpu_state[1];
947 s32 temp0, power0, temp1, power1;
948 s32 temp_combi, power_combi;
949 int rc, intake, pump;
951 rc = do_read_one_cpu_values(state0, &temp0, &power0);
952 if (rc < 0) {
953 /* XXX What do we do now ? */
955 state1->overtemp = 0;
956 rc = do_read_one_cpu_values(state1, &temp1, &power1);
957 if (rc < 0) {
958 /* XXX What do we do now ? */
960 if (state1->overtemp)
961 state0->overtemp++;
963 temp_combi = max(temp0, temp1);
964 power_combi = max(power0, power1);
966 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
967 * full blown immediately and try to trigger a shutdown
969 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
970 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
971 temp_combi >> 16);
972 state0->overtemp += CPU_MAX_OVERTEMP / 4;
973 } else if (temp_combi > (state0->mpu.tmax << 16))
974 state0->overtemp++;
975 else
976 state0->overtemp = 0;
977 if (state0->overtemp >= CPU_MAX_OVERTEMP)
978 critical_state = 1;
979 if (state0->overtemp > 0) {
980 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
981 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
982 pump = state0->pump_max;
983 goto do_set_fans;
986 /* Do the PID */
987 do_cpu_pid(state0, temp_combi, power_combi);
989 /* Range check */
990 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
991 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
993 /* Calculate intake fan speed */
994 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
995 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
996 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
997 state0->intake_rpm = intake;
999 /* Calculate pump speed */
1000 pump = (state0->rpm * state0->pump_max) /
1001 state0->mpu.rmaxn_exhaust_fan;
1002 pump = min(pump, state0->pump_max);
1003 pump = max(pump, state0->pump_min);
1005 do_set_fans:
1006 /* We copy values from state 0 to state 1 for /sysfs */
1007 state1->rpm = state0->rpm;
1008 state1->intake_rpm = state0->intake_rpm;
1010 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
1011 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
1013 /* We should check for errors, shouldn't we ? But then, what
1014 * do we do once the error occurs ? For FCU notified fan
1015 * failures (-EFAULT) we probably want to notify userland
1016 * some way...
1018 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1019 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1020 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1021 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1023 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1024 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1025 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1026 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1029 static void do_monitor_cpu_split(struct cpu_pid_state *state)
1031 s32 temp, power;
1032 int rc, intake;
1034 /* Read current fan status */
1035 rc = do_read_one_cpu_values(state, &temp, &power);
1036 if (rc < 0) {
1037 /* XXX What do we do now ? */
1040 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1041 * full blown immediately and try to trigger a shutdown
1043 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1044 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1045 " (%d) !\n",
1046 state->index, temp >> 16);
1047 state->overtemp += CPU_MAX_OVERTEMP / 4;
1048 } else if (temp > (state->mpu.tmax << 16))
1049 state->overtemp++;
1050 else
1051 state->overtemp = 0;
1052 if (state->overtemp >= CPU_MAX_OVERTEMP)
1053 critical_state = 1;
1054 if (state->overtemp > 0) {
1055 state->rpm = state->mpu.rmaxn_exhaust_fan;
1056 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1057 goto do_set_fans;
1060 /* Do the PID */
1061 do_cpu_pid(state, temp, power);
1063 /* Range check */
1064 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1065 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1067 /* Calculate intake fan */
1068 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1069 intake = max(intake, (int)state->mpu.rminn_intake_fan);
1070 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1071 state->intake_rpm = intake;
1073 do_set_fans:
1074 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1075 state->index, (int)state->rpm, intake, state->overtemp);
1077 /* We should check for errors, shouldn't we ? But then, what
1078 * do we do once the error occurs ? For FCU notified fan
1079 * failures (-EFAULT) we probably want to notify userland
1080 * some way...
1082 if (state->index == 0) {
1083 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1084 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1085 } else {
1086 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1087 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1091 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1093 s32 temp, power, fan_min;
1094 int rc;
1096 /* Read current fan status */
1097 rc = do_read_one_cpu_values(state, &temp, &power);
1098 if (rc < 0) {
1099 /* XXX What do we do now ? */
1102 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1103 * full blown immediately and try to trigger a shutdown
1105 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1106 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1107 " (%d) !\n",
1108 state->index, temp >> 16);
1109 state->overtemp = CPU_MAX_OVERTEMP / 4;
1110 } else if (temp > (state->mpu.tmax << 16))
1111 state->overtemp++;
1112 else
1113 state->overtemp = 0;
1114 if (state->overtemp >= CPU_MAX_OVERTEMP)
1115 critical_state = 1;
1116 if (state->overtemp > 0) {
1117 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1118 goto do_set_fans;
1121 /* Do the PID */
1122 do_cpu_pid(state, temp, power);
1124 /* Check clamp from dimms */
1125 fan_min = dimm_output_clamp;
1126 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1128 DBG(" CPU min mpu = %d, min dimm = %d\n",
1129 state->mpu.rminn_intake_fan, dimm_output_clamp);
1131 state->rpm = max(state->rpm, (int)fan_min);
1132 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1133 state->intake_rpm = state->rpm;
1135 do_set_fans:
1136 DBG("** CPU %d RPM: %d overtemp: %d\n",
1137 state->index, (int)state->rpm, state->overtemp);
1139 /* We should check for errors, shouldn't we ? But then, what
1140 * do we do once the error occurs ? For FCU notified fan
1141 * failures (-EFAULT) we probably want to notify userland
1142 * some way...
1144 if (state->index == 0) {
1145 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1146 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1147 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1148 } else {
1149 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1150 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1151 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1156 * Initialize the state structure for one CPU control loop
1158 static int init_cpu_state(struct cpu_pid_state *state, int index)
1160 int err;
1162 state->index = index;
1163 state->first = 1;
1164 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1165 state->overtemp = 0;
1166 state->adc_config = 0x00;
1169 if (index == 0)
1170 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1171 else if (index == 1)
1172 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1173 if (state->monitor == NULL)
1174 goto fail;
1176 if (read_eeprom(index, &state->mpu))
1177 goto fail;
1179 state->count_power = state->mpu.tguardband;
1180 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1181 printk(KERN_WARNING "Warning ! too many power history slots\n");
1182 state->count_power = CPU_POWER_HISTORY_SIZE;
1184 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1186 if (index == 0) {
1187 err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1188 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1189 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1190 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1191 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1192 } else {
1193 err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1194 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1195 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1196 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1197 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1199 if (err)
1200 printk(KERN_WARNING "Failed to create some of the atribute"
1201 "files for CPU %d\n", index);
1203 return 0;
1204 fail:
1205 if (state->monitor)
1206 detach_i2c_chip(state->monitor);
1207 state->monitor = NULL;
1209 return -ENODEV;
1213 * Dispose of the state data for one CPU control loop
1215 static void dispose_cpu_state(struct cpu_pid_state *state)
1217 if (state->monitor == NULL)
1218 return;
1220 if (state->index == 0) {
1221 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1222 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1223 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1224 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1225 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1226 } else {
1227 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1228 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1229 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1230 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1231 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1234 detach_i2c_chip(state->monitor);
1235 state->monitor = NULL;
1239 * Motherboard backside & U3 heatsink fan control loop
1241 static void do_monitor_backside(struct backside_pid_state *state)
1243 s32 temp, integral, derivative, fan_min;
1244 s64 integ_p, deriv_p, prop_p, sum;
1245 int i, rc;
1247 if (--state->ticks != 0)
1248 return;
1249 state->ticks = backside_params.interval;
1251 DBG("backside:\n");
1253 /* Check fan status */
1254 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1255 if (rc < 0) {
1256 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1257 /* XXX What do we do now ? */
1258 } else
1259 state->pwm = rc;
1260 DBG(" current pwm: %d\n", state->pwm);
1262 /* Get some sensor readings */
1263 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1264 state->last_temp = temp;
1265 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1266 FIX32TOPRINT(backside_params.input_target));
1268 /* Store temperature and error in history array */
1269 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1270 state->sample_history[state->cur_sample] = temp;
1271 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1273 /* If first loop, fill the history table */
1274 if (state->first) {
1275 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1276 state->cur_sample = (state->cur_sample + 1) %
1277 BACKSIDE_PID_HISTORY_SIZE;
1278 state->sample_history[state->cur_sample] = temp;
1279 state->error_history[state->cur_sample] =
1280 temp - backside_params.input_target;
1282 state->first = 0;
1285 /* Calculate the integral term */
1286 sum = 0;
1287 integral = 0;
1288 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1289 integral += state->error_history[i];
1290 integral *= backside_params.interval;
1291 DBG(" integral: %08x\n", integral);
1292 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1293 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1294 sum += integ_p;
1296 /* Calculate the derivative term */
1297 derivative = state->error_history[state->cur_sample] -
1298 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1299 % BACKSIDE_PID_HISTORY_SIZE];
1300 derivative /= backside_params.interval;
1301 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1302 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1303 sum += deriv_p;
1305 /* Calculate the proportional term */
1306 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1307 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1308 sum += prop_p;
1310 /* Scale sum */
1311 sum >>= 36;
1313 DBG(" sum: %d\n", (int)sum);
1314 if (backside_params.additive)
1315 state->pwm += (s32)sum;
1316 else
1317 state->pwm = sum;
1319 /* Check for clamp */
1320 fan_min = (dimm_output_clamp * 100) / 14000;
1321 fan_min = max(fan_min, backside_params.output_min);
1323 state->pwm = max(state->pwm, fan_min);
1324 state->pwm = min(state->pwm, backside_params.output_max);
1326 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1327 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1331 * Initialize the state structure for the backside fan control loop
1333 static int init_backside_state(struct backside_pid_state *state)
1335 struct device_node *u3;
1336 int u3h = 1; /* conservative by default */
1337 int err;
1340 * There are different PID params for machines with U3 and machines
1341 * with U3H, pick the right ones now
1343 u3 = of_find_node_by_path("/u3@0,f8000000");
1344 if (u3 != NULL) {
1345 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1346 if (vers)
1347 if (((*vers) & 0x3f) < 0x34)
1348 u3h = 0;
1349 of_node_put(u3);
1352 if (rackmac) {
1353 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1354 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1355 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1356 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1357 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1358 backside_params.G_r = BACKSIDE_PID_G_r;
1359 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1360 backside_params.additive = 0;
1361 } else if (u3h) {
1362 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1363 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1364 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1365 backside_params.interval = BACKSIDE_PID_INTERVAL;
1366 backside_params.G_p = BACKSIDE_PID_G_p;
1367 backside_params.G_r = BACKSIDE_PID_G_r;
1368 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1369 backside_params.additive = 1;
1370 } else {
1371 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1372 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1373 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1374 backside_params.interval = BACKSIDE_PID_INTERVAL;
1375 backside_params.G_p = BACKSIDE_PID_G_p;
1376 backside_params.G_r = BACKSIDE_PID_G_r;
1377 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1378 backside_params.additive = 1;
1381 state->ticks = 1;
1382 state->first = 1;
1383 state->pwm = 50;
1385 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1386 if (state->monitor == NULL)
1387 return -ENODEV;
1389 err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1390 err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1391 if (err)
1392 printk(KERN_WARNING "Failed to create attribute file(s)"
1393 " for backside fan\n");
1395 return 0;
1399 * Dispose of the state data for the backside control loop
1401 static void dispose_backside_state(struct backside_pid_state *state)
1403 if (state->monitor == NULL)
1404 return;
1406 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1407 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1409 detach_i2c_chip(state->monitor);
1410 state->monitor = NULL;
1414 * Drives bay fan control loop
1416 static void do_monitor_drives(struct drives_pid_state *state)
1418 s32 temp, integral, derivative;
1419 s64 integ_p, deriv_p, prop_p, sum;
1420 int i, rc;
1422 if (--state->ticks != 0)
1423 return;
1424 state->ticks = DRIVES_PID_INTERVAL;
1426 DBG("drives:\n");
1428 /* Check fan status */
1429 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1430 if (rc < 0) {
1431 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1432 /* XXX What do we do now ? */
1433 } else
1434 state->rpm = rc;
1435 DBG(" current rpm: %d\n", state->rpm);
1437 /* Get some sensor readings */
1438 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1439 DS1775_TEMP)) << 8;
1440 state->last_temp = temp;
1441 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1442 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1444 /* Store temperature and error in history array */
1445 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1446 state->sample_history[state->cur_sample] = temp;
1447 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1449 /* If first loop, fill the history table */
1450 if (state->first) {
1451 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1452 state->cur_sample = (state->cur_sample + 1) %
1453 DRIVES_PID_HISTORY_SIZE;
1454 state->sample_history[state->cur_sample] = temp;
1455 state->error_history[state->cur_sample] =
1456 temp - DRIVES_PID_INPUT_TARGET;
1458 state->first = 0;
1461 /* Calculate the integral term */
1462 sum = 0;
1463 integral = 0;
1464 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1465 integral += state->error_history[i];
1466 integral *= DRIVES_PID_INTERVAL;
1467 DBG(" integral: %08x\n", integral);
1468 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1469 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1470 sum += integ_p;
1472 /* Calculate the derivative term */
1473 derivative = state->error_history[state->cur_sample] -
1474 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1475 % DRIVES_PID_HISTORY_SIZE];
1476 derivative /= DRIVES_PID_INTERVAL;
1477 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1478 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1479 sum += deriv_p;
1481 /* Calculate the proportional term */
1482 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1483 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1484 sum += prop_p;
1486 /* Scale sum */
1487 sum >>= 36;
1489 DBG(" sum: %d\n", (int)sum);
1490 state->rpm += (s32)sum;
1492 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1493 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1495 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1496 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1500 * Initialize the state structure for the drives bay fan control loop
1502 static int init_drives_state(struct drives_pid_state *state)
1504 int err;
1506 state->ticks = 1;
1507 state->first = 1;
1508 state->rpm = 1000;
1510 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1511 if (state->monitor == NULL)
1512 return -ENODEV;
1514 err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1515 err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1516 if (err)
1517 printk(KERN_WARNING "Failed to create attribute file(s)"
1518 " for drives bay fan\n");
1520 return 0;
1524 * Dispose of the state data for the drives control loop
1526 static void dispose_drives_state(struct drives_pid_state *state)
1528 if (state->monitor == NULL)
1529 return;
1531 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1532 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1534 detach_i2c_chip(state->monitor);
1535 state->monitor = NULL;
1539 * DIMMs temp control loop
1541 static void do_monitor_dimms(struct dimm_pid_state *state)
1543 s32 temp, integral, derivative, fan_min;
1544 s64 integ_p, deriv_p, prop_p, sum;
1545 int i;
1547 if (--state->ticks != 0)
1548 return;
1549 state->ticks = DIMM_PID_INTERVAL;
1551 DBG("DIMM:\n");
1553 DBG(" current value: %d\n", state->output);
1555 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1556 if (temp < 0)
1557 return;
1558 temp <<= 16;
1559 state->last_temp = temp;
1560 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1561 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1563 /* Store temperature and error in history array */
1564 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1565 state->sample_history[state->cur_sample] = temp;
1566 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1568 /* If first loop, fill the history table */
1569 if (state->first) {
1570 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1571 state->cur_sample = (state->cur_sample + 1) %
1572 DIMM_PID_HISTORY_SIZE;
1573 state->sample_history[state->cur_sample] = temp;
1574 state->error_history[state->cur_sample] =
1575 temp - DIMM_PID_INPUT_TARGET;
1577 state->first = 0;
1580 /* Calculate the integral term */
1581 sum = 0;
1582 integral = 0;
1583 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1584 integral += state->error_history[i];
1585 integral *= DIMM_PID_INTERVAL;
1586 DBG(" integral: %08x\n", integral);
1587 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1588 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1589 sum += integ_p;
1591 /* Calculate the derivative term */
1592 derivative = state->error_history[state->cur_sample] -
1593 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1594 % DIMM_PID_HISTORY_SIZE];
1595 derivative /= DIMM_PID_INTERVAL;
1596 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1597 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1598 sum += deriv_p;
1600 /* Calculate the proportional term */
1601 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1602 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1603 sum += prop_p;
1605 /* Scale sum */
1606 sum >>= 36;
1608 DBG(" sum: %d\n", (int)sum);
1609 state->output = (s32)sum;
1610 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1611 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1612 dimm_output_clamp = state->output;
1614 DBG("** DIMM clamp value: %d\n", (int)state->output);
1616 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1617 fan_min = (dimm_output_clamp * 100) / 14000;
1618 fan_min = max(fan_min, backside_params.output_min);
1619 if (backside_state.pwm < fan_min) {
1620 backside_state.pwm = fan_min;
1621 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1622 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1627 * Initialize the state structure for the DIMM temp control loop
1629 static int init_dimms_state(struct dimm_pid_state *state)
1631 state->ticks = 1;
1632 state->first = 1;
1633 state->output = 4000;
1635 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1636 if (state->monitor == NULL)
1637 return -ENODEV;
1639 if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1640 printk(KERN_WARNING "Failed to create attribute file"
1641 " for DIMM temperature\n");
1643 return 0;
1647 * Dispose of the state data for the DIMM control loop
1649 static void dispose_dimms_state(struct dimm_pid_state *state)
1651 if (state->monitor == NULL)
1652 return;
1654 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1656 detach_i2c_chip(state->monitor);
1657 state->monitor = NULL;
1661 * Slots fan control loop
1663 static void do_monitor_slots(struct slots_pid_state *state)
1665 s32 temp, integral, derivative;
1666 s64 integ_p, deriv_p, prop_p, sum;
1667 int i, rc;
1669 if (--state->ticks != 0)
1670 return;
1671 state->ticks = SLOTS_PID_INTERVAL;
1673 DBG("slots:\n");
1675 /* Check fan status */
1676 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1677 if (rc < 0) {
1678 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1679 /* XXX What do we do now ? */
1680 } else
1681 state->pwm = rc;
1682 DBG(" current pwm: %d\n", state->pwm);
1684 /* Get some sensor readings */
1685 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1686 DS1775_TEMP)) << 8;
1687 state->last_temp = temp;
1688 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1689 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1691 /* Store temperature and error in history array */
1692 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1693 state->sample_history[state->cur_sample] = temp;
1694 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1696 /* If first loop, fill the history table */
1697 if (state->first) {
1698 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1699 state->cur_sample = (state->cur_sample + 1) %
1700 SLOTS_PID_HISTORY_SIZE;
1701 state->sample_history[state->cur_sample] = temp;
1702 state->error_history[state->cur_sample] =
1703 temp - SLOTS_PID_INPUT_TARGET;
1705 state->first = 0;
1708 /* Calculate the integral term */
1709 sum = 0;
1710 integral = 0;
1711 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1712 integral += state->error_history[i];
1713 integral *= SLOTS_PID_INTERVAL;
1714 DBG(" integral: %08x\n", integral);
1715 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1716 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1717 sum += integ_p;
1719 /* Calculate the derivative term */
1720 derivative = state->error_history[state->cur_sample] -
1721 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1722 % SLOTS_PID_HISTORY_SIZE];
1723 derivative /= SLOTS_PID_INTERVAL;
1724 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1725 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1726 sum += deriv_p;
1728 /* Calculate the proportional term */
1729 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1730 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1731 sum += prop_p;
1733 /* Scale sum */
1734 sum >>= 36;
1736 DBG(" sum: %d\n", (int)sum);
1737 state->pwm = (s32)sum;
1739 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1740 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1742 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1743 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1747 * Initialize the state structure for the slots bay fan control loop
1749 static int init_slots_state(struct slots_pid_state *state)
1751 int err;
1753 state->ticks = 1;
1754 state->first = 1;
1755 state->pwm = 50;
1757 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1758 if (state->monitor == NULL)
1759 return -ENODEV;
1761 err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1762 err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1763 if (err)
1764 printk(KERN_WARNING "Failed to create attribute file(s)"
1765 " for slots bay fan\n");
1767 return 0;
1771 * Dispose of the state data for the slots control loop
1773 static void dispose_slots_state(struct slots_pid_state *state)
1775 if (state->monitor == NULL)
1776 return;
1778 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1779 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1781 detach_i2c_chip(state->monitor);
1782 state->monitor = NULL;
1786 static int call_critical_overtemp(void)
1788 char *argv[] = { critical_overtemp_path, NULL };
1789 static char *envp[] = { "HOME=/",
1790 "TERM=linux",
1791 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1792 NULL };
1794 return call_usermodehelper(critical_overtemp_path,
1795 argv, envp, UMH_WAIT_EXEC);
1800 * Here's the kernel thread that calls the various control loops
1802 static int main_control_loop(void *x)
1804 DBG("main_control_loop started\n");
1806 down(&driver_lock);
1808 if (start_fcu() < 0) {
1809 printk(KERN_ERR "kfand: failed to start FCU\n");
1810 up(&driver_lock);
1811 goto out;
1814 /* Set the PCI fan once for now on non-RackMac */
1815 if (!rackmac)
1816 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1818 /* Initialize ADCs */
1819 initialize_adc(&cpu_state[0]);
1820 if (cpu_state[1].monitor != NULL)
1821 initialize_adc(&cpu_state[1]);
1823 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1825 up(&driver_lock);
1827 while (state == state_attached) {
1828 unsigned long elapsed, start;
1830 start = jiffies;
1832 down(&driver_lock);
1834 /* Tickle the FCU just in case */
1835 if (--fcu_tickle_ticks < 0) {
1836 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1837 tickle_fcu();
1840 /* First, we always calculate the new DIMMs state on an Xserve */
1841 if (rackmac)
1842 do_monitor_dimms(&dimms_state);
1844 /* Then, the CPUs */
1845 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1846 do_monitor_cpu_combined();
1847 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1848 do_monitor_cpu_rack(&cpu_state[0]);
1849 if (cpu_state[1].monitor != NULL)
1850 do_monitor_cpu_rack(&cpu_state[1]);
1851 // better deal with UP
1852 } else {
1853 do_monitor_cpu_split(&cpu_state[0]);
1854 if (cpu_state[1].monitor != NULL)
1855 do_monitor_cpu_split(&cpu_state[1]);
1856 // better deal with UP
1858 /* Then, the rest */
1859 do_monitor_backside(&backside_state);
1860 if (rackmac)
1861 do_monitor_slots(&slots_state);
1862 else
1863 do_monitor_drives(&drives_state);
1864 up(&driver_lock);
1866 if (critical_state == 1) {
1867 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1868 printk(KERN_WARNING "Attempting to shut down...\n");
1869 if (call_critical_overtemp()) {
1870 printk(KERN_WARNING "Can't call %s, power off now!\n",
1871 critical_overtemp_path);
1872 machine_power_off();
1875 if (critical_state > 0)
1876 critical_state++;
1877 if (critical_state > MAX_CRITICAL_STATE) {
1878 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1879 machine_power_off();
1882 // FIXME: Deal with signals
1883 elapsed = jiffies - start;
1884 if (elapsed < HZ)
1885 schedule_timeout_interruptible(HZ - elapsed);
1888 out:
1889 DBG("main_control_loop ended\n");
1891 ctrl_task = 0;
1892 complete_and_exit(&ctrl_complete, 0);
1896 * Dispose the control loops when tearing down
1898 static void dispose_control_loops(void)
1900 dispose_cpu_state(&cpu_state[0]);
1901 dispose_cpu_state(&cpu_state[1]);
1902 dispose_backside_state(&backside_state);
1903 dispose_drives_state(&drives_state);
1904 dispose_slots_state(&slots_state);
1905 dispose_dimms_state(&dimms_state);
1909 * Create the control loops. U3-0 i2c bus is up, so we can now
1910 * get to the various sensors
1912 static int create_control_loops(void)
1914 struct device_node *np;
1916 /* Count CPUs from the device-tree, we don't care how many are
1917 * actually used by Linux
1919 cpu_count = 0;
1920 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1921 cpu_count++;
1923 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1925 /* Decide the type of PID algorithm to use based on the presence of
1926 * the pumps, though that may not be the best way, that is good enough
1927 * for now
1929 if (rackmac)
1930 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1931 else if (machine_is_compatible("PowerMac7,3")
1932 && (cpu_count > 1)
1933 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1934 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1935 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1936 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1937 } else
1938 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1940 /* Create control loops for everything. If any fail, everything
1941 * fails
1943 if (init_cpu_state(&cpu_state[0], 0))
1944 goto fail;
1945 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1946 fetch_cpu_pumps_minmax();
1948 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1949 goto fail;
1950 if (init_backside_state(&backside_state))
1951 goto fail;
1952 if (rackmac && init_dimms_state(&dimms_state))
1953 goto fail;
1954 if (rackmac && init_slots_state(&slots_state))
1955 goto fail;
1956 if (!rackmac && init_drives_state(&drives_state))
1957 goto fail;
1959 DBG("all control loops up !\n");
1961 return 0;
1963 fail:
1964 DBG("failure creating control loops, disposing\n");
1966 dispose_control_loops();
1968 return -ENODEV;
1972 * Start the control loops after everything is up, that is create
1973 * the thread that will make them run
1975 static void start_control_loops(void)
1977 init_completion(&ctrl_complete);
1979 ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1983 * Stop the control loops when tearing down
1985 static void stop_control_loops(void)
1987 if (ctrl_task)
1988 wait_for_completion(&ctrl_complete);
1992 * Attach to the i2c FCU after detecting U3-1 bus
1994 static int attach_fcu(void)
1996 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1997 if (fcu == NULL)
1998 return -ENODEV;
2000 DBG("FCU attached\n");
2002 return 0;
2006 * Detach from the i2c FCU when tearing down
2008 static void detach_fcu(void)
2010 if (fcu)
2011 detach_i2c_chip(fcu);
2012 fcu = NULL;
2016 * Attach to the i2c controller. We probe the various chips based
2017 * on the device-tree nodes and build everything for the driver to
2018 * run, we then kick the driver monitoring thread
2020 static int therm_pm72_attach(struct i2c_adapter *adapter)
2022 down(&driver_lock);
2024 /* Check state */
2025 if (state == state_detached)
2026 state = state_attaching;
2027 if (state != state_attaching) {
2028 up(&driver_lock);
2029 return 0;
2032 /* Check if we are looking for one of these */
2033 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2034 u3_0 = adapter;
2035 DBG("found U3-0\n");
2036 if (k2 || !rackmac)
2037 if (create_control_loops())
2038 u3_0 = NULL;
2039 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2040 u3_1 = adapter;
2041 DBG("found U3-1, attaching FCU\n");
2042 if (attach_fcu())
2043 u3_1 = NULL;
2044 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2045 k2 = adapter;
2046 DBG("Found K2\n");
2047 if (u3_0 && rackmac)
2048 if (create_control_loops())
2049 k2 = NULL;
2051 /* We got all we need, start control loops */
2052 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2053 DBG("everything up, starting control loops\n");
2054 state = state_attached;
2055 start_control_loops();
2057 up(&driver_lock);
2059 return 0;
2063 * Called on every adapter when the driver or the i2c controller
2064 * is going away.
2066 static int therm_pm72_detach(struct i2c_adapter *adapter)
2068 down(&driver_lock);
2070 if (state != state_detached)
2071 state = state_detaching;
2073 /* Stop control loops if any */
2074 DBG("stopping control loops\n");
2075 up(&driver_lock);
2076 stop_control_loops();
2077 down(&driver_lock);
2079 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2080 DBG("lost U3-0, disposing control loops\n");
2081 dispose_control_loops();
2082 u3_0 = NULL;
2085 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2086 DBG("lost U3-1, detaching FCU\n");
2087 detach_fcu();
2088 u3_1 = NULL;
2090 if (u3_0 == NULL && u3_1 == NULL)
2091 state = state_detached;
2093 up(&driver_lock);
2095 return 0;
2098 static int fan_check_loc_match(const char *loc, int fan)
2100 char tmp[64];
2101 char *c, *e;
2103 strlcpy(tmp, fcu_fans[fan].loc, 64);
2105 c = tmp;
2106 for (;;) {
2107 e = strchr(c, ',');
2108 if (e)
2109 *e = 0;
2110 if (strcmp(loc, c) == 0)
2111 return 1;
2112 if (e == NULL)
2113 break;
2114 c = e + 1;
2116 return 0;
2119 static void fcu_lookup_fans(struct device_node *fcu_node)
2121 struct device_node *np = NULL;
2122 int i;
2124 /* The table is filled by default with values that are suitable
2125 * for the old machines without device-tree informations. We scan
2126 * the device-tree and override those values with whatever is
2127 * there
2130 DBG("Looking up FCU controls in device-tree...\n");
2132 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2133 int type = -1;
2134 const char *loc;
2135 const u32 *reg;
2137 DBG(" control: %s, type: %s\n", np->name, np->type);
2139 /* Detect control type */
2140 if (!strcmp(np->type, "fan-rpm-control") ||
2141 !strcmp(np->type, "fan-rpm"))
2142 type = FCU_FAN_RPM;
2143 if (!strcmp(np->type, "fan-pwm-control") ||
2144 !strcmp(np->type, "fan-pwm"))
2145 type = FCU_FAN_PWM;
2146 /* Only care about fans for now */
2147 if (type == -1)
2148 continue;
2150 /* Lookup for a matching location */
2151 loc = of_get_property(np, "location", NULL);
2152 reg = of_get_property(np, "reg", NULL);
2153 if (loc == NULL || reg == NULL)
2154 continue;
2155 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2157 for (i = 0; i < FCU_FAN_COUNT; i++) {
2158 int fan_id;
2160 if (!fan_check_loc_match(loc, i))
2161 continue;
2162 DBG(" location match, index: %d\n", i);
2163 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2164 if (type != fcu_fans[i].type) {
2165 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2166 "in device-tree for %s\n", np->full_name);
2167 break;
2169 if (type == FCU_FAN_RPM)
2170 fan_id = ((*reg) - 0x10) / 2;
2171 else
2172 fan_id = ((*reg) - 0x30) / 2;
2173 if (fan_id > 7) {
2174 printk(KERN_WARNING "therm_pm72: Can't parse "
2175 "fan ID in device-tree for %s\n", np->full_name);
2176 break;
2178 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2179 fcu_fans[i].id = fan_id;
2183 /* Now dump the array */
2184 printk(KERN_INFO "Detected fan controls:\n");
2185 for (i = 0; i < FCU_FAN_COUNT; i++) {
2186 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2187 continue;
2188 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2189 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2190 fcu_fans[i].id, fcu_fans[i].loc);
2194 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
2196 state = state_detached;
2198 /* Lookup the fans in the device tree */
2199 fcu_lookup_fans(dev->node);
2201 /* Add the driver */
2202 return i2c_add_driver(&therm_pm72_driver);
2205 static int fcu_of_remove(struct of_device* dev)
2207 i2c_del_driver(&therm_pm72_driver);
2209 return 0;
2212 static struct of_device_id fcu_match[] =
2215 .type = "fcu",
2220 static struct of_platform_driver fcu_of_platform_driver =
2222 .name = "temperature",
2223 .match_table = fcu_match,
2224 .probe = fcu_of_probe,
2225 .remove = fcu_of_remove
2229 * Check machine type, attach to i2c controller
2231 static int __init therm_pm72_init(void)
2233 struct device_node *np;
2235 rackmac = machine_is_compatible("RackMac3,1");
2237 if (!machine_is_compatible("PowerMac7,2") &&
2238 !machine_is_compatible("PowerMac7,3") &&
2239 !rackmac)
2240 return -ENODEV;
2242 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2244 np = of_find_node_by_type(NULL, "fcu");
2245 if (np == NULL) {
2246 /* Some machines have strangely broken device-tree */
2247 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2248 if (np == NULL) {
2249 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2250 return -ENODEV;
2253 of_dev = of_platform_device_create(np, "temperature", NULL);
2254 if (of_dev == NULL) {
2255 printk(KERN_ERR "Can't register FCU platform device !\n");
2256 return -ENODEV;
2259 of_register_platform_driver(&fcu_of_platform_driver);
2261 return 0;
2264 static void __exit therm_pm72_exit(void)
2266 of_unregister_platform_driver(&fcu_of_platform_driver);
2268 if (of_dev)
2269 of_device_unregister(of_dev);
2272 module_init(therm_pm72_init);
2273 module_exit(therm_pm72_exit);
2275 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2276 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2277 MODULE_LICENSE("GPL");