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