Loading...
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
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>
18#include <asm/prom.h>
19#include <asm/machdep.h>
20#include <asm/io.h>
21#include <asm/system.h>
22#include <asm/sections.h>
23#include <asm/smu.h>
24
25#include "windfarm.h"
26
27#define VERSION "0.4"
28
29#undef DEBUG
30
31#ifdef DEBUG
32#define DBG(args...) printk(args)
33#else
34#define DBG(args...) do { } while(0)
35#endif
36
37static int smu_supports_new_fans_ops = 1;
38
39/*
40 * SMU fans control object
41 */
42
43static LIST_HEAD(smu_fans);
44
45struct 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;
52};
53#define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)
54
55static int smu_set_fan(int pwm, u8 id, u16 value)
56{
57 struct smu_cmd cmd;
58 u8 buffer[16];
59 DECLARE_COMPLETION_ONSTACK(comp);
60 int rc;
61
62 /* Fill SMU command structure */
63 cmd.cmd = SMU_CMD_FAN_COMMAND;
64
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.
70 */
71 retry:
72 if (smu_supports_new_fans_ops) {
73 buffer[0] = 0x30;
74 buffer[1] = id;
75 *((u16 *)(&buffer[2])) = value;
76 cmd.data_len = 4;
77 } else {
78 if (id > 7)
79 return -EINVAL;
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;
85 cmd.data_len = 14;
86 }
87
88 cmd.reply_len = 16;
89 cmd.data_buf = cmd.reply_buf = buffer;
90 cmd.status = 0;
91 cmd.done = smu_done_complete;
92 cmd.misc = ∁
93
94 rc = smu_queue_cmd(&cmd);
95 if (rc)
96 return rc;
97 wait_for_completion(&comp);
98
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;
104 goto retry;
105 }
106
107 return cmd.status;
108}
109
110static void smu_fan_release(struct wf_control *ct)
111{
112 struct smu_fan_control *fct = to_smu_fan(ct);
113
114 kfree(fct);
115}
116
117static int smu_fan_set(struct wf_control *ct, s32 value)
118{
119 struct smu_fan_control *fct = to_smu_fan(ct);
120
121 if (value < fct->min)
122 value = fct->min;
123 if (value > fct->max)
124 value = fct->max;
125 fct->value = value;
126
127 return smu_set_fan(fct->fan_type, fct->reg, value);
128}
129
130static int smu_fan_get(struct wf_control *ct, s32 *value)
131{
132 struct smu_fan_control *fct = to_smu_fan(ct);
133 *value = fct->value; /* todo: read from SMU */
134 return 0;
135}
136
137static s32 smu_fan_min(struct wf_control *ct)
138{
139 struct smu_fan_control *fct = to_smu_fan(ct);
140 return fct->min;
141}
142
143static s32 smu_fan_max(struct wf_control *ct)
144{
145 struct smu_fan_control *fct = to_smu_fan(ct);
146 return fct->max;
147}
148
149static 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,
156};
157
158static struct smu_fan_control *smu_fan_create(struct device_node *node,
159 int pwm_fan)
160{
161 struct smu_fan_control *fct;
162 const s32 *v;
163 const u32 *reg;
164 const char *l;
165
166 fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);
167 if (fct == NULL)
168 return NULL;
169 fct->ctrl.ops = &smu_fan_ops;
170 l = of_get_property(node, "location", NULL);
171 if (l == NULL)
172 goto fail;
173
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);
177
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.
182 *
183 * One problem though is that Apple seem to be inconsistent with case
184 * and the kernel doesn't have strcasecmp =P
185 */
186
187 fct->ctrl.name = NULL;
188
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";
216
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";
228
229 /* Unrecognized fan, bail out */
230 if (fct->ctrl.name == NULL)
231 goto fail;
232
233 /* Get min & max values*/
234 v = of_get_property(node, "min-value", NULL);
235 if (v == NULL)
236 goto fail;
237 fct->min = *v;
238 v = of_get_property(node, "max-value", NULL);
239 if (v == NULL)
240 goto fail;
241 fct->max = *v;
242
243 /* Get "reg" value */
244 reg = of_get_property(node, "reg", NULL);
245 if (reg == NULL)
246 goto fail;
247 fct->reg = *reg;
248
249 if (wf_register_control(&fct->ctrl))
250 goto fail;
251
252 return fct;
253 fail:
254 kfree(fct);
255 return NULL;
256}
257
258
259static int __init smu_controls_init(void)
260{
261 struct device_node *smu, *fans, *fan;
262
263 if (!smu_present())
264 return -ENODEV;
265
266 smu = of_find_node_by_type(NULL, "smu");
267 if (smu == NULL)
268 return -ENODEV;
269
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"))
274 break;
275 for (fan = NULL;
276 fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
277 struct smu_fan_control *fct;
278
279 fct = smu_fan_create(fan, 0);
280 if (fct == NULL) {
281 printk(KERN_WARNING "windfarm: Failed to create SMU "
282 "RPM fan %s\n", fan->name);
283 continue;
284 }
285 list_add(&fct->link, &smu_fans);
286 }
287 of_node_put(fans);
288
289
290 /* Look for PWM fans */
291 for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
292 if (!strcmp(fans->name, "pwm-fans"))
293 break;
294 for (fan = NULL;
295 fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
296 struct smu_fan_control *fct;
297
298 fct = smu_fan_create(fan, 1);
299 if (fct == NULL) {
300 printk(KERN_WARNING "windfarm: Failed to create SMU "
301 "PWM fan %s\n", fan->name);
302 continue;
303 }
304 list_add(&fct->link, &smu_fans);
305 }
306 of_node_put(fans);
307 of_node_put(smu);
308
309 return 0;
310}
311
312static void __exit smu_controls_exit(void)
313{
314 struct smu_fan_control *fct;
315
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);
320 }
321}
322
323
324module_init(smu_controls_init);
325module_exit(smu_controls_exit);
326
327MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
328MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
329MODULE_LICENSE("GPL");
330
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Windfarm PowerMac thermal control. SMU based controls
4 *
5 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
6 * <benh@kernel.crashing.org>
7 */
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 <linux/of.h>
18
19#include <asm/machdep.h>
20#include <asm/io.h>
21#include <asm/sections.h>
22#include <asm/smu.h>
23
24#include "windfarm.h"
25
26#define VERSION "0.4"
27
28#undef DEBUG
29
30#ifdef DEBUG
31#define DBG(args...) printk(args)
32#else
33#define DBG(args...) do { } while(0)
34#endif
35
36static int smu_supports_new_fans_ops = 1;
37
38/*
39 * SMU fans control object
40 */
41
42static LIST_HEAD(smu_fans);
43
44struct smu_fan_control {
45 struct list_head link;
46 int fan_type; /* 0 = rpm, 1 = pwm */
47 u32 reg; /* index in SMU */
48 s32 value; /* current value */
49 s32 min, max; /* min/max values */
50 struct wf_control ctrl;
51};
52#define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)
53
54static int smu_set_fan(int pwm, u8 id, u16 value)
55{
56 struct smu_cmd cmd;
57 u8 buffer[16];
58 DECLARE_COMPLETION_ONSTACK(comp);
59 int rc;
60
61 /* Fill SMU command structure */
62 cmd.cmd = SMU_CMD_FAN_COMMAND;
63
64 /* The SMU has an "old" and a "new" way of setting the fan speed
65 * Unfortunately, I found no reliable way to know which one works
66 * on a given machine model. After some investigations it appears
67 * that MacOS X just tries the new one, and if it fails fallbacks
68 * to the old ones ... Ugh.
69 */
70 retry:
71 if (smu_supports_new_fans_ops) {
72 buffer[0] = 0x30;
73 buffer[1] = id;
74 *((u16 *)(&buffer[2])) = value;
75 cmd.data_len = 4;
76 } else {
77 if (id > 7)
78 return -EINVAL;
79 /* Fill argument buffer */
80 memset(buffer, 0, 16);
81 buffer[0] = pwm ? 0x10 : 0x00;
82 buffer[1] = 0x01 << id;
83 *((u16 *)&buffer[2 + id * 2]) = value;
84 cmd.data_len = 14;
85 }
86
87 cmd.reply_len = 16;
88 cmd.data_buf = cmd.reply_buf = buffer;
89 cmd.status = 0;
90 cmd.done = smu_done_complete;
91 cmd.misc = ∁
92
93 rc = smu_queue_cmd(&cmd);
94 if (rc)
95 return rc;
96 wait_for_completion(&comp);
97
98 /* Handle fallback (see comment above) */
99 if (cmd.status != 0 && smu_supports_new_fans_ops) {
100 printk(KERN_WARNING "windfarm: SMU failed new fan command "
101 "falling back to old method\n");
102 smu_supports_new_fans_ops = 0;
103 goto retry;
104 }
105
106 return cmd.status;
107}
108
109static void smu_fan_release(struct wf_control *ct)
110{
111 struct smu_fan_control *fct = to_smu_fan(ct);
112
113 kfree(fct);
114}
115
116static int smu_fan_set(struct wf_control *ct, s32 value)
117{
118 struct smu_fan_control *fct = to_smu_fan(ct);
119
120 if (value < fct->min)
121 value = fct->min;
122 if (value > fct->max)
123 value = fct->max;
124 fct->value = value;
125
126 return smu_set_fan(fct->fan_type, fct->reg, value);
127}
128
129static int smu_fan_get(struct wf_control *ct, s32 *value)
130{
131 struct smu_fan_control *fct = to_smu_fan(ct);
132 *value = fct->value; /* todo: read from SMU */
133 return 0;
134}
135
136static s32 smu_fan_min(struct wf_control *ct)
137{
138 struct smu_fan_control *fct = to_smu_fan(ct);
139 return fct->min;
140}
141
142static s32 smu_fan_max(struct wf_control *ct)
143{
144 struct smu_fan_control *fct = to_smu_fan(ct);
145 return fct->max;
146}
147
148static const struct wf_control_ops smu_fan_ops = {
149 .set_value = smu_fan_set,
150 .get_value = smu_fan_get,
151 .get_min = smu_fan_min,
152 .get_max = smu_fan_max,
153 .release = smu_fan_release,
154 .owner = THIS_MODULE,
155};
156
157static struct smu_fan_control *smu_fan_create(struct device_node *node,
158 int pwm_fan)
159{
160 struct smu_fan_control *fct;
161 const s32 *v;
162 const u32 *reg;
163 const char *l;
164
165 fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);
166 if (fct == NULL)
167 return NULL;
168 fct->ctrl.ops = &smu_fan_ops;
169 l = of_get_property(node, "location", NULL);
170 if (l == NULL)
171 goto fail;
172
173 fct->fan_type = pwm_fan;
174 fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN;
175
176 /* We use the name & location here the same way we do for SMU sensors,
177 * see the comment in windfarm_smu_sensors.c. The locations are a bit
178 * less consistent here between the iMac and the desktop models, but
179 * that is good enough for our needs for now at least.
180 *
181 * One problem though is that Apple seem to be inconsistent with case
182 * and the kernel doesn't have strcasecmp =P
183 */
184
185 fct->ctrl.name = NULL;
186
187 /* Names used on desktop models */
188 if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") ||
189 !strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan") ||
190 !strcmp(l, "CPU A EXHAUST"))
191 fct->ctrl.name = "cpu-rear-fan-0";
192 else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1") ||
193 !strcmp(l, "CPU B EXHAUST"))
194 fct->ctrl.name = "cpu-rear-fan-1";
195 else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") ||
196 !strcmp(l, "Front fan 0") || !strcmp(l, "Front fan") ||
197 !strcmp(l, "CPU A INTAKE"))
198 fct->ctrl.name = "cpu-front-fan-0";
199 else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1") ||
200 !strcmp(l, "CPU B INTAKE"))
201 fct->ctrl.name = "cpu-front-fan-1";
202 else if (!strcmp(l, "CPU A PUMP"))
203 fct->ctrl.name = "cpu-pump-0";
204 else if (!strcmp(l, "CPU B PUMP"))
205 fct->ctrl.name = "cpu-pump-1";
206 else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") ||
207 !strcmp(l, "EXPANSION SLOTS INTAKE"))
208 fct->ctrl.name = "slots-fan";
209 else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") ||
210 !strcmp(l, "DRIVE BAY A INTAKE"))
211 fct->ctrl.name = "drive-bay-fan";
212 else if (!strcmp(l, "BACKSIDE"))
213 fct->ctrl.name = "backside-fan";
214
215 /* Names used on iMac models */
216 if (!strcmp(l, "System Fan") || !strcmp(l, "System fan"))
217 fct->ctrl.name = "system-fan";
218 else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan"))
219 fct->ctrl.name = "cpu-fan";
220 else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive"))
221 fct->ctrl.name = "drive-bay-fan";
222 else if (!strcmp(l, "HDD Fan")) /* seen on iMac G5 iSight */
223 fct->ctrl.name = "hard-drive-fan";
224 else if (!strcmp(l, "ODD Fan")) /* same */
225 fct->ctrl.name = "optical-drive-fan";
226
227 /* Unrecognized fan, bail out */
228 if (fct->ctrl.name == NULL)
229 goto fail;
230
231 /* Get min & max values*/
232 v = of_get_property(node, "min-value", NULL);
233 if (v == NULL)
234 goto fail;
235 fct->min = *v;
236 v = of_get_property(node, "max-value", NULL);
237 if (v == NULL)
238 goto fail;
239 fct->max = *v;
240
241 /* Get "reg" value */
242 reg = of_get_property(node, "reg", NULL);
243 if (reg == NULL)
244 goto fail;
245 fct->reg = *reg;
246
247 if (wf_register_control(&fct->ctrl))
248 goto fail;
249
250 return fct;
251 fail:
252 kfree(fct);
253 return NULL;
254}
255
256
257static int __init smu_controls_init(void)
258{
259 struct device_node *smu, *fans, *fan;
260
261 if (!smu_present())
262 return -ENODEV;
263
264 smu = of_find_node_by_type(NULL, "smu");
265 if (smu == NULL)
266 return -ENODEV;
267
268 /* Look for RPM fans */
269 for_each_child_of_node(smu, fans)
270 if (of_node_name_eq(fans, "rpm-fans") ||
271 of_device_is_compatible(fans, "smu-rpm-fans"))
272 break;
273 for_each_child_of_node(fans, fan) {
274 struct smu_fan_control *fct;
275
276 fct = smu_fan_create(fan, 0);
277 if (fct == NULL) {
278 printk(KERN_WARNING "windfarm: Failed to create SMU "
279 "RPM fan %pOFn\n", fan);
280 continue;
281 }
282 list_add(&fct->link, &smu_fans);
283 }
284 of_node_put(fans);
285
286
287 /* Look for PWM fans */
288 for_each_child_of_node(smu, fans)
289 if (of_node_name_eq(fans, "pwm-fans"))
290 break;
291 for_each_child_of_node(fans, fan) {
292 struct smu_fan_control *fct;
293
294 fct = smu_fan_create(fan, 1);
295 if (fct == NULL) {
296 printk(KERN_WARNING "windfarm: Failed to create SMU "
297 "PWM fan %pOFn\n", fan);
298 continue;
299 }
300 list_add(&fct->link, &smu_fans);
301 }
302 of_node_put(fans);
303 of_node_put(smu);
304
305 return 0;
306}
307
308static void __exit smu_controls_exit(void)
309{
310 struct smu_fan_control *fct;
311
312 while (!list_empty(&smu_fans)) {
313 fct = list_entry(smu_fans.next, struct smu_fan_control, link);
314 list_del(&fct->link);
315 wf_unregister_control(&fct->ctrl);
316 }
317}
318
319
320module_init(smu_controls_init);
321module_exit(smu_controls_exit);
322
323MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
324MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
325MODULE_LICENSE("GPL");
326