windfarm_pm91.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Windfarm PowerMac thermal control. SMU based 1 CPU desktop control loops
  3. *
  4. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5. * <benh@kernel.crashing.org>
  6. *
  7. * Released under the term of the GNU GPL v2.
  8. *
  9. * The algorithm used is the PID control algorithm, used the same
  10. * way the published Darwin code does, using the same values that
  11. * are present in the Darwin 8.2 snapshot property lists (note however
  12. * that none of the code has been re-used, it's a complete re-implementation
  13. *
  14. * The various control loops found in Darwin config file are:
  15. *
  16. * PowerMac9,1
  17. * ===========
  18. *
  19. * Has 3 control loops: CPU fans is similar to PowerMac8,1 (though it doesn't
  20. * try to play with other control loops fans). Drive bay is rather basic PID
  21. * with one sensor and one fan. Slots area is a bit different as the Darwin
  22. * driver is supposed to be capable of working in a special "AGP" mode which
  23. * involves the presence of an AGP sensor and an AGP fan (possibly on the
  24. * AGP card itself). I can't deal with that special mode as I don't have
  25. * access to those additional sensor/fans for now (though ultimately, it would
  26. * be possible to add sensor objects for them) so I'm only implementing the
  27. * basic PCI slot control loop
  28. */
  29. #include <linux/types.h>
  30. #include <linux/errno.h>
  31. #include <linux/kernel.h>
  32. #include <linux/delay.h>
  33. #include <linux/slab.h>
  34. #include <linux/init.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/wait.h>
  37. #include <linux/kmod.h>
  38. #include <linux/device.h>
  39. #include <linux/platform_device.h>
  40. #include <asm/prom.h>
  41. #include <asm/machdep.h>
  42. #include <asm/io.h>
  43. #include <asm/system.h>
  44. #include <asm/sections.h>
  45. #include <asm/smu.h>
  46. #include "windfarm.h"
  47. #include "windfarm_pid.h"
  48. #define VERSION "0.4"
  49. #undef DEBUG
  50. #ifdef DEBUG
  51. #define DBG(args...) printk(args)
  52. #else
  53. #define DBG(args...) do { } while(0)
  54. #endif
  55. /* define this to force CPU overtemp to 74 degree, useful for testing
  56. * the overtemp code
  57. */
  58. #undef HACKED_OVERTEMP
  59. static struct device *wf_smu_dev;
  60. /* Controls & sensors */
  61. static struct wf_sensor *sensor_cpu_power;
  62. static struct wf_sensor *sensor_cpu_temp;
  63. static struct wf_sensor *sensor_hd_temp;
  64. static struct wf_sensor *sensor_slots_power;
  65. static struct wf_control *fan_cpu_main;
  66. static struct wf_control *fan_cpu_second;
  67. static struct wf_control *fan_cpu_third;
  68. static struct wf_control *fan_hd;
  69. static struct wf_control *fan_slots;
  70. static struct wf_control *cpufreq_clamp;
  71. /* Set to kick the control loop into life */
  72. static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, wf_smu_started;
  73. /* Failure handling.. could be nicer */
  74. #define FAILURE_FAN 0x01
  75. #define FAILURE_SENSOR 0x02
  76. #define FAILURE_OVERTEMP 0x04
  77. static unsigned int wf_smu_failure_state;
  78. static int wf_smu_readjust, wf_smu_skipping;
  79. /*
  80. * ****** CPU Fans Control Loop ******
  81. *
  82. */
  83. #define WF_SMU_CPU_FANS_INTERVAL 1
  84. #define WF_SMU_CPU_FANS_MAX_HISTORY 16
  85. /* State data used by the cpu fans control loop
  86. */
  87. struct wf_smu_cpu_fans_state {
  88. int ticks;
  89. s32 cpu_setpoint;
  90. struct wf_cpu_pid_state pid;
  91. };
  92. static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
  93. /*
  94. * ****** Drive Fan Control Loop ******
  95. *
  96. */
  97. struct wf_smu_drive_fans_state {
  98. int ticks;
  99. s32 setpoint;
  100. struct wf_pid_state pid;
  101. };
  102. static struct wf_smu_drive_fans_state *wf_smu_drive_fans;
  103. /*
  104. * ****** Slots Fan Control Loop ******
  105. *
  106. */
  107. struct wf_smu_slots_fans_state {
  108. int ticks;
  109. s32 setpoint;
  110. struct wf_pid_state pid;
  111. };
  112. static struct wf_smu_slots_fans_state *wf_smu_slots_fans;
  113. /*
  114. * ***** Implementation *****
  115. *
  116. */
  117. static void wf_smu_create_cpu_fans(void)
  118. {
  119. struct wf_cpu_pid_param pid_param;
  120. struct smu_sdbp_header *hdr;
  121. struct smu_sdbp_cpupiddata *piddata;
  122. struct smu_sdbp_fvt *fvt;
  123. s32 tmax, tdelta, maxpow, powadj;
  124. /* First, locate the PID params in SMU SBD */
  125. hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
  126. if (hdr == 0) {
  127. printk(KERN_WARNING "windfarm: CPU PID fan config not found "
  128. "max fan speed\n");
  129. goto fail;
  130. }
  131. piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
  132. /* Get the FVT params for operating point 0 (the only supported one
  133. * for now) in order to get tmax
  134. */
  135. hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  136. if (hdr) {
  137. fvt = (struct smu_sdbp_fvt *)&hdr[1];
  138. tmax = ((s32)fvt->maxtemp) << 16;
  139. } else
  140. tmax = 0x5e0000; /* 94 degree default */
  141. /* Alloc & initialize state */
  142. wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
  143. GFP_KERNEL);
  144. if (wf_smu_cpu_fans == NULL)
  145. goto fail;
  146. wf_smu_cpu_fans->ticks = 1;
  147. /* Fill PID params */
  148. pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
  149. pid_param.history_len = piddata->history_len;
  150. if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
  151. printk(KERN_WARNING "windfarm: History size overflow on "
  152. "CPU control loop (%d)\n", piddata->history_len);
  153. pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
  154. }
  155. pid_param.gd = piddata->gd;
  156. pid_param.gp = piddata->gp;
  157. pid_param.gr = piddata->gr / pid_param.history_len;
  158. tdelta = ((s32)piddata->target_temp_delta) << 16;
  159. maxpow = ((s32)piddata->max_power) << 16;
  160. powadj = ((s32)piddata->power_adj) << 16;
  161. pid_param.tmax = tmax;
  162. pid_param.ttarget = tmax - tdelta;
  163. pid_param.pmaxadj = maxpow - powadj;
  164. pid_param.min = fan_cpu_main->ops->get_min(fan_cpu_main);
  165. pid_param.max = fan_cpu_main->ops->get_max(fan_cpu_main);
  166. wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
  167. DBG("wf: CPU Fan control initialized.\n");
  168. DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
  169. FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
  170. pid_param.min, pid_param.max);
  171. return;
  172. fail:
  173. printk(KERN_WARNING "windfarm: CPU fan config not found\n"
  174. "for this machine model, max fan speed\n");
  175. if (cpufreq_clamp)
  176. wf_control_set_max(cpufreq_clamp);
  177. if (fan_cpu_main)
  178. wf_control_set_max(fan_cpu_main);
  179. }
  180. static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
  181. {
  182. s32 new_setpoint, temp, power;
  183. int rc;
  184. if (--st->ticks != 0) {
  185. if (wf_smu_readjust)
  186. goto readjust;
  187. return;
  188. }
  189. st->ticks = WF_SMU_CPU_FANS_INTERVAL;
  190. rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);
  191. if (rc) {
  192. printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
  193. rc);
  194. wf_smu_failure_state |= FAILURE_SENSOR;
  195. return;
  196. }
  197. rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);
  198. if (rc) {
  199. printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
  200. rc);
  201. wf_smu_failure_state |= FAILURE_SENSOR;
  202. return;
  203. }
  204. DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
  205. FIX32TOPRINT(temp), FIX32TOPRINT(power));
  206. #ifdef HACKED_OVERTEMP
  207. if (temp > 0x4a0000)
  208. wf_smu_failure_state |= FAILURE_OVERTEMP;
  209. #else
  210. if (temp > st->pid.param.tmax)
  211. wf_smu_failure_state |= FAILURE_OVERTEMP;
  212. #endif
  213. new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
  214. DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
  215. if (st->cpu_setpoint == new_setpoint)
  216. return;
  217. st->cpu_setpoint = new_setpoint;
  218. readjust:
  219. if (fan_cpu_main && wf_smu_failure_state == 0) {
  220. rc = fan_cpu_main->ops->set_value(fan_cpu_main,
  221. st->cpu_setpoint);
  222. if (rc) {
  223. printk(KERN_WARNING "windfarm: CPU main fan"
  224. " error %d\n", rc);
  225. wf_smu_failure_state |= FAILURE_FAN;
  226. }
  227. }
  228. if (fan_cpu_second && wf_smu_failure_state == 0) {
  229. rc = fan_cpu_second->ops->set_value(fan_cpu_second,
  230. st->cpu_setpoint);
  231. if (rc) {
  232. printk(KERN_WARNING "windfarm: CPU second fan"
  233. " error %d\n", rc);
  234. wf_smu_failure_state |= FAILURE_FAN;
  235. }
  236. }
  237. if (fan_cpu_third && wf_smu_failure_state == 0) {
  238. rc = fan_cpu_main->ops->set_value(fan_cpu_third,
  239. st->cpu_setpoint);
  240. if (rc) {
  241. printk(KERN_WARNING "windfarm: CPU third fan"
  242. " error %d\n", rc);
  243. wf_smu_failure_state |= FAILURE_FAN;
  244. }
  245. }
  246. }
  247. static void wf_smu_create_drive_fans(void)
  248. {
  249. struct wf_pid_param param = {
  250. .interval = 5,
  251. .history_len = 2,
  252. .gd = 0x01e00000,
  253. .gp = 0x00500000,
  254. .gr = 0x00000000,
  255. .itarget = 0x00200000,
  256. };
  257. /* Alloc & initialize state */
  258. wf_smu_drive_fans = kmalloc(sizeof(struct wf_smu_drive_fans_state),
  259. GFP_KERNEL);
  260. if (wf_smu_drive_fans == NULL) {
  261. printk(KERN_WARNING "windfarm: Memory allocation error"
  262. " max fan speed\n");
  263. goto fail;
  264. }
  265. wf_smu_drive_fans->ticks = 1;
  266. /* Fill PID params */
  267. param.additive = (fan_hd->type == WF_CONTROL_RPM_FAN);
  268. param.min = fan_hd->ops->get_min(fan_hd);
  269. param.max = fan_hd->ops->get_max(fan_hd);
  270. wf_pid_init(&wf_smu_drive_fans->pid, &param);
  271. DBG("wf: Drive Fan control initialized.\n");
  272. DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
  273. FIX32TOPRINT(param.itarget), param.min, param.max);
  274. return;
  275. fail:
  276. if (fan_hd)
  277. wf_control_set_max(fan_hd);
  278. }
  279. static void wf_smu_drive_fans_tick(struct wf_smu_drive_fans_state *st)
  280. {
  281. s32 new_setpoint, temp;
  282. int rc;
  283. if (--st->ticks != 0) {
  284. if (wf_smu_readjust)
  285. goto readjust;
  286. return;
  287. }
  288. st->ticks = st->pid.param.interval;
  289. rc = sensor_hd_temp->ops->get_value(sensor_hd_temp, &temp);
  290. if (rc) {
  291. printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
  292. rc);
  293. wf_smu_failure_state |= FAILURE_SENSOR;
  294. return;
  295. }
  296. DBG("wf_smu: Drive Fans tick ! HD temp: %d.%03d\n",
  297. FIX32TOPRINT(temp));
  298. if (temp > (st->pid.param.itarget + 0x50000))
  299. wf_smu_failure_state |= FAILURE_OVERTEMP;
  300. new_setpoint = wf_pid_run(&st->pid, temp);
  301. DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
  302. if (st->setpoint == new_setpoint)
  303. return;
  304. st->setpoint = new_setpoint;
  305. readjust:
  306. if (fan_hd && wf_smu_failure_state == 0) {
  307. rc = fan_hd->ops->set_value(fan_hd, st->setpoint);
  308. if (rc) {
  309. printk(KERN_WARNING "windfarm: HD fan error %d\n",
  310. rc);
  311. wf_smu_failure_state |= FAILURE_FAN;
  312. }
  313. }
  314. }
  315. static void wf_smu_create_slots_fans(void)
  316. {
  317. struct wf_pid_param param = {
  318. .interval = 1,
  319. .history_len = 8,
  320. .gd = 0x00000000,
  321. .gp = 0x00000000,
  322. .gr = 0x00020000,
  323. .itarget = 0x00000000
  324. };
  325. /* Alloc & initialize state */
  326. wf_smu_slots_fans = kmalloc(sizeof(struct wf_smu_slots_fans_state),
  327. GFP_KERNEL);
  328. if (wf_smu_slots_fans == NULL) {
  329. printk(KERN_WARNING "windfarm: Memory allocation error"
  330. " max fan speed\n");
  331. goto fail;
  332. }
  333. wf_smu_slots_fans->ticks = 1;
  334. /* Fill PID params */
  335. param.additive = (fan_slots->type == WF_CONTROL_RPM_FAN);
  336. param.min = fan_slots->ops->get_min(fan_slots);
  337. param.max = fan_slots->ops->get_max(fan_slots);
  338. wf_pid_init(&wf_smu_slots_fans->pid, &param);
  339. DBG("wf: Slots Fan control initialized.\n");
  340. DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
  341. FIX32TOPRINT(param.itarget), param.min, param.max);
  342. return;
  343. fail:
  344. if (fan_slots)
  345. wf_control_set_max(fan_slots);
  346. }
  347. static void wf_smu_slots_fans_tick(struct wf_smu_slots_fans_state *st)
  348. {
  349. s32 new_setpoint, power;
  350. int rc;
  351. if (--st->ticks != 0) {
  352. if (wf_smu_readjust)
  353. goto readjust;
  354. return;
  355. }
  356. st->ticks = st->pid.param.interval;
  357. rc = sensor_slots_power->ops->get_value(sensor_slots_power, &power);
  358. if (rc) {
  359. printk(KERN_WARNING "windfarm: Slots power sensor error %d\n",
  360. rc);
  361. wf_smu_failure_state |= FAILURE_SENSOR;
  362. return;
  363. }
  364. DBG("wf_smu: Slots Fans tick ! Slots power: %d.%03d\n",
  365. FIX32TOPRINT(power));
  366. #if 0 /* Check what makes a good overtemp condition */
  367. if (power > (st->pid.param.itarget + 0x50000))
  368. wf_smu_failure_state |= FAILURE_OVERTEMP;
  369. #endif
  370. new_setpoint = wf_pid_run(&st->pid, power);
  371. DBG("wf_smu: new_setpoint: %d\n", (int)new_setpoint);
  372. if (st->setpoint == new_setpoint)
  373. return;
  374. st->setpoint = new_setpoint;
  375. readjust:
  376. if (fan_slots && wf_smu_failure_state == 0) {
  377. rc = fan_slots->ops->set_value(fan_slots, st->setpoint);
  378. if (rc) {
  379. printk(KERN_WARNING "windfarm: Slots fan error %d\n",
  380. rc);
  381. wf_smu_failure_state |= FAILURE_FAN;
  382. }
  383. }
  384. }
  385. /*
  386. * ****** Attributes ******
  387. *
  388. */
  389. #define BUILD_SHOW_FUNC_FIX(name, data) \
  390. static ssize_t show_##name(struct device *dev, \
  391. struct device_attribute *attr, \
  392. char *buf) \
  393. { \
  394. ssize_t r; \
  395. s32 val = 0; \
  396. data->ops->get_value(data, &val); \
  397. r = sprintf(buf, "%d.%03d", FIX32TOPRINT(val)); \
  398. return r; \
  399. } \
  400. static DEVICE_ATTR(name,S_IRUGO,show_##name, NULL);
  401. #define BUILD_SHOW_FUNC_INT(name, data) \
  402. static ssize_t show_##name(struct device *dev, \
  403. struct device_attribute *attr, \
  404. char *buf) \
  405. { \
  406. s32 val = 0; \
  407. data->ops->get_value(data, &val); \
  408. return sprintf(buf, "%d", val); \
  409. } \
  410. static DEVICE_ATTR(name,S_IRUGO,show_##name, NULL);
  411. BUILD_SHOW_FUNC_INT(cpu_fan, fan_cpu_main);
  412. BUILD_SHOW_FUNC_INT(hd_fan, fan_hd);
  413. BUILD_SHOW_FUNC_INT(slots_fan, fan_slots);
  414. BUILD_SHOW_FUNC_FIX(cpu_temp, sensor_cpu_temp);
  415. BUILD_SHOW_FUNC_FIX(cpu_power, sensor_cpu_power);
  416. BUILD_SHOW_FUNC_FIX(hd_temp, sensor_hd_temp);
  417. BUILD_SHOW_FUNC_FIX(slots_power, sensor_slots_power);
  418. /*
  419. * ****** Setup / Init / Misc ... ******
  420. *
  421. */
  422. static void wf_smu_tick(void)
  423. {
  424. unsigned int last_failure = wf_smu_failure_state;
  425. unsigned int new_failure;
  426. if (!wf_smu_started) {
  427. DBG("wf: creating control loops !\n");
  428. wf_smu_create_drive_fans();
  429. wf_smu_create_slots_fans();
  430. wf_smu_create_cpu_fans();
  431. wf_smu_started = 1;
  432. }
  433. /* Skipping ticks */
  434. if (wf_smu_skipping && --wf_smu_skipping)
  435. return;
  436. wf_smu_failure_state = 0;
  437. if (wf_smu_drive_fans)
  438. wf_smu_drive_fans_tick(wf_smu_drive_fans);
  439. if (wf_smu_slots_fans)
  440. wf_smu_slots_fans_tick(wf_smu_slots_fans);
  441. if (wf_smu_cpu_fans)
  442. wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
  443. wf_smu_readjust = 0;
  444. new_failure = wf_smu_failure_state & ~last_failure;
  445. /* If entering failure mode, clamp cpufreq and ramp all
  446. * fans to full speed.
  447. */
  448. if (wf_smu_failure_state && !last_failure) {
  449. if (cpufreq_clamp)
  450. wf_control_set_max(cpufreq_clamp);
  451. if (fan_cpu_main)
  452. wf_control_set_max(fan_cpu_main);
  453. if (fan_cpu_second)
  454. wf_control_set_max(fan_cpu_second);
  455. if (fan_cpu_third)
  456. wf_control_set_max(fan_cpu_third);
  457. if (fan_hd)
  458. wf_control_set_max(fan_hd);
  459. if (fan_slots)
  460. wf_control_set_max(fan_slots);
  461. }
  462. /* If leaving failure mode, unclamp cpufreq and readjust
  463. * all fans on next iteration
  464. */
  465. if (!wf_smu_failure_state && last_failure) {
  466. if (cpufreq_clamp)
  467. wf_control_set_min(cpufreq_clamp);
  468. wf_smu_readjust = 1;
  469. }
  470. /* Overtemp condition detected, notify and start skipping a couple
  471. * ticks to let the temperature go down
  472. */
  473. if (new_failure & FAILURE_OVERTEMP) {
  474. wf_set_overtemp();
  475. wf_smu_skipping = 2;
  476. }
  477. /* We only clear the overtemp condition if overtemp is cleared
  478. * _and_ no other failure is present. Since a sensor error will
  479. * clear the overtemp condition (can't measure temperature) at
  480. * the control loop levels, but we don't want to keep it clear
  481. * here in this case
  482. */
  483. if (new_failure == 0 && last_failure & FAILURE_OVERTEMP)
  484. wf_clear_overtemp();
  485. }
  486. static void wf_smu_new_control(struct wf_control *ct)
  487. {
  488. if (wf_smu_all_controls_ok)
  489. return;
  490. if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-rear-fan-0")) {
  491. if (wf_get_control(ct) == 0) {
  492. fan_cpu_main = ct;
  493. device_create_file(wf_smu_dev, &dev_attr_cpu_fan);
  494. }
  495. }
  496. if (fan_cpu_second == NULL && !strcmp(ct->name, "cpu-rear-fan-1")) {
  497. if (wf_get_control(ct) == 0)
  498. fan_cpu_second = ct;
  499. }
  500. if (fan_cpu_third == NULL && !strcmp(ct->name, "cpu-front-fan-0")) {
  501. if (wf_get_control(ct) == 0)
  502. fan_cpu_third = ct;
  503. }
  504. if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
  505. if (wf_get_control(ct) == 0)
  506. cpufreq_clamp = ct;
  507. }
  508. if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
  509. if (wf_get_control(ct) == 0) {
  510. fan_hd = ct;
  511. device_create_file(wf_smu_dev, &dev_attr_hd_fan);
  512. }
  513. }
  514. if (fan_slots == NULL && !strcmp(ct->name, "slots-fan")) {
  515. if (wf_get_control(ct) == 0) {
  516. fan_slots = ct;
  517. device_create_file(wf_smu_dev, &dev_attr_slots_fan);
  518. }
  519. }
  520. if (fan_cpu_main && (fan_cpu_second || fan_cpu_third) && fan_hd &&
  521. fan_slots && cpufreq_clamp)
  522. wf_smu_all_controls_ok = 1;
  523. }
  524. static void wf_smu_new_sensor(struct wf_sensor *sr)
  525. {
  526. if (wf_smu_all_sensors_ok)
  527. return;
  528. if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
  529. if (wf_get_sensor(sr) == 0) {
  530. sensor_cpu_power = sr;
  531. device_create_file(wf_smu_dev, &dev_attr_cpu_power);
  532. }
  533. }
  534. if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
  535. if (wf_get_sensor(sr) == 0) {
  536. sensor_cpu_temp = sr;
  537. device_create_file(wf_smu_dev, &dev_attr_cpu_temp);
  538. }
  539. }
  540. if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
  541. if (wf_get_sensor(sr) == 0) {
  542. sensor_hd_temp = sr;
  543. device_create_file(wf_smu_dev, &dev_attr_hd_temp);
  544. }
  545. }
  546. if (sensor_slots_power == NULL && !strcmp(sr->name, "slots-power")) {
  547. if (wf_get_sensor(sr) == 0) {
  548. sensor_slots_power = sr;
  549. device_create_file(wf_smu_dev, &dev_attr_slots_power);
  550. }
  551. }
  552. if (sensor_cpu_power && sensor_cpu_temp &&
  553. sensor_hd_temp && sensor_slots_power)
  554. wf_smu_all_sensors_ok = 1;
  555. }
  556. static int wf_smu_notify(struct notifier_block *self,
  557. unsigned long event, void *data)
  558. {
  559. switch(event) {
  560. case WF_EVENT_NEW_CONTROL:
  561. DBG("wf: new control %s detected\n",
  562. ((struct wf_control *)data)->name);
  563. wf_smu_new_control(data);
  564. wf_smu_readjust = 1;
  565. break;
  566. case WF_EVENT_NEW_SENSOR:
  567. DBG("wf: new sensor %s detected\n",
  568. ((struct wf_sensor *)data)->name);
  569. wf_smu_new_sensor(data);
  570. break;
  571. case WF_EVENT_TICK:
  572. if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
  573. wf_smu_tick();
  574. }
  575. return 0;
  576. }
  577. static struct notifier_block wf_smu_events = {
  578. .notifier_call = wf_smu_notify,
  579. };
  580. static int wf_init_pm(void)
  581. {
  582. printk(KERN_INFO "windfarm: Initializing for Desktop G5 model\n");
  583. return 0;
  584. }
  585. static int wf_smu_probe(struct device *ddev)
  586. {
  587. wf_smu_dev = ddev;
  588. wf_register_client(&wf_smu_events);
  589. return 0;
  590. }
  591. static int wf_smu_remove(struct device *ddev)
  592. {
  593. wf_unregister_client(&wf_smu_events);
  594. /* XXX We don't have yet a guarantee that our callback isn't
  595. * in progress when returning from wf_unregister_client, so
  596. * we add an arbitrary delay. I'll have to fix that in the core
  597. */
  598. msleep(1000);
  599. /* Release all sensors */
  600. /* One more crappy race: I don't think we have any guarantee here
  601. * that the attribute callback won't race with the sensor beeing
  602. * disposed of, and I'm not 100% certain what best way to deal
  603. * with that except by adding locks all over... I'll do that
  604. * eventually but heh, who ever rmmod this module anyway ?
  605. */
  606. if (sensor_cpu_power) {
  607. device_remove_file(wf_smu_dev, &dev_attr_cpu_power);
  608. wf_put_sensor(sensor_cpu_power);
  609. }
  610. if (sensor_cpu_temp) {
  611. device_remove_file(wf_smu_dev, &dev_attr_cpu_temp);
  612. wf_put_sensor(sensor_cpu_temp);
  613. }
  614. if (sensor_hd_temp) {
  615. device_remove_file(wf_smu_dev, &dev_attr_hd_temp);
  616. wf_put_sensor(sensor_hd_temp);
  617. }
  618. if (sensor_slots_power) {
  619. device_remove_file(wf_smu_dev, &dev_attr_slots_power);
  620. wf_put_sensor(sensor_slots_power);
  621. }
  622. /* Release all controls */
  623. if (fan_cpu_main) {
  624. device_remove_file(wf_smu_dev, &dev_attr_cpu_fan);
  625. wf_put_control(fan_cpu_main);
  626. }
  627. if (fan_cpu_second)
  628. wf_put_control(fan_cpu_second);
  629. if (fan_cpu_third)
  630. wf_put_control(fan_cpu_third);
  631. if (fan_hd) {
  632. device_remove_file(wf_smu_dev, &dev_attr_hd_fan);
  633. wf_put_control(fan_hd);
  634. }
  635. if (fan_slots) {
  636. device_remove_file(wf_smu_dev, &dev_attr_slots_fan);
  637. wf_put_control(fan_slots);
  638. }
  639. if (cpufreq_clamp)
  640. wf_put_control(cpufreq_clamp);
  641. /* Destroy control loops state structures */
  642. if (wf_smu_slots_fans)
  643. kfree(wf_smu_cpu_fans);
  644. if (wf_smu_drive_fans)
  645. kfree(wf_smu_cpu_fans);
  646. if (wf_smu_cpu_fans)
  647. kfree(wf_smu_cpu_fans);
  648. wf_smu_dev = NULL;
  649. return 0;
  650. }
  651. static struct device_driver wf_smu_driver = {
  652. .name = "windfarm",
  653. .bus = &platform_bus_type,
  654. .probe = wf_smu_probe,
  655. .remove = wf_smu_remove,
  656. };
  657. static int __init wf_smu_init(void)
  658. {
  659. int rc = -ENODEV;
  660. if (machine_is_compatible("PowerMac9,1"))
  661. rc = wf_init_pm();
  662. if (rc == 0) {
  663. #ifdef MODULE
  664. request_module("windfarm_smu_controls");
  665. request_module("windfarm_smu_sensors");
  666. request_module("windfarm_lm75_sensor");
  667. #endif /* MODULE */
  668. driver_register(&wf_smu_driver);
  669. }
  670. return rc;
  671. }
  672. static void __exit wf_smu_exit(void)
  673. {
  674. driver_unregister(&wf_smu_driver);
  675. }
  676. module_init(wf_smu_init);
  677. module_exit(wf_smu_exit);
  678. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  679. MODULE_DESCRIPTION("Thermal control logic for PowerMac9,1");
  680. MODULE_LICENSE("GPL");