2 * Creation Date: <2003/03/14 20:54:13 samuel>
3 * Time-stamp: <2004/03/20 14:20:59 samuel>
7 * The G4 "windtunnel" has a single fan controlled by an
8 * ADM1030 fan controller and a DS1775 thermostat.
10 * The fan controller is equipped with a temperature sensor
11 * which measures the case temperature. The DS1775 sensor
12 * measures the CPU temperature. This driver tunes the
13 * behavior of the fan. It is based upon empirical observations
14 * of the 'AppleFan' driver under Mac OS X.
16 * WARNING: This driver has only been testen on Apple's
17 * 1.25 MHz Dual G4 (March 03). It is tuned for a CPU
18 * temperatur around 57 C.
20 * Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
22 * Loosely based upon 'thermostat.c' written by Benjamin Herrenschmidt
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation
30 #include <linux/types.h>
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/sched.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/kthread.h>
42 #include <asm/machdep.h>
44 #include <asm/system.h>
45 #include <asm/sections.h>
46 #include <asm/of_platform.h>
47 #include <asm/macio.h>
49 #define LOG_TEMP 0 /* continously log temperature */
51 #define I2C_DRIVERID_G4FAN 0x9001 /* fixme */
53 static int do_probe( struct i2c_adapter *adapter, int addr, int kind);
55 /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
56 static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
57 0x4c, 0x4d, 0x4e, 0x4f,
58 0x2c, 0x2d, 0x2e, 0x2f,
65 struct task_struct *poll_task;
67 struct semaphore lock;
68 struct of_device *of_dev;
70 struct i2c_client *thermostat;
71 struct i2c_client *fan;
73 int overheat_temp; /* 100% fan at this temp */
77 int fan_level; /* active fan_table setting */
82 int r0, r1, r20, r23, r25; /* saved register */
85 #define T(x,y) (((x)<<8) | (y)*0x100/10 )
92 { 11, T(0,0), 11 }, /* min fan */
103 { 1, 0xfffff, 1 } /* on fire */
107 print_temp( const char *s, int temp )
109 printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
113 show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
115 return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
119 show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
121 return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
124 static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
125 static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
129 /************************************************************************/
130 /* controller thread */
131 /************************************************************************/
134 write_reg( struct i2c_client *cl, int reg, int data, int len )
138 if( len < 1 || len > 2 || data < 0 )
142 tmp[1] = (len == 1) ? data : (data >> 8);
146 if( i2c_master_send(cl, tmp, len) != len )
152 read_reg( struct i2c_client *cl, int reg, int len )
156 if( len != 1 && len != 2 )
159 if( i2c_master_send(cl, buf, 1) != 1 )
161 if( i2c_master_recv(cl, buf, len) != len )
163 return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
167 tune_fan( int fan_setting )
169 int val = (fan_setting << 3) | 7;
171 /* write_reg( x.fan, 0x24, val, 1 ); */
172 write_reg( x.fan, 0x25, val, 1 );
173 write_reg( x.fan, 0x20, 0, 1 );
174 print_temp("CPU-temp: ", x.temp );
176 print_temp(", Case: ", x.casetemp );
177 printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
179 x.fan_level = fan_setting;
185 int temp, i, level, casetemp;
187 temp = read_reg( x.thermostat, 0, 2 );
189 /* this actually occurs when the computer is loaded */
193 casetemp = read_reg(x.fan, 0x0b, 1) << 8;
194 casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
196 if( LOG_TEMP && x.temp != temp ) {
197 print_temp("CPU-temp: ", temp );
198 print_temp(", Case: ", casetemp );
199 printk(", Fan: %d\n", 11-x.fan_level );
202 x.casetemp = casetemp;
205 for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
208 level = fan_table[i].fan_down_setting;
211 for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
214 level = fan_table[i].fan_up_setting;
223 setup_hardware( void )
228 /* save registers (if we unload the module) */
229 x.r0 = read_reg( x.fan, 0x00, 1 );
230 x.r1 = read_reg( x.fan, 0x01, 1 );
231 x.r20 = read_reg( x.fan, 0x20, 1 );
232 x.r23 = read_reg( x.fan, 0x23, 1 );
233 x.r25 = read_reg( x.fan, 0x25, 1 );
235 /* improve measurement resolution (convergence time 1.5s) */
236 if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
238 if( write_reg( x.thermostat, 1, val, 1 ) )
239 printk("Failed writing config register\n");
241 /* disable interrupts and TAC input */
242 write_reg( x.fan, 0x01, 0x01, 1 );
244 write_reg( x.fan, 0x23, 0x91, 1 );
245 /* remote temp. controls fan */
246 write_reg( x.fan, 0x00, 0x95, 1 );
248 /* The thermostat (which besides measureing temperature controls
249 * has a THERM output which puts the fan on 100%) is usually
250 * set to kick in at 80 C (chip default). We reduce this a bit
251 * to be on the safe side (OSX doesn't)...
253 if( x.overheat_temp == (80 << 8) ) {
254 x.overheat_temp = 65 << 8;
255 x.overheat_hyst = 60 << 8;
256 write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
257 write_reg( x.thermostat, 3, x.overheat_temp, 2 );
259 print_temp("Reducing overheating limit to ", x.overheat_temp );
260 print_temp(" (Hyst: ", x.overheat_hyst );
264 /* set an initial fan setting */
267 /* tune_fan( fan_up_table[x.upind].fan_setting ); */
269 err = device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
270 err |= device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
273 "Failed to create temperature attribute file(s).\n");
279 device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
280 device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
282 write_reg( x.fan, 0x01, x.r1, 1 );
283 write_reg( x.fan, 0x20, x.r20, 1 );
284 write_reg( x.fan, 0x23, x.r23, 1 );
285 write_reg( x.fan, 0x25, x.r25, 1 );
286 write_reg( x.fan, 0x00, x.r0, 1 );
289 static int control_loop(void *dummy)
296 msleep_interruptible(8000);
297 if (kthread_should_stop())
313 /************************************************************************/
314 /* i2c probing and setup */
315 /************************************************************************/
318 do_attach( struct i2c_adapter *adapter )
322 if( strncmp(adapter->name, "uni-n", 5) )
326 ret = i2c_probe( adapter, &addr_data, &do_probe );
327 if( x.thermostat && x.fan ) {
329 x.poll_task = kthread_run(control_loop, NULL, "g4fand");
336 do_detach( struct i2c_client *client )
340 if( (err=i2c_detach_client(client)) )
341 printk(KERN_ERR "failed to detach thermostat client\n");
345 kthread_stop(x.poll_task);
348 if( client == x.thermostat )
350 else if( client == x.fan )
353 printk(KERN_ERR "g4fan: bad client\n");
360 static struct i2c_driver g4fan_driver = {
362 .name = "therm_windtunnel",
364 .id = I2C_DRIVERID_G4FAN,
365 .attach_adapter = do_attach,
366 .detach_client = do_detach,
370 attach_fan( struct i2c_client *cl )
375 /* check that this is an ADM1030 */
376 if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
378 printk("ADM1030 fan controller [@%02x]\n", cl->addr );
380 strlcpy( cl->name, "ADM1030 fan controller", sizeof(cl->name) );
382 if( !i2c_attach_client(cl) )
391 attach_thermostat( struct i2c_client *cl )
393 int hyst_temp, os_temp, temp;
398 if( (temp=read_reg(cl, 0, 2)) < 0 )
401 /* temperature sanity check */
402 if( temp < 0x1600 || temp > 0x3c00 )
404 hyst_temp = read_reg(cl, 2, 2);
405 os_temp = read_reg(cl, 3, 2);
406 if( hyst_temp < 0 || os_temp < 0 )
409 printk("DS1775 digital thermometer [@%02x]\n", cl->addr );
410 print_temp("Temp: ", temp );
411 print_temp(" Hyst: ", hyst_temp );
412 print_temp(" OS: ", os_temp );
416 x.overheat_temp = os_temp;
417 x.overheat_hyst = hyst_temp;
419 strlcpy( cl->name, "DS1775 thermostat", sizeof(cl->name) );
421 if( !i2c_attach_client(cl) )
424 if( cl != x.thermostat )
430 do_probe( struct i2c_adapter *adapter, int addr, int kind )
432 struct i2c_client *cl;
434 if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
435 | I2C_FUNC_SMBUS_WRITE_BYTE) )
438 if( !(cl=kzalloc(sizeof(*cl), GFP_KERNEL)) )
442 cl->adapter = adapter;
443 cl->driver = &g4fan_driver;
447 return attach_fan( cl );
448 return attach_thermostat( cl );
452 /************************************************************************/
453 /* initialization / cleanup */
454 /************************************************************************/
457 therm_of_probe( struct of_device *dev, const struct of_device_id *match )
459 return i2c_add_driver( &g4fan_driver );
463 therm_of_remove( struct of_device *dev )
465 i2c_del_driver( &g4fan_driver );
469 static struct of_device_id therm_of_match[] = {{
471 .compatible = "adm1030"
475 static struct of_platform_driver therm_of_driver = {
476 .name = "temperature",
477 .match_table = therm_of_match,
478 .probe = therm_of_probe,
479 .remove = therm_of_remove,
482 struct apple_thermal_info {
483 u8 id; /* implementation ID */
484 u8 fan_count; /* number of fans */
485 u8 thermostat_count; /* number of thermostats */
492 const struct apple_thermal_info *info;
493 struct device_node *np;
495 init_MUTEX( &x.lock );
497 if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
499 info = of_get_property(np, "thermal-info", NULL);
502 if( !info || !machine_is_compatible("PowerMac3,6") )
505 if( info->id != 3 ) {
506 printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
509 if( !(np=of_find_node_by_name(NULL, "fan")) )
511 x.of_dev = of_platform_device_create(np, "temperature", NULL);
515 printk(KERN_ERR "Can't register fan controller!\n");
519 of_register_platform_driver( &therm_of_driver );
526 of_unregister_platform_driver( &therm_of_driver );
529 of_device_unregister( x.of_dev );
532 module_init(g4fan_init);
533 module_exit(g4fan_exit);
535 MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
536 MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
537 MODULE_LICENSE("GPL");