GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / macintosh / therm_pm72.c
blobfc578324a1cee31986127333a6ce79ad8b04563f
3 #include <linux/types.h>
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/sched.h>
9 #include <linux/init.h>
10 #include <linux/spinlock.h>
11 #include <linux/wait.h>
12 #include <linux/reboot.h>
13 #include <linux/kmod.h>
14 #include <linux/i2c.h>
15 #include <linux/kthread.h>
16 #include <linux/mutex.h>
17 #include <linux/of_device.h>
18 #include <linux/of_platform.h>
19 #include <asm/prom.h>
20 #include <asm/machdep.h>
21 #include <asm/io.h>
22 #include <asm/system.h>
23 #include <asm/sections.h>
24 #include <asm/macio.h>
26 #include "therm_pm72.h"
28 #define VERSION "1.3"
30 #undef DEBUG
32 #ifdef DEBUG
33 #define DBG(args...) printk(args)
34 #else
35 #define DBG(args...) do { } while(0)
36 #endif
40 * Driver statics
43 static struct platform_device * of_dev;
44 static struct i2c_adapter * u3_0;
45 static struct i2c_adapter * u3_1;
46 static struct i2c_adapter * k2;
47 static struct i2c_client * fcu;
48 static struct cpu_pid_state cpu_state[2];
49 static struct basckside_pid_params backside_params;
50 static struct backside_pid_state backside_state;
51 static struct drives_pid_state drives_state;
52 static struct dimm_pid_state dimms_state;
53 static struct slots_pid_state slots_state;
54 static int state;
55 static int cpu_count;
56 static int cpu_pid_type;
57 static struct task_struct *ctrl_task;
58 static struct completion ctrl_complete;
59 static int critical_state;
60 static int rackmac;
61 static s32 dimm_output_clamp;
62 static int fcu_rpm_shift;
63 static int fcu_tickle_ticks;
64 static DEFINE_MUTEX(driver_lock);
67 * We have 3 types of CPU PID control. One is "split" old style control
68 * for intake & exhaust fans, the other is "combined" control for both
69 * CPUs that also deals with the pumps when present. To be "compatible"
70 * with OS X at this point, we only use "COMBINED" on the machines that
71 * are identified as having the pumps (though that identification is at
72 * least dodgy). Ultimately, we could probably switch completely to this
73 * algorithm provided we hack it to deal with the UP case
75 #define CPU_PID_TYPE_SPLIT 0
76 #define CPU_PID_TYPE_COMBINED 1
77 #define CPU_PID_TYPE_RACKMAC 2
80 * This table describes all fans in the FCU. The "id" and "type" values
81 * are defaults valid for all earlier machines. Newer machines will
82 * eventually override the table content based on the device-tree
84 struct fcu_fan_table
86 char* loc; /* location code */
87 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
88 int id; /* id or -1 */
91 #define FCU_FAN_RPM 0
92 #define FCU_FAN_PWM 1
94 #define FCU_FAN_ABSENT_ID -1
96 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
98 struct fcu_fan_table fcu_fans[] = {
99 [BACKSIDE_FAN_PWM_INDEX] = {
100 .loc = "BACKSIDE,SYS CTRLR FAN",
101 .type = FCU_FAN_PWM,
102 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
104 [DRIVES_FAN_RPM_INDEX] = {
105 .loc = "DRIVE BAY",
106 .type = FCU_FAN_RPM,
107 .id = DRIVES_FAN_RPM_DEFAULT_ID,
109 [SLOTS_FAN_PWM_INDEX] = {
110 .loc = "SLOT,PCI FAN",
111 .type = FCU_FAN_PWM,
112 .id = SLOTS_FAN_PWM_DEFAULT_ID,
114 [CPUA_INTAKE_FAN_RPM_INDEX] = {
115 .loc = "CPU A INTAKE",
116 .type = FCU_FAN_RPM,
117 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
119 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
120 .loc = "CPU A EXHAUST",
121 .type = FCU_FAN_RPM,
122 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
124 [CPUB_INTAKE_FAN_RPM_INDEX] = {
125 .loc = "CPU B INTAKE",
126 .type = FCU_FAN_RPM,
127 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
129 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
130 .loc = "CPU B EXHAUST",
131 .type = FCU_FAN_RPM,
132 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
134 /* pumps aren't present by default, have to be looked up in the
135 * device-tree
137 [CPUA_PUMP_RPM_INDEX] = {
138 .loc = "CPU A PUMP",
139 .type = FCU_FAN_RPM,
140 .id = FCU_FAN_ABSENT_ID,
142 [CPUB_PUMP_RPM_INDEX] = {
143 .loc = "CPU B PUMP",
144 .type = FCU_FAN_RPM,
145 .id = FCU_FAN_ABSENT_ID,
147 /* Xserve fans */
148 [CPU_A1_FAN_RPM_INDEX] = {
149 .loc = "CPU A 1",
150 .type = FCU_FAN_RPM,
151 .id = FCU_FAN_ABSENT_ID,
153 [CPU_A2_FAN_RPM_INDEX] = {
154 .loc = "CPU A 2",
155 .type = FCU_FAN_RPM,
156 .id = FCU_FAN_ABSENT_ID,
158 [CPU_A3_FAN_RPM_INDEX] = {
159 .loc = "CPU A 3",
160 .type = FCU_FAN_RPM,
161 .id = FCU_FAN_ABSENT_ID,
163 [CPU_B1_FAN_RPM_INDEX] = {
164 .loc = "CPU B 1",
165 .type = FCU_FAN_RPM,
166 .id = FCU_FAN_ABSENT_ID,
168 [CPU_B2_FAN_RPM_INDEX] = {
169 .loc = "CPU B 2",
170 .type = FCU_FAN_RPM,
171 .id = FCU_FAN_ABSENT_ID,
173 [CPU_B3_FAN_RPM_INDEX] = {
174 .loc = "CPU B 3",
175 .type = FCU_FAN_RPM,
176 .id = FCU_FAN_ABSENT_ID,
180 static struct i2c_driver therm_pm72_driver;
183 * Utility function to create an i2c_client structure and
184 * attach it to one of u3 adapters
186 static struct i2c_client *attach_i2c_chip(int id, const char *name)
188 struct i2c_client *clt;
189 struct i2c_adapter *adap;
190 struct i2c_board_info info;
192 if (id & 0x200)
193 adap = k2;
194 else if (id & 0x100)
195 adap = u3_1;
196 else
197 adap = u3_0;
198 if (adap == NULL)
199 return NULL;
201 memset(&info, 0, sizeof(struct i2c_board_info));
202 info.addr = (id >> 1) & 0x7f;
203 strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
204 clt = i2c_new_device(adap, &info);
205 if (!clt) {
206 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
207 return NULL;
211 * Let i2c-core delete that device on driver removal.
212 * This is safe because i2c-core holds the core_lock mutex for us.
214 list_add_tail(&clt->detected, &therm_pm72_driver.clients);
215 return clt;
219 * Here are the i2c chip access wrappers
222 static void initialize_adc(struct cpu_pid_state *state)
224 int rc;
225 u8 buf[2];
227 /* Read ADC the configuration register and cache it. We
228 * also make sure Config2 contains proper values, I've seen
229 * cases where we got stale grabage in there, thus preventing
230 * proper reading of conv. values
233 /* Clear Config2 */
234 buf[0] = 5;
235 buf[1] = 0;
236 i2c_master_send(state->monitor, buf, 2);
238 /* Read & cache Config1 */
239 buf[0] = 1;
240 rc = i2c_master_send(state->monitor, buf, 1);
241 if (rc > 0) {
242 rc = i2c_master_recv(state->monitor, buf, 1);
243 if (rc > 0) {
244 state->adc_config = buf[0];
245 DBG("ADC config reg: %02x\n", state->adc_config);
246 /* Disable shutdown mode */
247 state->adc_config &= 0xfe;
248 buf[0] = 1;
249 buf[1] = state->adc_config;
250 rc = i2c_master_send(state->monitor, buf, 2);
253 if (rc <= 0)
254 printk(KERN_ERR "therm_pm72: Error reading ADC config"
255 " register !\n");
258 static int read_smon_adc(struct cpu_pid_state *state, int chan)
260 int rc, data, tries = 0;
261 u8 buf[2];
263 for (;;) {
264 /* Set channel */
265 buf[0] = 1;
266 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
267 rc = i2c_master_send(state->monitor, buf, 2);
268 if (rc <= 0)
269 goto error;
270 /* Wait for convertion */
271 msleep(1);
272 /* Switch to data register */
273 buf[0] = 4;
274 rc = i2c_master_send(state->monitor, buf, 1);
275 if (rc <= 0)
276 goto error;
277 /* Read result */
278 rc = i2c_master_recv(state->monitor, buf, 2);
279 if (rc < 0)
280 goto error;
281 data = ((u16)buf[0]) << 8 | (u16)buf[1];
282 return data >> 6;
283 error:
284 DBG("Error reading ADC, retrying...\n");
285 if (++tries > 10) {
286 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
287 return -1;
289 msleep(10);
293 static int read_lm87_reg(struct i2c_client * chip, int reg)
295 int rc, tries = 0;
296 u8 buf;
298 for (;;) {
299 /* Set address */
300 buf = (u8)reg;
301 rc = i2c_master_send(chip, &buf, 1);
302 if (rc <= 0)
303 goto error;
304 rc = i2c_master_recv(chip, &buf, 1);
305 if (rc <= 0)
306 goto error;
307 return (int)buf;
308 error:
309 DBG("Error reading LM87, retrying...\n");
310 if (++tries > 10) {
311 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
312 return -1;
314 msleep(10);
318 static int fan_read_reg(int reg, unsigned char *buf, int nb)
320 int tries, nr, nw;
322 buf[0] = reg;
323 tries = 0;
324 for (;;) {
325 nw = i2c_master_send(fcu, buf, 1);
326 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
327 break;
328 msleep(10);
329 ++tries;
331 if (nw <= 0) {
332 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
333 return -EIO;
335 tries = 0;
336 for (;;) {
337 nr = i2c_master_recv(fcu, buf, nb);
338 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
339 break;
340 msleep(10);
341 ++tries;
343 if (nr <= 0)
344 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
345 return nr;
348 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
350 int tries, nw;
351 unsigned char buf[16];
353 buf[0] = reg;
354 memcpy(buf+1, ptr, nb);
355 ++nb;
356 tries = 0;
357 for (;;) {
358 nw = i2c_master_send(fcu, buf, nb);
359 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
360 break;
361 msleep(10);
362 ++tries;
364 if (nw < 0)
365 printk(KERN_ERR "Failure writing to FCU: %d", nw);
366 return nw;
369 static int start_fcu(void)
371 unsigned char buf = 0xff;
372 int rc;
374 rc = fan_write_reg(0xe, &buf, 1);
375 if (rc < 0)
376 return -EIO;
377 rc = fan_write_reg(0x2e, &buf, 1);
378 if (rc < 0)
379 return -EIO;
380 rc = fan_read_reg(0, &buf, 1);
381 if (rc < 0)
382 return -EIO;
383 fcu_rpm_shift = (buf == 1) ? 2 : 3;
384 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
385 fcu_rpm_shift);
387 return 0;
390 static int set_rpm_fan(int fan_index, int rpm)
392 unsigned char buf[2];
393 int rc, id, min, max;
395 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
396 return -EINVAL;
397 id = fcu_fans[fan_index].id;
398 if (id == FCU_FAN_ABSENT_ID)
399 return -EINVAL;
401 min = 2400 >> fcu_rpm_shift;
402 max = 56000 >> fcu_rpm_shift;
404 if (rpm < min)
405 rpm = min;
406 else if (rpm > max)
407 rpm = max;
408 buf[0] = rpm >> (8 - fcu_rpm_shift);
409 buf[1] = rpm << fcu_rpm_shift;
410 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
411 if (rc < 0)
412 return -EIO;
413 return 0;
416 static int get_rpm_fan(int fan_index, int programmed)
418 unsigned char failure;
419 unsigned char active;
420 unsigned char buf[2];
421 int rc, id, reg_base;
423 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
424 return -EINVAL;
425 id = fcu_fans[fan_index].id;
426 if (id == FCU_FAN_ABSENT_ID)
427 return -EINVAL;
429 rc = fan_read_reg(0xb, &failure, 1);
430 if (rc != 1)
431 return -EIO;
432 if ((failure & (1 << id)) != 0)
433 return -EFAULT;
434 rc = fan_read_reg(0xd, &active, 1);
435 if (rc != 1)
436 return -EIO;
437 if ((active & (1 << id)) == 0)
438 return -ENXIO;
440 /* Programmed value or real current speed */
441 reg_base = programmed ? 0x10 : 0x11;
442 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
443 if (rc != 2)
444 return -EIO;
446 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
449 static int set_pwm_fan(int fan_index, int pwm)
451 unsigned char buf[2];
452 int rc, id;
454 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
455 return -EINVAL;
456 id = fcu_fans[fan_index].id;
457 if (id == FCU_FAN_ABSENT_ID)
458 return -EINVAL;
460 if (pwm < 10)
461 pwm = 10;
462 else if (pwm > 100)
463 pwm = 100;
464 pwm = (pwm * 2559) / 1000;
465 buf[0] = pwm;
466 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
467 if (rc < 0)
468 return rc;
469 return 0;
472 static int get_pwm_fan(int fan_index)
474 unsigned char failure;
475 unsigned char active;
476 unsigned char buf[2];
477 int rc, id;
479 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
480 return -EINVAL;
481 id = fcu_fans[fan_index].id;
482 if (id == FCU_FAN_ABSENT_ID)
483 return -EINVAL;
485 rc = fan_read_reg(0x2b, &failure, 1);
486 if (rc != 1)
487 return -EIO;
488 if ((failure & (1 << id)) != 0)
489 return -EFAULT;
490 rc = fan_read_reg(0x2d, &active, 1);
491 if (rc != 1)
492 return -EIO;
493 if ((active & (1 << id)) == 0)
494 return -ENXIO;
496 /* Programmed value or real current speed */
497 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
498 if (rc != 1)
499 return -EIO;
501 return (buf[0] * 1000) / 2559;
504 static void tickle_fcu(void)
506 int pwm;
508 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
510 DBG("FCU Tickle, slots fan is: %d\n", pwm);
511 if (pwm < 0)
512 pwm = 100;
514 if (!rackmac) {
515 pwm = SLOTS_FAN_DEFAULT_PWM;
516 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
517 pwm = SLOTS_PID_OUTPUT_MIN;
519 /* That is hopefully enough to make the FCU happy */
520 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
525 * Utility routine to read the CPU calibration EEPROM data
526 * from the device-tree
528 static int read_eeprom(int cpu, struct mpu_data *out)
530 struct device_node *np;
531 char nodename[64];
532 const u8 *data;
533 int len;
535 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
536 np = of_find_node_by_path(nodename);
537 if (np == NULL) {
538 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
539 return -ENODEV;
541 data = of_get_property(np, "cpuid", &len);
542 if (data == NULL) {
543 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
544 of_node_put(np);
545 return -ENODEV;
547 memcpy(out, data, sizeof(struct mpu_data));
548 of_node_put(np);
550 return 0;
553 static void fetch_cpu_pumps_minmax(void)
555 struct cpu_pid_state *state0 = &cpu_state[0];
556 struct cpu_pid_state *state1 = &cpu_state[1];
557 u16 pump_min = 0, pump_max = 0xffff;
558 u16 tmp[4];
560 /* Try to fetch pumps min/max infos from eeprom */
562 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
563 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
564 pump_min = max(pump_min, tmp[0]);
565 pump_max = min(pump_max, tmp[1]);
567 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
568 pump_min = max(pump_min, tmp[2]);
569 pump_max = min(pump_max, tmp[3]);
572 /* Double check the values, this _IS_ needed as the EEPROM on
573 * some dual 2.5Ghz G5s seem, at least, to have both min & max
574 * same to the same value ... (grrrr)
576 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
577 pump_min = CPU_PUMP_OUTPUT_MIN;
578 pump_max = CPU_PUMP_OUTPUT_MAX;
581 state0->pump_min = state1->pump_min = pump_min;
582 state0->pump_max = state1->pump_max = pump_max;
586 * Now, unfortunately, sysfs doesn't give us a nice void * we could
587 * pass around to the attribute functions, so we don't really have
588 * choice but implement a bunch of them...
590 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
591 * the input twice... I accept patches :)
593 #define BUILD_SHOW_FUNC_FIX(name, data) \
594 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
596 ssize_t r; \
597 mutex_lock(&driver_lock); \
598 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
599 mutex_unlock(&driver_lock); \
600 return r; \
602 #define BUILD_SHOW_FUNC_INT(name, data) \
603 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
605 return sprintf(buf, "%d", data); \
608 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
609 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
610 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
611 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
612 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
614 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
615 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
616 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
617 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
618 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
620 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
621 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
623 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
624 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
626 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
627 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
629 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
631 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
632 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
633 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
634 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
635 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
637 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
638 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
639 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
640 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
641 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
643 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
644 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
646 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
647 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
649 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
650 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
652 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
655 * CPUs fans control loop
658 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
660 s32 ltemp, volts, amps;
661 int index, rc = 0;
663 /* Default (in case of error) */
664 *temp = state->cur_temp;
665 *power = state->cur_power;
667 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
668 index = (state->index == 0) ?
669 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
670 else
671 index = (state->index == 0) ?
672 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
674 /* Read current fan status */
675 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
676 if (rc < 0) {
677 DBG(" cpu %d, fan reading error !\n", state->index);
678 } else {
679 state->rpm = rc;
680 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
683 /* Get some sensor readings and scale it */
684 ltemp = read_smon_adc(state, 1);
685 if (ltemp == -1) {
686 state->overtemp++;
687 if (rc == 0)
688 rc = -EIO;
689 DBG(" cpu %d, temp reading error !\n", state->index);
690 } else {
691 /* Fixup temperature according to diode calibration
693 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
694 state->index,
695 ltemp, state->mpu.mdiode, state->mpu.bdiode);
696 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
697 state->last_temp = *temp;
698 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
702 * Read voltage & current and calculate power
704 volts = read_smon_adc(state, 3);
705 amps = read_smon_adc(state, 4);
707 /* Scale voltage and current raw sensor values according to fixed scales
708 * obtained in Darwin and calculate power from I and V
710 volts *= ADC_CPU_VOLTAGE_SCALE;
711 amps *= ADC_CPU_CURRENT_SCALE;
712 *power = (((u64)volts) * ((u64)amps)) >> 16;
713 state->voltage = volts;
714 state->current_a = amps;
715 state->last_power = *power;
717 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
718 state->index, FIX32TOPRINT(state->current_a),
719 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
721 return 0;
724 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
726 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
727 s64 integ_p, deriv_p, prop_p, sum;
728 int i;
730 /* Calculate power target value (could be done once for all)
731 * and convert to a 16.16 fp number
733 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
734 DBG(" power target: %d.%03d, error: %d.%03d\n",
735 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
737 /* Store temperature and power in history array */
738 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
739 state->temp_history[state->cur_temp] = temp;
740 state->cur_power = (state->cur_power + 1) % state->count_power;
741 state->power_history[state->cur_power] = power;
742 state->error_history[state->cur_power] = power_target - power;
744 /* If first loop, fill the history table */
745 if (state->first) {
746 for (i = 0; i < (state->count_power - 1); i++) {
747 state->cur_power = (state->cur_power + 1) % state->count_power;
748 state->power_history[state->cur_power] = power;
749 state->error_history[state->cur_power] = power_target - power;
751 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
752 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
753 state->temp_history[state->cur_temp] = temp;
755 state->first = 0;
758 /* Calculate the integral term normally based on the "power" values */
759 sum = 0;
760 integral = 0;
761 for (i = 0; i < state->count_power; i++)
762 integral += state->error_history[i];
763 integral *= CPU_PID_INTERVAL;
764 DBG(" integral: %08x\n", integral);
766 /* Calculate the adjusted input (sense value).
767 * G_r is 12.20
768 * integ is 16.16
769 * so the result is 28.36
771 * input target is mpu.ttarget, input max is mpu.tmax
773 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
774 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
775 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
776 adj_in_target = (state->mpu.ttarget << 16);
777 if (adj_in_target > sval)
778 adj_in_target = sval;
779 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
780 state->mpu.ttarget);
782 /* Calculate the derivative term */
783 derivative = state->temp_history[state->cur_temp] -
784 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
785 % CPU_TEMP_HISTORY_SIZE];
786 derivative /= CPU_PID_INTERVAL;
787 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
788 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
789 sum += deriv_p;
791 /* Calculate the proportional term */
792 proportional = temp - adj_in_target;
793 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
794 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
795 sum += prop_p;
797 /* Scale sum */
798 sum >>= 36;
800 DBG(" sum: %d\n", (int)sum);
801 state->rpm += (s32)sum;
804 static void do_monitor_cpu_combined(void)
806 struct cpu_pid_state *state0 = &cpu_state[0];
807 struct cpu_pid_state *state1 = &cpu_state[1];
808 s32 temp0, power0, temp1, power1;
809 s32 temp_combi, power_combi;
810 int rc, intake, pump;
812 rc = do_read_one_cpu_values(state0, &temp0, &power0);
813 if (rc < 0) {
815 state1->overtemp = 0;
816 rc = do_read_one_cpu_values(state1, &temp1, &power1);
817 if (rc < 0) {
819 if (state1->overtemp)
820 state0->overtemp++;
822 temp_combi = max(temp0, temp1);
823 power_combi = max(power0, power1);
825 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
826 * full blown immediately and try to trigger a shutdown
828 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
829 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
830 temp_combi >> 16);
831 state0->overtemp += CPU_MAX_OVERTEMP / 4;
832 } else if (temp_combi > (state0->mpu.tmax << 16)) {
833 state0->overtemp++;
834 printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
835 temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
836 } else {
837 if (state0->overtemp)
838 printk(KERN_WARNING "Temperature back down to %d\n",
839 temp_combi >> 16);
840 state0->overtemp = 0;
842 if (state0->overtemp >= CPU_MAX_OVERTEMP)
843 critical_state = 1;
844 if (state0->overtemp > 0) {
845 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
846 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
847 pump = state0->pump_max;
848 goto do_set_fans;
851 /* Do the PID */
852 do_cpu_pid(state0, temp_combi, power_combi);
854 /* Range check */
855 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
856 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
858 /* Calculate intake fan speed */
859 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
860 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
861 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
862 state0->intake_rpm = intake;
864 /* Calculate pump speed */
865 pump = (state0->rpm * state0->pump_max) /
866 state0->mpu.rmaxn_exhaust_fan;
867 pump = min(pump, state0->pump_max);
868 pump = max(pump, state0->pump_min);
870 do_set_fans:
871 /* We copy values from state 0 to state 1 for /sysfs */
872 state1->rpm = state0->rpm;
873 state1->intake_rpm = state0->intake_rpm;
875 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
876 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
878 /* We should check for errors, shouldn't we ? But then, what
879 * do we do once the error occurs ? For FCU notified fan
880 * failures (-EFAULT) we probably want to notify userland
881 * some way...
883 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
884 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
885 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
886 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
888 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
889 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
890 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
891 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
894 static void do_monitor_cpu_split(struct cpu_pid_state *state)
896 s32 temp, power;
897 int rc, intake;
899 /* Read current fan status */
900 rc = do_read_one_cpu_values(state, &temp, &power);
901 if (rc < 0) {
904 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
905 * full blown immediately and try to trigger a shutdown
907 if (temp >= ((state->mpu.tmax + 8) << 16)) {
908 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
909 " (%d) !\n",
910 state->index, temp >> 16);
911 state->overtemp += CPU_MAX_OVERTEMP / 4;
912 } else if (temp > (state->mpu.tmax << 16)) {
913 state->overtemp++;
914 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
915 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
916 } else {
917 if (state->overtemp)
918 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
919 state->index, temp >> 16);
920 state->overtemp = 0;
922 if (state->overtemp >= CPU_MAX_OVERTEMP)
923 critical_state = 1;
924 if (state->overtemp > 0) {
925 state->rpm = state->mpu.rmaxn_exhaust_fan;
926 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
927 goto do_set_fans;
930 /* Do the PID */
931 do_cpu_pid(state, temp, power);
933 /* Range check */
934 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
935 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
937 /* Calculate intake fan */
938 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
939 intake = max(intake, (int)state->mpu.rminn_intake_fan);
940 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
941 state->intake_rpm = intake;
943 do_set_fans:
944 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
945 state->index, (int)state->rpm, intake, state->overtemp);
947 /* We should check for errors, shouldn't we ? But then, what
948 * do we do once the error occurs ? For FCU notified fan
949 * failures (-EFAULT) we probably want to notify userland
950 * some way...
952 if (state->index == 0) {
953 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
954 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
955 } else {
956 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
957 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
961 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
963 s32 temp, power, fan_min;
964 int rc;
966 /* Read current fan status */
967 rc = do_read_one_cpu_values(state, &temp, &power);
968 if (rc < 0) {
971 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
972 * full blown immediately and try to trigger a shutdown
974 if (temp >= ((state->mpu.tmax + 8) << 16)) {
975 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
976 " (%d) !\n",
977 state->index, temp >> 16);
978 state->overtemp = CPU_MAX_OVERTEMP / 4;
979 } else if (temp > (state->mpu.tmax << 16)) {
980 state->overtemp++;
981 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
982 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
983 } else {
984 if (state->overtemp)
985 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
986 state->index, temp >> 16);
987 state->overtemp = 0;
989 if (state->overtemp >= CPU_MAX_OVERTEMP)
990 critical_state = 1;
991 if (state->overtemp > 0) {
992 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
993 goto do_set_fans;
996 /* Do the PID */
997 do_cpu_pid(state, temp, power);
999 /* Check clamp from dimms */
1000 fan_min = dimm_output_clamp;
1001 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1003 DBG(" CPU min mpu = %d, min dimm = %d\n",
1004 state->mpu.rminn_intake_fan, dimm_output_clamp);
1006 state->rpm = max(state->rpm, (int)fan_min);
1007 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1008 state->intake_rpm = state->rpm;
1010 do_set_fans:
1011 DBG("** CPU %d RPM: %d overtemp: %d\n",
1012 state->index, (int)state->rpm, state->overtemp);
1014 /* We should check for errors, shouldn't we ? But then, what
1015 * do we do once the error occurs ? For FCU notified fan
1016 * failures (-EFAULT) we probably want to notify userland
1017 * some way...
1019 if (state->index == 0) {
1020 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1021 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1022 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1023 } else {
1024 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1025 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1026 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1031 * Initialize the state structure for one CPU control loop
1033 static int init_cpu_state(struct cpu_pid_state *state, int index)
1035 int err;
1037 state->index = index;
1038 state->first = 1;
1039 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1040 state->overtemp = 0;
1041 state->adc_config = 0x00;
1044 if (index == 0)
1045 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1046 else if (index == 1)
1047 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1048 if (state->monitor == NULL)
1049 goto fail;
1051 if (read_eeprom(index, &state->mpu))
1052 goto fail;
1054 state->count_power = state->mpu.tguardband;
1055 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1056 printk(KERN_WARNING "Warning ! too many power history slots\n");
1057 state->count_power = CPU_POWER_HISTORY_SIZE;
1059 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1061 if (index == 0) {
1062 err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1063 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1064 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1065 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1066 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1067 } else {
1068 err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1069 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1070 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1071 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1072 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1074 if (err)
1075 printk(KERN_WARNING "Failed to create some of the atribute"
1076 "files for CPU %d\n", index);
1078 return 0;
1079 fail:
1080 state->monitor = NULL;
1082 return -ENODEV;
1086 * Dispose of the state data for one CPU control loop
1088 static void dispose_cpu_state(struct cpu_pid_state *state)
1090 if (state->monitor == NULL)
1091 return;
1093 if (state->index == 0) {
1094 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1095 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1096 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1097 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1098 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1099 } else {
1100 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1101 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1102 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1103 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1104 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1107 state->monitor = NULL;
1111 * Motherboard backside & U3 heatsink fan control loop
1113 static void do_monitor_backside(struct backside_pid_state *state)
1115 s32 temp, integral, derivative, fan_min;
1116 s64 integ_p, deriv_p, prop_p, sum;
1117 int i, rc;
1119 if (--state->ticks != 0)
1120 return;
1121 state->ticks = backside_params.interval;
1123 DBG("backside:\n");
1125 /* Check fan status */
1126 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1127 if (rc < 0) {
1128 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1129 } else
1130 state->pwm = rc;
1131 DBG(" current pwm: %d\n", state->pwm);
1133 /* Get some sensor readings */
1134 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1135 state->last_temp = temp;
1136 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1137 FIX32TOPRINT(backside_params.input_target));
1139 /* Store temperature and error in history array */
1140 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1141 state->sample_history[state->cur_sample] = temp;
1142 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1144 /* If first loop, fill the history table */
1145 if (state->first) {
1146 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1147 state->cur_sample = (state->cur_sample + 1) %
1148 BACKSIDE_PID_HISTORY_SIZE;
1149 state->sample_history[state->cur_sample] = temp;
1150 state->error_history[state->cur_sample] =
1151 temp - backside_params.input_target;
1153 state->first = 0;
1156 /* Calculate the integral term */
1157 sum = 0;
1158 integral = 0;
1159 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1160 integral += state->error_history[i];
1161 integral *= backside_params.interval;
1162 DBG(" integral: %08x\n", integral);
1163 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1164 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1165 sum += integ_p;
1167 /* Calculate the derivative term */
1168 derivative = state->error_history[state->cur_sample] -
1169 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1170 % BACKSIDE_PID_HISTORY_SIZE];
1171 derivative /= backside_params.interval;
1172 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1173 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1174 sum += deriv_p;
1176 /* Calculate the proportional term */
1177 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1178 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1179 sum += prop_p;
1181 /* Scale sum */
1182 sum >>= 36;
1184 DBG(" sum: %d\n", (int)sum);
1185 if (backside_params.additive)
1186 state->pwm += (s32)sum;
1187 else
1188 state->pwm = sum;
1190 /* Check for clamp */
1191 fan_min = (dimm_output_clamp * 100) / 14000;
1192 fan_min = max(fan_min, backside_params.output_min);
1194 state->pwm = max(state->pwm, fan_min);
1195 state->pwm = min(state->pwm, backside_params.output_max);
1197 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1198 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1202 * Initialize the state structure for the backside fan control loop
1204 static int init_backside_state(struct backside_pid_state *state)
1206 struct device_node *u3;
1207 int u3h = 1; /* conservative by default */
1208 int err;
1211 * There are different PID params for machines with U3 and machines
1212 * with U3H, pick the right ones now
1214 u3 = of_find_node_by_path("/u3@0,f8000000");
1215 if (u3 != NULL) {
1216 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1217 if (vers)
1218 if (((*vers) & 0x3f) < 0x34)
1219 u3h = 0;
1220 of_node_put(u3);
1223 if (rackmac) {
1224 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1225 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1226 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1227 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1228 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1229 backside_params.G_r = BACKSIDE_PID_G_r;
1230 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1231 backside_params.additive = 0;
1232 } else if (u3h) {
1233 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1234 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1235 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1236 backside_params.interval = BACKSIDE_PID_INTERVAL;
1237 backside_params.G_p = BACKSIDE_PID_G_p;
1238 backside_params.G_r = BACKSIDE_PID_G_r;
1239 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1240 backside_params.additive = 1;
1241 } else {
1242 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1243 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1244 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1245 backside_params.interval = BACKSIDE_PID_INTERVAL;
1246 backside_params.G_p = BACKSIDE_PID_G_p;
1247 backside_params.G_r = BACKSIDE_PID_G_r;
1248 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1249 backside_params.additive = 1;
1252 state->ticks = 1;
1253 state->first = 1;
1254 state->pwm = 50;
1256 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1257 if (state->monitor == NULL)
1258 return -ENODEV;
1260 err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1261 err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1262 if (err)
1263 printk(KERN_WARNING "Failed to create attribute file(s)"
1264 " for backside fan\n");
1266 return 0;
1270 * Dispose of the state data for the backside control loop
1272 static void dispose_backside_state(struct backside_pid_state *state)
1274 if (state->monitor == NULL)
1275 return;
1277 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1278 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1280 state->monitor = NULL;
1284 * Drives bay fan control loop
1286 static void do_monitor_drives(struct drives_pid_state *state)
1288 s32 temp, integral, derivative;
1289 s64 integ_p, deriv_p, prop_p, sum;
1290 int i, rc;
1292 if (--state->ticks != 0)
1293 return;
1294 state->ticks = DRIVES_PID_INTERVAL;
1296 DBG("drives:\n");
1298 /* Check fan status */
1299 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1300 if (rc < 0) {
1301 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1302 } else
1303 state->rpm = rc;
1304 DBG(" current rpm: %d\n", state->rpm);
1306 /* Get some sensor readings */
1307 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1308 DS1775_TEMP)) << 8;
1309 state->last_temp = temp;
1310 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1311 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1313 /* Store temperature and error in history array */
1314 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1315 state->sample_history[state->cur_sample] = temp;
1316 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1318 /* If first loop, fill the history table */
1319 if (state->first) {
1320 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1321 state->cur_sample = (state->cur_sample + 1) %
1322 DRIVES_PID_HISTORY_SIZE;
1323 state->sample_history[state->cur_sample] = temp;
1324 state->error_history[state->cur_sample] =
1325 temp - DRIVES_PID_INPUT_TARGET;
1327 state->first = 0;
1330 /* Calculate the integral term */
1331 sum = 0;
1332 integral = 0;
1333 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1334 integral += state->error_history[i];
1335 integral *= DRIVES_PID_INTERVAL;
1336 DBG(" integral: %08x\n", integral);
1337 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1338 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1339 sum += integ_p;
1341 /* Calculate the derivative term */
1342 derivative = state->error_history[state->cur_sample] -
1343 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1344 % DRIVES_PID_HISTORY_SIZE];
1345 derivative /= DRIVES_PID_INTERVAL;
1346 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1347 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1348 sum += deriv_p;
1350 /* Calculate the proportional term */
1351 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1352 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1353 sum += prop_p;
1355 /* Scale sum */
1356 sum >>= 36;
1358 DBG(" sum: %d\n", (int)sum);
1359 state->rpm += (s32)sum;
1361 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1362 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1364 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1365 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1369 * Initialize the state structure for the drives bay fan control loop
1371 static int init_drives_state(struct drives_pid_state *state)
1373 int err;
1375 state->ticks = 1;
1376 state->first = 1;
1377 state->rpm = 1000;
1379 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1380 if (state->monitor == NULL)
1381 return -ENODEV;
1383 err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1384 err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1385 if (err)
1386 printk(KERN_WARNING "Failed to create attribute file(s)"
1387 " for drives bay fan\n");
1389 return 0;
1393 * Dispose of the state data for the drives control loop
1395 static void dispose_drives_state(struct drives_pid_state *state)
1397 if (state->monitor == NULL)
1398 return;
1400 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1401 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1403 state->monitor = NULL;
1407 * DIMMs temp control loop
1409 static void do_monitor_dimms(struct dimm_pid_state *state)
1411 s32 temp, integral, derivative, fan_min;
1412 s64 integ_p, deriv_p, prop_p, sum;
1413 int i;
1415 if (--state->ticks != 0)
1416 return;
1417 state->ticks = DIMM_PID_INTERVAL;
1419 DBG("DIMM:\n");
1421 DBG(" current value: %d\n", state->output);
1423 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1424 if (temp < 0)
1425 return;
1426 temp <<= 16;
1427 state->last_temp = temp;
1428 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1429 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1431 /* Store temperature and error in history array */
1432 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1433 state->sample_history[state->cur_sample] = temp;
1434 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1436 /* If first loop, fill the history table */
1437 if (state->first) {
1438 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1439 state->cur_sample = (state->cur_sample + 1) %
1440 DIMM_PID_HISTORY_SIZE;
1441 state->sample_history[state->cur_sample] = temp;
1442 state->error_history[state->cur_sample] =
1443 temp - DIMM_PID_INPUT_TARGET;
1445 state->first = 0;
1448 /* Calculate the integral term */
1449 sum = 0;
1450 integral = 0;
1451 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1452 integral += state->error_history[i];
1453 integral *= DIMM_PID_INTERVAL;
1454 DBG(" integral: %08x\n", integral);
1455 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1456 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1457 sum += integ_p;
1459 /* Calculate the derivative term */
1460 derivative = state->error_history[state->cur_sample] -
1461 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1462 % DIMM_PID_HISTORY_SIZE];
1463 derivative /= DIMM_PID_INTERVAL;
1464 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1465 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1466 sum += deriv_p;
1468 /* Calculate the proportional term */
1469 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1470 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1471 sum += prop_p;
1473 /* Scale sum */
1474 sum >>= 36;
1476 DBG(" sum: %d\n", (int)sum);
1477 state->output = (s32)sum;
1478 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1479 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1480 dimm_output_clamp = state->output;
1482 DBG("** DIMM clamp value: %d\n", (int)state->output);
1484 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1485 fan_min = (dimm_output_clamp * 100) / 14000;
1486 fan_min = max(fan_min, backside_params.output_min);
1487 if (backside_state.pwm < fan_min) {
1488 backside_state.pwm = fan_min;
1489 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1490 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1495 * Initialize the state structure for the DIMM temp control loop
1497 static int init_dimms_state(struct dimm_pid_state *state)
1499 state->ticks = 1;
1500 state->first = 1;
1501 state->output = 4000;
1503 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1504 if (state->monitor == NULL)
1505 return -ENODEV;
1507 if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1508 printk(KERN_WARNING "Failed to create attribute file"
1509 " for DIMM temperature\n");
1511 return 0;
1515 * Dispose of the state data for the DIMM control loop
1517 static void dispose_dimms_state(struct dimm_pid_state *state)
1519 if (state->monitor == NULL)
1520 return;
1522 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1524 state->monitor = NULL;
1528 * Slots fan control loop
1530 static void do_monitor_slots(struct slots_pid_state *state)
1532 s32 temp, integral, derivative;
1533 s64 integ_p, deriv_p, prop_p, sum;
1534 int i, rc;
1536 if (--state->ticks != 0)
1537 return;
1538 state->ticks = SLOTS_PID_INTERVAL;
1540 DBG("slots:\n");
1542 /* Check fan status */
1543 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1544 if (rc < 0) {
1545 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1546 } else
1547 state->pwm = rc;
1548 DBG(" current pwm: %d\n", state->pwm);
1550 /* Get some sensor readings */
1551 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1552 DS1775_TEMP)) << 8;
1553 state->last_temp = temp;
1554 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1555 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1557 /* Store temperature and error in history array */
1558 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1559 state->sample_history[state->cur_sample] = temp;
1560 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1562 /* If first loop, fill the history table */
1563 if (state->first) {
1564 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1565 state->cur_sample = (state->cur_sample + 1) %
1566 SLOTS_PID_HISTORY_SIZE;
1567 state->sample_history[state->cur_sample] = temp;
1568 state->error_history[state->cur_sample] =
1569 temp - SLOTS_PID_INPUT_TARGET;
1571 state->first = 0;
1574 /* Calculate the integral term */
1575 sum = 0;
1576 integral = 0;
1577 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1578 integral += state->error_history[i];
1579 integral *= SLOTS_PID_INTERVAL;
1580 DBG(" integral: %08x\n", integral);
1581 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1582 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1583 sum += integ_p;
1585 /* Calculate the derivative term */
1586 derivative = state->error_history[state->cur_sample] -
1587 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1588 % SLOTS_PID_HISTORY_SIZE];
1589 derivative /= SLOTS_PID_INTERVAL;
1590 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1591 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1592 sum += deriv_p;
1594 /* Calculate the proportional term */
1595 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1596 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1597 sum += prop_p;
1599 /* Scale sum */
1600 sum >>= 36;
1602 DBG(" sum: %d\n", (int)sum);
1603 state->pwm = (s32)sum;
1605 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1606 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1608 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1609 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1613 * Initialize the state structure for the slots bay fan control loop
1615 static int init_slots_state(struct slots_pid_state *state)
1617 int err;
1619 state->ticks = 1;
1620 state->first = 1;
1621 state->pwm = 50;
1623 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1624 if (state->monitor == NULL)
1625 return -ENODEV;
1627 err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1628 err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1629 if (err)
1630 printk(KERN_WARNING "Failed to create attribute file(s)"
1631 " for slots bay fan\n");
1633 return 0;
1637 * Dispose of the state data for the slots control loop
1639 static void dispose_slots_state(struct slots_pid_state *state)
1641 if (state->monitor == NULL)
1642 return;
1644 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1645 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1647 state->monitor = NULL;
1651 static int call_critical_overtemp(void)
1653 char *argv[] = { critical_overtemp_path, NULL };
1654 static char *envp[] = { "HOME=/",
1655 "TERM=linux",
1656 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1657 NULL };
1659 return call_usermodehelper(critical_overtemp_path,
1660 argv, envp, UMH_WAIT_EXEC);
1665 * Here's the kernel thread that calls the various control loops
1667 static int main_control_loop(void *x)
1669 DBG("main_control_loop started\n");
1671 mutex_lock(&driver_lock);
1673 if (start_fcu() < 0) {
1674 printk(KERN_ERR "kfand: failed to start FCU\n");
1675 mutex_unlock(&driver_lock);
1676 goto out;
1679 /* Set the PCI fan once for now on non-RackMac */
1680 if (!rackmac)
1681 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1683 /* Initialize ADCs */
1684 initialize_adc(&cpu_state[0]);
1685 if (cpu_state[1].monitor != NULL)
1686 initialize_adc(&cpu_state[1]);
1688 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1690 mutex_unlock(&driver_lock);
1692 while (state == state_attached) {
1693 unsigned long elapsed, start;
1695 start = jiffies;
1697 mutex_lock(&driver_lock);
1699 /* Tickle the FCU just in case */
1700 if (--fcu_tickle_ticks < 0) {
1701 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1702 tickle_fcu();
1705 /* First, we always calculate the new DIMMs state on an Xserve */
1706 if (rackmac)
1707 do_monitor_dimms(&dimms_state);
1709 /* Then, the CPUs */
1710 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1711 do_monitor_cpu_combined();
1712 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1713 do_monitor_cpu_rack(&cpu_state[0]);
1714 if (cpu_state[1].monitor != NULL)
1715 do_monitor_cpu_rack(&cpu_state[1]);
1716 // better deal with UP
1717 } else {
1718 do_monitor_cpu_split(&cpu_state[0]);
1719 if (cpu_state[1].monitor != NULL)
1720 do_monitor_cpu_split(&cpu_state[1]);
1721 // better deal with UP
1723 /* Then, the rest */
1724 do_monitor_backside(&backside_state);
1725 if (rackmac)
1726 do_monitor_slots(&slots_state);
1727 else
1728 do_monitor_drives(&drives_state);
1729 mutex_unlock(&driver_lock);
1731 if (critical_state == 1) {
1732 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1733 printk(KERN_WARNING "Attempting to shut down...\n");
1734 if (call_critical_overtemp()) {
1735 printk(KERN_WARNING "Can't call %s, power off now!\n",
1736 critical_overtemp_path);
1737 machine_power_off();
1740 if (critical_state > 0)
1741 critical_state++;
1742 if (critical_state > MAX_CRITICAL_STATE) {
1743 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1744 machine_power_off();
1747 elapsed = jiffies - start;
1748 if (elapsed < HZ)
1749 schedule_timeout_interruptible(HZ - elapsed);
1752 out:
1753 DBG("main_control_loop ended\n");
1755 ctrl_task = 0;
1756 complete_and_exit(&ctrl_complete, 0);
1760 * Dispose the control loops when tearing down
1762 static void dispose_control_loops(void)
1764 dispose_cpu_state(&cpu_state[0]);
1765 dispose_cpu_state(&cpu_state[1]);
1766 dispose_backside_state(&backside_state);
1767 dispose_drives_state(&drives_state);
1768 dispose_slots_state(&slots_state);
1769 dispose_dimms_state(&dimms_state);
1773 * Create the control loops. U3-0 i2c bus is up, so we can now
1774 * get to the various sensors
1776 static int create_control_loops(void)
1778 struct device_node *np;
1780 /* Count CPUs from the device-tree, we don't care how many are
1781 * actually used by Linux
1783 cpu_count = 0;
1784 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1785 cpu_count++;
1787 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1789 /* Decide the type of PID algorithm to use based on the presence of
1790 * the pumps, though that may not be the best way, that is good enough
1791 * for now
1793 if (rackmac)
1794 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1795 else if (of_machine_is_compatible("PowerMac7,3")
1796 && (cpu_count > 1)
1797 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1798 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1799 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1800 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1801 } else
1802 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1804 /* Create control loops for everything. If any fail, everything
1805 * fails
1807 if (init_cpu_state(&cpu_state[0], 0))
1808 goto fail;
1809 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1810 fetch_cpu_pumps_minmax();
1812 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1813 goto fail;
1814 if (init_backside_state(&backside_state))
1815 goto fail;
1816 if (rackmac && init_dimms_state(&dimms_state))
1817 goto fail;
1818 if (rackmac && init_slots_state(&slots_state))
1819 goto fail;
1820 if (!rackmac && init_drives_state(&drives_state))
1821 goto fail;
1823 DBG("all control loops up !\n");
1825 return 0;
1827 fail:
1828 DBG("failure creating control loops, disposing\n");
1830 dispose_control_loops();
1832 return -ENODEV;
1836 * Start the control loops after everything is up, that is create
1837 * the thread that will make them run
1839 static void start_control_loops(void)
1841 init_completion(&ctrl_complete);
1843 ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1847 * Stop the control loops when tearing down
1849 static void stop_control_loops(void)
1851 if (ctrl_task)
1852 wait_for_completion(&ctrl_complete);
1856 * Attach to the i2c FCU after detecting U3-1 bus
1858 static int attach_fcu(void)
1860 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1861 if (fcu == NULL)
1862 return -ENODEV;
1864 DBG("FCU attached\n");
1866 return 0;
1870 * Detach from the i2c FCU when tearing down
1872 static void detach_fcu(void)
1874 fcu = NULL;
1878 * Attach to the i2c controller. We probe the various chips based
1879 * on the device-tree nodes and build everything for the driver to
1880 * run, we then kick the driver monitoring thread
1882 static int therm_pm72_attach(struct i2c_adapter *adapter)
1884 mutex_lock(&driver_lock);
1886 /* Check state */
1887 if (state == state_detached)
1888 state = state_attaching;
1889 if (state != state_attaching) {
1890 mutex_unlock(&driver_lock);
1891 return 0;
1894 /* Check if we are looking for one of these */
1895 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
1896 u3_0 = adapter;
1897 DBG("found U3-0\n");
1898 if (k2 || !rackmac)
1899 if (create_control_loops())
1900 u3_0 = NULL;
1901 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
1902 u3_1 = adapter;
1903 DBG("found U3-1, attaching FCU\n");
1904 if (attach_fcu())
1905 u3_1 = NULL;
1906 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
1907 k2 = adapter;
1908 DBG("Found K2\n");
1909 if (u3_0 && rackmac)
1910 if (create_control_loops())
1911 k2 = NULL;
1913 /* We got all we need, start control loops */
1914 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
1915 DBG("everything up, starting control loops\n");
1916 state = state_attached;
1917 start_control_loops();
1919 mutex_unlock(&driver_lock);
1921 return 0;
1924 static int therm_pm72_probe(struct i2c_client *client,
1925 const struct i2c_device_id *id)
1927 /* Always succeed, the real work was done in therm_pm72_attach() */
1928 return 0;
1932 * Called when any of the devices which participates into thermal management
1933 * is going away.
1935 static int therm_pm72_remove(struct i2c_client *client)
1937 struct i2c_adapter *adapter = client->adapter;
1939 mutex_lock(&driver_lock);
1941 if (state != state_detached)
1942 state = state_detaching;
1944 /* Stop control loops if any */
1945 DBG("stopping control loops\n");
1946 mutex_unlock(&driver_lock);
1947 stop_control_loops();
1948 mutex_lock(&driver_lock);
1950 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
1951 DBG("lost U3-0, disposing control loops\n");
1952 dispose_control_loops();
1953 u3_0 = NULL;
1956 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
1957 DBG("lost U3-1, detaching FCU\n");
1958 detach_fcu();
1959 u3_1 = NULL;
1961 if (u3_0 == NULL && u3_1 == NULL)
1962 state = state_detached;
1964 mutex_unlock(&driver_lock);
1966 return 0;
1970 * i2c_driver structure to attach to the host i2c controller
1973 static const struct i2c_device_id therm_pm72_id[] = {
1975 * Fake device name, thermal management is done by several
1976 * chips but we don't need to differentiate between them at
1977 * this point.
1979 { "therm_pm72", 0 },
1983 static struct i2c_driver therm_pm72_driver = {
1984 .driver = {
1985 .name = "therm_pm72",
1987 .attach_adapter = therm_pm72_attach,
1988 .probe = therm_pm72_probe,
1989 .remove = therm_pm72_remove,
1990 .id_table = therm_pm72_id,
1993 static int fan_check_loc_match(const char *loc, int fan)
1995 char tmp[64];
1996 char *c, *e;
1998 strlcpy(tmp, fcu_fans[fan].loc, 64);
2000 c = tmp;
2001 for (;;) {
2002 e = strchr(c, ',');
2003 if (e)
2004 *e = 0;
2005 if (strcmp(loc, c) == 0)
2006 return 1;
2007 if (e == NULL)
2008 break;
2009 c = e + 1;
2011 return 0;
2014 static void fcu_lookup_fans(struct device_node *fcu_node)
2016 struct device_node *np = NULL;
2017 int i;
2019 /* The table is filled by default with values that are suitable
2020 * for the old machines without device-tree informations. We scan
2021 * the device-tree and override those values with whatever is
2022 * there
2025 DBG("Looking up FCU controls in device-tree...\n");
2027 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2028 int type = -1;
2029 const char *loc;
2030 const u32 *reg;
2032 DBG(" control: %s, type: %s\n", np->name, np->type);
2034 /* Detect control type */
2035 if (!strcmp(np->type, "fan-rpm-control") ||
2036 !strcmp(np->type, "fan-rpm"))
2037 type = FCU_FAN_RPM;
2038 if (!strcmp(np->type, "fan-pwm-control") ||
2039 !strcmp(np->type, "fan-pwm"))
2040 type = FCU_FAN_PWM;
2041 /* Only care about fans for now */
2042 if (type == -1)
2043 continue;
2045 /* Lookup for a matching location */
2046 loc = of_get_property(np, "location", NULL);
2047 reg = of_get_property(np, "reg", NULL);
2048 if (loc == NULL || reg == NULL)
2049 continue;
2050 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2052 for (i = 0; i < FCU_FAN_COUNT; i++) {
2053 int fan_id;
2055 if (!fan_check_loc_match(loc, i))
2056 continue;
2057 DBG(" location match, index: %d\n", i);
2058 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2059 if (type != fcu_fans[i].type) {
2060 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2061 "in device-tree for %s\n", np->full_name);
2062 break;
2064 if (type == FCU_FAN_RPM)
2065 fan_id = ((*reg) - 0x10) / 2;
2066 else
2067 fan_id = ((*reg) - 0x30) / 2;
2068 if (fan_id > 7) {
2069 printk(KERN_WARNING "therm_pm72: Can't parse "
2070 "fan ID in device-tree for %s\n", np->full_name);
2071 break;
2073 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2074 fcu_fans[i].id = fan_id;
2078 /* Now dump the array */
2079 printk(KERN_INFO "Detected fan controls:\n");
2080 for (i = 0; i < FCU_FAN_COUNT; i++) {
2081 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2082 continue;
2083 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2084 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2085 fcu_fans[i].id, fcu_fans[i].loc);
2089 static int fcu_of_probe(struct platform_device* dev, const struct of_device_id *match)
2091 state = state_detached;
2093 /* Lookup the fans in the device tree */
2094 fcu_lookup_fans(dev->dev.of_node);
2096 /* Add the driver */
2097 return i2c_add_driver(&therm_pm72_driver);
2100 static int fcu_of_remove(struct platform_device* dev)
2102 i2c_del_driver(&therm_pm72_driver);
2104 return 0;
2107 static const struct of_device_id fcu_match[] =
2110 .type = "fcu",
2115 static struct of_platform_driver fcu_of_platform_driver =
2117 .driver = {
2118 .name = "temperature",
2119 .owner = THIS_MODULE,
2120 .of_match_table = fcu_match,
2122 .probe = fcu_of_probe,
2123 .remove = fcu_of_remove
2127 * Check machine type, attach to i2c controller
2129 static int __init therm_pm72_init(void)
2131 struct device_node *np;
2133 rackmac = of_machine_is_compatible("RackMac3,1");
2135 if (!of_machine_is_compatible("PowerMac7,2") &&
2136 !of_machine_is_compatible("PowerMac7,3") &&
2137 !rackmac)
2138 return -ENODEV;
2140 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2142 np = of_find_node_by_type(NULL, "fcu");
2143 if (np == NULL) {
2144 /* Some machines have strangely broken device-tree */
2145 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2146 if (np == NULL) {
2147 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2148 return -ENODEV;
2151 of_dev = of_platform_device_create(np, "temperature", NULL);
2152 if (of_dev == NULL) {
2153 printk(KERN_ERR "Can't register FCU platform device !\n");
2154 return -ENODEV;
2157 of_register_platform_driver(&fcu_of_platform_driver);
2159 return 0;
2162 static void __exit therm_pm72_exit(void)
2164 of_unregister_platform_driver(&fcu_of_platform_driver);
2166 if (of_dev)
2167 of_device_unregister(of_dev);
2170 module_init(therm_pm72_init);
2171 module_exit(therm_pm72_exit);
2173 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2174 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2175 MODULE_LICENSE("GPL");