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
56 * - Read fan speed from FCU, low level fan routines now deal
57 * with errors & check fan status, though higher level don't
59 * - Move a bunch of definitions to .h file
62 * - Fix build on ppc64 kernel
63 * - Move back statics definitions to .c file
64 * - Avoid calling schedule_timeout with a negative number
67 * - Fix typo when reading back fan speed on 2 CPU machines
70 * - Rework code accessing the ADC chips, make it more robust and
71 * closer to the chip spec. Also make sure it is configured properly,
72 * I've seen yet unexplained cases where on startup, I would have stale
73 * values in the configuration register
74 * - Switch back to use of target fan speed for PID, thus lowering
78 #include <linux/config.h>
79 #include <linux/types.h>
80 #include <linux/module.h>
81 #include <linux/errno.h>
82 #include <linux/kernel.h>
83 #include <linux/delay.h>
84 #include <linux/sched.h>
85 #include <linux/i2c.h>
86 #include <linux/slab.h>
87 #include <linux/init.h>
88 #include <linux/spinlock.h>
89 #include <linux/smp_lock.h>
90 #include <linux/wait.h>
91 #include <linux/reboot.h>
92 #include <linux/kmod.h>
93 #include <linux/i2c.h>
94 #include <linux/i2c-dev.h>
96 #include <asm/machdep.h>
98 #include <asm/system.h>
99 #include <asm/sections.h>
100 #include <asm/of_device.h>
102 #include "therm_pm72.h"
104 #define VERSION "0.9"
109 #define DBG(args...) printk(args)
111 #define DBG(args...) do { } while(0)
119 static struct of_device * of_dev;
120 static struct i2c_adapter * u3_0;
121 static struct i2c_adapter * u3_1;
122 static struct i2c_client * fcu;
123 static struct cpu_pid_state cpu_state[2];
124 static struct backside_pid_state backside_state;
125 static struct drives_pid_state drives_state;
127 static int cpu_count;
128 static pid_t ctrl_task;
129 static struct completion ctrl_complete;
130 static int critical_state;
131 static DECLARE_MUTEX(driver_lock);
134 * i2c_driver structure to attach to the host i2c controller
137 static int therm_pm72_attach(struct i2c_adapter *adapter);
138 static int therm_pm72_detach(struct i2c_adapter *adapter);
140 static struct i2c_driver therm_pm72_driver =
142 .name = "therm_pm72",
144 .flags = I2C_DF_NOTIFY,
145 .attach_adapter = therm_pm72_attach,
146 .detach_adapter = therm_pm72_detach,
150 * Utility function to create an i2c_client structure and
151 * attach it to one of u3 adapters
153 static struct i2c_client *attach_i2c_chip(int id, const char *name)
155 struct i2c_client *clt;
156 struct i2c_adapter *adap;
165 clt = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
168 memset(clt, 0, sizeof(struct i2c_client));
170 clt->addr = (id >> 1) & 0x7f;
172 clt->driver = &therm_pm72_driver;
173 clt->id = 0xDEADBEEF;
174 strncpy(clt->name, name, I2C_NAME_SIZE-1);
176 if (i2c_attach_client(clt)) {
177 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
185 * Utility function to get rid of the i2c_client structure
186 * (will also detach from the adapter hopepfully)
188 static void detach_i2c_chip(struct i2c_client *clt)
190 i2c_detach_client(clt);
195 * Here are the i2c chip access wrappers
198 static void initialize_adc(struct cpu_pid_state *state)
203 /* Read ADC the configuration register and cache it. We
204 * also make sure Config2 contains proper values, I've seen
205 * cases where we got stale grabage in there, thus preventing
206 * proper reading of conv. values
212 i2c_master_send(state->monitor, buf, 2);
214 /* Read & cache Config1 */
216 rc = i2c_master_send(state->monitor, buf, 1);
218 rc = i2c_master_recv(state->monitor, buf, 1);
220 state->adc_config = buf[0];
221 DBG("ADC config reg: %02x\n", state->adc_config);
222 /* Disable shutdown mode */
223 state->adc_config &= 0xfe;
225 buf[1] = state->adc_config;
226 rc = i2c_master_send(state->monitor, buf, 2);
230 printk(KERN_ERR "therm_pm72: Error reading ADC config"
234 static int read_smon_adc(struct cpu_pid_state *state, int chan)
236 int rc, data, tries = 0;
242 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
243 rc = i2c_master_send(state->monitor, buf, 2);
246 /* Wait for convertion */
248 /* Switch to data register */
250 rc = i2c_master_send(state->monitor, buf, 1);
254 rc = i2c_master_recv(state->monitor, buf, 2);
257 data = ((u16)buf[0]) << 8 | (u16)buf[1];
260 DBG("Error reading ADC, retrying...\n");
262 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
269 static int fan_read_reg(int reg, unsigned char *buf, int nb)
276 nw = i2c_master_send(fcu, buf, 1);
277 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
283 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
288 nr = i2c_master_recv(fcu, buf, nb);
289 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
295 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
299 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
302 unsigned char buf[16];
305 memcpy(buf+1, ptr, nb);
309 nw = i2c_master_send(fcu, buf, nb);
310 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
316 printk(KERN_ERR "Failure writing to FCU: %d", nw);
320 static int set_rpm_fan(int fan, int rpm)
322 unsigned char buf[2];
331 rc = fan_write_reg(0x10 + (fan * 2), buf, 2);
337 static int get_rpm_fan(int fan, int programmed)
339 unsigned char failure;
340 unsigned char active;
341 unsigned char buf[2];
344 rc = fan_read_reg(0xb, &failure, 1);
347 if ((failure & (1 << fan)) != 0)
349 rc = fan_read_reg(0xd, &active, 1);
352 if ((active & (1 << fan)) == 0)
355 /* Programmed value or real current speed */
356 reg_base = programmed ? 0x10 : 0x11;
357 rc = fan_read_reg(reg_base + (fan * 2), buf, 2);
361 return (buf[0] << 5) | buf[1] >> 3;
364 static int set_pwm_fan(int fan, int pwm)
366 unsigned char buf[2];
373 pwm = (pwm * 2559) / 1000;
375 rc = fan_write_reg(0x30 + (fan * 2), buf, 1);
381 static int get_pwm_fan(int fan)
383 unsigned char failure;
384 unsigned char active;
385 unsigned char buf[2];
388 rc = fan_read_reg(0x2b, &failure, 1);
391 if ((failure & (1 << fan)) != 0)
393 rc = fan_read_reg(0x2d, &active, 1);
396 if ((active & (1 << fan)) == 0)
399 /* Programmed value or real current speed */
400 rc = fan_read_reg(0x30 + (fan * 2), buf, 1);
404 return (buf[0] * 1000) / 2559;
408 * Utility routine to read the CPU calibration EEPROM data
409 * from the device-tree
411 static int read_eeprom(int cpu, struct mpu_data *out)
413 struct device_node *np;
418 /* prom.c routine for finding a node by path is a bit brain dead
419 * and requires exact @xxx unit numbers. This is a bit ugly but
420 * will work for these machines
422 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
423 np = of_find_node_by_path(nodename);
425 printk(KERN_ERR "therm_pm72: Failed to retreive cpuid node from device-tree\n");
428 data = (u8 *)get_property(np, "cpuid", &len);
430 printk(KERN_ERR "therm_pm72: Failed to retreive cpuid property from device-tree\n");
434 memcpy(out, data, sizeof(struct mpu_data));
441 * Now, unfortunately, sysfs doesn't give us a nice void * we could
442 * pass around to the attribute functions, so we don't really have
443 * choice but implement a bunch of them...
445 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
446 * the input twice... I accept patches :)
448 #define BUILD_SHOW_FUNC_FIX(name, data) \
449 static ssize_t show_##name(struct device *dev, char *buf) \
452 down(&driver_lock); \
453 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
457 #define BUILD_SHOW_FUNC_INT(name, data) \
458 static ssize_t show_##name(struct device *dev, char *buf) \
460 return sprintf(buf, "%d", data); \
463 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
464 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
465 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
466 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
467 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
469 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
470 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
471 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
472 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
473 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
475 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
476 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
478 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
479 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
481 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
482 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
483 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
484 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
485 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
487 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
488 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
489 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
490 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
491 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
493 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
494 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
496 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
497 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
500 * CPUs fans control loop
502 static void do_monitor_cpu(struct cpu_pid_state *state)
504 s32 temp, voltage, current_a, power, power_target;
505 s32 integral, derivative, proportional, adj_in_target, sval;
506 s64 integ_p, deriv_p, prop_p, sum;
509 DBG("cpu %d:\n", state->index);
511 /* Read current fan status */
512 if (state->index == 0)
513 rc = get_rpm_fan(CPUA_EXHAUST_FAN_RPM_ID, !RPM_PID_USE_ACTUAL_SPEED);
515 rc = get_rpm_fan(CPUB_EXHAUST_FAN_RPM_ID, !RPM_PID_USE_ACTUAL_SPEED);
517 printk(KERN_WARNING "Error %d reading CPU %d exhaust fan !\n",
519 /* XXX What do we do now ? */
522 DBG(" current rpm: %d\n", state->rpm);
524 /* Get some sensor readings and scale it */
525 temp = read_smon_adc(state, 1);
530 voltage = read_smon_adc(state, 3);
531 current_a = read_smon_adc(state, 4);
533 /* Fixup temperature according to diode calibration
535 DBG(" temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
536 temp, state->mpu.mdiode, state->mpu.bdiode);
537 temp = ((s32)temp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
538 state->last_temp = temp;
539 DBG(" temp: %d.%03d\n", FIX32TOPRINT(temp));
541 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
542 * full blown immediately and try to trigger a shutdown
544 if (temp >= ((state->mpu.tmax + 8) << 16)) {
545 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
547 state->index, temp >> 16);
548 state->overtemp = CPU_MAX_OVERTEMP;
549 } else if (temp > (state->mpu.tmax << 16))
553 if (state->overtemp >= CPU_MAX_OVERTEMP)
555 if (state->overtemp > 0) {
556 state->rpm = state->mpu.rmaxn_exhaust_fan;
557 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
561 /* Scale other sensor values according to fixed scales
562 * obtained in Darwin and calculate power from I and V
564 state->voltage = voltage *= ADC_CPU_VOLTAGE_SCALE;
565 state->current_a = current_a *= ADC_CPU_CURRENT_SCALE;
566 power = (((u64)current_a) * ((u64)voltage)) >> 16;
568 /* Calculate power target value (could be done once for all)
569 * and convert to a 16.16 fp number
571 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
573 DBG(" current: %d.%03d, voltage: %d.%03d\n",
574 FIX32TOPRINT(current_a), FIX32TOPRINT(voltage));
575 DBG(" power: %d.%03d W, target: %d.%03d, error: %d.%03d\n", FIX32TOPRINT(power),
576 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
578 /* Store temperature and power in history array */
579 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
580 state->temp_history[state->cur_temp] = temp;
581 state->cur_power = (state->cur_power + 1) % state->count_power;
582 state->power_history[state->cur_power] = power;
583 state->error_history[state->cur_power] = power_target - power;
585 /* If first loop, fill the history table */
587 for (i = 0; i < (state->count_power - 1); i++) {
588 state->cur_power = (state->cur_power + 1) % state->count_power;
589 state->power_history[state->cur_power] = power;
590 state->error_history[state->cur_power] = power_target - power;
592 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
593 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
594 state->temp_history[state->cur_temp] = temp;
599 /* Calculate the integral term normally based on the "power" values */
602 for (i = 0; i < state->count_power; i++)
603 integral += state->error_history[i];
604 integral *= CPU_PID_INTERVAL;
605 DBG(" integral: %08x\n", integral);
607 /* Calculate the adjusted input (sense value).
610 * so the result is 28.36
612 * input target is mpu.ttarget, input max is mpu.tmax
614 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
615 DBG(" integ_p: %d\n", (int)(deriv_p >> 36));
616 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
617 adj_in_target = (state->mpu.ttarget << 16);
618 if (adj_in_target > sval)
619 adj_in_target = sval;
620 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
623 /* Calculate the derivative term */
624 derivative = state->temp_history[state->cur_temp] -
625 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
626 % CPU_TEMP_HISTORY_SIZE];
627 derivative /= CPU_PID_INTERVAL;
628 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
629 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
632 /* Calculate the proportional term */
633 proportional = temp - adj_in_target;
634 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
635 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
641 DBG(" sum: %d\n", (int)sum);
642 state->rpm += (s32)sum;
644 if (state->rpm < state->mpu.rminn_exhaust_fan)
645 state->rpm = state->mpu.rminn_exhaust_fan;
646 if (state->rpm > state->mpu.rmaxn_exhaust_fan)
647 state->rpm = state->mpu.rmaxn_exhaust_fan;
649 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
650 if (intake < state->mpu.rminn_intake_fan)
651 intake = state->mpu.rminn_intake_fan;
652 if (intake > state->mpu.rmaxn_intake_fan)
653 intake = state->mpu.rmaxn_intake_fan;
654 state->intake_rpm = intake;
657 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
658 state->index, (int)state->rpm, intake, state->overtemp);
660 /* We should check for errors, shouldn't we ? But then, what
661 * do we do once the error occurs ? For FCU notified fan
662 * failures (-EFAULT) we probably want to notify userland
665 if (state->index == 0) {
666 set_rpm_fan(CPUA_INTAKE_FAN_RPM_ID, intake);
667 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_ID, state->rpm);
669 set_rpm_fan(CPUB_INTAKE_FAN_RPM_ID, intake);
670 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_ID, state->rpm);
675 * Initialize the state structure for one CPU control loop
677 static int init_cpu_state(struct cpu_pid_state *state, int index)
679 state->index = index;
683 state->adc_config = 0x00;
686 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
688 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
689 if (state->monitor == NULL)
692 if (read_eeprom(index, &state->mpu))
695 state->count_power = state->mpu.tguardband;
696 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
697 printk(KERN_WARNING "Warning ! too many power history slots\n");
698 state->count_power = CPU_POWER_HISTORY_SIZE;
700 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
703 device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
704 device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
705 device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
706 device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
707 device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
709 device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
710 device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
711 device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
712 device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
713 device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
719 detach_i2c_chip(state->monitor);
720 state->monitor = NULL;
726 * Dispose of the state data for one CPU control loop
728 static void dispose_cpu_state(struct cpu_pid_state *state)
730 if (state->monitor == NULL)
733 if (state->index == 0) {
734 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
735 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
736 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
737 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
738 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
740 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
741 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
742 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
743 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
744 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
747 detach_i2c_chip(state->monitor);
748 state->monitor = NULL;
752 * Motherboard backside & U3 heatsink fan control loop
754 static void do_monitor_backside(struct backside_pid_state *state)
756 s32 temp, integral, derivative;
757 s64 integ_p, deriv_p, prop_p, sum;
760 if (--state->ticks != 0)
762 state->ticks = BACKSIDE_PID_INTERVAL;
766 /* Check fan status */
767 rc = get_pwm_fan(BACKSIDE_FAN_PWM_ID);
769 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
770 /* XXX What do we do now ? */
773 DBG(" current pwm: %d\n", state->pwm);
775 /* Get some sensor readings */
776 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
777 state->last_temp = temp;
778 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
779 FIX32TOPRINT(BACKSIDE_PID_INPUT_TARGET));
781 /* Store temperature and error in history array */
782 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
783 state->sample_history[state->cur_sample] = temp;
784 state->error_history[state->cur_sample] = temp - BACKSIDE_PID_INPUT_TARGET;
786 /* If first loop, fill the history table */
788 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
789 state->cur_sample = (state->cur_sample + 1) %
790 BACKSIDE_PID_HISTORY_SIZE;
791 state->sample_history[state->cur_sample] = temp;
792 state->error_history[state->cur_sample] =
793 temp - BACKSIDE_PID_INPUT_TARGET;
798 /* Calculate the integral term */
801 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
802 integral += state->error_history[i];
803 integral *= BACKSIDE_PID_INTERVAL;
804 DBG(" integral: %08x\n", integral);
805 integ_p = ((s64)BACKSIDE_PID_G_r) * (s64)integral;
806 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
809 /* Calculate the derivative term */
810 derivative = state->error_history[state->cur_sample] -
811 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
812 % BACKSIDE_PID_HISTORY_SIZE];
813 derivative /= BACKSIDE_PID_INTERVAL;
814 deriv_p = ((s64)BACKSIDE_PID_G_d) * (s64)derivative;
815 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
818 /* Calculate the proportional term */
819 prop_p = ((s64)BACKSIDE_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
820 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
826 DBG(" sum: %d\n", (int)sum);
827 state->pwm += (s32)sum;
828 if (state->pwm < BACKSIDE_PID_OUTPUT_MIN)
829 state->pwm = BACKSIDE_PID_OUTPUT_MIN;
830 if (state->pwm > BACKSIDE_PID_OUTPUT_MAX)
831 state->pwm = BACKSIDE_PID_OUTPUT_MAX;
833 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
834 set_pwm_fan(BACKSIDE_FAN_PWM_ID, state->pwm);
838 * Initialize the state structure for the backside fan control loop
840 static int init_backside_state(struct backside_pid_state *state)
846 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
847 if (state->monitor == NULL)
850 device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
851 device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
857 * Dispose of the state data for the backside control loop
859 static void dispose_backside_state(struct backside_pid_state *state)
861 if (state->monitor == NULL)
864 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
865 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
867 detach_i2c_chip(state->monitor);
868 state->monitor = NULL;
872 * Drives bay fan control loop
874 static void do_monitor_drives(struct drives_pid_state *state)
876 s32 temp, integral, derivative;
877 s64 integ_p, deriv_p, prop_p, sum;
880 if (--state->ticks != 0)
882 state->ticks = DRIVES_PID_INTERVAL;
886 /* Check fan status */
887 rc = get_rpm_fan(DRIVES_FAN_RPM_ID, !RPM_PID_USE_ACTUAL_SPEED);
889 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
890 /* XXX What do we do now ? */
893 DBG(" current rpm: %d\n", state->rpm);
895 /* Get some sensor readings */
896 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor, DS1775_TEMP)) << 8;
897 state->last_temp = temp;
898 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
899 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
901 /* Store temperature and error in history array */
902 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
903 state->sample_history[state->cur_sample] = temp;
904 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
906 /* If first loop, fill the history table */
908 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
909 state->cur_sample = (state->cur_sample + 1) %
910 DRIVES_PID_HISTORY_SIZE;
911 state->sample_history[state->cur_sample] = temp;
912 state->error_history[state->cur_sample] =
913 temp - DRIVES_PID_INPUT_TARGET;
918 /* Calculate the integral term */
921 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
922 integral += state->error_history[i];
923 integral *= DRIVES_PID_INTERVAL;
924 DBG(" integral: %08x\n", integral);
925 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
926 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
929 /* Calculate the derivative term */
930 derivative = state->error_history[state->cur_sample] -
931 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
932 % DRIVES_PID_HISTORY_SIZE];
933 derivative /= DRIVES_PID_INTERVAL;
934 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
935 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
938 /* Calculate the proportional term */
939 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
940 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
946 DBG(" sum: %d\n", (int)sum);
947 state->rpm += (s32)sum;
948 if (state->rpm < DRIVES_PID_OUTPUT_MIN)
949 state->rpm = DRIVES_PID_OUTPUT_MIN;
950 if (state->rpm > DRIVES_PID_OUTPUT_MAX)
951 state->rpm = DRIVES_PID_OUTPUT_MAX;
953 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
954 set_rpm_fan(DRIVES_FAN_RPM_ID, state->rpm);
958 * Initialize the state structure for the drives bay fan control loop
960 static int init_drives_state(struct drives_pid_state *state)
966 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
967 if (state->monitor == NULL)
970 device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
971 device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
977 * Dispose of the state data for the drives control loop
979 static void dispose_drives_state(struct drives_pid_state *state)
981 if (state->monitor == NULL)
984 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
985 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
987 detach_i2c_chip(state->monitor);
988 state->monitor = NULL;
991 static int call_critical_overtemp(void)
993 char *argv[] = { critical_overtemp_path, NULL };
994 static char *envp[] = { "HOME=/",
996 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
999 return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
1004 * Here's the kernel thread that calls the various control loops
1006 static int main_control_loop(void *x)
1010 DBG("main_control_loop started\n");
1014 /* Set the PCI fan once for now */
1015 set_pwm_fan(SLOTS_FAN_PWM_ID, SLOTS_FAN_DEFAULT_PWM);
1017 /* Initialize ADCs */
1018 initialize_adc(&cpu_state[0]);
1019 if (cpu_state[1].monitor != NULL)
1020 initialize_adc(&cpu_state[1]);
1024 while (state == state_attached) {
1025 unsigned long elapsed, start;
1030 do_monitor_cpu(&cpu_state[0]);
1031 if (cpu_state[1].monitor != NULL)
1032 do_monitor_cpu(&cpu_state[1]);
1033 do_monitor_backside(&backside_state);
1034 do_monitor_drives(&drives_state);
1037 if (critical_state == 1) {
1038 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1039 printk(KERN_WARNING "Attempting to shut down...\n");
1040 if (call_critical_overtemp()) {
1041 printk(KERN_WARNING "Can't call %s, power off now!\n",
1042 critical_overtemp_path);
1043 machine_power_off();
1046 if (critical_state > 0)
1048 if (critical_state > MAX_CRITICAL_STATE) {
1049 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1050 machine_power_off();
1053 // FIXME: Deal with signals
1054 set_current_state(TASK_INTERRUPTIBLE);
1055 elapsed = jiffies - start;
1057 schedule_timeout(HZ - elapsed);
1060 DBG("main_control_loop ended\n");
1063 complete_and_exit(&ctrl_complete, 0);
1067 * Dispose the control loops when tearing down
1069 static void dispose_control_loops(void)
1071 dispose_cpu_state(&cpu_state[0]);
1072 dispose_cpu_state(&cpu_state[1]);
1074 dispose_backside_state(&backside_state);
1075 dispose_drives_state(&drives_state);
1079 * Create the control loops. U3-0 i2c bus is up, so we can now
1080 * get to the various sensors
1082 static int create_control_loops(void)
1084 struct device_node *np;
1086 /* Count CPUs from the device-tree, we don't care how many are
1087 * actually used by Linux
1090 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1093 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1095 /* Create control loops for everything. If any fail, everything
1098 if (init_cpu_state(&cpu_state[0], 0))
1100 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1102 if (init_backside_state(&backside_state))
1104 if (init_drives_state(&drives_state))
1107 DBG("all control loops up !\n");
1112 DBG("failure creating control loops, disposing\n");
1114 dispose_control_loops();
1120 * Start the control loops after everything is up, that is create
1121 * the thread that will make them run
1123 static void start_control_loops(void)
1125 init_completion(&ctrl_complete);
1127 ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL);
1131 * Stop the control loops when tearing down
1133 static void stop_control_loops(void)
1136 wait_for_completion(&ctrl_complete);
1140 * Attach to the i2c FCU after detecting U3-1 bus
1142 static int attach_fcu(void)
1144 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1148 DBG("FCU attached\n");
1154 * Detach from the i2c FCU when tearing down
1156 static void detach_fcu(void)
1159 detach_i2c_chip(fcu);
1164 * Attach to the i2c controller. We probe the various chips based
1165 * on the device-tree nodes and build everything for the driver to
1166 * run, we then kick the driver monitoring thread
1168 static int therm_pm72_attach(struct i2c_adapter *adapter)
1173 if (state == state_detached)
1174 state = state_attaching;
1175 if (state != state_attaching) {
1180 /* Check if we are looking for one of these */
1181 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
1183 DBG("found U3-0, creating control loops\n");
1184 if (create_control_loops())
1186 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
1188 DBG("found U3-1, attaching FCU\n");
1192 /* We got all we need, start control loops */
1193 if (u3_0 != NULL && u3_1 != NULL) {
1194 DBG("everything up, starting control loops\n");
1195 state = state_attached;
1196 start_control_loops();
1204 * Called on every adapter when the driver or the i2c controller
1207 static int therm_pm72_detach(struct i2c_adapter *adapter)
1211 if (state != state_detached)
1212 state = state_detaching;
1214 /* Stop control loops if any */
1215 DBG("stopping control loops\n");
1217 stop_control_loops();
1220 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
1221 DBG("lost U3-0, disposing control loops\n");
1222 dispose_control_loops();
1226 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
1227 DBG("lost U3-1, detaching FCU\n");
1231 if (u3_0 == NULL && u3_1 == NULL)
1232 state = state_detached;
1239 static int fcu_of_probe(struct of_device* dev, const struct of_match *match)
1243 state = state_detached;
1245 rc = i2c_add_driver(&therm_pm72_driver);
1251 static int fcu_of_remove(struct of_device* dev)
1253 i2c_del_driver(&therm_pm72_driver);
1258 static struct of_match fcu_of_match[] =
1261 .name = OF_ANY_MATCH,
1263 .compatible = OF_ANY_MATCH
1268 static struct of_platform_driver fcu_of_platform_driver =
1270 .name = "temperature",
1271 .match_table = fcu_of_match,
1272 .probe = fcu_of_probe,
1273 .remove = fcu_of_remove
1277 * Check machine type, attach to i2c controller
1279 static int __init therm_pm72_init(void)
1281 struct device_node *np;
1283 if (!machine_is_compatible("PowerMac7,2"))
1286 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
1288 np = of_find_node_by_type(NULL, "fcu");
1290 printk(KERN_ERR "Can't find FCU in device-tree !\n");
1293 of_dev = of_platform_device_create(np, "temperature");
1294 if (of_dev == NULL) {
1295 printk(KERN_ERR "Can't register FCU platform device !\n");
1299 of_register_driver(&fcu_of_platform_driver);
1304 static void __exit therm_pm72_exit(void)
1306 of_unregister_driver(&fcu_of_platform_driver);
1309 of_device_unregister(of_dev);
1312 module_init(therm_pm72_init);
1313 module_exit(therm_pm72_exit);
1315 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
1316 MODULE_DESCRIPTION("Driver for Apple's PowerMac7,2 G5 thermal control");
1317 MODULE_LICENSE("GPL");