windfarm_pm121.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * Windfarm PowerMac thermal control. iMac G5 iSight
  3. *
  4. * (c) Copyright 2007 Étienne Bersac <bersace@gmail.com>
  5. *
  6. * Bits & pieces from windfarm_pm81.c by (c) Copyright 2005 Benjamin
  7. * Herrenschmidt, IBM Corp. <benh@kernel.crashing.org>
  8. *
  9. * Released under the term of the GNU GPL v2.
  10. *
  11. *
  12. *
  13. * PowerMac12,1
  14. * ============
  15. *
  16. *
  17. * The algorithm used is the PID control algorithm, used the same way
  18. * the published Darwin code does, using the same values that are
  19. * present in the Darwin 8.10 snapshot property lists (note however
  20. * that none of the code has been re-used, it's a complete
  21. * re-implementation
  22. *
  23. * There is two models using PowerMac12,1. Model 2 is iMac G5 iSight
  24. * 17" while Model 3 is iMac G5 20". They do have both the same
  25. * controls with a tiny difference. The control-ids of hard-drive-fan
  26. * and cpu-fan is swapped.
  27. *
  28. *
  29. * Target Correction :
  30. *
  31. * controls have a target correction calculated as :
  32. *
  33. * new_min = ((((average_power * slope) >> 16) + offset) >> 16) + min_value
  34. * new_value = max(new_value, max(new_min, 0))
  35. *
  36. * OD Fan control correction.
  37. *
  38. * # model_id: 2
  39. * offset : -19563152
  40. * slope : 1956315
  41. *
  42. * # model_id: 3
  43. * offset : -15650652
  44. * slope : 1565065
  45. *
  46. * HD Fan control correction.
  47. *
  48. * # model_id: 2
  49. * offset : -15650652
  50. * slope : 1565065
  51. *
  52. * # model_id: 3
  53. * offset : -19563152
  54. * slope : 1956315
  55. *
  56. * CPU Fan control correction.
  57. *
  58. * # model_id: 2
  59. * offset : -25431900
  60. * slope : 2543190
  61. *
  62. * # model_id: 3
  63. * offset : -15650652
  64. * slope : 1565065
  65. *
  66. *
  67. * Target rubber-banding :
  68. *
  69. * Some controls have a target correction which depends on another
  70. * control value. The correction is computed in the following way :
  71. *
  72. * new_min = ref_value * slope + offset
  73. *
  74. * ref_value is the value of the reference control. If new_min is
  75. * greater than 0, then we correct the target value using :
  76. *
  77. * new_target = max (new_target, new_min >> 16)
  78. *
  79. *
  80. * # model_id : 2
  81. * control : cpu-fan
  82. * ref : optical-drive-fan
  83. * offset : -15650652
  84. * slope : 1565065
  85. *
  86. * # model_id : 3
  87. * control : optical-drive-fan
  88. * ref : hard-drive-fan
  89. * offset : -32768000
  90. * slope : 65536
  91. *
  92. *
  93. * In order to have the moste efficient correction with those
  94. * dependencies, we must trigger HD loop before OD loop before CPU
  95. * loop.
  96. *
  97. *
  98. * The various control loops found in Darwin config file are:
  99. *
  100. * HD Fan control loop.
  101. *
  102. * # model_id: 2
  103. * control : hard-drive-fan
  104. * sensor : hard-drive-temp
  105. * PID params : G_d = 0x00000000
  106. * G_p = 0x002D70A3
  107. * G_r = 0x00019999
  108. * History = 2 entries
  109. * Input target = 0x370000
  110. * Interval = 5s
  111. *
  112. * # model_id: 3
  113. * control : hard-drive-fan
  114. * sensor : hard-drive-temp
  115. * PID params : G_d = 0x00000000
  116. * G_p = 0x002170A3
  117. * G_r = 0x00019999
  118. * History = 2 entries
  119. * Input target = 0x370000
  120. * Interval = 5s
  121. *
  122. * OD Fan control loop.
  123. *
  124. * # model_id: 2
  125. * control : optical-drive-fan
  126. * sensor : optical-drive-temp
  127. * PID params : G_d = 0x00000000
  128. * G_p = 0x001FAE14
  129. * G_r = 0x00019999
  130. * History = 2 entries
  131. * Input target = 0x320000
  132. * Interval = 5s
  133. *
  134. * # model_id: 3
  135. * control : optical-drive-fan
  136. * sensor : optical-drive-temp
  137. * PID params : G_d = 0x00000000
  138. * G_p = 0x001FAE14
  139. * G_r = 0x00019999
  140. * History = 2 entries
  141. * Input target = 0x320000
  142. * Interval = 5s
  143. *
  144. * GPU Fan control loop.
  145. *
  146. * # model_id: 2
  147. * control : hard-drive-fan
  148. * sensor : gpu-temp
  149. * PID params : G_d = 0x00000000
  150. * G_p = 0x002A6666
  151. * G_r = 0x00019999
  152. * History = 2 entries
  153. * Input target = 0x5A0000
  154. * Interval = 5s
  155. *
  156. * # model_id: 3
  157. * control : cpu-fan
  158. * sensor : gpu-temp
  159. * PID params : G_d = 0x00000000
  160. * G_p = 0x0010CCCC
  161. * G_r = 0x00019999
  162. * History = 2 entries
  163. * Input target = 0x500000
  164. * Interval = 5s
  165. *
  166. * KODIAK (aka northbridge) Fan control loop.
  167. *
  168. * # model_id: 2
  169. * control : optical-drive-fan
  170. * sensor : north-bridge-temp
  171. * PID params : G_d = 0x00000000
  172. * G_p = 0x003BD70A
  173. * G_r = 0x00019999
  174. * History = 2 entries
  175. * Input target = 0x550000
  176. * Interval = 5s
  177. *
  178. * # model_id: 3
  179. * control : hard-drive-fan
  180. * sensor : north-bridge-temp
  181. * PID params : G_d = 0x00000000
  182. * G_p = 0x0030F5C2
  183. * G_r = 0x00019999
  184. * History = 2 entries
  185. * Input target = 0x550000
  186. * Interval = 5s
  187. *
  188. * CPU Fan control loop.
  189. *
  190. * control : cpu-fan
  191. * sensors : cpu-temp, cpu-power
  192. * PID params : from SDB partition
  193. *
  194. *
  195. * CPU Slew control loop.
  196. *
  197. * control : cpufreq-clamp
  198. * sensor : cpu-temp
  199. *
  200. */
  201. #undef DEBUG
  202. #include <linux/types.h>
  203. #include <linux/errno.h>
  204. #include <linux/kernel.h>
  205. #include <linux/delay.h>
  206. #include <linux/slab.h>
  207. #include <linux/init.h>
  208. #include <linux/spinlock.h>
  209. #include <linux/wait.h>
  210. #include <linux/kmod.h>
  211. #include <linux/device.h>
  212. #include <linux/platform_device.h>
  213. #include <asm/prom.h>
  214. #include <asm/machdep.h>
  215. #include <asm/io.h>
  216. #include <asm/sections.h>
  217. #include <asm/smu.h>
  218. #include "windfarm.h"
  219. #include "windfarm_pid.h"
  220. #define VERSION "0.3"
  221. static int pm121_mach_model; /* machine model id */
  222. /* Controls & sensors */
  223. static struct wf_sensor *sensor_cpu_power;
  224. static struct wf_sensor *sensor_cpu_temp;
  225. static struct wf_sensor *sensor_cpu_voltage;
  226. static struct wf_sensor *sensor_cpu_current;
  227. static struct wf_sensor *sensor_gpu_temp;
  228. static struct wf_sensor *sensor_north_bridge_temp;
  229. static struct wf_sensor *sensor_hard_drive_temp;
  230. static struct wf_sensor *sensor_optical_drive_temp;
  231. static struct wf_sensor *sensor_incoming_air_temp; /* unused ! */
  232. enum {
  233. FAN_CPU,
  234. FAN_HD,
  235. FAN_OD,
  236. CPUFREQ,
  237. N_CONTROLS
  238. };
  239. static struct wf_control *controls[N_CONTROLS] = {};
  240. /* Set to kick the control loop into life */
  241. static int pm121_all_controls_ok, pm121_all_sensors_ok;
  242. static bool pm121_started;
  243. enum {
  244. FAILURE_FAN = 1 << 0,
  245. FAILURE_SENSOR = 1 << 1,
  246. FAILURE_OVERTEMP = 1 << 2
  247. };
  248. /* All sys loops. Note the HD before the OD loop in order to have it
  249. run before. */
  250. enum {
  251. LOOP_GPU, /* control = hd or cpu, but luckily,
  252. it doesn't matter */
  253. LOOP_HD, /* control = hd */
  254. LOOP_KODIAK, /* control = hd or od */
  255. LOOP_OD, /* control = od */
  256. N_LOOPS
  257. };
  258. static const char *loop_names[N_LOOPS] = {
  259. "GPU",
  260. "HD",
  261. "KODIAK",
  262. "OD",
  263. };
  264. #define PM121_NUM_CONFIGS 2
  265. static unsigned int pm121_failure_state;
  266. static int pm121_readjust, pm121_skipping;
  267. static bool pm121_overtemp;
  268. static s32 average_power;
  269. struct pm121_correction {
  270. int offset;
  271. int slope;
  272. };
  273. static struct pm121_correction corrections[N_CONTROLS][PM121_NUM_CONFIGS] = {
  274. /* FAN_OD */
  275. {
  276. /* MODEL 2 */
  277. { .offset = -19563152,
  278. .slope = 1956315
  279. },
  280. /* MODEL 3 */
  281. { .offset = -15650652,
  282. .slope = 1565065
  283. },
  284. },
  285. /* FAN_HD */
  286. {
  287. /* MODEL 2 */
  288. { .offset = -15650652,
  289. .slope = 1565065
  290. },
  291. /* MODEL 3 */
  292. { .offset = -19563152,
  293. .slope = 1956315
  294. },
  295. },
  296. /* FAN_CPU */
  297. {
  298. /* MODEL 2 */
  299. { .offset = -25431900,
  300. .slope = 2543190
  301. },
  302. /* MODEL 3 */
  303. { .offset = -15650652,
  304. .slope = 1565065
  305. },
  306. },
  307. /* CPUFREQ has no correction (and is not implemented at all) */
  308. };
  309. struct pm121_connection {
  310. unsigned int control_id;
  311. unsigned int ref_id;
  312. struct pm121_correction correction;
  313. };
  314. static struct pm121_connection pm121_connections[] = {
  315. /* MODEL 2 */
  316. { .control_id = FAN_CPU,
  317. .ref_id = FAN_OD,
  318. { .offset = -32768000,
  319. .slope = 65536
  320. }
  321. },
  322. /* MODEL 3 */
  323. { .control_id = FAN_OD,
  324. .ref_id = FAN_HD,
  325. { .offset = -32768000,
  326. .slope = 65536
  327. }
  328. },
  329. };
  330. /* pointer to the current model connection */
  331. static struct pm121_connection *pm121_connection;
  332. /*
  333. * ****** System Fans Control Loop ******
  334. *
  335. */
  336. /* Since each loop handles only one control and we want to avoid
  337. * writing virtual control, we store the control correction with the
  338. * loop params. Some data are not set, there are common to all loop
  339. * and thus, hardcoded.
  340. */
  341. struct pm121_sys_param {
  342. /* purely informative since we use mach_model-2 as index */
  343. int model_id;
  344. struct wf_sensor **sensor; /* use sensor_id instead ? */
  345. s32 gp, itarget;
  346. unsigned int control_id;
  347. };
  348. static struct pm121_sys_param
  349. pm121_sys_all_params[N_LOOPS][PM121_NUM_CONFIGS] = {
  350. /* GPU Fan control loop */
  351. {
  352. { .model_id = 2,
  353. .sensor = &sensor_gpu_temp,
  354. .gp = 0x002A6666,
  355. .itarget = 0x5A0000,
  356. .control_id = FAN_HD,
  357. },
  358. { .model_id = 3,
  359. .sensor = &sensor_gpu_temp,
  360. .gp = 0x0010CCCC,
  361. .itarget = 0x500000,
  362. .control_id = FAN_CPU,
  363. },
  364. },
  365. /* HD Fan control loop */
  366. {
  367. { .model_id = 2,
  368. .sensor = &sensor_hard_drive_temp,
  369. .gp = 0x002D70A3,
  370. .itarget = 0x370000,
  371. .control_id = FAN_HD,
  372. },
  373. { .model_id = 3,
  374. .sensor = &sensor_hard_drive_temp,
  375. .gp = 0x002170A3,
  376. .itarget = 0x370000,
  377. .control_id = FAN_HD,
  378. },
  379. },
  380. /* KODIAK Fan control loop */
  381. {
  382. { .model_id = 2,
  383. .sensor = &sensor_north_bridge_temp,
  384. .gp = 0x003BD70A,
  385. .itarget = 0x550000,
  386. .control_id = FAN_OD,
  387. },
  388. { .model_id = 3,
  389. .sensor = &sensor_north_bridge_temp,
  390. .gp = 0x0030F5C2,
  391. .itarget = 0x550000,
  392. .control_id = FAN_HD,
  393. },
  394. },
  395. /* OD Fan control loop */
  396. {
  397. { .model_id = 2,
  398. .sensor = &sensor_optical_drive_temp,
  399. .gp = 0x001FAE14,
  400. .itarget = 0x320000,
  401. .control_id = FAN_OD,
  402. },
  403. { .model_id = 3,
  404. .sensor = &sensor_optical_drive_temp,
  405. .gp = 0x001FAE14,
  406. .itarget = 0x320000,
  407. .control_id = FAN_OD,
  408. },
  409. },
  410. };
  411. /* the hardcoded values */
  412. #define PM121_SYS_GD 0x00000000
  413. #define PM121_SYS_GR 0x00019999
  414. #define PM121_SYS_HISTORY_SIZE 2
  415. #define PM121_SYS_INTERVAL 5
  416. /* State data used by the system fans control loop
  417. */
  418. struct pm121_sys_state {
  419. int ticks;
  420. s32 setpoint;
  421. struct wf_pid_state pid;
  422. };
  423. struct pm121_sys_state *pm121_sys_state[N_LOOPS] = {};
  424. /*
  425. * ****** CPU Fans Control Loop ******
  426. *
  427. */
  428. #define PM121_CPU_INTERVAL 1
  429. /* State data used by the cpu fans control loop
  430. */
  431. struct pm121_cpu_state {
  432. int ticks;
  433. s32 setpoint;
  434. struct wf_cpu_pid_state pid;
  435. };
  436. static struct pm121_cpu_state *pm121_cpu_state;
  437. /*
  438. * ***** Implementation *****
  439. *
  440. */
  441. /* correction the value using the output-low-bound correction algo */
  442. static s32 pm121_correct(s32 new_setpoint,
  443. unsigned int control_id,
  444. s32 min)
  445. {
  446. s32 new_min;
  447. struct pm121_correction *correction;
  448. correction = &corrections[control_id][pm121_mach_model - 2];
  449. new_min = (average_power * correction->slope) >> 16;
  450. new_min += correction->offset;
  451. new_min = (new_min >> 16) + min;
  452. return max3(new_setpoint, new_min, 0);
  453. }
  454. static s32 pm121_connect(unsigned int control_id, s32 setpoint)
  455. {
  456. s32 new_min, value, new_setpoint;
  457. if (pm121_connection->control_id == control_id) {
  458. controls[control_id]->ops->get_value(controls[control_id],
  459. &value);
  460. new_min = value * pm121_connection->correction.slope;
  461. new_min += pm121_connection->correction.offset;
  462. if (new_min > 0) {
  463. new_setpoint = max(setpoint, (new_min >> 16));
  464. if (new_setpoint != setpoint) {
  465. pr_debug("pm121: %s depending on %s, "
  466. "corrected from %d to %d RPM\n",
  467. controls[control_id]->name,
  468. controls[pm121_connection->ref_id]->name,
  469. (int) setpoint, (int) new_setpoint);
  470. }
  471. } else
  472. new_setpoint = setpoint;
  473. }
  474. /* no connection */
  475. else
  476. new_setpoint = setpoint;
  477. return new_setpoint;
  478. }
  479. /* FAN LOOPS */
  480. static void pm121_create_sys_fans(int loop_id)
  481. {
  482. struct pm121_sys_param *param = NULL;
  483. struct wf_pid_param pid_param;
  484. struct wf_control *control = NULL;
  485. int i;
  486. /* First, locate the params for this model */
  487. for (i = 0; i < PM121_NUM_CONFIGS; i++) {
  488. if (pm121_sys_all_params[loop_id][i].model_id == pm121_mach_model) {
  489. param = &(pm121_sys_all_params[loop_id][i]);
  490. break;
  491. }
  492. }
  493. /* No params found, put fans to max */
  494. if (param == NULL) {
  495. printk(KERN_WARNING "pm121: %s fan config not found "
  496. " for this machine model\n",
  497. loop_names[loop_id]);
  498. goto fail;
  499. }
  500. control = controls[param->control_id];
  501. /* Alloc & initialize state */
  502. pm121_sys_state[loop_id] = kmalloc(sizeof(struct pm121_sys_state),
  503. GFP_KERNEL);
  504. if (pm121_sys_state[loop_id] == NULL) {
  505. printk(KERN_WARNING "pm121: Memory allocation error\n");
  506. goto fail;
  507. }
  508. pm121_sys_state[loop_id]->ticks = 1;
  509. /* Fill PID params */
  510. pid_param.gd = PM121_SYS_GD;
  511. pid_param.gp = param->gp;
  512. pid_param.gr = PM121_SYS_GR;
  513. pid_param.interval = PM121_SYS_INTERVAL;
  514. pid_param.history_len = PM121_SYS_HISTORY_SIZE;
  515. pid_param.itarget = param->itarget;
  516. if(control)
  517. {
  518. pid_param.min = control->ops->get_min(control);
  519. pid_param.max = control->ops->get_max(control);
  520. } else {
  521. /*
  522. * This is probably not the right!?
  523. * Perhaps goto fail if control == NULL above?
  524. */
  525. pid_param.min = 0;
  526. pid_param.max = 0;
  527. }
  528. wf_pid_init(&pm121_sys_state[loop_id]->pid, &pid_param);
  529. pr_debug("pm121: %s Fan control loop initialized.\n"
  530. " itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
  531. loop_names[loop_id], FIX32TOPRINT(pid_param.itarget),
  532. pid_param.min, pid_param.max);
  533. return;
  534. fail:
  535. /* note that this is not optimal since another loop may still
  536. control the same control */
  537. printk(KERN_WARNING "pm121: failed to set up %s loop "
  538. "setting \"%s\" to max speed.\n",
  539. loop_names[loop_id], control ? control->name : "uninitialized value");
  540. if (control)
  541. wf_control_set_max(control);
  542. }
  543. static void pm121_sys_fans_tick(int loop_id)
  544. {
  545. struct pm121_sys_param *param;
  546. struct pm121_sys_state *st;
  547. struct wf_sensor *sensor;
  548. struct wf_control *control;
  549. s32 temp, new_setpoint;
  550. int rc;
  551. param = &(pm121_sys_all_params[loop_id][pm121_mach_model-2]);
  552. st = pm121_sys_state[loop_id];
  553. sensor = *(param->sensor);
  554. control = controls[param->control_id];
  555. if (--st->ticks != 0) {
  556. if (pm121_readjust)
  557. goto readjust;
  558. return;
  559. }
  560. st->ticks = PM121_SYS_INTERVAL;
  561. rc = sensor->ops->get_value(sensor, &temp);
  562. if (rc) {
  563. printk(KERN_WARNING "windfarm: %s sensor error %d\n",
  564. sensor->name, rc);
  565. pm121_failure_state |= FAILURE_SENSOR;
  566. return;
  567. }
  568. pr_debug("pm121: %s Fan tick ! %s: %d.%03d\n",
  569. loop_names[loop_id], sensor->name,
  570. FIX32TOPRINT(temp));
  571. new_setpoint = wf_pid_run(&st->pid, temp);
  572. /* correction */
  573. new_setpoint = pm121_correct(new_setpoint,
  574. param->control_id,
  575. st->pid.param.min);
  576. /* linked corretion */
  577. new_setpoint = pm121_connect(param->control_id, new_setpoint);
  578. if (new_setpoint == st->setpoint)
  579. return;
  580. st->setpoint = new_setpoint;
  581. pr_debug("pm121: %s corrected setpoint: %d RPM\n",
  582. control->name, (int)new_setpoint);
  583. readjust:
  584. if (control && pm121_failure_state == 0) {
  585. rc = control->ops->set_value(control, st->setpoint);
  586. if (rc) {
  587. printk(KERN_WARNING "windfarm: %s fan error %d\n",
  588. control->name, rc);
  589. pm121_failure_state |= FAILURE_FAN;
  590. }
  591. }
  592. }
  593. /* CPU LOOP */
  594. static void pm121_create_cpu_fans(void)
  595. {
  596. struct wf_cpu_pid_param pid_param;
  597. const struct smu_sdbp_header *hdr;
  598. struct smu_sdbp_cpupiddata *piddata;
  599. struct smu_sdbp_fvt *fvt;
  600. struct wf_control *fan_cpu;
  601. s32 tmax, tdelta, maxpow, powadj;
  602. fan_cpu = controls[FAN_CPU];
  603. /* First, locate the PID params in SMU SBD */
  604. hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
  605. if (hdr == 0) {
  606. printk(KERN_WARNING "pm121: CPU PID fan config not found.\n");
  607. goto fail;
  608. }
  609. piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
  610. /* Get the FVT params for operating point 0 (the only supported one
  611. * for now) in order to get tmax
  612. */
  613. hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  614. if (hdr) {
  615. fvt = (struct smu_sdbp_fvt *)&hdr[1];
  616. tmax = ((s32)fvt->maxtemp) << 16;
  617. } else
  618. tmax = 0x5e0000; /* 94 degree default */
  619. /* Alloc & initialize state */
  620. pm121_cpu_state = kmalloc(sizeof(struct pm121_cpu_state),
  621. GFP_KERNEL);
  622. if (pm121_cpu_state == NULL)
  623. goto fail;
  624. pm121_cpu_state->ticks = 1;
  625. /* Fill PID params */
  626. pid_param.interval = PM121_CPU_INTERVAL;
  627. pid_param.history_len = piddata->history_len;
  628. if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
  629. printk(KERN_WARNING "pm121: History size overflow on "
  630. "CPU control loop (%d)\n", piddata->history_len);
  631. pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
  632. }
  633. pid_param.gd = piddata->gd;
  634. pid_param.gp = piddata->gp;
  635. pid_param.gr = piddata->gr / pid_param.history_len;
  636. tdelta = ((s32)piddata->target_temp_delta) << 16;
  637. maxpow = ((s32)piddata->max_power) << 16;
  638. powadj = ((s32)piddata->power_adj) << 16;
  639. pid_param.tmax = tmax;
  640. pid_param.ttarget = tmax - tdelta;
  641. pid_param.pmaxadj = maxpow - powadj;
  642. pid_param.min = fan_cpu->ops->get_min(fan_cpu);
  643. pid_param.max = fan_cpu->ops->get_max(fan_cpu);
  644. wf_cpu_pid_init(&pm121_cpu_state->pid, &pid_param);
  645. pr_debug("pm121: CPU Fan control initialized.\n");
  646. pr_debug(" ttarget=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM,\n",
  647. FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
  648. pid_param.min, pid_param.max);
  649. return;
  650. fail:
  651. printk(KERN_WARNING "pm121: CPU fan config not found, max fan speed\n");
  652. if (controls[CPUFREQ])
  653. wf_control_set_max(controls[CPUFREQ]);
  654. if (fan_cpu)
  655. wf_control_set_max(fan_cpu);
  656. }
  657. static void pm121_cpu_fans_tick(struct pm121_cpu_state *st)
  658. {
  659. s32 new_setpoint, temp, power;
  660. struct wf_control *fan_cpu = NULL;
  661. int rc;
  662. if (--st->ticks != 0) {
  663. if (pm121_readjust)
  664. goto readjust;
  665. return;
  666. }
  667. st->ticks = PM121_CPU_INTERVAL;
  668. fan_cpu = controls[FAN_CPU];
  669. rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);
  670. if (rc) {
  671. printk(KERN_WARNING "pm121: CPU temp sensor error %d\n",
  672. rc);
  673. pm121_failure_state |= FAILURE_SENSOR;
  674. return;
  675. }
  676. rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);
  677. if (rc) {
  678. printk(KERN_WARNING "pm121: CPU power sensor error %d\n",
  679. rc);
  680. pm121_failure_state |= FAILURE_SENSOR;
  681. return;
  682. }
  683. pr_debug("pm121: CPU Fans tick ! CPU temp: %d.%03d°C, power: %d.%03d\n",
  684. FIX32TOPRINT(temp), FIX32TOPRINT(power));
  685. if (temp > st->pid.param.tmax)
  686. pm121_failure_state |= FAILURE_OVERTEMP;
  687. new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
  688. /* correction */
  689. new_setpoint = pm121_correct(new_setpoint,
  690. FAN_CPU,
  691. st->pid.param.min);
  692. /* connected correction */
  693. new_setpoint = pm121_connect(FAN_CPU, new_setpoint);
  694. if (st->setpoint == new_setpoint)
  695. return;
  696. st->setpoint = new_setpoint;
  697. pr_debug("pm121: CPU corrected setpoint: %d RPM\n", (int)new_setpoint);
  698. readjust:
  699. if (fan_cpu && pm121_failure_state == 0) {
  700. rc = fan_cpu->ops->set_value(fan_cpu, st->setpoint);
  701. if (rc) {
  702. printk(KERN_WARNING "pm121: %s fan error %d\n",
  703. fan_cpu->name, rc);
  704. pm121_failure_state |= FAILURE_FAN;
  705. }
  706. }
  707. }
  708. /*
  709. * ****** Common ******
  710. *
  711. */
  712. static void pm121_tick(void)
  713. {
  714. unsigned int last_failure = pm121_failure_state;
  715. unsigned int new_failure;
  716. s32 total_power;
  717. int i;
  718. if (!pm121_started) {
  719. pr_debug("pm121: creating control loops !\n");
  720. for (i = 0; i < N_LOOPS; i++)
  721. pm121_create_sys_fans(i);
  722. pm121_create_cpu_fans();
  723. pm121_started = true;
  724. }
  725. /* skipping ticks */
  726. if (pm121_skipping && --pm121_skipping)
  727. return;
  728. /* compute average power */
  729. total_power = 0;
  730. for (i = 0; i < pm121_cpu_state->pid.param.history_len; i++)
  731. total_power += pm121_cpu_state->pid.powers[i];
  732. average_power = total_power / pm121_cpu_state->pid.param.history_len;
  733. pm121_failure_state = 0;
  734. for (i = 0 ; i < N_LOOPS; i++) {
  735. if (pm121_sys_state[i])
  736. pm121_sys_fans_tick(i);
  737. }
  738. if (pm121_cpu_state)
  739. pm121_cpu_fans_tick(pm121_cpu_state);
  740. pm121_readjust = 0;
  741. new_failure = pm121_failure_state & ~last_failure;
  742. /* If entering failure mode, clamp cpufreq and ramp all
  743. * fans to full speed.
  744. */
  745. if (pm121_failure_state && !last_failure) {
  746. for (i = 0; i < N_CONTROLS; i++) {
  747. if (controls[i])
  748. wf_control_set_max(controls[i]);
  749. }
  750. }
  751. /* If leaving failure mode, unclamp cpufreq and readjust
  752. * all fans on next iteration
  753. */
  754. if (!pm121_failure_state && last_failure) {
  755. if (controls[CPUFREQ])
  756. wf_control_set_min(controls[CPUFREQ]);
  757. pm121_readjust = 1;
  758. }
  759. /* Overtemp condition detected, notify and start skipping a couple
  760. * ticks to let the temperature go down
  761. */
  762. if (new_failure & FAILURE_OVERTEMP) {
  763. wf_set_overtemp();
  764. pm121_skipping = 2;
  765. pm121_overtemp = true;
  766. }
  767. /* We only clear the overtemp condition if overtemp is cleared
  768. * _and_ no other failure is present. Since a sensor error will
  769. * clear the overtemp condition (can't measure temperature) at
  770. * the control loop levels, but we don't want to keep it clear
  771. * here in this case
  772. */
  773. if (!pm121_failure_state && pm121_overtemp) {
  774. wf_clear_overtemp();
  775. pm121_overtemp = false;
  776. }
  777. }
  778. static struct wf_control* pm121_register_control(struct wf_control *ct,
  779. const char *match,
  780. unsigned int id)
  781. {
  782. if (controls[id] == NULL && !strcmp(ct->name, match)) {
  783. if (wf_get_control(ct) == 0)
  784. controls[id] = ct;
  785. }
  786. return controls[id];
  787. }
  788. static void pm121_new_control(struct wf_control *ct)
  789. {
  790. int all = 1;
  791. if (pm121_all_controls_ok)
  792. return;
  793. all = pm121_register_control(ct, "optical-drive-fan", FAN_OD) && all;
  794. all = pm121_register_control(ct, "hard-drive-fan", FAN_HD) && all;
  795. all = pm121_register_control(ct, "cpu-fan", FAN_CPU) && all;
  796. all = pm121_register_control(ct, "cpufreq-clamp", CPUFREQ) && all;
  797. if (all)
  798. pm121_all_controls_ok = 1;
  799. }
  800. static struct wf_sensor* pm121_register_sensor(struct wf_sensor *sensor,
  801. const char *match,
  802. struct wf_sensor **var)
  803. {
  804. if (*var == NULL && !strcmp(sensor->name, match)) {
  805. if (wf_get_sensor(sensor) == 0)
  806. *var = sensor;
  807. }
  808. return *var;
  809. }
  810. static void pm121_new_sensor(struct wf_sensor *sr)
  811. {
  812. int all = 1;
  813. if (pm121_all_sensors_ok)
  814. return;
  815. all = pm121_register_sensor(sr, "cpu-temp",
  816. &sensor_cpu_temp) && all;
  817. all = pm121_register_sensor(sr, "cpu-current",
  818. &sensor_cpu_current) && all;
  819. all = pm121_register_sensor(sr, "cpu-voltage",
  820. &sensor_cpu_voltage) && all;
  821. all = pm121_register_sensor(sr, "cpu-power",
  822. &sensor_cpu_power) && all;
  823. all = pm121_register_sensor(sr, "hard-drive-temp",
  824. &sensor_hard_drive_temp) && all;
  825. all = pm121_register_sensor(sr, "optical-drive-temp",
  826. &sensor_optical_drive_temp) && all;
  827. all = pm121_register_sensor(sr, "incoming-air-temp",
  828. &sensor_incoming_air_temp) && all;
  829. all = pm121_register_sensor(sr, "north-bridge-temp",
  830. &sensor_north_bridge_temp) && all;
  831. all = pm121_register_sensor(sr, "gpu-temp",
  832. &sensor_gpu_temp) && all;
  833. if (all)
  834. pm121_all_sensors_ok = 1;
  835. }
  836. static int pm121_notify(struct notifier_block *self,
  837. unsigned long event, void *data)
  838. {
  839. switch (event) {
  840. case WF_EVENT_NEW_CONTROL:
  841. pr_debug("pm121: new control %s detected\n",
  842. ((struct wf_control *)data)->name);
  843. pm121_new_control(data);
  844. break;
  845. case WF_EVENT_NEW_SENSOR:
  846. pr_debug("pm121: new sensor %s detected\n",
  847. ((struct wf_sensor *)data)->name);
  848. pm121_new_sensor(data);
  849. break;
  850. case WF_EVENT_TICK:
  851. if (pm121_all_controls_ok && pm121_all_sensors_ok)
  852. pm121_tick();
  853. break;
  854. }
  855. return 0;
  856. }
  857. static struct notifier_block pm121_events = {
  858. .notifier_call = pm121_notify,
  859. };
  860. static int pm121_init_pm(void)
  861. {
  862. const struct smu_sdbp_header *hdr;
  863. hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
  864. if (hdr != 0) {
  865. struct smu_sdbp_sensortree *st =
  866. (struct smu_sdbp_sensortree *)&hdr[1];
  867. pm121_mach_model = st->model_id;
  868. }
  869. pm121_connection = &pm121_connections[pm121_mach_model - 2];
  870. printk(KERN_INFO "pm121: Initializing for iMac G5 iSight model ID %d\n",
  871. pm121_mach_model);
  872. return 0;
  873. }
  874. static int pm121_probe(struct platform_device *ddev)
  875. {
  876. wf_register_client(&pm121_events);
  877. return 0;
  878. }
  879. static int pm121_remove(struct platform_device *ddev)
  880. {
  881. wf_unregister_client(&pm121_events);
  882. return 0;
  883. }
  884. static struct platform_driver pm121_driver = {
  885. .probe = pm121_probe,
  886. .remove = pm121_remove,
  887. .driver = {
  888. .name = "windfarm",
  889. .bus = &platform_bus_type,
  890. },
  891. };
  892. static int __init pm121_init(void)
  893. {
  894. int rc = -ENODEV;
  895. if (of_machine_is_compatible("PowerMac12,1"))
  896. rc = pm121_init_pm();
  897. if (rc == 0) {
  898. request_module("windfarm_smu_controls");
  899. request_module("windfarm_smu_sensors");
  900. request_module("windfarm_smu_sat");
  901. request_module("windfarm_lm75_sensor");
  902. request_module("windfarm_max6690_sensor");
  903. request_module("windfarm_cpufreq_clamp");
  904. platform_driver_register(&pm121_driver);
  905. }
  906. return rc;
  907. }
  908. static void __exit pm121_exit(void)
  909. {
  910. platform_driver_unregister(&pm121_driver);
  911. }
  912. module_init(pm121_init);
  913. module_exit(pm121_exit);
  914. MODULE_AUTHOR("Étienne Bersac <bersace@gmail.com>");
  915. MODULE_DESCRIPTION("Thermal control logic for iMac G5 (iSight)");
  916. MODULE_LICENSE("GPL");