windfarm_smu_controls.c 7.8 KB

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