therm_windtunnel.c 12 KB

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