| 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 | 
|  | 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 |  | 
| 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> | 
|  | 37 | #include <linux/slab.h> | 
|  | 38 | #include <linux/init.h> | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 39 | #include <linux/kthread.h> | 
| Stephen Rothwell | ad9e05a | 2008-05-23 16:27:02 +1000 | [diff] [blame] | 40 | #include <linux/of_platform.h> | 
| Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 41 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/prom.h> | 
|  | 43 | #include <asm/machdep.h> | 
|  | 44 | #include <asm/io.h> | 
|  | 45 | #include <asm/system.h> | 
|  | 46 | #include <asm/sections.h> | 
| Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 47 | #include <asm/macio.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | #define LOG_TEMP		0			/* continously log temperature */ | 
|  | 50 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static struct { | 
|  | 52 | volatile int		running; | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 53 | struct task_struct	*poll_task; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 55 | struct mutex	 	lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct of_device	*of_dev; | 
|  | 57 |  | 
|  | 58 | struct i2c_client	*thermostat; | 
|  | 59 | struct i2c_client	*fan; | 
|  | 60 |  | 
|  | 61 | int			overheat_temp;		/* 100% fan at this temp */ | 
|  | 62 | int			overheat_hyst; | 
|  | 63 | int			temp; | 
|  | 64 | int			casetemp; | 
|  | 65 | int			fan_level;		/* active fan_table setting */ | 
|  | 66 |  | 
|  | 67 | int			downind; | 
|  | 68 | int			upind; | 
|  | 69 |  | 
|  | 70 | int			r0, r1, r20, r23, r25;	/* saved register */ | 
|  | 71 | } x; | 
|  | 72 |  | 
|  | 73 | #define T(x,y)			(((x)<<8) | (y)*0x100/10 ) | 
|  | 74 |  | 
|  | 75 | static struct { | 
|  | 76 | int			fan_down_setting; | 
|  | 77 | int			temp; | 
|  | 78 | int			fan_up_setting; | 
|  | 79 | } fan_table[] = { | 
|  | 80 | { 11, T(0,0),  11 },	/* min fan */ | 
|  | 81 | { 11, T(55,0), 11 }, | 
|  | 82 | {  6, T(55,3), 11 }, | 
|  | 83 | {  7, T(56,0), 11 }, | 
|  | 84 | {  8, T(57,0), 8 }, | 
|  | 85 | {  7, T(58,3), 7 }, | 
|  | 86 | {  6, T(58,8), 6 }, | 
|  | 87 | {  5, T(59,2), 5 }, | 
|  | 88 | {  4, T(59,6), 4 }, | 
|  | 89 | {  3, T(59,9), 3 }, | 
|  | 90 | {  2, T(60,1), 2 }, | 
|  | 91 | {  1, 0xfffff, 1 }	/* on fire */ | 
|  | 92 | }; | 
|  | 93 |  | 
|  | 94 | static void | 
|  | 95 | print_temp( const char *s, int temp ) | 
|  | 96 | { | 
|  | 97 | printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 ); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static ssize_t | 
| Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 101 | show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { | 
|  | 103 | return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 ); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | static ssize_t | 
| Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 107 | show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { | 
|  | 109 | return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 ); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL ); | 
|  | 113 | static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL ); | 
|  | 114 |  | 
|  | 115 |  | 
|  | 116 |  | 
|  | 117 | /************************************************************************/ | 
|  | 118 | /*	controller thread						*/ | 
|  | 119 | /************************************************************************/ | 
|  | 120 |  | 
|  | 121 | static int | 
|  | 122 | write_reg( struct i2c_client *cl, int reg, int data, int len ) | 
|  | 123 | { | 
|  | 124 | u8 tmp[3]; | 
|  | 125 |  | 
|  | 126 | if( len < 1 || len > 2 || data < 0 ) | 
|  | 127 | return -EINVAL; | 
|  | 128 |  | 
|  | 129 | tmp[0] = reg; | 
|  | 130 | tmp[1] = (len == 1) ? data : (data >> 8); | 
|  | 131 | tmp[2] = data; | 
|  | 132 | len++; | 
|  | 133 |  | 
|  | 134 | if( i2c_master_send(cl, tmp, len) != len ) | 
|  | 135 | return -ENODEV; | 
|  | 136 | return 0; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | static int | 
|  | 140 | read_reg( struct i2c_client *cl, int reg, int len ) | 
|  | 141 | { | 
|  | 142 | u8 buf[2]; | 
|  | 143 |  | 
|  | 144 | if( len != 1 && len != 2 ) | 
|  | 145 | return -EINVAL; | 
|  | 146 | buf[0] = reg; | 
|  | 147 | if( i2c_master_send(cl, buf, 1) != 1 ) | 
|  | 148 | return -ENODEV; | 
|  | 149 | if( i2c_master_recv(cl, buf, len) != len ) | 
|  | 150 | return -ENODEV; | 
|  | 151 | return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0]; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static void | 
|  | 155 | tune_fan( int fan_setting ) | 
|  | 156 | { | 
|  | 157 | int val = (fan_setting << 3) | 7; | 
|  | 158 |  | 
|  | 159 | /* write_reg( x.fan, 0x24, val, 1 ); */ | 
|  | 160 | write_reg( x.fan, 0x25, val, 1 ); | 
|  | 161 | write_reg( x.fan, 0x20, 0, 1 ); | 
|  | 162 | print_temp("CPU-temp: ", x.temp ); | 
|  | 163 | if( x.casetemp ) | 
|  | 164 | print_temp(", Case: ", x.casetemp ); | 
|  | 165 | printk(",  Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting ); | 
|  | 166 |  | 
|  | 167 | x.fan_level = fan_setting; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | static void | 
|  | 171 | poll_temp( void ) | 
|  | 172 | { | 
|  | 173 | int temp, i, level, casetemp; | 
|  | 174 |  | 
|  | 175 | temp = read_reg( x.thermostat, 0, 2 ); | 
|  | 176 |  | 
|  | 177 | /* this actually occurs when the computer is loaded */ | 
|  | 178 | if( temp < 0 ) | 
|  | 179 | return; | 
|  | 180 |  | 
|  | 181 | casetemp = read_reg(x.fan, 0x0b, 1) << 8; | 
|  | 182 | casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5; | 
|  | 183 |  | 
|  | 184 | if( LOG_TEMP && x.temp != temp ) { | 
|  | 185 | print_temp("CPU-temp: ", temp ); | 
|  | 186 | print_temp(", Case: ", casetemp ); | 
|  | 187 | printk(",  Fan: %d\n", 11-x.fan_level ); | 
|  | 188 | } | 
|  | 189 | x.temp = temp; | 
|  | 190 | x.casetemp = casetemp; | 
|  | 191 |  | 
|  | 192 | level = -1; | 
|  | 193 | for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ ) | 
|  | 194 | ; | 
|  | 195 | if( i < x.downind ) | 
|  | 196 | level = fan_table[i].fan_down_setting; | 
|  | 197 | x.downind = i; | 
|  | 198 |  | 
|  | 199 | for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ ) | 
|  | 200 | ; | 
|  | 201 | if( x.upind < i ) | 
|  | 202 | level = fan_table[i].fan_up_setting; | 
|  | 203 | x.upind = i; | 
|  | 204 |  | 
|  | 205 | if( level >= 0 ) | 
|  | 206 | tune_fan( level ); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 |  | 
|  | 210 | static void | 
|  | 211 | setup_hardware( void ) | 
|  | 212 | { | 
|  | 213 | int val; | 
| Stephen Rothwell | 98894df | 2008-01-03 15:15:28 +1100 | [diff] [blame] | 214 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 |  | 
|  | 216 | /* save registers (if we unload the module) */ | 
|  | 217 | x.r0 = read_reg( x.fan, 0x00, 1 ); | 
|  | 218 | x.r1 = read_reg( x.fan, 0x01, 1 ); | 
|  | 219 | x.r20 = read_reg( x.fan, 0x20, 1 ); | 
|  | 220 | x.r23 = read_reg( x.fan, 0x23, 1 ); | 
|  | 221 | x.r25 = read_reg( x.fan, 0x25, 1 ); | 
|  | 222 |  | 
|  | 223 | /* improve measurement resolution (convergence time 1.5s) */ | 
|  | 224 | if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) { | 
|  | 225 | val |= 0x60; | 
|  | 226 | if( write_reg( x.thermostat, 1, val, 1 ) ) | 
|  | 227 | printk("Failed writing config register\n"); | 
|  | 228 | } | 
|  | 229 | /* disable interrupts and TAC input */ | 
|  | 230 | write_reg( x.fan, 0x01, 0x01, 1 ); | 
|  | 231 | /* enable filter */ | 
|  | 232 | write_reg( x.fan, 0x23, 0x91, 1 ); | 
|  | 233 | /* remote temp. controls fan */ | 
|  | 234 | write_reg( x.fan, 0x00, 0x95, 1 ); | 
|  | 235 |  | 
|  | 236 | /* The thermostat (which besides measureing temperature controls | 
|  | 237 | * has a THERM output which puts the fan on 100%) is usually | 
|  | 238 | * set to kick in at 80 C (chip default). We reduce this a bit | 
|  | 239 | * to be on the safe side (OSX doesn't)... | 
|  | 240 | */ | 
|  | 241 | if( x.overheat_temp == (80 << 8) ) { | 
| Lyonel Vincent | b8e4a7d | 2009-08-30 08:54:20 +0000 | [diff] [blame] | 242 | x.overheat_temp = 75 << 8; | 
|  | 243 | x.overheat_hyst = 70 << 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | write_reg( x.thermostat, 2, x.overheat_hyst, 2 ); | 
|  | 245 | write_reg( x.thermostat, 3, x.overheat_temp, 2 ); | 
|  | 246 |  | 
|  | 247 | print_temp("Reducing overheating limit to ", x.overheat_temp ); | 
|  | 248 | print_temp(" (Hyst: ", x.overheat_hyst ); | 
|  | 249 | printk(")\n"); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | /* set an initial fan setting */ | 
|  | 253 | x.downind = 0xffff; | 
|  | 254 | x.upind = -1; | 
|  | 255 | /* tune_fan( fan_up_table[x.upind].fan_setting ); */ | 
|  | 256 |  | 
| Stephen Rothwell | 98894df | 2008-01-03 15:15:28 +1100 | [diff] [blame] | 257 | err = device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature ); | 
|  | 258 | err |= device_create_file( &x.of_dev->dev, &dev_attr_case_temperature ); | 
|  | 259 | if (err) | 
|  | 260 | printk(KERN_WARNING | 
|  | 261 | "Failed to create temperature attribute file(s).\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
|  | 264 | static void | 
|  | 265 | restore_regs( void ) | 
|  | 266 | { | 
|  | 267 | device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature ); | 
|  | 268 | device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature ); | 
|  | 269 |  | 
|  | 270 | write_reg( x.fan, 0x01, x.r1, 1 ); | 
|  | 271 | write_reg( x.fan, 0x20, x.r20, 1 ); | 
|  | 272 | write_reg( x.fan, 0x23, x.r23, 1 ); | 
|  | 273 | write_reg( x.fan, 0x25, x.r25, 1 ); | 
|  | 274 | write_reg( x.fan, 0x00, x.r0, 1 ); | 
|  | 275 | } | 
|  | 276 |  | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 277 | static int control_loop(void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 279 | mutex_lock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | setup_hardware(); | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 281 | mutex_unlock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 |  | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 283 | for (;;) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | msleep_interruptible(8000); | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 285 | if (kthread_should_stop()) | 
|  | 286 | break; | 
|  | 287 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 288 | mutex_lock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | poll_temp(); | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 290 | mutex_unlock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } | 
|  | 292 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 293 | mutex_lock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | restore_regs(); | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 295 | mutex_unlock(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 |  | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 297 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
|  | 300 |  | 
|  | 301 | /************************************************************************/ | 
|  | 302 | /*	i2c probing and setup						*/ | 
|  | 303 | /************************************************************************/ | 
|  | 304 |  | 
|  | 305 | static int | 
|  | 306 | do_attach( struct i2c_adapter *adapter ) | 
|  | 307 | { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 308 | /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */ | 
|  | 309 | static const unsigned short scan_ds1775[] = { | 
|  | 310 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, | 
|  | 311 | I2C_CLIENT_END | 
|  | 312 | }; | 
|  | 313 | static const unsigned short scan_adm1030[] = { | 
|  | 314 | 0x2c, 0x2d, 0x2e, 0x2f, | 
|  | 315 | I2C_CLIENT_END | 
|  | 316 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 |  | 
|  | 318 | if( strncmp(adapter->name, "uni-n", 5) ) | 
|  | 319 | return 0; | 
|  | 320 |  | 
|  | 321 | if( !x.running ) { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 322 | struct i2c_board_info info; | 
|  | 323 |  | 
|  | 324 | memset(&info, 0, sizeof(struct i2c_board_info)); | 
|  | 325 | strlcpy(info.type, "therm_ds1775", I2C_NAME_SIZE); | 
|  | 326 | i2c_new_probed_device(adapter, &info, scan_ds1775); | 
|  | 327 |  | 
|  | 328 | strlcpy(info.type, "therm_adm1030", I2C_NAME_SIZE); | 
|  | 329 | i2c_new_probed_device(adapter, &info, scan_adm1030); | 
|  | 330 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if( x.thermostat && x.fan ) { | 
|  | 332 | x.running = 1; | 
| Paul Mackerras | 98f6740 | 2007-12-13 15:57:45 +1100 | [diff] [blame] | 333 | x.poll_task = kthread_run(control_loop, NULL, "g4fand"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } | 
|  | 335 | } | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 336 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
|  | 339 | static int | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 340 | do_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 342 | if (x.running) { | 
|  | 343 | x.running = 0; | 
|  | 344 | kthread_stop(x.poll_task); | 
|  | 345 | x.poll_task = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 347 | if (client == x.thermostat) | 
|  | 348 | x.thermostat = NULL; | 
|  | 349 | else if (client == x.fan) | 
|  | 350 | x.fan = NULL; | 
|  | 351 | else | 
|  | 352 | printk(KERN_ERR "g4fan: bad client\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 354 | return 0; | 
|  | 355 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 |  | 
|  | 357 | static int | 
|  | 358 | attach_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 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 368 | x.fan = cl; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return 0; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | static int | 
|  | 374 | attach_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; | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 401 | x.thermostat = cl; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return 0; | 
|  | 404 | } | 
|  | 405 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 406 | enum chip { ds1775, adm1030 }; | 
|  | 407 |  | 
|  | 408 | static const struct i2c_device_id therm_windtunnel_id[] = { | 
|  | 409 | { "therm_ds1775", ds1775 }, | 
|  | 410 | { "therm_adm1030", adm1030 }, | 
|  | 411 | { } | 
|  | 412 | }; | 
|  | 413 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | static int | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 415 | do_probe(struct i2c_client *cl, const struct i2c_device_id *id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 417 | struct i2c_adapter *adapter = cl->adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 |  | 
|  | 419 | if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA | 
|  | 420 | | I2C_FUNC_SMBUS_WRITE_BYTE) ) | 
|  | 421 | return 0; | 
|  | 422 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 423 | switch (id->driver_data) { | 
|  | 424 | case adm1030: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return attach_fan( cl ); | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 426 | case ds1775: | 
|  | 427 | return attach_thermostat(cl); | 
|  | 428 | } | 
|  | 429 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
| Jean Delvare | 036533e | 2009-06-15 18:01:52 +0200 | [diff] [blame] | 432 | static struct i2c_driver g4fan_driver = { | 
|  | 433 | .driver = { | 
|  | 434 | .name	= "therm_windtunnel", | 
|  | 435 | }, | 
|  | 436 | .attach_adapter = do_attach, | 
|  | 437 | .probe		= do_probe, | 
|  | 438 | .remove		= do_remove, | 
|  | 439 | .id_table	= therm_windtunnel_id, | 
|  | 440 | }; | 
|  | 441 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 |  | 
|  | 443 | /************************************************************************/ | 
|  | 444 | /*	initialization / cleanup					*/ | 
|  | 445 | /************************************************************************/ | 
|  | 446 |  | 
|  | 447 | static int | 
| Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 448 | therm_of_probe( struct of_device *dev, const struct of_device_id *match ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { | 
|  | 450 | return i2c_add_driver( &g4fan_driver ); | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | static int | 
|  | 454 | therm_of_remove( struct of_device *dev ) | 
|  | 455 | { | 
| Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 456 | i2c_del_driver( &g4fan_driver ); | 
|  | 457 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
| Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 460 | static struct of_device_id therm_of_match[] = {{ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | .name		= "fan", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | .compatible	= "adm1030" | 
|  | 463 | }, {} | 
|  | 464 | }; | 
|  | 465 |  | 
|  | 466 | static struct of_platform_driver therm_of_driver = { | 
|  | 467 | .name		= "temperature", | 
|  | 468 | .match_table	= therm_of_match, | 
|  | 469 | .probe		= therm_of_probe, | 
|  | 470 | .remove		= therm_of_remove, | 
|  | 471 | }; | 
|  | 472 |  | 
|  | 473 | struct apple_thermal_info { | 
|  | 474 | u8		id;			/* implementation ID */ | 
|  | 475 | u8		fan_count;		/* number of fans */ | 
|  | 476 | u8		thermostat_count;	/* number of thermostats */ | 
|  | 477 | u8		unused; | 
|  | 478 | }; | 
|  | 479 |  | 
|  | 480 | static int __init | 
|  | 481 | g4fan_init( void ) | 
|  | 482 | { | 
| Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 483 | const struct apple_thermal_info *info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | struct device_node *np; | 
|  | 485 |  | 
| Daniel Walker | 1baaeea | 2008-06-10 09:26:08 +1000 | [diff] [blame] | 486 | mutex_init(&x.lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 |  | 
|  | 488 | if( !(np=of_find_node_by_name(NULL, "power-mgt")) ) | 
|  | 489 | return -ENODEV; | 
| Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 490 | info = of_get_property(np, "thermal-info", NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | of_node_put(np); | 
|  | 492 |  | 
|  | 493 | if( !info || !machine_is_compatible("PowerMac3,6") ) | 
|  | 494 | return -ENODEV; | 
|  | 495 |  | 
|  | 496 | if( info->id != 3 ) { | 
|  | 497 | printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id ); | 
|  | 498 | return -ENODEV; | 
|  | 499 | } | 
|  | 500 | if( !(np=of_find_node_by_name(NULL, "fan")) ) | 
|  | 501 | return -ENODEV; | 
| Benjamin Herrenschmidt | 0365ba7 | 2005-09-22 21:44:06 -0700 | [diff] [blame] | 502 | x.of_dev = of_platform_device_create(np, "temperature", NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | of_node_put( np ); | 
|  | 504 |  | 
|  | 505 | if( !x.of_dev ) { | 
|  | 506 | printk(KERN_ERR "Can't register fan controller!\n"); | 
|  | 507 | return -ENODEV; | 
|  | 508 | } | 
|  | 509 |  | 
| Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 510 | of_register_platform_driver( &therm_of_driver ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return 0; | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | static void __exit | 
|  | 515 | g4fan_exit( void ) | 
|  | 516 | { | 
| Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 517 | of_unregister_platform_driver( &therm_of_driver ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 |  | 
|  | 519 | if( x.of_dev ) | 
|  | 520 | of_device_unregister( x.of_dev ); | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | module_init(g4fan_init); | 
|  | 524 | module_exit(g4fan_exit); | 
|  | 525 |  | 
|  | 526 | MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>"); | 
|  | 527 | MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller"); | 
|  | 528 | MODULE_LICENSE("GPL"); |