blob: 63ef1f6faaed3f43795e4e915e6ad2cb112debe5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Creation Date: <2003/03/14 20:54:13 samuel>
3 * Time-stamp: <2004/03/20 14:20:59 samuel>
4 *
5 * <therm_windtunnel.c>
6 *
7 * The G4 "windtunnel" has a single fan controlled by an
8 * ADM1030 fan controller and a DS1775 thermostat.
9 *
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.
15 *
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.
19 *
20 * Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
21 *
22 * Loosely based upon 'thermostat.c' written by Benjamin Herrenschmidt
23 *
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
27 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Paul Mackerras98f67402007-12-13 15:57:45 +110039#include <linux/kthread.h>
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/prom.h>
42#include <asm/machdep.h>
43#include <asm/io.h>
44#include <asm/system.h>
45#include <asm/sections.h>
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110046#include <asm/of_platform.h>
Jeff Mahoney5e655772005-07-06 15:44:41 -040047#include <asm/macio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define LOG_TEMP 0 /* continously log temperature */
50
51#define I2C_DRIVERID_G4FAN 0x9001 /* fixme */
52
53static int do_probe( struct i2c_adapter *adapter, int addr, int kind);
54
55/* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
Jean Delvareb3d5496e2005-04-02 20:31:02 +020056static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
57 0x4c, 0x4d, 0x4e, 0x4f,
58 0x2c, 0x2d, 0x2e, 0x2f,
59 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61I2C_CLIENT_INSMOD;
62
63static struct {
64 volatile int running;
Paul Mackerras98f67402007-12-13 15:57:45 +110065 struct task_struct *poll_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 struct semaphore lock;
68 struct of_device *of_dev;
69
70 struct i2c_client *thermostat;
71 struct i2c_client *fan;
72
73 int overheat_temp; /* 100% fan at this temp */
74 int overheat_hyst;
75 int temp;
76 int casetemp;
77 int fan_level; /* active fan_table setting */
78
79 int downind;
80 int upind;
81
82 int r0, r1, r20, r23, r25; /* saved register */
83} x;
84
85#define T(x,y) (((x)<<8) | (y)*0x100/10 )
86
87static struct {
88 int fan_down_setting;
89 int temp;
90 int fan_up_setting;
91} fan_table[] = {
92 { 11, T(0,0), 11 }, /* min fan */
93 { 11, T(55,0), 11 },
94 { 6, T(55,3), 11 },
95 { 7, T(56,0), 11 },
96 { 8, T(57,0), 8 },
97 { 7, T(58,3), 7 },
98 { 6, T(58,8), 6 },
99 { 5, T(59,2), 5 },
100 { 4, T(59,6), 4 },
101 { 3, T(59,9), 3 },
102 { 2, T(60,1), 2 },
103 { 1, 0xfffff, 1 } /* on fire */
104};
105
106static void
107print_temp( const char *s, int temp )
108{
109 printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
110}
111
112static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400113show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
116}
117
118static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400119show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
122}
123
124static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
125static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
126
127
128
129/************************************************************************/
130/* controller thread */
131/************************************************************************/
132
133static int
134write_reg( struct i2c_client *cl, int reg, int data, int len )
135{
136 u8 tmp[3];
137
138 if( len < 1 || len > 2 || data < 0 )
139 return -EINVAL;
140
141 tmp[0] = reg;
142 tmp[1] = (len == 1) ? data : (data >> 8);
143 tmp[2] = data;
144 len++;
145
146 if( i2c_master_send(cl, tmp, len) != len )
147 return -ENODEV;
148 return 0;
149}
150
151static int
152read_reg( struct i2c_client *cl, int reg, int len )
153{
154 u8 buf[2];
155
156 if( len != 1 && len != 2 )
157 return -EINVAL;
158 buf[0] = reg;
159 if( i2c_master_send(cl, buf, 1) != 1 )
160 return -ENODEV;
161 if( i2c_master_recv(cl, buf, len) != len )
162 return -ENODEV;
163 return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
164}
165
166static void
167tune_fan( int fan_setting )
168{
169 int val = (fan_setting << 3) | 7;
170
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 );
175 if( x.casetemp )
176 print_temp(", Case: ", x.casetemp );
177 printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
178
179 x.fan_level = fan_setting;
180}
181
182static void
183poll_temp( void )
184{
185 int temp, i, level, casetemp;
186
187 temp = read_reg( x.thermostat, 0, 2 );
188
189 /* this actually occurs when the computer is loaded */
190 if( temp < 0 )
191 return;
192
193 casetemp = read_reg(x.fan, 0x0b, 1) << 8;
194 casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
195
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 );
200 }
201 x.temp = temp;
202 x.casetemp = casetemp;
203
204 level = -1;
205 for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
206 ;
207 if( i < x.downind )
208 level = fan_table[i].fan_down_setting;
209 x.downind = i;
210
211 for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
212 ;
213 if( x.upind < i )
214 level = fan_table[i].fan_up_setting;
215 x.upind = i;
216
217 if( level >= 0 )
218 tune_fan( level );
219}
220
221
222static void
223setup_hardware( void )
224{
225 int val;
Stephen Rothwell98894df2008-01-03 15:15:28 +1100226 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
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 );
234
235 /* improve measurement resolution (convergence time 1.5s) */
236 if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
237 val |= 0x60;
238 if( write_reg( x.thermostat, 1, val, 1 ) )
239 printk("Failed writing config register\n");
240 }
241 /* disable interrupts and TAC input */
242 write_reg( x.fan, 0x01, 0x01, 1 );
243 /* enable filter */
244 write_reg( x.fan, 0x23, 0x91, 1 );
245 /* remote temp. controls fan */
246 write_reg( x.fan, 0x00, 0x95, 1 );
247
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)...
252 */
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 );
258
259 print_temp("Reducing overheating limit to ", x.overheat_temp );
260 print_temp(" (Hyst: ", x.overheat_hyst );
261 printk(")\n");
262 }
263
264 /* set an initial fan setting */
265 x.downind = 0xffff;
266 x.upind = -1;
267 /* tune_fan( fan_up_table[x.upind].fan_setting ); */
268
Stephen Rothwell98894df2008-01-03 15:15:28 +1100269 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 );
271 if (err)
272 printk(KERN_WARNING
273 "Failed to create temperature attribute file(s).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276static void
277restore_regs( void )
278{
279 device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
280 device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
281
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 );
287}
288
Paul Mackerras98f67402007-12-13 15:57:45 +1100289static int control_loop(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Paul Mackerras98f67402007-12-13 15:57:45 +1100291 down(&x.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 setup_hardware();
Paul Mackerras98f67402007-12-13 15:57:45 +1100293 up(&x.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Paul Mackerras98f67402007-12-13 15:57:45 +1100295 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 msleep_interruptible(8000);
Paul Mackerras98f67402007-12-13 15:57:45 +1100297 if (kthread_should_stop())
298 break;
299
300 down(&x.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 poll_temp();
Paul Mackerras98f67402007-12-13 15:57:45 +1100302 up(&x.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Paul Mackerras98f67402007-12-13 15:57:45 +1100305 down(&x.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 restore_regs();
Paul Mackerras98f67402007-12-13 15:57:45 +1100307 up(&x.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Paul Mackerras98f67402007-12-13 15:57:45 +1100309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312
313/************************************************************************/
314/* i2c probing and setup */
315/************************************************************************/
316
317static int
318do_attach( struct i2c_adapter *adapter )
319{
320 int ret = 0;
321
322 if( strncmp(adapter->name, "uni-n", 5) )
323 return 0;
324
325 if( !x.running ) {
326 ret = i2c_probe( adapter, &addr_data, &do_probe );
327 if( x.thermostat && x.fan ) {
328 x.running = 1;
Paul Mackerras98f67402007-12-13 15:57:45 +1100329 x.poll_task = kthread_run(control_loop, NULL, "g4fand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
332 return ret;
333}
334
335static int
336do_detach( struct i2c_client *client )
337{
338 int err;
339
340 if( (err=i2c_detach_client(client)) )
341 printk(KERN_ERR "failed to detach thermostat client\n");
342 else {
343 if( x.running ) {
344 x.running = 0;
Paul Mackerras98f67402007-12-13 15:57:45 +1100345 kthread_stop(x.poll_task);
346 x.poll_task = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 if( client == x.thermostat )
349 x.thermostat = NULL;
350 else if( client == x.fan )
351 x.fan = NULL;
352 else {
353 printk(KERN_ERR "g4fan: bad client\n");
354 }
355 kfree( client );
356 }
357 return err;
358}
359
360static struct i2c_driver g4fan_driver = {
Laurent Riffarda33ca232005-11-26 20:40:34 +0100361 .driver = {
Laurent Riffarda33ca232005-11-26 20:40:34 +0100362 .name = "therm_windtunnel",
363 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .id = I2C_DRIVERID_G4FAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .attach_adapter = do_attach,
366 .detach_client = do_detach,
367};
368
369static int
370attach_fan( struct i2c_client *cl )
371{
372 if( x.fan )
373 goto out;
374
375 /* check that this is an ADM1030 */
376 if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
377 goto out;
378 printk("ADM1030 fan controller [@%02x]\n", cl->addr );
379
380 strlcpy( cl->name, "ADM1030 fan controller", sizeof(cl->name) );
381
382 if( !i2c_attach_client(cl) )
383 x.fan = cl;
384 out:
385 if( cl != x.fan )
386 kfree( cl );
387 return 0;
388}
389
390static int
391attach_thermostat( struct i2c_client *cl )
392{
393 int hyst_temp, os_temp, temp;
394
395 if( x.thermostat )
396 goto out;
397
398 if( (temp=read_reg(cl, 0, 2)) < 0 )
399 goto out;
400
401 /* temperature sanity check */
402 if( temp < 0x1600 || temp > 0x3c00 )
403 goto out;
404 hyst_temp = read_reg(cl, 2, 2);
405 os_temp = read_reg(cl, 3, 2);
406 if( hyst_temp < 0 || os_temp < 0 )
407 goto out;
408
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 );
413 printk("\n");
414
415 x.temp = temp;
416 x.overheat_temp = os_temp;
417 x.overheat_hyst = hyst_temp;
418
419 strlcpy( cl->name, "DS1775 thermostat", sizeof(cl->name) );
420
421 if( !i2c_attach_client(cl) )
422 x.thermostat = cl;
423out:
424 if( cl != x.thermostat )
425 kfree( cl );
426 return 0;
427}
428
429static int
430do_probe( struct i2c_adapter *adapter, int addr, int kind )
431{
432 struct i2c_client *cl;
433
434 if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
435 | I2C_FUNC_SMBUS_WRITE_BYTE) )
436 return 0;
437
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700438 if( !(cl=kzalloc(sizeof(*cl), GFP_KERNEL)) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 cl->addr = addr;
442 cl->adapter = adapter;
443 cl->driver = &g4fan_driver;
444 cl->flags = 0;
445
446 if( addr < 0x48 )
447 return attach_fan( cl );
448 return attach_thermostat( cl );
449}
450
451
452/************************************************************************/
453/* initialization / cleanup */
454/************************************************************************/
455
456static int
Jeff Mahoney5e655772005-07-06 15:44:41 -0400457therm_of_probe( struct of_device *dev, const struct of_device_id *match )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 return i2c_add_driver( &g4fan_driver );
460}
461
462static int
463therm_of_remove( struct of_device *dev )
464{
Jean Delvareb3e82092007-05-01 23:26:32 +0200465 i2c_del_driver( &g4fan_driver );
466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Jeff Mahoney5e655772005-07-06 15:44:41 -0400469static struct of_device_id therm_of_match[] = {{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 .name = "fan",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 .compatible = "adm1030"
472 }, {}
473};
474
475static 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,
480};
481
482struct apple_thermal_info {
483 u8 id; /* implementation ID */
484 u8 fan_count; /* number of fans */
485 u8 thermostat_count; /* number of thermostats */
486 u8 unused;
487};
488
489static int __init
490g4fan_init( void )
491{
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000492 const struct apple_thermal_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct device_node *np;
494
495 init_MUTEX( &x.lock );
496
497 if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
498 return -ENODEV;
Stephen Rothwell01b27262007-04-27 13:41:15 +1000499 info = of_get_property(np, "thermal-info", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 of_node_put(np);
501
502 if( !info || !machine_is_compatible("PowerMac3,6") )
503 return -ENODEV;
504
505 if( info->id != 3 ) {
506 printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
507 return -ENODEV;
508 }
509 if( !(np=of_find_node_by_name(NULL, "fan")) )
510 return -ENODEV;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700511 x.of_dev = of_platform_device_create(np, "temperature", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 of_node_put( np );
513
514 if( !x.of_dev ) {
515 printk(KERN_ERR "Can't register fan controller!\n");
516 return -ENODEV;
517 }
518
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +1100519 of_register_platform_driver( &therm_of_driver );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
521}
522
523static void __exit
524g4fan_exit( void )
525{
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +1100526 of_unregister_platform_driver( &therm_of_driver );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if( x.of_dev )
529 of_device_unregister( x.of_dev );
530}
531
532module_init(g4fan_init);
533module_exit(g4fan_exit);
534
535MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
536MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
537MODULE_LICENSE("GPL");