therm_windtunnel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #include <linux/config.h>
  30. #include <linux/types.h>
  31. #include <linux/module.h>
  32. #include <linux/errno.h>
  33. #include <linux/kernel.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <linux/i2c.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <asm/prom.h>
  40. #include <asm/machdep.h>
  41. #include <asm/io.h>
  42. #include <asm/system.h>
  43. #include <asm/sections.h>
  44. #include <asm/of_device.h>
  45. #define LOG_TEMP 0 /* continously log temperature */
  46. #define I2C_DRIVERID_G4FAN 0x9001 /* fixme */
  47. static int do_probe( struct i2c_adapter *adapter, int addr, int kind);
  48. /* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
  49. static unsigned short normal_i2c[] = { 0x49, 0x2c, I2C_CLIENT_END };
  50. static unsigned short normal_i2c_range[] = { 0x48, 0x4f, 0x2c, 0x2f, I2C_CLIENT_END };
  51. I2C_CLIENT_INSMOD;
  52. static struct {
  53. volatile int running;
  54. struct completion completion;
  55. pid_t poll_task;
  56. struct semaphore lock;
  57. struct of_device *of_dev;
  58. struct i2c_client *thermostat;
  59. struct i2c_client *fan;
  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. int downind;
  66. int upind;
  67. int r0, r1, r20, r23, r25; /* saved register */
  68. } x;
  69. #define T(x,y) (((x)<<8) | (y)*0x100/10 )
  70. static struct {
  71. int fan_down_setting;
  72. int temp;
  73. int fan_up_setting;
  74. } fan_table[] = {
  75. { 11, T(0,0), 11 }, /* min fan */
  76. { 11, T(55,0), 11 },
  77. { 6, T(55,3), 11 },
  78. { 7, T(56,0), 11 },
  79. { 8, T(57,0), 8 },
  80. { 7, T(58,3), 7 },
  81. { 6, T(58,8), 6 },
  82. { 5, T(59,2), 5 },
  83. { 4, T(59,6), 4 },
  84. { 3, T(59,9), 3 },
  85. { 2, T(60,1), 2 },
  86. { 1, 0xfffff, 1 } /* on fire */
  87. };
  88. static void
  89. print_temp( const char *s, int temp )
  90. {
  91. printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
  92. }
  93. static ssize_t
  94. show_cpu_temperature( struct device *dev, char *buf )
  95. {
  96. return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
  97. }
  98. static ssize_t
  99. show_case_temperature( struct device *dev, char *buf )
  100. {
  101. return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
  102. }
  103. static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
  104. static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
  105. /************************************************************************/
  106. /* controller thread */
  107. /************************************************************************/
  108. static int
  109. write_reg( struct i2c_client *cl, int reg, int data, int len )
  110. {
  111. u8 tmp[3];
  112. if( len < 1 || len > 2 || data < 0 )
  113. return -EINVAL;
  114. tmp[0] = reg;
  115. tmp[1] = (len == 1) ? data : (data >> 8);
  116. tmp[2] = data;
  117. len++;
  118. if( i2c_master_send(cl, tmp, len) != len )
  119. return -ENODEV;
  120. return 0;
  121. }
  122. static int
  123. read_reg( struct i2c_client *cl, int reg, int len )
  124. {
  125. u8 buf[2];
  126. if( len != 1 && len != 2 )
  127. return -EINVAL;
  128. buf[0] = reg;
  129. if( i2c_master_send(cl, buf, 1) != 1 )
  130. return -ENODEV;
  131. if( i2c_master_recv(cl, buf, len) != len )
  132. return -ENODEV;
  133. return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
  134. }
  135. static void
  136. tune_fan( int fan_setting )
  137. {
  138. int val = (fan_setting << 3) | 7;
  139. /* write_reg( x.fan, 0x24, val, 1 ); */
  140. write_reg( x.fan, 0x25, val, 1 );
  141. write_reg( x.fan, 0x20, 0, 1 );
  142. print_temp("CPU-temp: ", x.temp );
  143. if( x.casetemp )
  144. print_temp(", Case: ", x.casetemp );
  145. printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
  146. x.fan_level = fan_setting;
  147. }
  148. static void
  149. poll_temp( void )
  150. {
  151. int temp, i, level, casetemp;
  152. temp = read_reg( x.thermostat, 0, 2 );
  153. /* this actually occurs when the computer is loaded */
  154. if( temp < 0 )
  155. return;
  156. casetemp = read_reg(x.fan, 0x0b, 1) << 8;
  157. casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
  158. if( LOG_TEMP && x.temp != temp ) {
  159. print_temp("CPU-temp: ", temp );
  160. print_temp(", Case: ", casetemp );
  161. printk(", Fan: %d\n", 11-x.fan_level );
  162. }
  163. x.temp = temp;
  164. x.casetemp = casetemp;
  165. level = -1;
  166. for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
  167. ;
  168. if( i < x.downind )
  169. level = fan_table[i].fan_down_setting;
  170. x.downind = i;
  171. for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
  172. ;
  173. if( x.upind < i )
  174. level = fan_table[i].fan_up_setting;
  175. x.upind = i;
  176. if( level >= 0 )
  177. tune_fan( level );
  178. }
  179. static void
  180. setup_hardware( void )
  181. {
  182. int val;
  183. /* save registers (if we unload the module) */
  184. x.r0 = read_reg( x.fan, 0x00, 1 );
  185. x.r1 = read_reg( x.fan, 0x01, 1 );
  186. x.r20 = read_reg( x.fan, 0x20, 1 );
  187. x.r23 = read_reg( x.fan, 0x23, 1 );
  188. x.r25 = read_reg( x.fan, 0x25, 1 );
  189. /* improve measurement resolution (convergence time 1.5s) */
  190. if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
  191. val |= 0x60;
  192. if( write_reg( x.thermostat, 1, val, 1 ) )
  193. printk("Failed writing config register\n");
  194. }
  195. /* disable interrupts and TAC input */
  196. write_reg( x.fan, 0x01, 0x01, 1 );
  197. /* enable filter */
  198. write_reg( x.fan, 0x23, 0x91, 1 );
  199. /* remote temp. controls fan */
  200. write_reg( x.fan, 0x00, 0x95, 1 );
  201. /* The thermostat (which besides measureing temperature controls
  202. * has a THERM output which puts the fan on 100%) is usually
  203. * set to kick in at 80 C (chip default). We reduce this a bit
  204. * to be on the safe side (OSX doesn't)...
  205. */
  206. if( x.overheat_temp == (80 << 8) ) {
  207. x.overheat_temp = 65 << 8;
  208. x.overheat_hyst = 60 << 8;
  209. write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
  210. write_reg( x.thermostat, 3, x.overheat_temp, 2 );
  211. print_temp("Reducing overheating limit to ", x.overheat_temp );
  212. print_temp(" (Hyst: ", x.overheat_hyst );
  213. printk(")\n");
  214. }
  215. /* set an initial fan setting */
  216. x.downind = 0xffff;
  217. x.upind = -1;
  218. /* tune_fan( fan_up_table[x.upind].fan_setting ); */
  219. device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
  220. device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
  221. }
  222. static void
  223. restore_regs( void )
  224. {
  225. device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
  226. device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
  227. write_reg( x.fan, 0x01, x.r1, 1 );
  228. write_reg( x.fan, 0x20, x.r20, 1 );
  229. write_reg( x.fan, 0x23, x.r23, 1 );
  230. write_reg( x.fan, 0x25, x.r25, 1 );
  231. write_reg( x.fan, 0x00, x.r0, 1 );
  232. }
  233. static int
  234. control_loop( void *dummy )
  235. {
  236. daemonize("g4fand");
  237. down( &x.lock );
  238. setup_hardware();
  239. while( x.running ) {
  240. up( &x.lock );
  241. msleep_interruptible(8000);
  242. down( &x.lock );
  243. poll_temp();
  244. }
  245. restore_regs();
  246. up( &x.lock );
  247. complete_and_exit( &x.completion, 0 );
  248. }
  249. /************************************************************************/
  250. /* i2c probing and setup */
  251. /************************************************************************/
  252. static int
  253. do_attach( struct i2c_adapter *adapter )
  254. {
  255. int ret = 0;
  256. if( strncmp(adapter->name, "uni-n", 5) )
  257. return 0;
  258. if( !x.running ) {
  259. ret = i2c_probe( adapter, &addr_data, &do_probe );
  260. if( x.thermostat && x.fan ) {
  261. x.running = 1;
  262. init_completion( &x.completion );
  263. x.poll_task = kernel_thread( control_loop, NULL, SIGCHLD | CLONE_KERNEL );
  264. }
  265. }
  266. return ret;
  267. }
  268. static int
  269. do_detach( struct i2c_client *client )
  270. {
  271. int err;
  272. if( (err=i2c_detach_client(client)) )
  273. printk(KERN_ERR "failed to detach thermostat client\n");
  274. else {
  275. if( x.running ) {
  276. x.running = 0;
  277. wait_for_completion( &x.completion );
  278. }
  279. if( client == x.thermostat )
  280. x.thermostat = NULL;
  281. else if( client == x.fan )
  282. x.fan = NULL;
  283. else {
  284. printk(KERN_ERR "g4fan: bad client\n");
  285. }
  286. kfree( client );
  287. }
  288. return err;
  289. }
  290. static struct i2c_driver g4fan_driver = {
  291. .owner = THIS_MODULE,
  292. .name = "therm_windtunnel",
  293. .id = I2C_DRIVERID_G4FAN,
  294. .flags = I2C_DF_NOTIFY,
  295. .attach_adapter = do_attach,
  296. .detach_client = do_detach,
  297. };
  298. static int
  299. attach_fan( struct i2c_client *cl )
  300. {
  301. if( x.fan )
  302. goto out;
  303. /* check that this is an ADM1030 */
  304. if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
  305. goto out;
  306. printk("ADM1030 fan controller [@%02x]\n", cl->addr );
  307. strlcpy( cl->name, "ADM1030 fan controller", sizeof(cl->name) );
  308. if( !i2c_attach_client(cl) )
  309. x.fan = cl;
  310. out:
  311. if( cl != x.fan )
  312. kfree( cl );
  313. return 0;
  314. }
  315. static int
  316. attach_thermostat( struct i2c_client *cl )
  317. {
  318. int hyst_temp, os_temp, temp;
  319. if( x.thermostat )
  320. goto out;
  321. if( (temp=read_reg(cl, 0, 2)) < 0 )
  322. goto out;
  323. /* temperature sanity check */
  324. if( temp < 0x1600 || temp > 0x3c00 )
  325. goto out;
  326. hyst_temp = read_reg(cl, 2, 2);
  327. os_temp = read_reg(cl, 3, 2);
  328. if( hyst_temp < 0 || os_temp < 0 )
  329. goto out;
  330. printk("DS1775 digital thermometer [@%02x]\n", cl->addr );
  331. print_temp("Temp: ", temp );
  332. print_temp(" Hyst: ", hyst_temp );
  333. print_temp(" OS: ", os_temp );
  334. printk("\n");
  335. x.temp = temp;
  336. x.overheat_temp = os_temp;
  337. x.overheat_hyst = hyst_temp;
  338. strlcpy( cl->name, "DS1775 thermostat", sizeof(cl->name) );
  339. if( !i2c_attach_client(cl) )
  340. x.thermostat = cl;
  341. out:
  342. if( cl != x.thermostat )
  343. kfree( cl );
  344. return 0;
  345. }
  346. static int
  347. do_probe( struct i2c_adapter *adapter, int addr, int kind )
  348. {
  349. struct i2c_client *cl;
  350. if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
  351. | I2C_FUNC_SMBUS_WRITE_BYTE) )
  352. return 0;
  353. if( !(cl=kmalloc(sizeof(*cl), GFP_KERNEL)) )
  354. return -ENOMEM;
  355. memset( cl, 0, sizeof(struct i2c_client) );
  356. cl->addr = addr;
  357. cl->adapter = adapter;
  358. cl->driver = &g4fan_driver;
  359. cl->flags = 0;
  360. if( addr < 0x48 )
  361. return attach_fan( cl );
  362. return attach_thermostat( cl );
  363. }
  364. /************************************************************************/
  365. /* initialization / cleanup */
  366. /************************************************************************/
  367. static int
  368. therm_of_probe( struct of_device *dev, const struct of_match *match )
  369. {
  370. return i2c_add_driver( &g4fan_driver );
  371. }
  372. static int
  373. therm_of_remove( struct of_device *dev )
  374. {
  375. return i2c_del_driver( &g4fan_driver );
  376. }
  377. static struct of_match therm_of_match[] = {{
  378. .name = "fan",
  379. .type = OF_ANY_MATCH,
  380. .compatible = "adm1030"
  381. }, {}
  382. };
  383. static struct of_platform_driver therm_of_driver = {
  384. .name = "temperature",
  385. .match_table = therm_of_match,
  386. .probe = therm_of_probe,
  387. .remove = therm_of_remove,
  388. };
  389. struct apple_thermal_info {
  390. u8 id; /* implementation ID */
  391. u8 fan_count; /* number of fans */
  392. u8 thermostat_count; /* number of thermostats */
  393. u8 unused;
  394. };
  395. static int __init
  396. g4fan_init( void )
  397. {
  398. struct apple_thermal_info *info;
  399. struct device_node *np;
  400. init_MUTEX( &x.lock );
  401. if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
  402. return -ENODEV;
  403. info = (struct apple_thermal_info*)get_property(np, "thermal-info", NULL);
  404. of_node_put(np);
  405. if( !info || !machine_is_compatible("PowerMac3,6") )
  406. return -ENODEV;
  407. if( info->id != 3 ) {
  408. printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
  409. return -ENODEV;
  410. }
  411. if( !(np=of_find_node_by_name(NULL, "fan")) )
  412. return -ENODEV;
  413. x.of_dev = of_platform_device_create( np, "temperature" );
  414. of_node_put( np );
  415. if( !x.of_dev ) {
  416. printk(KERN_ERR "Can't register fan controller!\n");
  417. return -ENODEV;
  418. }
  419. of_register_driver( &therm_of_driver );
  420. return 0;
  421. }
  422. static void __exit
  423. g4fan_exit( void )
  424. {
  425. of_unregister_driver( &therm_of_driver );
  426. if( x.of_dev )
  427. of_device_unregister( x.of_dev );
  428. }
  429. module_init(g4fan_init);
  430. module_exit(g4fan_exit);
  431. MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
  432. MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
  433. MODULE_LICENSE("GPL");