Blame view

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