-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasus_fan.c
429 lines (343 loc) · 11.2 KB
/
asus_fan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
267
268
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* asus_fan.c - Advanced ACPI fan control
*
*
* Copyright (C) 2010-2013 Giulio Piemontese
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* The development page for this driver is located at
*
* http://github.com/gpiemont/asusfan/
*
* Previusly (by Dmitry Ursegov)
* http://code.google.com/p/asusfan/
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
#include <linux/acpi.h>
#include <acpi/platform/acenv.h>
#include <acpi/platform/aclinux.h>
#include <acpi/actypes.h>
#include <acpi/acrestyp.h>
#include <acpi/acpixf.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>
#include <acpi/acexcep.h>
MODULE_AUTHOR("Giulio Piemontese");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Advanced ACPI ASUS fan control");
#ifndef ASUSFAN_VERBOSE
#define ASUSFAN_VERBOSE 0
#endif
#ifndef ASUSFAN_STABLE_RANGE
#define ASUSFAN_STABLE_RANGE 1
#endif
#ifndef ASUSFAN_TEMP_NUM_SAMPLES
#define ASUSFAN_TEMP_NUM_SAMPLES 5
#endif
MODULE_PARM_DESC(verbose, "Enable Speed changing/temperature status messages (0-2)");
int asusfan_verbose = ASUSFAN_VERBOSE;
module_param_named(verbose, asusfan_verbose, int, 0600);
MODULE_PARM_DESC(current_zone, "Current thermal zone");
int asusfan_curr_zone = -1;
module_param_named(current_zone, asusfan_curr_zone, int, 0444);
MODULE_PARM_DESC(previous_zone, "Previous thermal zone");
int asusfan_prev_zone = -1;
module_param_named(previous_zone, asusfan_prev_zone, int, 0444);
MODULE_PARM_DESC(current_speed, "Current fan speed");
int asusfan_curr_speed = -1;
module_param_named(current_speed, asusfan_curr_speed, int, 0444);
MODULE_PARM_DESC(target_speed, "Manually set target fan speed (Dangerous!)");
int asusfan_target_speed = -1;
module_param_named(target_speed, asusfan_target_speed, int, 0400);
MODULE_PARM_DESC(max_speed, "Maximum supported speed");
int asusfan_max_speed = 255;
module_param_named(max_speed, asusfan_max_speed, int, 0400);
MODULE_PARM_DESC(min_speed, "Minumum supported speed");
int asusfan_min_speed = 0;
module_param_named(min_speed, asusfan_min_speed, int, 0400);
MODULE_PARM_DESC(current_temp, "Current temperature value (°C)");
int asusfan_curr_temp = -1;
module_param_named(current_temp, asusfan_curr_temp, int, 0444);
MODULE_PARM_DESC(max_temp, "Maximum temperature value (°C) that can be evaluated");
int asusfan_max_temp = 110;
module_param_named(max_temp, asusfan_max_temp, int, 0444);
MODULE_PARM_DESC(min_temp, "Minimum temperature value (°C) that can be evaluated");
int asusfan_min_temp = 0;
module_param_named(min_temp, asusfan_min_temp, int, 0444);
MODULE_PARM_DESC(temp_stable_range, "Within this range (+/-) temperature is considered stable");
int asusfan_stable_range = ASUSFAN_STABLE_RANGE;
module_param_named(temp_stable_range, asusfan_stable_range, int, 0600);
MODULE_PARM_DESC(temp_num_samples, "Number of samples to establish the temperature behaviour");
int asusfan_num_samples = 0;
module_param_named(temp_num_samples, asusfan_num_samples, int, 0600);
#if 0
/*
* Acpi objects names to store/retrieve the fan speed and
* the temperature values, respectively.
* Every platform will require its own method, provided
* in the get_zone_temp()/set_fan_speed() routines.
* Anyway we provide a convenience struct too, to store basic I/O object names.
* I.e: read temperature from/store speed in.
* Ordered by acpi platform.
*
* XXX Find a way to determine platform/model via ACPI
*
*/
struct acpi_tz_entry {
const char *acpi_platform_name;
const char *acpi_tz_obj;
const char *acpi_target_obj;
const char *acpi_current_speed_obj; /* can be NULL */
};
static struct asus_tz_entries[] = {
{ "default", "\\_TZ.THRM._TMP", "\\_SB.ATKD.ECRW", "\\_SB.PCI0.SBRG.EC0.CDT1" },
NULL
};
int asus_acpi_platform_no = 0; /* default platform */
#endif
/*
* We took a pre-defined number of samples of temperature from the acpi,
* then we try to guess the ongoing curve. This is done in a separated thread.
*/
unsigned int fan_sample = 0;
char fan_num_samples = ASUSFAN_TEMP_NUM_SAMPLES;
char samples[ASUSFAN_TEMP_NUM_SAMPLES] ;
#define TIMER_FREQ (2*HZ) /* seconds */
#define MONITOR_FREQ (2*HZ)
/*
* When the temperature goes down to a low zone it is better to stay at
* a high speed some degrees more to reduce fan speed switching
*/
#define NUM_ZONES 6
#define TMP_DIFF 3
struct tmp_zone {
int tmp; /* °C */
int speed; /* 80-255 (0 - fan is disabled) */
unsigned int sleep; /* Sleep for sleep seconds more at tmp temperature */
};
int asusfan_current_zone = -1;
typedef enum status {
stable,
ascending,
descending
} status_t;
status_t __thermal_status = stable;
const char * const status_name[3] = {
"stable",
"ascending",
"descending"
};
MODULE_PARM_DESC(temp_status, "Current temperature status");
static char asusfan_temp_statusname[16];
char *asusfan_temp_status = asusfan_temp_statusname;
module_param_named(temp_status, asusfan_temp_status, charp, 0444);
static struct tmp_zone zone[NUM_ZONES] = {{ 40, 80, 1 },
{ 55, 110, 1 },
{ 70, 130, 2 },
{ 85, 160, 3 },
{ 100, 190, 5 },
{ 105, 210, 7 }};
static void timer_handler(struct work_struct *work);
static void temp_status_timer(struct work_struct *work);
static DECLARE_DELAYED_WORK(ws, timer_handler);
static DECLARE_DELAYED_WORK(wst, temp_status_timer);
static struct workqueue_struct *wqs;
static struct workqueue_struct *wqst;
static int get_temp(void)
{
struct acpi_buffer output;
union acpi_object out_obj;
acpi_status status;
int tmp;
output.length = sizeof(out_obj);
output.pointer = &out_obj;
//Get current temperature
status = acpi_evaluate_object(NULL, "\\_TZ.THRM._TMP",
NULL, &output);
if (status != AE_OK) printk("_TZ.THRM._TMP error\n");
tmp = (int)((out_obj.integer.value-2732))/10;
if((tmp > asusfan_max_temp ) || (tmp < asusfan_min_temp))
return -1;
if(asusfan_curr_temp != tmp)
asusfan_curr_temp = tmp;
return tmp;
}
static inline int get_temp_zone(int tmp) {
int i;
for (i = 0 ; i < (NUM_ZONES - 1); i++) {
if ((tmp >= zone[i].tmp) &&
(tmp < zone[i+1].tmp))
return i;
}
return i;
}
static void set_fan_speed(int speed)
{
struct acpi_object_list params;
union acpi_object in_obj;
acpi_status status;
#if 0
/*
* It is required to evaluate this object with a maximum
* temperature supported by the manual control. If temperature is
* higher the manual control will be disabled
*/
params.count = 2;
params.pointer = in_objs;
in_objs[0].type = in_objs[1].type = ACPI_TYPE_INTEGER;
in_objs[0].integer.value = zone[NUM_ZONES-1].tmp; //temp
in_objs[1].integer.value = 0;
status = acpi_evaluate_object(NULL, "\\_TZ.THRM._WLM",
NULL, NULL);
if (status != AE_OK)
printk("_TZ.THRM._CRT error\n");
#endif
//Set fan speed
params.count = 1;
params.pointer = &in_obj;
in_obj.type = ACPI_TYPE_INTEGER;
in_obj.integer.value = ((0x84 << 16)
+ (speed << 8) + (0xc4));
if(asusfan_verbose)
printk("acpi integer value (_SB.ATKD.ECRW ) : %llu\n", in_obj.integer.value);
status = acpi_evaluate_object(NULL, "\\_SB.ATKD.ECRW",
¶ms, NULL);
if (status != AE_OK) printk("_SB.ATKD.ECRW error\n");
}
static void timer_handler(struct work_struct *work)
{
static int prev_zone = 0;
int curr_zone = 0;
int tmp;
tmp = get_temp();
if (unlikely(tmp == -1))
return;
if(tmp >= zone[NUM_ZONES-1].tmp)
goto out;
//Set fan speed and save previous zone
for(curr_zone=0; curr_zone<(NUM_ZONES-1); curr_zone++)
if(tmp < zone[curr_zone].tmp)
break;
if(unlikely(curr_zone < prev_zone &&
tmp > zone[curr_zone].tmp - TMP_DIFF )) {
set_fan_speed(zone[prev_zone].speed);
asusfan_curr_speed = zone[prev_zone].speed;
if (asusfan_verbose > 0)
printk("tmp = %d, curr zone %d, prev zone %d, speed set to %d\n",
tmp, curr_zone, prev_zone, zone[prev_zone].speed);
}
else {
set_fan_speed(zone[curr_zone].speed);
asusfan_curr_speed = zone[curr_zone].speed;
prev_zone = curr_zone;
if (asusfan_verbose > 0)
printk("tmp = %d, curr zone %d, prev zone %d, speed set to %d\n",
tmp, curr_zone, prev_zone, zone[curr_zone].speed);
}
asusfan_curr_zone = curr_zone;
asusfan_prev_zone = prev_zone;
out:
queue_delayed_work(wqs, &ws, (TIMER_FREQ + zone[curr_zone].sleep) * HZ);
}
/**
*
* "Parabolic" tolerance curve :
*
* '
* \ '
* \ . . /
* \ |<---stable-->| /
* \ | | / not stable (ascending)
* ------ \| |/ +++++
*________________\______T______/______________
* |'. .'|
* not stable | '. .' |
* (descending) | '---' |
* | '-> T + stable_range
* |
* |
* '-> T - stable_range
*
*
*/
static void temp_status_timer(struct work_struct *work)
{
int i = 0;
int diff = 0;
samples[fan_sample++] = get_temp();
if ((fan_sample % ASUSFAN_TEMP_NUM_SAMPLES) == 0)
{
/** Last sample - first sample */
diff = (samples[fan_num_samples - 1] - samples[0]) / fan_sample;
if(asusfan_verbose > 1)
printk("samples[0] = %d , samples[%d] = %d, diff = %d\n",
samples[0], fan_num_samples - 1, samples[fan_num_samples-1],
diff);
if((diff == 0 ) || ((diff <= asusfan_stable_range) &&
( diff >= -asusfan_stable_range)))
{
__thermal_status = stable;
snprintf(asusfan_temp_statusname, 16, status_name[0]);
}
else if(diff > asusfan_stable_range )
{
__thermal_status = ascending;
snprintf(asusfan_temp_statusname, 16, status_name[1]);
}
else if(diff < -asusfan_stable_range )
{
__thermal_status = descending;
snprintf(asusfan_temp_statusname, 16, status_name[2]);
}
if(asusfan_verbose > 2) {
for(i = 0; i < fan_num_samples ; i++) {
printk("samples[%d] = %d ", i, samples[i]);
}
printk("\n");
}
/* reset sample counter */
fan_sample = 0;
}
queue_delayed_work(wqst, &wst, MONITOR_FREQ);
}
static int asus_fan_init(void)
{
memset(&samples, 0, fan_num_samples);
//Workqueue settings
wqs = create_singlethread_workqueue("tmp");
queue_delayed_work(wqs, &ws, HZ);
wqst = create_singlethread_workqueue("sampler");
queue_delayed_work(wqst, &wst, HZ);
printk("Asus Advanced Fan Control version 0.8\n");
return 0;
}
static void asus_fan_exit(void)
{
cancel_delayed_work(&ws);
flush_workqueue(wqs);
destroy_workqueue(wqs);
cancel_delayed_work(&wst);
flush_workqueue(wqst);
destroy_workqueue(wqst);
printk("Asus Fan Control driver unloaded\n");
}
module_init(asus_fan_init);
module_exit(asus_fan_exit);