windfarm_smu_controls.c 8.0 KB

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