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>
17 #include <linux/completion.h>
19 #include <asm/machdep.h>
21 #include <asm/system.h>
22 #include <asm/sections.h>
32 #define DBG(args...) printk(args)
34 #define DBG(args...) do { } while(0)
37 static int smu_supports_new_fans_ops
= 1;
40 * SMU fans control object
43 static LIST_HEAD(smu_fans
);
45 struct smu_fan_control
{
46 struct list_head link
;
47 int fan_type
; /* 0 = rpm, 1 = pwm */
48 u32 reg
; /* index in SMU */
49 s32 value
; /* current value */
50 s32 min
, max
; /* min/max values */
51 struct wf_control ctrl
;
53 #define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)
55 static int smu_set_fan(int pwm
, u8 id
, u16 value
)
59 DECLARE_COMPLETION_ONSTACK(comp
);
62 /* Fill SMU command structure */
63 cmd
.cmd
= SMU_CMD_FAN_COMMAND
;
65 /* The SMU has an "old" and a "new" way of setting the fan speed
66 * Unfortunately, I found no reliable way to know which one works
67 * on a given machine model. After some investigations it appears
68 * that MacOS X just tries the new one, and if it fails fallbacks
69 * to the old ones ... Ugh.
72 if (smu_supports_new_fans_ops
) {
75 *((u16
*)(&buffer
[2])) = value
;
80 /* Fill argument buffer */
81 memset(buffer
, 0, 16);
82 buffer
[0] = pwm
? 0x10 : 0x00;
83 buffer
[1] = 0x01 << id
;
84 *((u16
*)&buffer
[2 + id
* 2]) = value
;
89 cmd
.data_buf
= cmd
.reply_buf
= buffer
;
91 cmd
.done
= smu_done_complete
;
94 rc
= smu_queue_cmd(&cmd
);
97 wait_for_completion(&comp
);
99 /* Handle fallback (see coment above) */
100 if (cmd
.status
!= 0 && smu_supports_new_fans_ops
) {
101 printk(KERN_WARNING
"windfarm: SMU failed new fan command "
102 "falling back to old method\n");
103 smu_supports_new_fans_ops
= 0;
110 static void smu_fan_release(struct wf_control
*ct
)
112 struct smu_fan_control
*fct
= to_smu_fan(ct
);
117 static int smu_fan_set(struct wf_control
*ct
, s32 value
)
119 struct smu_fan_control
*fct
= to_smu_fan(ct
);
121 if (value
< fct
->min
)
123 if (value
> fct
->max
)
127 return smu_set_fan(fct
->fan_type
, fct
->reg
, value
);
130 static int smu_fan_get(struct wf_control
*ct
, s32
*value
)
132 struct smu_fan_control
*fct
= to_smu_fan(ct
);
133 *value
= fct
->value
; /* todo: read from SMU */
137 static s32
smu_fan_min(struct wf_control
*ct
)
139 struct smu_fan_control
*fct
= to_smu_fan(ct
);
143 static s32
smu_fan_max(struct wf_control
*ct
)
145 struct smu_fan_control
*fct
= to_smu_fan(ct
);
149 static struct wf_control_ops smu_fan_ops
= {
150 .set_value
= smu_fan_set
,
151 .get_value
= smu_fan_get
,
152 .get_min
= smu_fan_min
,
153 .get_max
= smu_fan_max
,
154 .release
= smu_fan_release
,
155 .owner
= THIS_MODULE
,
158 static struct smu_fan_control
*smu_fan_create(struct device_node
*node
,
161 struct smu_fan_control
*fct
;
166 fct
= kmalloc(sizeof(struct smu_fan_control
), GFP_KERNEL
);
169 fct
->ctrl
.ops
= &smu_fan_ops
;
170 l
= of_get_property(node
, "location", NULL
);
174 fct
->fan_type
= pwm_fan
;
175 fct
->ctrl
.type
= pwm_fan
? WF_CONTROL_PWM_FAN
: WF_CONTROL_RPM_FAN
;
176 sysfs_attr_init(&fct
->ctrl
.attr
.attr
);
178 /* We use the name & location here the same way we do for SMU sensors,
179 * see the comment in windfarm_smu_sensors.c. The locations are a bit
180 * less consistent here between the iMac and the desktop models, but
181 * that is good enough for our needs for now at least.
183 * One problem though is that Apple seem to be inconsistent with case
184 * and the kernel doesn't have strcasecmp =P
187 fct
->ctrl
.name
= NULL
;
189 /* Names used on desktop models */
190 if (!strcmp(l
, "Rear Fan 0") || !strcmp(l
, "Rear Fan") ||
191 !strcmp(l
, "Rear fan 0") || !strcmp(l
, "Rear fan") ||
192 !strcmp(l
, "CPU A EXHAUST"))
193 fct
->ctrl
.name
= "cpu-rear-fan-0";
194 else if (!strcmp(l
, "Rear Fan 1") || !strcmp(l
, "Rear fan 1") ||
195 !strcmp(l
, "CPU B EXHAUST"))
196 fct
->ctrl
.name
= "cpu-rear-fan-1";
197 else if (!strcmp(l
, "Front Fan 0") || !strcmp(l
, "Front Fan") ||
198 !strcmp(l
, "Front fan 0") || !strcmp(l
, "Front fan") ||
199 !strcmp(l
, "CPU A INTAKE"))
200 fct
->ctrl
.name
= "cpu-front-fan-0";
201 else if (!strcmp(l
, "Front Fan 1") || !strcmp(l
, "Front fan 1") ||
202 !strcmp(l
, "CPU B INTAKE"))
203 fct
->ctrl
.name
= "cpu-front-fan-1";
204 else if (!strcmp(l
, "CPU A PUMP"))
205 fct
->ctrl
.name
= "cpu-pump-0";
206 else if (!strcmp(l
, "CPU B PUMP"))
207 fct
->ctrl
.name
= "cpu-pump-1";
208 else if (!strcmp(l
, "Slots Fan") || !strcmp(l
, "Slots fan") ||
209 !strcmp(l
, "EXPANSION SLOTS INTAKE"))
210 fct
->ctrl
.name
= "slots-fan";
211 else if (!strcmp(l
, "Drive Bay") || !strcmp(l
, "Drive bay") ||
212 !strcmp(l
, "DRIVE BAY A INTAKE"))
213 fct
->ctrl
.name
= "drive-bay-fan";
214 else if (!strcmp(l
, "BACKSIDE"))
215 fct
->ctrl
.name
= "backside-fan";
217 /* Names used on iMac models */
218 if (!strcmp(l
, "System Fan") || !strcmp(l
, "System fan"))
219 fct
->ctrl
.name
= "system-fan";
220 else if (!strcmp(l
, "CPU Fan") || !strcmp(l
, "CPU fan"))
221 fct
->ctrl
.name
= "cpu-fan";
222 else if (!strcmp(l
, "Hard Drive") || !strcmp(l
, "Hard drive"))
223 fct
->ctrl
.name
= "drive-bay-fan";
224 else if (!strcmp(l
, "HDD Fan")) /* seen on iMac G5 iSight */
225 fct
->ctrl
.name
= "hard-drive-fan";
226 else if (!strcmp(l
, "ODD Fan")) /* same */
227 fct
->ctrl
.name
= "optical-drive-fan";
229 /* Unrecognized fan, bail out */
230 if (fct
->ctrl
.name
== NULL
)
233 /* Get min & max values*/
234 v
= of_get_property(node
, "min-value", NULL
);
238 v
= of_get_property(node
, "max-value", NULL
);
243 /* Get "reg" value */
244 reg
= of_get_property(node
, "reg", NULL
);
249 if (wf_register_control(&fct
->ctrl
))
259 static int __init
smu_controls_init(void)
261 struct device_node
*smu
, *fans
, *fan
;
266 smu
= of_find_node_by_type(NULL
, "smu");
270 /* Look for RPM fans */
271 for (fans
= NULL
; (fans
= of_get_next_child(smu
, fans
)) != NULL
;)
272 if (!strcmp(fans
->name
, "rpm-fans") ||
273 of_device_is_compatible(fans
, "smu-rpm-fans"))
276 fans
&& (fan
= of_get_next_child(fans
, fan
)) != NULL
;) {
277 struct smu_fan_control
*fct
;
279 fct
= smu_fan_create(fan
, 0);
281 printk(KERN_WARNING
"windfarm: Failed to create SMU "
282 "RPM fan %s\n", fan
->name
);
285 list_add(&fct
->link
, &smu_fans
);
290 /* Look for PWM fans */
291 for (fans
= NULL
; (fans
= of_get_next_child(smu
, fans
)) != NULL
;)
292 if (!strcmp(fans
->name
, "pwm-fans"))
295 fans
&& (fan
= of_get_next_child(fans
, fan
)) != NULL
;) {
296 struct smu_fan_control
*fct
;
298 fct
= smu_fan_create(fan
, 1);
300 printk(KERN_WARNING
"windfarm: Failed to create SMU "
301 "PWM fan %s\n", fan
->name
);
304 list_add(&fct
->link
, &smu_fans
);
312 static void __exit
smu_controls_exit(void)
314 struct smu_fan_control
*fct
;
316 while (!list_empty(&smu_fans
)) {
317 fct
= list_entry(smu_fans
.next
, struct smu_fan_control
, link
);
318 list_del(&fct
->link
);
319 wf_unregister_control(&fct
->ctrl
);
324 module_init(smu_controls_init
);
325 module_exit(smu_controls_exit
);
327 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
328 MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
329 MODULE_LICENSE("GPL");