diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/macintosh/therm_windtunnel.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/macintosh/therm_windtunnel.c')
-rw-r--r-- | drivers/macintosh/therm_windtunnel.c | 531 |
1 files changed, 531 insertions, 0 deletions
diff --git a/drivers/macintosh/therm_windtunnel.c b/drivers/macintosh/therm_windtunnel.c new file mode 100644 index 000000000000..c153699d0f84 --- /dev/null +++ b/drivers/macintosh/therm_windtunnel.c | |||
@@ -0,0 +1,531 @@ | |||
1 | /* | ||
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 | |||
30 | #include <linux/config.h> | ||
31 | #include <linux/types.h> | ||
32 | #include <linux/module.h> | ||
33 | #include <linux/errno.h> | ||
34 | #include <linux/kernel.h> | ||
35 | #include <linux/delay.h> | ||
36 | #include <linux/sched.h> | ||
37 | #include <linux/i2c.h> | ||
38 | #include <linux/slab.h> | ||
39 | #include <linux/init.h> | ||
40 | #include <asm/prom.h> | ||
41 | #include <asm/machdep.h> | ||
42 | #include <asm/io.h> | ||
43 | #include <asm/system.h> | ||
44 | #include <asm/sections.h> | ||
45 | #include <asm/of_device.h> | ||
46 | |||
47 | #define LOG_TEMP 0 /* continously log temperature */ | ||
48 | |||
49 | #define I2C_DRIVERID_G4FAN 0x9001 /* fixme */ | ||
50 | |||
51 | static int do_probe( struct i2c_adapter *adapter, int addr, int kind); | ||
52 | |||
53 | /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */ | ||
54 | static unsigned short normal_i2c[] = { 0x49, 0x2c, I2C_CLIENT_END }; | ||
55 | static unsigned short normal_i2c_range[] = { 0x48, 0x4f, 0x2c, 0x2f, I2C_CLIENT_END }; | ||
56 | |||
57 | I2C_CLIENT_INSMOD; | ||
58 | |||
59 | static struct { | ||
60 | volatile int running; | ||
61 | struct completion completion; | ||
62 | pid_t poll_task; | ||
63 | |||
64 | struct semaphore lock; | ||
65 | struct of_device *of_dev; | ||
66 | |||
67 | struct i2c_client *thermostat; | ||
68 | struct i2c_client *fan; | ||
69 | |||
70 | int overheat_temp; /* 100% fan at this temp */ | ||
71 | int overheat_hyst; | ||
72 | int temp; | ||
73 | int casetemp; | ||
74 | int fan_level; /* active fan_table setting */ | ||
75 | |||
76 | int downind; | ||
77 | int upind; | ||
78 | |||
79 | int r0, r1, r20, r23, r25; /* saved register */ | ||
80 | } x; | ||
81 | |||
82 | #define T(x,y) (((x)<<8) | (y)*0x100/10 ) | ||
83 | |||
84 | static struct { | ||
85 | int fan_down_setting; | ||
86 | int temp; | ||
87 | int fan_up_setting; | ||
88 | } fan_table[] = { | ||
89 | { 11, T(0,0), 11 }, /* min fan */ | ||
90 | { 11, T(55,0), 11 }, | ||
91 | { 6, T(55,3), 11 }, | ||
92 | { 7, T(56,0), 11 }, | ||
93 | { 8, T(57,0), 8 }, | ||
94 | { 7, T(58,3), 7 }, | ||
95 | { 6, T(58,8), 6 }, | ||
96 | { 5, T(59,2), 5 }, | ||
97 | { 4, T(59,6), 4 }, | ||
98 | { 3, T(59,9), 3 }, | ||
99 | { 2, T(60,1), 2 }, | ||
100 | { 1, 0xfffff, 1 } /* on fire */ | ||
101 | }; | ||
102 | |||
103 | static void | ||
104 | print_temp( const char *s, int temp ) | ||
105 | { | ||
106 | printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 ); | ||
107 | } | ||
108 | |||
109 | static ssize_t | ||
110 | show_cpu_temperature( struct device *dev, char *buf ) | ||
111 | { | ||
112 | return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 ); | ||
113 | } | ||
114 | |||
115 | static ssize_t | ||
116 | show_case_temperature( struct device *dev, char *buf ) | ||
117 | { | ||
118 | return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 ); | ||
119 | } | ||
120 | |||
121 | static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL ); | ||
122 | static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL ); | ||
123 | |||
124 | |||
125 | |||
126 | /************************************************************************/ | ||
127 | /* controller thread */ | ||
128 | /************************************************************************/ | ||
129 | |||
130 | static int | ||
131 | write_reg( struct i2c_client *cl, int reg, int data, int len ) | ||
132 | { | ||
133 | u8 tmp[3]; | ||
134 | |||
135 | if( len < 1 || len > 2 || data < 0 ) | ||
136 | return -EINVAL; | ||
137 | |||
138 | tmp[0] = reg; | ||
139 | tmp[1] = (len == 1) ? data : (data >> 8); | ||
140 | tmp[2] = data; | ||
141 | len++; | ||
142 | |||
143 | if( i2c_master_send(cl, tmp, len) != len ) | ||
144 | return -ENODEV; | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | static int | ||
149 | read_reg( struct i2c_client *cl, int reg, int len ) | ||
150 | { | ||
151 | u8 buf[2]; | ||
152 | |||
153 | if( len != 1 && len != 2 ) | ||
154 | return -EINVAL; | ||
155 | buf[0] = reg; | ||
156 | if( i2c_master_send(cl, buf, 1) != 1 ) | ||
157 | return -ENODEV; | ||
158 | if( i2c_master_recv(cl, buf, len) != len ) | ||
159 | return -ENODEV; | ||
160 | return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0]; | ||
161 | } | ||
162 | |||
163 | static void | ||
164 | tune_fan( int fan_setting ) | ||
165 | { | ||
166 | int val = (fan_setting << 3) | 7; | ||
167 | |||
168 | /* write_reg( x.fan, 0x24, val, 1 ); */ | ||
169 | write_reg( x.fan, 0x25, val, 1 ); | ||
170 | write_reg( x.fan, 0x20, 0, 1 ); | ||
171 | print_temp("CPU-temp: ", x.temp ); | ||
172 | if( x.casetemp ) | ||
173 | print_temp(", Case: ", x.casetemp ); | ||
174 | printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting ); | ||
175 | |||
176 | x.fan_level = fan_setting; | ||
177 | } | ||
178 | |||
179 | static void | ||
180 | poll_temp( void ) | ||
181 | { | ||
182 | int temp, i, level, casetemp; | ||
183 | |||
184 | temp = read_reg( x.thermostat, 0, 2 ); | ||
185 | |||
186 | /* this actually occurs when the computer is loaded */ | ||
187 | if( temp < 0 ) | ||
188 | return; | ||
189 | |||
190 | casetemp = read_reg(x.fan, 0x0b, 1) << 8; | ||
191 | casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5; | ||
192 | |||
193 | if( LOG_TEMP && x.temp != temp ) { | ||
194 | print_temp("CPU-temp: ", temp ); | ||
195 | print_temp(", Case: ", casetemp ); | ||
196 | printk(", Fan: %d\n", 11-x.fan_level ); | ||
197 | } | ||
198 | x.temp = temp; | ||
199 | x.casetemp = casetemp; | ||
200 | |||
201 | level = -1; | ||
202 | for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ ) | ||
203 | ; | ||
204 | if( i < x.downind ) | ||
205 | level = fan_table[i].fan_down_setting; | ||
206 | x.downind = i; | ||
207 | |||
208 | for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ ) | ||
209 | ; | ||
210 | if( x.upind < i ) | ||
211 | level = fan_table[i].fan_up_setting; | ||
212 | x.upind = i; | ||
213 | |||
214 | if( level >= 0 ) | ||
215 | tune_fan( level ); | ||
216 | } | ||
217 | |||
218 | |||
219 | static void | ||
220 | setup_hardware( void ) | ||
221 | { | ||
222 | int val; | ||
223 | |||
224 | /* save registers (if we unload the module) */ | ||
225 | x.r0 = read_reg( x.fan, 0x00, 1 ); | ||
226 | x.r1 = read_reg( x.fan, 0x01, 1 ); | ||
227 | x.r20 = read_reg( x.fan, 0x20, 1 ); | ||
228 | x.r23 = read_reg( x.fan, 0x23, 1 ); | ||
229 | x.r25 = read_reg( x.fan, 0x25, 1 ); | ||
230 | |||
231 | /* improve measurement resolution (convergence time 1.5s) */ | ||
232 | if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) { | ||
233 | val |= 0x60; | ||
234 | if( write_reg( x.thermostat, 1, val, 1 ) ) | ||
235 | printk("Failed writing config register\n"); | ||
236 | } | ||
237 | /* disable interrupts and TAC input */ | ||
238 | write_reg( x.fan, 0x01, 0x01, 1 ); | ||
239 | /* enable filter */ | ||
240 | write_reg( x.fan, 0x23, 0x91, 1 ); | ||
241 | /* remote temp. controls fan */ | ||
242 | write_reg( x.fan, 0x00, 0x95, 1 ); | ||
243 | |||
244 | /* The thermostat (which besides measureing temperature controls | ||
245 | * has a THERM output which puts the fan on 100%) is usually | ||
246 | * set to kick in at 80 C (chip default). We reduce this a bit | ||
247 | * to be on the safe side (OSX doesn't)... | ||
248 | */ | ||
249 | if( x.overheat_temp == (80 << 8) ) { | ||
250 | x.overheat_temp = 65 << 8; | ||
251 | x.overheat_hyst = 60 << 8; | ||
252 | write_reg( x.thermostat, 2, x.overheat_hyst, 2 ); | ||
253 | write_reg( x.thermostat, 3, x.overheat_temp, 2 ); | ||
254 | |||
255 | print_temp("Reducing overheating limit to ", x.overheat_temp ); | ||
256 | print_temp(" (Hyst: ", x.overheat_hyst ); | ||
257 | printk(")\n"); | ||
258 | } | ||
259 | |||
260 | /* set an initial fan setting */ | ||
261 | x.downind = 0xffff; | ||
262 | x.upind = -1; | ||
263 | /* tune_fan( fan_up_table[x.upind].fan_setting ); */ | ||
264 | |||
265 | device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature ); | ||
266 | device_create_file( &x.of_dev->dev, &dev_attr_case_temperature ); | ||
267 | } | ||
268 | |||
269 | static void | ||
270 | restore_regs( void ) | ||
271 | { | ||
272 | device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature ); | ||
273 | device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature ); | ||
274 | |||
275 | write_reg( x.fan, 0x01, x.r1, 1 ); | ||
276 | write_reg( x.fan, 0x20, x.r20, 1 ); | ||
277 | write_reg( x.fan, 0x23, x.r23, 1 ); | ||
278 | write_reg( x.fan, 0x25, x.r25, 1 ); | ||
279 | write_reg( x.fan, 0x00, x.r0, 1 ); | ||
280 | } | ||
281 | |||
282 | static int | ||
283 | control_loop( void *dummy ) | ||
284 | { | ||
285 | daemonize("g4fand"); | ||
286 | |||
287 | down( &x.lock ); | ||
288 | setup_hardware(); | ||
289 | |||
290 | while( x.running ) { | ||
291 | up( &x.lock ); | ||
292 | |||
293 | msleep_interruptible(8000); | ||
294 | |||
295 | down( &x.lock ); | ||
296 | poll_temp(); | ||
297 | } | ||
298 | |||
299 | restore_regs(); | ||
300 | up( &x.lock ); | ||
301 | |||
302 | complete_and_exit( &x.completion, 0 ); | ||
303 | } | ||
304 | |||
305 | |||
306 | /************************************************************************/ | ||
307 | /* i2c probing and setup */ | ||
308 | /************************************************************************/ | ||
309 | |||
310 | static int | ||
311 | do_attach( struct i2c_adapter *adapter ) | ||
312 | { | ||
313 | int ret = 0; | ||
314 | |||
315 | if( strncmp(adapter->name, "uni-n", 5) ) | ||
316 | return 0; | ||
317 | |||
318 | if( !x.running ) { | ||
319 | ret = i2c_probe( adapter, &addr_data, &do_probe ); | ||
320 | if( x.thermostat && x.fan ) { | ||
321 | x.running = 1; | ||
322 | init_completion( &x.completion ); | ||
323 | x.poll_task = kernel_thread( control_loop, NULL, SIGCHLD | CLONE_KERNEL ); | ||
324 | } | ||
325 | } | ||
326 | return ret; | ||
327 | } | ||
328 | |||
329 | static int | ||
330 | do_detach( struct i2c_client *client ) | ||
331 | { | ||
332 | int err; | ||
333 | |||
334 | if( (err=i2c_detach_client(client)) ) | ||
335 | printk(KERN_ERR "failed to detach thermostat client\n"); | ||
336 | else { | ||
337 | if( x.running ) { | ||
338 | x.running = 0; | ||
339 | wait_for_completion( &x.completion ); | ||
340 | } | ||
341 | if( client == x.thermostat ) | ||
342 | x.thermostat = NULL; | ||
343 | else if( client == x.fan ) | ||
344 | x.fan = NULL; | ||
345 | else { | ||
346 | printk(KERN_ERR "g4fan: bad client\n"); | ||
347 | } | ||
348 | kfree( client ); | ||
349 | } | ||
350 | return err; | ||
351 | } | ||
352 | |||
353 | static struct i2c_driver g4fan_driver = { | ||
354 | .owner = THIS_MODULE, | ||
355 | .name = "therm_windtunnel", | ||
356 | .id = I2C_DRIVERID_G4FAN, | ||
357 | .flags = I2C_DF_NOTIFY, | ||
358 | .attach_adapter = do_attach, | ||
359 | .detach_client = do_detach, | ||
360 | }; | ||
361 | |||
362 | static int | ||
363 | attach_fan( struct i2c_client *cl ) | ||
364 | { | ||
365 | if( x.fan ) | ||
366 | goto out; | ||
367 | |||
368 | /* check that this is an ADM1030 */ | ||
369 | if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 ) | ||
370 | goto out; | ||
371 | printk("ADM1030 fan controller [@%02x]\n", cl->addr ); | ||
372 | |||
373 | strlcpy( cl->name, "ADM1030 fan controller", sizeof(cl->name) ); | ||
374 | |||
375 | if( !i2c_attach_client(cl) ) | ||
376 | x.fan = cl; | ||
377 | out: | ||
378 | if( cl != x.fan ) | ||
379 | kfree( cl ); | ||
380 | return 0; | ||
381 | } | ||
382 | |||
383 | static int | ||
384 | attach_thermostat( struct i2c_client *cl ) | ||
385 | { | ||
386 | int hyst_temp, os_temp, temp; | ||
387 | |||
388 | if( x.thermostat ) | ||
389 | goto out; | ||
390 | |||
391 | if( (temp=read_reg(cl, 0, 2)) < 0 ) | ||
392 | goto out; | ||
393 | |||
394 | /* temperature sanity check */ | ||
395 | if( temp < 0x1600 || temp > 0x3c00 ) | ||
396 | goto out; | ||
397 | hyst_temp = read_reg(cl, 2, 2); | ||
398 | os_temp = read_reg(cl, 3, 2); | ||
399 | if( hyst_temp < 0 || os_temp < 0 ) | ||
400 | goto out; | ||
401 | |||
402 | printk("DS1775 digital thermometer [@%02x]\n", cl->addr ); | ||
403 | print_temp("Temp: ", temp ); | ||
404 | print_temp(" Hyst: ", hyst_temp ); | ||
405 | print_temp(" OS: ", os_temp ); | ||
406 | printk("\n"); | ||
407 | |||
408 | x.temp = temp; | ||
409 | x.overheat_temp = os_temp; | ||
410 | x.overheat_hyst = hyst_temp; | ||
411 | |||
412 | strlcpy( cl->name, "DS1775 thermostat", sizeof(cl->name) ); | ||
413 | |||
414 | if( !i2c_attach_client(cl) ) | ||
415 | x.thermostat = cl; | ||
416 | out: | ||
417 | if( cl != x.thermostat ) | ||
418 | kfree( cl ); | ||
419 | return 0; | ||
420 | } | ||
421 | |||
422 | static int | ||
423 | do_probe( struct i2c_adapter *adapter, int addr, int kind ) | ||
424 | { | ||
425 | struct i2c_client *cl; | ||
426 | |||
427 | if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA | ||
428 | | I2C_FUNC_SMBUS_WRITE_BYTE) ) | ||
429 | return 0; | ||
430 | |||
431 | if( !(cl=kmalloc(sizeof(*cl), GFP_KERNEL)) ) | ||
432 | return -ENOMEM; | ||
433 | memset( cl, 0, sizeof(struct i2c_client) ); | ||
434 | |||
435 | cl->addr = addr; | ||
436 | cl->adapter = adapter; | ||
437 | cl->driver = &g4fan_driver; | ||
438 | cl->flags = 0; | ||
439 | |||
440 | if( addr < 0x48 ) | ||
441 | return attach_fan( cl ); | ||
442 | return attach_thermostat( cl ); | ||
443 | } | ||
444 | |||
445 | |||
446 | /************************************************************************/ | ||
447 | /* initialization / cleanup */ | ||
448 | /************************************************************************/ | ||
449 | |||
450 | static int | ||
451 | therm_of_probe( struct of_device *dev, const struct of_match *match ) | ||
452 | { | ||
453 | return i2c_add_driver( &g4fan_driver ); | ||
454 | } | ||
455 | |||
456 | static int | ||
457 | therm_of_remove( struct of_device *dev ) | ||
458 | { | ||
459 | return i2c_del_driver( &g4fan_driver ); | ||
460 | } | ||
461 | |||
462 | static struct of_match therm_of_match[] = {{ | ||
463 | .name = "fan", | ||
464 | .type = OF_ANY_MATCH, | ||
465 | .compatible = "adm1030" | ||
466 | }, {} | ||
467 | }; | ||
468 | |||
469 | static struct of_platform_driver therm_of_driver = { | ||
470 | .name = "temperature", | ||
471 | .match_table = therm_of_match, | ||
472 | .probe = therm_of_probe, | ||
473 | .remove = therm_of_remove, | ||
474 | }; | ||
475 | |||
476 | struct apple_thermal_info { | ||
477 | u8 id; /* implementation ID */ | ||
478 | u8 fan_count; /* number of fans */ | ||
479 | u8 thermostat_count; /* number of thermostats */ | ||
480 | u8 unused; | ||
481 | }; | ||
482 | |||
483 | static int __init | ||
484 | g4fan_init( void ) | ||
485 | { | ||
486 | struct apple_thermal_info *info; | ||
487 | struct device_node *np; | ||
488 | |||
489 | init_MUTEX( &x.lock ); | ||
490 | |||
491 | if( !(np=of_find_node_by_name(NULL, "power-mgt")) ) | ||
492 | return -ENODEV; | ||
493 | info = (struct apple_thermal_info*)get_property(np, "thermal-info", NULL); | ||
494 | of_node_put(np); | ||
495 | |||
496 | if( !info || !machine_is_compatible("PowerMac3,6") ) | ||
497 | return -ENODEV; | ||
498 | |||
499 | if( info->id != 3 ) { | ||
500 | printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id ); | ||
501 | return -ENODEV; | ||
502 | } | ||
503 | if( !(np=of_find_node_by_name(NULL, "fan")) ) | ||
504 | return -ENODEV; | ||
505 | x.of_dev = of_platform_device_create( np, "temperature" ); | ||
506 | of_node_put( np ); | ||
507 | |||
508 | if( !x.of_dev ) { | ||
509 | printk(KERN_ERR "Can't register fan controller!\n"); | ||
510 | return -ENODEV; | ||
511 | } | ||
512 | |||
513 | of_register_driver( &therm_of_driver ); | ||
514 | return 0; | ||
515 | } | ||
516 | |||
517 | static void __exit | ||
518 | g4fan_exit( void ) | ||
519 | { | ||
520 | of_unregister_driver( &therm_of_driver ); | ||
521 | |||
522 | if( x.of_dev ) | ||
523 | of_device_unregister( x.of_dev ); | ||
524 | } | ||
525 | |||
526 | module_init(g4fan_init); | ||
527 | module_exit(g4fan_exit); | ||
528 | |||
529 | MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>"); | ||
530 | MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller"); | ||
531 | MODULE_LICENSE("GPL"); | ||