MichaIng / DietPi

Lightweight justice for your single-board computer!
https://dietpi.com/
GNU General Public License v2.0
4.88k stars 497 forks source link

DietPi-Config | Fan control #1818

Open MichaIng opened 6 years ago

MichaIng commented 6 years ago

At least for Odroid XU4 this is quite easy:

For RPi there was some similarly easy way.

Would be nice to have it inside DietPi-Config where applicable/easy to accomplish.

enwi commented 6 years ago

@MichaIng You do know that the HC1 and HC2 do not have any connections for a fan?

MichaIng commented 6 years ago

@enwi Good hint. Although the board itself seems to have, according to forum user.

As HC1/2 is the same board, thus we cannot estimate any difference, AFAIK, we can still add the option. Of course it does not have any effect if no fan is attached.

And theoretically you could attach one with some case modification ;) (?).

enwi commented 6 years ago

@MichaIng Yeah that's what I did. I am using a USB fan, that I've attached to the metal extrusion. But to not confuse anybody there is no other way of adding a fan to HC1/2.

MichaIng commented 6 years ago

I am working on it. At least for testing/debug reasons it contains more code than I planned 🤣: https://github.com/Fourdee/DietPi/pull/1978

@enwi Did the guide actually work as expected for you, something to take care about? I have no Odroid to test, hope it is the same for all Odroids (whether they have native fan support by case or not). I doubt that this is the same for all SBCs and x86_64. Rough research lead to many different methods, also based on how the fan is attached. My aim is to release it for Odroids (in case XU4/HC1/HC2 board) first and then add support for other hardware step by step.

enwi commented 6 years ago

@MichaIng On the HC1 and HC2 you can mount a fan to the metal extrusion. The XU4 either comes with a fan mounted or with a larger heatsink. All other Odroids except C0 have a heatsink where you can probably attach a fan afaik.

@MichaIng You do know that the HC1 and HC2 do not have any connections for a fan?

Also with that I meant that there is no way you can connect any cable to the board to control the fan since it only has a connection for a serial interface and a battery. See this link.

MichaIng commented 6 years ago

@enwi Many thanks for the infos. Ah, so you do not control your fan in this case, leaving it at static (max) speed?

Just checked. Yeah, I thought XU4/HC1/HC2 are exactly the same, but the board layout/features are not. XU4 comes with separate fan connector, HC1/2 do not. There is a similar connector, but that's the RTC backup battery 😉. For other boards, there are USB fans offered. Needs check how/if they can be controlled.

For RPi there are several guides for using GPIO pins for fan control, but this needs a separate controller board then. We could add a default (software) interface and configuration for this to the script. But this needs someone who uses this setup and knows a bid how similar control (defined temperature limits, where the fan runs at defined speeds) can be achieved.

For general use, this turns out way more complicated then I though 🤣. So far this then just works for XU4 natively. At least I hope that other boards with this fan connector work similar or identical. Especially for native PCs this could be also interesting. However, is still fun to write this, using our most current coding knowledge and methods to create menus, error handling etc.

enwi commented 6 years ago

@MichaIng That's correct, the fan has two speed settings that can be selected with a switch and it is running at the low setting, which is plenty to cool the HC1.

Fourdee commented 6 years ago

Testing:

Native PC / Intel

