therm_windtunnel.c 13 KB

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