Difference between revisions of "Patch for controlling fan speed"

From ThinkWiki
Jump to: navigation, search
(added line for models without fan)
m
 
(33 intermediate revisions by 11 users not shown)
Line 1: Line 1:
 
==Overview==
 
==Overview==
 +
 +
{{NOTE|The patch is no longer necessary with kernel > 2.6.10. After opening a root shell with '''$sudo -s''', you should be able to run al of these commands. See: [[How_to_control_fan_speed|How to control fan speed]]}}
 +
  
 
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.
 
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.
Line 30: Line 33:
  
 
{{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).}}
 
{{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).}}
 
==Models on which this patch works==
 
 
* ThinkPad {{T41}}<br />Speed levels in RPM: 1-2 = ~2980, 3-5 = ~3500, 6-7 = ~4050; disengaged mode works at ~5100)
 
* ThinkPad {{T43}}, {{T43p}}<br />Speed levels in RPM: 1-2 = ~3300, 3-5 = ~4100, 6-7 = ~4700; disengaged mode works)
 
 
Probably many other models.
 
 
{{NOTE|If you try this patch on other models, please update this page with your results.}}
 
 
==Models on which this patch doesn't work==
 
 
* ThinkPad {{600E}}, {{600X}}, {{770E}}, {{770X}} (these use a different fan control interface)
 
* ThinkPad {{560}} (these models don't have a fan)
 
  
 
==The patch==
 
==The patch==
  
 
===for ibm-acpi 0.11===
 
===for ibm-acpi 0.11===
 +
{{CodeRef|ibm-acpi-0.11-2.6.13-fan.patch}}
  
 
This also includes a minor fix (rename of <tt>device_add</tt>) to make [[ibm-acpi]] 0.11 compile on kernel 2.6.13.
 
This also includes a minor fix (rename of <tt>device_add</tt>) to make [[ibm-acpi]] 0.11 compile on kernel 2.6.13.
  
<pre>
+
===for ibm-acpi 0.12a as found in kernels 2.6.14 and 2.6.17===
--- ibm-acpi-0.11-orig/ibm_acpi.c 2005-03-17 12:06:16.000000000 +0200
 
+++ ibm-acpi-0.11/ibm_acpi.c 2005-10-26 06:21:57.000000000 +0200
 
@@ -1488,11 +1488,18 @@ static int fan_read(char *p)
 
} else {
 
/* all except 570, 600e/x, 770e, 770x */
 
if (!acpi_ec_read(fan_status_offset, &status))
 
len += sprintf(p + len, "status:\t\tunreadable\n");
 
- else
 
+ else {
 
len += sprintf(p + len, "status:\t\t%s\n",
 
-       enabled(status, 7));
 
+               status ? "enabled" : "disabled");
 
+ if (status & 0x40)
 
+ len += sprintf(p + len, "level:\t\tdisengaged\n");
 
+ else if (status & 0x80)
 
+ len += sprintf(p + len, "level:\t\tauto\n");
 
+ else
 
+ len += sprintf(p + len, "level:\t\t%d\n", status);
 
+ }
 
 
if (!acpi_ec_read(fan_rpm_offset,    &lo) ||
 
    !acpi_ec_read(fan_rpm_offset + 1, &hi))
 
len += sprintf(p + len, "speed:\t\tunreadable\n");
 
@@ -1506,9 +1513,12 @@ static int fan_read(char *p)
 
len += sprintf(p + len, "commands:\tlevel <level>"
 
      " (<level> is 0-7)\n");
 
if (!gfan_handle)
 
/* all except 570, 600e/x, 770e, 770x */
 
- len += sprintf(p + len, "commands:\tenable, disable\n");
 
+ len += sprintf(p + len,
 
+               "commands:\tenable, disable, level <level>\n"
 
+               "        \t(<level> is 0-7, auto"
 
+               "or disengaged)\n");
 
if (fans_handle)
 
/* X31, X40 */
 
len += sprintf(p + len, "commands:\tspeed <speed>"
 
      " (<speed> is 0-65535)\n");
 
@@ -1528,17 +1538,29 @@ static int fan_write(char *buf)
 
/* 570, 770x-JL */
 
if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
 
