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