PageRenderTime 70ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/macintosh/therm_pm72.c

https://bitbucket.org/ndreys/linux-sunxi
C | 2280 lines | 1555 code | 314 blank | 411 comment | 352 complexity | bb8ffbed94b4960c6ff282ead354f6e1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Device driver for the thermostats & fan controller of the
  3. * Apple G5 "PowerMac7,2" desktop machines.
  4. *
  5. * (c) Copyright IBM Corp. 2003-2004
  6. *
  7. * Maintained by: Benjamin Herrenschmidt
  8. * <benh@kernel.crashing.org>
  9. *
  10. *
  11. * The algorithm used is the PID control algorithm, used the same
  12. * way the published Darwin code does, using the same values that
  13. * are present in the Darwin 7.0 snapshot property lists.
  14. *
  15. * As far as the CPUs control loops are concerned, I use the
  16. * calibration & PID constants provided by the EEPROM,
  17. * I do _not_ embed any value from the property lists, as the ones
  18. * provided by Darwin 7.0 seem to always have an older version that
  19. * what I've seen on the actual computers.
  20. * It would be interesting to verify that though. Darwin has a
  21. * version code of 1.0.0d11 for all control loops it seems, while
  22. * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
  23. *
  24. * Darwin doesn't provide source to all parts, some missing
  25. * bits like the AppleFCU driver or the actual scale of some
  26. * of the values returned by sensors had to be "guessed" some
  27. * way... or based on what Open Firmware does.
  28. *
  29. * I didn't yet figure out how to get the slots power consumption
  30. * out of the FCU, so that part has not been implemented yet and
  31. * the slots fan is set to a fixed 50% PWM, hoping this value is
  32. * safe enough ...
  33. *
  34. * Note: I have observed strange oscillations of the CPU control
  35. * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
  36. * oscillates slowly (over several minutes) between the minimum
  37. * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
  38. * this, it could be some incorrect constant or an error in the
  39. * way I ported the algorithm, or it could be just normal. I
  40. * don't have full understanding on the way Apple tweaked the PID
  41. * algorithm for the CPU control, it is definitely not a standard
  42. * implementation...
  43. *
  44. * TODO: - Check MPU structure version/signature
  45. * - Add things like /sbin/overtemp for non-critical
  46. * overtemp conditions so userland can take some policy
  47. * decisions, like slowing down CPUs
  48. * - Deal with fan and i2c failures in a better way
  49. * - Maybe do a generic PID based on params used for
  50. * U3 and Drives ? Definitely need to factor code a bit
  51. * better... also make sensor detection more robust using
  52. * the device-tree to probe for them
  53. * - Figure out how to get the slots consumption and set the
  54. * slots fan accordingly
  55. *
  56. * History:
  57. *
  58. * Nov. 13, 2003 : 0.5
  59. * - First release
  60. *
  61. * Nov. 14, 2003 : 0.6
  62. * - Read fan speed from FCU, low level fan routines now deal
  63. * with errors & check fan status, though higher level don't
  64. * do much.
  65. * - Move a bunch of definitions to .h file
  66. *
  67. * Nov. 18, 2003 : 0.7
  68. * - Fix build on ppc64 kernel
  69. * - Move back statics definitions to .c file
  70. * - Avoid calling schedule_timeout with a negative number
  71. *
  72. * Dec. 18, 2003 : 0.8
  73. * - Fix typo when reading back fan speed on 2 CPU machines
  74. *
  75. * Mar. 11, 2004 : 0.9
  76. * - Rework code accessing the ADC chips, make it more robust and
  77. * closer to the chip spec. Also make sure it is configured properly,
  78. * I've seen yet unexplained cases where on startup, I would have stale
  79. * values in the configuration register
  80. * - Switch back to use of target fan speed for PID, thus lowering
  81. * pressure on i2c
  82. *
  83. * Oct. 20, 2004 : 1.1
  84. * - Add device-tree lookup for fan IDs, should detect liquid cooling
  85. * pumps when present
  86. * - Enable driver for PowerMac7,3 machines
  87. * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
  88. * - Add new CPU cooling algorithm for machines with liquid cooling
  89. * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
  90. * - Fix a signed/unsigned compare issue in some PID loops
  91. *
  92. * Mar. 10, 2005 : 1.2
  93. * - Add basic support for Xserve G5
  94. * - Retrieve pumps min/max from EEPROM image in device-tree (broken)
  95. * - Use min/max macros here or there
  96. * - Latest darwin updated U3H min fan speed to 20% PWM
  97. *
  98. * July. 06, 2006 : 1.3
  99. * - Fix setting of RPM fans on Xserve G5 (they were going too fast)
  100. * - Add missing slots fan control loop for Xserve G5
  101. * - Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
  102. * still can't properly implement the control loop for these, so let's
  103. * reduce the noise a little bit, it appears that 40% still gives us
  104. * a pretty good air flow
  105. * - Add code to "tickle" the FCU regulary so it doesn't think that
  106. * we are gone while in fact, the machine just didn't need any fan
  107. * speed change lately
  108. *
  109. */
  110. #include <linux/types.h>
  111. #include <linux/module.h>
  112. #include <linux/errno.h>
  113. #include <linux/kernel.h>
  114. #include <linux/delay.h>
  115. #include <linux/sched.h>
  116. #include <linux/init.h>
  117. #include <linux/spinlock.h>
  118. #include <linux/wait.h>
  119. #include <linux/reboot.h>
  120. #include <linux/kmod.h>
  121. #include <linux/i2c.h>
  122. #include <linux/kthread.h>
  123. #include <linux/mutex.h>
  124. #include <linux/of_device.h>
  125. #include <linux/of_platform.h>
  126. #include <asm/prom.h>
  127. #include <asm/machdep.h>
  128. #include <asm/io.h>
  129. #include <asm/system.h>
  130. #include <asm/sections.h>
  131. #include <asm/macio.h>
  132. #include "therm_pm72.h"
  133. #define VERSION "1.3"
  134. #undef DEBUG
  135. #ifdef DEBUG
  136. #define DBG(args...) printk(args)
  137. #else
  138. #define DBG(args...) do { } while(0)
  139. #endif
  140. /*
  141. * Driver statics
  142. */
  143. static struct platform_device * of_dev;
  144. static struct i2c_adapter * u3_0;
  145. static struct i2c_adapter * u3_1;
  146. static struct i2c_adapter * k2;
  147. static struct i2c_client * fcu;
  148. static struct cpu_pid_state processor_state[2];
  149. static struct basckside_pid_params backside_params;
  150. static struct backside_pid_state backside_state;
  151. static struct drives_pid_state drives_state;
  152. static struct dimm_pid_state dimms_state;
  153. static struct slots_pid_state slots_state;
  154. static int state;
  155. static int cpu_count;
  156. static int cpu_pid_type;
  157. static struct task_struct *ctrl_task;
  158. static struct completion ctrl_complete;
  159. static int critical_state;
  160. static int rackmac;
  161. static s32 dimm_output_clamp;
  162. static int fcu_rpm_shift;
  163. static int fcu_tickle_ticks;
  164. static DEFINE_MUTEX(driver_lock);
  165. /*
  166. * We have 3 types of CPU PID control. One is "split" old style control
  167. * for intake & exhaust fans, the other is "combined" control for both
  168. * CPUs that also deals with the pumps when present. To be "compatible"
  169. * with OS X at this point, we only use "COMBINED" on the machines that
  170. * are identified as having the pumps (though that identification is at
  171. * least dodgy). Ultimately, we could probably switch completely to this
  172. * algorithm provided we hack it to deal with the UP case
  173. */
  174. #define CPU_PID_TYPE_SPLIT 0
  175. #define CPU_PID_TYPE_COMBINED 1
  176. #define CPU_PID_TYPE_RACKMAC 2
  177. /*
  178. * This table describes all fans in the FCU. The "id" and "type" values
  179. * are defaults valid for all earlier machines. Newer machines will
  180. * eventually override the table content based on the device-tree
  181. */
  182. struct fcu_fan_table
  183. {
  184. char* loc; /* location code */
  185. int type; /* 0 = rpm, 1 = pwm, 2 = pump */
  186. int id; /* id or -1 */
  187. };
  188. #define FCU_FAN_RPM 0
  189. #define FCU_FAN_PWM 1
  190. #define FCU_FAN_ABSENT_ID -1
  191. #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
  192. struct fcu_fan_table fcu_fans[] = {
  193. [BACKSIDE_FAN_PWM_INDEX] = {
  194. .loc = "BACKSIDE,SYS CTRLR FAN",
  195. .type = FCU_FAN_PWM,
  196. .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
  197. },
  198. [DRIVES_FAN_RPM_INDEX] = {
  199. .loc = "DRIVE BAY",
  200. .type = FCU_FAN_RPM,
  201. .id = DRIVES_FAN_RPM_DEFAULT_ID,
  202. },
  203. [SLOTS_FAN_PWM_INDEX] = {
  204. .loc = "SLOT,PCI FAN",
  205. .type = FCU_FAN_PWM,
  206. .id = SLOTS_FAN_PWM_DEFAULT_ID,
  207. },
  208. [CPUA_INTAKE_FAN_RPM_INDEX] = {
  209. .loc = "CPU A INTAKE",
  210. .type = FCU_FAN_RPM,
  211. .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
  212. },
  213. [CPUA_EXHAUST_FAN_RPM_INDEX] = {
  214. .loc = "CPU A EXHAUST",
  215. .type = FCU_FAN_RPM,
  216. .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
  217. },
  218. [CPUB_INTAKE_FAN_RPM_INDEX] = {
  219. .loc = "CPU B INTAKE",
  220. .type = FCU_FAN_RPM,
  221. .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
  222. },
  223. [CPUB_EXHAUST_FAN_RPM_INDEX] = {
  224. .loc = "CPU B EXHAUST",
  225. .type = FCU_FAN_RPM,
  226. .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
  227. },
  228. /* pumps aren't present by default, have to be looked up in the
  229. * device-tree
  230. */
  231. [CPUA_PUMP_RPM_INDEX] = {
  232. .loc = "CPU A PUMP",
  233. .type = FCU_FAN_RPM,
  234. .id = FCU_FAN_ABSENT_ID,
  235. },
  236. [CPUB_PUMP_RPM_INDEX] = {
  237. .loc = "CPU B PUMP",
  238. .type = FCU_FAN_RPM,
  239. .id = FCU_FAN_ABSENT_ID,
  240. },
  241. /* Xserve fans */
  242. [CPU_A1_FAN_RPM_INDEX] = {
  243. .loc = "CPU A 1",
  244. .type = FCU_FAN_RPM,
  245. .id = FCU_FAN_ABSENT_ID,
  246. },
  247. [CPU_A2_FAN_RPM_INDEX] = {
  248. .loc = "CPU A 2",
  249. .type = FCU_FAN_RPM,
  250. .id = FCU_FAN_ABSENT_ID,
  251. },
  252. [CPU_A3_FAN_RPM_INDEX] = {
  253. .loc = "CPU A 3",
  254. .type = FCU_FAN_RPM,
  255. .id = FCU_FAN_ABSENT_ID,
  256. },
  257. [CPU_B1_FAN_RPM_INDEX] = {
  258. .loc = "CPU B 1",
  259. .type = FCU_FAN_RPM,
  260. .id = FCU_FAN_ABSENT_ID,
  261. },
  262. [CPU_B2_FAN_RPM_INDEX] = {
  263. .loc = "CPU B 2",
  264. .type = FCU_FAN_RPM,
  265. .id = FCU_FAN_ABSENT_ID,
  266. },
  267. [CPU_B3_FAN_RPM_INDEX] = {
  268. .loc = "CPU B 3",
  269. .type = FCU_FAN_RPM,
  270. .id = FCU_FAN_ABSENT_ID,
  271. },
  272. };
  273. static struct i2c_driver therm_pm72_driver;
  274. /*
  275. * Utility function to create an i2c_client structure and
  276. * attach it to one of u3 adapters
  277. */
  278. static struct i2c_client *attach_i2c_chip(int id, const char *name)
  279. {
  280. struct i2c_client *clt;
  281. struct i2c_adapter *adap;
  282. struct i2c_board_info info;
  283. if (id & 0x200)
  284. adap = k2;
  285. else if (id & 0x100)
  286. adap = u3_1;
  287. else
  288. adap = u3_0;
  289. if (adap == NULL)
  290. return NULL;
  291. memset(&info, 0, sizeof(struct i2c_board_info));
  292. info.addr = (id >> 1) & 0x7f;
  293. strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
  294. clt = i2c_new_device(adap, &info);
  295. if (!clt) {
  296. printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
  297. return NULL;
  298. }
  299. /*
  300. * Let i2c-core delete that device on driver removal.
  301. * This is safe because i2c-core holds the core_lock mutex for us.
  302. */
  303. list_add_tail(&clt->detected, &therm_pm72_driver.clients);
  304. return clt;
  305. }
  306. /*
  307. * Here are the i2c chip access wrappers
  308. */
  309. static void initialize_adc(struct cpu_pid_state *state)
  310. {
  311. int rc;
  312. u8 buf[2];
  313. /* Read ADC the configuration register and cache it. We
  314. * also make sure Config2 contains proper values, I've seen
  315. * cases where we got stale grabage in there, thus preventing
  316. * proper reading of conv. values
  317. */
  318. /* Clear Config2 */
  319. buf[0] = 5;
  320. buf[1] = 0;
  321. i2c_master_send(state->monitor, buf, 2);
  322. /* Read & cache Config1 */
  323. buf[0] = 1;
  324. rc = i2c_master_send(state->monitor, buf, 1);
  325. if (rc > 0) {
  326. rc = i2c_master_recv(state->monitor, buf, 1);
  327. if (rc > 0) {
  328. state->adc_config = buf[0];
  329. DBG("ADC config reg: %02x\n", state->adc_config);
  330. /* Disable shutdown mode */
  331. state->adc_config &= 0xfe;
  332. buf[0] = 1;
  333. buf[1] = state->adc_config;
  334. rc = i2c_master_send(state->monitor, buf, 2);
  335. }
  336. }
  337. if (rc <= 0)
  338. printk(KERN_ERR "therm_pm72: Error reading ADC config"
  339. " register !\n");
  340. }
  341. static int read_smon_adc(struct cpu_pid_state *state, int chan)
  342. {
  343. int rc, data, tries = 0;
  344. u8 buf[2];
  345. for (;;) {
  346. /* Set channel */
  347. buf[0] = 1;
  348. buf[1] = (state->adc_config & 0x1f) | (chan << 5);
  349. rc = i2c_master_send(state->monitor, buf, 2);
  350. if (rc <= 0)
  351. goto error;
  352. /* Wait for conversion */
  353. msleep(1);
  354. /* Switch to data register */
  355. buf[0] = 4;
  356. rc = i2c_master_send(state->monitor, buf, 1);
  357. if (rc <= 0)
  358. goto error;
  359. /* Read result */
  360. rc = i2c_master_recv(state->monitor, buf, 2);
  361. if (rc < 0)
  362. goto error;
  363. data = ((u16)buf[0]) << 8 | (u16)buf[1];
  364. return data >> 6;
  365. error:
  366. DBG("Error reading ADC, retrying...\n");
  367. if (++tries > 10) {
  368. printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
  369. return -1;
  370. }
  371. msleep(10);
  372. }
  373. }
  374. static int read_lm87_reg(struct i2c_client * chip, int reg)
  375. {
  376. int rc, tries = 0;
  377. u8 buf;
  378. for (;;) {
  379. /* Set address */
  380. buf = (u8)reg;
  381. rc = i2c_master_send(chip, &buf, 1);
  382. if (rc <= 0)
  383. goto error;
  384. rc = i2c_master_recv(chip, &buf, 1);
  385. if (rc <= 0)
  386. goto error;
  387. return (int)buf;
  388. error:
  389. DBG("Error reading LM87, retrying...\n");
  390. if (++tries > 10) {
  391. printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
  392. return -1;
  393. }
  394. msleep(10);
  395. }
  396. }
  397. static int fan_read_reg(int reg, unsigned char *buf, int nb)
  398. {
  399. int tries, nr, nw;
  400. buf[0] = reg;
  401. tries = 0;
  402. for (;;) {
  403. nw = i2c_master_send(fcu, buf, 1);
  404. if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
  405. break;
  406. msleep(10);
  407. ++tries;
  408. }
  409. if (nw <= 0) {
  410. printk(KERN_ERR "Failure writing address to FCU: %d", nw);
  411. return -EIO;
  412. }
  413. tries = 0;
  414. for (;;) {
  415. nr = i2c_master_recv(fcu, buf, nb);
  416. if (nr > 0 || (nr < 0 && nr != -ENODEV) || tries >= 100)
  417. break;
  418. msleep(10);
  419. ++tries;
  420. }
  421. if (nr <= 0)
  422. printk(KERN_ERR "Failure reading data from FCU: %d", nw);
  423. return nr;
  424. }
  425. static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
  426. {
  427. int tries, nw;
  428. unsigned char buf[16];
  429. buf[0] = reg;
  430. memcpy(buf+1, ptr, nb);
  431. ++nb;
  432. tries = 0;
  433. for (;;) {
  434. nw = i2c_master_send(fcu, buf, nb);
  435. if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
  436. break;
  437. msleep(10);
  438. ++tries;
  439. }
  440. if (nw < 0)
  441. printk(KERN_ERR "Failure writing to FCU: %d", nw);
  442. return nw;
  443. }
  444. static int start_fcu(void)
  445. {
  446. unsigned char buf = 0xff;
  447. int rc;
  448. rc = fan_write_reg(0xe, &buf, 1);
  449. if (rc < 0)
  450. return -EIO;
  451. rc = fan_write_reg(0x2e, &buf, 1);
  452. if (rc < 0)
  453. return -EIO;
  454. rc = fan_read_reg(0, &buf, 1);
  455. if (rc < 0)
  456. return -EIO;
  457. fcu_rpm_shift = (buf == 1) ? 2 : 3;
  458. printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
  459. fcu_rpm_shift);
  460. return 0;
  461. }
  462. static int set_rpm_fan(int fan_index, int rpm)
  463. {
  464. unsigned char buf[2];
  465. int rc, id, min, max;
  466. if (fcu_fans[fan_index].type != FCU_FAN_RPM)
  467. return -EINVAL;
  468. id = fcu_fans[fan_index].id;
  469. if (id == FCU_FAN_ABSENT_ID)
  470. return -EINVAL;
  471. min = 2400 >> fcu_rpm_shift;
  472. max = 56000 >> fcu_rpm_shift;
  473. if (rpm < min)
  474. rpm = min;
  475. else if (rpm > max)
  476. rpm = max;
  477. buf[0] = rpm >> (8 - fcu_rpm_shift);
  478. buf[1] = rpm << fcu_rpm_shift;
  479. rc = fan_write_reg(0x10 + (id * 2), buf, 2);
  480. if (rc < 0)
  481. return -EIO;
  482. return 0;
  483. }
  484. static int get_rpm_fan(int fan_index, int programmed)
  485. {
  486. unsigned char failure;
  487. unsigned char active;
  488. unsigned char buf[2];
  489. int rc, id, reg_base;
  490. if (fcu_fans[fan_index].type != FCU_FAN_RPM)
  491. return -EINVAL;
  492. id = fcu_fans[fan_index].id;
  493. if (id == FCU_FAN_ABSENT_ID)
  494. return -EINVAL;
  495. rc = fan_read_reg(0xb, &failure, 1);
  496. if (rc != 1)
  497. return -EIO;
  498. if ((failure & (1 << id)) != 0)
  499. return -EFAULT;
  500. rc = fan_read_reg(0xd, &active, 1);
  501. if (rc != 1)
  502. return -EIO;
  503. if ((active & (1 << id)) == 0)
  504. return -ENXIO;
  505. /* Programmed value or real current speed */
  506. reg_base = programmed ? 0x10 : 0x11;
  507. rc = fan_read_reg(reg_base + (id * 2), buf, 2);
  508. if (rc != 2)
  509. return -EIO;
  510. return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
  511. }
  512. static int set_pwm_fan(int fan_index, int pwm)
  513. {
  514. unsigned char buf[2];
  515. int rc, id;
  516. if (fcu_fans[fan_index].type != FCU_FAN_PWM)
  517. return -EINVAL;
  518. id = fcu_fans[fan_index].id;
  519. if (id == FCU_FAN_ABSENT_ID)
  520. return -EINVAL;
  521. if (pwm < 10)
  522. pwm = 10;
  523. else if (pwm > 100)
  524. pwm = 100;
  525. pwm = (pwm * 2559) / 1000;
  526. buf[0] = pwm;
  527. rc = fan_write_reg(0x30 + (id * 2), buf, 1);
  528. if (rc < 0)
  529. return rc;
  530. return 0;
  531. }
  532. static int get_pwm_fan(int fan_index)
  533. {
  534. unsigned char failure;
  535. unsigned char active;
  536. unsigned char buf[2];
  537. int rc, id;
  538. if (fcu_fans[fan_index].type != FCU_FAN_PWM)
  539. return -EINVAL;
  540. id = fcu_fans[fan_index].id;
  541. if (id == FCU_FAN_ABSENT_ID)
  542. return -EINVAL;
  543. rc = fan_read_reg(0x2b, &failure, 1);
  544. if (rc != 1)
  545. return -EIO;
  546. if ((failure & (1 << id)) != 0)
  547. return -EFAULT;
  548. rc = fan_read_reg(0x2d, &active, 1);
  549. if (rc != 1)
  550. return -EIO;
  551. if ((active & (1 << id)) == 0)
  552. return -ENXIO;
  553. /* Programmed value or real current speed */
  554. rc = fan_read_reg(0x30 + (id * 2), buf, 1);
  555. if (rc != 1)
  556. return -EIO;
  557. return (buf[0] * 1000) / 2559;
  558. }
  559. static void tickle_fcu(void)
  560. {
  561. int pwm;
  562. pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
  563. DBG("FCU Tickle, slots fan is: %d\n", pwm);
  564. if (pwm < 0)
  565. pwm = 100;
  566. if (!rackmac) {
  567. pwm = SLOTS_FAN_DEFAULT_PWM;
  568. } else if (pwm < SLOTS_PID_OUTPUT_MIN)
  569. pwm = SLOTS_PID_OUTPUT_MIN;
  570. /* That is hopefully enough to make the FCU happy */
  571. set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
  572. }
  573. /*
  574. * Utility routine to read the CPU calibration EEPROM data
  575. * from the device-tree
  576. */
  577. static int read_eeprom(int cpu, struct mpu_data *out)
  578. {
  579. struct device_node *np;
  580. char nodename[64];
  581. const u8 *data;
  582. int len;
  583. /* prom.c routine for finding a node by path is a bit brain dead
  584. * and requires exact @xxx unit numbers. This is a bit ugly but
  585. * will work for these machines
  586. */
  587. sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
  588. np = of_find_node_by_path(nodename);
  589. if (np == NULL) {
  590. printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
  591. return -ENODEV;
  592. }
  593. data = of_get_property(np, "cpuid", &len);
  594. if (data == NULL) {
  595. printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
  596. of_node_put(np);
  597. return -ENODEV;
  598. }
  599. memcpy(out, data, sizeof(struct mpu_data));
  600. of_node_put(np);
  601. return 0;
  602. }
  603. static void fetch_cpu_pumps_minmax(void)
  604. {
  605. struct cpu_pid_state *state0 = &processor_state[0];
  606. struct cpu_pid_state *state1 = &processor_state[1];
  607. u16 pump_min = 0, pump_max = 0xffff;
  608. u16 tmp[4];
  609. /* Try to fetch pumps min/max infos from eeprom */
  610. memcpy(&tmp, &state0->mpu.processor_part_num, 8);
  611. if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
  612. pump_min = max(pump_min, tmp[0]);
  613. pump_max = min(pump_max, tmp[1]);
  614. }
  615. if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
  616. pump_min = max(pump_min, tmp[2]);
  617. pump_max = min(pump_max, tmp[3]);
  618. }
  619. /* Double check the values, this _IS_ needed as the EEPROM on
  620. * some dual 2.5Ghz G5s seem, at least, to have both min & max
  621. * same to the same value ... (grrrr)
  622. */
  623. if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
  624. pump_min = CPU_PUMP_OUTPUT_MIN;
  625. pump_max = CPU_PUMP_OUTPUT_MAX;
  626. }
  627. state0->pump_min = state1->pump_min = pump_min;
  628. state0->pump_max = state1->pump_max = pump_max;
  629. }
  630. /*
  631. * Now, unfortunately, sysfs doesn't give us a nice void * we could
  632. * pass around to the attribute functions, so we don't really have
  633. * choice but implement a bunch of them...
  634. *
  635. * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
  636. * the input twice... I accept patches :)
  637. */
  638. #define BUILD_SHOW_FUNC_FIX(name, data) \
  639. static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
  640. { \
  641. ssize_t r; \
  642. mutex_lock(&driver_lock); \
  643. r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
  644. mutex_unlock(&driver_lock); \
  645. return r; \
  646. }
  647. #define BUILD_SHOW_FUNC_INT(name, data) \
  648. static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
  649. { \
  650. return sprintf(buf, "%d", data); \
  651. }
  652. BUILD_SHOW_FUNC_FIX(cpu0_temperature, processor_state[0].last_temp)
  653. BUILD_SHOW_FUNC_FIX(cpu0_voltage, processor_state[0].voltage)
  654. BUILD_SHOW_FUNC_FIX(cpu0_current, processor_state[0].current_a)
  655. BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, processor_state[0].rpm)
  656. BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, processor_state[0].intake_rpm)
  657. BUILD_SHOW_FUNC_FIX(cpu1_temperature, processor_state[1].last_temp)
  658. BUILD_SHOW_FUNC_FIX(cpu1_voltage, processor_state[1].voltage)
  659. BUILD_SHOW_FUNC_FIX(cpu1_current, processor_state[1].current_a)
  660. BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, processor_state[1].rpm)
  661. BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, processor_state[1].intake_rpm)
  662. BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
  663. BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
  664. BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
  665. BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
  666. BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
  667. BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
  668. BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
  669. static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
  670. static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
  671. static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
  672. static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
  673. static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
  674. static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
  675. static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
  676. static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
  677. static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
  678. static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
  679. static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
  680. static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
  681. static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
  682. static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
  683. static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
  684. static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
  685. static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
  686. /*
  687. * CPUs fans control loop
  688. */
  689. static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
  690. {
  691. s32 ltemp, volts, amps;
  692. int index, rc = 0;
  693. /* Default (in case of error) */
  694. *temp = state->cur_temp;
  695. *power = state->cur_power;
  696. if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
  697. index = (state->index == 0) ?
  698. CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
  699. else
  700. index = (state->index == 0) ?
  701. CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
  702. /* Read current fan status */
  703. rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
  704. if (rc < 0) {
  705. /* XXX What do we do now ? Nothing for now, keep old value, but
  706. * return error upstream
  707. */
  708. DBG(" cpu %d, fan reading error !\n", state->index);
  709. } else {
  710. state->rpm = rc;
  711. DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
  712. }
  713. /* Get some sensor readings and scale it */
  714. ltemp = read_smon_adc(state, 1);
  715. if (ltemp == -1) {
  716. /* XXX What do we do now ? */
  717. state->overtemp++;
  718. if (rc == 0)
  719. rc = -EIO;
  720. DBG(" cpu %d, temp reading error !\n", state->index);
  721. } else {
  722. /* Fixup temperature according to diode calibration
  723. */
  724. DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
  725. state->index,
  726. ltemp, state->mpu.mdiode, state->mpu.bdiode);
  727. *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
  728. state->last_temp = *temp;
  729. DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
  730. }
  731. /*
  732. * Read voltage & current and calculate power
  733. */
  734. volts = read_smon_adc(state, 3);
  735. amps = read_smon_adc(state, 4);
  736. /* Scale voltage and current raw sensor values according to fixed scales
  737. * obtained in Darwin and calculate power from I and V
  738. */
  739. volts *= ADC_CPU_VOLTAGE_SCALE;
  740. amps *= ADC_CPU_CURRENT_SCALE;
  741. *power = (((u64)volts) * ((u64)amps)) >> 16;
  742. state->voltage = volts;
  743. state->current_a = amps;
  744. state->last_power = *power;
  745. DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
  746. state->index, FIX32TOPRINT(state->current_a),
  747. FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
  748. return 0;
  749. }
  750. static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
  751. {
  752. s32 power_target, integral, derivative, proportional, adj_in_target, sval;
  753. s64 integ_p, deriv_p, prop_p, sum;
  754. int i;
  755. /* Calculate power target value (could be done once for all)
  756. * and convert to a 16.16 fp number
  757. */
  758. power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
  759. DBG(" power target: %d.%03d, error: %d.%03d\n",
  760. FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
  761. /* Store temperature and power in history array */
  762. state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
  763. state->temp_history[state->cur_temp] = temp;
  764. state->cur_power = (state->cur_power + 1) % state->count_power;
  765. state->power_history[state->cur_power] = power;
  766. state->error_history[state->cur_power] = power_target - power;
  767. /* If first loop, fill the history table */
  768. if (state->first) {
  769. for (i = 0; i < (state->count_power - 1); i++) {
  770. state->cur_power = (state->cur_power + 1) % state->count_power;
  771. state->power_history[state->cur_power] = power;
  772. state->error_history[state->cur_power] = power_target - power;
  773. }
  774. for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
  775. state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
  776. state->temp_history[state->cur_temp] = temp;
  777. }
  778. state->first = 0;
  779. }
  780. /* Calculate the integral term normally based on the "power" values */
  781. sum = 0;
  782. integral = 0;
  783. for (i = 0; i < state->count_power; i++)
  784. integral += state->error_history[i];
  785. integral *= CPU_PID_INTERVAL;
  786. DBG(" integral: %08x\n", integral);
  787. /* Calculate the adjusted input (sense value).
  788. * G_r is 12.20
  789. * integ is 16.16
  790. * so the result is 28.36
  791. *
  792. * input target is mpu.ttarget, input max is mpu.tmax
  793. */
  794. integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
  795. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  796. sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
  797. adj_in_target = (state->mpu.ttarget << 16);
  798. if (adj_in_target > sval)
  799. adj_in_target = sval;
  800. DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
  801. state->mpu.ttarget);
  802. /* Calculate the derivative term */
  803. derivative = state->temp_history[state->cur_temp] -
  804. state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
  805. % CPU_TEMP_HISTORY_SIZE];
  806. derivative /= CPU_PID_INTERVAL;
  807. deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
  808. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  809. sum += deriv_p;
  810. /* Calculate the proportional term */
  811. proportional = temp - adj_in_target;
  812. prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
  813. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  814. sum += prop_p;
  815. /* Scale sum */
  816. sum >>= 36;
  817. DBG(" sum: %d\n", (int)sum);
  818. state->rpm += (s32)sum;
  819. }
  820. static void do_monitor_cpu_combined(void)
  821. {
  822. struct cpu_pid_state *state0 = &processor_state[0];
  823. struct cpu_pid_state *state1 = &processor_state[1];
  824. s32 temp0, power0, temp1, power1;
  825. s32 temp_combi, power_combi;
  826. int rc, intake, pump;
  827. rc = do_read_one_cpu_values(state0, &temp0, &power0);
  828. if (rc < 0) {
  829. /* XXX What do we do now ? */
  830. }
  831. state1->overtemp = 0;
  832. rc = do_read_one_cpu_values(state1, &temp1, &power1);
  833. if (rc < 0) {
  834. /* XXX What do we do now ? */
  835. }
  836. if (state1->overtemp)
  837. state0->overtemp++;
  838. temp_combi = max(temp0, temp1);
  839. power_combi = max(power0, power1);
  840. /* Check tmax, increment overtemp if we are there. At tmax+8, we go
  841. * full blown immediately and try to trigger a shutdown
  842. */
  843. if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
  844. printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
  845. temp_combi >> 16);
  846. state0->overtemp += CPU_MAX_OVERTEMP / 4;
  847. } else if (temp_combi > (state0->mpu.tmax << 16)) {
  848. state0->overtemp++;
  849. printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
  850. temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
  851. } else {
  852. if (state0->overtemp)
  853. printk(KERN_WARNING "Temperature back down to %d\n",
  854. temp_combi >> 16);
  855. state0->overtemp = 0;
  856. }
  857. if (state0->overtemp >= CPU_MAX_OVERTEMP)
  858. critical_state = 1;
  859. if (state0->overtemp > 0) {
  860. state0->rpm = state0->mpu.rmaxn_exhaust_fan;
  861. state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
  862. pump = state0->pump_max;
  863. goto do_set_fans;
  864. }
  865. /* Do the PID */
  866. do_cpu_pid(state0, temp_combi, power_combi);
  867. /* Range check */
  868. state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
  869. state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
  870. /* Calculate intake fan speed */
  871. intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
  872. intake = max(intake, (int)state0->mpu.rminn_intake_fan);
  873. intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
  874. state0->intake_rpm = intake;
  875. /* Calculate pump speed */
  876. pump = (state0->rpm * state0->pump_max) /
  877. state0->mpu.rmaxn_exhaust_fan;
  878. pump = min(pump, state0->pump_max);
  879. pump = max(pump, state0->pump_min);
  880. do_set_fans:
  881. /* We copy values from state 0 to state 1 for /sysfs */
  882. state1->rpm = state0->rpm;
  883. state1->intake_rpm = state0->intake_rpm;
  884. DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
  885. state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
  886. /* We should check for errors, shouldn't we ? But then, what
  887. * do we do once the error occurs ? For FCU notified fan
  888. * failures (-EFAULT) we probably want to notify userland
  889. * some way...
  890. */
  891. set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
  892. set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
  893. set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
  894. set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
  895. if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
  896. set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
  897. if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
  898. set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
  899. }
  900. static void do_monitor_cpu_split(struct cpu_pid_state *state)
  901. {
  902. s32 temp, power;
  903. int rc, intake;
  904. /* Read current fan status */
  905. rc = do_read_one_cpu_values(state, &temp, &power);
  906. if (rc < 0) {
  907. /* XXX What do we do now ? */
  908. }
  909. /* Check tmax, increment overtemp if we are there. At tmax+8, we go
  910. * full blown immediately and try to trigger a shutdown
  911. */
  912. if (temp >= ((state->mpu.tmax + 8) << 16)) {
  913. printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
  914. " (%d) !\n",
  915. state->index, temp >> 16);
  916. state->overtemp += CPU_MAX_OVERTEMP / 4;
  917. } else if (temp > (state->mpu.tmax << 16)) {
  918. state->overtemp++;
  919. printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
  920. state->index, temp >> 16, state->mpu.tmax, state->overtemp);
  921. } else {
  922. if (state->overtemp)
  923. printk(KERN_WARNING "CPU %d temperature back down to %d\n",
  924. state->index, temp >> 16);
  925. state->overtemp = 0;
  926. }
  927. if (state->overtemp >= CPU_MAX_OVERTEMP)
  928. critical_state = 1;
  929. if (state->overtemp > 0) {
  930. state->rpm = state->mpu.rmaxn_exhaust_fan;
  931. state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
  932. goto do_set_fans;
  933. }
  934. /* Do the PID */
  935. do_cpu_pid(state, temp, power);
  936. /* Range check */
  937. state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
  938. state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
  939. /* Calculate intake fan */
  940. intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
  941. intake = max(intake, (int)state->mpu.rminn_intake_fan);
  942. intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
  943. state->intake_rpm = intake;
  944. do_set_fans:
  945. DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
  946. state->index, (int)state->rpm, intake, state->overtemp);
  947. /* We should check for errors, shouldn't we ? But then, what
  948. * do we do once the error occurs ? For FCU notified fan
  949. * failures (-EFAULT) we probably want to notify userland
  950. * some way...
  951. */
  952. if (state->index == 0) {
  953. set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
  954. set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
  955. } else {
  956. set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
  957. set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
  958. }
  959. }
  960. static void do_monitor_cpu_rack(struct cpu_pid_state *state)
  961. {
  962. s32 temp, power, fan_min;
  963. int rc;
  964. /* Read current fan status */
  965. rc = do_read_one_cpu_values(state, &temp, &power);
  966. if (rc < 0) {
  967. /* XXX What do we do now ? */
  968. }
  969. /* Check tmax, increment overtemp if we are there. At tmax+8, we go
  970. * full blown immediately and try to trigger a shutdown
  971. */
  972. if (temp >= ((state->mpu.tmax + 8) << 16)) {
  973. printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
  974. " (%d) !\n",
  975. state->index, temp >> 16);
  976. state->overtemp = CPU_MAX_OVERTEMP / 4;
  977. } else if (temp > (state->mpu.tmax << 16)) {
  978. state->overtemp++;
  979. printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
  980. state->index, temp >> 16, state->mpu.tmax, state->overtemp);
  981. } else {
  982. if (state->overtemp)
  983. printk(KERN_WARNING "CPU %d temperature back down to %d\n",
  984. state->index, temp >> 16);
  985. state->overtemp = 0;
  986. }
  987. if (state->overtemp >= CPU_MAX_OVERTEMP)
  988. critical_state = 1;
  989. if (state->overtemp > 0) {
  990. state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
  991. goto do_set_fans;
  992. }
  993. /* Do the PID */
  994. do_cpu_pid(state, temp, power);
  995. /* Check clamp from dimms */
  996. fan_min = dimm_output_clamp;
  997. fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
  998. DBG(" CPU min mpu = %d, min dimm = %d\n",
  999. state->mpu.rminn_intake_fan, dimm_output_clamp);
  1000. state->rpm = max(state->rpm, (int)fan_min);
  1001. state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
  1002. state->intake_rpm = state->rpm;
  1003. do_set_fans:
  1004. DBG("** CPU %d RPM: %d overtemp: %d\n",
  1005. state->index, (int)state->rpm, state->overtemp);
  1006. /* We should check for errors, shouldn't we ? But then, what
  1007. * do we do once the error occurs ? For FCU notified fan
  1008. * failures (-EFAULT) we probably want to notify userland
  1009. * some way...
  1010. */
  1011. if (state->index == 0) {
  1012. set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
  1013. set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
  1014. set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
  1015. } else {
  1016. set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
  1017. set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
  1018. set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
  1019. }
  1020. }
  1021. /*
  1022. * Initialize the state structure for one CPU control loop
  1023. */
  1024. static int init_processor_state(struct cpu_pid_state *state, int index)
  1025. {
  1026. int err;
  1027. state->index = index;
  1028. state->first = 1;
  1029. state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
  1030. state->overtemp = 0;
  1031. state->adc_config = 0x00;
  1032. if (index == 0)
  1033. state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
  1034. else if (index == 1)
  1035. state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
  1036. if (state->monitor == NULL)
  1037. goto fail;
  1038. if (read_eeprom(index, &state->mpu))
  1039. goto fail;
  1040. state->count_power = state->mpu.tguardband;
  1041. if (state->count_power > CPU_POWER_HISTORY_SIZE) {
  1042. printk(KERN_WARNING "Warning ! too many power history slots\n");
  1043. state->count_power = CPU_POWER_HISTORY_SIZE;
  1044. }
  1045. DBG("CPU %d Using %d power history entries\n", index, state->count_power);
  1046. if (index == 0) {
  1047. err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
  1048. err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
  1049. err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
  1050. err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
  1051. err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
  1052. } else {
  1053. err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
  1054. err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
  1055. err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
  1056. err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
  1057. err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
  1058. }
  1059. if (err)
  1060. printk(KERN_WARNING "Failed to create some of the attribute"
  1061. "files for CPU %d\n", index);
  1062. return 0;
  1063. fail:
  1064. state->monitor = NULL;
  1065. return -ENODEV;
  1066. }
  1067. /*
  1068. * Dispose of the state data for one CPU control loop
  1069. */
  1070. static void dispose_processor_state(struct cpu_pid_state *state)
  1071. {
  1072. if (state->monitor == NULL)
  1073. return;
  1074. if (state->index == 0) {
  1075. device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
  1076. device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
  1077. device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
  1078. device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
  1079. device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
  1080. } else {
  1081. device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
  1082. device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
  1083. device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
  1084. device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
  1085. device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
  1086. }
  1087. state->monitor = NULL;
  1088. }
  1089. /*
  1090. * Motherboard backside & U3 heatsink fan control loop
  1091. */
  1092. static void do_monitor_backside(struct backside_pid_state *state)
  1093. {
  1094. s32 temp, integral, derivative, fan_min;
  1095. s64 integ_p, deriv_p, prop_p, sum;
  1096. int i, rc;
  1097. if (--state->ticks != 0)
  1098. return;
  1099. state->ticks = backside_params.interval;
  1100. DBG("backside:\n");
  1101. /* Check fan status */
  1102. rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
  1103. if (rc < 0) {
  1104. printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
  1105. /* XXX What do we do now ? */
  1106. } else
  1107. state->pwm = rc;
  1108. DBG(" current pwm: %d\n", state->pwm);
  1109. /* Get some sensor readings */
  1110. temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
  1111. state->last_temp = temp;
  1112. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1113. FIX32TOPRINT(backside_params.input_target));
  1114. /* Store temperature and error in history array */
  1115. state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
  1116. state->sample_history[state->cur_sample] = temp;
  1117. state->error_history[state->cur_sample] = temp - backside_params.input_target;
  1118. /* If first loop, fill the history table */
  1119. if (state->first) {
  1120. for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
  1121. state->cur_sample = (state->cur_sample + 1) %
  1122. BACKSIDE_PID_HISTORY_SIZE;
  1123. state->sample_history[state->cur_sample] = temp;
  1124. state->error_history[state->cur_sample] =
  1125. temp - backside_params.input_target;
  1126. }
  1127. state->first = 0;
  1128. }
  1129. /* Calculate the integral term */
  1130. sum = 0;
  1131. integral = 0;
  1132. for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
  1133. integral += state->error_history[i];
  1134. integral *= backside_params.interval;
  1135. DBG(" integral: %08x\n", integral);
  1136. integ_p = ((s64)backside_params.G_r) * (s64)integral;
  1137. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1138. sum += integ_p;
  1139. /* Calculate the derivative term */
  1140. derivative = state->error_history[state->cur_sample] -
  1141. state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
  1142. % BACKSIDE_PID_HISTORY_SIZE];
  1143. derivative /= backside_params.interval;
  1144. deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
  1145. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1146. sum += deriv_p;
  1147. /* Calculate the proportional term */
  1148. prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
  1149. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1150. sum += prop_p;
  1151. /* Scale sum */
  1152. sum >>= 36;
  1153. DBG(" sum: %d\n", (int)sum);
  1154. if (backside_params.additive)
  1155. state->pwm += (s32)sum;
  1156. else
  1157. state->pwm = sum;
  1158. /* Check for clamp */
  1159. fan_min = (dimm_output_clamp * 100) / 14000;
  1160. fan_min = max(fan_min, backside_params.output_min);
  1161. state->pwm = max(state->pwm, fan_min);
  1162. state->pwm = min(state->pwm, backside_params.output_max);
  1163. DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
  1164. set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
  1165. }
  1166. /*
  1167. * Initialize the state structure for the backside fan control loop
  1168. */
  1169. static int init_backside_state(struct backside_pid_state *state)
  1170. {
  1171. struct device_node *u3;
  1172. int u3h = 1; /* conservative by default */
  1173. int err;
  1174. /*
  1175. * There are different PID params for machines with U3 and machines
  1176. * with U3H, pick the right ones now
  1177. */
  1178. u3 = of_find_node_by_path("/u3@0,f8000000");
  1179. if (u3 != NULL) {
  1180. const u32 *vers = of_get_property(u3, "device-rev", NULL);
  1181. if (vers)
  1182. if (((*vers) & 0x3f) < 0x34)
  1183. u3h = 0;
  1184. of_node_put(u3);
  1185. }
  1186. if (rackmac) {
  1187. backside_params.G_d = BACKSIDE_PID_RACK_G_d;
  1188. backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
  1189. backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
  1190. backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
  1191. backside_params.G_p = BACKSIDE_PID_RACK_G_p;
  1192. backside_params.G_r = BACKSIDE_PID_G_r;
  1193. backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
  1194. backside_params.additive = 0;
  1195. } else if (u3h) {
  1196. backside_params.G_d = BACKSIDE_PID_U3H_G_d;
  1197. backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
  1198. backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
  1199. backside_params.interval = BACKSIDE_PID_INTERVAL;
  1200. backside_params.G_p = BACKSIDE_PID_G_p;
  1201. backside_params.G_r = BACKSIDE_PID_G_r;
  1202. backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
  1203. backside_params.additive = 1;
  1204. } else {
  1205. backside_params.G_d = BACKSIDE_PID_U3_G_d;
  1206. backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
  1207. backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
  1208. backside_params.interval = BACKSIDE_PID_INTERVAL;
  1209. backside_params.G_p = BACKSIDE_PID_G_p;
  1210. backside_params.G_r = BACKSIDE_PID_G_r;
  1211. backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
  1212. backside_params.additive = 1;
  1213. }
  1214. state->ticks = 1;
  1215. state->first = 1;
  1216. state->pwm = 50;
  1217. state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
  1218. if (state->monitor == NULL)
  1219. return -ENODEV;
  1220. err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
  1221. err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
  1222. if (err)
  1223. printk(KERN_WARNING "Failed to create attribute file(s)"
  1224. " for backside fan\n");
  1225. return 0;
  1226. }
  1227. /*
  1228. * Dispose of the state data for the backside control loop
  1229. */
  1230. static void dispose_backside_state(struct backside_pid_state *state)
  1231. {
  1232. if (state->monitor == NULL)
  1233. return;
  1234. device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
  1235. device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
  1236. state->monitor = NULL;
  1237. }
  1238. /*
  1239. * Drives bay fan control loop
  1240. */
  1241. static void do_monitor_drives(struct drives_pid_state *state)
  1242. {
  1243. s32 temp, integral, derivative;
  1244. s64 integ_p, deriv_p, prop_p, sum;
  1245. int i, rc;
  1246. if (--state->ticks != 0)
  1247. return;
  1248. state->ticks = DRIVES_PID_INTERVAL;
  1249. DBG("drives:\n");
  1250. /* Check fan status */
  1251. rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
  1252. if (rc < 0) {
  1253. printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
  1254. /* XXX What do we do now ? */
  1255. } else
  1256. state->rpm = rc;
  1257. DBG(" current rpm: %d\n", state->rpm);
  1258. /* Get some sensor readings */
  1259. temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
  1260. DS1775_TEMP)) << 8;
  1261. state->last_temp = temp;
  1262. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1263. FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
  1264. /* Store temperature and error in history array */
  1265. state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
  1266. state->sample_history[state->cur_sample] = temp;
  1267. state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
  1268. /* If first loop, fill the history table */
  1269. if (state->first) {
  1270. for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
  1271. state->cur_sample = (state->cur_sample + 1) %
  1272. DRIVES_PID_HISTORY_SIZE;
  1273. state->sample_history[state->cur_sample] = temp;
  1274. state->error_history[state->cur_sample] =
  1275. temp - DRIVES_PID_INPUT_TARGET;
  1276. }
  1277. state->first = 0;
  1278. }
  1279. /* Calculate the integral term */
  1280. sum = 0;
  1281. integral = 0;
  1282. for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
  1283. integral += state->error_history[i];
  1284. integral *= DRIVES_PID_INTERVAL;
  1285. DBG(" integral: %08x\n", integral);
  1286. integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
  1287. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1288. sum += integ_p;
  1289. /* Calculate the derivative term */
  1290. derivative = state->error_history[state->cur_sample] -
  1291. state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
  1292. % DRIVES_PID_HISTORY_SIZE];
  1293. derivative /= DRIVES_PID_INTERVAL;
  1294. deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
  1295. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1296. sum += deriv_p;
  1297. /* Calculate the proportional term */
  1298. prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
  1299. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1300. sum += prop_p;
  1301. /* Scale sum */
  1302. sum >>= 36;
  1303. DBG(" sum: %d\n", (int)sum);
  1304. state->rpm += (s32)sum;
  1305. state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
  1306. state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
  1307. DBG("** DRIVES RPM: %d\n", (int)state->rpm);
  1308. set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
  1309. }
  1310. /*
  1311. * Initialize the state structure for the drives bay fan control loop
  1312. */
  1313. static int init_drives_state(struct drives_pid_state *state)
  1314. {
  1315. int err;
  1316. state->ticks = 1;
  1317. state->first = 1;
  1318. state->rpm = 1000;
  1319. state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
  1320. if (state->monitor == NULL)
  1321. return -ENODEV;
  1322. err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
  1323. err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
  1324. if (err)
  1325. printk(KERN_WARNING "Failed to create attribute file(s)"
  1326. " for drives bay fan\n");
  1327. return 0;
  1328. }
  1329. /*
  1330. * Dispose of the state data for the drives control loop
  1331. */
  1332. static void dispose_drives_state(struct drives_pid_state *state)
  1333. {
  1334. if (state->monitor == NULL)
  1335. return;
  1336. device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
  1337. device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
  1338. state->monitor = NULL;
  1339. }
  1340. /*
  1341. * DIMMs temp control loop
  1342. */
  1343. static void do_monitor_dimms(struct dimm_pid_state *state)
  1344. {
  1345. s32 temp, integral, derivative, fan_min;
  1346. s64 integ_p, deriv_p, prop_p, sum;
  1347. int i;
  1348. if (--state->ticks != 0)
  1349. return;
  1350. state->ticks = DIMM_PID_INTERVAL;
  1351. DBG("DIMM:\n");
  1352. DBG(" current value: %d\n", state->output);
  1353. temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
  1354. if (temp < 0)
  1355. return;
  1356. temp <<= 16;
  1357. state->last_temp = temp;
  1358. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1359. FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
  1360. /* Store temperature and error in history array */
  1361. state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
  1362. state->sample_history[state->cur_sample] = temp;
  1363. state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
  1364. /* If first loop, fill the history table */
  1365. if (state->first) {
  1366. for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
  1367. state->cur_sample = (state->cur_sample + 1) %
  1368. DIMM_PID_HISTORY_SIZE;
  1369. state->sample_history[state->cur_sample] = temp;
  1370. state->error_history[state->cur_sample] =
  1371. temp - DIMM_PID_INPUT_TARGET;
  1372. }
  1373. state->first = 0;
  1374. }
  1375. /* Calculate the integral term */
  1376. sum = 0;
  1377. integral = 0;
  1378. for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
  1379. integral += state->error_history[i];
  1380. integral *= DIMM_PID_INTERVAL;
  1381. DBG(" integral: %08x\n", integral);
  1382. integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
  1383. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1384. sum += integ_p;
  1385. /* Calculate the derivative term */
  1386. derivative = state->error_history[state->cur_sample] -
  1387. state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
  1388. % DIMM_PID_HISTORY_SIZE];
  1389. derivative /= DIMM_PID_INTERVAL;
  1390. deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
  1391. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1392. sum += deriv_p;
  1393. /* Calculate the proportional term */
  1394. prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
  1395. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1396. sum += prop_p;
  1397. /* Scale sum */
  1398. sum >>= 36;
  1399. DBG(" sum: %d\n", (int)sum);
  1400. state->output = (s32)sum;
  1401. state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
  1402. state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
  1403. dimm_output_clamp = state->output;
  1404. DBG("** DIMM clamp value: %d\n", (int)state->output);
  1405. /* Backside PID is only every 5 seconds, force backside fan clamping now */
  1406. fan_min = (dimm_output_clamp * 100) / 14000;
  1407. fan_min = max(fan_min, backside_params.output_min);
  1408. if (backside_state.pwm < fan_min) {
  1409. backside_state.pwm = fan_min;
  1410. DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
  1411. set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
  1412. }
  1413. }
  1414. /*
  1415. * Initialize the state structure for the DIMM temp control loop
  1416. */
  1417. static int init_dimms_state(struct dimm_pid_state *state)
  1418. {
  1419. state->ticks = 1;
  1420. state->first = 1;
  1421. state->output = 4000;
  1422. state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
  1423. if (state->monitor == NULL)
  1424. return -ENODEV;
  1425. if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
  1426. printk(KERN_WARNING "Failed to create attribute file"
  1427. " for DIMM temperature\n");
  1428. return 0;
  1429. }
  1430. /*
  1431. * Dispose of the state data for the DIMM control loop
  1432. */
  1433. static void dispose_dimms_state(struct dimm_pid_state *state)
  1434. {
  1435. if (state->monitor == NULL)
  1436. return;
  1437. device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
  1438. state->monitor = NULL;
  1439. }
  1440. /*
  1441. * Slots fan control loop
  1442. */
  1443. static void do_monitor_slots(struct slots_pid_state *state)
  1444. {
  1445. s32 temp, integral, derivative;
  1446. s64 integ_p, deriv_p, prop_p, sum;
  1447. int i, rc;
  1448. if (--state->ticks != 0)
  1449. return;
  1450. state->ticks = SLOTS_PID_INTERVAL;
  1451. DBG("slots:\n");
  1452. /* Check fan status */
  1453. rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
  1454. if (rc < 0) {
  1455. printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
  1456. /* XXX What do we do now ? */
  1457. } else
  1458. state->pwm = rc;
  1459. DBG(" current pwm: %d\n", state->pwm);
  1460. /* Get some sensor readings */
  1461. temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
  1462. DS1775_TEMP)) << 8;
  1463. state->last_temp = temp;
  1464. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1465. FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
  1466. /* Store temperature and error in history array */
  1467. state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
  1468. state->sample_history[state->cur_sample] = temp;
  1469. state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
  1470. /* If first loop, fill the history table */
  1471. if (state->first) {
  1472. for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
  1473. state->cur_sample = (state->cur_sample + 1) %
  1474. SLOTS_PID_HISTORY_SIZE;
  1475. state->sample_history[state->cur_sample] = temp;
  1476. state->error_history[state->cur_sample] =
  1477. temp - SLOTS_PID_INPUT_TARGET;
  1478. }
  1479. state->first = 0;
  1480. }
  1481. /* Calculate the integral term */
  1482. sum = 0;
  1483. integral = 0;
  1484. for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
  1485. integral += state->error_history[i];
  1486. integral *= SLOTS_PID_INTERVAL;
  1487. DBG(" integral: %08x\n", integral);
  1488. integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
  1489. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1490. sum += integ_p;
  1491. /* Calculate the derivative term */
  1492. derivative = state->error_history[state->cur_sample] -
  1493. state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
  1494. % SLOTS_PID_HISTORY_SIZE];
  1495. derivative /= SLOTS_PID_INTERVAL;
  1496. deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
  1497. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1498. sum += deriv_p;
  1499. /* Calculate the proportional term */
  1500. prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
  1501. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1502. sum += prop_p;
  1503. /* Scale sum */
  1504. sum >>= 36;
  1505. DBG(" sum: %d\n", (int)sum);
  1506. state->pwm = (s32)sum;
  1507. state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
  1508. state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
  1509. DBG("** DRIVES PWM: %d\n", (int)state->pwm);
  1510. set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
  1511. }
  1512. /*
  1513. * Initialize the state structure for the slots bay fan control loop
  1514. */
  1515. static int init_slots_state(struct slots_pid_state *state)
  1516. {
  1517. int err;
  1518. state->ticks = 1;
  1519. state->first = 1;
  1520. state->pwm = 50;
  1521. state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
  1522. if (state->monitor == NULL)
  1523. return -ENODEV;
  1524. err = device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
  1525. err |= device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
  1526. if (err)
  1527. printk(KERN_WARNING "Failed to create attribute file(s)"
  1528. " for slots bay fan\n");
  1529. return 0;
  1530. }
  1531. /*
  1532. * Dispose of the state data for the slots control loop
  1533. */
  1534. static void dispose_slots_state(struct slots_pid_state *state)
  1535. {
  1536. if (state->monitor == NULL)
  1537. return;
  1538. device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
  1539. device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
  1540. state->monitor = NULL;
  1541. }
  1542. static int call_critical_overtemp(void)
  1543. {
  1544. char *argv[] = { critical_overtemp_path, NULL };
  1545. static char *envp[] = { "HOME=/",
  1546. "TERM=linux",
  1547. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  1548. NULL };
  1549. return call_usermodehelper(critical_overtemp_path,
  1550. argv, envp, UMH_WAIT_EXEC);
  1551. }
  1552. /*
  1553. * Here's the kernel thread that calls the various control loops
  1554. */
  1555. static int main_control_loop(void *x)
  1556. {
  1557. DBG("main_control_loop started\n");
  1558. mutex_lock(&driver_lock);
  1559. if (start_fcu() < 0) {
  1560. printk(KERN_ERR "kfand: failed to start FCU\n");
  1561. mutex_unlock(&driver_lock);
  1562. goto out;
  1563. }
  1564. /* Set the PCI fan once for now on non-RackMac */
  1565. if (!rackmac)
  1566. set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
  1567. /* Initialize ADCs */
  1568. initialize_adc(&processor_state[0]);
  1569. if (processor_state[1].monitor != NULL)
  1570. initialize_adc(&processor_state[1]);
  1571. fcu_tickle_ticks = FCU_TICKLE_TICKS;
  1572. mutex_unlock(&driver_lock);
  1573. while (state == state_attached) {
  1574. unsigned long elapsed, start;
  1575. start = jiffies;
  1576. mutex_lock(&driver_lock);
  1577. /* Tickle the FCU just in case */
  1578. if (--fcu_tickle_ticks < 0) {
  1579. fcu_tickle_ticks = FCU_TICKLE_TICKS;
  1580. tickle_fcu();
  1581. }
  1582. /* First, we always calculate the new DIMMs state on an Xserve */
  1583. if (rackmac)
  1584. do_monitor_dimms(&dimms_state);
  1585. /* Then, the CPUs */
  1586. if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
  1587. do_monitor_cpu_combined();
  1588. else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
  1589. do_monitor_cpu_rack(&processor_state[0]);
  1590. if (processor_state[1].monitor != NULL)
  1591. do_monitor_cpu_rack(&processor_state[1]);
  1592. // better deal with UP
  1593. } else {
  1594. do_monitor_cpu_split(&processor_state[0]);
  1595. if (processor_state[1].monitor != NULL)
  1596. do_monitor_cpu_split(&processor_state[1]);
  1597. // better deal with UP
  1598. }
  1599. /* Then, the rest */
  1600. do_monitor_backside(&backside_state);
  1601. if (rackmac)
  1602. do_monitor_slots(&slots_state);
  1603. else
  1604. do_monitor_drives(&drives_state);
  1605. mutex_unlock(&driver_lock);
  1606. if (critical_state == 1) {
  1607. printk(KERN_WARNING "Temperature control detected a critical condition\n");
  1608. printk(KERN_WARNING "Attempting to shut down...\n");
  1609. if (call_critical_overtemp()) {
  1610. printk(KERN_WARNING "Can't call %s, power off now!\n",
  1611. critical_overtemp_path);
  1612. machine_power_off();
  1613. }
  1614. }
  1615. if (critical_state > 0)
  1616. critical_state++;
  1617. if (critical_state > MAX_CRITICAL_STATE) {
  1618. printk(KERN_WARNING "Shutdown timed out, power off now !\n");
  1619. machine_power_off();
  1620. }
  1621. // FIXME: Deal with signals
  1622. elapsed = jiffies - start;
  1623. if (elapsed < HZ)
  1624. schedule_timeout_interruptible(HZ - elapsed);
  1625. }
  1626. out:
  1627. DBG("main_control_loop ended\n");
  1628. ctrl_task = 0;
  1629. complete_and_exit(&ctrl_complete, 0);
  1630. }
  1631. /*
  1632. * Dispose the control loops when tearing down
  1633. */
  1634. static void dispose_control_loops(void)
  1635. {
  1636. dispose_processor_state(&processor_state[0]);
  1637. dispose_processor_state(&processor_state[1]);
  1638. dispose_backside_state(&backside_state);
  1639. dispose_drives_state(&drives_state);
  1640. dispose_slots_state(&slots_state);
  1641. dispose_dimms_state(&dimms_state);
  1642. }
  1643. /*
  1644. * Create the control loops. U3-0 i2c bus is up, so we can now
  1645. * get to the various sensors
  1646. */
  1647. static int create_control_loops(void)
  1648. {
  1649. struct device_node *np;
  1650. /* Count CPUs from the device-tree, we don't care how many are
  1651. * actually used by Linux
  1652. */
  1653. cpu_count = 0;
  1654. for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
  1655. cpu_count++;
  1656. DBG("counted %d CPUs in the device-tree\n", cpu_count);
  1657. /* Decide the type of PID algorithm to use based on the presence of
  1658. * the pumps, though that may not be the best way, that is good enough
  1659. * for now
  1660. */
  1661. if (rackmac)
  1662. cpu_pid_type = CPU_PID_TYPE_RACKMAC;
  1663. else if (of_machine_is_compatible("PowerMac7,3")
  1664. && (cpu_count > 1)
  1665. && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
  1666. && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
  1667. printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
  1668. cpu_pid_type = CPU_PID_TYPE_COMBINED;
  1669. } else
  1670. cpu_pid_type = CPU_PID_TYPE_SPLIT;
  1671. /* Create control loops for everything. If any fail, everything
  1672. * fails
  1673. */
  1674. if (init_processor_state(&processor_state[0], 0))
  1675. goto fail;
  1676. if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
  1677. fetch_cpu_pumps_minmax();
  1678. if (cpu_count > 1 && init_processor_state(&processor_state[1], 1))
  1679. goto fail;
  1680. if (init_backside_state(&backside_state))
  1681. goto fail;
  1682. if (rackmac && init_dimms_state(&dimms_state))
  1683. goto fail;
  1684. if (rackmac && init_slots_state(&slots_state))
  1685. goto fail;
  1686. if (!rackmac && init_drives_state(&drives_state))
  1687. goto fail;
  1688. DBG("all control loops up !\n");
  1689. return 0;
  1690. fail:
  1691. DBG("failure creating control loops, disposing\n");
  1692. dispose_control_loops();
  1693. return -ENODEV;
  1694. }
  1695. /*
  1696. * Start the control loops after everything is up, that is create
  1697. * the thread that will make them run
  1698. */
  1699. static void start_control_loops(void)
  1700. {
  1701. init_completion(&ctrl_complete);
  1702. ctrl_task = kthread_run(main_control_loop, NULL, "kfand");
  1703. }
  1704. /*
  1705. * Stop the control loops when tearing down
  1706. */
  1707. static void stop_control_loops(void)
  1708. {
  1709. if (ctrl_task)
  1710. wait_for_completion(&ctrl_complete);
  1711. }
  1712. /*
  1713. * Attach to the i2c FCU after detecting U3-1 bus
  1714. */
  1715. static int attach_fcu(void)
  1716. {
  1717. fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
  1718. if (fcu == NULL)
  1719. return -ENODEV;
  1720. DBG("FCU attached\n");
  1721. return 0;
  1722. }
  1723. /*
  1724. * Detach from the i2c FCU when tearing down
  1725. */
  1726. static void detach_fcu(void)
  1727. {
  1728. fcu = NULL;
  1729. }
  1730. /*
  1731. * Attach to the i2c controller. We probe the various chips based
  1732. * on the device-tree nodes and build everything for the driver to
  1733. * run, we then kick the driver monitoring thread
  1734. */
  1735. static int therm_pm72_attach(struct i2c_adapter *adapter)
  1736. {
  1737. mutex_lock(&driver_lock);
  1738. /* Check state */
  1739. if (state == state_detached)
  1740. state = state_attaching;
  1741. if (state != state_attaching) {
  1742. mutex_unlock(&driver_lock);
  1743. return 0;
  1744. }
  1745. /* Check if we are looking for one of these */
  1746. if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
  1747. u3_0 = adapter;
  1748. DBG("found U3-0\n");
  1749. if (k2 || !rackmac)
  1750. if (create_control_loops())
  1751. u3_0 = NULL;
  1752. } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
  1753. u3_1 = adapter;
  1754. DBG("found U3-1, attaching FCU\n");
  1755. if (attach_fcu())
  1756. u3_1 = NULL;
  1757. } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
  1758. k2 = adapter;
  1759. DBG("Found K2\n");
  1760. if (u3_0 && rackmac)
  1761. if (create_control_loops())
  1762. k2 = NULL;
  1763. }
  1764. /* We got all we need, start control loops */
  1765. if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
  1766. DBG("everything up, starting control loops\n");
  1767. state = state_attached;
  1768. start_control_loops();
  1769. }
  1770. mutex_unlock(&driver_lock);
  1771. return 0;
  1772. }
  1773. static int therm_pm72_probe(struct i2c_client *client,
  1774. const struct i2c_device_id *id)
  1775. {
  1776. /* Always succeed, the real work was done in therm_pm72_attach() */
  1777. return 0;
  1778. }
  1779. /*
  1780. * Called when any of the devices which participates into thermal management
  1781. * is going away.
  1782. */
  1783. static int therm_pm72_remove(struct i2c_client *client)
  1784. {
  1785. struct i2c_adapter *adapter = client->adapter;
  1786. mutex_lock(&driver_lock);
  1787. if (state != state_detached)
  1788. state = state_detaching;
  1789. /* Stop control loops if any */
  1790. DBG("stopping control loops\n");
  1791. mutex_unlock(&driver_lock);
  1792. stop_control_loops();
  1793. mutex_lock(&driver_lock);
  1794. if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
  1795. DBG("lost U3-0, disposing control loops\n");
  1796. dispose_control_loops();
  1797. u3_0 = NULL;
  1798. }
  1799. if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
  1800. DBG("lost U3-1, detaching FCU\n");
  1801. detach_fcu();
  1802. u3_1 = NULL;
  1803. }
  1804. if (u3_0 == NULL && u3_1 == NULL)
  1805. state = state_detached;
  1806. mutex_unlock(&driver_lock);
  1807. return 0;
  1808. }
  1809. /*
  1810. * i2c_driver structure to attach to the host i2c controller
  1811. */
  1812. static const struct i2c_device_id therm_pm72_id[] = {
  1813. /*
  1814. * Fake device name, thermal management is done by several
  1815. * chips but we don't need to differentiate between them at
  1816. * this point.
  1817. */
  1818. { "therm_pm72", 0 },
  1819. { }
  1820. };
  1821. static struct i2c_driver therm_pm72_driver = {
  1822. .driver = {
  1823. .name = "therm_pm72",
  1824. },
  1825. .attach_adapter = therm_pm72_attach,
  1826. .probe = therm_pm72_probe,
  1827. .remove = therm_pm72_remove,
  1828. .id_table = therm_pm72_id,
  1829. };
  1830. static int fan_check_loc_match(const char *loc, int fan)
  1831. {
  1832. char tmp[64];
  1833. char *c, *e;
  1834. strlcpy(tmp, fcu_fans[fan].loc, 64);
  1835. c = tmp;
  1836. for (;;) {
  1837. e = strchr(c, ',');
  1838. if (e)
  1839. *e = 0;
  1840. if (strcmp(loc, c) == 0)
  1841. return 1;
  1842. if (e == NULL)
  1843. break;
  1844. c = e + 1;
  1845. }
  1846. return 0;
  1847. }
  1848. static void fcu_lookup_fans(struct device_node *fcu_node)
  1849. {
  1850. struct device_node *np = NULL;
  1851. int i;
  1852. /* The table is filled by default with values that are suitable
  1853. * for the old machines without device-tree informations. We scan
  1854. * the device-tree and override those values with whatever is
  1855. * there
  1856. */
  1857. DBG("Looking up FCU controls in device-tree...\n");
  1858. while ((np = of_get_next_child(fcu_node, np)) != NULL) {
  1859. int type = -1;
  1860. const char *loc;
  1861. const u32 *reg;
  1862. DBG(" control: %s, type: %s\n", np->name, np->type);
  1863. /* Detect control type */
  1864. if (!strcmp(np->type, "fan-rpm-control") ||
  1865. !strcmp(np->type, "fan-rpm"))
  1866. type = FCU_FAN_RPM;
  1867. if (!strcmp(np->type, "fan-pwm-control") ||
  1868. !strcmp(np->type, "fan-pwm"))
  1869. type = FCU_FAN_PWM;
  1870. /* Only care about fans for now */
  1871. if (type == -1)
  1872. continue;
  1873. /* Lookup for a matching location */
  1874. loc = of_get_property(np, "location", NULL);
  1875. reg = of_get_property(np, "reg", NULL);
  1876. if (loc == NULL || reg == NULL)
  1877. continue;
  1878. DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
  1879. for (i = 0; i < FCU_FAN_COUNT; i++) {
  1880. int fan_id;
  1881. if (!fan_check_loc_match(loc, i))
  1882. continue;
  1883. DBG(" location match, index: %d\n", i);
  1884. fcu_fans[i].id = FCU_FAN_ABSENT_ID;
  1885. if (type != fcu_fans[i].type) {
  1886. printk(KERN_WARNING "therm_pm72: Fan type mismatch "
  1887. "in device-tree for %s\n", np->full_name);
  1888. break;
  1889. }
  1890. if (type == FCU_FAN_RPM)
  1891. fan_id = ((*reg) - 0x10) / 2;
  1892. else
  1893. fan_id = ((*reg) - 0x30) / 2;
  1894. if (fan_id > 7) {
  1895. printk(KERN_WARNING "therm_pm72: Can't parse "
  1896. "fan ID in device-tree for %s\n", np->full_name);
  1897. break;
  1898. }
  1899. DBG(" fan id -> %d, type -> %d\n", fan_id, type);
  1900. fcu_fans[i].id = fan_id;
  1901. }
  1902. }
  1903. /* Now dump the array */
  1904. printk(KERN_INFO "Detected fan controls:\n");
  1905. for (i = 0; i < FCU_FAN_COUNT; i++) {
  1906. if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
  1907. continue;
  1908. printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
  1909. fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
  1910. fcu_fans[i].id, fcu_fans[i].loc);
  1911. }
  1912. }
  1913. static int fcu_of_probe(struct platform_device* dev)
  1914. {
  1915. state = state_detached;
  1916. of_dev = dev;
  1917. dev_info(&dev->dev, "PowerMac G5 Thermal control driver %s\n", VERSION);
  1918. /* Lookup the fans in the device tree */
  1919. fcu_lookup_fans(dev->dev.of_node);
  1920. /* Add the driver */
  1921. return i2c_add_driver(&therm_pm72_driver);
  1922. }
  1923. static int fcu_of_remove(struct platform_device* dev)
  1924. {
  1925. i2c_del_driver(&therm_pm72_driver);
  1926. return 0;
  1927. }
  1928. static const struct of_device_id fcu_match[] =
  1929. {
  1930. {
  1931. .type = "fcu",
  1932. },
  1933. {},
  1934. };
  1935. MODULE_DEVICE_TABLE(of, fcu_match);
  1936. static struct platform_driver fcu_of_platform_driver =
  1937. {
  1938. .driver = {
  1939. .name = "temperature",
  1940. .owner = THIS_MODULE,
  1941. .of_match_table = fcu_match,
  1942. },
  1943. .probe = fcu_of_probe,
  1944. .remove = fcu_of_remove
  1945. };
  1946. /*
  1947. * Check machine type, attach to i2c controller
  1948. */
  1949. static int __init therm_pm72_init(void)
  1950. {
  1951. rackmac = of_machine_is_compatible("RackMac3,1");
  1952. if (!of_machine_is_compatible("PowerMac7,2") &&
  1953. !of_machine_is_compatible("PowerMac7,3") &&
  1954. !rackmac)
  1955. return -ENODEV;
  1956. return platform_driver_register(&fcu_of_platform_driver);
  1957. }
  1958. static void __exit therm_pm72_exit(void)
  1959. {
  1960. platform_driver_unregister(&fcu_of_platform_driver);
  1961. }
  1962. module_init(therm_pm72_init);
  1963. module_exit(therm_pm72_exit);
  1964. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  1965. MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
  1966. MODULE_LICENSE("GPL");