VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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  *
50  * History:
51  *
52  *  Nov. 13, 2003 : 0.5
53  *      - First release
54  *
55  *  Nov. 14, 2003 : 0.6
56  *      - Read fan speed from FCU, low level fan routines now deal
57  *        with errors & check fan status, though higher level don't
58  *        do much.
59  *      - Move a bunch of definitions to .h file
60  *
61  *  Nov. 18, 2003 : 0.7
62  *      - Fix build on ppc64 kernel
63  *      - Move back statics definitions to .c file
64  *      - Avoid calling schedule_timeout with a negative number
65  *
66  *  Dec. 18, 2003 : 0.8
67  *      - Fix typo when reading back fan speed on 2 CPU machines
68  *
69  *  Mar. 11, 2004 : 0.9
70  *      - Rework code accessing the ADC chips, make it more robust and
71  *        closer to the chip spec. Also make sure it is configured properly,
72  *        I've seen yet unexplained cases where on startup, I would have stale
73  *        values in the configuration register
74  *      - Switch back to use of target fan speed for PID, thus lowering
75  *        pressure on i2c
76  */
77
78 #include <linux/config.h>
79 #include <linux/types.h>
80 #include <linux/module.h>
81 #include <linux/errno.h>
82 #include <linux/kernel.h>
83 #include <linux/delay.h>
84 #include <linux/sched.h>
85 #include <linux/i2c.h>
86 #include <linux/slab.h>
87 #include <linux/init.h>
88 #include <linux/spinlock.h>
89 #include <linux/smp_lock.h>
90 #include <linux/wait.h>
91 #include <linux/reboot.h>
92 #include <linux/kmod.h>
93 #include <linux/i2c.h>
94 #include <linux/i2c-dev.h>
95 #include <asm/prom.h>
96 #include <asm/machdep.h>
97 #include <asm/io.h>
98 #include <asm/system.h>
99 #include <asm/sections.h>
100 #include <asm/of_device.h>
101
102 #include "therm_pm72.h"
103
104 #define VERSION "0.9"
105
106 #undef DEBUG
107
108 #ifdef DEBUG
109 #define DBG(args...)    printk(args)
110 #else
111 #define DBG(args...)    do { } while(0)
112 #endif
113
114
115 /*
116  * Driver statics
117  */
118
119 static struct of_device *               of_dev;
120 static struct i2c_adapter *             u3_0;
121 static struct i2c_adapter *             u3_1;
122 static struct i2c_client *              fcu;
123 static struct cpu_pid_state             cpu_state[2];
124 static struct backside_pid_state        backside_state;
125 static struct drives_pid_state          drives_state;
126 static int                              state;
127 static int                              cpu_count;
128 static pid_t                            ctrl_task;
129 static struct completion                ctrl_complete;
130 static int                              critical_state;
131 static DECLARE_MUTEX(driver_lock);
132
133 /*
134  * i2c_driver structure to attach to the host i2c controller
135  */
136
137 static int therm_pm72_attach(struct i2c_adapter *adapter);
138 static int therm_pm72_detach(struct i2c_adapter *adapter);
139
140 static struct i2c_driver therm_pm72_driver =
141 {
142         .name           = "therm_pm72",
143         .id             = 0xDEADBEEF,
144         .flags          = I2C_DF_NOTIFY,
145         .attach_adapter = therm_pm72_attach,
146         .detach_adapter = therm_pm72_detach,
147 };
148
149 /*
150  * Utility function to create an i2c_client structure and
151  * attach it to one of u3 adapters
152  */
153 static struct i2c_client *attach_i2c_chip(int id, const char *name)
154 {
155         struct i2c_client *clt;
156         struct i2c_adapter *adap;
157
158         if (id & 0x100)
159                 adap = u3_1;
160         else
161                 adap = u3_0;
162         if (adap == NULL)
163                 return NULL;
164
165         clt = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
166         if (clt == NULL)
167                 return NULL;
168         memset(clt, 0, sizeof(struct i2c_client));
169
170         clt->addr = (id >> 1) & 0x7f;
171         clt->adapter = adap;
172         clt->driver = &therm_pm72_driver;
173         clt->id = 0xDEADBEEF;
174         strncpy(clt->name, name, I2C_NAME_SIZE-1);
175
176         if (i2c_attach_client(clt)) {
177                 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
178                 kfree(clt);
179                 return NULL;
180         }
181         return clt;
182 }
183
184 /*
185  * Utility function to get rid of the i2c_client structure
186  * (will also detach from the adapter hopepfully)
187  */
188 static void detach_i2c_chip(struct i2c_client *clt)
189 {
190         i2c_detach_client(clt);
191         kfree(clt);
192 }
193
194 /*
195  * Here are the i2c chip access wrappers
196  */
197
198 static void initialize_adc(struct cpu_pid_state *state)
199 {
200         int rc;
201         u8 buf[2];
202
203         /* Read ADC the configuration register and cache it. We
204          * also make sure Config2 contains proper values, I've seen
205          * cases where we got stale grabage in there, thus preventing
206          * proper reading of conv. values
207          */
208
209         /* Clear Config2 */
210         buf[0] = 5;
211         buf[1] = 0;
212         i2c_master_send(state->monitor, buf, 2);
213
214         /* Read & cache Config1 */
215         buf[0] = 1;
216         rc = i2c_master_send(state->monitor, buf, 1);
217         if (rc > 0) {
218                 rc = i2c_master_recv(state->monitor, buf, 1);
219                 if (rc > 0) {
220                         state->adc_config = buf[0];
221                         DBG("ADC config reg: %02x\n", state->adc_config);
222                         /* Disable shutdown mode */
223                         state->adc_config &= 0xfe;
224                         buf[0] = 1;
225                         buf[1] = state->adc_config;
226                         rc = i2c_master_send(state->monitor, buf, 2);
227                 }
228         }
229         if (rc <= 0)
230                 printk(KERN_ERR "therm_pm72: Error reading ADC config"
231                        " register !\n");
232 }
233
234 static int read_smon_adc(struct cpu_pid_state *state, int chan)
235 {
236         int rc, data, tries = 0;
237         u8 buf[2];
238
239         for (;;) {
240                 /* Set channel */
241                 buf[0] = 1;
242                 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
243                 rc = i2c_master_send(state->monitor, buf, 2);
244                 if (rc <= 0)
245                         goto error;
246                 /* Wait for convertion */
247                 msleep(1);
248                 /* Switch to data register */
249                 buf[0] = 4;
250                 rc = i2c_master_send(state->monitor, buf, 1);
251                 if (rc <= 0)
252                         goto error;
253                 /* Read result */
254                 rc = i2c_master_recv(state->monitor, buf, 2);
255                 if (rc < 0)
256                         goto error;
257                 data = ((u16)buf[0]) << 8 | (u16)buf[1];
258                 return data >> 6;
259         error:
260                 DBG("Error reading ADC, retrying...\n");
261                 if (++tries > 10) {
262                         printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
263                         return -1;
264                 }
265                 msleep(10);
266         }
267 }
268
269 static int fan_read_reg(int reg, unsigned char *buf, int nb)
270 {
271         int tries, nr, nw;
272
273         buf[0] = reg;
274         tries = 0;
275         for (;;) {
276                 nw = i2c_master_send(fcu, buf, 1);
277                 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
278                         break;
279                 msleep(10);
280                 ++tries;
281         }
282         if (nw <= 0) {
283                 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
284                 return -EIO;
285         }
286         tries = 0;
287         for (;;) {
288                 nr = i2c_master_recv(fcu, buf, nb);
289                 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
290                         break;
291                 msleep(10);
292                 ++tries;
293         }
294         if (nr <= 0)
295                 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
296         return nr;
297 }
298
299 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
300 {
301         int tries, nw;
302         unsigned char buf[16];
303
304         buf[0] = reg;
305         memcpy(buf+1, ptr, nb);
306         ++nb;
307         tries = 0;
308         for (;;) {
309                 nw = i2c_master_send(fcu, buf, nb);
310                 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
311                         break;
312                 msleep(10);
313                 ++tries;
314         }
315         if (nw < 0)
316                 printk(KERN_ERR "Failure writing to FCU: %d", nw);
317         return nw;
318 }
319
320 static int start_fcu(void)
321 {
322         unsigned char buf = 0xff;
323         int rc;
324
325         rc = fan_write_reg(0xe, &buf, 1);
326         if (rc < 0)
327                 return -EIO;
328         rc = fan_write_reg(0x2e, &buf, 1);
329         if (rc < 0)
330                 return -EIO;
331         return 0;
332 }
333
334 static int set_rpm_fan(int fan, int rpm)
335 {
336         unsigned char buf[2];
337         int rc;
338
339         if (rpm < 300)
340                 rpm = 300;
341         else if (rpm > 8191)
342                 rpm = 8191;
343         buf[0] = rpm >> 5;
344         buf[1] = rpm << 3;
345         rc = fan_write_reg(0x10 + (fan * 2), buf, 2);
346         if (rc < 0)
347                 return -EIO;
348         return 0;
349 }
350
351 static int get_rpm_fan(int fan, int programmed)
352 {
353         unsigned char failure;
354         unsigned char active;
355         unsigned char buf[2];
356         int rc, reg_base;
357
358         rc = fan_read_reg(0xb, &failure, 1);
359         if (rc != 1)
360                 return -EIO;
361         if ((failure & (1 << fan)) != 0)
362                 return -EFAULT;
363         rc = fan_read_reg(0xd, &active, 1);
364         if (rc != 1)
365                 return -EIO;
366         if ((active & (1 << fan)) == 0)
367                 return -ENXIO;
368
369         /* Programmed value or real current speed */
370         reg_base = programmed ? 0x10 : 0x11;
371         rc = fan_read_reg(reg_base + (fan * 2), buf, 2);
372         if (rc != 2)
373                 return -EIO;
374
375         return (buf[0] << 5) | buf[1] >> 3;
376 }
377
378 static int set_pwm_fan(int fan, int pwm)
379 {
380         unsigned char buf[2];
381         int rc;
382
383         if (pwm < 10)
384                 pwm = 10;
385         else if (pwm > 100)
386                 pwm = 100;
387         pwm = (pwm * 2559) / 1000;
388         buf[0] = pwm;
389         rc = fan_write_reg(0x30 + (fan * 2), buf, 1);
390         if (rc < 0)
391                 return rc;
392         return 0;
393 }
394
395 static int get_pwm_fan(int fan)
396 {
397         unsigned char failure;
398         unsigned char active;
399         unsigned char buf[2];
400         int rc;
401
402         rc = fan_read_reg(0x2b, &failure, 1);
403         if (rc != 1)
404                 return -EIO;
405         if ((failure & (1 << fan)) != 0)
406                 return -EFAULT;
407         rc = fan_read_reg(0x2d, &active, 1);
408         if (rc != 1)
409                 return -EIO;
410         if ((active & (1 << fan)) == 0)
411                 return -ENXIO;
412
413         /* Programmed value or real current speed */
414         rc = fan_read_reg(0x30 + (fan * 2), buf, 1);
415         if (rc != 1)
416                 return -EIO;
417
418         return (buf[0] * 1000) / 2559;
419 }
420
421 /*
422  * Utility routine to read the CPU calibration EEPROM data
423  * from the device-tree
424  */
425 static int read_eeprom(int cpu, struct mpu_data *out)
426 {
427         struct device_node *np;
428         char nodename[64];
429         u8 *data;
430         int len;
431
432         /* prom.c routine for finding a node by path is a bit brain dead
433          * and requires exact @xxx unit numbers. This is a bit ugly but
434          * will work for these machines
435          */
436         sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
437         np = of_find_node_by_path(nodename);
438         if (np == NULL) {
439                 printk(KERN_ERR "therm_pm72: Failed to retreive cpuid node from device-tree\n");
440                 return -ENODEV;
441         }
442         data = (u8 *)get_property(np, "cpuid", &len);
443         if (data == NULL) {
444                 printk(KERN_ERR "therm_pm72: Failed to retreive cpuid property from device-tree\n");
445                 of_node_put(np);
446                 return -ENODEV;
447         }
448         memcpy(out, data, sizeof(struct mpu_data));
449         of_node_put(np);
450         
451         return 0;
452 }
453
454 /* 
455  * Now, unfortunately, sysfs doesn't give us a nice void * we could
456  * pass around to the attribute functions, so we don't really have
457  * choice but implement a bunch of them...
458  *
459  * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
460  * the input twice... I accept patches :)
461  */
462 #define BUILD_SHOW_FUNC_FIX(name, data)                         \
463 static ssize_t show_##name(struct device *dev, char *buf)       \
464 {                                                               \
465         ssize_t r;                                              \
466         down(&driver_lock);                                     \
467         r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data));        \
468         up(&driver_lock);                                       \
469         return r;                                               \
470 }
471 #define BUILD_SHOW_FUNC_INT(name, data)                         \
472 static ssize_t show_##name(struct device *dev, char *buf)       \
473 {                                                               \
474         return sprintf(buf, "%d", data);                        \
475 }
476
477 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
478 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
479 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
480 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
481 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
482
483 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
484 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
485 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
486 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
487 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
488
489 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
490 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
491
492 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
493 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
494
495 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
496 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
497 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
498 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
499 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
500
501 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
502 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
503 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
504 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
505 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
506
507 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
508 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
509
510 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
511 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
512
513 /*
514  * CPUs fans control loop
515  */
516 static void do_monitor_cpu(struct cpu_pid_state *state)
517 {
518         s32 temp, voltage, current_a, power, power_target;
519         s32 integral, derivative, proportional, adj_in_target, sval;
520         s64 integ_p, deriv_p, prop_p, sum; 
521         int i, intake, rc;
522
523         DBG("cpu %d:\n", state->index);
524
525         /* Read current fan status */
526         if (state->index == 0)
527                 rc = get_rpm_fan(CPUA_EXHAUST_FAN_RPM_ID, !RPM_PID_USE_ACTUAL_SPEED);
528         else
529                 rc = get_rpm_fan(CPUB_EXHAUST_FAN_RPM_ID, !RPM_PID_USE_ACTUAL_SPEED);
530         if (rc < 0) {
531                 printk(KERN_WARNING "Error %d reading CPU %d exhaust fan !\n",
532                        rc, state->index);
533                 /* XXX What do we do now ? */
534         } else
535                 state->rpm = rc;
536         DBG("  current rpm: %d\n", state->rpm);
537
538         /* Get some sensor readings and scale it */
539         temp = read_smon_adc(state, 1);
540         if (temp == -1) {
541                 state->overtemp++;
542                 return;
543         }
544         voltage = read_smon_adc(state, 3);
545         current_a = read_smon_adc(state, 4);
546
547         /* Fixup temperature according to diode calibration
548          */
549         DBG("  temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
550             temp, state->mpu.mdiode, state->mpu.bdiode);
551         temp = ((s32)temp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
552         state->last_temp = temp;
553         DBG("  temp: %d.%03d\n", FIX32TOPRINT(temp));
554
555         /* Check tmax, increment overtemp if we are there. At tmax+8, we go
556          * full blown immediately and try to trigger a shutdown
557          */
558         if (temp >= ((state->mpu.tmax + 8) << 16)) {
559                 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
560                        " (%d) !\n",
561                        state->index, temp >> 16);
562                 state->overtemp = CPU_MAX_OVERTEMP;
563         } else if (temp > (state->mpu.tmax << 16))
564                 state->overtemp++;
565         else
566                 state->overtemp = 0;
567         if (state->overtemp >= CPU_MAX_OVERTEMP)
568                 critical_state = 1;
569         if (state->overtemp > 0) {
570                 state->rpm = state->mpu.rmaxn_exhaust_fan;
571                 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
572                 goto do_set_fans;
573         }
574         
575         /* Scale other sensor values according to fixed scales
576          * obtained in Darwin and calculate power from I and V
577          */
578         state->voltage = voltage *= ADC_CPU_VOLTAGE_SCALE;
579         state->current_a = current_a *= ADC_CPU_CURRENT_SCALE;
580         power = (((u64)current_a) * ((u64)voltage)) >> 16;
581
582         /* Calculate power target value (could be done once for all)
583          * and convert to a 16.16 fp number
584          */
585         power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
586
587         DBG("  current: %d.%03d, voltage: %d.%03d\n",
588             FIX32TOPRINT(current_a), FIX32TOPRINT(voltage));
589         DBG("  power: %d.%03d W, target: %d.%03d, error: %d.%03d\n", FIX32TOPRINT(power),
590             FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
591
592         /* Store temperature and power in history array */
593         state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
594         state->temp_history[state->cur_temp] = temp;
595         state->cur_power = (state->cur_power + 1) % state->count_power;
596         state->power_history[state->cur_power] = power;
597         state->error_history[state->cur_power] = power_target - power;
598         
599         /* If first loop, fill the history table */
600         if (state->first) {
601                 for (i = 0; i < (state->count_power - 1); i++) {
602                         state->cur_power = (state->cur_power + 1) % state->count_power;
603                         state->power_history[state->cur_power] = power;
604                         state->error_history[state->cur_power] = power_target - power;
605                 }
606                 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
607                         state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
608                         state->temp_history[state->cur_temp] = temp;                    
609                 }
610                 state->first = 0;
611         }
612
613         /* Calculate the integral term normally based on the "power" values */
614         sum = 0;
615         integral = 0;
616         for (i = 0; i < state->count_power; i++)
617                 integral += state->error_history[i];
618         integral *= CPU_PID_INTERVAL;
619         DBG("  integral: %08x\n", integral);
620
621         /* Calculate the adjusted input (sense value).
622          *   G_r is 12.20
623          *   integ is 16.16
624          *   so the result is 28.36
625          *
626          * input target is mpu.ttarget, input max is mpu.tmax
627          */
628         integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
629         DBG("   integ_p: %d\n", (int)(deriv_p >> 36));
630         sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
631         adj_in_target = (state->mpu.ttarget << 16);
632         if (adj_in_target > sval)
633                 adj_in_target = sval;
634         DBG("   adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
635             state->mpu.ttarget);
636
637         /* Calculate the derivative term */
638         derivative = state->temp_history[state->cur_temp] -
639                 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
640                                     % CPU_TEMP_HISTORY_SIZE];
641         derivative /= CPU_PID_INTERVAL;
642         deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
643         DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
644         sum += deriv_p;
645
646         /* Calculate the proportional term */
647         proportional = temp - adj_in_target;
648         prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
649         DBG("   prop_p: %d\n", (int)(prop_p >> 36));
650         sum += prop_p;
651
652         /* Scale sum */
653         sum >>= 36;
654
655         DBG("   sum: %d\n", (int)sum);
656         state->rpm += (s32)sum;
657
658         if (state->rpm < state->mpu.rminn_exhaust_fan)
659                 state->rpm = state->mpu.rminn_exhaust_fan;
660         if (state->rpm > state->mpu.rmaxn_exhaust_fan)
661                 state->rpm = state->mpu.rmaxn_exhaust_fan;
662
663         intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
664         if (intake < state->mpu.rminn_intake_fan)
665                 intake = state->mpu.rminn_intake_fan;
666         if (intake > state->mpu.rmaxn_intake_fan)
667                 intake = state->mpu.rmaxn_intake_fan;
668         state->intake_rpm = intake;
669
670  do_set_fans:
671         DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
672             state->index, (int)state->rpm, intake, state->overtemp);
673
674         /* We should check for errors, shouldn't we ? But then, what
675          * do we do once the error occurs ? For FCU notified fan
676          * failures (-EFAULT) we probably want to notify userland
677          * some way...
678          */
679         if (state->index == 0) {
680                 set_rpm_fan(CPUA_INTAKE_FAN_RPM_ID, intake);
681                 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_ID, state->rpm);
682         } else {
683                 set_rpm_fan(CPUB_INTAKE_FAN_RPM_ID, intake);
684                 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_ID, state->rpm);
685         }
686 }
687
688 /*
689  * Initialize the state structure for one CPU control loop
690  */
691 static int init_cpu_state(struct cpu_pid_state *state, int index)
692 {
693         state->index = index;
694         state->first = 1;
695         state->rpm = 1000;
696         state->overtemp = 0;
697         state->adc_config = 0x00;
698
699         if (index == 0)
700                 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
701         else if (index == 1)
702                 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
703         if (state->monitor == NULL)
704                 goto fail;
705
706         if (read_eeprom(index, &state->mpu))
707                 goto fail;
708
709         state->count_power = state->mpu.tguardband;
710         if (state->count_power > CPU_POWER_HISTORY_SIZE) {
711                 printk(KERN_WARNING "Warning ! too many power history slots\n");
712                 state->count_power = CPU_POWER_HISTORY_SIZE;
713         }
714         DBG("CPU %d Using %d power history entries\n", index, state->count_power);
715
716         if (index == 0) {
717                 device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
718                 device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
719                 device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
720                 device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
721                 device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
722         } else {
723                 device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
724                 device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
725                 device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
726                 device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
727                 device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
728         }
729
730         return 0;
731  fail:
732         if (state->monitor)
733                 detach_i2c_chip(state->monitor);
734         state->monitor = NULL;
735         
736         return -ENODEV;
737 }
738
739 /*
740  * Dispose of the state data for one CPU control loop
741  */
742 static void dispose_cpu_state(struct cpu_pid_state *state)
743 {
744         if (state->monitor == NULL)
745                 return;
746
747         if (state->index == 0) {
748                 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
749                 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
750                 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
751                 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
752                 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
753         } else {
754                 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
755                 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
756                 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
757                 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
758                 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
759         }
760
761         detach_i2c_chip(state->monitor);
762         state->monitor = NULL;
763 }
764
765 /*
766  * Motherboard backside & U3 heatsink fan control loop
767  */
768 static void do_monitor_backside(struct backside_pid_state *state)
769 {
770         s32 temp, integral, derivative;
771         s64 integ_p, deriv_p, prop_p, sum; 
772         int i, rc;
773
774         if (--state->ticks != 0)
775                 return;
776         state->ticks = BACKSIDE_PID_INTERVAL;
777
778         DBG("backside:\n");
779
780         /* Check fan status */
781         rc = get_pwm_fan(BACKSIDE_FAN_PWM_ID);
782         if (rc < 0) {
783                 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
784                 /* XXX What do we do now ? */
785         } else
786                 state->pwm = rc;
787         DBG("  current pwm: %d\n", state->pwm);
788
789         /* Get some sensor readings */
790         temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
791         state->last_temp = temp;
792         DBG("  temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
793             FIX32TOPRINT(BACKSIDE_PID_INPUT_TARGET));
794
795         /* Store temperature and error in history array */
796         state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
797         state->sample_history[state->cur_sample] = temp;
798         state->error_history[state->cur_sample] = temp - BACKSIDE_PID_INPUT_TARGET;
799         
800         /* If first loop, fill the history table */
801         if (state->first) {
802                 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
803                         state->cur_sample = (state->cur_sample + 1) %
804                                 BACKSIDE_PID_HISTORY_SIZE;
805                         state->sample_history[state->cur_sample] = temp;
806                         state->error_history[state->cur_sample] =
807                                 temp - BACKSIDE_PID_INPUT_TARGET;
808                 }
809                 state->first = 0;
810         }
811
812         /* Calculate the integral term */
813         sum = 0;
814         integral = 0;
815         for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
816                 integral += state->error_history[i];
817         integral *= BACKSIDE_PID_INTERVAL;
818         DBG("  integral: %08x\n", integral);
819         integ_p = ((s64)BACKSIDE_PID_G_r) * (s64)integral;
820         DBG("   integ_p: %d\n", (int)(integ_p >> 36));
821         sum += integ_p;
822
823         /* Calculate the derivative term */
824         derivative = state->error_history[state->cur_sample] -
825                 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
826                                     % BACKSIDE_PID_HISTORY_SIZE];
827         derivative /= BACKSIDE_PID_INTERVAL;
828         deriv_p = ((s64)BACKSIDE_PID_G_d) * (s64)derivative;
829         DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
830         sum += deriv_p;
831
832         /* Calculate the proportional term */
833         prop_p = ((s64)BACKSIDE_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
834         DBG("   prop_p: %d\n", (int)(prop_p >> 36));
835         sum += prop_p;
836
837         /* Scale sum */
838         sum >>= 36;
839
840         DBG("   sum: %d\n", (int)sum);
841         state->pwm += (s32)sum;
842         if (state->pwm < BACKSIDE_PID_OUTPUT_MIN)
843                 state->pwm = BACKSIDE_PID_OUTPUT_MIN;
844         if (state->pwm > BACKSIDE_PID_OUTPUT_MAX)
845                 state->pwm = BACKSIDE_PID_OUTPUT_MAX;
846
847         DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
848         set_pwm_fan(BACKSIDE_FAN_PWM_ID, state->pwm);
849 }
850
851 /*
852  * Initialize the state structure for the backside fan control loop
853  */
854 static int init_backside_state(struct backside_pid_state *state)
855 {
856         state->ticks = 1;
857         state->first = 1;
858         state->pwm = 50;
859
860         state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
861         if (state->monitor == NULL)
862                 return -ENODEV;
863
864         device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
865         device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
866
867         return 0;
868 }
869
870 /*
871  * Dispose of the state data for the backside control loop
872  */
873 static void dispose_backside_state(struct backside_pid_state *state)
874 {
875         if (state->monitor == NULL)
876                 return;
877
878         device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
879         device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
880
881         detach_i2c_chip(state->monitor);
882         state->monitor = NULL;
883 }
884  
885 /*
886  * Drives bay fan control loop
887  */
888 static void do_monitor_drives(struct drives_pid_state *state)
889 {
890         s32 temp, integral, derivative;
891         s64 integ_p, deriv_p, prop_p, sum; 
892         int i, rc;
893
894         if (--state->ticks != 0)
895                 return;
896         state->ticks = DRIVES_PID_INTERVAL;
897
898         DBG("drives:\n");
899
900         /* Check fan status */
901         rc = get_rpm_fan(DRIVES_FAN_RPM_ID, !RPM_PID_USE_ACTUAL_SPEED);
902         if (rc < 0) {
903                 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
904                 /* XXX What do we do now ? */
905         } else
906                 state->rpm = rc;
907         DBG("  current rpm: %d\n", state->rpm);
908
909         /* Get some sensor readings */
910         temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor, DS1775_TEMP)) << 8;
911         state->last_temp = temp;
912         DBG("  temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
913             FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
914
915         /* Store temperature and error in history array */
916         state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
917         state->sample_history[state->cur_sample] = temp;
918         state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
919         
920         /* If first loop, fill the history table */
921         if (state->first) {
922                 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
923                         state->cur_sample = (state->cur_sample + 1) %
924                                 DRIVES_PID_HISTORY_SIZE;
925                         state->sample_history[state->cur_sample] = temp;
926                         state->error_history[state->cur_sample] =
927                                 temp - DRIVES_PID_INPUT_TARGET;
928                 }
929                 state->first = 0;
930         }
931
932         /* Calculate the integral term */
933         sum = 0;
934         integral = 0;
935         for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
936                 integral += state->error_history[i];
937         integral *= DRIVES_PID_INTERVAL;
938         DBG("  integral: %08x\n", integral);
939         integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
940         DBG("   integ_p: %d\n", (int)(integ_p >> 36));
941         sum += integ_p;
942
943         /* Calculate the derivative term */
944         derivative = state->error_history[state->cur_sample] -
945                 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
946                                     % DRIVES_PID_HISTORY_SIZE];
947         derivative /= DRIVES_PID_INTERVAL;
948         deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
949         DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
950         sum += deriv_p;
951
952         /* Calculate the proportional term */
953         prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
954         DBG("   prop_p: %d\n", (int)(prop_p >> 36));
955         sum += prop_p;
956
957         /* Scale sum */
958         sum >>= 36;
959
960         DBG("   sum: %d\n", (int)sum);
961         state->rpm += (s32)sum;
962         if (state->rpm < DRIVES_PID_OUTPUT_MIN)
963                 state->rpm = DRIVES_PID_OUTPUT_MIN;
964         if (state->rpm > DRIVES_PID_OUTPUT_MAX)
965                 state->rpm = DRIVES_PID_OUTPUT_MAX;
966
967         DBG("** DRIVES RPM: %d\n", (int)state->rpm);
968         set_rpm_fan(DRIVES_FAN_RPM_ID, state->rpm);
969 }
970
971 /*
972  * Initialize the state structure for the drives bay fan control loop
973  */
974 static int init_drives_state(struct drives_pid_state *state)
975 {
976         state->ticks = 1;
977         state->first = 1;
978         state->rpm = 1000;
979
980         state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
981         if (state->monitor == NULL)
982                 return -ENODEV;
983
984         device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
985         device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
986
987         return 0;
988 }
989
990 /*
991  * Dispose of the state data for the drives control loop
992  */
993 static void dispose_drives_state(struct drives_pid_state *state)
994 {
995         if (state->monitor == NULL)
996                 return;
997
998         device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
999         device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1000
1001         detach_i2c_chip(state->monitor);
1002         state->monitor = NULL;
1003 }
1004
1005 static int call_critical_overtemp(void)
1006 {
1007         char *argv[] = { critical_overtemp_path, NULL };
1008         static char *envp[] = { "HOME=/",
1009                                 "TERM=linux",
1010                                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
1011                                 NULL };
1012
1013         return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
1014 }
1015
1016
1017 /*
1018  * Here's the kernel thread that calls the various control loops
1019  */
1020 static int main_control_loop(void *x)
1021 {
1022         daemonize("kfand");
1023
1024         DBG("main_control_loop started\n");
1025
1026         down(&driver_lock);
1027
1028         if (start_fcu() < 0) {
1029                 printk(KERN_ERR "kfand: failed to start FCU\n");
1030                 up(&driver_lock);
1031                 goto out;
1032         }
1033
1034         /* Set the PCI fan once for now */
1035         set_pwm_fan(SLOTS_FAN_PWM_ID, SLOTS_FAN_DEFAULT_PWM);
1036
1037         /* Initialize ADCs */
1038         initialize_adc(&cpu_state[0]);
1039         if (cpu_state[1].monitor != NULL)
1040                 initialize_adc(&cpu_state[1]);
1041
1042         up(&driver_lock);
1043
1044         while (state == state_attached) {
1045                 unsigned long elapsed, start;
1046
1047                 start = jiffies;
1048
1049                 down(&driver_lock);
1050                 do_monitor_cpu(&cpu_state[0]);
1051                 if (cpu_state[1].monitor != NULL)
1052                         do_monitor_cpu(&cpu_state[1]);
1053                 do_monitor_backside(&backside_state);
1054                 do_monitor_drives(&drives_state);
1055                 up(&driver_lock);
1056
1057                 if (critical_state == 1) {
1058                         printk(KERN_WARNING "Temperature control detected a critical condition\n");
1059                         printk(KERN_WARNING "Attempting to shut down...\n");
1060                         if (call_critical_overtemp()) {
1061                                 printk(KERN_WARNING "Can't call %s, power off now!\n",
1062                                        critical_overtemp_path);
1063                                 machine_power_off();
1064                         }
1065                 }
1066                 if (critical_state > 0)
1067                         critical_state++;
1068                 if (critical_state > MAX_CRITICAL_STATE) {
1069                         printk(KERN_WARNING "Shutdown timed out, power off now !\n");
1070                         machine_power_off();
1071                 }
1072
1073                 // FIXME: Deal with signals
1074                 set_current_state(TASK_INTERRUPTIBLE);
1075                 elapsed = jiffies - start;
1076                 if (elapsed < HZ)
1077                         schedule_timeout(HZ - elapsed);
1078         }
1079
1080  out:
1081         DBG("main_control_loop ended\n");
1082
1083         ctrl_task = 0;
1084         complete_and_exit(&ctrl_complete, 0);
1085 }
1086
1087 /*
1088  * Dispose the control loops when tearing down
1089  */
1090 static void dispose_control_loops(void)
1091 {
1092         dispose_cpu_state(&cpu_state[0]);
1093         dispose_cpu_state(&cpu_state[1]);
1094
1095         dispose_backside_state(&backside_state);
1096         dispose_drives_state(&drives_state);
1097 }
1098
1099 /*
1100  * Create the control loops. U3-0 i2c bus is up, so we can now
1101  * get to the various sensors
1102  */
1103 static int create_control_loops(void)
1104 {
1105         struct device_node *np;
1106
1107         /* Count CPUs from the device-tree, we don't care how many are
1108          * actually used by Linux
1109          */
1110         cpu_count = 0;
1111         for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
1112                 cpu_count++;
1113
1114         DBG("counted %d CPUs in the device-tree\n", cpu_count);
1115
1116         /* Create control loops for everything. If any fail, everything
1117          * fails
1118          */
1119         if (init_cpu_state(&cpu_state[0], 0))
1120                 goto fail;
1121         if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
1122                 goto fail;
1123         if (init_backside_state(&backside_state))
1124                 goto fail;
1125         if (init_drives_state(&drives_state))
1126                 goto fail;
1127
1128         DBG("all control loops up !\n");
1129
1130         return 0;
1131         
1132  fail:
1133         DBG("failure creating control loops, disposing\n");
1134
1135         dispose_control_loops();
1136
1137         return -ENODEV;
1138 }
1139
1140 /*
1141  * Start the control loops after everything is up, that is create
1142  * the thread that will make them run
1143  */
1144 static void start_control_loops(void)
1145 {
1146         init_completion(&ctrl_complete);
1147
1148         ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL);
1149 }
1150
1151 /*
1152  * Stop the control loops when tearing down
1153  */
1154 static void stop_control_loops(void)
1155 {
1156         if (ctrl_task != 0)
1157                 wait_for_completion(&ctrl_complete);
1158 }
1159
1160 /*
1161  * Attach to the i2c FCU after detecting U3-1 bus
1162  */
1163 static int attach_fcu(void)
1164 {
1165         fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
1166         if (fcu == NULL)
1167                 return -ENODEV;
1168
1169         DBG("FCU attached\n");
1170
1171         return 0;
1172 }
1173
1174 /*
1175  * Detach from the i2c FCU when tearing down
1176  */
1177 static void detach_fcu(void)
1178 {
1179         if (fcu)
1180                 detach_i2c_chip(fcu);
1181         fcu = NULL;
1182 }
1183
1184 /*
1185  * Attach to the i2c controller. We probe the various chips based
1186  * on the device-tree nodes and build everything for the driver to
1187  * run, we then kick the driver monitoring thread
1188  */
1189 static int therm_pm72_attach(struct i2c_adapter *adapter)
1190 {
1191         down(&driver_lock);
1192
1193         /* Check state */
1194         if (state == state_detached)
1195                 state = state_attaching;
1196         if (state != state_attaching) {
1197                 up(&driver_lock);
1198                 return 0;
1199         }
1200
1201         /* Check if we are looking for one of these */
1202         if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
1203                 u3_0 = adapter;
1204                 DBG("found U3-0, creating control loops\n");
1205                 if (create_control_loops())
1206                         u3_0 = NULL;
1207         } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
1208                 u3_1 = adapter;
1209                 DBG("found U3-1, attaching FCU\n");
1210                 if (attach_fcu())
1211                         u3_1 = NULL;
1212         }
1213         /* We got all we need, start control loops */
1214         if (u3_0 != NULL && u3_1 != NULL) {
1215                 DBG("everything up, starting control loops\n");
1216                 state = state_attached;
1217                 start_control_loops();
1218         }
1219         up(&driver_lock);
1220
1221         return 0;
1222 }
1223
1224 /*
1225  * Called on every adapter when the driver or the i2c controller
1226  * is going away.
1227  */
1228 static int therm_pm72_detach(struct i2c_adapter *adapter)
1229 {
1230         down(&driver_lock);
1231
1232         if (state != state_detached)
1233                 state = state_detaching;
1234
1235         /* Stop control loops if any */
1236         DBG("stopping control loops\n");
1237         up(&driver_lock);
1238         stop_control_loops();
1239         down(&driver_lock);
1240
1241         if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
1242                 DBG("lost U3-0, disposing control loops\n");
1243                 dispose_control_loops();
1244                 u3_0 = NULL;
1245         }
1246         
1247         if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
1248                 DBG("lost U3-1, detaching FCU\n");
1249                 detach_fcu();
1250                 u3_1 = NULL;
1251         }
1252         if (u3_0 == NULL && u3_1 == NULL)
1253                 state = state_detached;
1254
1255         up(&driver_lock);
1256
1257         return 0;
1258 }
1259
1260 static int fcu_of_probe(struct of_device* dev, const struct of_match *match)
1261 {
1262         int rc;
1263
1264         state = state_detached;
1265
1266         rc = i2c_add_driver(&therm_pm72_driver);
1267         if (rc < 0)
1268                 return rc;
1269         return 0;
1270 }
1271
1272 static int fcu_of_remove(struct of_device* dev)
1273 {
1274         i2c_del_driver(&therm_pm72_driver);
1275
1276         return 0;
1277 }
1278
1279 static struct of_match fcu_of_match[] = 
1280 {
1281         {
1282         .name           = OF_ANY_MATCH,
1283         .type           = "fcu",
1284         .compatible     = OF_ANY_MATCH
1285         },
1286         {},
1287 };
1288
1289 static struct of_platform_driver fcu_of_platform_driver = 
1290 {
1291         .name           = "temperature",
1292         .match_table    = fcu_of_match,
1293         .probe          = fcu_of_probe,
1294         .remove         = fcu_of_remove
1295 };
1296
1297 /*
1298  * Check machine type, attach to i2c controller
1299  */
1300 static int __init therm_pm72_init(void)
1301 {
1302         struct device_node *np;
1303
1304         if (!machine_is_compatible("PowerMac7,2"))
1305                 return -ENODEV;
1306
1307         printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
1308
1309         np = of_find_node_by_type(NULL, "fcu");
1310         if (np == NULL) {
1311                 printk(KERN_ERR "Can't find FCU in device-tree !\n");
1312                 return -ENODEV;
1313         }
1314         of_dev = of_platform_device_create(np, "temperature");
1315         if (of_dev == NULL) {
1316                 printk(KERN_ERR "Can't register FCU platform device !\n");
1317                 return -ENODEV;
1318         }
1319
1320         of_register_driver(&fcu_of_platform_driver);
1321         
1322         return 0;
1323 }
1324
1325 static void __exit therm_pm72_exit(void)
1326 {
1327         of_unregister_driver(&fcu_of_platform_driver);
1328
1329         if (of_dev)
1330                 of_device_unregister(of_dev);
1331 }
1332
1333 module_init(therm_pm72_init);
1334 module_exit(therm_pm72_exit);
1335
1336 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
1337 MODULE_DESCRIPTION("Driver for Apple's PowerMac7,2 G5 thermal control");
1338 MODULE_LICENSE("GPL");
1339