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>
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
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
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
62 * - Read fan speed from FCU, low level fan routines now deal
63 * with errors & check fan status, though higher level don't
65 * - Move a bunch of definitions to .h file
68 * - Fix build on ppc64 kernel
69 * - Move back statics definitions to .c file
70 * - Avoid calling schedule_timeout with a negative number
73 * - Fix typo when reading back fan speed on 2 CPU machines
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
84 * - Add device-tree lookup for fan IDs, should detect liquid cooling
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
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>
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"
142 #define DBG(args...) printk(args)
144 #define DBG(args...) do { } while(0)
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;
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;
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
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",
211 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
213 [DRIVES_FAN_RPM_INDEX] = {
216 .id = DRIVES_FAN_RPM_DEFAULT_ID,
218 [SLOTS_FAN_PWM_INDEX] = {
219 .loc = "SLOT,PCI FAN",
221 .id = SLOTS_FAN_PWM_DEFAULT_ID,
223 [CPUA_INTAKE_FAN_RPM_INDEX] = {
224 .loc = "CPU A INTAKE",
226 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
228 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
229 .loc = "CPU A EXHAUST",
231 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
233 [CPUB_INTAKE_FAN_RPM_INDEX] = {
234 .loc = "CPU B INTAKE",
236 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
238 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
239 .loc = "CPU B EXHAUST",
241 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
243 /* pumps aren't present by default, have to be looked up in the
246 [CPUA_PUMP_RPM_INDEX] = {
249 .id = FCU_FAN_ABSENT_ID,
251 [CPUB_PUMP_RPM_INDEX] = {
254 .id = FCU_FAN_ABSENT_ID,
257 [CPU_A1_FAN_RPM_INDEX] = {
260 .id = FCU_FAN_ABSENT_ID,
262 [CPU_A2_FAN_RPM_INDEX] = {
265 .id = FCU_FAN_ABSENT_ID,
267 [CPU_A3_FAN_RPM_INDEX] = {
270 .id = FCU_FAN_ABSENT_ID,
272 [CPU_B1_FAN_RPM_INDEX] = {
275 .id = FCU_FAN_ABSENT_ID,
277 [CPU_B2_FAN_RPM_INDEX] = {
280 .id = FCU_FAN_ABSENT_ID,
282 [CPU_B3_FAN_RPM_INDEX] = {
285 .id = FCU_FAN_ABSENT_ID,
289 static struct i2c_driver therm_pm72_driver;
292 * Utility function to create an i2c_client structure and
293 * attach it to one of u3 adapters
295 static struct i2c_client *attach_i2c_chip(int id, const char *name)
297 struct i2c_client *clt;
298 struct i2c_adapter *adap;
299 struct i2c_board_info info;
310 memset(&info, 0, sizeof(struct i2c_board_info));
311 info.addr = (id >> 1) & 0x7f;
312 strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
313 clt = i2c_new_device(adap, &info);
315 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
320 * Let i2c-core delete that device on driver removal.
321 * This is safe because i2c-core holds the core_lock mutex for us.
323 list_add_tail(&clt->detected, &therm_pm72_driver.clients);
328 * Here are the i2c chip access wrappers
331 static void initialize_adc(struct cpu_pid_state *state)
336 /* Read ADC the configuration register and cache it. We
337 * also make sure Config2 contains proper values, I've seen
338 * cases where we got stale grabage in there, thus preventing
339 * proper reading of conv. values
345 i2c_master_send(state->monitor, buf, 2);
347 /* Read & cache Config1 */
349 rc = i2c_master_send(state->monitor, buf, 1);
351 rc = i2c_master_recv(state->monitor, buf, 1);
353 state->adc_config = buf[0];
354 DBG("ADC config reg: %02x\n", state->adc_config);
355 /* Disable shutdown mode */
356 state->adc_config &= 0xfe;
358 buf[1] = state->adc_config;
359 rc = i2c_master_send(state->monitor, buf, 2);
363 printk(KERN_ERR "therm_pm72: Error reading ADC config"
367 static int read_smon_adc(struct cpu_pid_state *state, int chan)
369 int rc, data, tries = 0;
375 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
376 rc = i2c_master_send(state->monitor, buf, 2);
379 /* Wait for convertion */
381 /* Switch to data register */
383 rc = i2c_master_send(state->monitor, buf, 1);
387 rc = i2c_master_recv(state->monitor, buf, 2);
390 data = ((u16)buf[0]) << 8 | (u16)buf[1];
393 DBG("Error reading ADC, retrying...\n");
395 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
402 static int read_lm87_reg(struct i2c_client * chip, int reg)
410 rc = i2c_master_send(chip, &buf, 1);
413 rc = i2c_master_recv(chip, &buf, 1);
418 DBG("Error reading LM87, retrying...\n");
420 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
427 static int fan_read_reg(int reg, unsigned char *buf, int nb)
434 nw = i2c_master_send(fcu, buf, 1);
435 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
441 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
446 nr = i2c_master_recv(fcu, buf, nb);
447 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
453 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
457 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
460 unsigned char buf[16];
463 memcpy(buf+1, ptr, nb);
467 nw = i2c_master_send(fcu, buf, nb);
468 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
474 printk(KERN_ERR "Failure writing to FCU: %d", nw);
478 static int start_fcu(void)
480 unsigned char buf = 0xff;
483 rc = fan_write_reg(0xe, &buf, 1);
486 rc = fan_write_reg(0x2e, &buf, 1);
489 rc = fan_read_reg(0, &buf, 1);
492 fcu_rpm_shift = (buf == 1) ? 2 : 3;
493 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
499 static int set_rpm_fan(int fan_index, int rpm)
501 unsigned char buf[2];
502 int rc, id, min, max;
504 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
506 id = fcu_fans[fan_index].id;
507 if (id == FCU_FAN_ABSENT_ID)
510 min = 2400 >> fcu_rpm_shift;
511 max = 56000 >> fcu_rpm_shift;
517 buf[0] = rpm >> (8 - fcu_rpm_shift);
518 buf[1] = rpm << fcu_rpm_shift;
519 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
525 static int get_rpm_fan(int fan_index, int programmed)
527 unsigned char failure;
528 unsigned char active;
529 unsigned char buf[2];
530 int rc, id, reg_base;
532 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
534 id = fcu_fans[fan_index].id;
535 if (id == FCU_FAN_ABSENT_ID)
538 rc = fan_read_reg(0xb, &failure, 1);
541 if ((failure & (1 << id)) != 0)
543 rc = fan_read_reg(0xd, &active, 1);
546 if ((active & (1 << id)) == 0)
549 /* Programmed value or real current speed */
550 reg_base = programmed ? 0x10 : 0x11;
551 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
555 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
558 static int set_pwm_fan(int fan_index, int pwm)
560 unsigned char buf[2];
563 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
565 id = fcu_fans[fan_index].id;
566 if (id == FCU_FAN_ABSENT_ID)
573 pwm = (pwm * 2559) / 1000;
575 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
581 static int get_pwm_fan(int fan_index)
583 unsigned char failure;
584 unsigned char active;
585 unsigned char buf[2];
588 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
590 id = fcu_fans[fan_index].id;
591 if (id == FCU_FAN_ABSENT_ID)
594 rc = fan_read_reg(0x2b, &failure, 1);
597 if ((failure & (1 << id)) != 0)
599 rc = fan_read_reg(0x2d, &active, 1);
602 if ((active & (1 << id)) == 0)
605 /* Programmed value or real current speed */
606 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
610 return (buf[0] * 1000) / 2559;
613 static void tickle_fcu(void)
617 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
619 DBG("FCU Tickle, slots fan is: %d\n", pwm);
624 pwm = SLOTS_FAN_DEFAULT_PWM;
625 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
626 pwm = SLOTS_PID_OUTPUT_MIN;
628 /* That is hopefully enough to make the FCU happy */
629 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
634 * Utility routine to read the CPU calibration EEPROM data
635 * from the device-tree
637 static int read_eeprom(int cpu, struct mpu_data *out)
639 struct device_node *np;
644 /* prom.c routine for finding a node by path is a bit brain dead
645 * and requires exact @xxx unit numbers. This is a bit ugly but
646 * will work for these machines
648 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
649 np = of_find_node_by_path(nodename);
651 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
654 data = of_get_property(np, "cpuid", &len);
656 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
660 memcpy(out, data, sizeof(struct mpu_data));
666 static void fetch_cpu_pumps_minmax(void)
668 struct cpu_pid_state *state0 = &cpu_state[0];
669 struct cpu_pid_state *state1 = &cpu_state[1];
670 u16 pump_min = 0, pump_max = 0xffff;
673 /* Try to fetch pumps min/max infos from eeprom */
675 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
676 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
677 pump_min = max(pump_min, tmp[0]);
678 pump_max = min(pump_max, tmp[1]);
680 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
681 pump_min = max(pump_min, tmp[2]);
682 pump_max = min(pump_max, tmp[3]);
685 /* Double check the values, this _IS_ needed as the EEPROM on
686 * some dual 2.5Ghz G5s seem, at least, to have both min & max
687 * same to the same value ... (grrrr)
689 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
690 pump_min = CPU_PUMP_OUTPUT_MIN;
691 pump_max = CPU_PUMP_OUTPUT_MAX;
694 state0->pump_min = state1->pump_min = pump_min;
695 state0->pump_max = state1->pump_max = pump_max;
699 * Now, unfortunately, sysfs doesn't give us a nice void * we could
700 * pass around to the attribute functions, so we don't really have
701 * choice but implement a bunch of them...
703 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
704 * the input twice... I accept patches :)
706 #define BUILD_SHOW_FUNC_FIX(name, data) \
707 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
710 mutex_lock(&driver_lock); \
711 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
712 mutex_unlock(&driver_lock); \
715 #define BUILD_SHOW_FUNC_INT(name, data) \
716 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
718 return sprintf(buf, "%d", data); \
721 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
722 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
723 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
724 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
725 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
727 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
728 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
729 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
730 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
731 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
733 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
734 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
736 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
737 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
739 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
740 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
742 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
744 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
745 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
746 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
747 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
748 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
750 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
751 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
752 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
753 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
754 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
756 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
757 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
759 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
760 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
762 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
763 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
765 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
768 * CPUs fans control loop
771 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
773 s32 ltemp, volts, amps;
776 /* Default (in case of error) */
777 *temp = state->cur_temp;
778 *power = state->cur_power;
780 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
781 index = (state->index == 0) ?
782 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
784 index = (state->index == 0) ?
785 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
787 /* Read current fan status */
788 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
790 /* XXX What do we do now ? Nothing for now, keep old value, but
791 * return error upstream
793 DBG(" cpu %d, fan reading error !\n", state->index);
796 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
799 /* Get some sensor readings and scale it */
800 ltemp = read_smon_adc(state, 1);
802 /* XXX What do we do now ? */
806 DBG(" cpu %d, temp reading error !\n", state->index);
808 /* Fixup temperature according to diode calibration
810 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
812 ltemp, state->mpu.mdiode, state->mpu.bdiode);
813 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
814 state->last_temp = *temp;
815 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
819 * Read voltage & current and calculate power
821 volts = read_smon_adc(state, 3);
822 amps = read_smon_adc(state, 4);
824 /* Scale voltage and current raw sensor values according to fixed scales
825 * obtained in Darwin and calculate power from I and V
827 volts *= ADC_CPU_VOLTAGE_SCALE;
828 amps *= ADC_CPU_CURRENT_SCALE;
829 *power = (((u64)volts) * ((u64)amps)) >> 16;
830 state->voltage = volts;
831 state->current_a = amps;
832 state->last_power = *power;
834 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
835 state->index, FIX32TOPRINT(state->current_a),
836 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
841 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
843 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
844 s64 integ_p, deriv_p, prop_p, sum;
847 /* Calculate power target value (could be done once for all)
848 * and convert to a 16.16 fp number
850 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
851 DBG(" power target: %d.%03d, error: %d.%03d\n",
852 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
854 /* Store temperature and power in history array */
855 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
856 state->temp_history[state->cur_temp] = temp;
857 state->cur_power = (state->cur_power + 1) % state->count_power;
858 state->power_history[state->cur_power] = power;
859 state->error_history[state->cur_power] = power_target - power;
861 /* If first loop, fill the history table */
863 for (i = 0; i < (state->count_power - 1); i++) {
864 state->cur_power = (state->cur_power + 1) % state->count_power;
865 state->power_history[state->cur_power] = power;
866 state->error_history[state->cur_power] = power_target - power;
868 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
869 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
870 state->temp_history[state->cur_temp] = temp;
875 /* Calculate the integral term normally based on the "power" values */
878 for (i = 0; i < state->count_power; i++)
879 integral += state->error_history[i];
880 integral *= CPU_PID_INTERVAL;
881 DBG(" integral: %08x\n", integral);
883 /* Calculate the adjusted input (sense value).
886 * so the result is 28.36
888 * input target is mpu.ttarget, input max is mpu.tmax
890 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
891 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
892 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
893 adj_in_target = (state->mpu.ttarget << 16);
894 if (adj_in_target > sval)
895 adj_in_target = sval;
896 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
899 /* Calculate the derivative term */
900 derivative = state->temp_history[state->cur_temp] -
901 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
902 % CPU_TEMP_HISTORY_SIZE];
903 derivative /= CPU_PID_INTERVAL;
904 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
905 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
908 /* Calculate the proportional term */
909 proportional = temp - adj_in_target;
910 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
911 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
917 DBG(" sum: %d\n", (int)sum);
918 state->rpm += (s32)sum;
921 static void do_monitor_cpu_combined(void)
923 struct cpu_pid_state *state0 = &cpu_state[0];
924 struct cpu_pid_state *state1 = &cpu_state[1];
925 s32 temp0, power0, temp1, power1;
926 s32 temp_combi, power_combi;
927 int rc, intake, pump;
929 rc = do_read_one_cpu_values(state0, &temp0, &power0);
931 /* XXX What do we do now ? */
933 state1->overtemp = 0;
934 rc = do_read_one_cpu_values(state1, &temp1, &power1);
936 /* XXX What do we do now ? */
938 if (state1->overtemp)
941 temp_combi = max(temp0, temp1);
942 power_combi = max(power0, power1);
944 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
945 * full blown immediately and try to trigger a shutdown
947 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
948 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
950 state0->overtemp += CPU_MAX_OVERTEMP / 4;
951 } else if (temp_combi > (state0->mpu.tmax << 16)) {
953 printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
954 temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
956 if (state0->overtemp)
957 printk(KERN_WARNING "Temperature back down to %d\n",
959 state0->overtemp = 0;
961 if (state0->overtemp >= CPU_MAX_OVERTEMP)
963 if (state0->overtemp > 0) {
964 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
965 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
966 pump = state0->pump_max;
971 do_cpu_pid(state0, temp_combi, power_combi);
974 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
975 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
977 /* Calculate intake fan speed */
978 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
979 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
980 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
981 state0->intake_rpm = intake;
983 /* Calculate pump speed */
984 pump = (state0->rpm * state0->pump_max) /
985 state0->mpu.rmaxn_exhaust_fan;
986 pump = min(pump, state0->pump_max);
987 pump = max(pump, state0->pump_min);
990 /* We copy values from state 0 to state 1 for /sysfs */
991 state1->rpm = state0->rpm;
992 state1->intake_rpm = state0->intake_rpm;
994 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
995 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
997 /* We should check for errors, shouldn't we ? But then, what
998 * do we do once the error occurs ? For FCU notified fan
999 * failures (-EFAULT) we probably want to notify userland
1002 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1003 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1004 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1005 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1007 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1008 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1009 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1010 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1013 static void do_monitor_cpu_split(struct cpu_pid_state *state)
1018 /* Read current fan status */
1019 rc = do_read_one_cpu_values(state, &temp, &power);
1021 /* XXX What do we do now ? */
1024 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1025 * full blown immediately and try to trigger a shutdown
1027 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1028 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1030 state->index, temp >> 16);
1031 state->overtemp += CPU_MAX_OVERTEMP / 4;
1032 } else if (temp > (state->mpu.tmax << 16)) {
1034 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1035 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1037 if (state->overtemp)
1038 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1039 state->index, temp >> 16);
1040 state->overtemp = 0;
1042 if (state->overtemp >= CPU_MAX_OVERTEMP)
1044 if (state->overtemp > 0) {
1045 state->rpm = state->mpu.rmaxn_exhaust_fan;
1046 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1051 do_cpu_pid(state, temp, power);
1054 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1055 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1057 /* Calculate intake fan */
1058 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1059 intake = max(intake, (int)state->mpu.rminn_intake_fan);
1060 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1061 state->intake_rpm = intake;
1064 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1065 state->index, (int)state->rpm, intake, state->overtemp);
1067 /* We should check for errors, shouldn't we ? But then, what
1068 * do we do once the error occurs ? For FCU notified fan
1069 * failures (-EFAULT) we probably want to notify userland
1072 if (state->index == 0) {
1073 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1074 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1076 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1077 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1081 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1083 s32 temp, power, fan_min;
1086 /* Read current fan status */
1087 rc = do_read_one_cpu_values(state, &temp, &power);
1089 /* XXX What do we do now ? */
1092 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1093 * full blown immediately and try to trigger a shutdown
1095 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1096 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1098 state->index, temp >> 16);
1099 state->overtemp = CPU_MAX_OVERTEMP / 4;
1100 } else if (temp > (state->mpu.tmax << 16)) {
1102 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1103 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1105 if (state->overtemp)
1106 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1107 state->index, temp >> 16);
1108 state->overtemp = 0;
1110 if (state->overtemp >= CPU_MAX_OVERTEMP)
1112 if (state->overtemp > 0) {
1113 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1118 do_cpu_pid(state, temp, power);
1120 /* Check clamp from dimms */
1121 fan_min = dimm_output_clamp;
1122 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1124 DBG(" CPU min mpu = %d, min dimm = %d\n",
1125 state->mpu.rminn_intake_fan, dimm_output_clamp);
1127 state->rpm = max(state->rpm, (int)fan_min);
1128 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1129 state->intake_rpm = state->rpm;
1132 DBG("** CPU %d RPM: %d overtemp: %d\n",
1133 state->index, (int)state->rpm, state->overtemp);
1135 /* We should check for errors, shouldn't we ? But then, what
1136 * do we do once the error occurs ? For FCU notified fan
1137 * failures (-EFAULT) we probably want to notify userland
1140 if (state->index == 0) {
1141 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1142 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1143 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1145 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1146 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1147 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1152 * Initialize the state structure for one CPU control loop
1154 static int init_cpu_state(struct cpu_pid_state *state, int index)
1158 state->index = index;
1160 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1161 state->overtemp = 0;
1162 state->adc_config = 0x00;
1166 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1167 else if (index == 1)
1168 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1169 if (state->monitor == NULL)
1172 if (read_eeprom(index, &state->mpu))
1175 state->count_power = state->mpu.tguardband;
1176 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1177 printk(KERN_WARNING "Warning ! too many power history slots\n");
1178 state->count_power = CPU_POWER_HISTORY_SIZE;
1180 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1183 err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1184 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1185 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1186 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1187 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1189 err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1190 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1191 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1192 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1193 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1196 printk(KERN_WARNING "Failed to create some of the atribute"
1197 "files for CPU %d\n", index);
1201 state->monitor = NULL;
1207 * Dispose of the state data for one CPU control loop
1209 static void dispose_cpu_state(struct cpu_pid_state *state)
1211 if (state->monitor == NULL)
1214 if (state->index == 0) {
1215 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1216 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1217 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1218 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1219 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1221 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1222 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1223 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1224 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1225 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1228 state->monitor = NULL;
1232 * Motherboard backside & U3 heatsink fan control loop
1234 static void do_monitor_backside(struct backside_pid_state *state)
1236 s32 temp, integral, derivative, fan_min;
1237 s64 integ_p, deriv_p, prop_p, sum;
1240 if (--state->ticks != 0)
1242 state->ticks = backside_params.interval;
1246 /* Check fan status */
1247 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1249 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1250 /* XXX What do we do now ? */
1253 DBG(" current pwm: %d\n", state->pwm);
1255 /* Get some sensor readings */
1256 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1257 state->last_temp = temp;
1258 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1259 FIX32TOPRINT(backside_params.input_target));
1261 /* Store temperature and error in history array */
1262 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1263 state->sample_history[state->cur_sample] = temp;
1264 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1266 /* If first loop, fill the history table */
1268 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1269 state->cur_sample = (state->cur_sample + 1) %
1270 BACKSIDE_PID_HISTORY_SIZE;
1271 state->sample_history[state->cur_sample] = temp;
1272 state->error_history[state->cur_sample] =
1273 temp - backside_params.input_target;
1278 /* Calculate the integral term */
1281 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1282 integral += state->error_history[i];
1283 integral *= backside_params.interval;
1284 DBG(" integral: %08x\n", integral);
1285 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1286 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1289 /* Calculate the derivative term */
1290 derivative = state->error_history[state->cur_sample] -
1291 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1292 % BACKSIDE_PID_HISTORY_SIZE];
1293 derivative /= backside_params.interval;
1294 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1295 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1298 /* Calculate the proportional term */
1299 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1300 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1306 DBG(" sum: %d\n", (int)sum);
1307 if (backside_params.additive)
1308 state->pwm += (s32)sum;
1312 /* Check for clamp */
1313 fan_min = (dimm_output_clamp * 100) / 14000;
1314 fan_min = max(fan_min, backside_params.output_min);
1316 state->pwm = max(state->pwm, fan_min);
1317 state->pwm = min(state->pwm, backside_params.output_max);
1319 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1320 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1324 * Initialize the state structure for the backside fan control loop
1326 static int init_backside_state(struct backside_pid_state *state)
1328 struct device_node *u3;
1329 int u3h = 1; /* conservative by default */
1333 * There are different PID params for machines with U3 and machines
1334 * with U3H, pick the right ones now
1336 u3 = of_find_node_by_path("/u3@0,f8000000");
1338 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1340 if (((*vers) & 0x3f) < 0x34)
1346 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1347 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1348 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1349 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1350 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1351 backside_params.G_r = BACKSIDE_PID_G_r;
1352 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1353 backside_params.additive = 0;
1355 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1356 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1357 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1358 backside_params.interval = BACKSIDE_PID_INTERVAL;
1359 backside_params.G_p = BACKSIDE_PID_G_p;
1360 backside_params.G_r = BACKSIDE_PID_G_r;
1361 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1362 backside_params.additive = 1;
1364 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1365 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1366 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1367 backside_params.interval = BACKSIDE_PID_INTERVAL;
1368 backside_params.G_p = BACKSIDE_PID_G_p;
1369 backside_params.G_r = BACKSIDE_PID_G_r;
1370 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1371 backside_params.additive = 1;
1378 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1379 if (state->monitor == NULL)
1382 err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1383 err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1385 printk(KERN_WARNING "Failed to create attribute file(s)"
1386 " for backside fan\n");
1392 * Dispose of the state data for the backside control loop
1394 static void dispose_backside_state(struct backside_pid_state *state)
1396 if (state->monitor == NULL)
1399 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1400 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1402 state->monitor = NULL;
1406 * Drives bay fan control loop
1408 static void do_monitor_drives(struct drives_pid_state *state)
1410 s32 temp, integral, derivative;
1411 s64 integ_p, deriv_p, prop_p, sum;
1414 if (--state->ticks != 0)
1416 state->ticks = DRIVES_PID_INTERVAL;
1420 /* Check fan status */
1421 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1423 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1424 /* XXX What do we do now ? */
1427 DBG(" current rpm: %d\n", state->rpm);
1429 /* Get some sensor readings */
1430 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1432 state->last_temp = temp;
1433 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1434 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1436 /* Store temperature and error in history array */
1437 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1438 state->sample_history[state->cur_sample] = temp;
1439 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1441 /* If first loop, fill the history table */
1443 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1444 state->cur_sample = (state->cur_sample + 1) %
1445 DRIVES_PID_HISTORY_SIZE;
1446 state->sample_history[state->cur_sample] = temp;
1447 state->error_history[state->cur_sample] =
1448 temp - DRIVES_PID_INPUT_TARGET;
1453 /* Calculate the integral term */
1456 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1457 integral += state->error_history[i];
1458 integral *= DRIVES_PID_INTERVAL;
1459 DBG(" integral: %08x\n", integral);
1460 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1461 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1464 /* Calculate the derivative term */
1465 derivative = state->error_history[state->cur_sample] -
1466 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1467 % DRIVES_PID_HISTORY_SIZE];
1468 derivative /= DRIVES_PID_INTERVAL;
1469 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1470 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1473 /* Calculate the proportional term */
1474 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1475 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1481 DBG(" sum: %d\n", (int)sum);
1482 state->rpm += (s32)sum;
1484 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1485 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1487 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1488 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1492 * Initialize the state structure for the drives bay fan control loop
1494 static int init_drives_state(struct drives_pid_state *state)
1502 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1503 if (state->monitor == NULL)
1506 err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1507 err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1509 printk(KERN_WARNING "Failed to create attribute file(s)"
1510 " for drives bay fan\n");
1516 * Dispose of the state data for the drives control loop
1518 static void dispose_drives_state(struct drives_pid_state *state)
1520 if (state->monitor == NULL)
1523 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1524 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1526 state->monitor = NULL;
1530 * DIMMs temp control loop
1532 static void do_monitor_dimms(struct dimm_pid_state *state)
1534 s32 temp, integral, derivative, fan_min;
1535 s64 integ_p, deriv_p, prop_p, sum;
1538 if (--state->ticks != 0)
1540 state->ticks = DIMM_PID_INTERVAL;
1544 DBG(" current value: %d\n", state->output);
1546 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1550 state->last_temp = temp;
1551 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1552 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1554 /* Store temperature and error in history array */
1555 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1556 state->sample_history[state->cur_sample] = temp;
1557 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1559 /* If first loop, fill the history table */
1561 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1562 state->cur_sample = (state->cur_sample + 1) %
1563 DIMM_PID_HISTORY_SIZE;
1564 state->sample_history[state->cur_sample] = temp;
1565 state->error_history[state->cur_sample] =
1566 temp - DIMM_PID_INPUT_TARGET;
1571 /* Calculate the integral term */
1574 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1575 integral += state->error_history[i];
1576 integral *= DIMM_PID_INTERVAL;
1577 DBG(" integral: %08x\n", integral);
1578 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1579 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1582 /* Calculate the derivative term */
1583 derivative = state->error_history[state->cur_sample] -
1584 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1585 % DIMM_PID_HISTORY_SIZE];
1586 derivative /= DIMM_PID_INTERVAL;
1587 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1588 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1591 /* Calculate the proportional term */
1592 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1593 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1599 DBG(" sum: %d\n", (int)sum);
1600 state->output = (s32)sum;
1601 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1602 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1603 dimm_output_clamp = state->output;
1605 DBG("** DIMM clamp value: %d\n", (int)state->output);
1607 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1608 fan_min = (dimm_output_clamp * 100) / 14000;
1609 fan_min = max(fan_min, backside_params.output_min);
1610 if (backside_state.pwm < fan_min) {
1611 backside_state.pwm = fan_min;
1612 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1613 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1618 * Initialize the state structure for the DIMM temp control loop
1620 static int init_dimms_state(struct dimm_pid_state *state)
1624 state->output = 4000;
1626 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1627 if (state->monitor == NULL)
1630 if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1631 printk(KERN_WARNING "Failed to create attribute file"
1632 " for DIMM temperature\n");
1638 * Dispose of the state data for the DIMM control loop
1640 static void dispose_dimms_state(struct dimm_pid_state *state)
1642 if (state->monitor == NULL)
1645 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1647 state->monitor = NULL;
1651 * Slots fan control loop
1653 static void do_monitor_slots(struct slots_pid_state *state)
1655 s32 temp, integral, derivative;
1656 s64 integ_p, deriv_p, prop_p, sum;
1659 if (--state->ticks != 0)
1661 state->ticks = SLOTS_PID_INTERVAL;
1665 /* Check fan status */
1666 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1668 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1669 /* XXX What do we do now ? */
1672 DBG(" current pwm: %d\n", state->pwm);
1674 /* Get some sensor readings */
1675 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1677 state->last_temp = temp;
1678 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1679 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1681 /* Store temperature and error in history array */
1682 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1683 state->sample_history[state->cur_sample] = temp;
1684 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1686 /* If first loop, fill the history table */
1688 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1689 state->cur_sample = (state->cur_sample + 1) %
1690 SLOTS_PID_HISTORY_SIZE;
1691 state->sample_history[state->cur_sample] = temp;
1692 state->error_history[state->cur_sample] =
1693 temp - SLOTS_PID_INPUT_TARGET;
1698 /* Calculate the integral term */
1701 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1702 integral += state->error_history[i];
1703 integral *= SLOTS_PID_INTERVAL;
1704 DBG(" integral: %08x\n", integral);
1705 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1706 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1709 /* Calculate the derivative term */
1710 derivative = state->error_history[state->cur_sample] -
1711 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1712 % SLOTS_PID_HISTORY_SIZE];
1713 derivative /= SLOTS_PID_INTERVAL;
1714 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1715 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1718 /* Calculate the proportional term */
1719 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1720 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1726 DBG(" sum: %d\n", (int)sum);
1727 state->pwm = (s32)sum;
1729 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1730 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1732 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1733 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1737 * Initialize the state structure for the slots bay fan control loop
1739 static int init_slots_state(struct slots_pid_state *state)
1747 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1748 if (state->monitor == NULL)
1751 err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1752 err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1754 printk(KERN_WARNING "Failed to create attribute file(s)"
1755 " for slots bay fan\n");
1761 * Dispose of the state data for the slots control loop
1763 static void dispose_slots_state(struct slots_pid_state *state)
1765 if (state->monitor == NULL)
1768 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1769 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1771 state->monitor = NULL;
1775 static int call_critical_overtemp(void)
1777 char *argv[] = { critical_overtemp_path, NULL };
1778 static char *envp[] = { "HOME=/",
1780 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1783 return call_usermodehelper(critical_overtemp_path,
1784 argv, envp, UMH_WAIT_EXEC);
1789 * Here's the kernel thread that calls the various control loops
1791 static int main_control_loop(void *x)
1793 DBG("main_control_loop started\n");
1795 mutex_lock(&driver_lock);
1797 if (start_fcu() < 0) {
1798 printk(KERN_ERR "kfand: failed to start FCU\n");
1799 mutex_unlock(&driver_lock);
1803 /* Set the PCI fan once for now on non-RackMac */
1805 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1807 /* Initialize ADCs */
1808 initialize_adc(&cpu_state[0]);
1809 if (cpu_state[1].monitor != NULL)
1810 initialize_adc(&cpu_state[1]);
1812 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1814 mutex_unlock(&driver_lock);
1816 while (state == state_attached) {
1817 unsigned long elapsed, start;
1821 mutex_lock(&driver_lock);
1823 /* Tickle the FCU just in case */
1824 if (--fcu_tickle_ticks < 0) {
1825 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1829 /* First, we always calculate the new DIMMs state on an Xserve */
1831 do_monitor_dimms(&dimms_state);
1833 /* Then, the CPUs */
1834 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1835 do_monitor_cpu_combined();
1836 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1837 do_monitor_cpu_rack(&cpu_state[0]);
1838 if (cpu_state[1].monitor != NULL)
1839 do_monitor_cpu_rack(&cpu_state[1]);
1840 // better deal with UP
1842 do_monitor_cpu_split(&cpu_state[0]);
1843 if (cpu_state[1].monitor != NULL)
1844 do_monitor_cpu_split(&cpu_state[1]);
1845 // better deal with UP
1847 /* Then, the rest */
1848 do_monitor_backside(&backside_state);
1850 do_monitor_slots(&slots_state);
1852 do_monitor_drives(&drives_state);
1853 mutex_unlock(&driver_lock);
1855 if (critical_state == 1) {
1856 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1857 printk(KERN_WARNING "Attempting to shut down...\n");
1858 if (call_critical_overtemp()) {
1859 printk(KERN_WARNING "Can't call %s, power off now!\n",
1860 critical_overtemp_path);
1861 machine_power_off();
1864 if (critical_state > 0)
1866 if (critical_state > MAX_CRITICAL_STATE) {
1867 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1868 machine_power_off();
1871 // FIXME: Deal with signals
1872 elapsed = jiffies - start;
1874 schedule_timeout_interruptible(HZ - elapsed);
1878 DBG("main_control_loop ended\n");
1881 complete_and_exit(&ctrl_complete, 0);
1885 * Dispose the control loops when tearing down
1887 static void dispose_control_loops(void)
1889 dispose_cpu_state(&cpu_state[0]);
1890 dispose_cpu_state(&cpu_state[1]);
1891 dispose_backside_state(&backside_state);
1892 dispose_drives_state(&drives_state);
1893 dispose_slots_state(&slots_state);
1894 dispose_dimms_state(&dimms_state);
1898 * Create the control loops. U3-0 i2c bus is up, so we can now
1899 * get to the various sensors
1901 static int create_control_loops(void)
1903 struct device_node *np;
1905 /* Count CPUs from the device-tree, we don't care how many are
1906 * actually used by Linux
1909 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1912 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1914 /* Decide the type of PID algorithm to use based on the presence of
1915 * the pumps, though that may not be the best way, that is good enough
1919 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1920 else if (of_machine_is_compatible("PowerMac7,3")
1922 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1923 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1924 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1925 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1927 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1929 /* Create control loops for everything. If any fail, everything
1932 if (init_cpu_state(&cpu_state[0], 0))
1934 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1935 fetch_cpu_pumps_minmax();
1937 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1939 if (init_backside_state(&backside_state))
1941 if (rackmac && init_dimms_state(&dimms_state))
1943 if (rackmac && init_slots_state(&slots_state))
1945 if (!rackmac && init_drives_state(&drives_state))
1948 DBG("all control loops up !\n");
1953 DBG("failure creating control loops, disposing\n");
1955 dispose_control_loops();
1961 * Start the control loops after everything is up, that is create
1962 * the thread that will make them run
1964 static void start_control_loops(void)
1966 init_completion(&ctrl_complete);
1968 ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1972 * Stop the control loops when tearing down
1974 static void stop_control_loops(void)
1977 wait_for_completion(&ctrl_complete);
1981 * Attach to the i2c FCU after detecting U3-1 bus
1983 static int attach_fcu(void)
1985 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1989 DBG("FCU attached\n");
1995 * Detach from the i2c FCU when tearing down
1997 static void detach_fcu(void)
2003 * Attach to the i2c controller. We probe the various chips based
2004 * on the device-tree nodes and build everything for the driver to
2005 * run, we then kick the driver monitoring thread
2007 static int therm_pm72_attach(struct i2c_adapter *adapter)
2009 mutex_lock(&driver_lock);
2012 if (state == state_detached)
2013 state = state_attaching;
2014 if (state != state_attaching) {
2015 mutex_unlock(&driver_lock);
2019 /* Check if we are looking for one of these */
2020 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2022 DBG("found U3-0\n");
2024 if (create_control_loops())
2026 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2028 DBG("found U3-1, attaching FCU\n");
2031 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2034 if (u3_0 && rackmac)
2035 if (create_control_loops())
2038 /* We got all we need, start control loops */
2039 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2040 DBG("everything up, starting control loops\n");
2041 state = state_attached;
2042 start_control_loops();
2044 mutex_unlock(&driver_lock);
2049 static int therm_pm72_probe(struct i2c_client *client,
2050 const struct i2c_device_id *id)
2052 /* Always succeed, the real work was done in therm_pm72_attach() */
2057 * Called when any of the devices which participates into thermal management
2060 static int therm_pm72_remove(struct i2c_client *client)
2062 struct i2c_adapter *adapter = client->adapter;
2064 mutex_lock(&driver_lock);
2066 if (state != state_detached)
2067 state = state_detaching;
2069 /* Stop control loops if any */
2070 DBG("stopping control loops\n");
2071 mutex_unlock(&driver_lock);
2072 stop_control_loops();
2073 mutex_lock(&driver_lock);
2075 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2076 DBG("lost U3-0, disposing control loops\n");
2077 dispose_control_loops();
2081 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2082 DBG("lost U3-1, detaching FCU\n");
2086 if (u3_0 == NULL && u3_1 == NULL)
2087 state = state_detached;
2089 mutex_unlock(&driver_lock);
2095 * i2c_driver structure to attach to the host i2c controller
2098 static const struct i2c_device_id therm_pm72_id[] = {
2100 * Fake device name, thermal management is done by several
2101 * chips but we don't need to differentiate between them at
2104 { "therm_pm72", 0 },
2108 static struct i2c_driver therm_pm72_driver = {
2110 .name = "therm_pm72",
2112 .attach_adapter = therm_pm72_attach,
2113 .probe = therm_pm72_probe,
2114 .remove = therm_pm72_remove,
2115 .id_table = therm_pm72_id,
2118 static int fan_check_loc_match(const char *loc, int fan)
2123 strlcpy(tmp, fcu_fans[fan].loc, 64);
2130 if (strcmp(loc, c) == 0)
2139 static void fcu_lookup_fans(struct device_node *fcu_node)
2141 struct device_node *np = NULL;
2144 /* The table is filled by default with values that are suitable
2145 * for the old machines without device-tree informations. We scan
2146 * the device-tree and override those values with whatever is
2150 DBG("Looking up FCU controls in device-tree...\n");
2152 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2157 DBG(" control: %s, type: %s\n", np->name, np->type);
2159 /* Detect control type */
2160 if (!strcmp(np->type, "fan-rpm-control") ||
2161 !strcmp(np->type, "fan-rpm"))
2163 if (!strcmp(np->type, "fan-pwm-control") ||
2164 !strcmp(np->type, "fan-pwm"))
2166 /* Only care about fans for now */
2170 /* Lookup for a matching location */
2171 loc = of_get_property(np, "location", NULL);
2172 reg = of_get_property(np, "reg", NULL);
2173 if (loc == NULL || reg == NULL)
2175 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2177 for (i = 0; i < FCU_FAN_COUNT; i++) {
2180 if (!fan_check_loc_match(loc, i))
2182 DBG(" location match, index: %d\n", i);
2183 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2184 if (type != fcu_fans[i].type) {
2185 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2186 "in device-tree for %s\n", np->full_name);
2189 if (type == FCU_FAN_RPM)
2190 fan_id = ((*reg) - 0x10) / 2;
2192 fan_id = ((*reg) - 0x30) / 2;
2194 printk(KERN_WARNING "therm_pm72: Can't parse "
2195 "fan ID in device-tree for %s\n", np->full_name);
2198 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2199 fcu_fans[i].id = fan_id;
2203 /* Now dump the array */
2204 printk(KERN_INFO "Detected fan controls:\n");
2205 for (i = 0; i < FCU_FAN_COUNT; i++) {
2206 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2208 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2209 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2210 fcu_fans[i].id, fcu_fans[i].loc);
2214 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
2216 state = state_detached;
2218 /* Lookup the fans in the device tree */
2219 fcu_lookup_fans(dev->node);
2221 /* Add the driver */
2222 return i2c_add_driver(&therm_pm72_driver);
2225 static int fcu_of_remove(struct of_device* dev)
2227 i2c_del_driver(&therm_pm72_driver);
2232 static const struct of_device_id fcu_match[] =
2240 static struct of_platform_driver fcu_of_platform_driver =
2242 .name = "temperature",
2243 .match_table = fcu_match,
2244 .probe = fcu_of_probe,
2245 .remove = fcu_of_remove
2249 * Check machine type, attach to i2c controller
2251 static int __init therm_pm72_init(void)
2253 struct device_node *np;
2255 rackmac = of_machine_is_compatible("RackMac3,1");
2257 if (!of_machine_is_compatible("PowerMac7,2") &&
2258 !of_machine_is_compatible("PowerMac7,3") &&
2262 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2264 np = of_find_node_by_type(NULL, "fcu");
2266 /* Some machines have strangely broken device-tree */
2267 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2269 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2273 of_dev = of_platform_device_create(np, "temperature", NULL);
2274 if (of_dev == NULL) {
2275 printk(KERN_ERR "Can't register FCU platform device !\n");
2279 of_register_platform_driver(&fcu_of_platform_driver);
2284 static void __exit therm_pm72_exit(void)
2286 of_unregister_platform_driver(&fcu_of_platform_driver);
2289 of_device_unregister(of_dev);
2292 module_init(therm_pm72_init);
2293 module_exit(therm_pm72_exit);
2295 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2296 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2297 MODULE_LICENSE("GPL");