blob: e43554e754a46f09bebf28732857fac8f2794441 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device driver for the thermostats & fan controller of the
3 * Apple G5 "PowerMac7,2" desktop machines.
4 *
5 * (c) Copyright IBM Corp. 2003-2004
6 *
7 * Maintained by: Benjamin Herrenschmidt
8 * <benh@kernel.crashing.org>
9 *
10 *
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.
14 *
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
23 *
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.
28 *
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 ...
33 *
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...
43 *
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
55 *
56 * History:
57 *
58 * Nov. 13, 2003 : 0.5
59 * - First release
60 *
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
66 *
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
71 *
72 * Dec. 18, 2003 : 0.8
73 * - Fix typo when reading back fan speed on 2 CPU machines
74 *
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
82 *
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
91 *
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
97 *
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +100098 * 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
108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <linux/slab.h>
118#include <linux/init.h>
119#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <linux/wait.h>
121#include <linux/reboot.h>
122#include <linux/kmod.h>
123#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#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>
Jeff Mahoney5e655772005-07-06 15:44:41 -0400130#include <asm/macio.h>
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +1100131#include <asm/of_platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133#include "therm_pm72.h"
134
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000135#define VERSION "1.3"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#undef DEBUG
138
139#ifdef DEBUG
140#define DBG(args...) printk(args)
141#else
142#define DBG(args...) do { } while(0)
143#endif
144
145
146/*
147 * Driver statics
148 */
149
150static struct of_device * of_dev;
151static struct i2c_adapter * u3_0;
152static struct i2c_adapter * u3_1;
153static struct i2c_adapter * k2;
154static struct i2c_client * fcu;
155static struct cpu_pid_state cpu_state[2];
156static struct basckside_pid_params backside_params;
157static struct backside_pid_state backside_state;
158static struct drives_pid_state drives_state;
159static struct dimm_pid_state dimms_state;
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000160static struct slots_pid_state slots_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int state;
162static int cpu_count;
163static int cpu_pid_type;
164static pid_t ctrl_task;
165static struct completion ctrl_complete;
166static int critical_state;
167static int rackmac;
168static s32 dimm_output_clamp;
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000169static int fcu_rpm_shift;
170static int fcu_tickle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static DECLARE_MUTEX(driver_lock);
172
173/*
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
181 */
182#define CPU_PID_TYPE_SPLIT 0
183#define CPU_PID_TYPE_COMBINED 1
184#define CPU_PID_TYPE_RACKMAC 2
185
186/*
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
190 */
191struct fcu_fan_table
192{
193 char* loc; /* location code */
194 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
195 int id; /* id or -1 */
196};
197
198#define FCU_FAN_RPM 0
199#define FCU_FAN_PWM 1
200
201#define FCU_FAN_ABSENT_ID -1
202
203#define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
204
205struct 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,
210 },
211 [DRIVES_FAN_RPM_INDEX] = {
212 .loc = "DRIVE BAY",
213 .type = FCU_FAN_RPM,
214 .id = DRIVES_FAN_RPM_DEFAULT_ID,
215 },
216 [SLOTS_FAN_PWM_INDEX] = {
217 .loc = "SLOT,PCI FAN",
218 .type = FCU_FAN_PWM,
219 .id = SLOTS_FAN_PWM_DEFAULT_ID,
220 },
221 [CPUA_INTAKE_FAN_RPM_INDEX] = {
222 .loc = "CPU A INTAKE",
223 .type = FCU_FAN_RPM,
224 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
225 },
226 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
227 .loc = "CPU A EXHAUST",
228 .type = FCU_FAN_RPM,
229 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
230 },
231 [CPUB_INTAKE_FAN_RPM_INDEX] = {
232 .loc = "CPU B INTAKE",
233 .type = FCU_FAN_RPM,
234 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
235 },
236 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
237 .loc = "CPU B EXHAUST",
238 .type = FCU_FAN_RPM,
239 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
240 },
241 /* pumps aren't present by default, have to be looked up in the
242 * device-tree
243 */
244 [CPUA_PUMP_RPM_INDEX] = {
245 .loc = "CPU A PUMP",
246 .type = FCU_FAN_RPM,
247 .id = FCU_FAN_ABSENT_ID,
248 },
249 [CPUB_PUMP_RPM_INDEX] = {
250 .loc = "CPU B PUMP",
251 .type = FCU_FAN_RPM,
252 .id = FCU_FAN_ABSENT_ID,
253 },
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,
259 },
260 [CPU_A2_FAN_RPM_INDEX] = {
261 .loc = "CPU A 2",
262 .type = FCU_FAN_RPM,
263 .id = FCU_FAN_ABSENT_ID,
264 },
265 [CPU_A3_FAN_RPM_INDEX] = {
266 .loc = "CPU A 3",
267 .type = FCU_FAN_RPM,
268 .id = FCU_FAN_ABSENT_ID,
269 },
270 [CPU_B1_FAN_RPM_INDEX] = {
271 .loc = "CPU B 1",
272 .type = FCU_FAN_RPM,
273 .id = FCU_FAN_ABSENT_ID,
274 },
275 [CPU_B2_FAN_RPM_INDEX] = {
276 .loc = "CPU B 2",
277 .type = FCU_FAN_RPM,
278 .id = FCU_FAN_ABSENT_ID,
279 },
280 [CPU_B3_FAN_RPM_INDEX] = {
281 .loc = "CPU B 3",
282 .type = FCU_FAN_RPM,
283 .id = FCU_FAN_ABSENT_ID,
284 },
285};
286
287/*
288 * i2c_driver structure to attach to the host i2c controller
289 */
290
291static int therm_pm72_attach(struct i2c_adapter *adapter);
292static int therm_pm72_detach(struct i2c_adapter *adapter);
293
294static struct i2c_driver therm_pm72_driver =
295{
Laurent Riffarda33ca232005-11-26 20:40:34 +0100296 .driver = {
Laurent Riffarda33ca232005-11-26 20:40:34 +0100297 .name = "therm_pm72",
298 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .attach_adapter = therm_pm72_attach,
300 .detach_adapter = therm_pm72_detach,
301};
302
303/*
304 * Utility function to create an i2c_client structure and
305 * attach it to one of u3 adapters
306 */
307static struct i2c_client *attach_i2c_chip(int id, const char *name)
308{
309 struct i2c_client *clt;
310 struct i2c_adapter *adap;
311
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;
320
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700321 clt = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (clt == NULL)
323 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
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);
329
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;
334 }
335 return clt;
336}
337
338/*
339 * Utility function to get rid of the i2c_client structure
340 * (will also detach from the adapter hopepfully)
341 */
342static void detach_i2c_chip(struct i2c_client *clt)
343{
344 i2c_detach_client(clt);
345 kfree(clt);
346}
347
348/*
349 * Here are the i2c chip access wrappers
350 */
351
352static void initialize_adc(struct cpu_pid_state *state)
353{
354 int rc;
355 u8 buf[2];
356
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
361 */
362
363 /* Clear Config2 */
364 buf[0] = 5;
365 buf[1] = 0;
366 i2c_master_send(state->monitor, buf, 2);
367
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);
381 }
382 }
383 if (rc <= 0)
384 printk(KERN_ERR "therm_pm72: Error reading ADC config"
385 " register !\n");
386}
387
388static int read_smon_adc(struct cpu_pid_state *state, int chan)
389{
390 int rc, data, tries = 0;
391 u8 buf[2];
392
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;
418 }
419 msleep(10);
420 }
421}
422
423static int read_lm87_reg(struct i2c_client * chip, int reg)
424{
425 int rc, tries = 0;
426 u8 buf;
427
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;
443 }
444 msleep(10);
445 }
446}
447
448static int fan_read_reg(int reg, unsigned char *buf, int nb)
449{
450 int tries, nr, nw;
451
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;
460 }
461 if (nw <= 0) {
462 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
463 return -EIO;
464 }
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;
472 }
473 if (nr <= 0)
474 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
475 return nr;
476}
477
478static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
479{
480 int tries, nw;
481 unsigned char buf[16];
482
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;
493 }
494 if (nw < 0)
495 printk(KERN_ERR "Failure writing to FCU: %d", nw);
496 return nw;
497}
498
499static int start_fcu(void)
500{
501 unsigned char buf = 0xff;
502 int rc;
503
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;
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000510 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);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 0;
518}
519
520static int set_rpm_fan(int fan_index, int rpm)
521{
522 unsigned char buf[2];
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000523 int rc, id, min, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
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;
530
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000531 min = 2400 >> fcu_rpm_shift;
532 max = 56000 >> fcu_rpm_shift;
533
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
541 if (rc < 0)
542 return -EIO;
543 return 0;
544}
545
546static int get_rpm_fan(int fan_index, int programmed)
547{
548 unsigned char failure;
549 unsigned char active;
550 unsigned char buf[2];
551 int rc, id, reg_base;
552
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;
558
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;
569
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;
575
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000576 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579static int set_pwm_fan(int fan_index, int pwm)
580{
581 unsigned char buf[2];
582 int rc, id;
583
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;
589
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;
600}
601
602static int get_pwm_fan(int fan_index)
603{
604 unsigned char failure;
605 unsigned char active;
606 unsigned char buf[2];
607 int rc, id;
608
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;
614
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;
625
626 /* Programmed value or real current speed */
627 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
628 if (rc != 1)
629 return -EIO;
630
631 return (buf[0] * 1000) / 2559;
632}
633
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000634static void tickle_fcu(void)
635{
636 int pwm;
637
638 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
639
640 DBG("FCU Tickle, slots fan is: %d\n", pwm);
641 if (pwm < 0)
642 pwm = 100;
643
644 if (!rackmac) {
645 pwm = SLOTS_FAN_DEFAULT_PWM;
646 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
647 pwm = SLOTS_PID_OUTPUT_MIN;
648
649 /* That is hopefully enough to make the FCU happy */
650 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
651}
652
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/*
655 * Utility routine to read the CPU calibration EEPROM data
656 * from the device-tree
657 */
658static int read_eeprom(int cpu, struct mpu_data *out)
659{
660 struct device_node *np;
661 char nodename[64];
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000662 const u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int len;
664
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
668 */
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) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100672 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -ENODEV;
674 }
Stephen Rothwell01b27262007-04-27 13:41:15 +1000675 data = of_get_property(np, "cpuid", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (data == NULL) {
Adrian Bunk943ffb52006-01-10 00:10:13 +0100677 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 of_node_put(np);
679 return -ENODEV;
680 }
681 memcpy(out, data, sizeof(struct mpu_data));
682 of_node_put(np);
683
684 return 0;
685}
686
687static void fetch_cpu_pumps_minmax(void)
688{
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];
693
694 /* Try to fetch pumps min/max infos from eeprom */
695
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]);
700 }
701 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
702 pump_min = max(pump_min, tmp[2]);
703 pump_max = min(pump_max, tmp[3]);
704 }
705
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)
709 */
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;
713 }
714
715 state0->pump_min = state1->pump_min = pump_min;
716 state0->pump_max = state1->pump_max = pump_max;
717}
718
719/*
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...
723 *
724 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
725 * the input twice... I accept patches :)
726 */
727#define BUILD_SHOW_FUNC_FIX(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400728static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{ \
730 ssize_t r; \
731 down(&driver_lock); \
732 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
733 up(&driver_lock); \
734 return r; \
735}
736#define BUILD_SHOW_FUNC_INT(name, data) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400737static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{ \
739 return sprintf(buf, "%d", data); \
740}
741
742BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
743BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
744BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
745BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
746BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
747
748BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
749BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
750BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
751BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
752BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
753
754BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
755BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
756
757BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
758BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
759
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000760BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
761BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
764
765static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
766static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
767static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
768static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
769static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
770
771static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
772static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
773static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
774static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
775static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
776
777static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
778static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
779
780static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
781static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
782
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +1000783static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
784static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
787
788/*
789 * CPUs fans control loop
790 */
791
792static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
793{
794 s32 ltemp, volts, amps;
795 int index, rc = 0;
796
797 /* Default (in case of error) */
798 *temp = state->cur_temp;
799 *power = state->cur_power;
800
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;
807
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
813 */
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);
818 }
819
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
830 */
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)));
837 }
838
839 /*
840 * Read voltage & current and calculate power
841 */
842 volts = read_smon_adc(state, 3);
843 amps = read_smon_adc(state, 4);
844
845 /* Scale voltage and current raw sensor values according to fixed scales
846 * obtained in Darwin and calculate power from I and V
847 */
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;
854
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));
858
859 return 0;
860}
861
862static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
863{
864 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
865 s64 integ_p, deriv_p, prop_p, sum;
866 int i;
867
868 /* Calculate power target value (could be done once for all)
869 * and convert to a 16.16 fp number
870 */
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));
874
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;
881
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;
888 }
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;
892 }
893 state->first = 0;
894 }
895
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);
903
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
908 *
909 * input target is mpu.ttarget, input max is mpu.tmax
910 */
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);
919
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;
928
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;
934
935 /* Scale sum */
936 sum >>= 36;
937
938 DBG(" sum: %d\n", (int)sum);
939 state->rpm += (s32)sum;
940}
941
942static void do_monitor_cpu_combined(void)
943{
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;
949
950 rc = do_read_one_cpu_values(state0, &temp0, &power0);
951 if (rc < 0) {
952 /* XXX What do we do now ? */
953 }
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 ? */
958 }
959 if (state1->overtemp)
960 state0->overtemp++;
961
962 temp_combi = max(temp0, temp1);
963 power_combi = max(power0, power1);
964
965 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
966 * full blown immediately and try to trigger a shutdown
967 */
968 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
969 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
970 temp_combi >> 16);
Benjamin Herrenschmidtf12f4d92006-01-02 13:04:44 +1100971 state0->overtemp += CPU_MAX_OVERTEMP / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } 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;
Benjamin Herrenschmidt6ee7fb72005-12-19 11:24:53 +1100981 pump = state0->pump_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto do_set_fans;
983 }
984
985 /* Do the PID */
986 do_cpu_pid(state0, temp_combi, power_combi);
987
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);
991
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;
997
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);
1003
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;
1008
1009 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
1010 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
1011
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...
1016 */
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);
1021
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);
1026}
1027
1028static void do_monitor_cpu_split(struct cpu_pid_state *state)
1029{
1030 s32 temp, power;
1031 int rc, intake;
1032
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 ? */
1037 }
1038
1039 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1040 * full blown immediately and try to trigger a shutdown
1041 */
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);
Benjamin Herrenschmidtf12f4d92006-01-02 13:04:44 +11001046 state->overtemp += CPU_MAX_OVERTEMP / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 } 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;
1057 }
1058
1059 /* Do the PID */
1060 do_cpu_pid(state, temp, power);
1061
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);
1065
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;
1071
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);
1075
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...
1080 */
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);
1087 }
1088}
1089
1090static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1091{
1092 s32 temp, power, fan_min;
1093 int rc;
1094
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 ? */
1099 }
1100
1101 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
1102 * full blown immediately and try to trigger a shutdown
1103 */
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);
Benjamin Herrenschmidtf12f4d92006-01-02 13:04:44 +11001108 state->overtemp = CPU_MAX_OVERTEMP / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 } 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;
1118 }
1119
1120 /* Do the PID */
1121 do_cpu_pid(state, temp, power);
1122
1123 /* Check clamp from dimms */
1124 fan_min = dimm_output_clamp;
1125 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1126
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001127 DBG(" CPU min mpu = %d, min dimm = %d\n",
1128 state->mpu.rminn_intake_fan, dimm_output_clamp);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 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;
1133
1134 do_set_fans:
1135 DBG("** CPU %d RPM: %d overtemp: %d\n",
1136 state->index, (int)state->rpm, state->overtemp);
1137
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...
1142 */
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);
1151 }
1152}
1153
1154/*
1155 * Initialize the state structure for one CPU control loop
1156 */
1157static int init_cpu_state(struct cpu_pid_state *state, int index)
1158{
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;
1164
1165
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;
1172
1173 if (read_eeprom(index, &state->mpu))
1174 goto fail;
1175
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;
1180 }
1181 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1182
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);
1195 }
1196
1197 return 0;
1198 fail:
1199 if (state->monitor)
1200 detach_i2c_chip(state->monitor);
1201 state->monitor = NULL;
1202
1203 return -ENODEV;
1204}
1205
1206/*
1207 * Dispose of the state data for one CPU control loop
1208 */
1209static void dispose_cpu_state(struct cpu_pid_state *state)
1210{
1211 if (state->monitor == NULL)
1212 return;
1213
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);
1226 }
1227
1228 detach_i2c_chip(state->monitor);
1229 state->monitor = NULL;
1230}
1231
1232/*
1233 * Motherboard backside & U3 heatsink fan control loop
1234 */
1235static void do_monitor_backside(struct backside_pid_state *state)
1236{
1237 s32 temp, integral, derivative, fan_min;
1238 s64 integ_p, deriv_p, prop_p, sum;
1239 int i, rc;
1240
1241 if (--state->ticks != 0)
1242 return;
1243 state->ticks = backside_params.interval;
1244
1245 DBG("backside:\n");
1246
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);
1255
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));
1261
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;
1266
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;
1275 }
1276 state->first = 0;
1277 }
1278
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;
1289
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;
1298
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;
1303
1304 /* Scale sum */
1305 sum >>= 36;
1306
1307 DBG(" sum: %d\n", (int)sum);
1308 if (backside_params.additive)
1309 state->pwm += (s32)sum;
1310 else
1311 state->pwm = sum;
1312
1313 /* Check for clamp */
1314 fan_min = (dimm_output_clamp * 100) / 14000;
1315 fan_min = max(fan_min, backside_params.output_min);
1316
1317 state->pwm = max(state->pwm, fan_min);
1318 state->pwm = min(state->pwm, backside_params.output_max);
1319
1320 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1321 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1322}
1323
1324/*
1325 * Initialize the state structure for the backside fan control loop
1326 */
1327static int init_backside_state(struct backside_pid_state *state)
1328{
1329 struct device_node *u3;
1330 int u3h = 1; /* conservative by default */
1331
1332 /*
1333 * There are different PID params for machines with U3 and machines
1334 * with U3H, pick the right ones now
1335 */
1336 u3 = of_find_node_by_path("/u3@0,f8000000");
1337 if (u3 != NULL) {
Stephen Rothwell01b27262007-04-27 13:41:15 +10001338 const u32 *vers = of_get_property(u3, "device-rev", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (vers)
1340 if (((*vers) & 0x3f) < 0x34)
1341 u3h = 0;
1342 of_node_put(u3);
1343 }
1344
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;
1372 }
1373
1374 state->ticks = 1;
1375 state->first = 1;
1376 state->pwm = 50;
1377
1378 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1379 if (state->monitor == NULL)
1380 return -ENODEV;
1381
1382 device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1383 device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1384
1385 return 0;
1386}
1387
1388/*
1389 * Dispose of the state data for the backside control loop
1390 */
1391static void dispose_backside_state(struct backside_pid_state *state)
1392{
1393 if (state->monitor == NULL)
1394 return;
1395
1396 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1397 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1398
1399 detach_i2c_chip(state->monitor);
1400 state->monitor = NULL;
1401}
1402
1403/*
1404 * Drives bay fan control loop
1405 */
1406static void do_monitor_drives(struct drives_pid_state *state)
1407{
1408 s32 temp, integral, derivative;
1409 s64 integ_p, deriv_p, prop_p, sum;
1410 int i, rc;
1411
1412 if (--state->ticks != 0)
1413 return;
1414 state->ticks = DRIVES_PID_INTERVAL;
1415
1416 DBG("drives:\n");
1417
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);
1426
1427 /* Get some sensor readings */
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001428 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1429 DS1775_TEMP)) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 state->last_temp = temp;
1431 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1432 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1433
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;
1438
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;
1447 }
1448 state->first = 0;
1449 }
1450
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;
1461
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;
1470
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;
1475
1476 /* Scale sum */
1477 sum >>= 36;
1478
1479 DBG(" sum: %d\n", (int)sum);
1480 state->rpm += (s32)sum;
1481
1482 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1483 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1484
1485 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1486 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1487}
1488
1489/*
1490 * Initialize the state structure for the drives bay fan control loop
1491 */
1492static int init_drives_state(struct drives_pid_state *state)
1493{
1494 state->ticks = 1;
1495 state->first = 1;
1496 state->rpm = 1000;
1497
1498 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1499 if (state->monitor == NULL)
1500 return -ENODEV;
1501
1502 device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1503 device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1504
1505 return 0;
1506}
1507
1508/*
1509 * Dispose of the state data for the drives control loop
1510 */
1511static void dispose_drives_state(struct drives_pid_state *state)
1512{
1513 if (state->monitor == NULL)
1514 return;
1515
1516 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1517 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1518
1519 detach_i2c_chip(state->monitor);
1520 state->monitor = NULL;
1521}
1522
1523/*
1524 * DIMMs temp control loop
1525 */
1526static void do_monitor_dimms(struct dimm_pid_state *state)
1527{
1528 s32 temp, integral, derivative, fan_min;
1529 s64 integ_p, deriv_p, prop_p, sum;
1530 int i;
1531
1532 if (--state->ticks != 0)
1533 return;
1534 state->ticks = DIMM_PID_INTERVAL;
1535
1536 DBG("DIMM:\n");
1537
1538 DBG(" current value: %d\n", state->output);
1539
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));
1547
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;
1552
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;
1561 }
1562 state->first = 0;
1563 }
1564
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;
1575
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;
1584
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;
1589
1590 /* Scale sum */
1591 sum >>= 36;
1592
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;
1598
1599 DBG("** DIMM clamp value: %d\n", (int)state->output);
1600
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);
1608 }
1609}
1610
1611/*
1612 * Initialize the state structure for the DIMM temp control loop
1613 */
1614static int init_dimms_state(struct dimm_pid_state *state)
1615{
1616 state->ticks = 1;
1617 state->first = 1;
1618 state->output = 4000;
1619
1620 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1621 if (state->monitor == NULL)
1622 return -ENODEV;
1623
1624 device_create_file(&of_dev->dev, &dev_attr_dimms_temperature);
1625
1626 return 0;
1627}
1628
1629/*
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001630 * Dispose of the state data for the DIMM control loop
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
1632static void dispose_dimms_state(struct dimm_pid_state *state)
1633{
1634 if (state->monitor == NULL)
1635 return;
1636
1637 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1638
1639 detach_i2c_chip(state->monitor);
1640 state->monitor = NULL;
1641}
1642
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001643/*
1644 * Slots fan control loop
1645 */
1646static void do_monitor_slots(struct slots_pid_state *state)
1647{
1648 s32 temp, integral, derivative;
1649 s64 integ_p, deriv_p, prop_p, sum;
1650 int i, rc;
1651
1652 if (--state->ticks != 0)
1653 return;
1654 state->ticks = SLOTS_PID_INTERVAL;
1655
1656 DBG("slots:\n");
1657
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);
1666
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));
1673
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;
1678
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;
1687 }
1688 state->first = 0;
1689 }
1690
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;
1701
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;
1710
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;
1715
1716 /* Scale sum */
1717 sum >>= 36;
1718
1719 DBG(" sum: %d\n", (int)sum);
1720 state->pwm = (s32)sum;
1721
1722 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
1723 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
1724
1725 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
1726 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
1727}
1728
1729/*
1730 * Initialize the state structure for the slots bay fan control loop
1731 */
1732static int init_slots_state(struct slots_pid_state *state)
1733{
1734 state->ticks = 1;
1735 state->first = 1;
1736 state->pwm = 50;
1737
1738 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
1739 if (state->monitor == NULL)
1740 return -ENODEV;
1741
1742 device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
1743 device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1744
1745 return 0;
1746}
1747
1748/*
1749 * Dispose of the state data for the slots control loop
1750 */
1751static void dispose_slots_state(struct slots_pid_state *state)
1752{
1753 if (state->monitor == NULL)
1754 return;
1755
1756 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
1757 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
1758
1759 detach_i2c_chip(state->monitor);
1760 state->monitor = NULL;
1761}
1762
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764static int call_critical_overtemp(void)
1765{
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 };
1771
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -07001772 return call_usermodehelper(critical_overtemp_path,
1773 argv, envp, UMH_WAIT_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776
1777/*
1778 * Here's the kernel thread that calls the various control loops
1779 */
1780static int main_control_loop(void *x)
1781{
1782 daemonize("kfand");
1783
1784 DBG("main_control_loop started\n");
1785
1786 down(&driver_lock);
1787
1788 if (start_fcu() < 0) {
1789 printk(KERN_ERR "kfand: failed to start FCU\n");
1790 up(&driver_lock);
1791 goto out;
1792 }
1793
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001794 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /* Initialize ADCs */
1799 initialize_adc(&cpu_state[0]);
1800 if (cpu_state[1].monitor != NULL)
1801 initialize_adc(&cpu_state[1]);
1802
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001803 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 up(&driver_lock);
1806
1807 while (state == state_attached) {
1808 unsigned long elapsed, start;
1809
1810 start = jiffies;
1811
1812 down(&driver_lock);
1813
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001814 /* Tickle the FCU just in case */
1815 if (--fcu_tickle_ticks < 0) {
1816 fcu_tickle_ticks = FCU_TICKLE_TICKS;
1817 tickle_fcu();
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /* First, we always calculate the new DIMMs state on an Xserve */
1821 if (rackmac)
1822 do_monitor_dimms(&dimms_state);
1823
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
1837 }
1838 /* Then, the rest */
1839 do_monitor_backside(&backside_state);
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001840 if (rackmac)
1841 do_monitor_slots(&slots_state);
1842 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 do_monitor_drives(&drives_state);
1844 up(&driver_lock);
1845
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();
1853 }
1854 }
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();
1860 }
1861
1862 // FIXME: Deal with signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 elapsed = jiffies - start;
1864 if (elapsed < HZ)
Nishanth Aravamudan12621a12005-11-07 01:01:17 -08001865 schedule_timeout_interruptible(HZ - elapsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
1868 out:
1869 DBG("main_control_loop ended\n");
1870
1871 ctrl_task = 0;
1872 complete_and_exit(&ctrl_complete, 0);
1873}
1874
1875/*
1876 * Dispose the control loops when tearing down
1877 */
1878static void dispose_control_loops(void)
1879{
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);
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001884 dispose_slots_state(&slots_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 dispose_dimms_state(&dimms_state);
1886}
1887
1888/*
1889 * Create the control loops. U3-0 i2c bus is up, so we can now
1890 * get to the various sensors
1891 */
1892static int create_control_loops(void)
1893{
1894 struct device_node *np;
1895
1896 /* Count CPUs from the device-tree, we don't care how many are
1897 * actually used by Linux
1898 */
1899 cpu_count = 0;
1900 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1901 cpu_count++;
1902
1903 DBG("counted %d CPUs in the device-tree\n", cpu_count);
1904
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
1908 */
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;
1919
1920 /* Create control loops for everything. If any fail, everything
1921 * fails
1922 */
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();
1927
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;
Benjamin Herrenschmidt861fa772006-07-06 18:03:06 +10001934 if (rackmac && init_slots_state(&slots_state))
1935 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!rackmac && init_drives_state(&drives_state))
1937 goto fail;
1938
1939 DBG("all control loops up !\n");
1940
1941 return 0;
1942
1943 fail:
1944 DBG("failure creating control loops, disposing\n");
1945
1946 dispose_control_loops();
1947
1948 return -ENODEV;
1949}
1950
1951/*
1952 * Start the control loops after everything is up, that is create
1953 * the thread that will make them run
1954 */
1955static void start_control_loops(void)
1956{
1957 init_completion(&ctrl_complete);
1958
1959 ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL);
1960}
1961
1962/*
1963 * Stop the control loops when tearing down
1964 */
1965static void stop_control_loops(void)
1966{
1967 if (ctrl_task != 0)
1968 wait_for_completion(&ctrl_complete);
1969}
1970
1971/*
1972 * Attach to the i2c FCU after detecting U3-1 bus
1973 */
1974static int attach_fcu(void)
1975{
1976 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1977 if (fcu == NULL)
1978 return -ENODEV;
1979
1980 DBG("FCU attached\n");
1981
1982 return 0;
1983}
1984
1985/*
1986 * Detach from the i2c FCU when tearing down
1987 */
1988static void detach_fcu(void)
1989{
1990 if (fcu)
1991 detach_i2c_chip(fcu);
1992 fcu = NULL;
1993}
1994
1995/*
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
1999 */
2000static int therm_pm72_attach(struct i2c_adapter *adapter)
2001{
2002 down(&driver_lock);
2003
2004 /* Check state */
2005 if (state == state_detached)
2006 state = state_attaching;
2007 if (state != state_attaching) {
2008 up(&driver_lock);
2009 return 0;
2010 }
2011
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;
2030 }
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();
2036 }
2037 up(&driver_lock);
2038
2039 return 0;
2040}
2041
2042/*
2043 * Called on every adapter when the driver or the i2c controller
2044 * is going away.
2045 */
2046static int therm_pm72_detach(struct i2c_adapter *adapter)
2047{
2048 down(&driver_lock);
2049
2050 if (state != state_detached)
2051 state = state_detaching;
2052
2053 /* Stop control loops if any */
2054 DBG("stopping control loops\n");
2055 up(&driver_lock);
2056 stop_control_loops();
2057 down(&driver_lock);
2058
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;
2063 }
2064
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;
2069 }
2070 if (u3_0 == NULL && u3_1 == NULL)
2071 state = state_detached;
2072
2073 up(&driver_lock);
2074
2075 return 0;
2076}
2077
2078static int fan_check_loc_match(const char *loc, int fan)
2079{
2080 char tmp[64];
2081 char *c, *e;
2082
2083 strlcpy(tmp, fcu_fans[fan].loc, 64);
2084
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;
2095 }
2096 return 0;
2097}
2098
2099static void fcu_lookup_fans(struct device_node *fcu_node)
2100{
2101 struct device_node *np = NULL;
2102 int i;
2103
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
2108 */
2109
2110 DBG("Looking up FCU controls in device-tree...\n");
2111
2112 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
2113 int type = -1;
Jeremy Kerr018a3d12006-07-12 15:40:29 +10002114 const char *loc;
2115 const u32 *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 DBG(" control: %s, type: %s\n", np->name, np->type);
2118
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;
2129
2130 /* Lookup for a matching location */
Stephen Rothwell01b27262007-04-27 13:41:15 +10002131 loc = of_get_property(np, "location", NULL);
2132 reg = of_get_property(np, "reg", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (loc == NULL || reg == NULL)
2134 continue;
2135 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
2136
2137 for (i = 0; i < FCU_FAN_COUNT; i++) {
2138 int fan_id;
2139
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;
2148 }
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;
2157 }
2158 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
2159 fcu_fans[i].id = fan_id;
2160 }
2161 }
2162
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);
2171 }
2172}
2173
Jeff Mahoney5e655772005-07-06 15:44:41 -04002174static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 state = state_detached;
2177
2178 /* Lookup the fans in the device tree */
2179 fcu_lookup_fans(dev->node);
2180
2181 /* Add the driver */
Arthur Othienoc9662b42006-01-06 00:11:29 -08002182 return i2c_add_driver(&therm_pm72_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185static int fcu_of_remove(struct of_device* dev)
2186{
2187 i2c_del_driver(&therm_pm72_driver);
2188
2189 return 0;
2190}
2191
Jeff Mahoney5e655772005-07-06 15:44:41 -04002192static struct of_device_id fcu_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 .type = "fcu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 },
2197 {},
2198};
2199
2200static struct of_platform_driver fcu_of_platform_driver =
2201{
2202 .name = "temperature",
Jeff Mahoney5e655772005-07-06 15:44:41 -04002203 .match_table = fcu_match,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .probe = fcu_of_probe,
2205 .remove = fcu_of_remove
2206};
2207
2208/*
2209 * Check machine type, attach to i2c controller
2210 */
2211static int __init therm_pm72_init(void)
2212{
2213 struct device_node *np;
2214
2215 rackmac = machine_is_compatible("RackMac3,1");
2216
2217 if (!machine_is_compatible("PowerMac7,2") &&
2218 !machine_is_compatible("PowerMac7,3") &&
2219 !rackmac)
2220 return -ENODEV;
2221
2222 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
2223
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;
2231 }
2232 }
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -07002233 of_dev = of_platform_device_create(np, "temperature", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (of_dev == NULL) {
2235 printk(KERN_ERR "Can't register FCU platform device !\n");
2236 return -ENODEV;
2237 }
2238
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +11002239 of_register_platform_driver(&fcu_of_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 return 0;
2242}
2243
2244static void __exit therm_pm72_exit(void)
2245{
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +11002246 of_unregister_platform_driver(&fcu_of_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 if (of_dev)
2249 of_device_unregister(of_dev);
2250}
2251
2252module_init(therm_pm72_init);
2253module_exit(therm_pm72_exit);
2254
2255MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
2256MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
2257MODULE_LICENSE("GPL");
2258