]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/macintosh/therm_windtunnel.c
KVM: Portability: Split kvm_vcpu into arch dependent and independent parts (part 1)
[mirror_ubuntu-bionic-kernel.git] / drivers / macintosh / therm_windtunnel.c
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/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
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_platform.h>
46 #include <asm/macio.h>
47
48 #define LOG_TEMP 0 /* continously log temperature */
49
50 static int do_probe( struct i2c_adapter *adapter, int addr, int kind);
51
52 /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
53 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
54 0x4c, 0x4d, 0x4e, 0x4f,
55 0x2c, 0x2d, 0x2e, 0x2f,
56 I2C_CLIENT_END };
57
58 I2C_CLIENT_INSMOD;
59
60 static struct {
61 volatile int running;
62 struct completion completion;
63 pid_t poll_task;
64
65 struct semaphore lock;
66 struct of_device *of_dev;
67
68 struct i2c_client *thermostat;
69 struct i2c_client *fan;
70
71 int overheat_temp; /* 100% fan at this temp */
72 int overheat_hyst;
73 int temp;
74 int casetemp;
75 int fan_level; /* active fan_table setting */
76
77 int downind;
78 int upind;
79
80 int r0, r1, r20, r23, r25; /* saved register */
81 } x;
82
83 #define T(x,y) (((x)<<8) | (y)*0x100/10 )
84
85 static struct {
86 int fan_down_setting;
87 int temp;
88 int fan_up_setting;
89 } fan_table[] = {
90 { 11, T(0,0), 11 }, /* min fan */
91 { 11, T(55,0), 11 },
92 { 6, T(55,3), 11 },
93 { 7, T(56,0), 11 },
94 { 8, T(57,0), 8 },
95 { 7, T(58,3), 7 },
96 { 6, T(58,8), 6 },
97 { 5, T(59,2), 5 },
98 { 4, T(59,6), 4 },
99 { 3, T(59,9), 3 },
100 { 2, T(60,1), 2 },
101 { 1, 0xfffff, 1 } /* on fire */
102 };
103
104 static void
105 print_temp( const char *s, int temp )
106 {
107 printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
108 }
109
110 static ssize_t
111 show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
112 {
113 return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
114 }
115
116 static ssize_t
117 show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
118 {
119 return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
120 }
121
122 static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
123 static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
124
125
126
127 /************************************************************************/
128 /* controller thread */
129 /************************************************************************/
130
131 static int
132 write_reg( struct i2c_client *cl, int reg, int data, int len )
133 {
134 u8 tmp[3];
135
136 if( len < 1 || len > 2 || data < 0 )
137 return -EINVAL;
138
139 tmp[0] = reg;
140 tmp[1] = (len == 1) ? data : (data >> 8);
141 tmp[2] = data;
142 len++;
143
144 if( i2c_master_send(cl, tmp, len) != len )
145 return -ENODEV;
146 return 0;
147 }
148
149 static int
150 read_reg( struct i2c_client *cl, int reg, int len )
151 {
152 u8 buf[2];
153
154 if( len != 1 && len != 2 )
155 return -EINVAL;
156 buf[0] = reg;
157 if( i2c_master_send(cl, buf, 1) != 1 )
158 return -ENODEV;
159 if( i2c_master_recv(cl, buf, len) != len )
160 return -ENODEV;
161 return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
162 }
163
164 static void
165 tune_fan( int fan_setting )
166 {
167 int val = (fan_setting << 3) | 7;
168
169 /* write_reg( x.fan, 0x24, val, 1 ); */
170 write_reg( x.fan, 0x25, val, 1 );
171 write_reg( x.fan, 0x20, 0, 1 );
172 print_temp("CPU-temp: ", x.temp );
173 if( x.casetemp )
174 print_temp(", Case: ", x.casetemp );
175 printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
176
177 x.fan_level = fan_setting;
178 }
179
180 static void
181 poll_temp( void )
182 {
183 int temp, i, level, casetemp;
184
185 temp = read_reg( x.thermostat, 0, 2 );
186
187 /* this actually occurs when the computer is loaded */
188 if( temp < 0 )
189 return;
190
191 casetemp = read_reg(x.fan, 0x0b, 1) << 8;
192 casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
193
194 if( LOG_TEMP && x.temp != temp ) {
195 print_temp("CPU-temp: ", temp );
196 print_temp(", Case: ", casetemp );
197 printk(", Fan: %d\n", 11-x.fan_level );
198 }
199 x.temp = temp;
200 x.casetemp = casetemp;
201
202 level = -1;
203 for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
204 ;
205 if( i < x.downind )
206 level = fan_table[i].fan_down_setting;
207 x.downind = i;
208
209 for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
210 ;
211 if( x.upind < i )
212 level = fan_table[i].fan_up_setting;
213 x.upind = i;
214
215 if( level >= 0 )
216 tune_fan( level );
217 }
218
219
220 static void
221 setup_hardware( void )
222 {
223 int val;
224
225 /* save registers (if we unload the module) */
226 x.r0 = read_reg( x.fan, 0x00, 1 );
227 x.r1 = read_reg( x.fan, 0x01, 1 );
228 x.r20 = read_reg( x.fan, 0x20, 1 );
229 x.r23 = read_reg( x.fan, 0x23, 1 );
230 x.r25 = read_reg( x.fan, 0x25, 1 );
231
232 /* improve measurement resolution (convergence time 1.5s) */
233 if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
234 val |= 0x60;
235 if( write_reg( x.thermostat, 1, val, 1 ) )
236 printk("Failed writing config register\n");
237 }
238 /* disable interrupts and TAC input */
239 write_reg( x.fan, 0x01, 0x01, 1 );
240 /* enable filter */
241 write_reg( x.fan, 0x23, 0x91, 1 );
242 /* remote temp. controls fan */
243 write_reg( x.fan, 0x00, 0x95, 1 );
244
245 /* The thermostat (which besides measureing temperature controls
246 * has a THERM output which puts the fan on 100%) is usually
247 * set to kick in at 80 C (chip default). We reduce this a bit
248 * to be on the safe side (OSX doesn't)...
249 */
250 if( x.overheat_temp == (80 << 8) ) {
251 x.overheat_temp = 65 << 8;
252 x.overheat_hyst = 60 << 8;
253 write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
254 write_reg( x.thermostat, 3, x.overheat_temp, 2 );
255
256 print_temp("Reducing overheating limit to ", x.overheat_temp );
257 print_temp(" (Hyst: ", x.overheat_hyst );
258 printk(")\n");
259 }
260
261 /* set an initial fan setting */
262 x.downind = 0xffff;
263 x.upind = -1;
264 /* tune_fan( fan_up_table[x.upind].fan_setting ); */
265
266 device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
267 device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
268 }
269
270 static void
271 restore_regs( void )
272 {
273 device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
274 device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
275
276 write_reg( x.fan, 0x01, x.r1, 1 );
277 write_reg( x.fan, 0x20, x.r20, 1 );
278 write_reg( x.fan, 0x23, x.r23, 1 );
279 write_reg( x.fan, 0x25, x.r25, 1 );
280 write_reg( x.fan, 0x00, x.r0, 1 );
281 }
282
283 static int
284 control_loop( void *dummy )
285 {
286 daemonize("g4fand");
287
288 down( &x.lock );
289 setup_hardware();
290
291 while( x.running ) {
292 up( &x.lock );
293
294 msleep_interruptible(8000);
295
296 down( &x.lock );
297 poll_temp();
298 }
299
300 restore_regs();
301 up( &x.lock );
302
303 complete_and_exit( &x.completion, 0 );
304 }
305
306
307 /************************************************************************/
308 /* i2c probing and setup */
309 /************************************************************************/
310
311 static int
312 do_attach( struct i2c_adapter *adapter )
313 {
314 int ret = 0;
315
316 if( strncmp(adapter->name, "uni-n", 5) )
317 return 0;
318
319 if( !x.running ) {
320 ret = i2c_probe( adapter, &addr_data, &do_probe );
321 if( x.thermostat && x.fan ) {
322 x.running = 1;
323 init_completion( &x.completion );
324 x.poll_task = kernel_thread( control_loop, NULL, SIGCHLD | CLONE_KERNEL );
325 }
326 }
327 return ret;
328 }
329
330 static int
331 do_detach( struct i2c_client *client )
332 {
333 int err;
334
335 if( (err=i2c_detach_client(client)) )
336 printk(KERN_ERR "failed to detach thermostat client\n");
337 else {
338 if( x.running ) {
339 x.running = 0;
340 wait_for_completion( &x.completion );
341 }
342 if( client == x.thermostat )
343 x.thermostat = NULL;
344 else if( client == x.fan )
345 x.fan = NULL;
346 else {
347 printk(KERN_ERR "g4fan: bad client\n");
348 }
349 kfree( client );
350 }
351 return err;
352 }
353
354 static struct i2c_driver g4fan_driver = {
355 .driver = {
356 .name = "therm_windtunnel",
357 },
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=kzalloc(sizeof(*cl), GFP_KERNEL)) )
432 return -ENOMEM;
433
434 cl->addr = addr;
435 cl->adapter = adapter;
436 cl->driver = &g4fan_driver;
437 cl->flags = 0;
438
439 if( addr < 0x48 )
440 return attach_fan( cl );
441 return attach_thermostat( cl );
442 }
443
444
445 /************************************************************************/
446 /* initialization / cleanup */
447 /************************************************************************/
448
449 static int
450 therm_of_probe( struct of_device *dev, const struct of_device_id *match )
451 {
452 return i2c_add_driver( &g4fan_driver );
453 }
454
455 static int
456 therm_of_remove( struct of_device *dev )
457 {
458 i2c_del_driver( &g4fan_driver );
459 return 0;
460 }
461
462 static struct of_device_id therm_of_match[] = {{
463 .name = "fan",
464 .compatible = "adm1030"
465 }, {}
466 };
467
468 static struct of_platform_driver therm_of_driver = {
469 .name = "temperature",
470 .match_table = therm_of_match,
471 .probe = therm_of_probe,
472 .remove = therm_of_remove,
473 };
474
475 struct apple_thermal_info {
476 u8 id; /* implementation ID */
477 u8 fan_count; /* number of fans */
478 u8 thermostat_count; /* number of thermostats */
479 u8 unused;
480 };
481
482 static int __init
483 g4fan_init( void )
484 {
485 const struct apple_thermal_info *info;
486 struct device_node *np;
487
488 init_MUTEX( &x.lock );
489
490 if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
491 return -ENODEV;
492 info = of_get_property(np, "thermal-info", NULL);
493 of_node_put(np);
494
495 if( !info || !machine_is_compatible("PowerMac3,6") )
496 return -ENODEV;
497
498 if( info->id != 3 ) {
499 printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
500 return -ENODEV;
501 }
502 if( !(np=of_find_node_by_name(NULL, "fan")) )
503 return -ENODEV;
504 x.of_dev = of_platform_device_create(np, "temperature", NULL);
505 of_node_put( np );
506
507 if( !x.of_dev ) {
508 printk(KERN_ERR "Can't register fan controller!\n");
509 return -ENODEV;
510 }
511
512 of_register_platform_driver( &therm_of_driver );
513 return 0;
514 }
515
516 static void __exit
517 g4fan_exit( void )
518 {
519 of_unregister_platform_driver( &therm_of_driver );
520
521 if( x.of_dev )
522 of_device_unregister( x.of_dev );
523 }
524
525 module_init(g4fan_init);
526 module_exit(g4fan_exit);
527
528 MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
529 MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
530 MODULE_LICENSE("GPL");