| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 18 |  *	temperature around 57 C. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 38 | #include <linux/kthread.h> | 
| Stephen Rothwell | ad9e05a | 2008-05-23 16:27:02 +1000 | [diff] [blame] | 39 | #include <linux/of_platform.h> | 
| Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 40 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 46 | #include <asm/macio.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 48 | #define LOG_TEMP		0			/* continuously log temperature */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static struct { | 
 | 51 | 	volatile int		running; | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 52 | 	struct task_struct	*poll_task; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | 	 | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 54 | 	struct mutex	 	lock; | 
| Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 55 | 	struct platform_device	*of_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
 | 74 | static 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 |  | 
 | 93 | static void | 
 | 94 | print_temp( const char *s, int temp ) | 
 | 95 | { | 
 | 96 | 	printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 ); | 
 | 97 | } | 
 | 98 |  | 
 | 99 | static ssize_t | 
| Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 100 | show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { | 
 | 102 | 	return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 ); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | static ssize_t | 
| Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 106 | show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { | 
 | 108 | 	return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 ); | 
 | 109 | } | 
 | 110 |  | 
 | 111 | static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL ); | 
 | 112 | static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL ); | 
 | 113 |  | 
 | 114 |  | 
 | 115 |  | 
 | 116 | /************************************************************************/ | 
 | 117 | /*	controller thread						*/ | 
 | 118 | /************************************************************************/ | 
 | 119 |  | 
 | 120 | static int | 
 | 121 | write_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 |  | 
 | 138 | static int | 
 | 139 | read_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 |  | 
 | 153 | static void | 
 | 154 | tune_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 |  | 
 | 169 | static void | 
 | 170 | poll_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 |  | 
 | 209 | static void | 
 | 210 | setup_hardware( void ) | 
 | 211 | { | 
 | 212 | 	int val; | 
| Stephen Rothwell | 98894df | 2008-01-03 15:15:28 +1100 | [diff] [blame] | 213 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) ) { | 
| Lyonel Vincent | b8e4a7d | 2009-08-30 08:54:20 +0000 | [diff] [blame] | 241 | 		x.overheat_temp = 75 << 8; | 
 | 242 | 		x.overheat_hyst = 70 << 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Stephen Rothwell | 98894df | 2008-01-03 15:15:28 +1100 | [diff] [blame] | 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"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } | 
 | 262 |  | 
 | 263 | static void | 
 | 264 | restore_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 |  | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 276 | static int control_loop(void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 278 | 	mutex_lock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | 	setup_hardware(); | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 280 | 	mutex_unlock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 |  | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 282 | 	for (;;) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 		msleep_interruptible(8000); | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 284 | 		if (kthread_should_stop()) | 
 | 285 | 			break; | 
 | 286 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 287 | 		mutex_lock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | 		poll_temp(); | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 289 | 		mutex_unlock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 	} | 
 | 291 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 292 | 	mutex_lock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | 	restore_regs(); | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 294 | 	mutex_unlock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 |  | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 296 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } | 
 | 298 |  | 
 | 299 |  | 
 | 300 | /************************************************************************/ | 
 | 301 | /*	i2c probing and setup						*/ | 
 | 302 | /************************************************************************/ | 
 | 303 |  | 
 | 304 | static int | 
 | 305 | do_attach( struct i2c_adapter *adapter ) | 
 | 306 | { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 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 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 |  | 
 | 317 | 	if( strncmp(adapter->name, "uni-n", 5) ) | 
 | 318 | 		return 0; | 
 | 319 |  | 
 | 320 | 	if( !x.running ) { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 321 | 		struct i2c_board_info info; | 
 | 322 |  | 
 | 323 | 		memset(&info, 0, sizeof(struct i2c_board_info)); | 
 | 324 | 		strlcpy(info.type, "therm_ds1775", I2C_NAME_SIZE); | 
| Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 325 | 		i2c_new_probed_device(adapter, &info, scan_ds1775, NULL); | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 326 |  | 
 | 327 | 		strlcpy(info.type, "therm_adm1030", I2C_NAME_SIZE); | 
| Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 328 | 		i2c_new_probed_device(adapter, &info, scan_adm1030, NULL); | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 329 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 		if( x.thermostat && x.fan ) { | 
 | 331 | 			x.running = 1; | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 332 | 			x.poll_task = kthread_run(control_loop, NULL, "g4fand"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | 		} | 
 | 334 | 	} | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 335 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } | 
 | 337 |  | 
 | 338 | static int | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 339 | do_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 341 | 	if (x.running) { | 
 | 342 | 		x.running = 0; | 
 | 343 | 		kthread_stop(x.poll_task); | 
 | 344 | 		x.poll_task = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | 	} | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 346 | 	if (client == x.thermostat) | 
 | 347 | 		x.thermostat = NULL; | 
 | 348 | 	else if (client == x.fan) | 
 | 349 | 		x.fan = NULL; | 
 | 350 | 	else | 
 | 351 | 		printk(KERN_ERR "g4fan: bad client\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 353 | 	return 0; | 
 | 354 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 |  | 
 | 356 | static int | 
 | 357 | attach_fan( struct i2c_client *cl ) | 
 | 358 | { | 
 | 359 | 	if( x.fan ) | 
 | 360 | 		goto out; | 
 | 361 |  | 
 | 362 | 	/* check that this is an ADM1030 */ | 
 | 363 | 	if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 ) | 
 | 364 | 		goto out; | 
 | 365 | 	printk("ADM1030 fan controller [@%02x]\n", cl->addr ); | 
 | 366 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 367 | 	x.fan = cl; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 |  out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | 	return 0; | 
 | 370 | } | 
 | 371 |  | 
 | 372 | static int | 
 | 373 | attach_thermostat( struct i2c_client *cl )  | 
 | 374 | { | 
 | 375 | 	int hyst_temp, os_temp, temp; | 
 | 376 |  | 
 | 377 | 	if( x.thermostat ) | 
 | 378 | 		goto out; | 
 | 379 |  | 
 | 380 | 	if( (temp=read_reg(cl, 0, 2)) < 0 ) | 
 | 381 | 		goto out; | 
 | 382 | 	 | 
 | 383 | 	/* temperature sanity check */ | 
 | 384 | 	if( temp < 0x1600 || temp > 0x3c00 ) | 
 | 385 | 		goto out; | 
 | 386 | 	hyst_temp = read_reg(cl, 2, 2); | 
 | 387 | 	os_temp = read_reg(cl, 3, 2); | 
 | 388 | 	if( hyst_temp < 0 || os_temp < 0 ) | 
 | 389 | 		goto out; | 
 | 390 |  | 
 | 391 | 	printk("DS1775 digital thermometer [@%02x]\n", cl->addr ); | 
 | 392 | 	print_temp("Temp: ", temp ); | 
 | 393 | 	print_temp("  Hyst: ", hyst_temp ); | 
 | 394 | 	print_temp("  OS: ", os_temp ); | 
 | 395 | 	printk("\n"); | 
 | 396 |  | 
 | 397 | 	x.temp = temp; | 
 | 398 | 	x.overheat_temp = os_temp; | 
 | 399 | 	x.overheat_hyst = hyst_temp; | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 400 | 	x.thermostat = cl; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | 	return 0; | 
 | 403 | } | 
 | 404 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 405 | enum chip { ds1775, adm1030 }; | 
 | 406 |  | 
 | 407 | static const struct i2c_device_id therm_windtunnel_id[] = { | 
 | 408 | 	{ "therm_ds1775", ds1775 }, | 
 | 409 | 	{ "therm_adm1030", adm1030 }, | 
 | 410 | 	{ } | 
 | 411 | }; | 
 | 412 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | static int | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 414 | do_probe(struct i2c_client *cl, const struct i2c_device_id *id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 416 | 	struct i2c_adapter *adapter = cl->adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 |  | 
 | 418 | 	if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA | 
 | 419 | 				     | I2C_FUNC_SMBUS_WRITE_BYTE) ) | 
 | 420 | 		return 0; | 
 | 421 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 422 | 	switch (id->driver_data) { | 
 | 423 | 	case adm1030: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | 		return attach_fan( cl ); | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 425 | 	case ds1775: | 
 | 426 | 		return attach_thermostat(cl); | 
 | 427 | 	} | 
 | 428 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } | 
 | 430 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 431 | static struct i2c_driver g4fan_driver = { | 
 | 432 | 	.driver = { | 
 | 433 | 		.name	= "therm_windtunnel", | 
 | 434 | 	}, | 
 | 435 | 	.attach_adapter = do_attach, | 
 | 436 | 	.probe		= do_probe, | 
 | 437 | 	.remove		= do_remove, | 
 | 438 | 	.id_table	= therm_windtunnel_id, | 
 | 439 | }; | 
 | 440 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 |  | 
 | 442 | /************************************************************************/ | 
 | 443 | /*	initialization / cleanup					*/ | 
 | 444 | /************************************************************************/ | 
 | 445 |  | 
| Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 446 | static int therm_of_probe(struct platform_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { | 
 | 448 | 	return i2c_add_driver( &g4fan_driver ); | 
 | 449 | } | 
 | 450 |  | 
 | 451 | static int | 
| Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 452 | therm_of_remove( struct platform_device *dev ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { | 
| Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 454 | 	i2c_del_driver( &g4fan_driver ); | 
 | 455 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } | 
 | 457 |  | 
| Márton Németh | 46759a7 | 2010-01-10 01:43:14 +0000 | [diff] [blame] | 458 | static const struct of_device_id therm_of_match[] = {{ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | 	.name		= "fan", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | 	.compatible	= "adm1030" | 
 | 461 |     }, {} | 
 | 462 | }; | 
 | 463 |  | 
| Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 464 | static struct platform_driver therm_of_driver = { | 
| Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 465 | 	.driver = { | 
 | 466 | 		.name = "temperature", | 
 | 467 | 		.owner = THIS_MODULE, | 
 | 468 | 		.of_match_table = therm_of_match, | 
 | 469 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | 	.probe		= therm_of_probe, | 
 | 471 | 	.remove		= therm_of_remove, | 
 | 472 | }; | 
 | 473 |  | 
 | 474 | struct apple_thermal_info { | 
 | 475 | 	u8		id;			/* implementation ID */ | 
 | 476 | 	u8		fan_count;		/* number of fans */ | 
 | 477 | 	u8		thermostat_count;	/* number of thermostats */ | 
 | 478 | 	u8		unused; | 
 | 479 | }; | 
 | 480 |  | 
 | 481 | static int __init | 
 | 482 | g4fan_init( void ) | 
 | 483 | { | 
| Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 484 | 	const struct apple_thermal_info *info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | 	struct device_node *np; | 
 | 486 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 487 | 	mutex_init(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 |  | 
 | 489 | 	if( !(np=of_find_node_by_name(NULL, "power-mgt")) ) | 
 | 490 | 		return -ENODEV; | 
| Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 491 | 	info = of_get_property(np, "thermal-info", NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | 	of_node_put(np); | 
 | 493 |  | 
| Grant Likely | 71a157e8e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 494 | 	if( !info || !of_machine_is_compatible("PowerMac3,6") ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | 		return -ENODEV; | 
 | 496 |  | 
 | 497 | 	if( info->id != 3 ) { | 
 | 498 | 		printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id ); | 
 | 499 | 		return -ENODEV; | 
 | 500 | 	} | 
 | 501 | 	if( !(np=of_find_node_by_name(NULL, "fan")) ) | 
 | 502 | 		return -ENODEV; | 
| Benjamin Herrenschmidt | 0365ba7 | 2005-09-22 21:44:06 -0700 | [diff] [blame] | 503 | 	x.of_dev = of_platform_device_create(np, "temperature", NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | 	of_node_put( np ); | 
 | 505 |  | 
 | 506 | 	if( !x.of_dev ) { | 
 | 507 | 		printk(KERN_ERR "Can't register fan controller!\n"); | 
 | 508 | 		return -ENODEV; | 
 | 509 | 	} | 
 | 510 |  | 
| Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 511 | 	platform_driver_register( &therm_of_driver ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | 	return 0; | 
 | 513 | } | 
 | 514 |  | 
 | 515 | static void __exit | 
 | 516 | g4fan_exit( void ) | 
 | 517 | { | 
| Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 518 | 	platform_driver_unregister( &therm_of_driver ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 |  | 
 | 520 | 	if( x.of_dev ) | 
 | 521 | 		of_device_unregister( x.of_dev ); | 
 | 522 | } | 
 | 523 |  | 
 | 524 | module_init(g4fan_init); | 
 | 525 | module_exit(g4fan_exit); | 
 | 526 |  | 
 | 527 | MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>"); | 
 | 528 | MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller"); | 
 | 529 | MODULE_LICENSE("GPL"); |