windfarm_fcu_controls.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 using 0 for now which is what therm_pm72 used to do and
  40. * what Darwin -apparently- does based on observed behaviour.
  41. */
  42. #define RPM_PID_USE_ACTUAL_SPEED 0
  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. fan->target = value;
  137. buf[0] = value >> (8 - shift);
  138. buf[1] = value << shift;
  139. rc = wf_fcu_write_reg(pv, 0x10 + (fan->id * 2), buf, 2);
  140. if (rc < 0)
  141. return -EIO;
  142. return 0;
  143. }
  144. static int wf_fcu_fan_get_rpm(struct wf_control *ct, s32 *value)
  145. {
  146. struct wf_fcu_fan *fan = ct->priv;
  147. struct wf_fcu_priv *pv = fan->fcu_priv;
  148. int rc, reg_base, shift = pv->rpm_shift;
  149. unsigned char failure;
  150. unsigned char active;
  151. unsigned char buf[2];
  152. rc = wf_fcu_read_reg(pv, 0xb, &failure, 1);
  153. if (rc != 1)
  154. return -EIO;
  155. if ((failure & (1 << fan->id)) != 0)
  156. return -EFAULT;
  157. rc = wf_fcu_read_reg(pv, 0xd, &active, 1);
  158. if (rc != 1)
  159. return -EIO;
  160. if ((active & (1 << fan->id)) == 0)
  161. return -ENXIO;
  162. /* Programmed value or real current speed */
  163. #if RPM_PID_USE_ACTUAL_SPEED
  164. reg_base = 0x11;
  165. #else
  166. reg_base = 0x10;
  167. #endif
  168. rc = wf_fcu_read_reg(pv, reg_base + (fan->id * 2), buf, 2);
  169. if (rc != 2)
  170. return -EIO;
  171. *value = (buf[0] << (8 - shift)) | buf[1] >> shift;
  172. return 0;
  173. }
  174. static int wf_fcu_fan_set_pwm(struct wf_control *ct, s32 value)
  175. {
  176. struct wf_fcu_fan *fan = ct->priv;
  177. struct wf_fcu_priv *pv = fan->fcu_priv;
  178. unsigned char buf[2];
  179. int rc;
  180. if (value < fan->min)
  181. value = fan->min;
  182. if (value > fan->max)
  183. value = fan->max;
  184. fan->target = value;
  185. value = (value * 2559) / 1000;
  186. buf[0] = value;
  187. rc = wf_fcu_write_reg(pv, 0x30 + (fan->id * 2), buf, 1);
  188. if (rc < 0)
  189. return -EIO;
  190. return 0;
  191. }
  192. static int wf_fcu_fan_get_pwm(struct wf_control *ct, s32 *value)
  193. {
  194. struct wf_fcu_fan *fan = ct->priv;
  195. struct wf_fcu_priv *pv = fan->fcu_priv;
  196. unsigned char failure;
  197. unsigned char active;
  198. unsigned char buf[2];
  199. int rc;
  200. rc = wf_fcu_read_reg(pv, 0x2b, &failure, 1);
  201. if (rc != 1)
  202. return -EIO;
  203. if ((failure & (1 << fan->id)) != 0)
  204. return -EFAULT;
  205. rc = wf_fcu_read_reg(pv, 0x2d, &active, 1);
  206. if (rc != 1)
  207. return -EIO;
  208. if ((active & (1 << fan->id)) == 0)
  209. return -ENXIO;
  210. rc = wf_fcu_read_reg(pv, 0x30 + (fan->id * 2), buf, 1);
  211. if (rc != 1)
  212. return -EIO;
  213. *value = (((s32)buf[0]) * 1000) / 2559;
  214. return 0;
  215. }
  216. static s32 wf_fcu_fan_min(struct wf_control *ct)
  217. {
  218. struct wf_fcu_fan *fan = ct->priv;
  219. return fan->min;
  220. }
  221. static s32 wf_fcu_fan_max(struct wf_control *ct)
  222. {
  223. struct wf_fcu_fan *fan = ct->priv;
  224. return fan->max;
  225. }
  226. static const struct wf_control_ops wf_fcu_fan_rpm_ops = {
  227. .set_value = wf_fcu_fan_set_rpm,
  228. .get_value = wf_fcu_fan_get_rpm,
  229. .get_min = wf_fcu_fan_min,
  230. .get_max = wf_fcu_fan_max,
  231. .release = wf_fcu_fan_release,
  232. .owner = THIS_MODULE,
  233. };
  234. static const struct wf_control_ops wf_fcu_fan_pwm_ops = {
  235. .set_value = wf_fcu_fan_set_pwm,
  236. .get_value = wf_fcu_fan_get_pwm,
  237. .get_min = wf_fcu_fan_min,
  238. .get_max = wf_fcu_fan_max,
  239. .release = wf_fcu_fan_release,
  240. .owner = THIS_MODULE,
  241. };
  242. static void wf_fcu_get_pump_minmax(struct wf_fcu_fan *fan)
  243. {
  244. const struct mpu_data *mpu = wf_get_mpu(0);
  245. u16 pump_min = 0, pump_max = 0xffff;
  246. u16 tmp[4];
  247. /* Try to fetch pumps min/max infos from eeprom */
  248. if (mpu) {
  249. memcpy(&tmp, mpu->processor_part_num, 8);
  250. if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
  251. pump_min = max(pump_min, tmp[0]);
  252. pump_max = min(pump_max, tmp[1]);
  253. }
  254. if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
  255. pump_min = max(pump_min, tmp[2]);
  256. pump_max = min(pump_max, tmp[3]);
  257. }
  258. }
  259. /* Double check the values, this _IS_ needed as the EEPROM on
  260. * some dual 2.5Ghz G5s seem, at least, to have both min & max
  261. * same to the same value ... (grrrr)
  262. */
  263. if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
  264. pump_min = CPU_PUMP_OUTPUT_MIN;
  265. pump_max = CPU_PUMP_OUTPUT_MAX;
  266. }
  267. fan->min = pump_min;
  268. fan->max = pump_max;
  269. DBG("wf_fcu: pump min/max for %s set to: [%d..%d] RPM\n",
  270. fan->ctrl.name, pump_min, pump_max);
  271. }
  272. static void wf_fcu_get_rpmfan_minmax(struct wf_fcu_fan *fan)
  273. {
  274. struct wf_fcu_priv *pv = fan->fcu_priv;
  275. const struct mpu_data *mpu0 = wf_get_mpu(0);
  276. const struct mpu_data *mpu1 = wf_get_mpu(1);
  277. /* Default */
  278. fan->min = 2400 >> pv->rpm_shift;
  279. fan->max = 56000 >> pv->rpm_shift;
  280. /* CPU fans have min/max in MPU */
  281. if (mpu0 && !strcmp(fan->ctrl.name, "cpu-front-fan-0")) {
  282. fan->min = max(fan->min, (s32)mpu0->rminn_intake_fan);
  283. fan->max = min(fan->max, (s32)mpu0->rmaxn_intake_fan);
  284. goto bail;
  285. }
  286. if (mpu1 && !strcmp(fan->ctrl.name, "cpu-front-fan-1")) {
  287. fan->min = max(fan->min, (s32)mpu1->rminn_intake_fan);
  288. fan->max = min(fan->max, (s32)mpu1->rmaxn_intake_fan);
  289. goto bail;
  290. }
  291. if (mpu0 && !strcmp(fan->ctrl.name, "cpu-rear-fan-0")) {
  292. fan->min = max(fan->min, (s32)mpu0->rminn_exhaust_fan);
  293. fan->max = min(fan->max, (s32)mpu0->rmaxn_exhaust_fan);
  294. goto bail;
  295. }
  296. if (mpu1 && !strcmp(fan->ctrl.name, "cpu-rear-fan-1")) {
  297. fan->min = max(fan->min, (s32)mpu1->rminn_exhaust_fan);
  298. fan->max = min(fan->max, (s32)mpu1->rmaxn_exhaust_fan);
  299. goto bail;
  300. }
  301. /* Rackmac variants, we just use mpu0 intake */
  302. if (!strncmp(fan->ctrl.name, "cpu-fan", 7)) {
  303. fan->min = max(fan->min, (s32)mpu0->rminn_intake_fan);
  304. fan->max = min(fan->max, (s32)mpu0->rmaxn_intake_fan);
  305. goto bail;
  306. }
  307. bail:
  308. DBG("wf_fcu: fan min/max for %s set to: [%d..%d] RPM\n",
  309. fan->ctrl.name, fan->min, fan->max);
  310. }
  311. static void wf_fcu_add_fan(struct wf_fcu_priv *pv, const char *name,
  312. int type, int id)
  313. {
  314. struct wf_fcu_fan *fan;
  315. fan = kzalloc(sizeof(*fan), GFP_KERNEL);
  316. if (!fan)
  317. return;
  318. fan->fcu_priv = pv;
  319. fan->id = id;
  320. fan->ctrl.name = name;
  321. fan->ctrl.priv = fan;
  322. /* min/max is oddball but the code comes from
  323. * therm_pm72 which seems to work so ...
  324. */
  325. if (type == FCU_FAN_RPM) {
  326. if (!strncmp(name, "cpu-pump", strlen("cpu-pump")))
  327. wf_fcu_get_pump_minmax(fan);
  328. else
  329. wf_fcu_get_rpmfan_minmax(fan);
  330. fan->ctrl.type = WF_CONTROL_RPM_FAN;
  331. fan->ctrl.ops = &wf_fcu_fan_rpm_ops;
  332. } else {
  333. fan->min = 10;
  334. fan->max = 100;
  335. fan->ctrl.type = WF_CONTROL_PWM_FAN;
  336. fan->ctrl.ops = &wf_fcu_fan_pwm_ops;
  337. }
  338. if (wf_register_control(&fan->ctrl)) {
  339. pr_err("wf_fcu: Failed to register fan %s\n", name);
  340. kfree(fan);
  341. return;
  342. }
  343. list_add(&fan->link, &pv->fan_list);
  344. kref_get(&pv->ref);
  345. }
  346. static void wf_fcu_lookup_fans(struct wf_fcu_priv *pv)
  347. {
  348. /* Translation of device-tree location properties to
  349. * windfarm fan names
  350. */
  351. static const struct {
  352. const char *dt_name; /* Device-tree name */
  353. const char *ct_name; /* Control name */
  354. } loc_trans[] = {
  355. { "BACKSIDE", "backside-fan", },
  356. { "SYS CTRLR FAN", "backside-fan", },
  357. { "DRIVE BAY", "drive-bay-fan", },
  358. { "SLOT", "slots-fan", },
  359. { "PCI FAN", "slots-fan", },
  360. { "CPU A INTAKE", "cpu-front-fan-0", },
  361. { "CPU A EXHAUST", "cpu-rear-fan-0", },
  362. { "CPU B INTAKE", "cpu-front-fan-1", },
  363. { "CPU B EXHAUST", "cpu-rear-fan-1", },
  364. { "CPU A PUMP", "cpu-pump-0", },
  365. { "CPU B PUMP", "cpu-pump-1", },
  366. { "CPU A 1", "cpu-fan-a-0", },
  367. { "CPU A 2", "cpu-fan-b-0", },
  368. { "CPU A 3", "cpu-fan-c-0", },
  369. { "CPU B 1", "cpu-fan-a-1", },
  370. { "CPU B 2", "cpu-fan-b-1", },
  371. { "CPU B 3", "cpu-fan-c-1", },
  372. };
  373. struct device_node *np = NULL, *fcu = pv->i2c->dev.of_node;
  374. int i;
  375. DBG("Looking up FCU controls in device-tree...\n");
  376. while ((np = of_get_next_child(fcu, np)) != NULL) {
  377. int id, type = -1;
  378. const char *loc;
  379. const char *name;
  380. const u32 *reg;
  381. DBG(" control: %s, type: %s\n", np->name, np->type);
  382. /* Detect control type */
  383. if (!strcmp(np->type, "fan-rpm-control") ||
  384. !strcmp(np->type, "fan-rpm"))
  385. type = FCU_FAN_RPM;
  386. if (!strcmp(np->type, "fan-pwm-control") ||
  387. !strcmp(np->type, "fan-pwm"))
  388. type = FCU_FAN_PWM;
  389. /* Only care about fans for now */
  390. if (type == -1)
  391. continue;
  392. /* Lookup for a matching location */
  393. loc = of_get_property(np, "location", NULL);
  394. reg = of_get_property(np, "reg", NULL);
  395. if (loc == NULL || reg == NULL)
  396. continue;
  397. DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
  398. for (i = 0; i < ARRAY_SIZE(loc_trans); i++) {
  399. if (strncmp(loc, loc_trans[i].dt_name,
  400. strlen(loc_trans[i].dt_name)))
  401. continue;
  402. name = loc_trans[i].ct_name;
  403. DBG(" location match, name: %s\n", name);
  404. if (type == FCU_FAN_RPM)
  405. id = ((*reg) - 0x10) / 2;
  406. else
  407. id = ((*reg) - 0x30) / 2;
  408. if (id > 7) {
  409. pr_warning("wf_fcu: Can't parse "
  410. "fan ID in device-tree for %s\n",
  411. np->full_name);
  412. break;
  413. }
  414. wf_fcu_add_fan(pv, name, type, id);
  415. break;
  416. }
  417. }
  418. }
  419. static void wf_fcu_default_fans(struct wf_fcu_priv *pv)
  420. {
  421. /* We only support the default fans for PowerMac7,2 */
  422. if (!of_machine_is_compatible("PowerMac7,2"))
  423. return;
  424. wf_fcu_add_fan(pv, "backside-fan", FCU_FAN_PWM, 1);
  425. wf_fcu_add_fan(pv, "drive-bay-fan", FCU_FAN_RPM, 2);
  426. wf_fcu_add_fan(pv, "slots-fan", FCU_FAN_PWM, 2);
  427. wf_fcu_add_fan(pv, "cpu-front-fan-0", FCU_FAN_RPM, 3);
  428. wf_fcu_add_fan(pv, "cpu-rear-fan-0", FCU_FAN_RPM, 4);
  429. wf_fcu_add_fan(pv, "cpu-front-fan-1", FCU_FAN_RPM, 5);
  430. wf_fcu_add_fan(pv, "cpu-rear-fan-1", FCU_FAN_RPM, 6);
  431. }
  432. static int wf_fcu_init_chip(struct wf_fcu_priv *pv)
  433. {
  434. unsigned char buf = 0xff;
  435. int rc;
  436. rc = wf_fcu_write_reg(pv, 0xe, &buf, 1);
  437. if (rc < 0)
  438. return -EIO;
  439. rc = wf_fcu_write_reg(pv, 0x2e, &buf, 1);
  440. if (rc < 0)
  441. return -EIO;
  442. rc = wf_fcu_read_reg(pv, 0, &buf, 1);
  443. if (rc < 0)
  444. return -EIO;
  445. pv->rpm_shift = (buf == 1) ? 2 : 3;
  446. pr_debug("wf_fcu: FCU Initialized, RPM fan shift is %d\n",
  447. pv->rpm_shift);
  448. return 0;
  449. }
  450. static int wf_fcu_probe(struct i2c_client *client,
  451. const struct i2c_device_id *id)
  452. {
  453. struct wf_fcu_priv *pv;
  454. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  455. if (!pv)
  456. return -ENOMEM;
  457. kref_init(&pv->ref);
  458. mutex_init(&pv->lock);
  459. INIT_LIST_HEAD(&pv->fan_list);
  460. pv->i2c = client;
  461. /*
  462. * First we must start the FCU which will query the
  463. * shift value to apply to RPMs
  464. */
  465. if (wf_fcu_init_chip(pv)) {
  466. pr_err("wf_fcu: Initialization failed !\n");
  467. kfree(pv);
  468. return -ENXIO;
  469. }
  470. /* First lookup fans in the device-tree */
  471. wf_fcu_lookup_fans(pv);
  472. /*
  473. * Older machines don't have the device-tree entries
  474. * we are looking for, just hard code the list
  475. */
  476. if (list_empty(&pv->fan_list))
  477. wf_fcu_default_fans(pv);
  478. /* Still no fans ? FAIL */
  479. if (list_empty(&pv->fan_list)) {
  480. pr_err("wf_fcu: Failed to find fans for your machine\n");
  481. kfree(pv);
  482. return -ENODEV;
  483. }
  484. dev_set_drvdata(&client->dev, pv);
  485. return 0;
  486. }
  487. static int wf_fcu_remove(struct i2c_client *client)
  488. {
  489. struct wf_fcu_priv *pv = dev_get_drvdata(&client->dev);
  490. struct wf_fcu_fan *fan;
  491. while (!list_empty(&pv->fan_list)) {
  492. fan = list_first_entry(&pv->fan_list, struct wf_fcu_fan, link);
  493. list_del(&fan->link);
  494. wf_unregister_control(&fan->ctrl);
  495. }
  496. kref_put(&pv->ref, wf_fcu_release);
  497. return 0;
  498. }
  499. static const struct i2c_device_id wf_fcu_id[] = {
  500. { "MAC,fcu", 0 },
  501. { }
  502. };
  503. MODULE_DEVICE_TABLE(i2c, wf_fcu_id);
  504. static struct i2c_driver wf_fcu_driver = {
  505. .driver = {
  506. .name = "wf_fcu",
  507. },
  508. .probe = wf_fcu_probe,
  509. .remove = wf_fcu_remove,
  510. .id_table = wf_fcu_id,
  511. };
  512. module_i2c_driver(wf_fcu_driver);
  513. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  514. MODULE_DESCRIPTION("FCU control objects for PowerMacs thermal control");
  515. MODULE_LICENSE("GPL");