windfarm_pm112.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control.
  4. * Control loops for machines with SMU and PPC970MP processors.
  5. *
  6. * Copyright (C) 2005 Paul Mackerras, IBM Corp. <[email protected]>
  7. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reboot.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. #include <asm/smu.h>
  18. #include "windfarm.h"
  19. #include "windfarm_pid.h"
  20. #define VERSION "0.2"
  21. #define DEBUG
  22. #undef LOTSA_DEBUG
  23. #ifdef DEBUG
  24. #define DBG(args...) printk(args)
  25. #else
  26. #define DBG(args...) do { } while(0)
  27. #endif
  28. #ifdef LOTSA_DEBUG
  29. #define DBG_LOTS(args...) printk(args)
  30. #else
  31. #define DBG_LOTS(args...) do { } while(0)
  32. #endif
  33. /* define this to force CPU overtemp to 60 degree, useful for testing
  34. * the overtemp code
  35. */
  36. #undef HACKED_OVERTEMP
  37. /* We currently only handle 2 chips, 4 cores... */
  38. #define NR_CHIPS 2
  39. #define NR_CORES 4
  40. #define NR_CPU_FANS 3 * NR_CHIPS
  41. /* Controls and sensors */
  42. static struct wf_sensor *sens_cpu_temp[NR_CORES];
  43. static struct wf_sensor *sens_cpu_power[NR_CORES];
  44. static struct wf_sensor *hd_temp;
  45. static struct wf_sensor *slots_power;
  46. static struct wf_sensor *u4_temp;
  47. static struct wf_control *cpu_fans[NR_CPU_FANS];
  48. static char *cpu_fan_names[NR_CPU_FANS] = {
  49. "cpu-rear-fan-0",
  50. "cpu-rear-fan-1",
  51. "cpu-front-fan-0",
  52. "cpu-front-fan-1",
  53. "cpu-pump-0",
  54. "cpu-pump-1",
  55. };
  56. static struct wf_control *cpufreq_clamp;
  57. /* Second pump isn't required (and isn't actually present) */
  58. #define CPU_FANS_REQD (NR_CPU_FANS - 2)
  59. #define FIRST_PUMP 4
  60. #define LAST_PUMP 5
  61. /* We keep a temperature history for average calculation of 180s */
  62. #define CPU_TEMP_HIST_SIZE 180
  63. /* Scale factor for fan speed, *100 */
  64. static int cpu_fan_scale[NR_CPU_FANS] = {
  65. 100,
  66. 100,
  67. 97, /* inlet fans run at 97% of exhaust fan */
  68. 97,
  69. 100, /* updated later */
  70. 100, /* updated later */
  71. };
  72. static struct wf_control *backside_fan;
  73. static struct wf_control *slots_fan;
  74. static struct wf_control *drive_bay_fan;
  75. /* PID loop state */
  76. static struct wf_cpu_pid_state cpu_pid[NR_CORES];
  77. static u32 cpu_thist[CPU_TEMP_HIST_SIZE];
  78. static int cpu_thist_pt;
  79. static s64 cpu_thist_total;
  80. static s32 cpu_all_tmax = 100 << 16;
  81. static int cpu_last_target;
  82. static struct wf_pid_state backside_pid;
  83. static int backside_tick;
  84. static struct wf_pid_state slots_pid;
  85. static bool slots_started;
  86. static struct wf_pid_state drive_bay_pid;
  87. static int drive_bay_tick;
  88. static int nr_cores;
  89. static int have_all_controls;
  90. static int have_all_sensors;
  91. static bool started;
  92. static int failure_state;
  93. #define FAILURE_SENSOR 1
  94. #define FAILURE_FAN 2
  95. #define FAILURE_PERM 4
  96. #define FAILURE_LOW_OVERTEMP 8
  97. #define FAILURE_HIGH_OVERTEMP 16
  98. /* Overtemp values */
  99. #define LOW_OVER_AVERAGE 0
  100. #define LOW_OVER_IMMEDIATE (10 << 16)
  101. #define LOW_OVER_CLEAR ((-10) << 16)
  102. #define HIGH_OVER_IMMEDIATE (14 << 16)
  103. #define HIGH_OVER_AVERAGE (10 << 16)
  104. #define HIGH_OVER_IMMEDIATE (14 << 16)
  105. /* Implementation... */
  106. static int create_cpu_loop(int cpu)
  107. {
  108. int chip = cpu / 2;
  109. int core = cpu & 1;
  110. struct smu_sdbp_header *hdr;
  111. struct smu_sdbp_cpupiddata *piddata;
  112. struct wf_cpu_pid_param pid;
  113. struct wf_control *main_fan = cpu_fans[0];
  114. s32 tmax;
  115. int fmin;
  116. /* Get FVT params to get Tmax; if not found, assume default */
  117. hdr = smu_sat_get_sdb_partition(chip, 0xC4 + core, NULL);
  118. if (hdr) {
  119. struct smu_sdbp_fvt *fvt = (struct smu_sdbp_fvt *)&hdr[1];
  120. tmax = fvt->maxtemp << 16;
  121. } else
  122. tmax = 95 << 16; /* default to 95 degrees C */
  123. /* We keep a global tmax for overtemp calculations */
  124. if (tmax < cpu_all_tmax)
  125. cpu_all_tmax = tmax;
  126. kfree(hdr);
  127. /* Get PID params from the appropriate SAT */
  128. hdr = smu_sat_get_sdb_partition(chip, 0xC8 + core, NULL);
  129. if (hdr == NULL) {
  130. printk(KERN_WARNING"windfarm: can't get CPU PID fan config\n");
  131. return -EINVAL;
  132. }
  133. piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];
  134. /*
  135. * Darwin has a minimum fan speed of 1000 rpm for the 4-way and
  136. * 515 for the 2-way. That appears to be overkill, so for now,
  137. * impose a minimum of 750 or 515.
  138. */
  139. fmin = (nr_cores > 2) ? 750 : 515;
  140. /* Initialize PID loop */
  141. pid.interval = 1; /* seconds */
  142. pid.history_len = piddata->history_len;
  143. pid.gd = piddata->gd;
  144. pid.gp = piddata->gp;
  145. pid.gr = piddata->gr / piddata->history_len;
  146. pid.pmaxadj = (piddata->max_power << 16) - (piddata->power_adj << 8);
  147. pid.ttarget = tmax - (piddata->target_temp_delta << 16);
  148. pid.tmax = tmax;
  149. pid.min = main_fan->ops->get_min(main_fan);
  150. pid.max = main_fan->ops->get_max(main_fan);
  151. if (pid.min < fmin)
  152. pid.min = fmin;
  153. wf_cpu_pid_init(&cpu_pid[cpu], &pid);
  154. kfree(hdr);
  155. return 0;
  156. }
  157. static void cpu_max_all_fans(void)
  158. {
  159. int i;
  160. /* We max all CPU fans in case of a sensor error. We also do the
  161. * cpufreq clamping now, even if it's supposedly done later by the
  162. * generic code anyway, we do it earlier here to react faster
  163. */
  164. if (cpufreq_clamp)
  165. wf_control_set_max(cpufreq_clamp);
  166. for (i = 0; i < NR_CPU_FANS; ++i)
  167. if (cpu_fans[i])
  168. wf_control_set_max(cpu_fans[i]);
  169. }
  170. static int cpu_check_overtemp(s32 temp)
  171. {
  172. int new_state = 0;
  173. s32 t_avg, t_old;
  174. /* First check for immediate overtemps */
  175. if (temp >= (cpu_all_tmax + LOW_OVER_IMMEDIATE)) {
  176. new_state |= FAILURE_LOW_OVERTEMP;
  177. if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
  178. printk(KERN_ERR "windfarm: Overtemp due to immediate CPU"
  179. " temperature !\n");
  180. }
  181. if (temp >= (cpu_all_tmax + HIGH_OVER_IMMEDIATE)) {
  182. new_state |= FAILURE_HIGH_OVERTEMP;
  183. if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
  184. printk(KERN_ERR "windfarm: Critical overtemp due to"
  185. " immediate CPU temperature !\n");
  186. }
  187. /* We calculate a history of max temperatures and use that for the
  188. * overtemp management
  189. */
  190. t_old = cpu_thist[cpu_thist_pt];
  191. cpu_thist[cpu_thist_pt] = temp;
  192. cpu_thist_pt = (cpu_thist_pt + 1) % CPU_TEMP_HIST_SIZE;
  193. cpu_thist_total -= t_old;
  194. cpu_thist_total += temp;
  195. t_avg = cpu_thist_total / CPU_TEMP_HIST_SIZE;
  196. DBG_LOTS("t_avg = %d.%03d (out: %d.%03d, in: %d.%03d)\n",
  197. FIX32TOPRINT(t_avg), FIX32TOPRINT(t_old), FIX32TOPRINT(temp));
  198. /* Now check for average overtemps */
  199. if (t_avg >= (cpu_all_tmax + LOW_OVER_AVERAGE)) {
  200. new_state |= FAILURE_LOW_OVERTEMP;
  201. if ((failure_state & FAILURE_LOW_OVERTEMP) == 0)
  202. printk(KERN_ERR "windfarm: Overtemp due to average CPU"
  203. " temperature !\n");
  204. }
  205. if (t_avg >= (cpu_all_tmax + HIGH_OVER_AVERAGE)) {
  206. new_state |= FAILURE_HIGH_OVERTEMP;
  207. if ((failure_state & FAILURE_HIGH_OVERTEMP) == 0)
  208. printk(KERN_ERR "windfarm: Critical overtemp due to"
  209. " average CPU temperature !\n");
  210. }
  211. /* Now handle overtemp conditions. We don't currently use the windfarm
  212. * overtemp handling core as it's not fully suited to the needs of those
  213. * new machine. This will be fixed later.
  214. */
  215. if (new_state) {
  216. /* High overtemp -> immediate shutdown */
  217. if (new_state & FAILURE_HIGH_OVERTEMP)
  218. machine_power_off();
  219. if ((failure_state & new_state) != new_state)
  220. cpu_max_all_fans();
  221. failure_state |= new_state;
  222. } else if ((failure_state & FAILURE_LOW_OVERTEMP) &&
  223. (temp < (cpu_all_tmax + LOW_OVER_CLEAR))) {
  224. printk(KERN_ERR "windfarm: Overtemp condition cleared !\n");
  225. failure_state &= ~FAILURE_LOW_OVERTEMP;
  226. }
  227. return failure_state & (FAILURE_LOW_OVERTEMP | FAILURE_HIGH_OVERTEMP);
  228. }
  229. static void cpu_fans_tick(void)
  230. {
  231. int err, cpu;
  232. s32 greatest_delta = 0;
  233. s32 temp, power, t_max = 0;
  234. int i, t, target = 0;
  235. struct wf_sensor *sr;
  236. struct wf_control *ct;
  237. struct wf_cpu_pid_state *sp;
  238. DBG_LOTS(KERN_DEBUG);
  239. for (cpu = 0; cpu < nr_cores; ++cpu) {
  240. /* Get CPU core temperature */
  241. sr = sens_cpu_temp[cpu];
  242. err = sr->ops->get_value(sr, &temp);
  243. if (err) {
  244. DBG("\n");
  245. printk(KERN_WARNING "windfarm: CPU %d temperature "
  246. "sensor error %d\n", cpu, err);
  247. failure_state |= FAILURE_SENSOR;
  248. cpu_max_all_fans();
  249. return;
  250. }
  251. /* Keep track of highest temp */
  252. t_max = max(t_max, temp);
  253. /* Get CPU power */
  254. sr = sens_cpu_power[cpu];
  255. err = sr->ops->get_value(sr, &power);
  256. if (err) {
  257. DBG("\n");
  258. printk(KERN_WARNING "windfarm: CPU %d power "
  259. "sensor error %d\n", cpu, err);
  260. failure_state |= FAILURE_SENSOR;
  261. cpu_max_all_fans();
  262. return;
  263. }
  264. /* Run PID */
  265. sp = &cpu_pid[cpu];
  266. t = wf_cpu_pid_run(sp, power, temp);
  267. if (cpu == 0 || sp->last_delta > greatest_delta) {
  268. greatest_delta = sp->last_delta;
  269. target = t;
  270. }
  271. DBG_LOTS("[%d] P=%d.%.3d T=%d.%.3d ",
  272. cpu, FIX32TOPRINT(power), FIX32TOPRINT(temp));
  273. }
  274. DBG_LOTS("fans = %d, t_max = %d.%03d\n", target, FIX32TOPRINT(t_max));
  275. /* Darwin limits decrease to 20 per iteration */
  276. if (target < (cpu_last_target - 20))
  277. target = cpu_last_target - 20;
  278. cpu_last_target = target;
  279. for (cpu = 0; cpu < nr_cores; ++cpu)
  280. cpu_pid[cpu].target = target;
  281. /* Handle possible overtemps */
  282. if (cpu_check_overtemp(t_max))
  283. return;
  284. /* Set fans */
  285. for (i = 0; i < NR_CPU_FANS; ++i) {
  286. ct = cpu_fans[i];
  287. if (ct == NULL)
  288. continue;
  289. err = ct->ops->set_value(ct, target * cpu_fan_scale[i] / 100);
  290. if (err) {
  291. printk(KERN_WARNING "windfarm: fan %s reports "
  292. "error %d\n", ct->name, err);
  293. failure_state |= FAILURE_FAN;
  294. break;
  295. }
  296. }
  297. }
  298. /* Backside/U4 fan */
  299. static struct wf_pid_param backside_param = {
  300. .interval = 5,
  301. .history_len = 2,
  302. .gd = 48 << 20,
  303. .gp = 5 << 20,
  304. .gr = 0,
  305. .itarget = 64 << 16,
  306. .additive = 1,
  307. };
  308. static void backside_fan_tick(void)
  309. {
  310. s32 temp;
  311. int speed;
  312. int err;
  313. if (!backside_fan || !u4_temp)
  314. return;
  315. if (!backside_tick) {
  316. /* first time; initialize things */
  317. printk(KERN_INFO "windfarm: Backside control loop started.\n");
  318. backside_param.min = backside_fan->ops->get_min(backside_fan);
  319. backside_param.max = backside_fan->ops->get_max(backside_fan);
  320. wf_pid_init(&backside_pid, &backside_param);
  321. backside_tick = 1;
  322. }
  323. if (--backside_tick > 0)
  324. return;
  325. backside_tick = backside_pid.param.interval;
  326. err = u4_temp->ops->get_value(u4_temp, &temp);
  327. if (err) {
  328. printk(KERN_WARNING "windfarm: U4 temp sensor error %d\n",
  329. err);
  330. failure_state |= FAILURE_SENSOR;
  331. wf_control_set_max(backside_fan);
  332. return;
  333. }
  334. speed = wf_pid_run(&backside_pid, temp);
  335. DBG_LOTS("backside PID temp=%d.%.3d speed=%d\n",
  336. FIX32TOPRINT(temp), speed);
  337. err = backside_fan->ops->set_value(backside_fan, speed);
  338. if (err) {
  339. printk(KERN_WARNING "windfarm: backside fan error %d\n", err);
  340. failure_state |= FAILURE_FAN;
  341. }
  342. }
  343. /* Drive bay fan */
  344. static struct wf_pid_param drive_bay_prm = {
  345. .interval = 5,
  346. .history_len = 2,
  347. .gd = 30 << 20,
  348. .gp = 5 << 20,
  349. .gr = 0,
  350. .itarget = 40 << 16,
  351. .additive = 1,
  352. };
  353. static void drive_bay_fan_tick(void)
  354. {
  355. s32 temp;
  356. int speed;
  357. int err;
  358. if (!drive_bay_fan || !hd_temp)
  359. return;
  360. if (!drive_bay_tick) {
  361. /* first time; initialize things */
  362. printk(KERN_INFO "windfarm: Drive bay control loop started.\n");
  363. drive_bay_prm.min = drive_bay_fan->ops->get_min(drive_bay_fan);
  364. drive_bay_prm.max = drive_bay_fan->ops->get_max(drive_bay_fan);
  365. wf_pid_init(&drive_bay_pid, &drive_bay_prm);
  366. drive_bay_tick = 1;
  367. }
  368. if (--drive_bay_tick > 0)
  369. return;
  370. drive_bay_tick = drive_bay_pid.param.interval;
  371. err = hd_temp->ops->get_value(hd_temp, &temp);
  372. if (err) {
  373. printk(KERN_WARNING "windfarm: drive bay temp sensor "
  374. "error %d\n", err);
  375. failure_state |= FAILURE_SENSOR;
  376. wf_control_set_max(drive_bay_fan);
  377. return;
  378. }
  379. speed = wf_pid_run(&drive_bay_pid, temp);
  380. DBG_LOTS("drive_bay PID temp=%d.%.3d speed=%d\n",
  381. FIX32TOPRINT(temp), speed);
  382. err = drive_bay_fan->ops->set_value(drive_bay_fan, speed);
  383. if (err) {
  384. printk(KERN_WARNING "windfarm: drive bay fan error %d\n", err);
  385. failure_state |= FAILURE_FAN;
  386. }
  387. }
  388. /* PCI slots area fan */
  389. /* This makes the fan speed proportional to the power consumed */
  390. static struct wf_pid_param slots_param = {
  391. .interval = 1,
  392. .history_len = 2,
  393. .gd = 0,
  394. .gp = 0,
  395. .gr = 0x1277952,
  396. .itarget = 0,
  397. .min = 1560,
  398. .max = 3510,
  399. };
  400. static void slots_fan_tick(void)
  401. {
  402. s32 power;
  403. int speed;
  404. int err;
  405. if (!slots_fan || !slots_power)
  406. return;
  407. if (!slots_started) {
  408. /* first time; initialize things */
  409. printk(KERN_INFO "windfarm: Slots control loop started.\n");
  410. wf_pid_init(&slots_pid, &slots_param);
  411. slots_started = true;
  412. }
  413. err = slots_power->ops->get_value(slots_power, &power);
  414. if (err) {
  415. printk(KERN_WARNING "windfarm: slots power sensor error %d\n",
  416. err);
  417. failure_state |= FAILURE_SENSOR;
  418. wf_control_set_max(slots_fan);
  419. return;
  420. }
  421. speed = wf_pid_run(&slots_pid, power);
  422. DBG_LOTS("slots PID power=%d.%.3d speed=%d\n",
  423. FIX32TOPRINT(power), speed);
  424. err = slots_fan->ops->set_value(slots_fan, speed);
  425. if (err) {
  426. printk(KERN_WARNING "windfarm: slots fan error %d\n", err);
  427. failure_state |= FAILURE_FAN;
  428. }
  429. }
  430. static void set_fail_state(void)
  431. {
  432. int i;
  433. if (cpufreq_clamp)
  434. wf_control_set_max(cpufreq_clamp);
  435. for (i = 0; i < NR_CPU_FANS; ++i)
  436. if (cpu_fans[i])
  437. wf_control_set_max(cpu_fans[i]);
  438. if (backside_fan)
  439. wf_control_set_max(backside_fan);
  440. if (slots_fan)
  441. wf_control_set_max(slots_fan);
  442. if (drive_bay_fan)
  443. wf_control_set_max(drive_bay_fan);
  444. }
  445. static void pm112_tick(void)
  446. {
  447. int i, last_failure;
  448. if (!started) {
  449. started = true;
  450. printk(KERN_INFO "windfarm: CPUs control loops started.\n");
  451. for (i = 0; i < nr_cores; ++i) {
  452. if (create_cpu_loop(i) < 0) {
  453. failure_state = FAILURE_PERM;
  454. set_fail_state();
  455. break;
  456. }
  457. }
  458. DBG_LOTS("cpu_all_tmax=%d.%03d\n", FIX32TOPRINT(cpu_all_tmax));
  459. #ifdef HACKED_OVERTEMP
  460. cpu_all_tmax = 60 << 16;
  461. #endif
  462. }
  463. /* Permanent failure, bail out */
  464. if (failure_state & FAILURE_PERM)
  465. return;
  466. /* Clear all failure bits except low overtemp which will be eventually
  467. * cleared by the control loop itself
  468. */
  469. last_failure = failure_state;
  470. failure_state &= FAILURE_LOW_OVERTEMP;
  471. cpu_fans_tick();
  472. backside_fan_tick();
  473. slots_fan_tick();
  474. drive_bay_fan_tick();
  475. DBG_LOTS("last_failure: 0x%x, failure_state: %x\n",
  476. last_failure, failure_state);
  477. /* Check for failures. Any failure causes cpufreq clamping */
  478. if (failure_state && last_failure == 0 && cpufreq_clamp)
  479. wf_control_set_max(cpufreq_clamp);
  480. if (failure_state == 0 && last_failure && cpufreq_clamp)
  481. wf_control_set_min(cpufreq_clamp);
  482. /* That's it for now, we might want to deal with other failures
  483. * differently in the future though
  484. */
  485. }
  486. static void pm112_new_control(struct wf_control *ct)
  487. {
  488. int i, max_exhaust;
  489. if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {
  490. if (wf_get_control(ct) == 0)
  491. cpufreq_clamp = ct;
  492. }
  493. for (i = 0; i < NR_CPU_FANS; ++i) {
  494. if (!strcmp(ct->name, cpu_fan_names[i])) {
  495. if (cpu_fans[i] == NULL && wf_get_control(ct) == 0)
  496. cpu_fans[i] = ct;
  497. break;
  498. }
  499. }
  500. if (i >= NR_CPU_FANS) {
  501. /* not a CPU fan, try the others */
  502. if (!strcmp(ct->name, "backside-fan")) {
  503. if (backside_fan == NULL && wf_get_control(ct) == 0)
  504. backside_fan = ct;
  505. } else if (!strcmp(ct->name, "slots-fan")) {
  506. if (slots_fan == NULL && wf_get_control(ct) == 0)
  507. slots_fan = ct;
  508. } else if (!strcmp(ct->name, "drive-bay-fan")) {
  509. if (drive_bay_fan == NULL && wf_get_control(ct) == 0)
  510. drive_bay_fan = ct;
  511. }
  512. return;
  513. }
  514. for (i = 0; i < CPU_FANS_REQD; ++i)
  515. if (cpu_fans[i] == NULL)
  516. return;
  517. /* work out pump scaling factors */
  518. max_exhaust = cpu_fans[0]->ops->get_max(cpu_fans[0]);
  519. for (i = FIRST_PUMP; i <= LAST_PUMP; ++i)
  520. if ((ct = cpu_fans[i]) != NULL)
  521. cpu_fan_scale[i] =
  522. ct->ops->get_max(ct) * 100 / max_exhaust;
  523. have_all_controls = 1;
  524. }
  525. static void pm112_new_sensor(struct wf_sensor *sr)
  526. {
  527. unsigned int i;
  528. if (!strncmp(sr->name, "cpu-temp-", 9)) {
  529. i = sr->name[9] - '0';
  530. if (sr->name[10] == 0 && i < NR_CORES &&
  531. sens_cpu_temp[i] == NULL && wf_get_sensor(sr) == 0)
  532. sens_cpu_temp[i] = sr;
  533. } else if (!strncmp(sr->name, "cpu-power-", 10)) {
  534. i = sr->name[10] - '0';
  535. if (sr->name[11] == 0 && i < NR_CORES &&
  536. sens_cpu_power[i] == NULL && wf_get_sensor(sr) == 0)
  537. sens_cpu_power[i] = sr;
  538. } else if (!strcmp(sr->name, "hd-temp")) {
  539. if (hd_temp == NULL && wf_get_sensor(sr) == 0)
  540. hd_temp = sr;
  541. } else if (!strcmp(sr->name, "slots-power")) {
  542. if (slots_power == NULL && wf_get_sensor(sr) == 0)
  543. slots_power = sr;
  544. } else if (!strcmp(sr->name, "backside-temp")) {
  545. if (u4_temp == NULL && wf_get_sensor(sr) == 0)
  546. u4_temp = sr;
  547. } else
  548. return;
  549. /* check if we have all the sensors we need */
  550. for (i = 0; i < nr_cores; ++i)
  551. if (sens_cpu_temp[i] == NULL || sens_cpu_power[i] == NULL)
  552. return;
  553. have_all_sensors = 1;
  554. }
  555. static int pm112_wf_notify(struct notifier_block *self,
  556. unsigned long event, void *data)
  557. {
  558. switch (event) {
  559. case WF_EVENT_NEW_SENSOR:
  560. pm112_new_sensor(data);
  561. break;
  562. case WF_EVENT_NEW_CONTROL:
  563. pm112_new_control(data);
  564. break;
  565. case WF_EVENT_TICK:
  566. if (have_all_controls && have_all_sensors)
  567. pm112_tick();
  568. }
  569. return 0;
  570. }
  571. static struct notifier_block pm112_events = {
  572. .notifier_call = pm112_wf_notify,
  573. };
  574. static int wf_pm112_probe(struct platform_device *dev)
  575. {
  576. wf_register_client(&pm112_events);
  577. return 0;
  578. }
  579. static int wf_pm112_remove(struct platform_device *dev)
  580. {
  581. wf_unregister_client(&pm112_events);
  582. /* should release all sensors and controls */
  583. return 0;
  584. }
  585. static struct platform_driver wf_pm112_driver = {
  586. .probe = wf_pm112_probe,
  587. .remove = wf_pm112_remove,
  588. .driver = {
  589. .name = "windfarm",
  590. },
  591. };
  592. static int __init wf_pm112_init(void)
  593. {
  594. struct device_node *cpu;
  595. if (!of_machine_is_compatible("PowerMac11,2"))
  596. return -ENODEV;
  597. /* Count the number of CPU cores */
  598. nr_cores = 0;
  599. for_each_node_by_type(cpu, "cpu")
  600. ++nr_cores;
  601. printk(KERN_INFO "windfarm: initializing for dual-core desktop G5\n");
  602. #ifdef MODULE
  603. request_module("windfarm_smu_controls");
  604. request_module("windfarm_smu_sensors");
  605. request_module("windfarm_smu_sat");
  606. request_module("windfarm_lm75_sensor");
  607. request_module("windfarm_max6690_sensor");
  608. request_module("windfarm_cpufreq_clamp");
  609. #endif /* MODULE */
  610. platform_driver_register(&wf_pm112_driver);
  611. return 0;
  612. }
  613. static void __exit wf_pm112_exit(void)
  614. {
  615. platform_driver_unregister(&wf_pm112_driver);
  616. }
  617. module_init(wf_pm112_init);
  618. module_exit(wf_pm112_exit);
  619. MODULE_AUTHOR("Paul Mackerras <[email protected]>");
  620. MODULE_DESCRIPTION("Thermal control for PowerMac11,2");
  621. MODULE_LICENSE("GPL");
  622. MODULE_ALIAS("platform:windfarm");