windfarm_fcu_controls.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Windfarm PowerMac thermal control. FCU fan control
  3. *
  4. * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
  5. *
  6. * Released under the term of the GNU GPL v2.
  7. */
  8. #undef DEBUG
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/wait.h>
  16. #include <linux/i2c.h>
  17. #include <asm/prom.h>
  18. #include <asm/machdep.h>
  19. #include <asm/io.h>
  20. #include <asm/sections.h>
  21. #include "windfarm.h"
  22. #include "windfarm_mpu.h"
  23. #define VERSION "1.0"
  24. #ifdef DEBUG
  25. #define DBG(args...) printk(args)
  26. #else
  27. #define DBG(args...) do { } while(0)
  28. #endif
  29. /*
  30. * This option is "weird" :) Basically, if you define this to 1
  31. * the control loop for the RPMs fans (not PWMs) will apply the
  32. * correction factor obtained from the PID to the actual RPM
  33. * speed read from the FCU.
  34. *
  35. * If you define the below constant to 0, then it will be
  36. * applied to the setpoint RPM speed, that is basically the
  37. * speed we proviously "asked" for.
  38. *
  39. * I'm not sure which of these Apple's algorithm is supposed
  40. * to use
  41. */
  42. #define RPM_PID_USE_ACTUAL_SPEED 1
  43. /* Default min/max for pumps */
  44. #define CPU_PUMP_OUTPUT_MAX 3200
  45. #define CPU_PUMP_OUTPUT_MIN 1250
  46. #define FCU_FAN_RPM 0
  47. #define FCU_FAN_PWM 1
  48. struct wf_fcu_priv {
  49. struct kref ref;
  50. struct i2c_client *i2c;
  51. struct mutex lock;
  52. struct list_head fan_list;
  53. int rpm_shift;
  54. };
  55. struct wf_fcu_fan {
  56. struct list_head link;
  57. int id;
  58. s32 min, max, target;
  59. struct wf_fcu_priv *fcu_priv;
  60. struct wf_control ctrl;
  61. };
  62. static void wf_fcu_release(struct kref *ref)
  63. {
  64. struct wf_fcu_priv *pv = container_of(ref, struct wf_fcu_priv, ref);
  65. kfree(pv);
  66. }
  67. static void wf_fcu_fan_release(struct wf_control *ct)
  68. {
  69. struct wf_fcu_fan *fan = ct->priv;
  70. kref_put(&fan->fcu_priv->ref, wf_fcu_release);
  71. kfree(fan);
  72. }
  73. static int wf_fcu_read_reg(struct wf_fcu_priv *pv, int reg,
  74. unsigned char *buf, int nb)
  75. {
  76. int tries, nr, nw;
  77. mutex_lock(&pv->lock);
  78. buf[0] = reg;
  79. tries = 0;
  80. for (;;) {
  81. nw = i2c_master_send(pv->i2c, buf, 1);
  82. if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
  83. break;
  84. msleep(10);
  85. ++tries;
  86. }
  87. if (nw <= 0) {
  88. pr_err("Failure writing address to FCU: %d", nw);
  89. nr = nw;
  90. goto bail;
  91. }
  92. tries = 0;
  93. for (;;) {
  94. nr = i2c_master_recv(pv->i2c, buf, nb);
  95. if (nr > 0 || (nr < 0 && nr != -ENODEV) || tries >= 100)
  96. break;
  97. msleep(10);
  98. ++tries;
  99. }
  100. if (nr <= 0)
  101. pr_err("wf_fcu: Failure reading data from FCU: %d", nw);
  102. bail:
  103. mutex_unlock(&pv->lock);
  104. return nr;
  105. }
  106. static int wf_fcu_write_reg(struct wf_fcu_priv *pv, int reg,
  107. const unsigned char *ptr, int nb)
  108. {
  109. int tries, nw;
  110. unsigned char buf[16];
  111. buf[0] = reg;
  112. memcpy(buf+1, ptr, nb);
  113. ++nb;
  114. tries = 0;
  115. for (;;) {
  116. nw = i2c_master_send(pv->i2c, buf, nb);
  117. if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
  118. break;
  119. msleep(10);
  120. ++tries;
  121. }
  122. if (nw < 0)
  123. pr_err("wf_fcu: Failure writing to FCU: %d", nw);
  124. return nw;
  125. }
  126. static int wf_fcu_fan_set_rpm(struct wf_control *ct, s32 value)
  127. {
  128. struct wf_fcu_fan *fan = ct->priv;
  129. struct wf_fcu_priv *pv = fan->fcu_priv;
  130. int rc, shift = pv->rpm_shift;
  131. unsigned char buf[2];
  132. if (value < fan->min)
  133. value = fan->min;
  134. if (value > fan->max)
  135. value = fan->max;
  136. if (fan->target && fan->target == value)
  137. return 0;
  138. fan->target = value;
  139. buf[0] = value >> (8 - shift);
  140. buf[1] = value << shift;
  141. rc = wf_fcu_write_reg(pv, 0x10 + (fan->id * 2), buf, 2);
  142. if (rc < 0)
  143. return -EIO;
  144. return 0;
  145. }
  146. static int wf_fcu_fan_get_rpm(struct wf_control *ct, s32 *value)
  147. {
  148. struct wf_fcu_fan *fan = ct->priv;
  149. struct wf_fcu_priv *pv = fan->fcu_priv;
  150. int rc, reg_base, shift = pv->rpm_shift;
  151. unsigned char failure;
  152. unsigned char active;
  153. unsigned char buf[2];
  154. rc = wf_fcu_read_reg(pv, 0xb, &failure, 1);
  155. if (rc != 1)
  156. return -EIO;
  157. if ((failure & (1 << fan->id)) != 0)
  158. return -EFAULT;
  159. rc = wf_fcu_read_reg(pv, 0xd, &active, 1);
  160. if (rc != 1)
  161. return -EIO;
  162. if ((active & (1 << fan->id)) == 0)
  163. return -ENXIO;
  164. /* Programmed value or real current speed */
  165. #if RPM_PID_USE_ACTUAL_SPEED
  166. reg_base = 0x11;
  167. #else
  168. reg_base = 0x10;
  169. #endif
  170. rc = wf_fcu_read_reg(pv, reg_base + (fan->id * 2), buf, 2);
  171. if (rc != 2)
  172. return -EIO;
  173. *value = (buf[0] << (8 - shift)) | buf[1] >> shift;
  174. return 0;
  175. }
  176. static int wf_fcu_fan_set_pwm(struct wf_control *ct, s32 value)
  177. {
  178. struct wf_fcu_fan *fan = ct->priv;
  179. struct wf_fcu_priv *pv = fan->fcu_priv;
  180. unsigned char buf[2];
  181. int rc;
  182. if (value < fan->min)
  183. value = fan->min;
  184. if (value > fan->max)
  185. value = fan->max;
  186. if (fan->target && fan->target == value)
  187. return 0;
  188. fan->target = value;
  189. value = (value * 2559) / 1000;
  190. buf[0] = value;
  191. rc = wf_fcu_write_reg(pv, 0x30 + (fan->id * 2), buf, 1);
  192. if (rc < 0)
  193. return -EIO;
  194. return 0;
  195. }
  196. static int wf_fcu_fan_get_pwm(struct wf_control *ct, s32 *value)
  197. {
  198. struct wf_fcu_fan *fan = ct->priv;
  199. struct wf_fcu_priv *pv = fan->fcu_priv;
  200. unsigned char failure;
  201. unsigned char active;
  202. unsigned char buf[2];
  203. int rc;
  204. rc = wf_fcu_read_reg(pv, 0x2b, &failure, 1);
  205. if (rc != 1)
  206. return -EIO;
  207. if ((failure & (1 << fan->id)) != 0)
  208. return -EFAULT;
  209. rc = wf_fcu_read_reg(pv, 0x2d, &active, 1);
  210. if (rc != 1)
  211. return -EIO;
  212. if ((active & (1 << fan->id)) == 0)
  213. return -ENXIO;
  214. rc = wf_fcu_read_reg(pv, 0x30 + (fan->id * 2), buf, 1);
  215. if (rc != 1)
  216. return -EIO;
  217. *value = (((s32)buf[0]) * 1000) / 2559;
  218. return 0;
  219. }
  220. static s32 wf_fcu_fan_min(struct wf_control *ct)
  221. {
  222. struct wf_fcu_fan *fan = ct->priv;
  223. return fan->min;
  224. }
  225. static s32 wf_fcu_fan_max(struct wf_control *ct)
  226. {
  227. struct wf_fcu_fan *fan = ct->priv;
  228. return fan->max;
  229. }
  230. static const struct wf_control_ops wf_fcu_fan_rpm_ops = {
  231. .set_value = wf_fcu_fan_set_rpm,
  232. .get_value = wf_fcu_fan_get_rpm,
  233. .get_min = wf_fcu_fan_min,
  234. .get_max = wf_fcu_fan_max,
  235. .release = wf_fcu_fan_release,
  236. .owner = THIS_MODULE,
  237. };
  238. static const struct wf_control_ops wf_fcu_fan_pwm_ops = {
  239. .set_value = wf_fcu_fan_set_pwm,
  240. .get_value = wf_fcu_fan_get_pwm,
  241. .get_min = wf_fcu_fan_min,
  242. .get_max = wf_fcu_fan_max,
  243. .release = wf_fcu_fan_release,
  244. .owner = THIS_MODULE,
  245. };
  246. static void __devinit wf_fcu_get_pump_minmax(struct wf_fcu_fan *fan)
  247. {
  248. const struct mpu_data *mpu = wf_get_mpu(0);
  249. u16 pump_min = 0, pump_max = 0xffff;
  250. u16 tmp[4];
  251. /* Try to fetch pumps min/max infos from eeprom */
  252. if (mpu) {
  253. memcpy(&tmp, mpu->processor_part_num, 8);
  254. if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
  255. pump_min = max(pump_min, tmp[0]);
  256. pump_max = min(pump_max, tmp[1]);
  257. }
  258. if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
  259. pump_min = max(pump_min, tmp[2]);
  260. pump_max = min(pump_max, tmp[3]);
  261. }
  262. }
  263. /* Double check the values, this _IS_ needed as the EEPROM on
  264. * some dual 2.5Ghz G5s seem, at least, to have both min & max
  265. * same to the same value ... (grrrr)
  266. */
  267. if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
  268. pump_min = CPU_PUMP_OUTPUT_MIN;
  269. pump_max = CPU_PUMP_OUTPUT_MAX;
  270. }
  271. fan->min = pump_min;
  272. fan->max = pump_max;
  273. DBG("wf_fcu: pump min/max for %s set to: [%d..%d] RPM\n",
  274. fan->ctrl.name, pump_min, pump_max);
  275. }
  276. static void __devinit wf_fcu_get_rpmfan_minmax(struct wf_fcu_fan *fan)
  277. {
  278. struct wf_fcu_priv *pv = fan->fcu_priv;
  279. const struct mpu_data *mpu0 = wf_get_mpu(0);
  280. const struct mpu_data *mpu1 = wf_get_mpu(1);
  281. /* Default */
  282. fan->min = 2400 >> pv->rpm_shift;
  283. fan->max = 56000 >> pv->rpm_shift;
  284. /* CPU fans have min/max in MPU */
  285. if (mpu0 && !strcmp(fan->ctrl.name, "cpu-front-fan-0")) {
  286. fan->min = max(fan->min, (s32)mpu0->rminn_intake_fan);
  287. fan->max = min(fan->max, (s32)mpu0->rmaxn_intake_fan);
  288. goto bail;
  289. }
  290. if (mpu1 && !strcmp(fan->ctrl.name, "cpu-front-fan-1")) {
  291. fan->min = max(fan->min, (s32)mpu1->rminn_intake_fan);
  292. fan->max = min(fan->max, (s32)mpu1->rmaxn_intake_fan);
  293. goto bail;
  294. }
  295. if (mpu0 && !strcmp(fan->ctrl.name, "cpu-rear-fan-0")) {
  296. fan->min = max(fan->min, (s32)mpu0->rminn_exhaust_fan);
  297. fan->max = min(fan->max, (s32)mpu0->rmaxn_exhaust_fan);
  298. goto bail;
  299. }
  300. if (mpu1 && !strcmp(fan->ctrl.name, "cpu-rear-fan-1")) {
  301. fan->min = max(fan->min, (s32)mpu1->rminn_exhaust_fan);
  302. fan->max = min(fan->max, (s32)mpu1->rmaxn_exhaust_fan);
  303. goto bail;
  304. }
  305. /* Rackmac variants, we just use mpu0 intake */
  306. if (!strncmp(fan->ctrl.name, "cpu-fan", 7)) {
  307. fan->min = max(fan->min, (s32)mpu0->rminn_intake_fan);
  308. fan->max = min(fan->max, (s32)mpu0->rmaxn_intake_fan);
  309. goto bail;
  310. }
  311. bail:
  312. DBG("wf_fcu: fan min/max for %s set to: [%d..%d] RPM\n",
  313. fan->ctrl.name, fan->min, fan->max);
  314. }
  315. static void __devinit wf_fcu_add_fan(struct wf_fcu_priv *pv,
  316. const char *name,
  317. int type, int id)
  318. {
  319. struct wf_fcu_fan *fan;
  320. fan = kzalloc(sizeof(*fan), GFP_KERNEL);
  321. if (!fan)
  322. return;
  323. fan->fcu_priv = pv;
  324. fan->id = id;
  325. fan->ctrl.name = name;
  326. fan->ctrl.priv = fan;
  327. /* min/max is oddball but the code comes from
  328. * therm_pm72 which seems to work so ...
  329. */
  330. if (type == FCU_FAN_RPM) {
  331. if (!strncmp(name, "cpu-pump", strlen("cpu-pump")))
  332. wf_fcu_get_pump_minmax(fan);
  333. else
  334. wf_fcu_get_rpmfan_minmax(fan);
  335. fan->ctrl.type = WF_CONTROL_RPM_FAN;
  336. fan->ctrl.ops = &wf_fcu_fan_rpm_ops;
  337. } else {
  338. fan->min = 10;
  339. fan->max = 100;
  340. fan->ctrl.type = WF_CONTROL_PWM_FAN;
  341. fan->ctrl.ops = &wf_fcu_fan_pwm_ops;
  342. }
  343. if (wf_register_control(&fan->ctrl)) {
  344. pr_err("wf_fcu: Failed to register fan %s\n", name);
  345. kfree(fan);
  346. return;
  347. }
  348. list_add(&fan->link, &pv->fan_list);
  349. kref_get(&pv->ref);
  350. }
  351. static void __devinit wf_fcu_lookup_fans(struct wf_fcu_priv *pv)
  352. {
  353. /* Translation of device-tree location properties to
  354. * windfarm fan names
  355. */
  356. static const struct {
  357. const char *dt_name; /* Device-tree name */
  358. const char *ct_name; /* Control name */
  359. } loc_trans[] = {
  360. { "BACKSIDE", "backside-fan", },
  361. { "SYS CTRLR FAN", "backside-fan", },
  362. { "DRIVE BAY", "drive-bay-fan", },
  363. { "SLOT", "slots-fan", },
  364. { "PCI FAN", "slots-fan", },
  365. { "CPU A INTAKE", "cpu-front-fan-0", },
  366. { "CPU A EXHAUST", "cpu-rear-fan-0", },
  367. { "CPU B INTAKE", "cpu-front-fan-1", },
  368. { "CPU B EXHAUST", "cpu-rear-fan-1", },
  369. { "CPU A PUMP", "cpu-pump-0", },
  370. { "CPU B PUMP", "cpu-pump-1", },
  371. { "CPU A 1", "cpu-fan-a-0", },
  372. { "CPU A 2", "cpu-fan-b-0", },
  373. { "CPU A 3", "cpu-fan-c-0", },
  374. { "CPU B 1", "cpu-fan-a-1", },
  375. { "CPU B 2", "cpu-fan-b-1", },
  376. { "CPU B 3", "cpu-fan-c-1", },
  377. };
  378. struct device_node *np = NULL, *fcu = pv->i2c->dev.of_node;
  379. int i;
  380. DBG("Looking up FCU controls in device-tree...\n");
  381. while ((np = of_get_next_child(fcu, np)) != NULL) {
  382. int id, type = -1;
  383. const char *loc;
  384. const char *name;
  385. const u32 *reg;
  386. DBG(" control: %s, type: %s\n", np->name, np->type);
  387. /* Detect control type */
  388. if (!strcmp(np->type, "fan-rpm-control") ||
  389. !strcmp(np->type, "fan-rpm"))
  390. type = FCU_FAN_RPM;
  391. if (!strcmp(np->type, "fan-pwm-control") ||
  392. !strcmp(np->type, "fan-pwm"))
  393. type = FCU_FAN_PWM;
  394. /* Only care about fans for now */
  395. if (type == -1)
  396. continue;
  397. /* Lookup for a matching location */
  398. loc = of_get_property(np, "location", NULL);
  399. reg = of_get_property(np, "reg", NULL);
  400. if (loc == NULL || reg == NULL)
  401. continue;
  402. DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
  403. for (i = 0; i < ARRAY_SIZE(loc_trans); i++) {
  404. if (strncmp(loc, loc_trans[i].dt_name,
  405. strlen(loc_trans[i].dt_name)))
  406. continue;
  407. name = loc_trans[i].ct_name;
  408. DBG(" location match, name: %s\n", name);
  409. if (type == FCU_FAN_RPM)
  410. id = ((*reg) - 0x10) / 2;
  411. else
  412. id = ((*reg) - 0x30) / 2;
  413. if (id > 7) {
  414. pr_warning("wf_fcu: Can't parse "
  415. "fan ID in device-tree for %s\n",
  416. np->full_name);
  417. break;
  418. }
  419. wf_fcu_add_fan(pv, name, type, id);
  420. break;
  421. }
  422. }
  423. }
  424. static void __devinit wf_fcu_default_fans(struct wf_fcu_priv *pv)
  425. {
  426. /* We only support the default fans for PowerMac7,2 */
  427. if (!of_machine_is_compatible("PowerMac7,2"))
  428. return;
  429. wf_fcu_add_fan(pv, "backside-fan", FCU_FAN_PWM, 1);
  430. wf_fcu_add_fan(pv, "drive-bay-fan", FCU_FAN_RPM, 2);
  431. wf_fcu_add_fan(pv, "slots-fan", FCU_FAN_PWM, 2);
  432. wf_fcu_add_fan(pv, "cpu-front-fan-0", FCU_FAN_RPM, 3);
  433. wf_fcu_add_fan(pv, "cpu-rear-fan-0", FCU_FAN_RPM, 4);
  434. wf_fcu_add_fan(pv, "cpu-front-fan-1", FCU_FAN_RPM, 5);
  435. wf_fcu_add_fan(pv, "cpu-rear-fan-1", FCU_FAN_RPM, 6);
  436. }
  437. static int __devinit wf_fcu_init_chip(struct wf_fcu_priv *pv)
  438. {
  439. unsigned char buf = 0xff;
  440. int rc;
  441. rc = wf_fcu_write_reg(pv, 0xe, &buf, 1);
  442. if (rc < 0)
  443. return -EIO;
  444. rc = wf_fcu_write_reg(pv, 0x2e, &buf, 1);
  445. if (rc < 0)
  446. return -EIO;
  447. rc = wf_fcu_read_reg(pv, 0, &buf, 1);
  448. if (rc < 0)
  449. return -EIO;
  450. pv->rpm_shift = (buf == 1) ? 2 : 3;
  451. pr_debug("wf_fcu: FCU Initialized, RPM fan shift is %d\n",
  452. pv->rpm_shift);
  453. return 0;
  454. }
  455. static int __devinit wf_fcu_probe(struct i2c_client *client,
  456. const struct i2c_device_id *id)
  457. {
  458. struct wf_fcu_priv *pv;
  459. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  460. if (!pv)
  461. return -ENOMEM;
  462. kref_init(&pv->ref);
  463. mutex_init(&pv->lock);
  464. INIT_LIST_HEAD(&pv->fan_list);
  465. pv->i2c = client;
  466. /*
  467. * First we must start the FCU which will query the
  468. * shift value to apply to RPMs
  469. */
  470. if (wf_fcu_init_chip(pv)) {
  471. pr_err("wf_fcu: Initialization failed !\n");
  472. kfree(pv);
  473. return -ENXIO;
  474. }
  475. /* First lookup fans in the device-tree */
  476. wf_fcu_lookup_fans(pv);
  477. /*
  478. * Older machines don't have the device-tree entries
  479. * we are looking for, just hard code the list
  480. */
  481. if (list_empty(&pv->fan_list))
  482. wf_fcu_default_fans(pv);
  483. /* Still no fans ? FAIL */
  484. if (list_empty(&pv->fan_list)) {
  485. pr_err("wf_fcu: Failed to find fans for your machine\n");
  486. kfree(pv);
  487. return -ENODEV;
  488. }
  489. dev_set_drvdata(&client->dev, pv);
  490. return 0;
  491. }
  492. static int __devexit wf_fcu_remove(struct i2c_client *client)
  493. {
  494. struct wf_fcu_priv *pv = dev_get_drvdata(&client->dev);
  495. struct wf_fcu_fan *fan;
  496. while (!list_empty(&pv->fan_list)) {
  497. fan = list_first_entry(&pv->fan_list, struct wf_fcu_fan, link);
  498. list_del(&fan->link);
  499. wf_unregister_control(&fan->ctrl);
  500. }
  501. kref_put(&pv->ref, wf_fcu_release);
  502. return 0;
  503. }
  504. static const struct i2c_device_id wf_fcu_id[] = {
  505. { "MAC,fcu", 0 },
  506. { }
  507. };
  508. MODULE_DEVICE_TABLE(i2c, wf_fcu_id);
  509. static struct i2c_driver wf_fcu_driver = {
  510. .driver = {
  511. .name = "wf_fcu",
  512. },
  513. .probe = wf_fcu_probe,
  514. .remove = wf_fcu_remove,
  515. .id_table = wf_fcu_id,
  516. };
  517. static int __init wf_fcu_init(void)
  518. {
  519. return i2c_add_driver(&wf_fcu_driver);
  520. }
  521. static void __exit wf_fcu_exit(void)
  522. {
  523. i2c_del_driver(&wf_fcu_driver);
  524. }
  525. module_init(wf_fcu_init);
  526. module_exit(wf_fcu_exit);
  527. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  528. MODULE_DESCRIPTION("FCU control objects for PowerMacs thermal control");
  529. MODULE_LICENSE("GPL");