return -EIO;
 
} else if (!gfan_handle &&
 
-   strlencmp(cmd, "enable") == 0) {
 
+   ( (strlencmp(cmd, "enable") == 0) ||
 
+     (strlencmp(cmd, "level auto") == 0) ) ) {
 
/* all except 570, 600e/x, 770e, 770x */
 
if (!acpi_ec_write(fan_status_offset, 0x80))
 
return -EIO;
 
} else if (!gfan_handle &&
 
  strlencmp(cmd, "disable") == 0) {
 
/* all except 570, 600e/x, 770e, 770x */
 
if (!acpi_ec_write(fan_status_offset, 0x00))
 
return -EIO;
 
+ } else if (!gfan_handle &&
 
+   strlencmp(cmd, "level disengaged") == 0) {
 
+ /* all except 570, 600e/x, 770e, 770x */
 
+ if (!acpi_ec_write(fan_status_offset, 0x40))
 
+ return -EIO;
 
+ } else if (!gfan_handle &&
 
+     sscanf(cmd, "level %d", &level) == 1 &&
 
+     level >=0 && level <= 7) {
 
+ /* all except 570, 600e/x, 770e, 770x */
 
+ if (!acpi_ec_write(fan_status_offset, level))
 
+ return -EIO;
 
} else if (fans_handle &&
 
    sscanf(cmd, "speed %d", &speed) == 1 &&
 
    speed >= 0 && speed <= 65535) {
 
/* X31, X40 */
 
@@ -1751,9 +1773,9 @@ static int __init setup_notify(struct ib
 
 
return 0;
 
}
 
 
-static int device_add(struct acpi_device *device)
 
+static int ibmacpi_device_add(struct acpi_device *device)
 
{
 
return 0;
 
}
 
 
@@ -1769,9 +1791,9 @@ static int __init register_driver(struct
 
 
memset(ibm->driver, 0, sizeof(struct acpi_driver));
 
sprintf(ibm->driver->name, "%s/%s", IBM_NAME, ibm->name);
 
ibm->driver->ids = ibm->hid;
 
- ibm->driver->ops.add = &device_add;
 
+ ibm->driver->ops.add = &ibmacpi_device_add;
 
 
ret = acpi_bus_register_driver(ibm->driver);
 
if (ret < 0) {
 
printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
 
</pre>
 
 
 
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).
 
 
 
===for ibm-acpi 0.12a as found in kernel 2.6.14===
 
  
 
A slightly modified version which also keeps the lines in the format expected by the gkrellm plugin.
 
A slightly modified version which also keeps the lines in the format expected by the gkrellm plugin.
  
<pre>
+
{{CodeRef|ibm-acpi-0.12a-2.6.14-fan.patch}}
--- drivers/acpi/ibm_acpi.c.orig        2005-11-01 19:47:44.262270250 +0100
 
+++ drivers/acpi/ibm_acpi.c    2005-11-01 20:16:16.081252250 +0100
 
@@ -1465,6 +1465,7 @@ static int fan_read(char *p)
 
{
 
        int len = 0;
 
        int s;
 
+      char status_read = 0;
 
        u8 lo, hi, status;
 
  
        if (gfan_handle) {
+
{{CodeRef|ibm-acpi-0.12a-2.6.17-fan.patch}}
@@ -1477,9 +1478,11 @@ static int fan_read(char *p)
 
                /* all except 570, 600e/x, 770e, 770x */
 
                if (!acpi_ec_read(fan_status_offset, &status))
 
                        len += sprintf(p + len, "status:\t\tunreadable\n");
 
-               else
 
+              else {
 
                        len += sprintf(p + len, "status:\t\t%s\n",
 
-                                     enabled(status, 7));
 
+                                    status ? "enabled" : "disabled");
 
+                      status_read = 1;
 
+              }
 
  
                if (!acpi_ec_read(fan_rpm_offset, &lo) ||
+
{{NOTE|In order to patch the ibm_acpi.c file you have to issue the following command: <code>patch -p0 -l < fanpatch</code>. The '-l' option is important because the patch pasted here doesn't have any tabs any more. Another piece of advice: Always try to patch the files first before really patching them. That is done by adding --dry-run to the command.}}
                    !acpi_ec_read(fan_rpm_offset + 1, &hi))
 
@@ -1487,6 +1490,14 @@ static int fan_read(char *p)
 
                else
 
                        len += sprintf(p + len, "speed:\t\t%d\n",
 
                                      (hi << 8) + lo);
 
+              if (status_read) {
 
+                      if (status & 0x40)
 
+                              len += sprintf(p + len, "level:\t\tdisengaged\n");
 
+                      else if (status & 0x80)
 
+                              len += sprintf(p + len, "level:\t\tauto\n");
 
+                      else
 
+                              len += sprintf(p + len, "level:\t\t%d\n", status);
 
+              }
 
        }
 
  
        if (sfan_handle)
+
===Ideas for improvement===
@@ -1495,7 +1506,10 @@ static int fan_read(char *p)
 
                              " (<level> is 0-7)\n");
 
        if (!gfan_handle)
 
                /* all except 570, 600e/x, 770e, 770x */
 
-              len += sprintf(p + len, "commands:\tenable, disable\n");
 
+              len += sprintf(p + len,
 
+                            "commands:\tenable, disable, level <level>\n"
 
+                            "        \t(<level> is 0-7, auto "
 
+                            "or disengaged)\n");
 
        if (fans_handle)
 
                /* X31, X40 */
 
                len += sprintf(p + len, "commands:\tspeed <speed>"
 
@@ -1516,7 +1530,8 @@ static int fan_write(char *buf)
 
                        /* 570, 770x-JL */
 
                        if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
 
                                return -EIO;
 
-              } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
 
+              } else if (!gfan_handle && ( (strlencmp(cmd, "enable") == 0) ||
 
+                              (strlencmp(cmd, "level auto") == 0) ) ) {
 
                        /* all except 570, 600e/x, 770e, 770x */
 
                        if (!acpi_ec_write(fan_status_offset, 0x80))
 
                                return -EIO;
 
@@ -1524,6 +1539,17 @@ static int fan_write(char *buf)
 
                        /* all except 570, 600e/x, 770e, 770x */
 
                        if (!acpi_ec_write(fan_status_offset, 0x00))
 
                                return -EIO;
 
+              } else if (!gfan_handle &&
 
+                          strlencmp(cmd, "level disengaged") == 0) {
 
+                      /* all except 570, 600e/x, 770e, 770x */
 
+                      if (!acpi_ec_write(fan_status_offset, 0x40))
 
+                              return -EIO;
 
+              } else if (!gfan_handle &&
 
+                  sscanf(cmd, "level %d", &level) == 1 &&
 
+                  level >=0 && level <= 7) {
 
+                      /* all except 570, 600e/x, 770e, 770x */
 
+                      if (!acpi_ec_write(fan_status_offset, level))
 
+                              return -EIO;
 
                } else if (fans_handle &&
 
                          sscanf(cmd, "speed %d", &speed) == 1 &&
 
                          speed >= 0 && speed <= 65535) {
 
</pre>
 
  
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).
+
* 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.
 
 
==Controlling the fan without this patch==
 
 
 
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}}.
 
 
 
