V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / macintosh / therm_pm72.c
blobe43554e754a46f09bebf28732857fac8f2794441
1 /*
2 * Device driver for the thermostats & fan controller of the
3 * Apple G5 "PowerMac7,2" desktop machines.
5 * (c) Copyright IBM Corp. 2003-2004
7 * Maintained by: Benjamin Herrenschmidt
8 * <benh@kernel.crashing.org>
9 *
11 * The algorithm used is the PID control algorithm, used the same
12 * way the published Darwin code does, using the same values that
13 * are present in the Darwin 7.0 snapshot property lists.
15 * As far as the CPUs control loops are concerned, I use the
16 * calibration & PID constants provided by the EEPROM,
17 * I do _not_ embed any value from the property lists, as the ones
18 * provided by Darwin 7.0 seem to always have an older version that
19 * what I've seen on the actual computers.
20 * It would be interesting to verify that though. Darwin has a
21 * version code of 1.0.0d11 for all control loops it seems, while
22 * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
24 * Darwin doesn't provide source to all parts, some missing
25 * bits like the AppleFCU driver or the actual scale of some
26 * of the values returned by sensors had to be "guessed" some
27 * way... or based on what Open Firmware does.
29 * I didn't yet figure out how to get the slots power consumption
30 * out of the FCU, so that part has not been implemented yet and
31 * the slots fan is set to a fixed 50% PWM, hoping this value is
32 * safe enough ...
34 * Note: I have observed strange oscillations of the CPU control
35 * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
36 * oscillates slowly (over several minutes) between the minimum
37 * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
38 * this, it could be some incorrect constant or an error in the
39 * way I ported the algorithm, or it could be just normal. I
40 * don't have full understanding on the way Apple tweaked the PID
41 * algorithm for the CPU control, it is definitely not a standard
42 * implementation...
44 * TODO: - Check MPU structure version/signature
45 * - Add things like /sbin/overtemp for non-critical
46 * overtemp conditions so userland can take some policy
47 * decisions, like slewing down CPUs
48 * - Deal with fan and i2c failures in a better way
49 * - Maybe do a generic PID based on params used for
50 * U3 and Drives ? Definitely need to factor code a bit
51 * bettter... also make sensor detection more robust using
52 * the device-tree to probe for them
53 * - Figure out how to get the slots consumption and set the
54 * slots fan accordingly
56 * History:
58 * Nov. 13, 2003 : 0.5
59 * - First release
61 * Nov. 14, 2003 : 0.6
62 * - Read fan speed from FCU, low level fan routines now deal
63 * with errors & check fan status, though higher level don't
64 * do much.
65 * - Move a bunch of definitions to .h file
67 * Nov. 18, 2003 : 0.7
68 * - Fix build on ppc64 kernel
69 * - Move back statics definitions to .c file
70 * - Avoid calling schedule_timeout with a negative number
72 * Dec. 18, 2003 : 0.8
73 * - Fix typo when reading back fan speed on 2 CPU machines
75 * Mar. 11, 2004 : 0.9
76 * - Rework code accessing the ADC chips, make it more robust and
77 * closer to the chip spec. Also make sure it is configured properly,
78 * I've seen yet unexplained cases where on startup, I would have stale
79 * values in the configuration register
80 * - Switch back to use of target fan speed for PID, thus lowering
81 * pressure on i2c
83 * Oct. 20, 2004 : 1.1
84 * - Add device-tree lookup for fan IDs, should detect liquid cooling
85 * pumps when present
86 * - Enable driver for PowerMac7,3 machines
87 * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
88 * - Add new CPU cooling algorithm for machines with liquid cooling
89 * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
90 * - Fix a signed/unsigned compare issue in some PID loops
92 * Mar. 10, 2005 : 1.2
93 * - Add basic support for Xserve G5
94 * - Retreive pumps min/max from EEPROM image in device-tree (broken)
95 * - Use min/max macros here or there
96 * - Latest darwin updated U3H min fan speed to 20% PWM
98 * July. 06, 2006 : 1.3
99 * - Fix setting of RPM fans on Xserve G5 (they were going too fast)
100 * - Add missing slots fan control loop for Xserve G5
101 * - Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
102 * still can't properly implement the control loop for these, so let's
103 * reduce the noise a little bit, it appears that 40% still gives us
104 * a pretty good air flow
105 * - Add code to "tickle" the FCU regulary so it doesn't think that
106 * we are gone while in fact, the machine just didn't need any fan
107 * speed change lately
111 #include <linux/types.h>
112 #include <linux/module.h>
113 #include <linux/errno.h>
114 #include <linux/kernel.h>
115 #include <linux/delay.h>
116 #include <linux/sched.h>
117 #include <linux/slab.h>
118 #include <linux/init.h>
119 #include <linux/spinlock.h>
120 #include <linux/wait.h>
121 #include <linux/reboot.h>
122 #include <linux/kmod.h>
123 #include <linux/i2c.h>
124 #include <asm/prom.h>
125 #include <asm/machdep.h>
126 #include <asm/io.h>
127 #include <asm/system.h>
128 #include <asm/sections.h>
129 #include <asm/of_device.h>
130 #include <asm/macio.h>
131 #include <asm/of_platform.h>
133 #include "therm_pm72.h"
135 #define VERSION "1.3"
137 #undef DEBUG
139 #ifdef DEBUG
140 #define DBG(args...) printk(args)
141 #else
142 #define DBG(args...) do { } while(0)
143 #endif
147 * Driver statics
150 static struct of_device * of_dev;
151 static struct i2c_adapter * u3_0;
152 static struct i2c_adapter * u3_1;
153 static struct i2c_adapter * k2;
154 static struct i2c_client * fcu;
155 static struct cpu_pid_state cpu_state[2];
156 static struct basckside_pid_params backside_params;
157 static struct backside_pid_state backside_state;
158 static struct drives_pid_state drives_state;
159 static struct dimm_pid_state dimms_state;
160 static struct slots_pid_state slots_state;
161 static int state;
162 static int cpu_count;
163 static int cpu_pid_type;
164 static pid_t ctrl_task;
165 static struct completion ctrl_complete;
166 static int critical_state;
167 static int rackmac;
168 static s32 dimm_output_clamp;
169 static int fcu_rpm_shift;
170 static int fcu_tickle_ticks;
171 static DECLARE_MUTEX(driver_lock);
174 * We have 3 types of CPU PID control. One is "split" old style control
175 * for intake & exhaust fans, the other is "combined" control for both
176 * CPUs that also deals with the pumps when present. To be "compatible"
177 * with OS X at this point, we only use "COMBINED" on the machines that
178 * are identified as having the pumps (though that identification is at
179 * least dodgy). Ultimately, we could probably switch completely to this
180 * algorithm provided we hack it to deal with the UP case
182 #define CPU_PID_TYPE_SPLIT 0
183 #define CPU_PID_TYPE_COMBINED 1
184 #define CPU_PID_TYPE_RACKMAC 2
187 * This table describes all fans in the FCU. The "id" and "type" values
188 * are defaults valid for all earlier machines. Newer machines will
189 * eventually override the table content based on the device-tree
191 struct fcu_fan_table
193 char* loc; /* location code */
194 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
195 int id; /* id or -1 */
198 #define FCU_FAN_RPM 0
199 #define FCU_FAN_PWM 1
201 #define FCU_FAN_ABSENT_ID -1
203 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
205 struct fcu_fan_table fcu_fans[] = {
206 [BACKSIDE_FAN_PWM_INDEX] = {
207 .loc = "BACKSIDE,SYS CTRLR FAN",
208 .type = FCU_FAN_PWM,
209 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
211 [DRIVES_FAN_RPM_INDEX] = {
212 .loc = "DRIVE BAY",
213 .type = FCU_FAN_RPM,
214 .id = DRIVES_FAN_RPM_DEFAULT_ID,
216 [SLOTS_FAN_PWM_INDEX] = {
217 .loc = "SLOT,PCI FAN",
218 .type = FCU_FAN_PWM,
219 .id = SLOTS_FAN_PWM_DEFAULT_ID,
221 [CPUA_INTAKE_FAN_RPM_INDEX] = {
222 .loc = "CPU A INTAKE",
223 .type = FCU_FAN_RPM,
224 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
226 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
227 .loc = "CPU A EXHAUST",
228 .type = FCU_FAN_RPM,
229 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
231 [CPUB_INTAKE_FAN_RPM_INDEX] = {
232 .loc = "CPU B INTAKE",
233 .type = FCU_FAN_RPM,
234 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
236 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
237 .loc = "CPU B EXHAUST",
238 .type = FCU_FAN_RPM,
239 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
241 /* pumps aren't present by default, have to be looked up in the
242 * device-tree
244 [CPUA_PUMP_RPM_INDEX] = {
245 .loc = "CPU A PUMP",
246 .type = FCU_FAN_RPM,
247 .id = FCU_FAN_ABSENT_ID,
249 [CPUB_PUMP_RPM_INDEX] = {
250 .loc = "CPU B PUMP",
251 .type = FCU_FAN_RPM,
252 .id = FCU_FAN_ABSENT_ID,
254 /* Xserve fans */
255 [CPU_A1_FAN_RPM_INDEX] = {
256 .loc = "CPU A 1",
257 .type = FCU_FAN_RPM,
258 .id = FCU_FAN_ABSENT_ID,
260 [CPU_A2_FAN_RPM_INDEX] = {
261 .loc = "CPU A 2",
262 .type = FCU_FAN_RPM,
263 .id = FCU_FAN_ABSENT_ID,
265 [CPU_A3_FAN_RPM_INDEX] = {
266 .loc = "CPU A 3",
267 .type = FCU_FAN_RPM,
268 .id = FCU_FAN_ABSENT_ID,
270 [CPU_B1_FAN_RPM_INDEX] = {
271 .loc = "CPU B 1",
272 .type = FCU_FAN_RPM,
273 .id = FCU_FAN_ABSENT_ID,
275 [CPU_B2_FAN_RPM_INDEX] = {
276 .loc = "CPU B 2",
277 .type = FCU_FAN_RPM,
278 .id = FCU_FAN_ABSENT_ID,
280 [CPU_B3_FAN_RPM_INDEX] = {
281 .loc = "CPU B 3",
282 .type = FCU_FAN_RPM,
283 .id = FCU_FAN_ABSENT_ID,
288 * i2c_driver structure to attach to the host i2c controller
291 static int therm_pm72_attach(struct i2c_adapter *adapter);
292 static int therm_pm72_detach(struct i2c_adapter *adapter);
294 static struct i2c_driver therm_pm72_driver =
296 .driver = {
297 .name = "therm_pm72",
299 .attach_adapter = therm_pm72_attach,
300 .detach_adapter = therm_pm72_detach,
304 * Utility function to create an i2c_client structure and
305 * attach it to one of u3 adapters
307 static struct i2c_client *attach_i2c_chip(int id, const char *name)
309 struct i2c_client *clt;
310 struct i2c_adapter *adap;
312 if (id & 0x200)
313 adap = k2;
314 else if (id & 0x100)
315 adap = u3_1;
316 else
317 adap = u3_0;
318 if (adap == NULL)
319 return NULL;
321 clt = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
322 if (clt == NULL)
323 return NULL;
325 clt->addr = (id >> 1) & 0x7f;
326 clt->adapter = adap;
327 clt->driver = &therm_pm72_driver;
328 strncpy(clt->name, name, I2C_NAME_SIZE-1);
330 if (i2c_attach_client(clt)) {
331 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
332 kfree(clt);
333 return NULL;
335 return clt;
339 * Utility function to get rid of the i2c_client structure
340 * (will also detach from the adapter hopepfully)
342 static void detach_i2c_chip(struct i2c_client *clt)
344 i2c_detach_client(clt);
345 kfree(clt);
349 * Here are the i2c chip access wrappers
352 static void initialize_adc(struct cpu_pid_state *state)
354 int rc;
355 u8 buf[2];
357 /* Read ADC the configuration register and cache it. We
358 * also make sure Config2 contains proper values, I've seen
359 * cases where we got stale grabage in there, thus preventing
360 * proper reading of conv. values
363 /* Clear Config2 */
364 buf[0] = 5;
365 buf[1] = 0;
366 i2c_master_send(state->monitor, buf, 2);
368 /* Read & cache Config1 */
369 buf[0] = 1;
370 rc = i2c_master_send(state->monitor, buf, 1);
371 if (rc > 0) {
372 rc = i2c_master_recv(state->monitor, buf, 1);
373 if (rc > 0) {
374 state->adc_config = buf[0];
375 DBG("ADC config reg: %02x\n", state->adc_config);
376 /* Disable shutdown mode */
377 state->adc_config &= 0xfe;
378 buf[0] = 1;
379 buf[1] = state->adc_config;
380 rc = i2c_master_send(state->monitor, buf, 2);
383 if (rc <= 0)
384 printk(KERN_ERR "therm_pm72: Error reading ADC config"
385 " register !\n");
388 static int read_smon_adc(struct cpu_pid_state *state, int chan)
390 int rc, data, tries = 0;
391 u8 buf[2];
393 for (;;) {
394 /* Set channel */
395 buf[0] = 1;
396 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
397 rc = i2c_master_send(state->monitor, buf, 2);
398 if (rc <= 0)
399 goto error;
400 /* Wait for convertion */
401 msleep(1);
402 /* Switch to data register */
403 buf[0] = 4;
404 rc = i2c_master_send(state->monitor, buf, 1);
405 if (rc <= 0)
406 goto error;
407 /* Read result */
408 rc = i2c_master_recv(state->monitor, buf, 2);
409 if (rc < 0)
410 goto error;
411 data = ((u16)buf[0]) << 8 | (u16)buf[1];
412 return data >> 6;
413 error:
414 DBG("Error reading ADC, retrying...\n");
415 if (++tries > 10) {
416 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
417 return -1;
419 msleep(10);
423 static int read_lm87_reg(struct i2c_client * chip, int reg)
425 int rc, tries = 0;
426 u8 buf;
428 for (;;) {
429 /* Set address */
430 buf = (u8)reg;
431 rc = i2c_master_send(chip, &buf, 1);
432 if (rc <= 0)
433 goto error;
434 rc = i2c_master_recv(chip, &buf, 1);
435 if (rc <= 0)
436 goto error;
437 return (int)buf;
438 error:
439 DBG("Error reading LM87, retrying...\n");
440 if (++tries > 10) {
441 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
442 return -1;
444 msleep(10);
448 static int fan_read_reg(int reg, unsigned char *buf, int nb)
450 int tries, nr, nw;
452 buf[0] = reg;
453 tries = 0;
454 for (;;) {
455 nw = i2c_master_send(fcu, buf, 1);
456 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
457 break;
458 msleep(10);
459 ++tries;
461 if (nw <= 0) {
462 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
463 return -EIO;
465 tries = 0;
466 for (;;) {
467 nr = i2c_master_recv(fcu, buf, nb);
468 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
469 break;
470 msleep(10);
471 ++tries;
473 if (nr <= 0)
474 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
475 return nr;
478 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
480 int tries, nw;
481 unsigned char buf[16];
483 buf[0] = reg;
484 memcpy(buf+1, ptr, nb);
485 ++nb;
486 tries = 0;
487 for (;;) {
488 nw = i2c_master_send(fcu, buf, nb);
489 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
490 break;
491 msleep(10);
492 ++tries;
494 if (nw < 0)
495 printk(KERN_ERR "Failure writing to FCU: %d", nw);
496 return nw;
499 static int start_fcu(void)
501 unsigned char buf = 0xff;
502 int rc;
504 rc = fan_write_reg(0xe, &buf, 1);
505 if (rc < 0)
506 return -EIO;
507 rc = fan_write_reg(0x2e, &buf, 1);
508 if (rc < 0)
509 return -EIO;
510 rc = fan_read_reg(0, &buf, 1);
511 if (rc < 0)
512 return -EIO;
513 fcu_rpm_shift = (buf == 1) ? 2 : 3;
514 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
515 fcu_rpm_shift);
517 return 0;
520 static int set_rpm_fan(int fan_index, int rpm)
522 unsigned char buf[2];
523 int rc, id, min, max;
525 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
526 return -EINVAL;
527 id = fcu_fans[fan_index].id;
528 if (id == FCU_FAN_ABSENT_ID)
529 return -EINVAL;
531 min = 2400 >> fcu_rpm_shift;
532 max = 56000 >> fcu_rpm_shift;
534 if (rpm < min)
535 rpm = min;
536 else if (rpm > max)
537 rpm = max;
538 buf[0] = rpm >> (8 - fcu_rpm_shift);
539 buf[1] = rpm << fcu_rpm_shift;
540 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
541 if (rc < 0)
542 return -EIO;
543 return 0;
546 static int get_rpm_fan(int fan_index, int programmed)
548 unsigned char failure;
549 unsigned char active;
550 unsigned char buf[2];
551 int rc, id, reg_base;
553 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
554 return -EINVAL;
555 id = fcu_fans[fan_index].id;
556 if (id == FCU_FAN_ABSENT_ID)
557 return -EINVAL;
559 rc = fan_read_reg(0xb, &failure, 1);
560 if (rc != 1)
561 return -EIO;
562 if ((failure & (1 << id)) != 0)
563 return -EFAULT;
564 rc = fan_read_reg(0xd, &active, 1);
565 if (rc != 1)
566 return -EIO;
567 if ((active & (1 << id)) == 0)
568 return -ENXIO;
570 /* Programmed value or real current speed */
571 reg_base = programmed ? 0x10 : 0x11;
572 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
573 if (rc != 2)
574 return -EIO;
576 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
579 static int set_pwm_fan(int fan_index, int pwm)
581 unsigned char buf[2];
582 int rc, id;
584 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
585 return -EINVAL;
586 id = fcu_fans[fan_index].id;
587 if (id == FCU_FAN_ABSENT_ID)
588 return -EINVAL;
590 if (pwm < 10)
591 pwm = 10;
592 else if (pwm > 100)
593 pwm = 100;
594 pwm = (pwm * 2559) / 1000;
595 buf[0] = pwm;
596 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
597 if (rc < 0)
598 return rc;
599 return 0;
602 static int get_pwm_fan(int fan_index)
604 unsigned char failure;
605 unsigned char active;
606 unsigned char buf[2];
607 int rc, id;
609 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
610 return -EINVAL;
611 id = fcu_fans[fan_index].id;
612 if (id == FCU_FAN_ABSENT_ID)
613 return -EINVAL;
615 rc = fan_read_reg(0x2b, &failure, 1);
616 if (rc != 1)
617 return -EIO;
618 if ((failure & (1 << id)) != 0)
619 return -EFAULT;
620 rc = fan_read_reg(0x2d, &active, 1);
621 if (rc != 1)
622 return -EIO;
623 if ((active & (1 << id)) == 0)
624 return -ENXIO;
626 /* Programmed value or real current speed */
627 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
628 if (rc != 1)
629 return -EIO;
631 return (buf[0] * 1000) / 2559;
634 static void tickle_fcu(void)
636 int pwm;
638 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
640 DBG("FCU Tickle, slots fan is: %d\n", pwm);
641 if (pwm < 0)
642 pwm = 100;
644 if (!rackmac) {
645 pwm = SLOTS_FAN_DEFAULT_PWM;
646 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
647 pwm = SLOTS_PID_OUTPUT_MIN;
649 /* That is hopefully enough to make the FCU happy */
650 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
655 * Utility routine to read the CPU calibration EEPROM data
656 * from the device-tree
658 static int read_eeprom(int cpu, struct mpu_data *out)
660 struct device_node *np;
661 char nodename[64];
662 const u8 *data;
663 int len;
665 /* prom.c routine for finding a node by path is a bit brain dead
666 * and requires exact @xxx unit numbers. This is a bit ugly but
667 * will work for these machines
669 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
670 np = of_find_node_by_path(nodename);
671 if (np == NULL) {
672 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
673 return -ENODEV;
675 data = of_get_property(np, "cpuid", &len);
676 if (data == NULL) {
677 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
678 of_node_put(np);
679 return -ENODEV;
681 memcpy(out, data, sizeof(struct mpu_data));
682 of_node_put(np);
684 return 0;
687 static void fetch_cpu_pumps_minmax(void)
689 struct cpu_pid_state *state0 = &cpu_state[0];
690 struct cpu_pid_state *state1 = &cpu_state[1];
691 u16 pump_min = 0, pump_max = 0xffff;
692 u16 tmp[4];
694 /* Try to fetch pumps min/max infos from eeprom */
696 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
697 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
698 pump_min = max(pump_min, tmp[0]);
699 pump_max = min(pump_max, tmp[1]);
701 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
702 pump_min = max(pump_min, tmp[2]);
703 pump_max = min(pump_max, tmp[3]);
706 /* Double check the values, this _IS_ needed as the EEPROM on
707 * some dual 2.5Ghz G5s seem, at least, to have both min & max
708 * same to the same value ... (grrrr)
710 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
711 pump_min = CPU_PUMP_OUTPUT_MIN;
712 pump_max = CPU_PUMP_OUTPUT_MAX;
715 state0->pump_min = state1->pump_min = pump_min;
716 state0->pump_max = state1->pump_max = pump_max;
720 * Now, unfortunately, sysfs doesn't give us a nice void * we could
721 * pass around to the attribute functions, so we don't really have
722 * choice but implement a bunch of them...
724 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
725 * the input twice... I accept patches :)
727 #define BUILD_SHOW_FUNC_FIX(name, data) \
728 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
730 ssize_t r; \
731 down(&driver_lock); \
732 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
733 up(&driver_lock); \
734 return r; \
736 #define BUILD_SHOW_FUNC_INT(name, data) \
737 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
739 return sprintf(buf, "%d", data); \
742 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
743 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
744 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
745 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
746 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
748 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
749 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
750 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
751 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
752 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
754 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
755 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
757 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
758 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
760 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
761 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
763 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
765 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
766 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
767 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
768 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
769 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
771 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
772 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
773 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
774 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
775 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
777 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
778 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
780 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
781 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
783 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
784 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
786 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
789 * CPUs fans control loop
792 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
794 s32 ltemp, volts, amps;
795 int index, rc = 0;
797 /* Default (in case of error) */
798 *temp = state->cur_temp;
799 *power = state->cur_power;
801 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
802 index = (state->index == 0) ?
803 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
804 else
805 index = (state->index == 0) ?
806 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
808 /* Read current fan status */
809 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
810 if (rc < 0) {
811 /* XXX What do we do now ? Nothing for now, keep old value, but
812 * return error upstream
814 DBG(" cpu %d, fan reading error !\n", state->index);
815 } else {
816 state->rpm = rc;
817 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
820 /* Get some sensor readings and scale it */
821 ltemp = read_smon_adc(state, 1);
822 if (ltemp == -1) {
823 /* XXX What do we do now ? */
824 state->overtemp++;
825 if (rc == 0)
826 rc = -EIO;
827 DBG(" cpu %d, temp reading error !\n", state->index);
828 } else {
829 /* Fixup temperature according to diode calibration
831 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
832 state->index,
833 ltemp, state->mpu.mdiode, state->mpu.bdiode);
834 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
835 state->last_temp = *temp;
836 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
840 * Read voltage & current and calculate power
842 volts = read_smon_adc(state, 3);
843 amps = read_smon_adc(state, 4);
845 /* Scale voltage and current raw sensor values according to fixed scales
846 * obtained in Darwin and calculate power from I and V
848 volts *= ADC_CPU_VOLTAGE_SCALE;
849 amps *= ADC_CPU_CURRENT_SCALE;
850 *power = (((u64)volts) * ((u64)amps)) >> 16;
851 state->voltage = volts;
852 state->current_a = amps;
853 state->last_power = *power;
855 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
856 state->index, FIX32TOPRINT(state->current_a),
857 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
859 return 0;
862 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
864 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
865 s64 integ_p, deriv_p, prop_p, sum;
866 int i;
868 /* Calculate power target value (could be done once for all)
869 * and convert to a 16.16 fp number
871 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
872 DBG(" power target: %d.%03d, error: %d.%03d\n",
873 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
875 /* Store temperature and power in history array */
876 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
877 state->temp_history[state->cur_temp] = temp;
878 state->cur_power = (state->cur_power + 1) % state->count_power;
879 state->power_history[state->cur_power] = power;
880 state->error_history[state->cur_power] = power_target - power;
882 /* If first loop, fill the history table */
883 if (state->first) {
884 for (i = 0; i < (state->count_power - 1); i++) {
885 state->cur_power = (state->cur_power + 1) % state->count_power;
886 state->power_history[state->cur_power] = power;
887 state->error_history[state->cur_power] = power_target - power;
889 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
890 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
891 state->temp_history[state->cur_temp] = temp;
893 state->first = 0;
896 /* Calculate the integral term normally based on the "power" values */
897 sum = 0;
898 integral = 0;
899 for (i = 0; i < state->count_power; i++)
900 integral += state->error_history[i];
901 integral *= CPU_PID_INTERVAL;
902 DBG(" integral: %08x\n", integral);
904 /* Calculate the adjusted input (sense value).
905 * G_r is 12.20
906 * integ is 16.16
907 * so the result is 28.36
909 * input target is mpu.ttarget, input max is mpu.tmax
911 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
912 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
913 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
914 adj_in_target = (state->mpu.ttarget << 16);
915 if (adj_in_target > sval)
916 adj_in_target = sval;
917 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
918 state->mpu.ttarget);
920 /* Calculate the derivative term */
921 derivative = state->temp_history[state->cur_temp] -
922 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
923 % CPU_TEMP_HISTORY_SIZE];
924 derivative /= CPU_PID_INTERVAL;
925 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
926 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
927 sum += deriv_p;
929 /* Calculate the proportional term */
930 proportional = temp - adj_in_target;
931 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
932 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
933 sum += prop_p;
935 /* Scale sum */
936 sum >>= 36;
938 DBG(" sum: %d\n", (int)sum);
939 state->rpm += (s32)sum;
942 static void do_monitor_cpu_combined(void)
944 struct cpu_pid_state *state0 = &cpu_state[0];
945 struct cpu_pid_state *state1 = &cpu_state[1];
946 s32 temp0, power0, temp1, power1;
947 s32 temp_combi, power_combi;
948 int rc, intake, pump;
950 rc = do_read_one_cpu_values(state0, &temp0, &power0);
951 if (rc < 0) {
952 /* XXX What do we do now ? */
954 state1->overtemp = 0;
955 rc = do_read_one_cpu_values(state1, &temp1, &power1);
956 if (rc < 0) {
957 /* XXX What do we do now ? */
959 if (state1->overtemp)
960 state0->overtemp++;
962 temp_combi = max(temp0, temp1);
963 power_combi = max(power0, power1);
965 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
966 * full blown immediately and try to trigger a shutdown
968 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
969 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
970 temp_combi >> 16);
971 state0->overtemp += CPU_MAX_OVERTEMP / 4;
972 } else if (temp_combi > (state0->mpu.tmax << 16))
973 state0->overtemp++;
974 else
975 state0->overtemp = 0;
976 if (state0->overtemp >= CPU_MAX_OVERTEMP)
977 critical_state = 1;
978 if (state0->overtemp > 0) {
979 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
980 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
981 pump = state0->pump_max;
982 goto do_set_fans;
985 /* Do the PID */
986 do_cpu_pid(state0, temp_combi, power_combi);
988 /* Range check */
989 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
990 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
992 /* Calculate intake fan speed */
993 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
994 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
995 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
996 state0->intake_rpm = intake;
998 /* Calculate pump speed */
999 pump = (state0->rpm * state0->pump_max) /
1000 state0->mpu.rmaxn_exhaust_fan;
1001 pump = min(pump, state0->pump_max);
1002 pump = max(pump, state0->pump_min);
1004 do_set_fans:
1005 /* We copy values from state 0 to state 1 for /sysfs */
1006 state1->rpm = state0->rpm;
1007 state1->intake_rpm = state0->intake_rpm;
1009 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
1010 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
1012 /* We should check for errors, shouldn't we ? But then, what
1013 * do we do once the error occurs ? For FCU notified fan
1014 * failures (-EFAULT) we probably want to notify userland
1015 * some way...
1017 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1018 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1019 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1020 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1022 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1023 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1024 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1025 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1028 static void do_monitor_cpu_split(struct cpu_pid_state *state)
1030 s32 temp, power;
1031 int rc, intake;
1033 /* Read current fan status */
1034 rc = do_read_one_cpu_values(state, &temp, &power);
1035 if (rc < 0) {
1036 /* XXX What do we do now ? */
1039 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1040 * full blown immediately and try to trigger a shutdown
1042 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1043 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1044 " (%d) !\n",
1045 state->index, temp >> 16);
1046 state->overtemp += CPU_MAX_OVERTEMP / 4;
1047 } else if (temp > (state->mpu.tmax << 16))
1048 state->overtemp++;
1049 else
1050 state->overtemp = 0;
1051 if (state->overtemp >= CPU_MAX_OVERTEMP)
1052 critical_state = 1;
1053 if (state->overtemp > 0) {
1054 state->rpm = state->mpu.rmaxn_exhaust_fan;
1055 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1056 goto do_set_fans;
1059 /* Do the PID */
1060 do_cpu_pid(state, temp, power);
1062 /* Range check */
1063 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1064 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1066 /* Calculate intake fan */
1067 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1068 intake = max(intake, (int)state->mpu.rminn_intake_fan);
1069 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1070 state->intake_rpm = intake;
1072 do_set_fans:
1073 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1074 state->index, (int)state->rpm, intake, state->overtemp);
1076 /* We should check for errors, shouldn't we ? But then, what
1077 * do we do once the error occurs ? For FCU notified fan
1078 * failures (-EFAULT) we probably want to notify userland
1079 * some way...
1081 if (state->index == 0) {
1082 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1083 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1084 } else {
1085 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1086 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1090 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1092 s32 temp, power, fan_min;
1093 int rc;
1095 /* Read current fan status */
1096 rc = do_read_one_cpu_values(state, &temp, &power);
1097 if (rc < 0) {
1098 /* XXX What do we do now ? */
1101 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1102 * full blown immediately and try to trigger a shutdown
1104 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1105 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1106 " (%d) !\n",
1107 state->index, temp >> 16);
1108 state->overtemp = CPU_MAX_OVERTEMP / 4;
1109 } else if (temp > (state->mpu.tmax << 16))
1110 state->overtemp++;
1111 else
1112 state->overtemp = 0;
1113 if (state->overtemp >= CPU_MAX_OVERTEMP)
1114 critical_state = 1;
1115 if (state->overtemp > 0) {
1116 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1117 goto do_set_fans;
1120 /* Do the PID */
1121 do_cpu_pid(state, temp, power);
1123 /* Check clamp from dimms */
1124 fan_min = dimm_output_clamp;
1125 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1127 DBG(" CPU min mpu = %d, min dimm = %d\n",
1128 state->mpu.rminn_intake_fan, dimm_output_clamp);
1130 state->rpm = max(state->rpm, (int)fan_min);
1131 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1132 state->intake_rpm = state->rpm;
1134 do_set_fans:
1135 DBG("** CPU %d RPM: %d overtemp: %d\n",
1136 state->index, (int)state->rpm, state->overtemp);
1138 /* We should check for errors, shouldn't we ? But then, what
1139 * do we do once the error occurs ? For FCU notified fan
1140 * failures (-EFAULT) we probably want to notify userland
1141 * some way...
1143 if (state->index == 0) {
1144 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1145 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1146 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1147 } else {
1148 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1149 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1150 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1155 * Initialize the state structure for one CPU control loop
1157 static int init_cpu_state(struct cpu_pid_state *state, int index)
1159 state->index = index;
1160 state->first = 1;
1161 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1162 state->overtemp = 0;
1163 state->adc_config = 0x00;
1166 if (index == 0)
1167 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1168 else if (index == 1)
1169 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1170 if (state->monitor == NULL)
1171 goto fail;
1173 if (read_eeprom(index, &state->mpu))
1174 goto fail;
1176 state->count_power = state->mpu.tguardband;
1177 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1178 printk(KERN_WARNING "Warning ! too many power history slots\n");
1179 state->count_power = CPU_POWER_HISTORY_SIZE;
1181 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1183 if (index == 0) {
1184 device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1185 device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1186 device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1187 device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1188 device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1189 } else {
1190 device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1191 device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1192 device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1193 device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1194 device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1197 return 0;
1198 fail:
1199 if (state->monitor)
1200 detach_i2c_chip(state->monitor);
1201 state->monitor = NULL;
1203 return -ENODEV;
1207 * Dispose of the state data for one CPU control loop
1209 static void dispose_cpu_state(struct cpu_pid_state *state)
1211 if (state->monitor == NULL)
1212 return;
1214 if (state->index == 0) {
1215 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1216 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1217 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1218 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1219 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1220 } else {
1221 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1222 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1223 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1224 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1225 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1228 detach_i2c_chip(state->monitor);
1229 state->monitor = NULL;
1233 * Motherboard backside & U3 heatsink fan control loop
1235 static void do_monitor_backside(struct backside_pid_state *state)
1237 s32 temp, integral, derivative, fan_min;
1238 s64 integ_p, deriv_p, prop_p, sum;
1239 int i, rc;
1241 if (--state->ticks != 0)
1242 return;
1243 state->ticks = backside_params.interval;
1245 DBG("backside:\n");
1247 /* Check fan status */
1248 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1249 if (rc < 0) {
1250 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1251 /* XXX What do we do now ? */
1252 } else
1253 state->pwm = rc;
1254 DBG(" current pwm: %d\n", state->pwm);
1256 /* Get some sensor readings */
1257 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1258 state->last_temp = temp;
1259 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1260 FIX32TOPRINT(backside_params.input_target));
1262 /* Store temperature and error in history array */
1263 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1264 state->sample_history[state->cur_sample] = temp;
1265 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1267 /* If first loop, fill the history table */
1268 if (state->first) {
1269 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1270 state->cur_sample = (state->cur_sample + 1) %
1271 BACKSIDE_PID_HISTORY_SIZE;
1272 state->sample_history[state->cur_sample] = temp;
1273 state->error_history[state->cur_sample] =
1274 temp - backside_params.input_target;
1276 state->first = 0;
1279 /* Calculate the integral term */
1280 sum = 0;
1281 integral = 0;
1282 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1283 integral += state->error_history[i];
1284 integral *= backside_params.interval;
1285 DBG(" integral: %08x\n", integral);
1286 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1287 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1288 sum += integ_p;
1290 /* Calculate the derivative term */
1291 derivative = state->error_history[state->cur_sample] -
1292 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1293 % BACKSIDE_PID_HISTORY_SIZE];
1294 derivative /= backside_params.interval;
1295 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1296 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1297 sum += deriv_p;
1299 /* Calculate the proportional term */
1300 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1301 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1302 sum += prop_p;
1304 /* Scale sum */
1305 sum >>= 36;
1307 DBG(" sum: %d\n", (int)sum);
1308 if (backside_params.additive)
1309 state->pwm += (s32)sum;
1310 else
1311 state->pwm = sum;
1313 /* Check for clamp */
1314 fan_min = (dimm_output_clamp * 100) / 14000;
1315 fan_min = max(fan_min, backside_params.output_min);
1317 state->pwm = max(state->pwm, fan_min);
1318 state->pwm = min(state->pwm, backside_params.output_max);
1320 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1321 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1325 * Initialize the state structure for the backside fan control loop
1327 static int init_backside_state(struct backside_pid_state *state)
1329 struct device_node *u3;
1330 int u3h = 1; /* conservative by default */
1333 * There are different PID params for machines with U3 and machines
1334 * with U3H, pick the right ones now
1336 u3 = of_find_node_by_path("/u3@0,f8000000");
1337 if (u3 != NULL) {
1338 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1339 if (vers)
1340 if (((*vers) & 0x3f) < 0x34)
1341 u3h = 0;
1342 of_node_put(u3);
1345 if (rackmac) {
1346 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1347 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1348 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1349 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1350 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1351 backside_params.G_r = BACKSIDE_PID_G_r;
1352 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1353 backside_params.additive = 0;
1354 } else if (u3h) {
1355 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1356 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1357 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1358 backside_params.interval = BACKSIDE_PID_INTERVAL;
1359 backside_params.G_p = BACKSIDE_PID_G_p;
1360 backside_params.G_r = BACKSIDE_PID_G_r;
1361 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1362 backside_params.additive = 1;
1363 } else {
1364 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1365 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1366 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1367 backside_params.interval = BACKSIDE_PID_INTERVAL;
1368 backside_params.G_p = BACKSIDE_PID_G_p;
1369 backside_params.G_r = BACKSIDE_PID_G_r;
1370 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1371 backside_params.additive = 1;
1374 state->ticks = 1;
1375 state->first = 1;
1376 state->pwm = 50;
1378 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1379 if (state->monitor == NULL)
1380 return -ENODEV;
1382 device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1383 device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1385 return 0;
1389 * Dispose of the state data for the backside control loop
1391 static void dispose_backside_state(struct backside_pid_state *state)
1393 if (state->monitor == NULL)
1394 return;
1396 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1397 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1399 detach_i2c_chip(state->monitor);
1400 state->monitor = NULL;
1404 * Drives bay fan control loop
1406 static void do_monitor_drives(struct drives_pid_state *state)
1408 s32 temp, integral, derivative;
1409 s64 integ_p, deriv_p, prop_p, sum;
1410 int i, rc;
1412 if (--state->ticks != 0)
1413 return;
1414 state->ticks = DRIVES_PID_INTERVAL;
1416 DBG("drives:\n");
1418 /* Check fan status */
1419 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1420 if (rc < 0) {
1421 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1422 /* XXX What do we do now ? */
1423 } else
1424 state->rpm = rc;
1425 DBG(" current rpm: %d\n", state->rpm);
1427 /* Get some sensor readings */
1428 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1429 DS1775_TEMP)) << 8;
1430 state->last_temp = temp;
1431 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1432 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1434 /* Store temperature and error in history array */
1435 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1436 state->sample_history[state->cur_sample] = temp;
1437 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1439 /* If first loop, fill the history table */
1440 if (state->first) {
1441 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1442 state->cur_sample = (state->cur_sample + 1) %
1443 DRIVES_PID_HISTORY_SIZE;
1444 state->sample_history[state->cur_sample] = temp;
1445 state->error_history[state->cur_sample] =
1446 temp - DRIVES_PID_INPUT_TARGET;
1448 state->first = 0;
1451 /* Calculate the integral term */
1452 sum = 0;
1453 integral = 0;
1454 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1455 integral += state->error_history[i];
1456 integral *= DRIVES_PID_INTERVAL;
1457 DBG(" integral: %08x\n", integral);
1458 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1459 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1460 sum += integ_p;
1462 /* Calculate the derivative term */
1463 derivative = state->error_history[state->cur_sample] -
1464 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1465 % DRIVES_PID_HISTORY_SIZE];
1466 derivative /= DRIVES_PID_INTERVAL;
1467 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1468 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1469 sum += deriv_p;
1471 /* Calculate the proportional term */
1472 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1473 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1474 sum += prop_p;
1476 /* Scale sum */
1477 sum >>= 36;
1479 DBG(" sum: %d\n", (int)sum);
1480 state->rpm += (s32)sum;
1482 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1483 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1485 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1486 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1490 * Initialize the state structure for the drives bay fan control loop
1492 static int init_drives_state(struct drives_pid_state *state)
1494 state->ticks = 1;
1495 state->first = 1;
1496 state->rpm = 1000;
1498 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1499 if (state->monitor == NULL)
1500 return -ENODEV;
1502 device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1503 device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1505 return 0;
1509 * Dispose of the state data for the drives control loop
1511 static void dispose_drives_state(struct drives_pid_state *state)
1513 if (state->monitor == NULL)
1514 return;
1516 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1517 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1519 detach_i2c_chip(state->monitor);
1520 state->monitor = NULL;
1524 * DIMMs temp control loop
1526 static void do_monitor_dimms(struct dimm_pid_state *state)
1528 s32 temp, integral, derivative, fan_min;
1529 s64 integ_p, deriv_p, prop_p, sum;
1530 int i;
1532 if (--state->ticks != 0)
1533 return;
1534 state->ticks = DIMM_PID_INTERVAL;
1536 DBG("DIMM:\n");
1538 DBG(" current value: %d\n", state->output);
1540 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1541 if (temp < 0)
1542 return;
1543 temp <<= 16;
1544 state->last_temp = temp;
1545 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1546 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1548 /* Store temperature and error in history array */
1549 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1550 state->sample_history[state->cur_sample] = temp;
1551 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1553 /* If first loop, fill the history table */
1554 if (state->first) {
1555 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1556 state->cur_sample = (state->cur_sample + 1) %
1557 DIMM_PID_HISTORY_SIZE;
1558 state->sample_history[state->cur_sample] = temp;
1559 state->error_history[state->cur_sample] =
1560 temp - DIMM_PID_INPUT_TARGET;
1562 state->first = 0;
1565 /* Calculate the integral term */
1566 sum = 0;
1567 integral = 0;
1568 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1569 integral += state->error_history[i];
1570 integral *= DIMM_PID_INTERVAL;
1571 DBG(" integral: %08x\n", integral);
1572 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1573 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1574 sum += integ_p;
1576 /* Calculate the derivative term */
1577 derivative = state->error_history[state->cur_sample] -
1578 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1579 % DIMM_PID_HISTORY_SIZE];
1580 derivative /= DIMM_PID_INTERVAL;
1581 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1582 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1583 sum += deriv_p;
1585 /* Calculate the proportional term */
1586 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1587 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1588 sum += prop_p;
1590 /* Scale sum */
1591 sum >>= 36;
1593 DBG(" sum: %d\n", (int)sum);
1594 state->output = (s32)sum;
1595 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1596 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1597 dimm_output_clamp = state->output;
1599 DBG("** DIMM clamp value: %d\n", (int)state->output);
1601 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1602 fan_min = (dimm_output_clamp * 100) / 14000;
1603 fan_min = max(fan_min, backside_params.output_min);
1604 if (backside_state.pwm < fan_min) {
1605 backside_state.pwm = fan_min;
1606 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1607 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1612 * Initialize the state structure for the DIMM temp control loop
1614 static int init_dimms_state(struct dimm_pid_state *state)
1616 state->ticks = 1;
1617 state->first = 1;
1618 state->output = 4000;
1620 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1621 if (state->monitor == NULL)
1622 return -ENODEV;
1624 device_create_file(&of_dev->dev, &dev_attr_dimms_temperature);
1626 return 0;
1630 * Dispose of the state data for the DIMM control loop
1632 static void dispose_dimms_state(struct dimm_pid_state *state)
1634 if (state->monitor == NULL)
1635 return;
1637 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1639 detach_i2c_chip(state->monitor);
1640 state->monitor = NULL;
1644 * Slots fan control loop
1646 static void do_monitor_slots(struct slots_pid_state *state)
1648 s32 temp, integral, derivative;
1649 s64 integ_p, deriv_p, prop_p, sum;
1650 int i, rc;
1652 if (--state->ticks != 0)
1653 return;
1654 state->ticks = SLOTS_PID_INTERVAL;
1656 DBG("slots:\n");
1658 /* Check fan status */
1659 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1660 if (rc < 0) {
1661 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1662 /* XXX What do we do now ? */
1663 } else
1664 state->pwm = rc;
1665 DBG(" current pwm: %d\n", state->pwm);
1667 /* Get some sensor readings */
1668 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1669 DS1775_TEMP)) << 8;
1670 state->last_temp = temp;
1671 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1672 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1674 /* Store temperature and error in history array */
1675 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1676 state->sample_history[state->cur_sample] = temp;
1677 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1679 /* If first loop, fill the history table */
1680 if (state->first) {
1681 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1682 state->cur_sample = (state->cur_sample + 1) %
1683 SLOTS_PID_HISTORY_SIZE;
1684 state->sample_history[state->cur_sample] = temp;
1685 state->error_history[state->cur_sample] =
1686 temp - SLOTS_PID_INPUT_TARGET;
1688 state->first = 0;
1691 /* Calculate the integral term */
1692 sum = 0;
1693 integral = 0;
1694 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1695 integral += state->error_history[i];
1696 integral *= SLOTS_PID_INTERVAL;
1697 DBG(" integral: %08x\n", integral);
1698 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1699 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1700 sum += integ_p;
1702 /* Calculate the derivative term */
1703 derivative = state->error_history[state->cur_sample] -
1704 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1705 % SLOTS_PID_HISTORY_SIZE];
1706 derivative /= SLOTS_PID_INTERVAL;
1707 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1708 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1709 sum += deriv_p;
1711 /* Calculate the proportional term */
1712 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1713 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1714 sum += prop_p;
1716 /* Scale sum */
1717 sum >>= 36;
1719 DBG(" sum: %d\n", (int)sum);
1720 state->pwm = (s32)sum;
1722 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1723 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1725 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1726 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1730 * Initialize the state structure for the slots bay fan control loop
1732 static int init_slots_state(struct slots_pid_state *state)
1734 state->ticks = 1;
1735 state->first = 1;
1736 state->pwm = 50;
1738 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1739 if (state->monitor == NULL)
1740 return -ENODEV;
1742 device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1743 device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1745 return 0;
1749 * Dispose of the state data for the slots control loop
1751 static void dispose_slots_state(struct slots_pid_state *state)
1753 if (state->monitor == NULL)
1754 return;
1756 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1757 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1759 detach_i2c_chip(state->monitor);
1760 state->monitor = NULL;
1764 static int call_critical_overtemp(void)
1766 char *argv[] = { critical_overtemp_path, NULL };
1767 static char *envp[] = { "HOME=/",
1768 "TERM=linux",
1769 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1770 NULL };
1772 return call_usermodehelper(critical_overtemp_path,
1773 argv, envp, UMH_WAIT_EXEC);
1778 * Here's the kernel thread that calls the various control loops
1780 static int main_control_loop(void *x)
1782 daemonize("kfand");
1784 DBG("main_control_loop started\n");
1786 down(&driver_lock);
1788 if (start_fcu() < 0) {
1789 printk(KERN_ERR "kfand: failed to start FCU\n");
1790 up(&driver_lock);
1791 goto out;
1794 /* Set the PCI fan once for now on non-RackMac */
1795 if (!rackmac)
1796 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1798 /* Initialize ADCs */
1799 initialize_adc(&cpu_state[0]);
1800 if (cpu_state[1].monitor != NULL)
1801 initialize_adc(&cpu_state[1]);
1803 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1805 up(&driver_lock);
1807 while (state == state_attached) {
1808 unsigned long elapsed, start;
1810 start = jiffies;
1812 down(&driver_lock);
1814 /* Tickle the FCU just in case */
1815 if (--fcu_tickle_ticks < 0) {
1816 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1817 tickle_fcu();
1820 /* First, we always calculate the new DIMMs state on an Xserve */
1821 if (rackmac)
1822 do_monitor_dimms(&dimms_state);
1824 /* Then, the CPUs */
1825 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1826 do_monitor_cpu_combined();
1827 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1828 do_monitor_cpu_rack(&cpu_state[0]);
1829 if (cpu_state[1].monitor != NULL)
1830 do_monitor_cpu_rack(&cpu_state[1]);
1831 // better deal with UP
1832 } else {
1833 do_monitor_cpu_split(&cpu_state[0]);
1834 if (cpu_state[1].monitor != NULL)
1835 do_monitor_cpu_split(&cpu_state[1]);
1836 // better deal with UP
1838 /* Then, the rest */
1839 do_monitor_backside(&backside_state);
1840 if (rackmac)
1841 do_monitor_slots(&slots_state);
1842 else
1843 do_monitor_drives(&drives_state);
1844 up(&driver_lock);
1846 if (critical_state == 1) {
1847 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1848 printk(KERN_WARNING "Attempting to shut down...\n");
1849 if (call_critical_overtemp()) {
1850 printk(KERN_WARNING "Can't call %s, power off now!\n",
1851 critical_overtemp_path);
1852 machine_power_off();
1855 if (critical_state > 0)
1856 critical_state++;
1857 if (critical_state > MAX_CRITICAL_STATE) {
1858 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1859 machine_power_off();
1862 // FIXME: Deal with signals
1863 elapsed = jiffies - start;
1864 if (elapsed < HZ)
1865 schedule_timeout_interruptible(HZ - elapsed);
1868 out:
1869 DBG("main_control_loop ended\n");
1871 ctrl_task = 0;
1872 complete_and_exit(&ctrl_complete, 0);
1876 * Dispose the control loops when tearing down
1878 static void dispose_control_loops(void)
1880 dispose_cpu_state(&cpu_state[0]);
1881 dispose_cpu_state(&cpu_state[1]);
1882 dispose_backside_state(&backside_state);
1883 dispose_drives_state(&drives_state);
1884 dispose_slots_state(&slots_state);
1885 dispose_dimms_state(&dimms_state);
1889 * Create the control loops. U3-0 i2c bus is up, so we can now
1890 * get to the various sensors
1892 static int create_control_loops(void)
1894 struct device_node *np;
1896 /* Count CPUs from the device-tree, we don't care how many are
1897 * actually used by Linux
1899 cpu_count = 0;
1900 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1901 cpu_count++;
1903 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1905 /* Decide the type of PID algorithm to use based on the presence of
1906 * the pumps, though that may not be the best way, that is good enough
1907 * for now
1909 if (rackmac)
1910 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1911 else if (machine_is_compatible("PowerMac7,3")
1912 && (cpu_count > 1)
1913 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1914 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1915 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1916 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1917 } else
1918 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1920 /* Create control loops for everything. If any fail, everything
1921 * fails
1923 if (init_cpu_state(&cpu_state[0], 0))
1924 goto fail;
1925 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1926 fetch_cpu_pumps_minmax();
1928 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1929 goto fail;
1930 if (init_backside_state(&backside_state))
1931 goto fail;
1932 if (rackmac && init_dimms_state(&dimms_state))
1933 goto fail;
1934 if (rackmac && init_slots_state(&slots_state))
1935 goto fail;
1936 if (!rackmac && init_drives_state(&drives_state))
1937 goto fail;
1939 DBG("all control loops up !\n");
1941 return 0;
1943 fail:
1944 DBG("failure creating control loops, disposing\n");
1946 dispose_control_loops();
1948 return -ENODEV;
1952 * Start the control loops after everything is up, that is create
1953 * the thread that will make them run
1955 static void start_control_loops(void)
1957 init_completion(&ctrl_complete);
1959 ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL);
1963 * Stop the control loops when tearing down
1965 static void stop_control_loops(void)
1967 if (ctrl_task != 0)
1968 wait_for_completion(&ctrl_complete);
1972 * Attach to the i2c FCU after detecting U3-1 bus
1974 static int attach_fcu(void)
1976 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1977 if (fcu == NULL)
1978 return -ENODEV;
1980 DBG("FCU attached\n");
1982 return 0;
1986 * Detach from the i2c FCU when tearing down
1988 static void detach_fcu(void)
1990 if (fcu)
1991 detach_i2c_chip(fcu);
1992 fcu = NULL;
1996 * Attach to the i2c controller. We probe the various chips based
1997 * on the device-tree nodes and build everything for the driver to
1998 * run, we then kick the driver monitoring thread
2000 static int therm_pm72_attach(struct i2c_adapter *adapter)
2002 down(&driver_lock);
2004 /* Check state */
2005 if (state == state_detached)
2006 state = state_attaching;
2007 if (state != state_attaching) {
2008 up(&driver_lock);
2009 return 0;
2012 /* Check if we are looking for one of these */
2013 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2014 u3_0 = adapter;
2015 DBG("found U3-0\n");
2016 if (k2 || !rackmac)
2017 if (create_control_loops())
2018 u3_0 = NULL;
2019 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2020 u3_1 = adapter;
2021 DBG("found U3-1, attaching FCU\n");
2022 if (attach_fcu())
2023 u3_1 = NULL;
2024 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2025 k2 = adapter;
2026 DBG("Found K2\n");
2027 if (u3_0 && rackmac)
2028 if (create_control_loops())
2029 k2 = NULL;
2031 /* We got all we need, start control loops */
2032 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2033 DBG("everything up, starting control loops\n");
2034 state = state_attached;
2035 start_control_loops();
2037 up(&driver_lock);
2039 return 0;
2043 * Called on every adapter when the driver or the i2c controller
2044 * is going away.
2046 static int therm_pm72_detach(struct i2c_adapter *adapter)
2048 down(&driver_lock);
2050 if (state != state_detached)
2051 state = state_detaching;
2053 /* Stop control loops if any */
2054 DBG("stopping control loops\n");
2055 up(&driver_lock);
2056 stop_control_loops();
2057 down(&driver_lock);
2059 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2060 DBG("lost U3-0, disposing control loops\n");
2061 dispose_control_loops();
2062 u3_0 = NULL;
2065 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2066 DBG("lost U3-1, detaching FCU\n");
2067 detach_fcu();
2068 u3_1 = NULL;
2070 if (u3_0 == NULL && u3_1 == NULL)
2071 state = state_detached;
2073 up(&driver_lock);
2075 return 0;
2078 static int fan_check_loc_match(const char *loc, int fan)
2080 char tmp[64];
2081 char *c, *e;
2083 strlcpy(tmp, fcu_fans[fan].loc, 64);
2085 c = tmp;
2086 for (;;) {
2087 e = strchr(c, ',');
2088 if (e)
2089 *e = 0;
2090 if (strcmp(loc, c) == 0)
2091 return 1;
2092 if (e == NULL)
2093 break;
2094 c = e + 1;
2096 return 0;
2099 static void fcu_lookup_fans(struct device_node *fcu_node)
2101 struct device_node *np = NULL;
2102 int i;
2104 /* The table is filled by default with values that are suitable
2105 * for the old machines without device-tree informations. We scan
2106 * the device-tree and override those values with whatever is
2107 * there
2110 DBG("Looking up FCU controls in device-tree...\n");
2112 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2113 int type = -1;
2114 const char *loc;
2115 const u32 *reg;
2117 DBG(" control: %s, type: %s\n", np->name, np->type);
2119 /* Detect control type */
2120 if (!strcmp(np->type, "fan-rpm-control") ||
2121 !strcmp(np->type, "fan-rpm"))
2122 type = FCU_FAN_RPM;
2123 if (!strcmp(np->type, "fan-pwm-control") ||
2124 !strcmp(np->type, "fan-pwm"))
2125 type = FCU_FAN_PWM;
2126 /* Only care about fans for now */
2127 if (type == -1)
2128 continue;
2130 /* Lookup for a matching location */
2131 loc = of_get_property(np, "location", NULL);
2132 reg = of_get_property(np, "reg", NULL);
2133 if (loc == NULL || reg == NULL)
2134 continue;
2135 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2137 for (i = 0; i < FCU_FAN_COUNT; i++) {
2138 int fan_id;
2140 if (!fan_check_loc_match(loc, i))
2141 continue;
2142 DBG(" location match, index: %d\n", i);
2143 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2144 if (type != fcu_fans[i].type) {
2145 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2146 "in device-tree for %s\n", np->full_name);
2147 break;
2149 if (type == FCU_FAN_RPM)
2150 fan_id = ((*reg) - 0x10) / 2;
2151 else
2152 fan_id = ((*reg) - 0x30) / 2;
2153 if (fan_id > 7) {
2154 printk(KERN_WARNING "therm_pm72: Can't parse "
2155 "fan ID in device-tree for %s\n", np->full_name);
2156 break;
2158 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2159 fcu_fans[i].id = fan_id;
2163 /* Now dump the array */
2164 printk(KERN_INFO "Detected fan controls:\n");
2165 for (i = 0; i < FCU_FAN_COUNT; i++) {
2166 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2167 continue;
2168 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2169 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2170 fcu_fans[i].id, fcu_fans[i].loc);
2174 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
2176 state = state_detached;
2178 /* Lookup the fans in the device tree */
2179 fcu_lookup_fans(dev->node);
2181 /* Add the driver */
2182 return i2c_add_driver(&therm_pm72_driver);
2185 static int fcu_of_remove(struct of_device* dev)
2187 i2c_del_driver(&therm_pm72_driver);
2189 return 0;
2192 static struct of_device_id fcu_match[] =
2195 .type = "fcu",
2200 static struct of_platform_driver fcu_of_platform_driver =
2202 .name = "temperature",
2203 .match_table = fcu_match,
2204 .probe = fcu_of_probe,
2205 .remove = fcu_of_remove
2209 * Check machine type, attach to i2c controller
2211 static int __init therm_pm72_init(void)
2213 struct device_node *np;
2215 rackmac = machine_is_compatible("RackMac3,1");
2217 if (!machine_is_compatible("PowerMac7,2") &&
2218 !machine_is_compatible("PowerMac7,3") &&
2219 !rackmac)
2220 return -ENODEV;
2222 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2224 np = of_find_node_by_type(NULL, "fcu");
2225 if (np == NULL) {
2226 /* Some machines have strangely broken device-tree */
2227 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2228 if (np == NULL) {
2229 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2230 return -ENODEV;
2233 of_dev = of_platform_device_create(np, "temperature", NULL);
2234 if (of_dev == NULL) {
2235 printk(KERN_ERR "Can't register FCU platform device !\n");
2236 return -ENODEV;
2239 of_register_platform_driver(&fcu_of_platform_driver);
2241 return 0;
2244 static void __exit therm_pm72_exit(void)
2246 of_unregister_platform_driver(&fcu_of_platform_driver);
2248 if (of_dev)
2249 of_device_unregister(of_dev);
2252 module_init(therm_pm72_init);
2253 module_exit(therm_pm72_exit);
2255 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2256 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2257 MODULE_LICENSE("GPL");