therm_windtunnel.c 12 KB

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