For example:
 
 
 
{{cmdroot|echo 0x2F 0x04 > /proc/acpi/ibm/ecdump}}
 
 
 
will set the fan to manual mode 4 (see the specifications below).
 
 
 
==Hardware specs==
 
 
 
The patch relies on the following hardware behavior, which was discovered experimentally 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.
 
 
 
ACPI DSDT register _SB.PCI0.LPC.EC.HFSP (8 bits, offset 0x2F in the <tt>EmbeddedController</tt> address space)
 
is read/writable and has the following meaning:
 
 
  7 6 5 4 3 2 1 0
 
  | | \_________/
 
  | |      |
 
  | |      +--------- manual fan speed level (0=disable, 1=min, ..., 7=max)
 
  | +---------------- disengaged (0=normal, 1=disengaged, overrides all)
 
  +------------------ automatic fan speed control (0=manual, 1=automatic, overrides manual)
 
  
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.
+
==Alternative methods and hardware specs==
  
When bit 7 is on, the embedded controller sets the fan speed automatically according to system temperaturesand some unknown algorithm. This overrides manual control.
+
See [[How to control fan speed]].
  
When bit 6 is on, 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. This overrides manual and automatic control. 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!).
+
==Supported models==
  
{{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.}}
+
See [[How to control fan speed]].
  
 
[[Category:Patches]]
 
[[Category:Patches]]

Latest revision as of 01:08, 24 March 2008

Overview

NOTE!
The patch is no longer necessary with kernel > 2.6.10. After opening a root shell with $sudo -s, you should be able to run al of these commands. See: How to control fan speed


This patch extends the ibm-acpi Linux kernel module to control fan speed. It can be used to reduce fan noise (both speed and pulsing) and to decrease fan power consumption.

When this patch is applied and the ibm-acpi module is loaded with the experimental=1 module parameter, the following new capabilities are added to /proc/acpi/ibm/fan:

  • # echo level LEVEL > /proc/acpi/ibm/fan sets a fan speed level between 0 and 7, where LEVEL=0 means fan off and LEVEL=7 is the fastest speed.
  • # 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).
  • # echo level disengaged > /proc/acpi/ibm/fan tells the embedded controller to disengage fan speed control (see specs below).
  • # cat /proc/acpi/ibm/fan shows the current fan level (in addition to the fan speed in RPM).

For example:

#cat /proc/acpi/ibm/fan
status:         enabled
level:          auto
speed:          4219
commands:       enable, disable, level <level>
                (<level> is 0-7, auto or disengaged)

#echo level 2 > /proc/acpi/ibm/fan

#cat /proc/acpi/ibm/fan
status:         enabled
level:          2
speed:          3142
commands:       enable, disable, level <level>
                (<level> is 0-7, auto or disengaged)

This patch is best used with an ACPI fan control script that monitors system temperature and sets the fan speed accordingly.

ATTENTION!
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).

The patch

for ibm-acpi 0.11

ibm-acpi-0.11-2.6.13-fan.patch (download)

This also includes a minor fix (rename of device_add) to make ibm-acpi 0.11 compile on kernel 2.6.13.

for ibm-acpi 0.12a as found in kernels 2.6.14 and 2.6.17

A slightly modified version which also keeps the lines in the format expected by the gkrellm plugin.

ibm-acpi-0.12a-2.6.14-fan.patch (download)

ibm-acpi-0.12a-2.6.17-fan.patch (download)

NOTE!
In order to patch the ibm_acpi.c file you have to issue the following command: patch -p0 -l < fanpatch. The '-l' option is important because the patch pasted here doesn't have any tabs any more. Another piece of advice: Always try to patch the files first before really patching them. That is done by adding --dry-run to the command.

Ideas for improvement

  • When fan speed is controlled from userspace (e.g., by the 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 (leve: auto) if /proc/acpi/ibm/fan was not written to for N seconds.

Alternative methods and hardware specs

See How to control fan speed.

Supported models

See How to control fan speed.