Linux Audio

Check our new training course

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