windfarm_smu_controls.c 6.6 KB

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