From a902e6a657e0807da0bb5d154db2e208b7f567ef Mon Sep 17 00:00:00 2001
From: =?utf8?q?Daniel=20Gonz=C3=A1lez=20Cabanelas?= <dgcbueu@gmail.com>
Date: Sun, 28 Jun 2020 23:52:10 +0200
Subject: [PATCH] mvebu: LS421DE: use automatic fan control with thermal zones
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

The Buffalo Linkstation LS421DE has a chassis fan for cooling two internal
hard drives. Currently there is no control over this fan, running always
at fixed medium speed.

With the recent jump to the kernel 5.4, now we can monitor the hard drive
temperature and control the fan with thermal zones.

Install the kmod-hwmon-drivetemp module and wire up a thermal zone on the
dts file to allow automatic fan control by the kernel.

Tested succesfully using a single Crucial BX500 SSD drive.

Signed-off-by: Daniel González Cabanelas <dgcbueu@gmail.com>
---
 .../boot/dts/armada-370-buffalo-ls421de.dts   | 65 ++++++++++++++++++-
 target/linux/mvebu/image/cortexa9.mk          |  5 +-
 2 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/target/linux/mvebu/files/arch/arm/boot/dts/armada-370-buffalo-ls421de.dts b/target/linux/mvebu/files/arch/arm/boot/dts/armada-370-buffalo-ls421de.dts
index 4d2760eb72..ea392f9e41 100644
--- a/target/linux/mvebu/files/arch/arm/boot/dts/armada-370-buffalo-ls421de.dts
+++ b/target/linux/mvebu/files/arch/arm/boot/dts/armada-370-buffalo-ls421de.dts
@@ -11,6 +11,7 @@
 #include "mvebu-linkstation-fan.dtsi"
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/thermal/thermal.h>
 
 / {
 	model = "Buffalo LinkStation LS421DE";
@@ -40,10 +41,60 @@
 			  MBUS_ID(0x09, 0x01) 0 0xf1100000 0x10000>;
 	};
 
-	gpio_fan {
+	system_fan: gpio_fan {
 		gpios = <&gpio0 13 GPIO_ACTIVE_HIGH
 			 &gpio0 14 GPIO_ACTIVE_HIGH>;
 		alarm-gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>;
+
+		#cooling-cells = <2>;
+	};
+
+	thermal-zones {
+		chassis-thermal {
+			polling-delay = <20000>;
+			polling-delay-passive = <2000>;
+
+			thermal-sensors = <&hdd0_temp>, <&hdd1_temp>;
+			coefficients = <1 1>;
+
+			trips {
+				chassis_alert1: chassis-alert1 {
+					temperature = <36000>;
+					hysteresis = <2000>;
+					type = "active";
+				};
+				chassis_alert2: chassis-alert2 {
+					temperature = <46000>;
+					hysteresis = <2000>;
+					type = "active";
+				};
+				chassis_alert3: chassis-alert3 {
+					temperature = <56000>;
+					hysteresis = <2000>;
+					type = "passive";
+				};
+				chassis_crit: chassis-crit {
+					temperature = <66000>;
+					hysteresis = <2000>;
+					type = "critical";
+				};
+			};
+
+			cooling-maps {
+				map1 {
+					trip = <&chassis_alert1>;
+					cooling-device = <&system_fan THERMAL_NO_LIMIT 1>;
+				};
+				map2 {
+					trip = <&chassis_alert2>;
+					cooling-device = <&system_fan 2 2>;
+				};
+				map3 {
+					trip = <&chassis_alert3>;
+					cooling-device = <&system_fan 3 THERMAL_NO_LIMIT>;
+				};
+			};
+		};
 	};
 
 	gpio_keys {
@@ -225,6 +276,18 @@
 &sata {
 	nr-ports = <2>;
 	status = "okay";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	hdd0_temp: sata-port@0 {
+		reg = <0>;
+		#thermal-sensor-cells = <0>;
+	};
+
+	hdd1_temp: sata-port@1 {
+		reg = <1>;
+		#thermal-sensor-cells = <0>;
+	};
 };
 
 &sdio {
diff --git a/target/linux/mvebu/image/cortexa9.mk b/target/linux/mvebu/image/cortexa9.mk
index f01a76a9d3..7f0a2fe697 100644
--- a/target/linux/mvebu/image/cortexa9.mk
+++ b/target/linux/mvebu/image/cortexa9.mk
@@ -17,8 +17,9 @@ define Device/buffalo_ls421de
   KERNEL_INITRAMFS := kernel-bin | append-dtb | uImage none
   DEVICE_DTS := armada-370-buffalo-ls421de
   DEVICE_PACKAGES :=  \
-    kmod-rtc-rs5c372a kmod-hwmon-gpiofan kmod-usb3 kmod-md-raid0 \
-    kmod-md-raid1 kmod-md-mod kmod-fs-xfs mkf2fs e2fsprogs partx-utils
+    kmod-rtc-rs5c372a kmod-hwmon-gpiofan kmod-hwmon-drivetemp kmod-usb3 \
+    kmod-md-raid0 kmod-md-raid1 kmod-md-mod kmod-fs-xfs mkf2fs e2fsprogs \
+    partx-utils
 endef
 TARGET_DEVICES += buffalo_ls421de
 
-- 
2.30.2