windfarm_pm81.c 23 KB

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