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