2 * Windfarm PowerMac thermal control. SMU based controls
4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * Released under the term of the GNU GPL v2.
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/wait.h>
18 #include <asm/machdep.h>
20 #include <asm/system.h>
21 #include <asm/sections.h>
31 #define DBG(args...) printk(args)
33 #define DBG(args...) do { } while(0)
37 * SMU fans control object
40 static LIST_HEAD(smu_fans
);
42 struct smu_fan_control
{
43 struct list_head link
;
44 int fan_type
; /* 0 = rpm, 1 = pwm */
45 u32 reg
; /* index in SMU */
46 s32 value
; /* current value */
47 s32 min
, max
; /* min/max values */
48 struct wf_control ctrl
;
50 #define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)
52 static int smu_set_fan(int pwm
, u8 id
, u16 value
)
56 DECLARE_COMPLETION(comp
);
59 /* Fill SMU command structure */
60 cmd
.cmd
= SMU_CMD_FAN_COMMAND
;
63 cmd
.data_buf
= cmd
.reply_buf
= buffer
;
65 cmd
.done
= smu_done_complete
;
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
;
74 rc
= smu_queue_cmd(&cmd
);
77 wait_for_completion(&comp
);
81 static void smu_fan_release(struct wf_control
*ct
)
83 struct smu_fan_control
*fct
= to_smu_fan(ct
);
88 static int smu_fan_set(struct wf_control
*ct
, s32 value
)
90 struct smu_fan_control
*fct
= to_smu_fan(ct
);
98 return smu_set_fan(fct
->fan_type
, fct
->reg
, value
);
101 static int smu_fan_get(struct wf_control
*ct
, s32
*value
)
103 struct smu_fan_control
*fct
= to_smu_fan(ct
);
104 *value
= fct
->value
; /* todo: read from SMU */
108 static s32
smu_fan_min(struct wf_control
*ct
)
110 struct smu_fan_control
*fct
= to_smu_fan(ct
);
114 static s32
smu_fan_max(struct wf_control
*ct
)
116 struct smu_fan_control
*fct
= to_smu_fan(ct
);
120 static struct wf_control_ops smu_fan_ops
= {
121 .set_value
= smu_fan_set
,
122 .get_value
= smu_fan_get
,
123 .get_min
= smu_fan_min
,
124 .get_max
= smu_fan_max
,
125 .release
= smu_fan_release
,
126 .owner
= THIS_MODULE
,
129 static struct smu_fan_control
*smu_fan_create(struct device_node
*node
,
132 struct smu_fan_control
*fct
;
136 fct
= kmalloc(sizeof(struct smu_fan_control
), GFP_KERNEL
);
139 fct
->ctrl
.ops
= &smu_fan_ops
;
140 l
= (char *)get_property(node
, "location", NULL
);
144 fct
->fan_type
= pwm_fan
;
145 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.
152 * One problem though is that Apple seem to be inconsistent with case
153 * and the kernel doesn't have strcasecmp =P
156 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 fct
->ctrl
.name
= "cpu-rear-fan-0";
162 else if (!strcmp(l
, "Rear Fan 1") || !strcmp(l
, "Rear fan 1"))
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 fct
->ctrl
.name
= "cpu-front-fan-0";
167 else if (!strcmp(l
, "Front Fan 1") || !strcmp(l
, "Front fan 1"))
168 fct
->ctrl
.name
= "cpu-front-fan-1";
169 else if (!strcmp(l
, "Slots Fan") || !strcmp(l
, "Slots fan"))
170 fct
->ctrl
.name
= "slots-fan";
171 else if (!strcmp(l
, "Drive Bay") || !strcmp(l
, "Drive bay"))
172 fct
->ctrl
.name
= "drive-bay-fan";
174 /* Names used on iMac models */
175 if (!strcmp(l
, "System Fan") || !strcmp(l
, "System fan"))
176 fct
->ctrl
.name
= "system-fan";
177 else if (!strcmp(l
, "CPU Fan") || !strcmp(l
, "CPU fan"))
178 fct
->ctrl
.name
= "cpu-fan";
179 else if (!strcmp(l
, "Hard Drive") || !strcmp(l
, "Hard drive"))
180 fct
->ctrl
.name
= "drive-bay-fan";
182 /* Unrecognized fan, bail out */
183 if (fct
->ctrl
.name
== NULL
)
186 /* Get min & max values*/
187 v
= (s32
*)get_property(node
, "min-value", NULL
);
191 v
= (s32
*)get_property(node
, "max-value", NULL
);
196 /* Get "reg" value */
197 reg
= (u32
*)get_property(node
, "reg", NULL
);
202 if (wf_register_control(&fct
->ctrl
))
212 static int __init
smu_controls_init(void)
214 struct device_node
*smu
, *fans
, *fan
;
219 smu
= of_find_node_by_type(NULL
, "smu");
223 /* Look for RPM fans */
224 for (fans
= NULL
; (fans
= of_get_next_child(smu
, fans
)) != NULL
;)
225 if (!strcmp(fans
->name
, "rpm-fans"))
228 fans
&& (fan
= of_get_next_child(fans
, fan
)) != NULL
;) {
229 struct smu_fan_control
*fct
;
231 fct
= smu_fan_create(fan
, 0);
233 printk(KERN_WARNING
"windfarm: Failed to create SMU "
234 "RPM fan %s\n", fan
->name
);
237 list_add(&fct
->link
, &smu_fans
);
242 /* Look for PWM fans */
243 for (fans
= NULL
; (fans
= of_get_next_child(smu
, fans
)) != NULL
;)
244 if (!strcmp(fans
->name
, "pwm-fans"))
247 fans
&& (fan
= of_get_next_child(fans
, fan
)) != NULL
;) {
248 struct smu_fan_control
*fct
;
250 fct
= smu_fan_create(fan
, 1);
252 printk(KERN_WARNING
"windfarm: Failed to create SMU "
253 "PWM fan %s\n", fan
->name
);
256 list_add(&fct
->link
, &smu_fans
);
264 static void __exit
smu_controls_exit(void)
266 struct smu_fan_control
*fct
;
268 while (!list_empty(&smu_fans
)) {
269 fct
= list_entry(smu_fans
.next
, struct smu_fan_control
, link
);
270 list_del(&fct
->link
);
271 wf_unregister_control(&fct
->ctrl
);
276 module_init(smu_controls_init
);
277 module_exit(smu_controls_exit
);
279 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
280 MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
281 MODULE_LICENSE("GPL");