therm_windtunnel.c 12 KB

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