xref: /linux/drivers/macintosh/therm_windtunnel.c (revision b86406d42ae3c41ae0ce332ea24350829b88af51)
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  *	temperature 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/init.h>
38 #include <linux/kthread.h>
39 #include <linux/of_platform.h>
40 
41 #include <asm/machdep.h>
42 #include <asm/io.h>
43 #include <asm/sections.h>
44 #include <asm/macio.h>
45 
46 #define LOG_TEMP		0			/* continuously log temperature */
47 
48 static struct {
49 	volatile int		running;
50 	struct task_struct	*poll_task;
51 
52 	struct mutex	 	lock;
53 	struct platform_device	*of_dev;
54 
55 	struct i2c_client	*thermostat;
56 	struct i2c_client	*fan;
57 
58 	int			overheat_temp;		/* 100% fan at this temp */
59 	int			overheat_hyst;
60 	int			temp;
61 	int			casetemp;
62 	int			fan_level;		/* active fan_table setting */
63 
64 	int			downind;
65 	int			upind;
66 
67 	int			r0, r1, r20, r23, r25;	/* saved register */
68 } x;
69 
70 #define T(x,y)			(((x)<<8) | (y)*0x100/10 )
71 
72 static struct {
73 	int			fan_down_setting;
74 	int			temp;
75 	int			fan_up_setting;
76 } fan_table[] = {
77 	{ 11, T(0,0),  11 },	/* min fan */
78 	{ 11, T(55,0), 11 },
79 	{  6, T(55,3), 11 },
80 	{  7, T(56,0), 11 },
81 	{  8, T(57,0), 8 },
82 	{  7, T(58,3), 7 },
83 	{  6, T(58,8), 6 },
84 	{  5, T(59,2), 5 },
85 	{  4, T(59,6), 4 },
86 	{  3, T(59,9), 3 },
87 	{  2, T(60,1), 2 },
88 	{  1, 0xfffff, 1 }	/* on fire */
89 };
90 
91 static void
92 print_temp( const char *s, int temp )
93 {
94 	printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
95 }
96 
97 static ssize_t
98 show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
99 {
100 	return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
101 }
102 
103 static ssize_t
104 show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
105 {
106 	return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
107 }
108 
109 static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
110 static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
111 
112 
113 
114 /************************************************************************/
115 /*	controller thread						*/
116 /************************************************************************/
117 
118 static int
119 write_reg( struct i2c_client *cl, int reg, int data, int len )
120 {
121 	u8 tmp[3];
122 
123 	if( len < 1 || len > 2 || data < 0 )
124 		return -EINVAL;
125 
126 	tmp[0] = reg;
127 	tmp[1] = (len == 1) ? data : (data >> 8);
128 	tmp[2] = data;
129 	len++;
130 
131 	if( i2c_master_send(cl, tmp, len) != len )
132 		return -ENODEV;
133 	return 0;
134 }
135 
136 static int
137 read_reg( struct i2c_client *cl, int reg, int len )
138 {
139 	u8 buf[2];
140 
141 	if( len != 1 && len != 2 )
142 		return -EINVAL;
143 	buf[0] = reg;
144 	if( i2c_master_send(cl, buf, 1) != 1 )
145 		return -ENODEV;
146 	if( i2c_master_recv(cl, buf, len) != len )
147 		return -ENODEV;
148 	return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
149 }
150 
151 static void
152 tune_fan( int fan_setting )
153 {
154 	int val = (fan_setting << 3) | 7;
155 
156 	/* write_reg( x.fan, 0x24, val, 1 ); */
157 	write_reg( x.fan, 0x25, val, 1 );
158 	write_reg( x.fan, 0x20, 0, 1 );
159 	print_temp("CPU-temp: ", x.temp );
160 	if( x.casetemp )
161 		print_temp(", Case: ", x.casetemp );
162 	printk(",  Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
163 
164 	x.fan_level = fan_setting;
165 }
166 
167 static void
168 poll_temp( void )
169 {
170 	int temp, i, level, casetemp;
171 
172 	temp = read_reg( x.thermostat, 0, 2 );
173 
174 	/* this actually occurs when the computer is loaded */
175 	if( temp < 0 )
176 		return;
177 
178 	casetemp = read_reg(x.fan, 0x0b, 1) << 8;
179 	casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
180 
181 	if( LOG_TEMP && x.temp != temp ) {
182 		print_temp("CPU-temp: ", temp );
183 		print_temp(", Case: ", casetemp );
184 		printk(",  Fan: %d\n", 11-x.fan_level );
185 	}
186 	x.temp = temp;
187 	x.casetemp = casetemp;
188 
189 	level = -1;
190 	for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
191 		;
192 	if( i < x.downind )
193 		level = fan_table[i].fan_down_setting;
194 	x.downind = i;
195 
196 	for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
197 		;
198 	if( x.upind < i )
199 		level = fan_table[i].fan_up_setting;
200 	x.upind = i;
201 
202 	if( level >= 0 )
203 		tune_fan( level );
204 }
205 
206 
207 static void
208 setup_hardware( void )
209 {
210 	int val;
211 	int err;
212 
213 	/* save registers (if we unload the module) */
214 	x.r0 = read_reg( x.fan, 0x00, 1 );
215 	x.r1 = read_reg( x.fan, 0x01, 1 );
216 	x.r20 = read_reg( x.fan, 0x20, 1 );
217 	x.r23 = read_reg( x.fan, 0x23, 1 );
218 	x.r25 = read_reg( x.fan, 0x25, 1 );
219 
220 	/* improve measurement resolution (convergence time 1.5s) */
221 	if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
222 		val |= 0x60;
223 		if( write_reg( x.thermostat, 1, val, 1 ) )
224 			printk("Failed writing config register\n");
225 	}
226 	/* disable interrupts and TAC input */
227 	write_reg( x.fan, 0x01, 0x01, 1 );
228 	/* enable filter */
229 	write_reg( x.fan, 0x23, 0x91, 1 );
230 	/* remote temp. controls fan */
231 	write_reg( x.fan, 0x00, 0x95, 1 );
232 
233 	/* The thermostat (which besides measureing temperature controls
234 	 * has a THERM output which puts the fan on 100%) is usually
235 	 * set to kick in at 80 C (chip default). We reduce this a bit
236 	 * to be on the safe side (OSX doesn't)...
237 	 */
238 	if( x.overheat_temp == (80 << 8) ) {
239 		x.overheat_temp = 75 << 8;
240 		x.overheat_hyst = 70 << 8;
241 		write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
242 		write_reg( x.thermostat, 3, x.overheat_temp, 2 );
243 
244 		print_temp("Reducing overheating limit to ", x.overheat_temp );
245 		print_temp(" (Hyst: ", x.overheat_hyst );
246 		printk(")\n");
247 	}
248 
249 	/* set an initial fan setting */
250 	x.downind = 0xffff;
251 	x.upind = -1;
252 	/* tune_fan( fan_up_table[x.upind].fan_setting ); */
253 
254 	err = device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
255 	err |= device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
256 	if (err)
257 		printk(KERN_WARNING
258 			"Failed to create temperature attribute file(s).\n");
259 }
260 
261 static void
262 restore_regs( void )
263 {
264 	device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
265 	device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
266 
267 	write_reg( x.fan, 0x01, x.r1, 1 );
268 	write_reg( x.fan, 0x20, x.r20, 1 );
269 	write_reg( x.fan, 0x23, x.r23, 1 );
270 	write_reg( x.fan, 0x25, x.r25, 1 );
271 	write_reg( x.fan, 0x00, x.r0, 1 );
272 }
273 
274 static int control_loop(void *dummy)
275 {
276 	mutex_lock(&x.lock);
277 	setup_hardware();
278 	mutex_unlock(&x.lock);
279 
280 	for (;;) {
281 		msleep_interruptible(8000);
282 		if (kthread_should_stop())
283 			break;
284 
285 		mutex_lock(&x.lock);
286 		poll_temp();
287 		mutex_unlock(&x.lock);
288 	}
289 
290 	mutex_lock(&x.lock);
291 	restore_regs();
292 	mutex_unlock(&x.lock);
293 
294 	return 0;
295 }
296 
297 
298 /************************************************************************/
299 /*	i2c probing and setup						*/
300 /************************************************************************/
301 
302 static void do_attach(struct i2c_adapter *adapter)
303 {
304 	struct i2c_board_info info = { };
305 	struct device_node *np;
306 
307 	/* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
308 	static const unsigned short scan_ds1775[] = {
309 		0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
310 		I2C_CLIENT_END
311 	};
312 	static const unsigned short scan_adm1030[] = {
313 		0x2c, 0x2d, 0x2e, 0x2f,
314 		I2C_CLIENT_END
315 	};
316 
317 	if (x.running || strncmp(adapter->name, "uni-n", 5))
318 		return;
319 
320 	np = of_find_compatible_node(adapter->dev.of_node, NULL, "MAC,ds1775");
321 	if (np) {
322 		of_node_put(np);
323 	} else {
324 		strlcpy(info.type, "MAC,ds1775", I2C_NAME_SIZE);
325 		i2c_new_scanned_device(adapter, &info, scan_ds1775, NULL);
326 	}
327 
328 	np = of_find_compatible_node(adapter->dev.of_node, NULL, "MAC,adm1030");
329 	if (np) {
330 		of_node_put(np);
331 	} else {
332 		strlcpy(info.type, "MAC,adm1030", I2C_NAME_SIZE);
333 		i2c_new_scanned_device(adapter, &info, scan_adm1030, NULL);
334 	}
335 }
336 
337 static void
338 do_remove(struct i2c_client *client)
339 {
340 	if (x.running) {
341 		x.running = 0;
342 		kthread_stop(x.poll_task);
343 		x.poll_task = NULL;
344 	}
345 	if (client == x.thermostat)
346 		x.thermostat = NULL;
347 	else if (client == x.fan)
348 		x.fan = NULL;
349 	else
350 		printk(KERN_ERR "g4fan: bad client\n");
351 }
352 
353 static int
354 attach_fan( struct i2c_client *cl )
355 {
356 	if( x.fan )
357 		goto out;
358 
359 	/* check that this is an ADM1030 */
360 	if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
361 		goto out;
362 	printk("ADM1030 fan controller [@%02x]\n", cl->addr );
363 
364 	x.fan = cl;
365  out:
366 	return 0;
367 }
368 
369 static int
370 attach_thermostat( struct i2c_client *cl )
371 {
372 	int hyst_temp, os_temp, temp;
373 
374 	if( x.thermostat )
375 		goto out;
376 
377 	if( (temp=read_reg(cl, 0, 2)) < 0 )
378 		goto out;
379 
380 	/* temperature sanity check */
381 	if( temp < 0x1600 || temp > 0x3c00 )
382 		goto out;
383 	hyst_temp = read_reg(cl, 2, 2);
384 	os_temp = read_reg(cl, 3, 2);
385 	if( hyst_temp < 0 || os_temp < 0 )
386 		goto out;
387 
388 	printk("DS1775 digital thermometer [@%02x]\n", cl->addr );
389 	print_temp("Temp: ", temp );
390 	print_temp("  Hyst: ", hyst_temp );
391 	print_temp("  OS: ", os_temp );
392 	printk("\n");
393 
394 	x.temp = temp;
395 	x.overheat_temp = os_temp;
396 	x.overheat_hyst = hyst_temp;
397 	x.thermostat = cl;
398 out:
399 	return 0;
400 }
401 
402 enum chip { ds1775, adm1030 };
403 
404 static const struct i2c_device_id therm_windtunnel_id[] = {
405 	{ "MAC,ds1775", ds1775 },
406 	{ "MAC,adm1030", adm1030 },
407 	{ }
408 };
409 MODULE_DEVICE_TABLE(i2c, therm_windtunnel_id);
410 
411 static int
412 do_probe(struct i2c_client *cl, const struct i2c_device_id *id)
413 {
414 	struct i2c_adapter *adapter = cl->adapter;
415 	int ret = 0;
416 
417 	if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
418 				     | I2C_FUNC_SMBUS_WRITE_BYTE) )
419 		return 0;
420 
421 	switch (id->driver_data) {
422 	case adm1030:
423 		ret = attach_fan(cl);
424 		break;
425 	case ds1775:
426 		ret = attach_thermostat(cl);
427 		break;
428 	}
429 
430 	if (!x.running && x.thermostat && x.fan) {
431 		x.running = 1;
432 		x.poll_task = kthread_run(control_loop, NULL, "g4fand");
433 	}
434 
435 	return ret;
436 }
437 
438 static struct i2c_driver g4fan_driver = {
439 	.driver = {
440 		.name	= "therm_windtunnel",
441 	},
442 	.probe		= do_probe,
443 	.remove		= do_remove,
444 	.id_table	= therm_windtunnel_id,
445 };
446 
447 
448 /************************************************************************/
449 /*	initialization / cleanup					*/
450 /************************************************************************/
451 
452 static int therm_of_probe(struct platform_device *dev)
453 {
454 	struct i2c_adapter *adap;
455 	int ret, i = 0;
456 
457 	adap = i2c_get_adapter(0);
458 	if (!adap)
459 		return -EPROBE_DEFER;
460 
461 	ret = i2c_add_driver(&g4fan_driver);
462 	if (ret) {
463 		i2c_put_adapter(adap);
464 		return ret;
465 	}
466 
467 	/* We assume Macs have consecutive I2C bus numbers starting at 0 */
468 	while (adap) {
469 		do_attach(adap);
470 		if (x.running)
471 			return 0;
472 		i2c_put_adapter(adap);
473 		adap = i2c_get_adapter(++i);
474 	}
475 
476 	return -ENODEV;
477 }
478 
479 static int
480 therm_of_remove( struct platform_device *dev )
481 {
482 	i2c_del_driver( &g4fan_driver );
483 	return 0;
484 }
485 
486 static const struct of_device_id therm_of_match[] = {{
487 	.name		= "fan",
488 	.compatible	= "adm1030"
489     }, {}
490 };
491 MODULE_DEVICE_TABLE(of, therm_of_match);
492 
493 static struct platform_driver therm_of_driver = {
494 	.driver = {
495 		.name = "temperature",
496 		.of_match_table = therm_of_match,
497 	},
498 	.probe		= therm_of_probe,
499 	.remove		= therm_of_remove,
500 };
501 
502 struct apple_thermal_info {
503 	u8		id;			/* implementation ID */
504 	u8		fan_count;		/* number of fans */
505 	u8		thermostat_count;	/* number of thermostats */
506 	u8		unused;
507 };
508 
509 static int __init
510 g4fan_init( void )
511 {
512 	const struct apple_thermal_info *info;
513 	struct device_node *np;
514 
515 	mutex_init(&x.lock);
516 
517 	if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
518 		return -ENODEV;
519 	info = of_get_property(np, "thermal-info", NULL);
520 	of_node_put(np);
521 
522 	if( !info || !of_machine_is_compatible("PowerMac3,6") )
523 		return -ENODEV;
524 
525 	if( info->id != 3 ) {
526 		printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
527 		return -ENODEV;
528 	}
529 	if( !(np=of_find_node_by_name(NULL, "fan")) )
530 		return -ENODEV;
531 	x.of_dev = of_platform_device_create(np, "temperature", NULL);
532 	of_node_put( np );
533 
534 	if( !x.of_dev ) {
535 		printk(KERN_ERR "Can't register fan controller!\n");
536 		return -ENODEV;
537 	}
538 
539 	platform_driver_register( &therm_of_driver );
540 	return 0;
541 }
542 
543 static void __exit
544 g4fan_exit( void )
545 {
546 	platform_driver_unregister( &therm_of_driver );
547 
548 	if( x.of_dev )
549 		of_device_unregister( x.of_dev );
550 }
551 
552 module_init(g4fan_init);
553 module_exit(g4fan_exit);
554 
555 MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
556 MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
557 MODULE_LICENSE("GPL");
558