windfarm_smu_controls.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. SMU based controls
  4. *
  5. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  6. * <[email protected]>
  7. */
  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/completion.h>
  16. #include <linux/of.h>
  17. #include <asm/machdep.h>
  18. #include <asm/io.h>
  19. #include <asm/sections.h>
  20. #include <asm/smu.h>
  21. #include "windfarm.h"
  22. #define VERSION "0.4"
  23. #undef DEBUG
  24. #ifdef DEBUG
  25. #define DBG(args...) printk(args)
  26. #else
  27. #define DBG(args...) do { } while(0)
  28. #endif
  29. static int smu_supports_new_fans_ops = 1;
  30. /*
  31. * SMU fans control object
  32. */
  33. static LIST_HEAD(smu_fans);
  34. struct smu_fan_control {
  35. struct list_head link;
  36. int fan_type; /* 0 = rpm, 1 = pwm */
  37. u32 reg; /* index in SMU */
  38. s32 value; /* current value */
  39. s32 min, max; /* min/max values */
  40. struct wf_control ctrl;
  41. };
  42. #define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)
  43. static int smu_set_fan(int pwm, u8 id, u16 value)
  44. {
  45. struct smu_cmd cmd;
  46. u8 buffer[16];
  47. DECLARE_COMPLETION_ONSTACK(comp);
  48. int rc;
  49. /* Fill SMU command structure */
  50. cmd.cmd = SMU_CMD_FAN_COMMAND;
  51. /* The SMU has an "old" and a "new" way of setting the fan speed
  52. * Unfortunately, I found no reliable way to know which one works
  53. * on a given machine model. After some investigations it appears
  54. * that MacOS X just tries the new one, and if it fails fallbacks
  55. * to the old ones ... Ugh.
  56. */
  57. retry:
  58. if (smu_supports_new_fans_ops) {
  59. buffer[0] = 0x30;
  60. buffer[1] = id;
  61. *((u16 *)(&buffer[2])) = value;
  62. cmd.data_len = 4;
  63. } else {
  64. if (id > 7)
  65. return -EINVAL;
  66. /* Fill argument buffer */
  67. memset(buffer, 0, 16);
  68. buffer[0] = pwm ? 0x10 : 0x00;
  69. buffer[1] = 0x01 << id;
  70. *((u16 *)&buffer[2 + id * 2]) = value;
  71. cmd.data_len = 14;
  72. }
  73. cmd.reply_len = 16;
  74. cmd.data_buf = cmd.reply_buf = buffer;
  75. cmd.status = 0;
  76. cmd.done = smu_done_complete;
  77. cmd.misc = &comp;
  78. rc = smu_queue_cmd(&cmd);
  79. if (rc)
  80. return rc;
  81. wait_for_completion(&comp);
  82. /* Handle fallback (see comment above) */
  83. if (cmd.status != 0 && smu_supports_new_fans_ops) {
  84. printk(KERN_WARNING "windfarm: SMU failed new fan command "
  85. "falling back to old method\n");
  86. smu_supports_new_fans_ops = 0;
  87. goto retry;
  88. }
  89. return cmd.status;
  90. }
  91. static void smu_fan_release(struct wf_control *ct)
  92. {
  93. struct smu_fan_control *fct = to_smu_fan(ct);
  94. kfree(fct);
  95. }
  96. static int smu_fan_set(struct wf_control *ct, s32 value)
  97. {
  98. struct smu_fan_control *fct = to_smu_fan(ct);
  99. if (value < fct->min)
  100. value = fct->min;
  101. if (value > fct->max)
  102. value = fct->max;
  103. fct->value = value;
  104. return smu_set_fan(fct->fan_type, fct->reg, value);
  105. }
  106. static int smu_fan_get(struct wf_control *ct, s32 *value)
  107. {
  108. struct smu_fan_control *fct = to_smu_fan(ct);
  109. *value = fct->value; /* todo: read from SMU */
  110. return 0;
  111. }
  112. static s32 smu_fan_min(struct wf_control *ct)
  113. {
  114. struct smu_fan_control *fct = to_smu_fan(ct);
  115. return fct->min;
  116. }
  117. static s32 smu_fan_max(struct wf_control *ct)
  118. {
  119. struct smu_fan_control *fct = to_smu_fan(ct);
  120. return fct->max;
  121. }
  122. static const struct wf_control_ops smu_fan_ops = {
  123. .set_value = smu_fan_set,
  124. .get_value = smu_fan_get,
  125. .get_min = smu_fan_min,
  126. .get_max = smu_fan_max,
  127. .release = smu_fan_release,
  128. .owner = THIS_MODULE,
  129. };
  130. static struct smu_fan_control *smu_fan_create(struct device_node *node,
  131. int pwm_fan)
  132. {
  133. struct smu_fan_control *fct;
  134. const s32 *v;
  135. const u32 *reg;
  136. const char *l;
  137. fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);
  138. if (fct == NULL)
  139. return NULL;
  140. fct->ctrl.ops = &smu_fan_ops;
  141. l = of_get_property(node, "location", NULL);
  142. if (l == NULL)
  143. goto fail;
  144. fct->fan_type = pwm_fan;
  145. fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN;
  146. /* We use the name & location here the same way we do for SMU sensors,
  147. * see the comment in windfarm_smu_sensors.c. The locations are a bit
  148. * less consistent here between the iMac and the desktop models, but
  149. * that is good enough for our needs for now at least.
  150. *
  151. * One problem though is that Apple seem to be inconsistent with case
  152. * and the kernel doesn't have strcasecmp =P
  153. */
  154. fct->ctrl.name = NULL;
  155. /* Names used on desktop models */
  156. if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") ||
  157. !strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan") ||
  158. !strcmp(l, "CPU A EXHAUST"))
  159. fct->ctrl.name = "cpu-rear-fan-0";
  160. else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1") ||
  161. !strcmp(l, "CPU B EXHAUST"))
  162. fct->ctrl.name = "cpu-rear-fan-1";
  163. else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") ||
  164. !strcmp(l, "Front fan 0") || !strcmp(l, "Front fan") ||
  165. !strcmp(l, "CPU A INTAKE"))
  166. fct->ctrl.name = "cpu-front-fan-0";
  167. else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1") ||
  168. !strcmp(l, "CPU B INTAKE"))
  169. fct->ctrl.name = "cpu-front-fan-1";
  170. else if (!strcmp(l, "CPU A PUMP"))
  171. fct->ctrl.name = "cpu-pump-0";
  172. else if (!strcmp(l, "CPU B PUMP"))
  173. fct->ctrl.name = "cpu-pump-1";
  174. else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") ||
  175. !strcmp(l, "EXPANSION SLOTS INTAKE"))
  176. fct->ctrl.name = "slots-fan";
  177. else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") ||
  178. !strcmp(l, "DRIVE BAY A INTAKE"))
  179. fct->ctrl.name = "drive-bay-fan";
  180. else if (!strcmp(l, "BACKSIDE"))
  181. fct->ctrl.name = "backside-fan";
  182. /* Names used on iMac models */
  183. if (!strcmp(l, "System Fan") || !strcmp(l, "System fan"))
  184. fct->ctrl.name = "system-fan";
  185. else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan"))
  186. fct->ctrl.name = "cpu-fan";
  187. else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive"))
  188. fct->ctrl.name = "drive-bay-fan";
  189. else if (!strcmp(l, "HDD Fan")) /* seen on iMac G5 iSight */
  190. fct->ctrl.name = "hard-drive-fan";
  191. else if (!strcmp(l, "ODD Fan")) /* same */
  192. fct->ctrl.name = "optical-drive-fan";
  193. /* Unrecognized fan, bail out */
  194. if (fct->ctrl.name == NULL)
  195. goto fail;
  196. /* Get min & max values*/
  197. v = of_get_property(node, "min-value", NULL);
  198. if (v == NULL)
  199. goto fail;
  200. fct->min = *v;
  201. v = of_get_property(node, "max-value", NULL);
  202. if (v == NULL)
  203. goto fail;
  204. fct->max = *v;
  205. /* Get "reg" value */
  206. reg = of_get_property(node, "reg", NULL);
  207. if (reg == NULL)
  208. goto fail;
  209. fct->reg = *reg;
  210. if (wf_register_control(&fct->ctrl))
  211. goto fail;
  212. return fct;
  213. fail:
  214. kfree(fct);
  215. return NULL;
  216. }
  217. static int __init smu_controls_init(void)
  218. {
  219. struct device_node *smu, *fans, *fan;
  220. if (!smu_present())
  221. return -ENODEV;
  222. smu = of_find_node_by_type(NULL, "smu");
  223. if (smu == NULL)
  224. return -ENODEV;
  225. /* Look for RPM fans */
  226. for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
  227. if (of_node_name_eq(fans, "rpm-fans") ||
  228. of_device_is_compatible(fans, "smu-rpm-fans"))
  229. break;
  230. for (fan = NULL;
  231. fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
  232. struct smu_fan_control *fct;
  233. fct = smu_fan_create(fan, 0);
  234. if (fct == NULL) {
  235. printk(KERN_WARNING "windfarm: Failed to create SMU "
  236. "RPM fan %pOFn\n", fan);
  237. continue;
  238. }
  239. list_add(&fct->link, &smu_fans);
  240. }
  241. of_node_put(fans);
  242. /* Look for PWM fans */
  243. for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
  244. if (of_node_name_eq(fans, "pwm-fans"))
  245. break;
  246. for (fan = NULL;
  247. fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
  248. struct smu_fan_control *fct;
  249. fct = smu_fan_create(fan, 1);
  250. if (fct == NULL) {
  251. printk(KERN_WARNING "windfarm: Failed to create SMU "
  252. "PWM fan %pOFn\n", fan);
  253. continue;
  254. }
  255. list_add(&fct->link, &smu_fans);
  256. }
  257. of_node_put(fans);
  258. of_node_put(smu);
  259. return 0;
  260. }
  261. static void __exit smu_controls_exit(void)
  262. {
  263. struct smu_fan_control *fct;
  264. while (!list_empty(&smu_fans)) {
  265. fct = list_entry(smu_fans.next, struct smu_fan_control, link);
  266. list_del(&fct->link);
  267. wf_unregister_control(&fct->ctrl);
  268. }
  269. }
  270. module_init(smu_controls_init);
  271. module_exit(smu_controls_exit);
  272. MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");
  273. MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
  274. MODULE_LICENSE("GPL");