[  OK  ] DietPi-Set_CPU_fan | Root access verified.
/DietPi/dietpi/func/dietpi-set_cpu_fan: line 47: /sys/devices/platform/pwm-fan/hwmon/hwmon0/automatic: No such file or directory
/sys/devices/platform/pwm-fan/hwmon/hwmon0/automatic: 
/sys/devices/virtual/thermal/thermal_zone0/trip_point_0_temp: 100000
/DietPi/dietpi/func/dietpi-set_cpu_fan: line 49: /sys/devices/platform/pwm-fan/hwmon/hwmon0/fan_speed: No such file or directory
/sys/devices/platform/pwm-fan/hwmon/hwmon0/fan_speed: 
/DietPi/dietpi/func/dietpi-set_cpu_fan: line 50: /sys/devices/platform/pwm-fan/hwmon/hwmon0/pwm1: No such file or directory
/sys/devices/platform/pwm-fan/hwmon/hwmon0/pwm1: 
[FAILED] DietPi-Set_CPU_fan | CPU fan control is not available on your device. Aborting...
root@DietPi:~# cat /sys/devices/platform/
ACPI0003:00/       INT33A1:00/        PNP0C14:00/        snd-soc-dummy/
ACPI000C:00/       microcode/         PNP0C14:01/        thinkpad_acpi/
alarmtimer/        pcspkr/            PNP0C14:02/        thinkpad_hwmon/
coretemp.0/        PNP0C0A:00/        PNP0C14:03/        uevent
efi-framebuffer.0/ PNP0C0C:00/        power/             
efivars.0/         PNP0C0D:00/        regulatory.0/      
i8042/             PNP0C0E:00/        serial8250/     
root@DietPi:~# find /sys -name *fan*
/sys/kernel/debug/tracing/events/syscalls/sys_enter_fanotify_init
/sys/kernel/debug/tracing/events/syscalls/sys_exit_fanotify_init
/sys/kernel/debug/tracing/events/syscalls/sys_enter_fanotify_mark
/sys/kernel/debug/tracing/events/syscalls/sys_exit_fanotify_mark
/sys/devices/platform/thinkpad_hwmon/fan1_input
/sys/bus/platform/drivers/thinkpad_hwmon/fan_watchdog
/sys/module/thinkpad_acpi/parameters/fan_control
/sys/module/rcutree/parameters/rcu_fanout_exact
/sys/module/rcutree/parameters/rcu_fanout_leaf

Trail and error:

root@DietPi:~# echo 1 > /sys/bus/platform/drivers/thinkpad_hwmon/fan_watchdog
bash: echo: write error: Operation not permitted
root@DietPi:~# echo 1 > /sys/module/thinkpad_acpi/parameters/fan_control
bash: /sys/module/thinkpad_acpi/parameters/fan_control: Permission denied

root@DietPi:~# cat /proc/acpi/ibm/fan 
status:     enabled
speed:      0
level:      auto

https://www.kernel.org/doc/Documentation/laptops/thinkpad-acpi.txt The fan may be enabled or disabled with the following commands:

echo enable  >/proc/acpi/ibm/fan
echo disable >/proc/acpi/ibm/fan

Placing a fan on level 0 is the same as disabling it. Enabling a fan will try to place it in a safe level if it is too slow or disabled.

The fan level can be controlled with the command:

echo 'level <level>' > /proc/acpi/ibm/fan
root@DietPi:~# echo 'enable' > /proc/acpi/ibm/fan
bash: echo: write error: Invalid argument

NanoPC T2/3

