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