From 4bf62f8dc97e24dadda64762ab47c299ac6eb8cb Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.com>
Date: Tue, 3 May 2022 17:20:11 +0100
Subject: [PATCH] dtoverlays: Add i2c-fan overlay

Add an i2c-fan overlay, initially with support for the EMC2301
fan controller found on the CM4IO board.
Based on the overlay from Traverse Technologies, but modified for
the EMC2305 driver submitted to linux-hwmon.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 arch/arm/boot/dts/overlays/Makefile           |  1 +
 arch/arm/boot/dts/overlays/README             | 33 +++++++
 .../arm/boot/dts/overlays/i2c-fan-overlay.dts | 92 +++++++++++++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 arch/arm/boot/dts/overlays/i2c-fan-overlay.dts

--- a/arch/arm/boot/dts/overlays/Makefile
+++ b/arch/arm/boot/dts/overlays/Makefile
@@ -85,6 +85,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
 	hy28b-2017.dtbo \
 	i-sabre-q2m.dtbo \
 	i2c-bcm2708.dtbo \
+	i2c-fan.dtbo \
 	i2c-gpio.dtbo \
 	i2c-mux.dtbo \
 	i2c-pwm-pca9685a.dtbo \
--- a/arch/arm/boot/dts/overlays/README
+++ b/arch/arm/boot/dts/overlays/README
@@ -1530,6 +1530,39 @@ Load:   dtoverlay=i2c-bcm2708
 Params: <None>
 
 
+Name:   i2c-fan
+Info:   Adds support for a number of I2C fan controllers
+Load:   dtoverlay=i2c-fan,<param>=<val>
+Params: addr                    Sets the address for the fan controller. Note
+                                that the device must be configured to use the
+                                specified address.
+
+        i2c0                    Choose the I2C0 bus on GPIOs 0&1
+
+        i2c_csi_dsi             Choose the I2C0 bus on GPIOs 44&45
+
+        minpwm                  PWM setting for the fan when the SoC is below
+                                mintemp (range 0-255. default 0)
+        maxpwm                  PWM setting for the fan when the SoC is above
+                                maxtemp (range 0-255. default 255)
+        midtemp                 Temperature (in millicelcius) at which the fan
+                                begins to speed up (default 50000)
+
+        midtemp_hyst            Temperature delta (in millicelcius) below
+                                mintemp at which the fan will drop to minrpm
+                                (default 2000)
+
+        maxtemp                 Temperature (in millicelcius) at which the fan
+                                will be held at maxrpm (default 70000)
+
+        maxtemp_hyst            Temperature delta (in millicelcius) below
+                                maxtemp at which the fan begins to slow down
+                                (default 2000)
+
+        emc2301                 Select the Microchip EMC230x controller family
+                                - EMC2301, EMC2302, EMC2303, EMC2305.
+
+
 Name:   i2c-gpio
 Info:   Adds support for software i2c controller on gpio pins
 Load:   dtoverlay=i2c-gpio,<param>=<val>
--- /dev/null
+++ b/arch/arm/boot/dts/overlays/i2c-fan-overlay.dts
@@ -0,0 +1,92 @@
+// Definitions for I2C based sensors using the Industrial IO or HWMON interface.
+/dts-v1/;
+/plugin/;
+
+#include <dt-bindings/thermal/thermal.h>
+
+/ {
+	compatible = "brcm,bcm2835";
+
+	fragment@0 {
+		target = <&i2cbus>;
+		__dormant__ {
+			#address-cells = <1>;
+			#size-cells = <0>;
+			status = "okay";
+
+			emc2301: emc2301@2f {
+				compatible = "microchip,emc2301";
+				reg = <0x2f>;
+				status = "okay";
+				#cooling-cells = <0x02>;
+			};
+		};
+	};
+
+	frag100: fragment@100 {
+		target = <&i2c_arm>;
+		i2cbus: __overlay__ {
+			status = "okay";
+		};
+	};
+
+	fragment@101 {
+		target = <&i2c0if>;
+		__dormant__ {
+			status = "okay";
+		};
+	};
+
+	fragment@102 {
+		target = <&i2c0mux>;
+		__dormant__ {
+			status = "okay";
+		};
+	};
+
+	fragment@103 {
+		target = <&cpu_thermal>;
+		polling-delay = <2000>; /* milliseconds */
+		__overlay__ {
+			trips {
+				fanmid0: fanmid0 {
+					temperature = <50000>;
+					hysteresis = <2000>;
+					type = "active";
+				};
+				fanmax0: fanmax0 {
+					temperature = <75000>;
+					hysteresis = <2000>;
+					type = "active";
+				};
+			};
+			cooling-maps {
+				map0: map0 {
+					trip = <&fanmid0>;
+					cooling-device = <&emc2301 2 6>;
+				};
+				map1: map1 {
+					trip = <&fanmax0>;
+					cooling-device = <&emc2301 7 THERMAL_NO_LIMIT>;
+				};
+			};
+		};
+	};
+
+	__overrides__ {
+		i2c0 =		<&frag100>,"target:0=",<&i2c0>;
+		i2c_csi_dsi =	<&frag100>,"target:0=",<&i2c_csi_dsi>,
+				<0>,"+101+102";
+		addr =		<&emc2301>,"reg:0";
+		minpwm =	<&emc2301>,"emc2305,pwm-min;0";
+		maxpwm =	<&emc2301>,"emc2305,pwm-max;0";
+		midtemp =	<&fanmid0>,"temperature:0";
+		midtemp_hyst =	<&fanmid0>,"hysteresis:0";
+		maxtemp =	<&fanmax0>,"temperature:0";
+		maxtemp_hyst =	<&fanmax0>,"hysteresis:0";
+
+		emc2301 =	<0>,"+0",
+				<&map0>,"cooling-device:0=",<&emc2301>,
+				<&map1>,"cooling-device:0=",<&emc2301>;
+	};
+};
