Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
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> | |
98f6740e | 39 | #include <linux/kthread.h> |
7eebde70 | 40 | |
1da177e4 LT |
41 | #include <asm/prom.h> |
42 | #include <asm/machdep.h> | |
43 | #include <asm/io.h> | |
44 | #include <asm/system.h> | |
45 | #include <asm/sections.h> | |
7eebde70 | 46 | #include <asm/of_platform.h> |
5e655772 | 47 | #include <asm/macio.h> |
1da177e4 LT |
48 | |
49 | #define LOG_TEMP 0 /* continously log temperature */ | |
50 | ||
51 | #define I2C_DRIVERID_G4FAN 0x9001 /* fixme */ | |
52 | ||
53 | static int do_probe( struct i2c_adapter *adapter, int addr, int kind); | |
54 | ||
55 | /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */ | |
b3d5496e JD |
56 | static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, |
57 | 0x4c, 0x4d, 0x4e, 0x4f, | |
58 | 0x2c, 0x2d, 0x2e, 0x2f, | |
59 | I2C_CLIENT_END }; | |
1da177e4 LT |
60 | |
61 | I2C_CLIENT_INSMOD; | |
62 | ||
63 | static struct { | |
64 | volatile int running; | |
98f6740e | 65 | struct task_struct *poll_task; |
1da177e4 LT |
66 | |
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 | ||
87 | static 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 | ||
106 | static void | |
107 | print_temp( const char *s, int temp ) | |
108 | { | |
109 | printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 ); | |
110 | } | |
111 | ||
112 | static ssize_t | |
e404e274 | 113 | show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf ) |
1da177e4 LT |
114 | { |
115 | return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 ); | |
116 | } | |
117 | ||
118 | static ssize_t | |
e404e274 | 119 | show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf ) |
1da177e4 LT |
120 | { |
121 | return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 ); | |
122 | } | |
123 | ||
124 | static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL ); | |
125 | static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL ); | |
126 | ||
127 | ||
128 | ||
129 | /************************************************************************/ | |
130 | /* controller thread */ | |
131 | /************************************************************************/ | |
132 | ||
133 | static int | |
134 | write_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 | ||
151 | static int | |
152 | read_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 | ||
166 | static void | |
167 | tune_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 | ||
182 | static void | |
183 | poll_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 | ||
222 | static void | |
223 | setup_hardware( void ) | |
224 | { | |
225 | int val; | |
226 | ||
227 | /* save registers (if we unload the module) */ | |
228 | x.r0 = read_reg( x.fan, 0x00, 1 ); | |
229 | x.r1 = read_reg( x.fan, 0x01, 1 ); | |
230 | x.r20 = read_reg( x.fan, 0x20, 1 ); | |
231 | x.r23 = read_reg( x.fan, 0x23, 1 ); | |
232 | x.r25 = read_reg( x.fan, 0x25, 1 ); | |
233 | ||
234 | /* improve measurement resolution (convergence time 1.5s) */ | |
235 | if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) { | |
236 | val |= 0x60; | |
237 | if( write_reg( x.thermostat, 1, val, 1 ) ) | |
238 | printk("Failed writing config register\n"); | |
239 | } | |
240 | /* disable interrupts and TAC input */ | |
241 | write_reg( x.fan, 0x01, 0x01, 1 ); | |
242 | /* enable filter */ | |
243 | write_reg( x.fan, 0x23, 0x91, 1 ); | |
244 | /* remote temp. controls fan */ | |
245 | write_reg( x.fan, 0x00, 0x95, 1 ); | |
246 | ||
247 | /* The thermostat (which besides measureing temperature controls | |
248 | * has a THERM output which puts the fan on 100%) is usually | |
249 | * set to kick in at 80 C (chip default). We reduce this a bit | |
250 | * to be on the safe side (OSX doesn't)... | |
251 | */ | |
252 | if( x.overheat_temp == (80 << 8) ) { | |
253 | x.overheat_temp = 65 << 8; | |
254 | x.overheat_hyst = 60 << 8; | |
255 | write_reg( x.thermostat, 2, x.overheat_hyst, 2 ); | |
256 | write_reg( x.thermostat, 3, x.overheat_temp, 2 ); | |
257 | ||
258 | print_temp("Reducing overheating limit to ", x.overheat_temp ); | |
259 | print_temp(" (Hyst: ", x.overheat_hyst ); | |
260 | printk(")\n"); | |
261 | } | |
262 | ||
263 | /* set an initial fan setting */ | |
264 | x.downind = 0xffff; | |
265 | x.upind = -1; | |
266 | /* tune_fan( fan_up_table[x.upind].fan_setting ); */ | |
267 | ||
268 | device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature ); | |
269 | device_create_file( &x.of_dev->dev, &dev_attr_case_temperature ); | |
270 | } | |
271 | ||
272 | static void | |
273 | restore_regs( void ) | |
274 | { | |
275 | device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature ); | |
276 | device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature ); | |
277 | ||
278 | write_reg( x.fan, 0x01, x.r1, 1 ); | |
279 | write_reg( x.fan, 0x20, x.r20, 1 ); | |
280 | write_reg( x.fan, 0x23, x.r23, 1 ); | |
281 | write_reg( x.fan, 0x25, x.r25, 1 ); | |
282 | write_reg( x.fan, 0x00, x.r0, 1 ); | |
283 | } | |
284 | ||
98f6740e | 285 | static int control_loop(void *dummy) |
1da177e4 | 286 | { |
98f6740e | 287 | down(&x.lock); |
1da177e4 | 288 | setup_hardware(); |
98f6740e | 289 | up(&x.lock); |
1da177e4 | 290 | |
98f6740e | 291 | for (;;) { |
1da177e4 | 292 | msleep_interruptible(8000); |
98f6740e PM |
293 | if (kthread_should_stop()) |
294 | break; | |
295 | ||
296 | down(&x.lock); | |
1da177e4 | 297 | poll_temp(); |
98f6740e | 298 | up(&x.lock); |
1da177e4 LT |
299 | } |
300 | ||
98f6740e | 301 | down(&x.lock); |
1da177e4 | 302 | restore_regs(); |
98f6740e | 303 | up(&x.lock); |
1da177e4 | 304 | |
98f6740e | 305 | return 0; |
1da177e4 LT |
306 | } |
307 | ||
308 | ||
309 | /************************************************************************/ | |
310 | /* i2c probing and setup */ | |
311 | /************************************************************************/ | |
312 | ||
313 | static int | |
314 | do_attach( struct i2c_adapter *adapter ) | |
315 | { | |
316 | int ret = 0; | |
317 | ||
318 | if( strncmp(adapter->name, "uni-n", 5) ) | |
319 | return 0; | |
320 | ||
321 | if( !x.running ) { | |
322 | ret = i2c_probe( adapter, &addr_data, &do_probe ); | |
323 | if( x.thermostat && x.fan ) { | |
324 | x.running = 1; | |
98f6740e | 325 | x.poll_task = kthread_run(control_loop, NULL, "g4fand"); |
1da177e4 LT |
326 | } |
327 | } | |
328 | return ret; | |
329 | } | |
330 | ||
331 | static int | |
332 | do_detach( struct i2c_client *client ) | |
333 | { | |
334 | int err; | |
335 | ||
336 | if( (err=i2c_detach_client(client)) ) | |
337 | printk(KERN_ERR "failed to detach thermostat client\n"); | |
338 | else { | |
339 | if( x.running ) { | |
340 | x.running = 0; | |
98f6740e PM |
341 | kthread_stop(x.poll_task); |
342 | x.poll_task = NULL; | |
1da177e4 LT |
343 | } |
344 | if( client == x.thermostat ) | |
345 | x.thermostat = NULL; | |
346 | else if( client == x.fan ) | |
347 | x.fan = NULL; | |
348 | else { | |
349 | printk(KERN_ERR "g4fan: bad client\n"); | |
350 | } | |
351 | kfree( client ); | |
352 | } | |
353 | return err; | |
354 | } | |
355 | ||
356 | static struct i2c_driver g4fan_driver = { | |
a33ca232 | 357 | .driver = { |
a33ca232 LR |
358 | .name = "therm_windtunnel", |
359 | }, | |
1da177e4 | 360 | .id = I2C_DRIVERID_G4FAN, |
1da177e4 LT |
361 | .attach_adapter = do_attach, |
362 | .detach_client = do_detach, | |
363 | }; | |
364 | ||
365 | static int | |
366 | attach_fan( struct i2c_client *cl ) | |
367 | { | |
368 | if( x.fan ) | |
369 | goto out; | |
370 | ||
371 | /* check that this is an ADM1030 */ | |
372 | if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 ) | |
373 | goto out; | |
374 | printk("ADM1030 fan controller [@%02x]\n", cl->addr ); | |
375 | ||
376 | strlcpy( cl->name, "ADM1030 fan controller", sizeof(cl->name) ); | |
377 | ||
378 | if( !i2c_attach_client(cl) ) | |
379 | x.fan = cl; | |
380 | out: | |
381 | if( cl != x.fan ) | |
382 | kfree( cl ); | |
383 | return 0; | |
384 | } | |
385 | ||
386 | static int | |
387 | attach_thermostat( struct i2c_client *cl ) | |
388 | { | |
389 | int hyst_temp, os_temp, temp; | |
390 | ||
391 | if( x.thermostat ) | |
392 | goto out; | |
393 | ||
394 | if( (temp=read_reg(cl, 0, 2)) < 0 ) | |
395 | goto out; | |
396 | ||
397 | /* temperature sanity check */ | |
398 | if( temp < 0x1600 || temp > 0x3c00 ) | |
399 | goto out; | |
400 | hyst_temp = read_reg(cl, 2, 2); | |
401 | os_temp = read_reg(cl, 3, 2); | |
402 | if( hyst_temp < 0 || os_temp < 0 ) | |
403 | goto out; | |
404 | ||
405 | printk("DS1775 digital thermometer [@%02x]\n", cl->addr ); | |
406 | print_temp("Temp: ", temp ); | |
407 | print_temp(" Hyst: ", hyst_temp ); | |
408 | print_temp(" OS: ", os_temp ); | |
409 | printk("\n"); | |
410 | ||
411 | x.temp = temp; | |
412 | x.overheat_temp = os_temp; | |
413 | x.overheat_hyst = hyst_temp; | |
414 | ||
415 | strlcpy( cl->name, "DS1775 thermostat", sizeof(cl->name) ); | |
416 | ||
417 | if( !i2c_attach_client(cl) ) | |
418 | x.thermostat = cl; | |
419 | out: | |
420 | if( cl != x.thermostat ) | |
421 | kfree( cl ); | |
422 | return 0; | |
423 | } | |
424 | ||
425 | static int | |
426 | do_probe( struct i2c_adapter *adapter, int addr, int kind ) | |
427 | { | |
428 | struct i2c_client *cl; | |
429 | ||
430 | if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA | |
431 | | I2C_FUNC_SMBUS_WRITE_BYTE) ) | |
432 | return 0; | |
433 | ||
dd00cc48 | 434 | if( !(cl=kzalloc(sizeof(*cl), GFP_KERNEL)) ) |
1da177e4 | 435 | return -ENOMEM; |
1da177e4 LT |
436 | |
437 | cl->addr = addr; | |
438 | cl->adapter = adapter; | |
439 | cl->driver = &g4fan_driver; | |
440 | cl->flags = 0; | |
441 | ||
442 | if( addr < 0x48 ) | |
443 | return attach_fan( cl ); | |
444 | return attach_thermostat( cl ); | |
445 | } | |
446 | ||
447 | ||
448 | /************************************************************************/ | |
449 | /* initialization / cleanup */ | |
450 | /************************************************************************/ | |
451 | ||
452 | static int | |
5e655772 | 453 | therm_of_probe( struct of_device *dev, const struct of_device_id *match ) |
1da177e4 LT |
454 | { |
455 | return i2c_add_driver( &g4fan_driver ); | |
456 | } | |
457 | ||
458 | static int | |
459 | therm_of_remove( struct of_device *dev ) | |
460 | { | |
b3e82096 JD |
461 | i2c_del_driver( &g4fan_driver ); |
462 | return 0; | |
1da177e4 LT |
463 | } |
464 | ||
5e655772 | 465 | static struct of_device_id therm_of_match[] = {{ |
1da177e4 | 466 | .name = "fan", |
1da177e4 LT |
467 | .compatible = "adm1030" |
468 | }, {} | |
469 | }; | |
470 | ||
471 | static struct of_platform_driver therm_of_driver = { | |
472 | .name = "temperature", | |
473 | .match_table = therm_of_match, | |
474 | .probe = therm_of_probe, | |
475 | .remove = therm_of_remove, | |
476 | }; | |
477 | ||
478 | struct apple_thermal_info { | |
479 | u8 id; /* implementation ID */ | |
480 | u8 fan_count; /* number of fans */ | |
481 | u8 thermostat_count; /* number of thermostats */ | |
482 | u8 unused; | |
483 | }; | |
484 | ||
485 | static int __init | |
486 | g4fan_init( void ) | |
487 | { | |
018a3d1d | 488 | const struct apple_thermal_info *info; |
1da177e4 LT |
489 | struct device_node *np; |
490 | ||
491 | init_MUTEX( &x.lock ); | |
492 | ||
493 | if( !(np=of_find_node_by_name(NULL, "power-mgt")) ) | |
494 | return -ENODEV; | |
01b2726d | 495 | info = of_get_property(np, "thermal-info", NULL); |
1da177e4 LT |
496 | of_node_put(np); |
497 | ||
498 | if( !info || !machine_is_compatible("PowerMac3,6") ) | |
499 | return -ENODEV; | |
500 | ||
501 | if( info->id != 3 ) { | |
502 | printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id ); | |
503 | return -ENODEV; | |
504 | } | |
505 | if( !(np=of_find_node_by_name(NULL, "fan")) ) | |
506 | return -ENODEV; | |
0365ba7f | 507 | x.of_dev = of_platform_device_create(np, "temperature", NULL); |
1da177e4 LT |
508 | of_node_put( np ); |
509 | ||
510 | if( !x.of_dev ) { | |
511 | printk(KERN_ERR "Can't register fan controller!\n"); | |
512 | return -ENODEV; | |
513 | } | |
514 | ||
7eebde70 | 515 | of_register_platform_driver( &therm_of_driver ); |
1da177e4 LT |
516 | return 0; |
517 | } | |
518 | ||
519 | static void __exit | |
520 | g4fan_exit( void ) | |
521 | { | |
7eebde70 | 522 | of_unregister_platform_driver( &therm_of_driver ); |
1da177e4 LT |
523 | |
524 | if( x.of_dev ) | |
525 | of_device_unregister( x.of_dev ); | |
526 | } | |
527 | ||
528 | module_init(g4fan_init); | |
529 | module_exit(g4fan_exit); | |
530 | ||
531 | MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>"); | |
532 | MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller"); | |
533 | MODULE_LICENSE("GPL"); |