windfarm_fcu_controls.c 14 KB

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