windfarm_pm121.c 25 KB

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