windfarm_pm81.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Windfarm PowerMac thermal control. iMac G5
  3. *
  4. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  5. * <[email protected]>
  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. * PowerMac8,1 and PowerMac8,2
  17. * ===========================
  18. *
  19. * System Fans control loop. Different based on models. In addition to the
  20. * usual PID algorithm, the control loop gets 2 additional pairs of linear
  21. * scaling factors (scale/offsets) expressed as 4.12 fixed point values
  22. * signed offset, unsigned scale)
  23. *
  24. * The targets are modified such as:
  25. * - the linked control (second control) gets the target value as-is
  26. * (typically the drive fan)
  27. * - the main control (first control) gets the target value scaled with
  28. * the first pair of factors, and is then modified as below
  29. * - the value of the target of the CPU Fan control loop is retrieved,
  30. * scaled with the second pair of factors, and the max of that and
  31. * the scaled target is applied to the main control.
  32. *
  33. * # model_id: 2
  34. * controls : system-fan, drive-bay-fan
  35. * sensors : hd-temp
  36. * PID params : G_d = 0x15400000
  37. * G_p = 0x00200000
  38. * G_r = 0x000002fd
  39. * History = 2 entries
  40. * Input target = 0x3a0000
  41. * Interval = 5s
  42. * linear-factors : offset = 0xff38 scale = 0x0ccd
  43. * offset = 0x0208 scale = 0x07ae
  44. *
  45. * # model_id: 3
  46. * controls : system-fan, drive-bay-fan
  47. * sensors : hd-temp
  48. * PID params : G_d = 0x08e00000
  49. * G_p = 0x00566666
  50. * G_r = 0x0000072b
  51. * History = 2 entries
  52. * Input target = 0x350000
  53. * Interval = 5s
  54. * linear-factors : offset = 0xff38 scale = 0x0ccd
  55. * offset = 0x0000 scale = 0x0000
  56. *
  57. * # model_id: 5
  58. * controls : system-fan
  59. * sensors : hd-temp
  60. * PID params : G_d = 0x15400000
  61. * G_p = 0x00233333
  62. * G_r = 0x000002fd
  63. * History = 2 entries
  64. * Input target = 0x3a0000
  65. * Interval = 5s
  66. * linear-factors : offset = 0x0000 scale = 0x1000
  67. * offset = 0x0091 scale = 0x0bae
  68. *
  69. * CPU Fan control loop. The loop is identical for all models. it
  70. * has an additional pair of scaling factor. This is used to scale the
  71. * systems fan control loop target result (the one before it gets scaled
  72. * by the System Fans control loop itself). Then, the max value of the
  73. * calculated target value and system fan value is sent to the fans
  74. *
  75. * controls : cpu-fan
  76. * sensors : cpu-temp cpu-power
  77. * PID params : From SMU sdb partition
  78. * linear-factors : offset = 0xfb50 scale = 0x1000
  79. *
  80. * CPU Slew control loop. Not implemented. The cpufreq driver in linux is
  81. * completely separate for now, though we could find a way to link it, either
  82. * as a client reacting to overtemp notifications, or directling monitoring
  83. * the CPU temperature
  84. *
  85. * WARNING ! The CPU control loop requires the CPU tmax for the current
  86. * operating point. However, we currently are completely separated from
  87. * the cpufreq driver and thus do not know what the current operating
  88. * point is. Fortunately, we also do not have any hardware supporting anything
  89. * but operating point 0 at the moment, thus we just peek that value directly
  90. * from the SDB partition. If we ever end up with actually slewing the system
  91. * clock and thus changing operating points, we'll have to find a way to
  92. * communicate with the CPU freq driver;
  93. *
  94. */
  95. #include <linux/types.h>
  96. #include <linux/errno.h>
  97. #include <linux/kernel.h>
  98. #include <linux/delay.h>
  99. #include <linux/slab.h>
  100. #include <linux/init.h>
  101. #include <linux/spinlock.h>
  102. #include <linux/wait.h>
  103. #include <linux/kmod.h>
  104. #include <linux/device.h>
  105. #include <linux/platform_device.h>
  106. #include <asm/prom.h>
  107. #include <asm/machdep.h>
  108. #include <asm/io.h>
  109. #include <asm/sections.h>
  110. #include <asm/smu.h>
  111. #include "windfarm.h"
  112. #include "windfarm_pid.h"
  113. #define VERSION "0.4"
  114. #undef DEBUG
  115. #ifdef DEBUG
  116. #define DBG(args...) printk(args)
  117. #else
  118. #define DBG(args...) do { } while(0)
  119. #endif
  120. /* define this to force CPU overtemp to 74 degree, useful for testing
  121. * the overtemp code
  122. */
  123. #undef HACKED_OVERTEMP
  124. static int wf_smu_mach_model; /* machine model id */
  125. /* Controls & sensors */
  126. static struct wf_sensor *sensor_cpu_power;
  127. static struct wf_sensor *sensor_cpu_temp;
  128. static struct wf_sensor *sensor_hd_temp;
  129. static struct wf_control *fan_cpu_main;
  130. static struct wf_control *fan_hd;
  131. static struct wf_control *fan_system;
  132. static struct wf_control *cpufreq_clamp;
  133. /* Set to kick the control loop into life */
  134. static int wf_smu_all_controls_ok, wf_smu_all_sensors_ok, wf_smu_started;
  135. /* Failure handling.. could be nicer */
  136. #define FAILURE_FAN 0x01
  137. #define FAILURE_SENSOR 0x02
  138. #define FAILURE_OVERTEMP 0x04
  139. static unsigned int wf_smu_failure_state;
  140. static int wf_smu_readjust, wf_smu_skipping;
  141. static bool wf_smu_overtemp;
  142. /*
  143. * ****** System Fans Control Loop ******
  144. *
  145. */
  146. /* Parameters for the System Fans control loop. Parameters
  147. * not in this table such as interval, history size, ...
  148. * are common to all versions and thus hard coded for now.
  149. */
  150. struct wf_smu_sys_fans_param {
  151. int model_id;
  152. s32 itarget;
  153. s32 gd, gp, gr;
  154. s16 offset0;
  155. u16 scale0;
  156. s16 offset1;
  157. u16 scale1;
  158. };
  159. #define WF_SMU_SYS_FANS_INTERVAL 5
  160. #define WF_SMU_SYS_FANS_HISTORY_SIZE 2
  161. /* State data used by the system fans control loop
  162. */
  163. struct wf_smu_sys_fans_state {
  164. int ticks;
  165. s32 sys_setpoint;
  166. s32 hd_setpoint;
  167. s16 offset0;
  168. u16 scale0;
  169. s16 offset1;
  170. u16 scale1;
  171. struct wf_pid_state pid;
  172. };
  173. /*
  174. * Configs for SMU System Fan control loop
  175. */
  176. static struct wf_smu_sys_fans_param wf_smu_sys_all_params[] = {
  177. /* Model ID 2 */
  178. {
  179. .model_id = 2,
  180. .itarget = 0x3a0000,
  181. .gd = 0x15400000,
  182. .gp = 0x00200000,
  183. .gr = 0x000002fd,
  184. .offset0 = 0xff38,
  185. .scale0 = 0x0ccd,
  186. .offset1 = 0x0208,
  187. .scale1 = 0x07ae,
  188. },
  189. /* Model ID 3 */
  190. {
  191. .model_id = 3,
  192. .itarget = 0x350000,
  193. .gd = 0x08e00000,
  194. .gp = 0x00566666,
  195. .gr = 0x0000072b,
  196. .offset0 = 0xff38,
  197. .scale0 = 0x0ccd,
  198. .offset1 = 0x0000,
  199. .scale1 = 0x0000,
  200. },
  201. /* Model ID 5 */
  202. {
  203. .model_id = 5,
  204. .itarget = 0x3a0000,
  205. .gd = 0x15400000,
  206. .gp = 0x00233333,
  207. .gr = 0x000002fd,
  208. .offset0 = 0x0000,
  209. .scale0 = 0x1000,
  210. .offset1 = 0x0091,
  211. .scale1 = 0x0bae,
  212. },
  213. };
  214. #define WF_SMU_SYS_FANS_NUM_CONFIGS ARRAY_SIZE(wf_smu_sys_all_params)
  215. static struct wf_smu_sys_fans_state *wf_smu_sys_fans;
  216. /*
  217. * ****** CPU Fans Control Loop ******
  218. *
  219. */
  220. #define WF_SMU_CPU_FANS_INTERVAL 1
  221. #define WF_SMU_CPU_FANS_MAX_HISTORY 16
  222. #define WF_SMU_CPU_FANS_SIBLING_SCALE 0x00001000
  223. #define WF_SMU_CPU_FANS_SIBLING_OFFSET 0xfffffb50
  224. /* State data used by the cpu fans control loop
  225. */
  226. struct wf_smu_cpu_fans_state {
  227. int ticks;
  228. s32 cpu_setpoint;
  229. s32 scale;
  230. s32 offset;
  231. struct wf_cpu_pid_state pid;
  232. };
  233. static struct wf_smu_cpu_fans_state *wf_smu_cpu_fans;
  234. /*
  235. * ***** Implementation *****
  236. *
  237. */
  238. static void wf_smu_create_sys_fans(void)
  239. {
  240. struct wf_smu_sys_fans_param *param = NULL;
  241. struct wf_pid_param pid_param;
  242. int i;
  243. /* First, locate the params for this model */
  244. for (i = 0; i < WF_SMU_SYS_FANS_NUM_CONFIGS; i++)
  245. if (wf_smu_sys_all_params[i].model_id == wf_smu_mach_model) {
  246. param = &wf_smu_sys_all_params[i];
  247. break;
  248. }
  249. /* No params found, put fans to max */
  250. if (param == NULL) {
  251. printk(KERN_WARNING "windfarm: System fan config not found "
  252. "for this machine model, max fan speed\n");
  253. goto fail;
  254. }
  255. /* Alloc & initialize state */
  256. wf_smu_sys_fans = kmalloc(sizeof(struct wf_smu_sys_fans_state),
  257. GFP_KERNEL);
  258. if (wf_smu_sys_fans == NULL) {
  259. printk(KERN_WARNING "windfarm: Memory allocation error"
  260. " max fan speed\n");
  261. goto fail;
  262. }
  263. wf_smu_sys_fans->ticks = 1;
  264. wf_smu_sys_fans->scale0 = param->scale0;
  265. wf_smu_sys_fans->offset0 = param->offset0;
  266. wf_smu_sys_fans->scale1 = param->scale1;
  267. wf_smu_sys_fans->offset1 = param->offset1;
  268. /* Fill PID params */
  269. pid_param.gd = param->gd;
  270. pid_param.gp = param->gp;
  271. pid_param.gr = param->gr;
  272. pid_param.interval = WF_SMU_SYS_FANS_INTERVAL;
  273. pid_param.history_len = WF_SMU_SYS_FANS_HISTORY_SIZE;
  274. pid_param.itarget = param->itarget;
  275. pid_param.min = wf_control_get_min(fan_system);
  276. pid_param.max = wf_control_get_max(fan_system);
  277. if (fan_hd) {
  278. pid_param.min =
  279. max(pid_param.min, wf_control_get_min(fan_hd));
  280. pid_param.max =
  281. min(pid_param.max, wf_control_get_max(fan_hd));
  282. }
  283. wf_pid_init(&wf_smu_sys_fans->pid, &pid_param);
  284. DBG("wf: System Fan control initialized.\n");
  285. DBG(" itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
  286. FIX32TOPRINT(pid_param.itarget), pid_param.min, pid_param.max);
  287. return;
  288. fail:
  289. if (fan_system)
  290. wf_control_set_max(fan_system);
  291. if (fan_hd)
  292. wf_control_set_max(fan_hd);
  293. }
  294. static void wf_smu_sys_fans_tick(struct wf_smu_sys_fans_state *st)
  295. {
  296. s32 new_setpoint, temp, scaled, cputarget;
  297. int rc;
  298. if (--st->ticks != 0) {
  299. if (wf_smu_readjust)
  300. goto readjust;
  301. return;
  302. }
  303. st->ticks = WF_SMU_SYS_FANS_INTERVAL;
  304. rc = wf_sensor_get(sensor_hd_temp, &temp);
  305. if (rc) {
  306. printk(KERN_WARNING "windfarm: HD temp sensor error %d\n",
  307. rc);
  308. wf_smu_failure_state |= FAILURE_SENSOR;
  309. return;
  310. }
  311. DBG("wf_smu: System Fans tick ! HD temp: %d.%03d\n",
  312. FIX32TOPRINT(temp));
  313. if (temp > (st->pid.param.itarget + 0x50000))
  314. wf_smu_failure_state |= FAILURE_OVERTEMP;
  315. new_setpoint = wf_pid_run(&st->pid, temp);
  316. DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
  317. scaled = ((((s64)new_setpoint) * (s64)st->scale0) >> 12) + st->offset0;
  318. DBG("wf_smu: scaled setpoint: %d RPM\n", (int)scaled);
  319. cputarget = wf_smu_cpu_fans ? wf_smu_cpu_fans->pid.target : 0;
  320. cputarget = ((((s64)cputarget) * (s64)st->scale1) >> 12) + st->offset1;
  321. scaled = max(scaled, cputarget);
  322. scaled = max(scaled, st->pid.param.min);
  323. scaled = min(scaled, st->pid.param.max);
  324. DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)scaled);
  325. if (st->sys_setpoint == scaled && new_setpoint == st->hd_setpoint)
  326. return;
  327. st->sys_setpoint = scaled;
  328. st->hd_setpoint = new_setpoint;
  329. readjust:
  330. if (fan_system && wf_smu_failure_state == 0) {
  331. rc = wf_control_set(fan_system, st->sys_setpoint);
  332. if (rc) {
  333. printk(KERN_WARNING "windfarm: Sys fan error %d\n",
  334. rc);
  335. wf_smu_failure_state |= FAILURE_FAN;
  336. }
  337. }
  338. if (fan_hd && wf_smu_failure_state == 0) {
  339. rc = wf_control_set(fan_hd, st->hd_setpoint);
  340. if (rc) {
  341. printk(KERN_WARNING "windfarm: HD fan error %d\n",
  342. rc);
  343. wf_smu_failure_state |= FAILURE_FAN;
  344. }
  345. }
  346. }
  347. static void wf_smu_create_cpu_fans(void)
  348. {
  349. struct wf_cpu_pid_param pid_param;
  350. const struct smu_sdbp_header *hdr;
  351. struct smu_sdbp_cpupiddata *piddata;
  352. struct smu_sdbp_fvt *fvt;
  353. s32 tmax, tdelta, maxpow, powadj;
  354. /* First, locate the PID params in SMU SBD */
  355. hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
  356. if (hdr == 0) {
  357. printk(KERN_WARNING "windfarm: CPU PID fan config not found "
  358. "max fan speed\n");
  359. goto fail;
  360. }
  361. piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
  362. /* Get the FVT params for operating point 0 (the only supported one
  363. * for now) in order to get tmax
  364. */
  365. hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  366. if (hdr) {
  367. fvt = (struct smu_sdbp_fvt *)&hdr[1];
  368. tmax = ((s32)fvt->maxtemp) << 16;
  369. } else
  370. tmax = 0x5e0000; /* 94 degree default */
  371. /* Alloc & initialize state */
  372. wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),
  373. GFP_KERNEL);
  374. if (wf_smu_cpu_fans == NULL)
  375. goto fail;
  376. wf_smu_cpu_fans->ticks = 1;
  377. wf_smu_cpu_fans->scale = WF_SMU_CPU_FANS_SIBLING_SCALE;
  378. wf_smu_cpu_fans->offset = WF_SMU_CPU_FANS_SIBLING_OFFSET;
  379. /* Fill PID params */
  380. pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;
  381. pid_param.history_len = piddata->history_len;
  382. if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
  383. printk(KERN_WARNING "windfarm: History size overflow on "
  384. "CPU control loop (%d)\n", piddata->history_len);
  385. pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
  386. }
  387. pid_param.gd = piddata->gd;
  388. pid_param.gp = piddata->gp;
  389. pid_param.gr = piddata->gr / pid_param.history_len;
  390. tdelta = ((s32)piddata->target_temp_delta) << 16;
  391. maxpow = ((s32)piddata->max_power) << 16;
  392. powadj = ((s32)piddata->power_adj) << 16;
  393. pid_param.tmax = tmax;
  394. pid_param.ttarget = tmax - tdelta;
  395. pid_param.pmaxadj = maxpow - powadj;
  396. pid_param.min = wf_control_get_min(fan_cpu_main);
  397. pid_param.max = wf_control_get_max(fan_cpu_main);
  398. wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);
  399. DBG("wf: CPU Fan control initialized.\n");
  400. DBG(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",
  401. FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
  402. pid_param.min, pid_param.max);
  403. return;
  404. fail:
  405. printk(KERN_WARNING "windfarm: CPU fan config not found\n"
  406. "for this machine model, max fan speed\n");
  407. if (cpufreq_clamp)
  408. wf_control_set_max(cpufreq_clamp);
  409. if (fan_cpu_main)
  410. wf_control_set_max(fan_cpu_main);
  411. }
  412. static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st)
  413. {
  414. s32 new_setpoint, temp, power, systarget;
  415. int rc;
  416. if (--st->ticks != 0) {
  417. if (wf_smu_readjust)
  418. goto readjust;
  419. return;
  420. }
  421. st->ticks = WF_SMU_CPU_FANS_INTERVAL;
  422. rc = wf_sensor_get(sensor_cpu_temp, &temp);
  423. if (rc) {
  424. printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",
  425. rc);
  426. wf_smu_failure_state |= FAILURE_SENSOR;
  427. return;
  428. }
  429. rc = wf_sensor_get(sensor_cpu_power, &power);
  430. if (rc) {
  431. printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",
  432. rc);
  433. wf_smu_failure_state |= FAILURE_SENSOR;
  434. return;
  435. }
  436. DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",
  437. FIX32TOPRINT(temp), FIX32TOPRINT(power));
  438. #ifdef HACKED_OVERTEMP
  439. if (temp > 0x4a0000)
  440. wf_smu_failure_state |= FAILURE_OVERTEMP;
  441. #else
  442. if (temp > st->pid.param.tmax)
  443. wf_smu_failure_state |= FAILURE_OVERTEMP;
  444. #endif
  445. new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
  446. DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);
  447. systarget = wf_smu_sys_fans ? wf_smu_sys_fans->pid.target : 0;
  448. systarget = ((((s64)systarget) * (s64)st->scale) >> 12)
  449. + st->offset;
  450. new_setpoint = max(new_setpoint, systarget);
  451. new_setpoint = max(new_setpoint, st->pid.param.min);
  452. new_setpoint = min(new_setpoint, st->pid.param.max);
  453. DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)new_setpoint);
  454. if (st->cpu_setpoint == new_setpoint)
  455. return;
  456. st->cpu_setpoint = new_setpoint;
  457. readjust:
  458. if (fan_cpu_main && wf_smu_failure_state == 0) {
  459. rc = wf_control_set(fan_cpu_main, st->cpu_setpoint);
  460. if (rc) {
  461. printk(KERN_WARNING "windfarm: CPU main fan"
  462. " error %d\n", rc);
  463. wf_smu_failure_state |= FAILURE_FAN;
  464. }
  465. }
  466. }
  467. /*
  468. * ****** Setup / Init / Misc ... ******
  469. *
  470. */
  471. static void wf_smu_tick(void)
  472. {
  473. unsigned int last_failure = wf_smu_failure_state;
  474. unsigned int new_failure;
  475. if (!wf_smu_started) {
  476. DBG("wf: creating control loops !\n");
  477. wf_smu_create_sys_fans();
  478. wf_smu_create_cpu_fans();
  479. wf_smu_started = 1;
  480. }
  481. /* Skipping ticks */
  482. if (wf_smu_skipping && --wf_smu_skipping)
  483. return;
  484. wf_smu_failure_state = 0;
  485. if (wf_smu_sys_fans)
  486. wf_smu_sys_fans_tick(wf_smu_sys_fans);
  487. if (wf_smu_cpu_fans)
  488. wf_smu_cpu_fans_tick(wf_smu_cpu_fans);
  489. wf_smu_readjust = 0;
  490. new_failure = wf_smu_failure_state & ~last_failure;
  491. /* If entering failure mode, clamp cpufreq and ramp all
  492. * fans to full speed.
  493. */
  494. if (wf_smu_failure_state && !last_failure) {
  495. if (cpufreq_clamp)
  496. wf_control_set_max(cpufreq_clamp);
  497. if (fan_system)
  498. wf_control_set_max(fan_system);
  499. if (fan_cpu_main)
  500. wf_control_set_max(fan_cpu_main);
  501. if (fan_hd)
  502. wf_control_set_max(fan_hd);
  503. }
  504. /* If leaving failure mode, unclamp cpufreq and readjust
  505. * all fans on next iteration
  506. */
  507. if (!wf_smu_failure_state && last_failure) {
  508. if (cpufreq_clamp)
  509. wf_control_set_min(cpufreq_clamp);
  510. wf_smu_readjust = 1;
  511. }
  512. /* Overtemp condition detected, notify and start skipping a couple
  513. * ticks to let the temperature go down
  514. */
  515. if (new_failure & FAILURE_OVERTEMP) {
  516. wf_set_overtemp();
  517. wf_smu_skipping = 2;
  518. wf_smu_overtemp = true;
  519. }
  520. /* We only clear the overtemp condition if overtemp is cleared
  521. * _and_ no other failure is present. Since a sensor error will
  522. * clear the overtemp condition (can't measure temperature) at
  523. * the control loop levels, but we don't want to keep it clear
  524. * here in this case
  525. */
  526. if (!wf_smu_failure_state && wf_smu_overtemp) {
  527. wf_clear_overtemp();
  528. wf_smu_overtemp = false;
  529. }
  530. }
  531. static void wf_smu_new_control(struct wf_control *ct)
  532. {
  533. if (wf_smu_all_controls_ok)
  534. return;
  535. if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-fan")) {
  536. if (wf_get_control(ct) == 0)
  537. fan_cpu_main = ct;
  538. }
  539. if (fan_system == NULL && !strcmp(ct->name, "system-fan")) {
  540. if (wf_get_control(ct) == 0)
  541. fan_system = ct;
  542. }
  543. if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
  544. if (wf_get_control(ct) == 0)
  545. cpufreq_clamp = ct;
  546. }
  547. /* Darwin property list says the HD fan is only for model ID
  548. * 0, 1, 2 and 3
  549. */
  550. if (wf_smu_mach_model > 3) {
  551. if (fan_system && fan_cpu_main && cpufreq_clamp)
  552. wf_smu_all_controls_ok = 1;
  553. return;
  554. }
  555. if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {
  556. if (wf_get_control(ct) == 0)
  557. fan_hd = ct;
  558. }
  559. if (fan_system && fan_hd && fan_cpu_main && cpufreq_clamp)
  560. wf_smu_all_controls_ok = 1;
  561. }
  562. static void wf_smu_new_sensor(struct wf_sensor *sr)
  563. {
  564. if (wf_smu_all_sensors_ok)
  565. return;
  566. if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {
  567. if (wf_get_sensor(sr) == 0)
  568. sensor_cpu_power = sr;
  569. }
  570. if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {
  571. if (wf_get_sensor(sr) == 0)
  572. sensor_cpu_temp = sr;
  573. }
  574. if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {
  575. if (wf_get_sensor(sr) == 0)
  576. sensor_hd_temp = sr;
  577. }
  578. if (sensor_cpu_power && sensor_cpu_temp && sensor_hd_temp)
  579. wf_smu_all_sensors_ok = 1;
  580. }
  581. static int wf_smu_notify(struct notifier_block *self,
  582. unsigned long event, void *data)
  583. {
  584. switch(event) {
  585. case WF_EVENT_NEW_CONTROL:
  586. DBG("wf: new control %s detected\n",
  587. ((struct wf_control *)data)->name);
  588. wf_smu_new_control(data);
  589. wf_smu_readjust = 1;
  590. break;
  591. case WF_EVENT_NEW_SENSOR:
  592. DBG("wf: new sensor %s detected\n",
  593. ((struct wf_sensor *)data)->name);
  594. wf_smu_new_sensor(data);
  595. break;
  596. case WF_EVENT_TICK:
  597. if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)
  598. wf_smu_tick();
  599. }
  600. return 0;
  601. }
  602. static struct notifier_block wf_smu_events = {
  603. .notifier_call = wf_smu_notify,
  604. };
  605. static int wf_init_pm(void)
  606. {
  607. const struct smu_sdbp_header *hdr;
  608. hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
  609. if (hdr != 0) {
  610. struct smu_sdbp_sensortree *st =
  611. (struct smu_sdbp_sensortree *)&hdr[1];
  612. wf_smu_mach_model = st->model_id;
  613. }
  614. printk(KERN_INFO "windfarm: Initializing for iMacG5 model ID %d\n",
  615. wf_smu_mach_model);
  616. return 0;
  617. }
  618. static int wf_smu_probe(struct platform_device *ddev)
  619. {
  620. wf_register_client(&wf_smu_events);
  621. return 0;
  622. }
  623. static int wf_smu_remove(struct platform_device *ddev)
  624. {
  625. wf_unregister_client(&wf_smu_events);
  626. /* XXX We don't have yet a guarantee that our callback isn't
  627. * in progress when returning from wf_unregister_client, so
  628. * we add an arbitrary delay. I'll have to fix that in the core
  629. */
  630. msleep(1000);
  631. /* Release all sensors */
  632. /* One more crappy race: I don't think we have any guarantee here
  633. * that the attribute callback won't race with the sensor beeing
  634. * disposed of, and I'm not 100% certain what best way to deal
  635. * with that except by adding locks all over... I'll do that
  636. * eventually but heh, who ever rmmod this module anyway ?
  637. */
  638. if (sensor_cpu_power)
  639. wf_put_sensor(sensor_cpu_power);
  640. if (sensor_cpu_temp)
  641. wf_put_sensor(sensor_cpu_temp);
  642. if (sensor_hd_temp)
  643. wf_put_sensor(sensor_hd_temp);
  644. /* Release all controls */
  645. if (fan_cpu_main)
  646. wf_put_control(fan_cpu_main);
  647. if (fan_hd)
  648. wf_put_control(fan_hd);
  649. if (fan_system)
  650. wf_put_control(fan_system);
  651. if (cpufreq_clamp)
  652. wf_put_control(cpufreq_clamp);
  653. /* Destroy control loops state structures */
  654. kfree(wf_smu_sys_fans);
  655. kfree(wf_smu_cpu_fans);
  656. return 0;
  657. }
  658. static struct platform_driver wf_smu_driver = {
  659. .probe = wf_smu_probe,
  660. .remove = wf_smu_remove,
  661. .driver = {
  662. .name = "windfarm",
  663. },
  664. };
  665. static int __init wf_smu_init(void)
  666. {
  667. int rc = -ENODEV;
  668. if (of_machine_is_compatible("PowerMac8,1") ||
  669. of_machine_is_compatible("PowerMac8,2"))
  670. rc = wf_init_pm();
  671. if (rc == 0) {
  672. #ifdef MODULE
  673. request_module("windfarm_smu_controls");
  674. request_module("windfarm_smu_sensors");
  675. request_module("windfarm_lm75_sensor");
  676. request_module("windfarm_cpufreq_clamp");
  677. #endif /* MODULE */
  678. platform_driver_register(&wf_smu_driver);
  679. }
  680. return rc;
  681. }
  682. static void __exit wf_smu_exit(void)
  683. {
  684. platform_driver_unregister(&wf_smu_driver);
  685. }
  686. module_init(wf_smu_init);
  687. module_exit(wf_smu_exit);
  688. MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");
  689. MODULE_DESCRIPTION("Thermal control logic for iMac G5");
  690. MODULE_LICENSE("GPL");
  691. MODULE_ALIAS("platform:windfarm");