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