windfarm_smu_controls.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_ONSTACK(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. const s32 *v;
  137. const u32 *reg;
  138. const char *l;
  139. fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);
  140. if (fct == NULL)
  141. return NULL;
  142. fct->ctrl.ops = &smu_fan_ops;
  143. l = of_get_property(node, "location", NULL);
  144. if (l == NULL)
  145. goto fail;
  146. fct->fan_type = pwm_fan;
  147. fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN;
  148. sysfs_attr_init(&fct->ctrl.attr.attr);
  149. /* We use the name & location here the same way we do for SMU sensors,
  150. * see the comment in windfarm_smu_sensors.c. The locations are a bit
  151. * less consistent here between the iMac and the desktop models, but
  152. * that is good enough for our needs for now at least.
  153. *
  154. * One problem though is that Apple seem to be inconsistent with case
  155. * and the kernel doesn't have strcasecmp =P
  156. */
  157. fct->ctrl.name = NULL;
  158. /* Names used on desktop models */
  159. if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") ||
  160. !strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan") ||
  161. !strcmp(l, "CPU A EXHAUST"))
  162. fct->ctrl.name = "cpu-rear-fan-0";
  163. else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1") ||
  164. !strcmp(l, "CPU B EXHAUST"))
  165. fct->ctrl.name = "cpu-rear-fan-1";
  166. else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") ||
  167. !strcmp(l, "Front fan 0") || !strcmp(l, "Front fan") ||
  168. !strcmp(l, "CPU A INTAKE"))
  169. fct->ctrl.name = "cpu-front-fan-0";
  170. else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1") ||
  171. !strcmp(l, "CPU B INTAKE"))
  172. fct->ctrl.name = "cpu-front-fan-1";
  173. else if (!strcmp(l, "CPU A PUMP"))
  174. fct->ctrl.name = "cpu-pump-0";
  175. else if (!strcmp(l, "CPU B PUMP"))
  176. fct->ctrl.name = "cpu-pump-1";
  177. else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") ||
  178. !strcmp(l, "EXPANSION SLOTS INTAKE"))
  179. fct->ctrl.name = "slots-fan";
  180. else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") ||
  181. !strcmp(l, "DRIVE BAY A INTAKE"))
  182. fct->ctrl.name = "drive-bay-fan";
  183. else if (!strcmp(l, "BACKSIDE"))
  184. fct->ctrl.name = "backside-fan";
  185. /* Names used on iMac models */
  186. if (!strcmp(l, "System Fan") || !strcmp(l, "System fan"))
  187. fct->ctrl.name = "system-fan";
  188. else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan"))
  189. fct->ctrl.name = "cpu-fan";
  190. else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive"))
  191. fct->ctrl.name = "drive-bay-fan";
  192. else if (!strcmp(l, "HDD Fan")) /* seen on iMac G5 iSight */
  193. fct->ctrl.name = "hard-drive-fan";
  194. else if (!strcmp(l, "ODD Fan")) /* same */
  195. fct->ctrl.name = "optical-drive-fan";
  196. /* Unrecognized fan, bail out */
  197. if (fct->ctrl.name == NULL)
  198. goto fail;
  199. /* Get min & max values*/
  200. v = of_get_property(node, "min-value", NULL);
  201. if (v == NULL)
  202. goto fail;
  203. fct->min = *v;
  204. v = of_get_property(node, "max-value", NULL);
  205. if (v == NULL)
  206. goto fail;
  207. fct->max = *v;
  208. /* Get "reg" value */
  209. reg = of_get_property(node, "reg", NULL);
  210. if (reg == NULL)
  211. goto fail;
  212. fct->reg = *reg;
  213. if (wf_register_control(&fct->ctrl))
  214. goto fail;
  215. return fct;
  216. fail:
  217. kfree(fct);
  218. return NULL;
  219. }
  220. static int __init smu_controls_init(void)
  221. {
  222. struct device_node *smu, *fans, *fan;
  223. if (!smu_present())
  224. return -ENODEV;
  225. smu = of_find_node_by_type(NULL, "smu");
  226. if (smu == NULL)
  227. return -ENODEV;
  228. /* Look for RPM fans */
  229. for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
  230. if (!strcmp(fans->name, "rpm-fans") ||
  231. of_device_is_compatible(fans, "smu-rpm-fans"))
  232. break;
  233. for (fan = NULL;
  234. fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
  235. struct smu_fan_control *fct;
  236. fct = smu_fan_create(fan, 0);
  237. if (fct == NULL) {
  238. printk(KERN_WARNING "windfarm: Failed to create SMU "
  239. "RPM fan %s\n", fan->name);
  240. continue;
  241. }
  242. list_add(&fct->link, &smu_fans);
  243. }
  244. of_node_put(fans);
  245. /* Look for PWM fans */
  246. for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
  247. if (!strcmp(fans->name, "pwm-fans"))
  248. break;
  249. for (fan = NULL;
  250. fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
  251. struct smu_fan_control *fct;
  252. fct = smu_fan_create(fan, 1);
  253. if (fct == NULL) {
  254. printk(KERN_WARNING "windfarm: Failed to create SMU "
  255. "PWM fan %s\n", fan->name);
  256. continue;
  257. }
  258. list_add(&fct->link, &smu_fans);
  259. }
  260. of_node_put(fans);
  261. of_node_put(smu);
  262. return 0;
  263. }
  264. static void __exit smu_controls_exit(void)
  265. {
  266. struct smu_fan_control *fct;
  267. while (!list_empty(&smu_fans)) {
  268. fct = list_entry(smu_fans.next, struct smu_fan_control, link);
  269. list_del(&fct->link);
  270. wf_unregister_control(&fct->ctrl);
  271. }
  272. }
  273. module_init(smu_controls_init);
  274. module_exit(smu_controls_exit);
  275. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  276. MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
  277. MODULE_LICENSE("GPL");