https://www.thinkwiki.org/w/api.php?action=feedcontributions&user=24.129.109.79&feedformat=atomThinkWiki - User contributions [en]2024-03-28T10:46:47ZUser contributionsMediaWiki 1.31.12https://www.thinkwiki.org/w/index.php?title=Patch_for_controlling_fan_speed&diff=13981Patch for controlling fan speed2006-01-03T01:45:04Z<p>24.129.109.79: /* Models on which this patch works */</p>
<hr />
<div>==Overview==<br />
<br />
This patch extends the [[ibm-acpi]] Linux kernel module to control fan speed. It can be used to reduce [[Problem with fan noise|fan noise]] (both speed and pulsing) and to decrease fan power consumption.<br />
<br />
When this patch is applied and the <tt>ibm-acpi</tt> module is loaded with the <tt>experimental=1</tt> module parameter, the following new capabilities are added to {{path|/proc/acpi/ibm/fan}}:<br />
* {{cmdroot|echo level LEVEL > /proc/acpi/ibm/fan}} sets a fan speed level between 0 and 7, where <tt>LEVEL</TT>=0 means fan off and <tt>LEVEL</tt>=7 is the fastest speed.<br />
* {{cmdroot|echo level auto > /proc/acpi/ibm/fan}} tells the embedded controller to set the fan speed automatically according to system temperatures (this is the default).<br />
* {{cmdroot|echo level disengaged > /proc/acpi/ibm/fan}} tells the embedded controller to disengage fan speed control (see specs below).<br />
* {{cmdroot|cat /proc/acpi/ibm/fan}} shows the current fan level (in addition to the fan speed in RPM).<br />
<br />
For example:<br />
<br />
'''#cat /proc/acpi/ibm/fan'''<br />
status: enabled<br />
level: auto<br />
speed: 4219<br />
commands: enable, disable, level <level><br />
(<level> is 0-7, auto or disengaged)<br />
<br />
'''#echo level 2 > /proc/acpi/ibm/fan'''<br />
<br />
'''#cat /proc/acpi/ibm/fan'''<br />
status: enabled<br />
level: 2<br />
speed: 3142<br />
commands: enable, disable, level <level><br />
(<level> is 0-7, auto or disengaged)<br />
<br />
This patch is best used with an [[ACPI fan control script#Variable speed control scripts|ACPI fan control script]] that monitors system temperature and sets the fan speed accordingly.<br />
<br />
{{WARN|Overriding the system's automatic temperature control may cause permanent hardware damage. Even when using temperature monitoring software, it is not clear whether the software can access all temperature sensor accessible to the embedded controller and understand them correctly. Moreover, this patch relies on an undocumented hardware interface, and may thus have arbitrary effects (especially on models it wasn't tested on).}}<br />
<br />
[http://www.example.com link title]==Models on which this patch works==<br />
<br />
* ThinkPad {{T43}}, {{T43p}} (fan levels RPM: 1-2 = ~3300, 3-5 = ~4100, 6-7 = ~4700; disengaged mode '''works)<br />
* ThinkPad''' {{T42}}, {{T42p}} (fan levels RPM: 1-2 = ~2900, 3-5 = ~3700, 6-7 = ~4700)<br />
* ThinkPad {{T41}}, {{T41p}} (fan levels RPM: 1-2 = ~2980, 3-5 = ~3500, 6-7 = ~4050; disengaged mode works at ~5100)<br />
* ThinkPad {{T40}} (fan levels RPM: 1-2 = ~2950, 3-5 = ~3600, 6-7 = ~4050)<br />
* ThinkPad {{T23}}<br />
* ThinkPad {{R52}}<br />
* ThinkPad {{R50}} (highest manual level is 3; disengage mode works and reaches much higher RPM)<br />
* ThinkPad {{X41T}}<br />
* ThinkPad {{X31}}<br />
* ThinkPad {{Z60t}} (fan levels RPM: 1-2 = ~1700, 3-5 = ~2800, 6-7 = ~3500)<br />
<br />
And probably additional models.<br />
<br />
{{NOTE|If you try this patch or the equivalent functionality in the [[ACPI_fan_control_script#Variable_speed_control_scripts|ACPI fan control scripts]], please update this page with your results.}}<br />
<br />
==Models on which this patch doesn't work==<br />
<br />
* ThinkPad {{600E}}, {{600X}}, {{770E}}, {{770X}} (these use a different fan control interface)<br />
* ThinkPad {{560}} (these models don't have a fan)<br />
<br />
==The patch==<br />
<br />
===for ibm-acpi 0.11===<br />
<br />
This also includes a minor fix (rename of <tt>device_add</tt>) to make [[ibm-acpi]] 0.11 compile on kernel 2.6.13.<br />
<br />
<pre><br />
--- ibm-acpi-0.11-orig/ibm_acpi.c 2005-03-17 12:06:16.000000000 +0200<br />
+++ ibm-acpi-0.11/ibm_acpi.c 2005-10-26 06:21:57.000000000 +0200<br />
@@ -1488,11 +1488,18 @@ static int fan_read(char *p)<br />
} else {<br />
/* all except 570, 600e/x, 770e, 770x */<br />
if (!acpi_ec_read(fan_status_offset, &status))<br />
len += sprintf(p + len, "status:\t\tunreadable\n");<br />
- else<br />
+ else {<br />
len += sprintf(p + len, "status:\t\t%s\n",<br />
- enabled(status, 7));<br />
+ status ? "enabled" : "disabled");<br />
+ if (status & 0x40)<br />
+ len += sprintf(p + len, "level:\t\tdisengaged\n");<br />
+ else if (status & 0x80)<br />
+ len += sprintf(p + len, "level:\t\tauto\n");<br />
+ else<br />
+ len += sprintf(p + len, "level:\t\t%d\n", status);<br />
+ }<br />
<br />
if (!acpi_ec_read(fan_rpm_offset, &lo) ||<br />
!acpi_ec_read(fan_rpm_offset + 1, &hi))<br />
len += sprintf(p + len, "speed:\t\tunreadable\n");<br />
@@ -1506,9 +1513,12 @@ static int fan_read(char *p)<br />
len += sprintf(p + len, "commands:\tlevel <level>"<br />
" (<level> is 0-7)\n");<br />
if (!gfan_handle)<br />
/* all except 570, 600e/x, 770e, 770x */<br />
- len += sprintf(p + len, "commands:\tenable, disable\n");<br />
+ len += sprintf(p + len, <br />
+ "commands:\tenable, disable, level <level>\n"<br />
+ " \t(<level> is 0-7, auto"<br />
+ "or disengaged)\n");<br />
if (fans_handle)<br />
/* X31, X40 */<br />
len += sprintf(p + len, "commands:\tspeed <speed>"<br />
" (<speed> is 0-65535)\n");<br />
@@ -1528,17 +1538,29 @@ static int fan_write(char *buf)<br />
/* 570, 770x-JL */<br />
if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))<br />
return -EIO;<br />
} else if (!gfan_handle &&<br />
- strlencmp(cmd, "enable") == 0) {<br />
+ ( (strlencmp(cmd, "enable") == 0) ||<br />
+ (strlencmp(cmd, "level auto") == 0) ) ) {<br />
/* all except 570, 600e/x, 770e, 770x */<br />
if (!acpi_ec_write(fan_status_offset, 0x80))<br />
return -EIO;<br />
} else if (!gfan_handle &&<br />
strlencmp(cmd, "disable") == 0) {<br />
/* all except 570, 600e/x, 770e, 770x */<br />
if (!acpi_ec_write(fan_status_offset, 0x00))<br />
return -EIO;<br />
+ } else if (!gfan_handle &&<br />
+ strlencmp(cmd, "level disengaged") == 0) {<br />
+ /* all except 570, 600e/x, 770e, 770x */<br />
+ if (!acpi_ec_write(fan_status_offset, 0x40))<br />
+ return -EIO;<br />
+ } else if (!gfan_handle &&<br />
+ sscanf(cmd, "level %d", &level) == 1 &&<br />
+ level >=0 && level <= 7) {<br />
+ /* all except 570, 600e/x, 770e, 770x */<br />
+ if (!acpi_ec_write(fan_status_offset, level))<br />
+ return -EIO;<br />
} else if (fans_handle &&<br />
sscanf(cmd, "speed %d", &speed) == 1 &&<br />
speed >= 0 && speed <= 65535) {<br />
/* X31, X40 */<br />
@@ -1751,9 +1773,9 @@ static int __init setup_notify(struct ib<br />
<br />
return 0;<br />
}<br />
<br />
-static int device_add(struct acpi_device *device)<br />
+static int ibmacpi_device_add(struct acpi_device *device)<br />
{<br />
return 0;<br />
}<br />
<br />
@@ -1769,9 +1791,9 @@ static int __init register_driver(struct<br />
<br />
memset(ibm->driver, 0, sizeof(struct acpi_driver));<br />
sprintf(ibm->driver->name, "%s/%s", IBM_NAME, ibm->name);<br />
ibm->driver->ids = ibm->hid;<br />
- ibm->driver->ops.add = &device_add;<br />
+ ibm->driver->ops.add = &ibmacpi_device_add;<br />
<br />
ret = acpi_bus_register_driver(ibm->driver);<br />
if (ret < 0) {<br />
printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",<br />
</pre><br />
<br />
The [[User:Thinker|author]] disclaims all warranty for this patch, and releases it to the public domain (meaning you may use and further distribute it under any terms you wish, including incorporating it into other software).<br />
<br />
===for ibm-acpi 0.12a as found in kernel 2.6.14===<br />
<br />
A slightly modified version which also keeps the lines in the format expected by the gkrellm plugin.<br />
<br />
{{NOTE|In order to patch the ibm_acpi.c file you have tu issue the following command: <code>patch -p0 -l < fanpatch</code>. The '-l' option is important because if the patch pasted here doesn't have any tabs any more. Another advice: Always try to patch the files first before really patching it. That is done by adding --dry-run to the command.}}<br />
<br />
<br />
<pre><br />
--- drivers/acpi/ibm_acpi.c.orig 2005-11-01 19:47:44.262270250 +0100<br />
+++ drivers/acpi/ibm_acpi.c 2005-11-01 20:16:16.081252250 +0100<br />
@@ -1465,6 +1465,7 @@ static int fan_read(char *p)<br />
{<br />
int len = 0;<br />
int s;<br />
+ char status_read = 0;<br />
u8 lo, hi, status;<br />
<br />
if (gfan_handle) {<br />
@@ -1477,9 +1478,11 @@ static int fan_read(char *p)<br />
/* all except 570, 600e/x, 770e, 770x */<br />
if (!acpi_ec_read(fan_status_offset, &status))<br />
len += sprintf(p + len, "status:\t\tunreadable\n");<br />
- else<br />
+ else {<br />
len += sprintf(p + len, "status:\t\t%s\n",<br />
- enabled(status, 7));<br />
+ status ? "enabled" : "disabled");<br />
+ status_read = 1;<br />
+ }<br />
<br />
if (!acpi_ec_read(fan_rpm_offset, &lo) ||<br />
!acpi_ec_read(fan_rpm_offset + 1, &hi))<br />
@@ -1487,6 +1490,14 @@ static int fan_read(char *p)<br />
else<br />
len += sprintf(p + len, "speed:\t\t%d\n",<br />
(hi << 8) + lo);<br />
+ if (status_read) {<br />
+ if (status & 0x40)<br />
+ len += sprintf(p + len, "level:\t\tdisengaged\n");<br />
+ else if (status & 0x80)<br />
+ len += sprintf(p + len, "level:\t\tauto\n");<br />
+ else<br />
+ len += sprintf(p + len, "level:\t\t%d\n", status);<br />
+ }<br />
}<br />
<br />
if (sfan_handle)<br />
@@ -1495,7 +1506,10 @@ static int fan_read(char *p)<br />
" (<level> is 0-7)\n");<br />
if (!gfan_handle)<br />
/* all except 570, 600e/x, 770e, 770x */<br />
- len += sprintf(p + len, "commands:\tenable, disable\n");<br />
+ len += sprintf(p + len, <br />
+ "commands:\tenable, disable, level <level>\n"<br />
+ " \t(<level> is 0-7, auto "<br />
+ "or disengaged)\n");<br />
if (fans_handle)<br />
/* X31, X40 */<br />
len += sprintf(p + len, "commands:\tspeed <speed>"<br />
@@ -1516,7 +1530,8 @@ static int fan_write(char *buf)<br />
/* 570, 770x-JL */<br />
if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))<br />
return -EIO;<br />
- } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {<br />
+ } else if (!gfan_handle && ( (strlencmp(cmd, "enable") == 0) || <br />
+ (strlencmp(cmd, "level auto") == 0) ) ) {<br />
/* all except 570, 600e/x, 770e, 770x */<br />
if (!acpi_ec_write(fan_status_offset, 0x80))<br />
return -EIO;<br />
@@ -1524,6 +1539,17 @@ static int fan_write(char *buf)<br />
/* all except 570, 600e/x, 770e, 770x */<br />
if (!acpi_ec_write(fan_status_offset, 0x00))<br />
return -EIO;<br />
+ } else if (!gfan_handle &&<br />
+ strlencmp(cmd, "level disengaged") == 0) {<br />
+ /* all except 570, 600e/x, 770e, 770x */<br />
+ if (!acpi_ec_write(fan_status_offset, 0x40))<br />
+ return -EIO;<br />
+ } else if (!gfan_handle &&<br />
+ sscanf(cmd, "level %d", &level) == 1 &&<br />
+ level >=0 && level <= 7) {<br />
+ /* all except 570, 600e/x, 770e, 770x */<br />
+ if (!acpi_ec_write(fan_status_offset, level))<br />
+ return -EIO;<br />
} else if (fans_handle &&<br />
sscanf(cmd, "speed %d", &speed) == 1 &&<br />
speed >= 0 && speed <= 65535) {<br />
</pre><br />
<br />
The [[User:spiney|author]] disclaims all warranty for this patch, and releases it to the public domain (meaning you may use and further distribute it under any terms you wish, including incorporating it into other software).<br />
<br />
===Ideas for improvement===<br />
<br />
* When fan speed is controlled from userspace (e.g., by the [[ACPI fan control script#Variable speed control scripts|ACPI fan control scripts]]), the userspace component may die (for whatever reason) leaving the fan at a low speed, potentially leading to damage. We can add a watchdog to the kernel component, which resets the fan to Embedded Controller control (<tt>leve: auto</tt>) if {{path|/proc/acpi/ibm/fan}} was not written to for ''N'' seconds.<br />
<br />
==Controlling the fan without this patch==<br />
<br />
If you have the [[ibm-acpi]] module loaded with <tt>experimental=1</tt>, you can control the fan without patching the kernel by directly writing to the relevant embedded controller register using {{path|/proc/acpi/ibm/ecdump}}. <br />
<br />
For example:<br />
<br />
{{cmdroot|echo 0x2F 0x04 > /proc/acpi/ibm/ecdump}}<br />
<br />
will set the fan to manual mode 4 (see the specifications below).<br />
<br />
==Hardware specs==<br />
<br />
The patch relies on the following hardware behavior, which was discovered experimentally by [[User:Thinker|Thinker]] and neither provided by nor confirmed by IBM/Lenovo. The following description may be inaccurate and may vary by model (see list of models above). The terminology probably does not match the one used by IBM/Lenovo engineers.<br />
<br />
ACPI DSDT register _SB.PCI0.LPC.EC.HFSP (8 bits, offset 0x2F in the <tt>EmbeddedController</tt> address space, accessed through the standard EC interface at IO ports 0x62 and 0x66) is read/writable and has the following meaning:<br />
<br />
Bits 7 6 5 4 3 2 1 0<br />
---------------<br />
Value 1 0 * * * * * * - automatic<br />
* 1 * * * * * * - disengaged<br />
0 0 N N N N N N - manual (0..63; 0=disable fan, 1=min, ..., 7=max)<br />
<br />
In ''automatic'' mode, the embedded controller sets the fan speed automatically according to system temperatures and some unknown algorithm.<br />
<br />
In ''manual'' mode, the fan level is forced to the given value and the EC will auto-regulate the fan to maintain at a (roughly) constant RPM, which is model-dependent. Manual speed levels 8-63 yield the same behavior as level 7, and the the ACPI DSDT uses level 7 for the emergency mode it enters upon critical CPU/GPU temperature, so apparently 7 is the real maximum level.<br />
<br />
In ''disengaged'' mode, the embedded controller does not read the fan RPM (hence <tt>EmbeddedController</tt> offset 0x84 is not updated), and does not maintain a stable fan speed. When disengaged mode is entered the fan speed is not immediately changed (except if the fan was disabed, in which case it is turned on at a low level). However, once in disengaged mode, fan speed will slowly increase to ''beyond'' the maximum manual level (this may cause hardware damage!).<br />
<br />
After boot, the register may not reflect the true state of the EC (on some models it reads 0x07 even though the EC is actually in automatic mode).<br />
<br />
{{HINT|Apparently the [[Problem with fan noise|pulsing fan noise]] experienced by some users can be cured by repeatedly running 2-4 seconds of manual control followed by 0.5-1 seconds of disengaged mode. The pulse occurs when the the embedded controller computes the fan speed and adjusts the fan voltage adaptively every few seconds (~4.8sec for the ThinkPad T43); the aforementioned mode switching doesn't give it a chance to do so. One of the [[ACPI fan control script#Variable speed control scripts|ACPI fan control scripts]] implements this solution.}}<br />
<br />
==Tools based on this patch or specs==<br />
* For Linux: see [[ACPI fan control script#Variable speed control scripts|ACPI fan control scripts]].<br />
* For Windows: Shimodax's ThinkPad fan control tool (see the [http://forum.thinkpads.com/viewtopic.php?t=17715 forum discussion] at thinkpads.com).<br />
<br />
[[Category:Patches]]</div>24.129.109.79