windfarm_smu_controls.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.3"
  25. #undef DEBUG
  26. #ifdef DEBUG
  27. #define DBG(args...) printk(args)
  28. #else
  29. #define DBG(args...) do { } while(0)
  30. #endif
  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(comp);
  49. int rc;
  50. /* Fill SMU command structure */
  51. cmd.cmd = SMU_CMD_FAN_COMMAND;
  52. cmd.data_len = 14;
  53. cmd.reply_len = 16;
  54. cmd.data_buf = cmd.reply_buf = buffer;
  55. cmd.status = 0;
  56. cmd.done = smu_done_complete;
  57. cmd.misc = &comp;
  58. /* Fill argument buffer */
  59. memset(buffer, 0, 16);
  60. buffer[0] = pwm ? 0x10 : 0x00;
  61. buffer[1] = 0x01 << id;
  62. *((u16 *)&buffer[2 + id * 2]) = value;
  63. rc = smu_queue_cmd(&cmd);
  64. if (rc)
  65. return rc;
  66. wait_for_completion(&comp);
  67. return cmd.status;
  68. }
  69. static void smu_fan_release(struct wf_control *ct)
  70. {
  71. struct smu_fan_control *fct = to_smu_fan(ct);
  72. kfree(fct);
  73. }
  74. static int smu_fan_set(struct wf_control *ct, s32 value)
  75. {
  76. struct smu_fan_control *fct = to_smu_fan(ct);
  77. if (value < fct->min)
  78. value = fct->min;
  79. if (value > fct->max)
  80. value = fct->max;
  81. fct->value = value;
  82. return smu_set_fan(fct->fan_type, fct->reg, value);
  83. }
  84. static int smu_fan_get(struct wf_control *ct, s32 *value)
  85. {
  86. struct smu_fan_control *fct = to_smu_fan(ct);
  87. *value = fct->value; /* todo: read from SMU */
  88. return 0;
  89. }
  90. static s32 smu_fan_min(struct wf_control *ct)
  91. {
  92. struct smu_fan_control *fct = to_smu_fan(ct);
  93. return fct->min;
  94. }
  95. static s32 smu_fan_max(struct wf_control *ct)
  96. {
  97. struct smu_fan_control *fct = to_smu_fan(ct);
  98. return fct->max;
  99. }
  100. static struct wf_control_ops smu_fan_ops = {
  101. .set_value = smu_fan_set,
  102. .get_value = smu_fan_get,
  103. .get_min = smu_fan_min,
  104. .get_max = smu_fan_max,
  105. .release = smu_fan_release,
  106. .owner = THIS_MODULE,
  107. };
  108. static struct smu_fan_control *smu_fan_create(struct device_node *node,
  109. int pwm_fan)
  110. {
  111. struct smu_fan_control *fct;
  112. s32 *v; u32 *reg;
  113. char *l;
  114. fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);
  115. if (fct == NULL)
  116. return NULL;
  117. fct->ctrl.ops = &smu_fan_ops;
  118. l = (char *)get_property(node, "location", NULL);
  119. if (l == NULL)
  120. goto fail;
  121. fct->fan_type = pwm_fan;
  122. fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN;
  123. /* We use the name & location here the same way we do for SMU sensors,
  124. * see the comment in windfarm_smu_sensors.c. The locations are a bit
  125. * less consistent here between the iMac and the desktop models, but
  126. * that is good enough for our needs for now at least.
  127. *
  128. * One problem though is that Apple seem to be inconsistent with case
  129. * and the kernel doesn't have strcasecmp =P
  130. */
  131. fct->ctrl.name = NULL;
  132. /* Names used on desktop models */
  133. if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") ||
  134. !strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan"))
  135. fct->ctrl.name = "cpu-rear-fan-0";
  136. else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1"))
  137. fct->ctrl.name = "cpu-rear-fan-1";
  138. else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") ||
  139. !strcmp(l, "Front fan 0") || !strcmp(l, "Front fan"))
  140. fct->ctrl.name = "cpu-front-fan-0";
  141. else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1"))
  142. fct->ctrl.name = "cpu-front-fan-1";
  143. else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan"))
  144. fct->ctrl.name = "slots-fan";
  145. else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay"))
  146. fct->ctrl.name = "drive-bay-fan";
  147. /* Names used on iMac models */
  148. if (!strcmp(l, "System Fan") || !strcmp(l, "System fan"))
  149. fct->ctrl.name = "system-fan";
  150. else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan"))
  151. fct->ctrl.name = "cpu-fan";
  152. else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive"))
  153. fct->ctrl.name = "drive-bay-fan";
  154. /* Unrecognized fan, bail out */
  155. if (fct->ctrl.name == NULL)
  156. goto fail;
  157. /* Get min & max values*/
  158. v = (s32 *)get_property(node, "min-value", NULL);
  159. if (v == NULL)
  160. goto fail;
  161. fct->min = *v;
  162. v = (s32 *)get_property(node, "max-value", NULL);
  163. if (v == NULL)
  164. goto fail;
  165. fct->max = *v;
  166. /* Get "reg" value */
  167. reg = (u32 *)get_property(node, "reg", NULL);
  168. if (reg == NULL)
  169. goto fail;
  170. fct->reg = *reg;
  171. if (wf_register_control(&fct->ctrl))
  172. goto fail;
  173. return fct;
  174. fail:
  175. kfree(fct);
  176. return NULL;
  177. }
  178. static int __init smu_controls_init(void)
  179. {
  180. struct device_node *smu, *fans, *fan;
  181. if (!smu_present())
  182. return -ENODEV;
  183. smu = of_find_node_by_type(NULL, "smu");
  184. if (smu == NULL)
  185. return -ENODEV;
  186. /* Look for RPM fans */
  187. for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
  188. if (!strcmp(fans->name, "rpm-fans"))
  189. break;
  190. for (fan = NULL;
  191. fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
  192. struct smu_fan_control *fct;
  193. fct = smu_fan_create(fan, 0);
  194. if (fct == NULL) {
  195. printk(KERN_WARNING "windfarm: Failed to create SMU "
  196. "RPM fan %s\n", fan->name);
  197. continue;
  198. }
  199. list_add(&fct->link, &smu_fans);
  200. }
  201. of_node_put(fans);
  202. /* Look for PWM fans */
  203. for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
  204. if (!strcmp(fans->name, "pwm-fans"))
  205. break;
  206. for (fan = NULL;
  207. fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
  208. struct smu_fan_control *fct;
  209. fct = smu_fan_create(fan, 1);
  210. if (fct == NULL) {
  211. printk(KERN_WARNING "windfarm: Failed to create SMU "
  212. "PWM fan %s\n", fan->name);
  213. continue;
  214. }
  215. list_add(&fct->link, &smu_fans);
  216. }
  217. of_node_put(fans);
  218. of_node_put(smu);
  219. return 0;
  220. }
  221. static void __exit smu_controls_exit(void)
  222. {
  223. struct smu_fan_control *fct;
  224. while (!list_empty(&smu_fans)) {
  225. fct = list_entry(smu_fans.next, struct smu_fan_control, link);
  226. list_del(&fct->link);
  227. wf_unregister_control(&fct->ctrl);
  228. }
  229. }
  230. module_init(smu_controls_init);
  231. module_exit(smu_controls_exit);
  232. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  233. MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
  234. MODULE_LICENSE("GPL");