windfarm_pm91.c 18 KB

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