windfarm_pm121.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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, pm121_started;
  242. enum {
  243. FAILURE_FAN = 1 << 0,
  244. FAILURE_SENSOR = 1 << 1,
  245. FAILURE_OVERTEMP = 1 << 2
  246. };
  247. /* All sys loops. Note the HD before the OD loop in order to have it
  248. run before. */
  249. enum {
  250. LOOP_GPU, /* control = hd or cpu, but luckily,
  251. it doesn't matter */
  252. LOOP_HD, /* control = hd */
  253. LOOP_KODIAK, /* control = hd or od */
  254. LOOP_OD, /* control = od */
  255. N_LOOPS
  256. };
  257. static const char *loop_names[N_LOOPS] = {
  258. "GPU",
  259. "HD",
  260. "KODIAK",
  261. "OD",
  262. };
  263. #define PM121_NUM_CONFIGS 2
  264. static unsigned int pm121_failure_state;
  265. static int pm121_readjust, pm121_skipping;
  266. static bool pm121_overtemp;
  267. static s32 average_power;
  268. struct pm121_correction {
  269. int offset;
  270. int slope;
  271. };
  272. static struct pm121_correction corrections[N_CONTROLS][PM121_NUM_CONFIGS] = {
  273. /* FAN_OD */
  274. {
  275. /* MODEL 2 */
  276. { .offset = -19563152,
  277. .slope = 1956315
  278. },
  279. /* MODEL 3 */
  280. { .offset = -15650652,
  281. .slope = 1565065
  282. },
  283. },
  284. /* FAN_HD */
  285. {
  286. /* MODEL 2 */
  287. { .offset = -15650652,
  288. .slope = 1565065
  289. },
  290. /* MODEL 3 */
  291. { .offset = -19563152,
  292. .slope = 1956315
  293. },
  294. },
  295. /* FAN_CPU */
  296. {
  297. /* MODEL 2 */
  298. { .offset = -25431900,
  299. .slope = 2543190
  300. },
  301. /* MODEL 3 */
  302. { .offset = -15650652,
  303. .slope = 1565065
  304. },
  305. },
  306. /* CPUFREQ has no correction (and is not implemented at all) */
  307. };
  308. struct pm121_connection {
  309. unsigned int control_id;
  310. unsigned int ref_id;
  311. struct pm121_correction correction;
  312. };
  313. static struct pm121_connection pm121_connections[] = {
  314. /* MODEL 2 */
  315. { .control_id = FAN_CPU,
  316. .ref_id = FAN_OD,
  317. { .offset = -32768000,
  318. .slope = 65536
  319. }
  320. },
  321. /* MODEL 3 */
  322. { .control_id = FAN_OD,
  323. .ref_id = FAN_HD,
  324. { .offset = -32768000,
  325. .slope = 65536
  326. }
  327. },
  328. };
  329. /* pointer to the current model connection */
  330. static struct pm121_connection *pm121_connection;
  331. /*
  332. * ****** System Fans Control Loop ******
  333. *
  334. */
  335. /* Since each loop handles only one control and we want to avoid
  336. * writing virtual control, we store the control correction with the
  337. * loop params. Some data are not set, there are common to all loop
  338. * and thus, hardcoded.
  339. */
  340. struct pm121_sys_param {
  341. /* purely informative since we use mach_model-2 as index */
  342. int model_id;
  343. struct wf_sensor **sensor; /* use sensor_id instead ? */
  344. s32 gp, itarget;
  345. unsigned int control_id;
  346. };
  347. static struct pm121_sys_param
  348. pm121_sys_all_params[N_LOOPS][PM121_NUM_CONFIGS] = {
  349. /* GPU Fan control loop */
  350. {
  351. { .model_id = 2,
  352. .sensor = &sensor_gpu_temp,
  353. .gp = 0x002A6666,
  354. .itarget = 0x5A0000,
  355. .control_id = FAN_HD,
  356. },
  357. { .model_id = 3,
  358. .sensor = &sensor_gpu_temp,
  359. .gp = 0x0010CCCC,
  360. .itarget = 0x500000,
  361. .control_id = FAN_CPU,
  362. },
  363. },
  364. /* HD Fan control loop */
  365. {
  366. { .model_id = 2,
  367. .sensor = &sensor_hard_drive_temp,
  368. .gp = 0x002D70A3,
  369. .itarget = 0x370000,
  370. .control_id = FAN_HD,
  371. },
  372. { .model_id = 3,
  373. .sensor = &sensor_hard_drive_temp,
  374. .gp = 0x002170A3,
  375. .itarget = 0x370000,
  376. .control_id = FAN_HD,
  377. },
  378. },
  379. /* KODIAK Fan control loop */
  380. {
  381. { .model_id = 2,
  382. .sensor = &sensor_north_bridge_temp,
  383. .gp = 0x003BD70A,
  384. .itarget = 0x550000,
  385. .control_id = FAN_OD,
  386. },
  387. { .model_id = 3,
  388. .sensor = &sensor_north_bridge_temp,
  389. .gp = 0x0030F5C2,
  390. .itarget = 0x550000,
  391. .control_id = FAN_HD,
  392. },
  393. },
  394. /* OD Fan control loop */
  395. {
  396. { .model_id = 2,
  397. .sensor = &sensor_optical_drive_temp,
  398. .gp = 0x001FAE14,
  399. .itarget = 0x320000,
  400. .control_id = FAN_OD,
  401. },
  402. { .model_id = 3,
  403. .sensor = &sensor_optical_drive_temp,
  404. .gp = 0x001FAE14,
  405. .itarget = 0x320000,
  406. .control_id = FAN_OD,
  407. },
  408. },
  409. };
  410. /* the hardcoded values */
  411. #define PM121_SYS_GD 0x00000000
  412. #define PM121_SYS_GR 0x00019999
  413. #define PM121_SYS_HISTORY_SIZE 2
  414. #define PM121_SYS_INTERVAL 5
  415. /* State data used by the system fans control loop
  416. */
  417. struct pm121_sys_state {
  418. int ticks;
  419. s32 setpoint;
  420. struct wf_pid_state pid;
  421. };
  422. struct pm121_sys_state *pm121_sys_state[N_LOOPS] = {};
  423. /*
  424. * ****** CPU Fans Control Loop ******
  425. *
  426. */
  427. #define PM121_CPU_INTERVAL 1
  428. /* State data used by the cpu fans control loop
  429. */
  430. struct pm121_cpu_state {
  431. int ticks;
  432. s32 setpoint;
  433. struct wf_cpu_pid_state pid;
  434. };
  435. static struct pm121_cpu_state *pm121_cpu_state;
  436. /*
  437. * ***** Implementation *****
  438. *
  439. */
  440. /* correction the value using the output-low-bound correction algo */
  441. static s32 pm121_correct(s32 new_setpoint,
  442. unsigned int control_id,
  443. s32 min)
  444. {
  445. s32 new_min;
  446. struct pm121_correction *correction;
  447. correction = &corrections[control_id][pm121_mach_model - 2];
  448. new_min = (average_power * correction->slope) >> 16;
  449. new_min += correction->offset;
  450. new_min = (new_min >> 16) + min;
  451. return max3(new_setpoint, new_min, 0);
  452. }
  453. static s32 pm121_connect(unsigned int control_id, s32 setpoint)
  454. {
  455. s32 new_min, value, new_setpoint;
  456. if (pm121_connection->control_id == control_id) {
  457. controls[control_id]->ops->get_value(controls[control_id],
  458. &value);
  459. new_min = value * pm121_connection->correction.slope;
  460. new_min += pm121_connection->correction.offset;
  461. if (new_min > 0) {
  462. new_setpoint = max(setpoint, (new_min >> 16));
  463. if (new_setpoint != setpoint) {
  464. pr_debug("pm121: %s depending on %s, "
  465. "corrected from %d to %d RPM\n",
  466. controls[control_id]->name,
  467. controls[pm121_connection->ref_id]->name,
  468. (int) setpoint, (int) new_setpoint);
  469. }
  470. } else
  471. new_setpoint = setpoint;
  472. }
  473. /* no connection */
  474. else
  475. new_setpoint = setpoint;
  476. return new_setpoint;
  477. }
  478. /* FAN LOOPS */
  479. static void pm121_create_sys_fans(int loop_id)
  480. {
  481. struct pm121_sys_param *param = NULL;
  482. struct wf_pid_param pid_param;
  483. struct wf_control *control = NULL;
  484. int i;
  485. /* First, locate the params for this model */
  486. for (i = 0; i < PM121_NUM_CONFIGS; i++) {
  487. if (pm121_sys_all_params[loop_id][i].model_id == pm121_mach_model) {
  488. param = &(pm121_sys_all_params[loop_id][i]);
  489. break;
  490. }
  491. }
  492. /* No params found, put fans to max */
  493. if (param == NULL) {
  494. printk(KERN_WARNING "pm121: %s fan config not found "
  495. " for this machine model\n",
  496. loop_names[loop_id]);
  497. goto fail;
  498. }
  499. control = controls[param->control_id];
  500. /* Alloc & initialize state */
  501. pm121_sys_state[loop_id] = kmalloc(sizeof(struct pm121_sys_state),
  502. GFP_KERNEL);
  503. if (pm121_sys_state[loop_id] == NULL) {
  504. printk(KERN_WARNING "pm121: Memory allocation error\n");
  505. goto fail;
  506. }
  507. pm121_sys_state[loop_id]->ticks = 1;
  508. /* Fill PID params */
  509. pid_param.gd = PM121_SYS_GD;
  510. pid_param.gp = param->gp;
  511. pid_param.gr = PM121_SYS_GR;
  512. pid_param.interval = PM121_SYS_INTERVAL;
  513. pid_param.history_len = PM121_SYS_HISTORY_SIZE;
  514. pid_param.itarget = param->itarget;
  515. if(control)
  516. {
  517. pid_param.min = control->ops->get_min(control);
  518. pid_param.max = control->ops->get_max(control);
  519. } else {
  520. /*
  521. * This is probably not the right!?
  522. * Perhaps goto fail if control == NULL above?
  523. */
  524. pid_param.min = 0;
  525. pid_param.max = 0;
  526. }
  527. wf_pid_init(&pm121_sys_state[loop_id]->pid, &pid_param);
  528. pr_debug("pm121: %s Fan control loop initialized.\n"
  529. " itarged=%d.%03d, min=%d RPM, max=%d RPM\n",
  530. loop_names[loop_id], FIX32TOPRINT(pid_param.itarget),
  531. pid_param.min, pid_param.max);
  532. return;
  533. fail:
  534. /* note that this is not optimal since another loop may still
  535. control the same control */
  536. printk(KERN_WARNING "pm121: failed to set up %s loop "
  537. "setting \"%s\" to max speed.\n",
  538. loop_names[loop_id], control ? control->name : "uninitialized value");
  539. if (control)
  540. wf_control_set_max(control);
  541. }
  542. static void pm121_sys_fans_tick(int loop_id)
  543. {
  544. struct pm121_sys_param *param;
  545. struct pm121_sys_state *st;
  546. struct wf_sensor *sensor;
  547. struct wf_control *control;
  548. s32 temp, new_setpoint;
  549. int rc;
  550. param = &(pm121_sys_all_params[loop_id][pm121_mach_model-2]);
  551. st = pm121_sys_state[loop_id];
  552. sensor = *(param->sensor);
  553. control = controls[param->control_id];
  554. if (--st->ticks != 0) {
  555. if (pm121_readjust)
  556. goto readjust;
  557. return;
  558. }
  559. st->ticks = PM121_SYS_INTERVAL;
  560. rc = sensor->ops->get_value(sensor, &temp);
  561. if (rc) {
  562. printk(KERN_WARNING "windfarm: %s sensor error %d\n",
  563. sensor->name, rc);
  564. pm121_failure_state |= FAILURE_SENSOR;
  565. return;
  566. }
  567. pr_debug("pm121: %s Fan tick ! %s: %d.%03d\n",
  568. loop_names[loop_id], sensor->name,
  569. FIX32TOPRINT(temp));
  570. new_setpoint = wf_pid_run(&st->pid, temp);
  571. /* correction */
  572. new_setpoint = pm121_correct(new_setpoint,
  573. param->control_id,
  574. st->pid.param.min);
  575. /* linked corretion */
  576. new_setpoint = pm121_connect(param->control_id, new_setpoint);
  577. if (new_setpoint == st->setpoint)
  578. return;
  579. st->setpoint = new_setpoint;
  580. pr_debug("pm121: %s corrected setpoint: %d RPM\n",
  581. control->name, (int)new_setpoint);
  582. readjust:
  583. if (control && pm121_failure_state == 0) {
  584. rc = control->ops->set_value(control, st->setpoint);
  585. if (rc) {
  586. printk(KERN_WARNING "windfarm: %s fan error %d\n",
  587. control->name, rc);
  588. pm121_failure_state |= FAILURE_FAN;
  589. }
  590. }
  591. }
  592. /* CPU LOOP */
  593. static void pm121_create_cpu_fans(void)
  594. {
  595. struct wf_cpu_pid_param pid_param;
  596. const struct smu_sdbp_header *hdr;
  597. struct smu_sdbp_cpupiddata *piddata;
  598. struct smu_sdbp_fvt *fvt;
  599. struct wf_control *fan_cpu;
  600. s32 tmax, tdelta, maxpow, powadj;
  601. fan_cpu = controls[FAN_CPU];
  602. /* First, locate the PID params in SMU SBD */
  603. hdr = smu_get_sdb_partition(SMU_SDB_CPUPIDDATA_ID, NULL);
  604. if (hdr == 0) {
  605. printk(KERN_WARNING "pm121: CPU PID fan config not found.\n");
  606. goto fail;
  607. }
  608. piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
  609. /* Get the FVT params for operating point 0 (the only supported one
  610. * for now) in order to get tmax
  611. */
  612. hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  613. if (hdr) {
  614. fvt = (struct smu_sdbp_fvt *)&hdr[1];
  615. tmax = ((s32)fvt->maxtemp) << 16;
  616. } else
  617. tmax = 0x5e0000; /* 94 degree default */
  618. /* Alloc & initialize state */
  619. pm121_cpu_state = kmalloc(sizeof(struct pm121_cpu_state),
  620. GFP_KERNEL);
  621. if (pm121_cpu_state == NULL)
  622. goto fail;
  623. pm121_cpu_state->ticks = 1;
  624. /* Fill PID params */
  625. pid_param.interval = PM121_CPU_INTERVAL;
  626. pid_param.history_len = piddata->history_len;
  627. if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {
  628. printk(KERN_WARNING "pm121: History size overflow on "
  629. "CPU control loop (%d)\n", piddata->history_len);
  630. pid_param.history_len = WF_CPU_PID_MAX_HISTORY;
  631. }
  632. pid_param.gd = piddata->gd;
  633. pid_param.gp = piddata->gp;
  634. pid_param.gr = piddata->gr / pid_param.history_len;
  635. tdelta = ((s32)piddata->target_temp_delta) << 16;
  636. maxpow = ((s32)piddata->max_power) << 16;
  637. powadj = ((s32)piddata->power_adj) << 16;
  638. pid_param.tmax = tmax;
  639. pid_param.ttarget = tmax - tdelta;
  640. pid_param.pmaxadj = maxpow - powadj;
  641. pid_param.min = fan_cpu->ops->get_min(fan_cpu);
  642. pid_param.max = fan_cpu->ops->get_max(fan_cpu);
  643. wf_cpu_pid_init(&pm121_cpu_state->pid, &pid_param);
  644. pr_debug("pm121: CPU Fan control initialized.\n");
  645. pr_debug(" ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM,\n",
  646. FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),
  647. pid_param.min, pid_param.max);
  648. return;
  649. fail:
  650. printk(KERN_WARNING "pm121: CPU fan config not found, max fan speed\n");
  651. if (controls[CPUFREQ])
  652. wf_control_set_max(controls[CPUFREQ]);
  653. if (fan_cpu)
  654. wf_control_set_max(fan_cpu);
  655. }
  656. static void pm121_cpu_fans_tick(struct pm121_cpu_state *st)
  657. {
  658. s32 new_setpoint, temp, power;
  659. struct wf_control *fan_cpu = NULL;
  660. int rc;
  661. if (--st->ticks != 0) {
  662. if (pm121_readjust)
  663. goto readjust;
  664. return;
  665. }
  666. st->ticks = PM121_CPU_INTERVAL;
  667. fan_cpu = controls[FAN_CPU];
  668. rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);
  669. if (rc) {
  670. printk(KERN_WARNING "pm121: CPU temp sensor error %d\n",
  671. rc);
  672. pm121_failure_state |= FAILURE_SENSOR;
  673. return;
  674. }
  675. rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);
  676. if (rc) {
  677. printk(KERN_WARNING "pm121: CPU power sensor error %d\n",
  678. rc);
  679. pm121_failure_state |= FAILURE_SENSOR;
  680. return;
  681. }
  682. pr_debug("pm121: CPU Fans tick ! CPU temp: %d.%03d°C, power: %d.%03d\n",
  683. FIX32TOPRINT(temp), FIX32TOPRINT(power));
  684. if (temp > st->pid.param.tmax)
  685. pm121_failure_state |= FAILURE_OVERTEMP;
  686. new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);
  687. /* correction */
  688. new_setpoint = pm121_correct(new_setpoint,
  689. FAN_CPU,
  690. st->pid.param.min);
  691. /* connected correction */
  692. new_setpoint = pm121_connect(FAN_CPU, new_setpoint);
  693. if (st->setpoint == new_setpoint)
  694. return;
  695. st->setpoint = new_setpoint;
  696. pr_debug("pm121: CPU corrected setpoint: %d RPM\n", (int)new_setpoint);
  697. readjust:
  698. if (fan_cpu && pm121_failure_state == 0) {
  699. rc = fan_cpu->ops->set_value(fan_cpu, st->setpoint);
  700. if (rc) {
  701. printk(KERN_WARNING "pm121: %s fan error %d\n",
  702. fan_cpu->name, rc);
  703. pm121_failure_state |= FAILURE_FAN;
  704. }
  705. }
  706. }
  707. /*
  708. * ****** Common ******
  709. *
  710. */
  711. static void pm121_tick(void)
  712. {
  713. unsigned int last_failure = pm121_failure_state;
  714. unsigned int new_failure;
  715. s32 total_power;
  716. int i;
  717. if (!pm121_started) {
  718. pr_debug("pm121: creating control loops !\n");
  719. for (i = 0; i < N_LOOPS; i++)
  720. pm121_create_sys_fans(i);
  721. pm121_create_cpu_fans();
  722. pm121_started = 1;
  723. }
  724. /* skipping ticks */
  725. if (pm121_skipping && --pm121_skipping)
  726. return;
  727. /* compute average power */
  728. total_power = 0;
  729. for (i = 0; i < pm121_cpu_state->pid.param.history_len; i++)
  730. total_power += pm121_cpu_state->pid.powers[i];
  731. average_power = total_power / pm121_cpu_state->pid.param.history_len;
  732. pm121_failure_state = 0;
  733. for (i = 0 ; i < N_LOOPS; i++) {
  734. if (pm121_sys_state[i])
  735. pm121_sys_fans_tick(i);
  736. }
  737. if (pm121_cpu_state)
  738. pm121_cpu_fans_tick(pm121_cpu_state);
  739. pm121_readjust = 0;
  740. new_failure = pm121_failure_state & ~last_failure;
  741. /* If entering failure mode, clamp cpufreq and ramp all
  742. * fans to full speed.
  743. */
  744. if (pm121_failure_state && !last_failure) {
  745. for (i = 0; i < N_CONTROLS; i++) {
  746. if (controls[i])
  747. wf_control_set_max(controls[i]);
  748. }
  749. }
  750. /* If leaving failure mode, unclamp cpufreq and readjust
  751. * all fans on next iteration
  752. */
  753. if (!pm121_failure_state && last_failure) {
  754. if (controls[CPUFREQ])
  755. wf_control_set_min(controls[CPUFREQ]);
  756. pm121_readjust = 1;
  757. }
  758. /* Overtemp condition detected, notify and start skipping a couple
  759. * ticks to let the temperature go down
  760. */
  761. if (new_failure & FAILURE_OVERTEMP) {
  762. wf_set_overtemp();
  763. pm121_skipping = 2;
  764. pm121_overtemp = true;
  765. }
  766. /* We only clear the overtemp condition if overtemp is cleared
  767. * _and_ no other failure is present. Since a sensor error will
  768. * clear the overtemp condition (can't measure temperature) at
  769. * the control loop levels, but we don't want to keep it clear
  770. * here in this case
  771. */
  772. if (!pm121_failure_state && pm121_overtemp) {
  773. wf_clear_overtemp();
  774. pm121_overtemp = false;
  775. }
  776. }
  777. static struct wf_control* pm121_register_control(struct wf_control *ct,
  778. const char *match,
  779. unsigned int id)
  780. {
  781. if (controls[id] == NULL && !strcmp(ct->name, match)) {
  782. if (wf_get_control(ct) == 0)
  783. controls[id] = ct;
  784. }
  785. return controls[id];
  786. }
  787. static void pm121_new_control(struct wf_control *ct)
  788. {
  789. int all = 1;
  790. if (pm121_all_controls_ok)
  791. return;
  792. all = pm121_register_control(ct, "optical-drive-fan", FAN_OD) && all;
  793. all = pm121_register_control(ct, "hard-drive-fan", FAN_HD) && all;
  794. all = pm121_register_control(ct, "cpu-fan", FAN_CPU) && all;
  795. all = pm121_register_control(ct, "cpufreq-clamp", CPUFREQ) && all;
  796. if (all)
  797. pm121_all_controls_ok = 1;
  798. }
  799. static struct wf_sensor* pm121_register_sensor(struct wf_sensor *sensor,
  800. const char *match,
  801. struct wf_sensor **var)
  802. {
  803. if (*var == NULL && !strcmp(sensor->name, match)) {
  804. if (wf_get_sensor(sensor) == 0)
  805. *var = sensor;
  806. }
  807. return *var;
  808. }
  809. static void pm121_new_sensor(struct wf_sensor *sr)
  810. {
  811. int all = 1;
  812. if (pm121_all_sensors_ok)
  813. return;
  814. all = pm121_register_sensor(sr, "cpu-temp",
  815. &sensor_cpu_temp) && all;
  816. all = pm121_register_sensor(sr, "cpu-current",
  817. &sensor_cpu_current) && all;
  818. all = pm121_register_sensor(sr, "cpu-voltage",
  819. &sensor_cpu_voltage) && all;
  820. all = pm121_register_sensor(sr, "cpu-power",
  821. &sensor_cpu_power) && all;
  822. all = pm121_register_sensor(sr, "hard-drive-temp",
  823. &sensor_hard_drive_temp) && all;
  824. all = pm121_register_sensor(sr, "optical-drive-temp",
  825. &sensor_optical_drive_temp) && all;
  826. all = pm121_register_sensor(sr, "incoming-air-temp",
  827. &sensor_incoming_air_temp) && all;
  828. all = pm121_register_sensor(sr, "north-bridge-temp",
  829. &sensor_north_bridge_temp) && all;
  830. all = pm121_register_sensor(sr, "gpu-temp",
  831. &sensor_gpu_temp) && all;
  832. if (all)
  833. pm121_all_sensors_ok = 1;
  834. }
  835. static int pm121_notify(struct notifier_block *self,
  836. unsigned long event, void *data)
  837. {
  838. switch (event) {
  839. case WF_EVENT_NEW_CONTROL:
  840. pr_debug("pm121: new control %s detected\n",
  841. ((struct wf_control *)data)->name);
  842. pm121_new_control(data);
  843. break;
  844. case WF_EVENT_NEW_SENSOR:
  845. pr_debug("pm121: new sensor %s detected\n",
  846. ((struct wf_sensor *)data)->name);
  847. pm121_new_sensor(data);
  848. break;
  849. case WF_EVENT_TICK:
  850. if (pm121_all_controls_ok && pm121_all_sensors_ok)
  851. pm121_tick();
  852. break;
  853. }
  854. return 0;
  855. }
  856. static struct notifier_block pm121_events = {
  857. .notifier_call = pm121_notify,
  858. };
  859. static int pm121_init_pm(void)
  860. {
  861. const struct smu_sdbp_header *hdr;
  862. hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);
  863. if (hdr != 0) {
  864. struct smu_sdbp_sensortree *st =
  865. (struct smu_sdbp_sensortree *)&hdr[1];
  866. pm121_mach_model = st->model_id;
  867. }
  868. pm121_connection = &pm121_connections[pm121_mach_model - 2];
  869. printk(KERN_INFO "pm121: Initializing for iMac G5 iSight model ID %d\n",
  870. pm121_mach_model);
  871. return 0;
  872. }
  873. static int pm121_probe(struct platform_device *ddev)
  874. {
  875. wf_register_client(&pm121_events);
  876. return 0;
  877. }
  878. static int pm121_remove(struct platform_device *ddev)
  879. {
  880. wf_unregister_client(&pm121_events);
  881. return 0;
  882. }
  883. static struct platform_driver pm121_driver = {
  884. .probe = pm121_probe,
  885. .remove = pm121_remove,
  886. .driver = {
  887. .name = "windfarm",
  888. .bus = &platform_bus_type,
  889. },
  890. };
  891. static int __init pm121_init(void)
  892. {
  893. int rc = -ENODEV;
  894. if (of_machine_is_compatible("PowerMac12,1"))
  895. rc = pm121_init_pm();
  896. if (rc == 0) {
  897. request_module("windfarm_smu_controls");
  898. request_module("windfarm_smu_sensors");
  899. request_module("windfarm_smu_sat");
  900. request_module("windfarm_lm75_sensor");
  901. request_module("windfarm_max6690_sensor");
  902. request_module("windfarm_cpufreq_clamp");
  903. platform_driver_register(&pm121_driver);
  904. }
  905. return rc;
  906. }
  907. static void __exit pm121_exit(void)
  908. {
  909. platform_driver_unregister(&pm121_driver);
  910. }
  911. module_init(pm121_init);
  912. module_exit(pm121_exit);
  913. MODULE_AUTHOR("Étienne Bersac <bersace@gmail.com>");
  914. MODULE_DESCRIPTION("Thermal control logic for iMac G5 (iSight)");
  915. MODULE_LICENSE("GPL");