root@DietPi:~# /DietPi/dietpi/func/dietpi-set_cpu_fan
[  OK  ] DietPi-Set_CPU_fan | Root access verified.
/DietPi/dietpi/func/dietpi-set_cpu_fan: line 47: /sys/devices/platform/pwm-fan/hwmon/hwmon0/automatic: No such file or directory
/sys/devices/platform/pwm-fan/hwmon/hwmon0/automatic:
/sys/devices/virtual/thermal/thermal_zone0/trip_point_0_temp: 80000
/DietPi/dietpi/func/dietpi-set_cpu_fan: line 49: /sys/devices/platform/pwm-fan/hwmon/hwmon0/fan_speed: No such file or directory
/sys/devices/platform/pwm-fan/hwmon/hwmon0/fan_speed:
/DietPi/dietpi/func/dietpi-set_cpu_fan: line 50: /sys/devices/platform/pwm-fan/hwmon/hwmon0/pwm1: No such file or directory
/sys/devices/platform/pwm-fan/hwmon/hwmon0/pwm1:
[FAILED] DietPi-Set_CPU_fan | CPU fan control is not available on your device. Aborting...
root@DietPi:~# cat /sys/devices/platform/
alarmtimer/           nexell-ion@0/         regulatory.0/
board/                nexell-pcm/           snd-soc-dummy/
bt_bcm/               nx-v4l2/              sound/
c0000000.soc/         panel-friendlyelec/   spdif-out/
c0010000.oscillator/  pmu/                  uevent
c0018000.onewire_pwm/ power/                wlan/
cpu/                  psci/
gpio_keys/            reg-dummy/
root@DietPi:~# find /sys -name '*fan*'
/sys/kernel/slab/fanotify_event_info
/sys/module/rcutree/parameters/rcu_fanout_exact
/sys/module/rcutree/parameters/rcu_fanout_leaf
root@DietPi:~# find /sys -name '*pwm*'
/sys/bus/platform/devices/c0018000.onewire_pwm
/sys/bus/platform/devices/c0018000.pwm
/sys/bus/platform/drivers/pwm-backlight
/sys/bus/platform/drivers/samsung-pwm
/sys/bus/platform/drivers/samsung-pwm/c0018000.pwm
/sys/bus/platform/drivers/onewire_ts/c0018000.onewire_pwm
/sys/devices/platform/c0018000.onewire_pwm
/sys/devices/platform/c0000000.soc/c0018000.pwm
/sys/devices/platform/c0000000.soc/c0018000.pwm/pwm
/sys/devices/platform/c0000000.soc/c0018000.pwm/pwm/pwmchip0
/sys/devices/platform/c0000000.soc/c0018000.pwm/pwm/pwmchip0/npwm
/sys/class/pwm
/sys/class/pwm/pwmchip0
/sys/firmware/devicetree/base/soc/pwm@c0018000
/sys/firmware/devicetree/base/soc/pwm@c0018000/samsung,pwm-outputs
/sys/firmware/devicetree/base/soc/pwm@c0018000/#pwm-cells
/sys/firmware/devicetree/base/soc/pinctrl@C0010000/pwm0
/sys/firmware/devicetree/base/soc/pinctrl@C0010000/pwm1
/sys/firmware/devicetree/base/soc/pinctrl@C0010000/pwm2
/sys/firmware/devicetree/base/soc/pinctrl@C0010000/pwm3
/sys/firmware/devicetree/base/soc/clocks/pwm2@c00be000
/sys/firmware/devicetree/base/soc/clocks/pwm3@c00be000
/sys/firmware/devicetree/base/soc/clocks/pwm0@c00ba000
/sys/firmware/devicetree/base/soc/clocks/pwm1@c00ba000
/sys/firmware/devicetree/base/onewire_pwm
/sys/firmware/devicetree/base/onewire_pwm/pwms
/sys/kernel/debug/clk/pwm0
/sys/kernel/debug/clk/pwm1
/sys/kernel/debug/clk/pwm2
/sys/kernel/debug/clk/pwm3
/sys/kernel/debug/pwm
MichaIng commented 6 years ago

@Fourdee Oh my god, every manufacturer has it's own fan interface it seems. Yours for IBM/Lenovo/ThinkPad I guess... 🤔

What is inside there?

cat /sys/devices/platform/thinkpad_hwmon/fan1_input
cat /sys/module/thinkpad_acpi/parameters/fan_control

level: auto

I guess auto means temp controlled, but can we adjust this, somehow attach temp to fan speeds?


Okay was reading the link a bid. There are several interfaces on how to control the fan on IBM/Lenovo/ThinkPad. What is most similar to XU4 is the following:

Sysfs ``` Sysfs notes: The sysfs interface follows the hwmon subsystem guidelines for the most part, and the exception is the fan safety watchdog. Writes to any of the sysfs attributes may return the EINVAL error if that operation is not supported in a given ThinkPad or if the parameter is out-of-bounds, and EPERM if it is forbidden. They may also return EINTR (interrupted system call), and EIO (I/O error while trying to talk to the firmware). Features not yet implemented by the driver return ENOSYS. hwmon device attribute pwm1_enable: 0: PWM offline (fan is set to full-speed mode) 1: Manual PWM control (use pwm1 to set fan level) 2: Hardware PWM control (EC "auto" mode) 3: reserved (Software PWM control, not implemented yet) Modes 0 and 2 are not supported by all ThinkPads, and the driver is not always able to detect this. If it does know a mode is unsupported, it will return -EINVAL. hwmon device attribute pwm1: Fan level, scaled from the firmware values of 0-7 to the hwmon scale of 0-255. 0 means fan stopped, 255 means highest normal speed (level 7). This attribute only commands the fan if pmw1_enable is set to 1 (manual PWM control). hwmon device attribute fan1_input: Fan tachometer reading, in RPM. May go stale on certain ThinkPads while the EC transitions the PWM to offline mode, which can take up to two minutes. May return rubbish on older ThinkPads. hwmon device attribute fan2_input: Fan tachometer reading, in RPM, for the secondary fan. Available only on some ThinkPads. If the secondary fan is not installed, will always read 0. hwmon driver attribute fan_watchdog: Fan safety watchdog timer interval, in seconds. Minimum is 1 second, maximum is 120 seconds. 0 disables the watchdog. To stop the fan: set pwm1 to zero, and pwm1_enable to 1. To start the fan in a safe mode: set pwm1_enable to 2. If that fails with EINVAL, try to set pwm1_enable to 1 and pwm1 to at least 128 (255 would be the safest choice, though). ```

