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 * temperature 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/init.h>
38 #include <linux/kthread.h>
39 #include <linux/of_platform.h>
42 #include <asm/machdep.h>
44 #include <asm/sections.h>
45 #include <asm/macio.h>
47 #define LOG_TEMP 0 /* continuously log temperature */
51 struct task_struct
*poll_task
;
54 struct platform_device
*of_dev
;
56 struct i2c_client
*thermostat
;
57 struct i2c_client
*fan
;
59 int overheat_temp
; /* 100% fan at this temp */
63 int fan_level
; /* active fan_table setting */
68 int r0
, r1
, r20
, r23
, r25
; /* saved register */
71 #define T(x,y) (((x)<<8) | (y)*0x100/10 )
78 { 11, T(0,0), 11 }, /* min fan */
89 { 1, 0xfffff, 1 } /* on fire */
93 print_temp( const char *s
, int temp
)
95 printk("%s%d.%d C", s
? s
: "", temp
>>8, (temp
& 255)*10/256 );
99 show_cpu_temperature( struct device
*dev
, struct device_attribute
*attr
, char *buf
)
101 return sprintf(buf
, "%d.%d\n", x
.temp
>>8, (x
.temp
& 255)*10/256 );
105 show_case_temperature( struct device
*dev
, struct device_attribute
*attr
, char *buf
)
107 return sprintf(buf
, "%d.%d\n", x
.casetemp
>>8, (x
.casetemp
& 255)*10/256 );
110 static DEVICE_ATTR(cpu_temperature
, S_IRUGO
, show_cpu_temperature
, NULL
);
111 static DEVICE_ATTR(case_temperature
, S_IRUGO
, show_case_temperature
, NULL
);
115 /************************************************************************/
116 /* controller thread */
117 /************************************************************************/
120 write_reg( struct i2c_client
*cl
, int reg
, int data
, int len
)
124 if( len
< 1 || len
> 2 || data
< 0 )
128 tmp
[1] = (len
== 1) ? data
: (data
>> 8);
132 if( i2c_master_send(cl
, tmp
, len
) != len
)
138 read_reg( struct i2c_client
*cl
, int reg
, int len
)
142 if( len
!= 1 && len
!= 2 )
145 if( i2c_master_send(cl
, buf
, 1) != 1 )
147 if( i2c_master_recv(cl
, buf
, len
) != len
)
149 return (len
== 2)? ((unsigned int)buf
[0] << 8) | buf
[1] : buf
[0];
153 tune_fan( int fan_setting
)
155 int val
= (fan_setting
<< 3) | 7;
157 /* write_reg( x.fan, 0x24, val, 1 ); */
158 write_reg( x
.fan
, 0x25, val
, 1 );
159 write_reg( x
.fan
, 0x20, 0, 1 );
160 print_temp("CPU-temp: ", x
.temp
);
162 print_temp(", Case: ", x
.casetemp
);
163 printk(", Fan: %d (tuned %+d)\n", 11-fan_setting
, x
.fan_level
-fan_setting
);
165 x
.fan_level
= fan_setting
;
171 int temp
, i
, level
, casetemp
;
173 temp
= read_reg( x
.thermostat
, 0, 2 );
175 /* this actually occurs when the computer is loaded */
179 casetemp
= read_reg(x
.fan
, 0x0b, 1) << 8;
180 casetemp
|= (read_reg(x
.fan
, 0x06, 1) & 0x7) << 5;
182 if( LOG_TEMP
&& x
.temp
!= temp
) {
183 print_temp("CPU-temp: ", temp
);
184 print_temp(", Case: ", casetemp
);
185 printk(", Fan: %d\n", 11-x
.fan_level
);
188 x
.casetemp
= casetemp
;
191 for( i
=0; (temp
& 0xffff) > fan_table
[i
].temp
; i
++ )
194 level
= fan_table
[i
].fan_down_setting
;
197 for( i
=0; (temp
& 0xffff) >= fan_table
[i
+1].temp
; i
++ )
200 level
= fan_table
[i
].fan_up_setting
;
209 setup_hardware( void )
214 /* save registers (if we unload the module) */
215 x
.r0
= read_reg( x
.fan
, 0x00, 1 );
216 x
.r1
= read_reg( x
.fan
, 0x01, 1 );
217 x
.r20
= read_reg( x
.fan
, 0x20, 1 );
218 x
.r23
= read_reg( x
.fan
, 0x23, 1 );
219 x
.r25
= read_reg( x
.fan
, 0x25, 1 );
221 /* improve measurement resolution (convergence time 1.5s) */
222 if( (val
=read_reg(x
.thermostat
, 1, 1)) >= 0 ) {
224 if( write_reg( x
.thermostat
, 1, val
, 1 ) )
225 printk("Failed writing config register\n");
227 /* disable interrupts and TAC input */
228 write_reg( x
.fan
, 0x01, 0x01, 1 );
230 write_reg( x
.fan
, 0x23, 0x91, 1 );
231 /* remote temp. controls fan */
232 write_reg( x
.fan
, 0x00, 0x95, 1 );
234 /* The thermostat (which besides measureing temperature controls
235 * has a THERM output which puts the fan on 100%) is usually
236 * set to kick in at 80 C (chip default). We reduce this a bit
237 * to be on the safe side (OSX doesn't)...
239 if( x
.overheat_temp
== (80 << 8) ) {
240 x
.overheat_temp
= 75 << 8;
241 x
.overheat_hyst
= 70 << 8;
242 write_reg( x
.thermostat
, 2, x
.overheat_hyst
, 2 );
243 write_reg( x
.thermostat
, 3, x
.overheat_temp
, 2 );
245 print_temp("Reducing overheating limit to ", x
.overheat_temp
);
246 print_temp(" (Hyst: ", x
.overheat_hyst
);
250 /* set an initial fan setting */
253 /* tune_fan( fan_up_table[x.upind].fan_setting ); */
255 err
= device_create_file( &x
.of_dev
->dev
, &dev_attr_cpu_temperature
);
256 err
|= device_create_file( &x
.of_dev
->dev
, &dev_attr_case_temperature
);
259 "Failed to create temperature attribute file(s).\n");
265 device_remove_file( &x
.of_dev
->dev
, &dev_attr_cpu_temperature
);
266 device_remove_file( &x
.of_dev
->dev
, &dev_attr_case_temperature
);
268 write_reg( x
.fan
, 0x01, x
.r1
, 1 );
269 write_reg( x
.fan
, 0x20, x
.r20
, 1 );
270 write_reg( x
.fan
, 0x23, x
.r23
, 1 );
271 write_reg( x
.fan
, 0x25, x
.r25
, 1 );
272 write_reg( x
.fan
, 0x00, x
.r0
, 1 );
275 static int control_loop(void *dummy
)
279 mutex_unlock(&x
.lock
);
282 msleep_interruptible(8000);
283 if (kthread_should_stop())
288 mutex_unlock(&x
.lock
);
293 mutex_unlock(&x
.lock
);
299 /************************************************************************/
300 /* i2c probing and setup */
301 /************************************************************************/
303 static void do_attach(struct i2c_adapter
*adapter
)
305 struct i2c_board_info info
= { };
306 struct device_node
*np
;
308 /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
309 static const unsigned short scan_ds1775
[] = {
310 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
313 static const unsigned short scan_adm1030
[] = {
314 0x2c, 0x2d, 0x2e, 0x2f,
318 if (x
.running
|| strncmp(adapter
->name
, "uni-n", 5))
321 np
= of_find_compatible_node(adapter
->dev
.of_node
, NULL
, "MAC,ds1775");
325 strlcpy(info
.type
, "MAC,ds1775", I2C_NAME_SIZE
);
326 i2c_new_scanned_device(adapter
, &info
, scan_ds1775
, NULL
);
329 np
= of_find_compatible_node(adapter
->dev
.of_node
, NULL
, "MAC,adm1030");
333 strlcpy(info
.type
, "MAC,adm1030", I2C_NAME_SIZE
);
334 i2c_new_scanned_device(adapter
, &info
, scan_adm1030
, NULL
);
339 do_remove(struct i2c_client
*client
)
343 kthread_stop(x
.poll_task
);
346 if (client
== x
.thermostat
)
348 else if (client
== x
.fan
)
351 printk(KERN_ERR
"g4fan: bad client\n");
357 attach_fan( struct i2c_client
*cl
)
362 /* check that this is an ADM1030 */
363 if( read_reg(cl
, 0x3d, 1) != 0x30 || read_reg(cl
, 0x3e, 1) != 0x41 )
365 printk("ADM1030 fan controller [@%02x]\n", cl
->addr
);
373 attach_thermostat( struct i2c_client
*cl
)
375 int hyst_temp
, os_temp
, temp
;
380 if( (temp
=read_reg(cl
, 0, 2)) < 0 )
383 /* temperature sanity check */
384 if( temp
< 0x1600 || temp
> 0x3c00 )
386 hyst_temp
= read_reg(cl
, 2, 2);
387 os_temp
= read_reg(cl
, 3, 2);
388 if( hyst_temp
< 0 || os_temp
< 0 )
391 printk("DS1775 digital thermometer [@%02x]\n", cl
->addr
);
392 print_temp("Temp: ", temp
);
393 print_temp(" Hyst: ", hyst_temp
);
394 print_temp(" OS: ", os_temp
);
398 x
.overheat_temp
= os_temp
;
399 x
.overheat_hyst
= hyst_temp
;
405 enum chip
{ ds1775
, adm1030
};
407 static const struct i2c_device_id therm_windtunnel_id
[] = {
408 { "MAC,ds1775", ds1775
},
409 { "MAC,adm1030", adm1030
},
412 MODULE_DEVICE_TABLE(i2c
, therm_windtunnel_id
);
415 do_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
417 struct i2c_adapter
*adapter
= cl
->adapter
;
420 if( !i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
421 | I2C_FUNC_SMBUS_WRITE_BYTE
) )
424 switch (id
->driver_data
) {
426 ret
= attach_fan(cl
);
429 ret
= attach_thermostat(cl
);
433 if (!x
.running
&& x
.thermostat
&& x
.fan
) {
435 x
.poll_task
= kthread_run(control_loop
, NULL
, "g4fand");
441 static struct i2c_driver g4fan_driver
= {
443 .name
= "therm_windtunnel",
447 .id_table
= therm_windtunnel_id
,
451 /************************************************************************/
452 /* initialization / cleanup */
453 /************************************************************************/
455 static int therm_of_probe(struct platform_device
*dev
)
457 struct i2c_adapter
*adap
;
460 adap
= i2c_get_adapter(0);
462 return -EPROBE_DEFER
;
464 ret
= i2c_add_driver(&g4fan_driver
);
466 i2c_put_adapter(adap
);
470 /* We assume Macs have consecutive I2C bus numbers starting at 0 */
475 i2c_put_adapter(adap
);
476 adap
= i2c_get_adapter(++i
);
483 therm_of_remove( struct platform_device
*dev
)
485 i2c_del_driver( &g4fan_driver
);
489 static const struct of_device_id therm_of_match
[] = {{
491 .compatible
= "adm1030"
494 MODULE_DEVICE_TABLE(of
, therm_of_match
);
496 static struct platform_driver therm_of_driver
= {
498 .name
= "temperature",
499 .of_match_table
= therm_of_match
,
501 .probe
= therm_of_probe
,
502 .remove
= therm_of_remove
,
505 struct apple_thermal_info
{
506 u8 id
; /* implementation ID */
507 u8 fan_count
; /* number of fans */
508 u8 thermostat_count
; /* number of thermostats */
515 const struct apple_thermal_info
*info
;
516 struct device_node
*np
;
520 if( !(np
=of_find_node_by_name(NULL
, "power-mgt")) )
522 info
= of_get_property(np
, "thermal-info", NULL
);
525 if( !info
|| !of_machine_is_compatible("PowerMac3,6") )
528 if( info
->id
!= 3 ) {
529 printk(KERN_ERR
"therm_windtunnel: unsupported thermal design %d\n", info
->id
);
532 if( !(np
=of_find_node_by_name(NULL
, "fan")) )
534 x
.of_dev
= of_platform_device_create(np
, "temperature", NULL
);
538 printk(KERN_ERR
"Can't register fan controller!\n");
542 platform_driver_register( &therm_of_driver
);
549 platform_driver_unregister( &therm_of_driver
);
552 of_device_unregister( x
.of_dev
);
555 module_init(g4fan_init
);
556 module_exit(g4fan_exit
);
558 MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
559 MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
560 MODULE_LICENSE("GPL");