x86, acpi/irq: pci device dev->irq is an isa irq not a gsi
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / macintosh / therm_pm72.c
blobb18fa948f3d1be64c3f3df73cbc715596fb05de7
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/init.h>
118 #include <linux/spinlock.h>
119 #include <linux/wait.h>
120 #include <linux/reboot.h>
121 #include <linux/kmod.h>
122 #include <linux/i2c.h>
123 #include <linux/kthread.h>
124 #include <linux/mutex.h>
125 #include <linux/of_device.h>
126 #include <linux/of_platform.h>
127 #include <asm/prom.h>
128 #include <asm/machdep.h>
129 #include <asm/io.h>
130 #include <asm/system.h>
131 #include <asm/sections.h>
132 #include <asm/macio.h>
134 #include "therm_pm72.h"
136 #define VERSION "1.3"
138 #undef DEBUG
140 #ifdef DEBUG
141 #define DBG(args...) printk(args)
142 #else
143 #define DBG(args...) do { } while(0)
144 #endif
148 * Driver statics
151 static struct of_device * of_dev;
152 static struct i2c_adapter * u3_0;
153 static struct i2c_adapter * u3_1;
154 static struct i2c_adapter * k2;
155 static struct i2c_client * fcu;
156 static struct cpu_pid_state cpu_state[2];
157 static struct basckside_pid_params backside_params;
158 static struct backside_pid_state backside_state;
159 static struct drives_pid_state drives_state;
160 static struct dimm_pid_state dimms_state;
161 static struct slots_pid_state slots_state;
162 static int state;
163 static int cpu_count;
164 static int cpu_pid_type;
165 static struct task_struct *ctrl_task;
166 static struct completion ctrl_complete;
167 static int critical_state;
168 static int rackmac;
169 static s32 dimm_output_clamp;
170 static int fcu_rpm_shift;
171 static int fcu_tickle_ticks;
172 static DEFINE_MUTEX(driver_lock);
175 * We have 3 types of CPU PID control. One is "split" old style control
176 * for intake & exhaust fans, the other is "combined" control for both
177 * CPUs that also deals with the pumps when present. To be "compatible"
178 * with OS X at this point, we only use "COMBINED" on the machines that
179 * are identified as having the pumps (though that identification is at
180 * least dodgy). Ultimately, we could probably switch completely to this
181 * algorithm provided we hack it to deal with the UP case
183 #define CPU_PID_TYPE_SPLIT 0
184 #define CPU_PID_TYPE_COMBINED 1
185 #define CPU_PID_TYPE_RACKMAC 2
188 * This table describes all fans in the FCU. The "id" and "type" values
189 * are defaults valid for all earlier machines. Newer machines will
190 * eventually override the table content based on the device-tree
192 struct fcu_fan_table
194 char* loc; /* location code */
195 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
196 int id; /* id or -1 */
199 #define FCU_FAN_RPM 0
200 #define FCU_FAN_PWM 1
202 #define FCU_FAN_ABSENT_ID -1
204 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
206 struct fcu_fan_table fcu_fans[] = {
207 [BACKSIDE_FAN_PWM_INDEX] = {
208 .loc = "BACKSIDE,SYS CTRLR FAN",
209 .type = FCU_FAN_PWM,
210 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
212 [DRIVES_FAN_RPM_INDEX] = {
213 .loc = "DRIVE BAY",
214 .type = FCU_FAN_RPM,
215 .id = DRIVES_FAN_RPM_DEFAULT_ID,
217 [SLOTS_FAN_PWM_INDEX] = {
218 .loc = "SLOT,PCI FAN",
219 .type = FCU_FAN_PWM,
220 .id = SLOTS_FAN_PWM_DEFAULT_ID,
222 [CPUA_INTAKE_FAN_RPM_INDEX] = {
223 .loc = "CPU A INTAKE",
224 .type = FCU_FAN_RPM,
225 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
227 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
228 .loc = "CPU A EXHAUST",
229 .type = FCU_FAN_RPM,
230 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
232 [CPUB_INTAKE_FAN_RPM_INDEX] = {
233 .loc = "CPU B INTAKE",
234 .type = FCU_FAN_RPM,
235 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
237 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
238 .loc = "CPU B EXHAUST",
239 .type = FCU_FAN_RPM,
240 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
242 /* pumps aren't present by default, have to be looked up in the
243 * device-tree
245 [CPUA_PUMP_RPM_INDEX] = {
246 .loc = "CPU A PUMP",
247 .type = FCU_FAN_RPM,
248 .id = FCU_FAN_ABSENT_ID,
250 [CPUB_PUMP_RPM_INDEX] = {
251 .loc = "CPU B PUMP",
252 .type = FCU_FAN_RPM,
253 .id = FCU_FAN_ABSENT_ID,
255 /* Xserve fans */
256 [CPU_A1_FAN_RPM_INDEX] = {
257 .loc = "CPU A 1",
258 .type = FCU_FAN_RPM,
259 .id = FCU_FAN_ABSENT_ID,
261 [CPU_A2_FAN_RPM_INDEX] = {
262 .loc = "CPU A 2",
263 .type = FCU_FAN_RPM,
264 .id = FCU_FAN_ABSENT_ID,
266 [CPU_A3_FAN_RPM_INDEX] = {
267 .loc = "CPU A 3",
268 .type = FCU_FAN_RPM,
269 .id = FCU_FAN_ABSENT_ID,
271 [CPU_B1_FAN_RPM_INDEX] = {
272 .loc = "CPU B 1",
273 .type = FCU_FAN_RPM,
274 .id = FCU_FAN_ABSENT_ID,
276 [CPU_B2_FAN_RPM_INDEX] = {
277 .loc = "CPU B 2",
278 .type = FCU_FAN_RPM,
279 .id = FCU_FAN_ABSENT_ID,
281 [CPU_B3_FAN_RPM_INDEX] = {
282 .loc = "CPU B 3",
283 .type = FCU_FAN_RPM,
284 .id = FCU_FAN_ABSENT_ID,
288 static struct i2c_driver therm_pm72_driver;
291 * Utility function to create an i2c_client structure and
292 * attach it to one of u3 adapters
294 static struct i2c_client *attach_i2c_chip(int id, const char *name)
296 struct i2c_client *clt;
297 struct i2c_adapter *adap;
298 struct i2c_board_info info;
300 if (id & 0x200)
301 adap = k2;
302 else if (id & 0x100)
303 adap = u3_1;
304 else
305 adap = u3_0;
306 if (adap == NULL)
307 return NULL;
309 memset(&info, 0, sizeof(struct i2c_board_info));
310 info.addr = (id >> 1) & 0x7f;
311 strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
312 clt = i2c_new_device(adap, &info);
313 if (!clt) {
314 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
315 return NULL;
319 * Let i2c-core delete that device on driver removal.
320 * This is safe because i2c-core holds the core_lock mutex for us.
322 list_add_tail(&clt->detected, &therm_pm72_driver.clients);
323 return clt;
327 * Here are the i2c chip access wrappers
330 static void initialize_adc(struct cpu_pid_state *state)
332 int rc;
333 u8 buf[2];
335 /* Read ADC the configuration register and cache it. We
336 * also make sure Config2 contains proper values, I've seen
337 * cases where we got stale grabage in there, thus preventing
338 * proper reading of conv. values
341 /* Clear Config2 */
342 buf[0] = 5;
343 buf[1] = 0;
344 i2c_master_send(state->monitor, buf, 2);
346 /* Read & cache Config1 */
347 buf[0] = 1;
348 rc = i2c_master_send(state->monitor, buf, 1);
349 if (rc > 0) {
350 rc = i2c_master_recv(state->monitor, buf, 1);
351 if (rc > 0) {
352 state->adc_config = buf[0];
353 DBG("ADC config reg: %02x\n", state->adc_config);
354 /* Disable shutdown mode */
355 state->adc_config &= 0xfe;
356 buf[0] = 1;
357 buf[1] = state->adc_config;
358 rc = i2c_master_send(state->monitor, buf, 2);
361 if (rc <= 0)
362 printk(KERN_ERR "therm_pm72: Error reading ADC config"
363 " register !\n");
366 static int read_smon_adc(struct cpu_pid_state *state, int chan)
368 int rc, data, tries = 0;
369 u8 buf[2];
371 for (;;) {
372 /* Set channel */
373 buf[0] = 1;
374 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
375 rc = i2c_master_send(state->monitor, buf, 2);
376 if (rc <= 0)
377 goto error;
378 /* Wait for convertion */
379 msleep(1);
380 /* Switch to data register */
381 buf[0] = 4;
382 rc = i2c_master_send(state->monitor, buf, 1);
383 if (rc <= 0)
384 goto error;
385 /* Read result */
386 rc = i2c_master_recv(state->monitor, buf, 2);
387 if (rc < 0)
388 goto error;
389 data = ((u16)buf[0]) << 8 | (u16)buf[1];
390 return data >> 6;
391 error:
392 DBG("Error reading ADC, retrying...\n");
393 if (++tries > 10) {
394 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
395 return -1;
397 msleep(10);
401 static int read_lm87_reg(struct i2c_client * chip, int reg)
403 int rc, tries = 0;
404 u8 buf;
406 for (;;) {
407 /* Set address */
408 buf = (u8)reg;
409 rc = i2c_master_send(chip, &buf, 1);
410 if (rc <= 0)
411 goto error;
412 rc = i2c_master_recv(chip, &buf, 1);
413 if (rc <= 0)
414 goto error;
415 return (int)buf;
416 error:
417 DBG("Error reading LM87, retrying...\n");
418 if (++tries > 10) {
419 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
420 return -1;
422 msleep(10);
426 static int fan_read_reg(int reg, unsigned char *buf, int nb)
428 int tries, nr, nw;
430 buf[0] = reg;
431 tries = 0;
432 for (;;) {
433 nw = i2c_master_send(fcu, buf, 1);
434 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
435 break;
436 msleep(10);
437 ++tries;
439 if (nw <= 0) {
440 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
441 return -EIO;
443 tries = 0;
444 for (;;) {
445 nr = i2c_master_recv(fcu, buf, nb);
446 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
447 break;
448 msleep(10);
449 ++tries;
451 if (nr <= 0)
452 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
453 return nr;
456 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
458 int tries, nw;
459 unsigned char buf[16];
461 buf[0] = reg;
462 memcpy(buf+1, ptr, nb);
463 ++nb;
464 tries = 0;
465 for (;;) {
466 nw = i2c_master_send(fcu, buf, nb);
467 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
468 break;
469 msleep(10);
470 ++tries;
472 if (nw < 0)
473 printk(KERN_ERR "Failure writing to FCU: %d", nw);
474 return nw;
477 static int start_fcu(void)
479 unsigned char buf = 0xff;
480 int rc;
482 rc = fan_write_reg(0xe, &buf, 1);
483 if (rc < 0)
484 return -EIO;
485 rc = fan_write_reg(0x2e, &buf, 1);
486 if (rc < 0)
487 return -EIO;
488 rc = fan_read_reg(0, &buf, 1);
489 if (rc < 0)
490 return -EIO;
491 fcu_rpm_shift = (buf == 1) ? 2 : 3;
492 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
493 fcu_rpm_shift);
495 return 0;
498 static int set_rpm_fan(int fan_index, int rpm)
500 unsigned char buf[2];
501 int rc, id, min, max;
503 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
504 return -EINVAL;
505 id = fcu_fans[fan_index].id;
506 if (id == FCU_FAN_ABSENT_ID)
507 return -EINVAL;
509 min = 2400 >> fcu_rpm_shift;
510 max = 56000 >> fcu_rpm_shift;
512 if (rpm < min)
513 rpm = min;
514 else if (rpm > max)
515 rpm = max;
516 buf[0] = rpm >> (8 - fcu_rpm_shift);
517 buf[1] = rpm << fcu_rpm_shift;
518 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
519 if (rc < 0)
520 return -EIO;
521 return 0;
524 static int get_rpm_fan(int fan_index, int programmed)
526 unsigned char failure;
527 unsigned char active;
528 unsigned char buf[2];
529 int rc, id, reg_base;
531 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
532 return -EINVAL;
533 id = fcu_fans[fan_index].id;
534 if (id == FCU_FAN_ABSENT_ID)
535 return -EINVAL;
537 rc = fan_read_reg(0xb, &failure, 1);
538 if (rc != 1)
539 return -EIO;
540 if ((failure & (1 << id)) != 0)
541 return -EFAULT;
542 rc = fan_read_reg(0xd, &active, 1);
543 if (rc != 1)
544 return -EIO;
545 if ((active & (1 << id)) == 0)
546 return -ENXIO;
548 /* Programmed value or real current speed */
549 reg_base = programmed ? 0x10 : 0x11;
550 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
551 if (rc != 2)
552 return -EIO;
554 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
557 static int set_pwm_fan(int fan_index, int pwm)
559 unsigned char buf[2];
560 int rc, id;
562 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
563 return -EINVAL;
564 id = fcu_fans[fan_index].id;
565 if (id == FCU_FAN_ABSENT_ID)
566 return -EINVAL;
568 if (pwm < 10)
569 pwm = 10;
570 else if (pwm > 100)
571 pwm = 100;
572 pwm = (pwm * 2559) / 1000;
573 buf[0] = pwm;
574 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
575 if (rc < 0)
576 return rc;
577 return 0;
580 static int get_pwm_fan(int fan_index)
582 unsigned char failure;
583 unsigned char active;
584 unsigned char buf[2];
585 int rc, id;
587 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
588 return -EINVAL;
589 id = fcu_fans[fan_index].id;
590 if (id == FCU_FAN_ABSENT_ID)
591 return -EINVAL;
593 rc = fan_read_reg(0x2b, &failure, 1);
594 if (rc != 1)
595 return -EIO;
596 if ((failure & (1 << id)) != 0)
597 return -EFAULT;
598 rc = fan_read_reg(0x2d, &active, 1);
599 if (rc != 1)
600 return -EIO;
601 if ((active & (1 << id)) == 0)
602 return -ENXIO;
604 /* Programmed value or real current speed */
605 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
606 if (rc != 1)
607 return -EIO;
609 return (buf[0] * 1000) / 2559;
612 static void tickle_fcu(void)
614 int pwm;
616 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
618 DBG("FCU Tickle, slots fan is: %d\n", pwm);
619 if (pwm < 0)
620 pwm = 100;
622 if (!rackmac) {
623 pwm = SLOTS_FAN_DEFAULT_PWM;
624 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
625 pwm = SLOTS_PID_OUTPUT_MIN;
627 /* That is hopefully enough to make the FCU happy */
628 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
633 * Utility routine to read the CPU calibration EEPROM data
634 * from the device-tree
636 static int read_eeprom(int cpu, struct mpu_data *out)
638 struct device_node *np;
639 char nodename[64];
640 const u8 *data;
641 int len;
643 /* prom.c routine for finding a node by path is a bit brain dead
644 * and requires exact @xxx unit numbers. This is a bit ugly but
645 * will work for these machines
647 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
648 np = of_find_node_by_path(nodename);
649 if (np == NULL) {
650 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
651 return -ENODEV;
653 data = of_get_property(np, "cpuid", &len);
654 if (data == NULL) {
655 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
656 of_node_put(np);
657 return -ENODEV;
659 memcpy(out, data, sizeof(struct mpu_data));
660 of_node_put(np);
662 return 0;
665 static void fetch_cpu_pumps_minmax(void)
667 struct cpu_pid_state *state0 = &cpu_state[0];
668 struct cpu_pid_state *state1 = &cpu_state[1];
669 u16 pump_min = 0, pump_max = 0xffff;
670 u16 tmp[4];
672 /* Try to fetch pumps min/max infos from eeprom */
674 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
675 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
676 pump_min = max(pump_min, tmp[0]);
677 pump_max = min(pump_max, tmp[1]);
679 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
680 pump_min = max(pump_min, tmp[2]);
681 pump_max = min(pump_max, tmp[3]);
684 /* Double check the values, this _IS_ needed as the EEPROM on
685 * some dual 2.5Ghz G5s seem, at least, to have both min & max
686 * same to the same value ... (grrrr)
688 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
689 pump_min = CPU_PUMP_OUTPUT_MIN;
690 pump_max = CPU_PUMP_OUTPUT_MAX;
693 state0->pump_min = state1->pump_min = pump_min;
694 state0->pump_max = state1->pump_max = pump_max;
698 * Now, unfortunately, sysfs doesn't give us a nice void * we could
699 * pass around to the attribute functions, so we don't really have
700 * choice but implement a bunch of them...
702 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
703 * the input twice... I accept patches :)
705 #define BUILD_SHOW_FUNC_FIX(name, data) \
706 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
708 ssize_t r; \
709 mutex_lock(&driver_lock); \
710 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
711 mutex_unlock(&driver_lock); \
712 return r; \
714 #define BUILD_SHOW_FUNC_INT(name, data) \
715 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
717 return sprintf(buf, "%d", data); \
720 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
721 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
722 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
723 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
724 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
726 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
727 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
728 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
729 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
730 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
732 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
733 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
735 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
736 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
738 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
739 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
741 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
743 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
744 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
745 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
746 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
747 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
749 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
750 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
751 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
752 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
753 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
755 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
756 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
758 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
759 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
761 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
762 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
764 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
767 * CPUs fans control loop
770 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
772 s32 ltemp, volts, amps;
773 int index, rc = 0;
775 /* Default (in case of error) */
776 *temp = state->cur_temp;
777 *power = state->cur_power;
779 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
780 index = (state->index == 0) ?
781 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
782 else
783 index = (state->index == 0) ?
784 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
786 /* Read current fan status */
787 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
788 if (rc < 0) {
789 /* XXX What do we do now ? Nothing for now, keep old value, but
790 * return error upstream
792 DBG(" cpu %d, fan reading error !\n", state->index);
793 } else {
794 state->rpm = rc;
795 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
798 /* Get some sensor readings and scale it */
799 ltemp = read_smon_adc(state, 1);
800 if (ltemp == -1) {
801 /* XXX What do we do now ? */
802 state->overtemp++;
803 if (rc == 0)
804 rc = -EIO;
805 DBG(" cpu %d, temp reading error !\n", state->index);
806 } else {
807 /* Fixup temperature according to diode calibration
809 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
810 state->index,
811 ltemp, state->mpu.mdiode, state->mpu.bdiode);
812 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
813 state->last_temp = *temp;
814 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
818 * Read voltage & current and calculate power
820 volts = read_smon_adc(state, 3);
821 amps = read_smon_adc(state, 4);
823 /* Scale voltage and current raw sensor values according to fixed scales
824 * obtained in Darwin and calculate power from I and V
826 volts *= ADC_CPU_VOLTAGE_SCALE;
827 amps *= ADC_CPU_CURRENT_SCALE;
828 *power = (((u64)volts) * ((u64)amps)) >> 16;
829 state->voltage = volts;
830 state->current_a = amps;
831 state->last_power = *power;
833 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
834 state->index, FIX32TOPRINT(state->current_a),
835 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
837 return 0;
840 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
842 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
843 s64 integ_p, deriv_p, prop_p, sum;
844 int i;
846 /* Calculate power target value (could be done once for all)
847 * and convert to a 16.16 fp number
849 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
850 DBG(" power target: %d.%03d, error: %d.%03d\n",
851 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
853 /* Store temperature and power in history array */
854 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
855 state->temp_history[state->cur_temp] = temp;
856 state->cur_power = (state->cur_power + 1) % state->count_power;
857 state->power_history[state->cur_power] = power;
858 state->error_history[state->cur_power] = power_target - power;
860 /* If first loop, fill the history table */
861 if (state->first) {
862 for (i = 0; i < (state->count_power - 1); i++) {
863 state->cur_power = (state->cur_power + 1) % state->count_power;
864 state->power_history[state->cur_power] = power;
865 state->error_history[state->cur_power] = power_target - power;
867 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
868 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
869 state->temp_history[state->cur_temp] = temp;
871 state->first = 0;
874 /* Calculate the integral term normally based on the "power" values */
875 sum = 0;
876 integral = 0;
877 for (i = 0; i < state->count_power; i++)
878 integral += state->error_history[i];
879 integral *= CPU_PID_INTERVAL;
880 DBG(" integral: %08x\n", integral);
882 /* Calculate the adjusted input (sense value).
883 * G_r is 12.20
884 * integ is 16.16
885 * so the result is 28.36
887 * input target is mpu.ttarget, input max is mpu.tmax
889 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
890 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
891 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
892 adj_in_target = (state->mpu.ttarget << 16);
893 if (adj_in_target > sval)
894 adj_in_target = sval;
895 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
896 state->mpu.ttarget);
898 /* Calculate the derivative term */
899 derivative = state->temp_history[state->cur_temp] -
900 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
901 % CPU_TEMP_HISTORY_SIZE];
902 derivative /= CPU_PID_INTERVAL;
903 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
904 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
905 sum += deriv_p;
907 /* Calculate the proportional term */
908 proportional = temp - adj_in_target;
909 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
910 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
911 sum += prop_p;
913 /* Scale sum */
914 sum >>= 36;
916 DBG(" sum: %d\n", (int)sum);
917 state->rpm += (s32)sum;
920 static void do_monitor_cpu_combined(void)
922 struct cpu_pid_state *state0 = &cpu_state[0];
923 struct cpu_pid_state *state1 = &cpu_state[1];
924 s32 temp0, power0, temp1, power1;
925 s32 temp_combi, power_combi;
926 int rc, intake, pump;
928 rc = do_read_one_cpu_values(state0, &temp0, &power0);
929 if (rc < 0) {
930 /* XXX What do we do now ? */
932 state1->overtemp = 0;
933 rc = do_read_one_cpu_values(state1, &temp1, &power1);
934 if (rc < 0) {
935 /* XXX What do we do now ? */
937 if (state1->overtemp)
938 state0->overtemp++;
940 temp_combi = max(temp0, temp1);
941 power_combi = max(power0, power1);
943 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
944 * full blown immediately and try to trigger a shutdown
946 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
947 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
948 temp_combi >> 16);
949 state0->overtemp += CPU_MAX_OVERTEMP / 4;
950 } else if (temp_combi > (state0->mpu.tmax << 16)) {
951 state0->overtemp++;
952 printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
953 temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
954 } else {
955 if (state0->overtemp)
956 printk(KERN_WARNING "Temperature back down to %d\n",
957 temp_combi >> 16);
958 state0->overtemp = 0;
960 if (state0->overtemp >= CPU_MAX_OVERTEMP)
961 critical_state = 1;
962 if (state0->overtemp > 0) {
963 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
964 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
965 pump = state0->pump_max;
966 goto do_set_fans;
969 /* Do the PID */
970 do_cpu_pid(state0, temp_combi, power_combi);
972 /* Range check */
973 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
974 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
976 /* Calculate intake fan speed */
977 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
978 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
979 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
980 state0->intake_rpm = intake;
982 /* Calculate pump speed */
983 pump = (state0->rpm * state0->pump_max) /
984 state0->mpu.rmaxn_exhaust_fan;
985 pump = min(pump, state0->pump_max);
986 pump = max(pump, state0->pump_min);
988 do_set_fans:
989 /* We copy values from state 0 to state 1 for /sysfs */
990 state1->rpm = state0->rpm;
991 state1->intake_rpm = state0->intake_rpm;
993 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
994 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
996 /* We should check for errors, shouldn't we ? But then, what
997 * do we do once the error occurs ? For FCU notified fan
998 * failures (-EFAULT) we probably want to notify userland
999 * some way...
1001 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1002 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1003 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1004 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1006 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1007 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1008 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1009 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1012 static void do_monitor_cpu_split(struct cpu_pid_state *state)
1014 s32 temp, power;
1015 int rc, intake;
1017 /* Read current fan status */
1018 rc = do_read_one_cpu_values(state, &temp, &power);
1019 if (rc < 0) {
1020 /* XXX What do we do now ? */
1023 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1024 * full blown immediately and try to trigger a shutdown
1026 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1027 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1028 " (%d) !\n",
1029 state->index, temp >> 16);
1030 state->overtemp += CPU_MAX_OVERTEMP / 4;
1031 } else if (temp > (state->mpu.tmax << 16)) {
1032 state->overtemp++;
1033 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1034 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1035 } else {
1036 if (state->overtemp)
1037 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1038 state->index, temp >> 16);
1039 state->overtemp = 0;
1041 if (state->overtemp >= CPU_MAX_OVERTEMP)
1042 critical_state = 1;
1043 if (state->overtemp > 0) {
1044 state->rpm = state->mpu.rmaxn_exhaust_fan;
1045 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1046 goto do_set_fans;
1049 /* Do the PID */
1050 do_cpu_pid(state, temp, power);
1052 /* Range check */
1053 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1054 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1056 /* Calculate intake fan */
1057 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1058 intake = max(intake, (int)state->mpu.rminn_intake_fan);
1059 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1060 state->intake_rpm = intake;
1062 do_set_fans:
1063 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1064 state->index, (int)state->rpm, intake, state->overtemp);
1066 /* We should check for errors, shouldn't we ? But then, what
1067 * do we do once the error occurs ? For FCU notified fan
1068 * failures (-EFAULT) we probably want to notify userland
1069 * some way...
1071 if (state->index == 0) {
1072 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1073 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1074 } else {
1075 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1076 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1080 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1082 s32 temp, power, fan_min;
1083 int rc;
1085 /* Read current fan status */
1086 rc = do_read_one_cpu_values(state, &temp, &power);
1087 if (rc < 0) {
1088 /* XXX What do we do now ? */
1091 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1092 * full blown immediately and try to trigger a shutdown
1094 if (temp >= ((state->mpu.tmax + 8) << 16)) {
1095 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1096 " (%d) !\n",
1097 state->index, temp >> 16);
1098 state->overtemp = CPU_MAX_OVERTEMP / 4;
1099 } else if (temp > (state->mpu.tmax << 16)) {
1100 state->overtemp++;
1101 printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1102 state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1103 } else {
1104 if (state->overtemp)
1105 printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1106 state->index, temp >> 16);
1107 state->overtemp = 0;
1109 if (state->overtemp >= CPU_MAX_OVERTEMP)
1110 critical_state = 1;
1111 if (state->overtemp > 0) {
1112 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1113 goto do_set_fans;
1116 /* Do the PID */
1117 do_cpu_pid(state, temp, power);
1119 /* Check clamp from dimms */
1120 fan_min = dimm_output_clamp;
1121 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1123 DBG(" CPU min mpu = %d, min dimm = %d\n",
1124 state->mpu.rminn_intake_fan, dimm_output_clamp);
1126 state->rpm = max(state->rpm, (int)fan_min);
1127 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1128 state->intake_rpm = state->rpm;
1130 do_set_fans:
1131 DBG("** CPU %d RPM: %d overtemp: %d\n",
1132 state->index, (int)state->rpm, state->overtemp);
1134 /* We should check for errors, shouldn't we ? But then, what
1135 * do we do once the error occurs ? For FCU notified fan
1136 * failures (-EFAULT) we probably want to notify userland
1137 * some way...
1139 if (state->index == 0) {
1140 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1141 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1142 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1143 } else {
1144 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1145 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1146 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1151 * Initialize the state structure for one CPU control loop
1153 static int init_cpu_state(struct cpu_pid_state *state, int index)
1155 int err;
1157 state->index = index;
1158 state->first = 1;
1159 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1160 state->overtemp = 0;
1161 state->adc_config = 0x00;
1164 if (index == 0)
1165 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1166 else if (index == 1)
1167 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1168 if (state->monitor == NULL)
1169 goto fail;
1171 if (read_eeprom(index, &state->mpu))
1172 goto fail;
1174 state->count_power = state->mpu.tguardband;
1175 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1176 printk(KERN_WARNING "Warning ! too many power history slots\n");
1177 state->count_power = CPU_POWER_HISTORY_SIZE;
1179 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1181 if (index == 0) {
1182 err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1183 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1184 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1185 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1186 err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1187 } else {
1188 err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1189 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1190 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1191 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1192 err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1194 if (err)
1195 printk(KERN_WARNING "Failed to create some of the atribute"
1196 "files for CPU %d\n", index);
1198 return 0;
1199 fail:
1200 state->monitor = NULL;
1202 return -ENODEV;
1206 * Dispose of the state data for one CPU control loop
1208 static void dispose_cpu_state(struct cpu_pid_state *state)
1210 if (state->monitor == NULL)
1211 return;
1213 if (state->index == 0) {
1214 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1215 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1216 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1217 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1218 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1219 } else {
1220 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1221 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1222 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1223 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1224 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1227 state->monitor = NULL;
1231 * Motherboard backside & U3 heatsink fan control loop
1233 static void do_monitor_backside(struct backside_pid_state *state)
1235 s32 temp, integral, derivative, fan_min;
1236 s64 integ_p, deriv_p, prop_p, sum;
1237 int i, rc;
1239 if (--state->ticks != 0)
1240 return;
1241 state->ticks = backside_params.interval;
1243 DBG("backside:\n");
1245 /* Check fan status */
1246 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1247 if (rc < 0) {
1248 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1249 /* XXX What do we do now ? */
1250 } else
1251 state->pwm = rc;
1252 DBG(" current pwm: %d\n", state->pwm);
1254 /* Get some sensor readings */
1255 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1256 state->last_temp = temp;
1257 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1258 FIX32TOPRINT(backside_params.input_target));
1260 /* Store temperature and error in history array */
1261 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1262 state->sample_history[state->cur_sample] = temp;
1263 state->error_history[state->cur_sample] = temp - backside_params.input_target;
1265 /* If first loop, fill the history table */
1266 if (state->first) {
1267 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1268 state->cur_sample = (state->cur_sample + 1) %
1269 BACKSIDE_PID_HISTORY_SIZE;
1270 state->sample_history[state->cur_sample] = temp;
1271 state->error_history[state->cur_sample] =
1272 temp - backside_params.input_target;
1274 state->first = 0;
1277 /* Calculate the integral term */
1278 sum = 0;
1279 integral = 0;
1280 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1281 integral += state->error_history[i];
1282 integral *= backside_params.interval;
1283 DBG(" integral: %08x\n", integral);
1284 integ_p = ((s64)backside_params.G_r) * (s64)integral;
1285 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1286 sum += integ_p;
1288 /* Calculate the derivative term */
1289 derivative = state->error_history[state->cur_sample] -
1290 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1291 % BACKSIDE_PID_HISTORY_SIZE];
1292 derivative /= backside_params.interval;
1293 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1294 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1295 sum += deriv_p;
1297 /* Calculate the proportional term */
1298 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1299 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1300 sum += prop_p;
1302 /* Scale sum */
1303 sum >>= 36;
1305 DBG(" sum: %d\n", (int)sum);
1306 if (backside_params.additive)
1307 state->pwm += (s32)sum;
1308 else
1309 state->pwm = sum;
1311 /* Check for clamp */
1312 fan_min = (dimm_output_clamp * 100) / 14000;
1313 fan_min = max(fan_min, backside_params.output_min);
1315 state->pwm = max(state->pwm, fan_min);
1316 state->pwm = min(state->pwm, backside_params.output_max);
1318 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1319 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1323 * Initialize the state structure for the backside fan control loop
1325 static int init_backside_state(struct backside_pid_state *state)
1327 struct device_node *u3;
1328 int u3h = 1; /* conservative by default */
1329 int err;
1332 * There are different PID params for machines with U3 and machines
1333 * with U3H, pick the right ones now
1335 u3 = of_find_node_by_path("/u3@0,f8000000");
1336 if (u3 != NULL) {
1337 const u32 *vers = of_get_property(u3, "device-rev", NULL);
1338 if (vers)
1339 if (((*vers) & 0x3f) < 0x34)
1340 u3h = 0;
1341 of_node_put(u3);
1344 if (rackmac) {
1345 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1346 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1347 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1348 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1349 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1350 backside_params.G_r = BACKSIDE_PID_G_r;
1351 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1352 backside_params.additive = 0;
1353 } else if (u3h) {
1354 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1355 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1356 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1357 backside_params.interval = BACKSIDE_PID_INTERVAL;
1358 backside_params.G_p = BACKSIDE_PID_G_p;
1359 backside_params.G_r = BACKSIDE_PID_G_r;
1360 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1361 backside_params.additive = 1;
1362 } else {
1363 backside_params.G_d = BACKSIDE_PID_U3_G_d;
1364 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1365 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1366 backside_params.interval = BACKSIDE_PID_INTERVAL;
1367 backside_params.G_p = BACKSIDE_PID_G_p;
1368 backside_params.G_r = BACKSIDE_PID_G_r;
1369 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1370 backside_params.additive = 1;
1373 state->ticks = 1;
1374 state->first = 1;
1375 state->pwm = 50;
1377 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1378 if (state->monitor == NULL)
1379 return -ENODEV;
1381 err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1382 err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1383 if (err)
1384 printk(KERN_WARNING "Failed to create attribute file(s)"
1385 " for backside fan\n");
1387 return 0;
1391 * Dispose of the state data for the backside control loop
1393 static void dispose_backside_state(struct backside_pid_state *state)
1395 if (state->monitor == NULL)
1396 return;
1398 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1399 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1401 state->monitor = NULL;
1405 * Drives bay fan control loop
1407 static void do_monitor_drives(struct drives_pid_state *state)
1409 s32 temp, integral, derivative;
1410 s64 integ_p, deriv_p, prop_p, sum;
1411 int i, rc;
1413 if (--state->ticks != 0)
1414 return;
1415 state->ticks = DRIVES_PID_INTERVAL;
1417 DBG("drives:\n");
1419 /* Check fan status */
1420 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1421 if (rc < 0) {
1422 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1423 /* XXX What do we do now ? */
1424 } else
1425 state->rpm = rc;
1426 DBG(" current rpm: %d\n", state->rpm);
1428 /* Get some sensor readings */
1429 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1430 DS1775_TEMP)) << 8;
1431 state->last_temp = temp;
1432 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1433 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1435 /* Store temperature and error in history array */
1436 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1437 state->sample_history[state->cur_sample] = temp;
1438 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1440 /* If first loop, fill the history table */
1441 if (state->first) {
1442 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1443 state->cur_sample = (state->cur_sample + 1) %
1444 DRIVES_PID_HISTORY_SIZE;
1445 state->sample_history[state->cur_sample] = temp;
1446 state->error_history[state->cur_sample] =
1447 temp - DRIVES_PID_INPUT_TARGET;
1449 state->first = 0;
1452 /* Calculate the integral term */
1453 sum = 0;
1454 integral = 0;
1455 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1456 integral += state->error_history[i];
1457 integral *= DRIVES_PID_INTERVAL;
1458 DBG(" integral: %08x\n", integral);
1459 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1460 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1461 sum += integ_p;
1463 /* Calculate the derivative term */
1464 derivative = state->error_history[state->cur_sample] -
1465 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1466 % DRIVES_PID_HISTORY_SIZE];
1467 derivative /= DRIVES_PID_INTERVAL;
1468 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1469 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1470 sum += deriv_p;
1472 /* Calculate the proportional term */
1473 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1474 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1475 sum += prop_p;
1477 /* Scale sum */
1478 sum >>= 36;
1480 DBG(" sum: %d\n", (int)sum);
1481 state->rpm += (s32)sum;
1483 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1484 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1486 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1487 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1491 * Initialize the state structure for the drives bay fan control loop
1493 static int init_drives_state(struct drives_pid_state *state)
1495 int err;
1497 state->ticks = 1;
1498 state->first = 1;
1499 state->rpm = 1000;
1501 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1502 if (state->monitor == NULL)
1503 return -ENODEV;
1505 err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1506 err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1507 if (err)
1508 printk(KERN_WARNING "Failed to create attribute file(s)"
1509 " for drives bay fan\n");
1511 return 0;
1515 * Dispose of the state data for the drives control loop
1517 static void dispose_drives_state(struct drives_pid_state *state)
1519 if (state->monitor == NULL)
1520 return;
1522 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1523 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1525 state->monitor = NULL;
1529 * DIMMs temp control loop
1531 static void do_monitor_dimms(struct dimm_pid_state *state)
1533 s32 temp, integral, derivative, fan_min;
1534 s64 integ_p, deriv_p, prop_p, sum;
1535 int i;
1537 if (--state->ticks != 0)
1538 return;
1539 state->ticks = DIMM_PID_INTERVAL;
1541 DBG("DIMM:\n");
1543 DBG(" current value: %d\n", state->output);
1545 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1546 if (temp < 0)
1547 return;
1548 temp <<= 16;
1549 state->last_temp = temp;
1550 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1551 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1553 /* Store temperature and error in history array */
1554 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1555 state->sample_history[state->cur_sample] = temp;
1556 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1558 /* If first loop, fill the history table */
1559 if (state->first) {
1560 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1561 state->cur_sample = (state->cur_sample + 1) %
1562 DIMM_PID_HISTORY_SIZE;
1563 state->sample_history[state->cur_sample] = temp;
1564 state->error_history[state->cur_sample] =
1565 temp - DIMM_PID_INPUT_TARGET;
1567 state->first = 0;
1570 /* Calculate the integral term */
1571 sum = 0;
1572 integral = 0;
1573 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1574 integral += state->error_history[i];
1575 integral *= DIMM_PID_INTERVAL;
1576 DBG(" integral: %08x\n", integral);
1577 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1578 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1579 sum += integ_p;
1581 /* Calculate the derivative term */
1582 derivative = state->error_history[state->cur_sample] -
1583 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1584 % DIMM_PID_HISTORY_SIZE];
1585 derivative /= DIMM_PID_INTERVAL;
1586 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1587 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1588 sum += deriv_p;
1590 /* Calculate the proportional term */
1591 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1592 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1593 sum += prop_p;
1595 /* Scale sum */
1596 sum >>= 36;
1598 DBG(" sum: %d\n", (int)sum);
1599 state->output = (s32)sum;
1600 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1601 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1602 dimm_output_clamp = state->output;
1604 DBG("** DIMM clamp value: %d\n", (int)state->output);
1606 /* Backside PID is only every 5 seconds, force backside fan clamping now */
1607 fan_min = (dimm_output_clamp * 100) / 14000;
1608 fan_min = max(fan_min, backside_params.output_min);
1609 if (backside_state.pwm < fan_min) {
1610 backside_state.pwm = fan_min;
1611 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
1612 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1617 * Initialize the state structure for the DIMM temp control loop
1619 static int init_dimms_state(struct dimm_pid_state *state)
1621 state->ticks = 1;
1622 state->first = 1;
1623 state->output = 4000;
1625 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1626 if (state->monitor == NULL)
1627 return -ENODEV;
1629 if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1630 printk(KERN_WARNING "Failed to create attribute file"
1631 " for DIMM temperature\n");
1633 return 0;
1637 * Dispose of the state data for the DIMM control loop
1639 static void dispose_dimms_state(struct dimm_pid_state *state)
1641 if (state->monitor == NULL)
1642 return;
1644 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1646 state->monitor = NULL;
1650 * Slots fan control loop
1652 static void do_monitor_slots(struct slots_pid_state *state)
1654 s32 temp, integral, derivative;
1655 s64 integ_p, deriv_p, prop_p, sum;
1656 int i, rc;
1658 if (--state->ticks != 0)
1659 return;
1660 state->ticks = SLOTS_PID_INTERVAL;
1662 DBG("slots:\n");
1664 /* Check fan status */
1665 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1666 if (rc < 0) {
1667 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1668 /* XXX What do we do now ? */
1669 } else
1670 state->pwm = rc;
1671 DBG(" current pwm: %d\n", state->pwm);
1673 /* Get some sensor readings */
1674 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1675 DS1775_TEMP)) << 8;
1676 state->last_temp = temp;
1677 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1678 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1680 /* Store temperature and error in history array */
1681 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1682 state->sample_history[state->cur_sample] = temp;
1683 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1685 /* If first loop, fill the history table */
1686 if (state->first) {
1687 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1688 state->cur_sample = (state->cur_sample + 1) %
1689 SLOTS_PID_HISTORY_SIZE;
1690 state->sample_history[state->cur_sample] = temp;
1691 state->error_history[state->cur_sample] =
1692 temp - SLOTS_PID_INPUT_TARGET;
1694 state->first = 0;
1697 /* Calculate the integral term */
1698 sum = 0;
1699 integral = 0;
1700 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
1701 integral += state->error_history[i];
1702 integral *= SLOTS_PID_INTERVAL;
1703 DBG(" integral: %08x\n", integral);
1704 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
1705 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
1706 sum += integ_p;
1708 /* Calculate the derivative term */
1709 derivative = state->error_history[state->cur_sample] -
1710 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
1711 % SLOTS_PID_HISTORY_SIZE];
1712 derivative /= SLOTS_PID_INTERVAL;
1713 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
1714 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
1715 sum += deriv_p;
1717 /* Calculate the proportional term */
1718 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1719 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
1720 sum += prop_p;
1722 /* Scale sum */
1723 sum >>= 36;
1725 DBG(" sum: %d\n", (int)sum);
1726 state->pwm = (s32)sum;
1728 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1729 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1731 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1732 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1736 * Initialize the state structure for the slots bay fan control loop
1738 static int init_slots_state(struct slots_pid_state *state)
1740 int err;
1742 state->ticks = 1;
1743 state->first = 1;
1744 state->pwm = 50;
1746 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1747 if (state->monitor == NULL)
1748 return -ENODEV;
1750 err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1751 err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1752 if (err)
1753 printk(KERN_WARNING "Failed to create attribute file(s)"
1754 " for slots bay fan\n");
1756 return 0;
1760 * Dispose of the state data for the slots control loop
1762 static void dispose_slots_state(struct slots_pid_state *state)
1764 if (state->monitor == NULL)
1765 return;
1767 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1768 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1770 state->monitor = NULL;
1774 static int call_critical_overtemp(void)
1776 char *argv[] = { critical_overtemp_path, NULL };
1777 static char *envp[] = { "HOME=/",
1778 "TERM=linux",
1779 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1780 NULL };
1782 return call_usermodehelper(critical_overtemp_path,
1783 argv, envp, UMH_WAIT_EXEC);
1788 * Here's the kernel thread that calls the various control loops
1790 static int main_control_loop(void *x)
1792 DBG("main_control_loop started\n");
1794 mutex_lock(&driver_lock);
1796 if (start_fcu() < 0) {
1797 printk(KERN_ERR "kfand: failed to start FCU\n");
1798 mutex_unlock(&driver_lock);
1799 goto out;
1802 /* Set the PCI fan once for now on non-RackMac */
1803 if (!rackmac)
1804 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
1806 /* Initialize ADCs */
1807 initialize_adc(&cpu_state[0]);
1808 if (cpu_state[1].monitor != NULL)
1809 initialize_adc(&cpu_state[1]);
1811 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1813 mutex_unlock(&driver_lock);
1815 while (state == state_attached) {
1816 unsigned long elapsed, start;
1818 start = jiffies;
1820 mutex_lock(&driver_lock);
1822 /* Tickle the FCU just in case */
1823 if (--fcu_tickle_ticks < 0) {
1824 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1825 tickle_fcu();
1828 /* First, we always calculate the new DIMMs state on an Xserve */
1829 if (rackmac)
1830 do_monitor_dimms(&dimms_state);
1832 /* Then, the CPUs */
1833 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1834 do_monitor_cpu_combined();
1835 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
1836 do_monitor_cpu_rack(&cpu_state[0]);
1837 if (cpu_state[1].monitor != NULL)
1838 do_monitor_cpu_rack(&cpu_state[1]);
1839 // better deal with UP
1840 } else {
1841 do_monitor_cpu_split(&cpu_state[0]);
1842 if (cpu_state[1].monitor != NULL)
1843 do_monitor_cpu_split(&cpu_state[1]);
1844 // better deal with UP
1846 /* Then, the rest */
1847 do_monitor_backside(&backside_state);
1848 if (rackmac)
1849 do_monitor_slots(&slots_state);
1850 else
1851 do_monitor_drives(&drives_state);
1852 mutex_unlock(&driver_lock);
1854 if (critical_state == 1) {
1855 printk(KERN_WARNING "Temperature control detected a critical condition\n");
1856 printk(KERN_WARNING "Attempting to shut down...\n");
1857 if (call_critical_overtemp()) {
1858 printk(KERN_WARNING "Can't call %s, power off now!\n",
1859 critical_overtemp_path);
1860 machine_power_off();
1863 if (critical_state > 0)
1864 critical_state++;
1865 if (critical_state > MAX_CRITICAL_STATE) {
1866 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1867 machine_power_off();
1870 // FIXME: Deal with signals
1871 elapsed = jiffies - start;
1872 if (elapsed < HZ)
1873 schedule_timeout_interruptible(HZ - elapsed);
1876 out:
1877 DBG("main_control_loop ended\n");
1879 ctrl_task = 0;
1880 complete_and_exit(&ctrl_complete, 0);
1884 * Dispose the control loops when tearing down
1886 static void dispose_control_loops(void)
1888 dispose_cpu_state(&cpu_state[0]);
1889 dispose_cpu_state(&cpu_state[1]);
1890 dispose_backside_state(&backside_state);
1891 dispose_drives_state(&drives_state);
1892 dispose_slots_state(&slots_state);
1893 dispose_dimms_state(&dimms_state);
1897 * Create the control loops. U3-0 i2c bus is up, so we can now
1898 * get to the various sensors
1900 static int create_control_loops(void)
1902 struct device_node *np;
1904 /* Count CPUs from the device-tree, we don't care how many are
1905 * actually used by Linux
1907 cpu_count = 0;
1908 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1909 cpu_count++;
1911 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1913 /* Decide the type of PID algorithm to use based on the presence of
1914 * the pumps, though that may not be the best way, that is good enough
1915 * for now
1917 if (rackmac)
1918 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
1919 else if (of_machine_is_compatible("PowerMac7,3")
1920 && (cpu_count > 1)
1921 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
1922 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
1923 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
1924 cpu_pid_type = CPU_PID_TYPE_COMBINED;
1925 } else
1926 cpu_pid_type = CPU_PID_TYPE_SPLIT;
1928 /* Create control loops for everything. If any fail, everything
1929 * fails
1931 if (init_cpu_state(&cpu_state[0], 0))
1932 goto fail;
1933 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
1934 fetch_cpu_pumps_minmax();
1936 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1937 goto fail;
1938 if (init_backside_state(&backside_state))
1939 goto fail;
1940 if (rackmac && init_dimms_state(&dimms_state))
1941 goto fail;
1942 if (rackmac && init_slots_state(&slots_state))
1943 goto fail;
1944 if (!rackmac && init_drives_state(&drives_state))
1945 goto fail;
1947 DBG("all control loops up !\n");
1949 return 0;
1951 fail:
1952 DBG("failure creating control loops, disposing\n");
1954 dispose_control_loops();
1956 return -ENODEV;
1960 * Start the control loops after everything is up, that is create
1961 * the thread that will make them run
1963 static void start_control_loops(void)
1965 init_completion(&ctrl_complete);
1967 ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
1971 * Stop the control loops when tearing down
1973 static void stop_control_loops(void)
1975 if (ctrl_task)
1976 wait_for_completion(&ctrl_complete);
1980 * Attach to the i2c FCU after detecting U3-1 bus
1982 static int attach_fcu(void)
1984 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1985 if (fcu == NULL)
1986 return -ENODEV;
1988 DBG("FCU attached\n");
1990 return 0;
1994 * Detach from the i2c FCU when tearing down
1996 static void detach_fcu(void)
1998 fcu = NULL;
2002 * Attach to the i2c controller. We probe the various chips based
2003 * on the device-tree nodes and build everything for the driver to
2004 * run, we then kick the driver monitoring thread
2006 static int therm_pm72_attach(struct i2c_adapter *adapter)
2008 mutex_lock(&driver_lock);
2010 /* Check state */
2011 if (state == state_detached)
2012 state = state_attaching;
2013 if (state != state_attaching) {
2014 mutex_unlock(&driver_lock);
2015 return 0;
2018 /* Check if we are looking for one of these */
2019 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
2020 u3_0 = adapter;
2021 DBG("found U3-0\n");
2022 if (k2 || !rackmac)
2023 if (create_control_loops())
2024 u3_0 = NULL;
2025 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
2026 u3_1 = adapter;
2027 DBG("found U3-1, attaching FCU\n");
2028 if (attach_fcu())
2029 u3_1 = NULL;
2030 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
2031 k2 = adapter;
2032 DBG("Found K2\n");
2033 if (u3_0 && rackmac)
2034 if (create_control_loops())
2035 k2 = NULL;
2037 /* We got all we need, start control loops */
2038 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
2039 DBG("everything up, starting control loops\n");
2040 state = state_attached;
2041 start_control_loops();
2043 mutex_unlock(&driver_lock);
2045 return 0;
2048 static int therm_pm72_probe(struct i2c_client *client,
2049 const struct i2c_device_id *id)
2051 /* Always succeed, the real work was done in therm_pm72_attach() */
2052 return 0;
2056 * Called when any of the devices which participates into thermal management
2057 * is going away.
2059 static int therm_pm72_remove(struct i2c_client *client)
2061 struct i2c_adapter *adapter = client->adapter;
2063 mutex_lock(&driver_lock);
2065 if (state != state_detached)
2066 state = state_detaching;
2068 /* Stop control loops if any */
2069 DBG("stopping control loops\n");
2070 mutex_unlock(&driver_lock);
2071 stop_control_loops();
2072 mutex_lock(&driver_lock);
2074 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
2075 DBG("lost U3-0, disposing control loops\n");
2076 dispose_control_loops();
2077 u3_0 = NULL;
2080 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
2081 DBG("lost U3-1, detaching FCU\n");
2082 detach_fcu();
2083 u3_1 = NULL;
2085 if (u3_0 == NULL && u3_1 == NULL)
2086 state = state_detached;
2088 mutex_unlock(&driver_lock);
2090 return 0;
2094 * i2c_driver structure to attach to the host i2c controller
2097 static const struct i2c_device_id therm_pm72_id[] = {
2099 * Fake device name, thermal management is done by several
2100 * chips but we don't need to differentiate between them at
2101 * this point.
2103 { "therm_pm72", 0 },
2107 static struct i2c_driver therm_pm72_driver = {
2108 .driver = {
2109 .name = "therm_pm72",
2111 .attach_adapter = therm_pm72_attach,
2112 .probe = therm_pm72_probe,
2113 .remove = therm_pm72_remove,
2114 .id_table = therm_pm72_id,
2117 static int fan_check_loc_match(const char *loc, int fan)
2119 char tmp[64];
2120 char *c, *e;
2122 strlcpy(tmp, fcu_fans[fan].loc, 64);
2124 c = tmp;
2125 for (;;) {
2126 e = strchr(c, ',');
2127 if (e)
2128 *e = 0;
2129 if (strcmp(loc, c) == 0)
2130 return 1;
2131 if (e == NULL)
2132 break;
2133 c = e + 1;
2135 return 0;
2138 static void fcu_lookup_fans(struct device_node *fcu_node)
2140 struct device_node *np = NULL;
2141 int i;
2143 /* The table is filled by default with values that are suitable
2144 * for the old machines without device-tree informations. We scan
2145 * the device-tree and override those values with whatever is
2146 * there
2149 DBG("Looking up FCU controls in device-tree...\n");
2151 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2152 int type = -1;
2153 const char *loc;
2154 const u32 *reg;
2156 DBG(" control: %s, type: %s\n", np->name, np->type);
2158 /* Detect control type */
2159 if (!strcmp(np->type, "fan-rpm-control") ||
2160 !strcmp(np->type, "fan-rpm"))
2161 type = FCU_FAN_RPM;
2162 if (!strcmp(np->type, "fan-pwm-control") ||
2163 !strcmp(np->type, "fan-pwm"))
2164 type = FCU_FAN_PWM;
2165 /* Only care about fans for now */
2166 if (type == -1)
2167 continue;
2169 /* Lookup for a matching location */
2170 loc = of_get_property(np, "location", NULL);
2171 reg = of_get_property(np, "reg", NULL);
2172 if (loc == NULL || reg == NULL)
2173 continue;
2174 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2176 for (i = 0; i < FCU_FAN_COUNT; i++) {
2177 int fan_id;
2179 if (!fan_check_loc_match(loc, i))
2180 continue;
2181 DBG(" location match, index: %d\n", i);
2182 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
2183 if (type != fcu_fans[i].type) {
2184 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
2185 "in device-tree for %s\n", np->full_name);
2186 break;
2188 if (type == FCU_FAN_RPM)
2189 fan_id = ((*reg) - 0x10) / 2;
2190 else
2191 fan_id = ((*reg) - 0x30) / 2;
2192 if (fan_id > 7) {
2193 printk(KERN_WARNING "therm_pm72: Can't parse "
2194 "fan ID in device-tree for %s\n", np->full_name);
2195 break;
2197 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2198 fcu_fans[i].id = fan_id;
2202 /* Now dump the array */
2203 printk(KERN_INFO "Detected fan controls:\n");
2204 for (i = 0; i < FCU_FAN_COUNT; i++) {
2205 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
2206 continue;
2207 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
2208 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
2209 fcu_fans[i].id, fcu_fans[i].loc);
2213 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
2215 state = state_detached;
2217 /* Lookup the fans in the device tree */
2218 fcu_lookup_fans(dev->node);
2220 /* Add the driver */
2221 return i2c_add_driver(&therm_pm72_driver);
2224 static int fcu_of_remove(struct of_device* dev)
2226 i2c_del_driver(&therm_pm72_driver);
2228 return 0;
2231 static const struct of_device_id fcu_match[] =
2234 .type = "fcu",
2239 static struct of_platform_driver fcu_of_platform_driver =
2241 .name = "temperature",
2242 .match_table = fcu_match,
2243 .probe = fcu_of_probe,
2244 .remove = fcu_of_remove
2248 * Check machine type, attach to i2c controller
2250 static int __init therm_pm72_init(void)
2252 struct device_node *np;
2254 rackmac = of_machine_is_compatible("RackMac3,1");
2256 if (!of_machine_is_compatible("PowerMac7,2") &&
2257 !of_machine_is_compatible("PowerMac7,3") &&
2258 !rackmac)
2259 return -ENODEV;
2261 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2263 np = of_find_node_by_type(NULL, "fcu");
2264 if (np == NULL) {
2265 /* Some machines have strangely broken device-tree */
2266 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
2267 if (np == NULL) {
2268 printk(KERN_ERR "Can't find FCU in device-tree !\n");
2269 return -ENODEV;
2272 of_dev = of_platform_device_create(np, "temperature", NULL);
2273 if (of_dev == NULL) {
2274 printk(KERN_ERR "Can't register FCU platform device !\n");
2275 return -ENODEV;
2278 of_register_platform_driver(&fcu_of_platform_driver);
2280 return 0;
2283 static void __exit therm_pm72_exit(void)
2285 of_unregister_platform_driver(&fcu_of_platform_driver);
2287 if (of_dev)
2288 of_device_unregister(of_dev);
2291 module_init(therm_pm72_init);
2292 module_exit(therm_pm72_exit);
2294 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2295 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2296 MODULE_LICENSE("GPL");