windfarm_fcu_controls.c 14 KB

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