Most importantly:

NOTE NOTE NOTE: fan control operations are disabled by default for
safety reasons.  To enable them, the module parameter "fan_control=1"
must be given to thinkpad-acpi.

So we can hope, that for most x86_64 systems, the above interface is at least similar. I am thinking about installing DietPi on a separate partition/drive of my laptop 🤔. HP Probook would be it.

Fourdee commented 6 years ago

@MichaIng

root@DietPi:~# cat /sys/devices/platform/thinkpad_hwmon/fan1_input
0
root@DietPi:~# cat /sys/module/thinkpad_acpi/parameters/fan_control
N

root@DietPi:~# echo 1 > /sys/module/thinkpad_acpi/parameters/fan_control
bash: /sys/module/thinkpad_acpi/parameters/fan_control: Permission denied
MichaIng commented 6 years ago

@Fourdee Does this work? echo 0 > /sys/bus/platform/drivers/thinkpad_hwmon/pwm1_enable Fan slowly speeds up until full speed...

echo 1 > /sys/bus/platform/drivers/thinkpad_hwmon/pwm1_enable
echo 0 > /sys/bus/platform/drivers/thinkpad_hwmon/pwm1

Fan slowly speeds down until stop... ...possibly after some time, it speeds up again due to watchdog. In this case:

echo 0 > /sys/bus/platform/drivers/thinkpad_hwmon/fan_watchdog
echo 1 > /sys/bus/platform/drivers/thinkpad_hwmon/pwm1_enable
echo 0 > /sys/bus/platform/drivers/thinkpad_hwmon/pwm1

...should lead to fan stays off. echo 2 > /sys/bus/platform/drivers/thinkpad_hwmon/pwm1_enable Fan, depending on temperature, turns on and auto adjusts speed to temp/usage. echo 1 > /sys/bus/platform/drivers/thinkpad_hwmon/fan_watchdog Just to be failsafe, when enabling auto mode anyway.


Btw.: I think before releasing this, it makes sense to do some more testing on Odroid XU4 (where the current implementation should work with) and add at least some more common x86_64 setups, which will quite take some time for research test and coding, since the fan control interfaces seem to be more different than expected. As there are still other topics to test/tune/finish, move this to v6.14? Or do an Odroid XU4-only release? (XU3 might be working as well)

MichaIng commented 6 years ago

@Fourdee Let's move to v6.14. This is no urgent thing and if release, it should be more than XU4-only 😉. Initial branch is still in place, so we can re-add it at any time.

MichaIng commented 5 years ago

Moved from own branch to main branch line .meta/ dir: https://github.com/Fourdee/DietPi/commit/2d9e9e3bd40daa15a10075490baedd4386bf7242

MichaIng commented 4 years ago

ROCKPro64 uses /sys/class/hwmon/hwmon3/: https://dietpi.com/phpbb/viewtopic.php?p=26996#p26996 Not sure if there is a generic way to detect the correct tuneables, probably we need to implement it per-device 😞.

cocoflan commented 3 years ago

Some days ago i tested an I2C cooling had, works perfectly on Dietpi, just needed some finetuning to get it work.

Python or C based control settings.

https://github.com/YahboomTechnology/Raspberry-Pi-RGB-Cooling-HAT

https://github.com/vookimedlo/yahboom_RGB_Cooling_HAT

MichaIng commented 3 years ago

Pinging @Rick847 here and #4454, with the idea to make this more generic, toggling a GPIO-attached fan based on CPU temperature, where no native PWM fan socket + kernel interface is available.

Joulinar commented 3 years ago

this one we have as well https://github.com/MichaIng/DietPi/issues/4003

MichaIng commented 3 years ago

True. Again very different (much simpler) to configure, but when we write such a script, why not adding every case of fan controls into it. But I think I'll add the RPi case fan to dietpi-config > Advanced Options earlier, as it is all together just a few lines of code, not comparable to the other solutions. Also while the others work OOTB, so can be live-controlled, for RPi fan changes via dtoverlay, a reboot is always required.