linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2
@ 2025-06-26 14:04 muhammed.efecetin.67
  2025-06-26 14:04 ` [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property muhammed.efecetin.67
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:04 UTC (permalink / raw)
  To: linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, neil.armstrong, lee, rafael,
	efectn, daniel.lezcano

From: Muhammed Efe Cetin <efectn@protonmail.com>

This patch series adds support for the Khadas MCU and fan control for Khadas Edge 2 board.

The first patch updates devicetree bindings for new cooling-levels property.
The second mfd patch removes unused nvmem code.
The third patch adds the Khadas Edge 2 registers to the mfd driver.
The fourth patch adds support for Khadas Edge 2's new fan control registers to MCU thermal driver and the last one adds the Khadas Edge 2 device tree node for the MCU and fan control.
And the final patch adds the Khadas Edge 2 device tree node for the MCU and fan control.

Muhammed Efe Cetin (5):
  dt-bindings: mfd: khadas-mcu: add cooling-levels property
  mfd: khadas-mcu: drop unused nvmem code
  mfd: khadas-mcu: add Khadas Edge 2 registers
  thermal: khadas_mcu_fan: add support for Khadas Edge 2
  arm64: dts: rockchip: add Khadas MCU and fan control nodes

 .../devicetree/bindings/mfd/khadas,mcu.yaml   |  9 ++-
 .../dts/rockchip/rk3588s-khadas-edge2.dts     | 59 ++++++++++++++
 drivers/mfd/khadas-mcu.c                      | 19 ++---
 drivers/thermal/khadas_mcu_fan.c              | 76 ++++++++++++++++++-
 include/linux/mfd/khadas-mcu.h                | 25 ++++++
 5 files changed, 170 insertions(+), 18 deletions(-)

-- 
2.49.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property
  2025-06-26 14:04 [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2 muhammed.efecetin.67
@ 2025-06-26 14:04 ` muhammed.efecetin.67
  2025-06-26 14:12   ` neil.armstrong
  2025-06-26 14:04 ` [PATCH 2/5] mfd: khadas-mcu: drop unused nvmem code muhammed.efecetin.67
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:04 UTC (permalink / raw)
  To: linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, neil.armstrong, lee, rafael,
	efectn, daniel.lezcano

From: Muhammed Efe Cetin <efectn@protonmail.com>

Add fan controlling step property which is used by Khadas Edge 2.

Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
---
 Documentation/devicetree/bindings/mfd/khadas,mcu.yaml | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml b/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
index 084960fd5..f69643aae 100644
--- a/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
+++ b/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
@@ -11,7 +11,7 @@ maintainers:
 
 description: |
   Khadas embeds a microcontroller on their VIM and Edge boards adding some
-  system feature as PWM Fan control (for VIM2 rev14 or VIM3), User memory
+  system feature as PWM Fan control (for VIM2 rev14, VIM3, Edge2), User memory
   storage, IR/Key resume control, system power LED control and more.
 
 properties:
@@ -22,6 +22,13 @@ properties:
   "#cooling-cells": # Only needed for boards having FAN control feature
     const: 2
 
+  cooling-levels:
+    description:
+      Max speed of PWM fan, from 0 to 100. This property is necessary for Khadas Edge 2.
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    items:
+      maximum: 100
+
   reg:
     maxItems: 1
 
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/5] mfd: khadas-mcu: drop unused nvmem code
  2025-06-26 14:04 [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2 muhammed.efecetin.67
  2025-06-26 14:04 ` [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property muhammed.efecetin.67
@ 2025-06-26 14:04 ` muhammed.efecetin.67
  2025-06-26 14:07   ` neil.armstrong
  2025-06-26 14:04 ` [PATCH 3/5] mfd: khadas-mcu: add Khadas Edge 2 registers muhammed.efecetin.67
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:04 UTC (permalink / raw)
  To: linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, neil.armstrong, lee, rafael,
	efectn, daniel.lezcano

From: Muhammed Efe Cetin <efectn@protonmail.com>

Drop "khadas-mcu-user-mem" code since it is not used anywhere.

Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
---
 drivers/mfd/khadas-mcu.c | 11 -----------
 1 file changed, 11 deletions(-)

diff --git a/drivers/mfd/khadas-mcu.c b/drivers/mfd/khadas-mcu.c
index ba981a788..ca4bd6cf5 100644
--- a/drivers/mfd/khadas-mcu.c
+++ b/drivers/mfd/khadas-mcu.c
@@ -80,10 +80,6 @@ static struct mfd_cell khadas_mcu_fan_cells[] = {
 	{ .name = "khadas-mcu-fan-ctrl", },
 };
 
-static struct mfd_cell khadas_mcu_cells[] = {
-	{ .name = "khadas-mcu-user-mem", },
-};
-
 static int khadas_mcu_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -105,13 +101,6 @@ static int khadas_mcu_probe(struct i2c_client *client)
 		return ret;
 	}
 
-	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
-				   khadas_mcu_cells,
-				   ARRAY_SIZE(khadas_mcu_cells),
-				   NULL, 0, NULL);
-	if (ret)
-		return ret;
-
 	if (of_property_present(dev->of_node, "#cooling-cells"))
 		return devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
 					    khadas_mcu_fan_cells,
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/5] mfd: khadas-mcu: add Khadas Edge 2 registers
  2025-06-26 14:04 [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2 muhammed.efecetin.67
  2025-06-26 14:04 ` [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property muhammed.efecetin.67
  2025-06-26 14:04 ` [PATCH 2/5] mfd: khadas-mcu: drop unused nvmem code muhammed.efecetin.67
@ 2025-06-26 14:04 ` muhammed.efecetin.67
  2025-06-26 14:13   ` neil.armstrong
  2025-06-26 14:04 ` [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2 muhammed.efecetin.67
  2025-06-26 14:04 ` [PATCH 5/5] arm64: dts: rockchip: add Khadas MCU and fan control nodes muhammed.efecetin.67
  4 siblings, 1 reply; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:04 UTC (permalink / raw)
  To: linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, neil.armstrong, lee, rafael,
	efectn, daniel.lezcano

From: Muhammed Efe Cetin <efectn@protonmail.com>

Add Khadas Edge 2 MCU registers to khadas-mcu.h and adjust some properties.
Also, i've added some registers with V2 suffix,
as same addresses were already exist for previous boards.

Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
---
 drivers/mfd/khadas-mcu.c       |  8 ++++++--
 include/linux/mfd/khadas-mcu.h | 25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/drivers/mfd/khadas-mcu.c b/drivers/mfd/khadas-mcu.c
index ca4bd6cf5..199ed4925 100644
--- a/drivers/mfd/khadas-mcu.c
+++ b/drivers/mfd/khadas-mcu.c
@@ -26,6 +26,10 @@ static bool khadas_mcu_reg_volatile(struct device *dev, unsigned int reg)
 	case KHADAS_MCU_CHECK_USER_PASSWD_REG:
 	case KHADAS_MCU_WOL_INIT_START_REG:
 	case KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG:
+	case KHADAS_MCU_LED_ON_RAM_REG:
+	case KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2:
+	case KHADAS_MCU_WDT_EN_REG:
+	case KHADAS_MCU_SYS_RST_REG:
 		return true;
 	default:
 		return false;
@@ -69,14 +73,14 @@ static const struct regmap_config khadas_mcu_regmap_config = {
 	.reg_bits	= 8,
 	.reg_stride	= 1,
 	.val_bits	= 8,
-	.max_register	= KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
+	.max_register	= KHADAS_MCU_SYS_RST_REG,
 	.volatile_reg	= khadas_mcu_reg_volatile,
 	.writeable_reg	= khadas_mcu_reg_writeable,
 	.cache_type	= REGCACHE_MAPLE,
 };
 
 static struct mfd_cell khadas_mcu_fan_cells[] = {
-	/* VIM1/2 Rev13+ and VIM3 only */
+	/* VIM1/2 Rev13+, VIM3 and Edge2 only */
 	{ .name = "khadas-mcu-fan-ctrl", },
 };
 
diff --git a/include/linux/mfd/khadas-mcu.h b/include/linux/mfd/khadas-mcu.h
index a99ba2ed0..de570e159 100644
--- a/include/linux/mfd/khadas-mcu.h
+++ b/include/linux/mfd/khadas-mcu.h
@@ -35,26 +35,45 @@
 #define KHADAS_MCU_FACTORY_TEST_REG		0x16 /* R */
 #define KHADAS_MCU_BOOT_MODE_REG		0x20 /* RW */
 #define KHADAS_MCU_BOOT_EN_WOL_REG		0x21 /* RW */
+#define KHADAS_MCU_BOOT_EN_DCIN_REG_V2		0x21 /* RW */
 #define KHADAS_MCU_BOOT_EN_RTC_REG		0x22 /* RW */
 #define KHADAS_MCU_BOOT_EN_EXP_REG		0x23 /* RW */
+#define KHADAS_MCU_LED_MODE_ON_REG_V2		0x23 /* RW */
+#define KHADAS_MCU_LED_MODE_OFF_REG_V2		0x24 /* RW */
 #define KHADAS_MCU_BOOT_EN_IR_REG		0x24 /* RW */
 #define KHADAS_MCU_BOOT_EN_DCIN_REG		0x25 /* RW */
+#define KHADAS_MCU_RGB_ON_R_REG			0x25 /* RW */
+#define KHADAS_MCU_RGB_ON_G_REG			0x26 /* RW */
 #define KHADAS_MCU_BOOT_EN_KEY_REG		0x26 /* RW */
+#define KHADAS_MCU_RGB_ON_B_REG			0x27 /* RW */
 #define KHADAS_MCU_KEY_MODE_REG			0x27 /* RW */
+#define KHADAS_MCU_RGB_OFF_R_REG		0x28 /* RW */
 #define KHADAS_MCU_LED_MODE_ON_REG		0x28 /* RW */
+#define KHADAS_MCU_RGB_OFF_G_REG		0x29 /* RW */
 #define KHADAS_MCU_LED_MODE_OFF_REG		0x29 /* RW */
+#define KHADAS_MCU_RGB_OFF_B_REG		0x2a /* RW */
+#define KHADAS_MCU_REST_CONF_REG		0x2c /* WO */
 #define KHADAS_MCU_SHUTDOWN_NORMAL_REG		0x2c /* RW */
 #define KHADAS_MCU_MAC_SWITCH_REG		0x2d /* RW */
 #define KHADAS_MCU_MCU_SLEEP_MODE_REG		0x2e /* RW */
+#define KHADAS_MCU_BOOT_EN_IR_REG_V2		0x2f /* RW */
 #define KHADAS_MCU_IR_CODE1_0_REG		0x2f /* RW */
 #define KHADAS_MCU_IR_CODE1_1_REG		0x30 /* RW */
+#define KHADAS_MCU_IR1_CUST1_REG		0x30 /* RW */
 #define KHADAS_MCU_IR_CODE1_2_REG		0x31 /* RW */
+#define KHADAS_MCU_IR1_CUST2_REG		0x31 /* RW */
 #define KHADAS_MCU_IR_CODE1_3_REG		0x32 /* RW */
+#define KHADAS_MCU_IR1_ORDER1_REG		0x32 /* RW */
 #define KHADAS_MCU_USB_PCIE_SWITCH_REG		0x33 /* RW */
+#define KHADAS_MCU_IR1_ORDER2_REG		0x33 /* RW */
+#define KHADAS_MCU_IR2_CUST1_REG		0x34 /* RW */
 #define KHADAS_MCU_IR_CODE2_0_REG		0x34 /* RW */
 #define KHADAS_MCU_IR_CODE2_1_REG		0x35 /* RW */
+#define KHADAS_MCU_IR2_CUST2_REG		0x35 /* RW */
 #define KHADAS_MCU_IR_CODE2_2_REG		0x36 /* RW */
+#define KHADAS_MCU_IR2_ORDER1_REG		0x36 /* RW */
 #define KHADAS_MCU_IR_CODE2_3_REG		0x37 /* RW */
+#define KHADAS_MCU_IR2_ORDER2_REG		0x36 /* RW */
 #define KHADAS_MCU_PASSWD_USER_0_REG		0x40 /* RW */
 #define KHADAS_MCU_PASSWD_USER_1_REG		0x41 /* RW */
 #define KHADAS_MCU_PASSWD_USER_2_REG		0x42 /* RW */
@@ -69,6 +88,11 @@
 #define KHADAS_MCU_SHUTDOWN_NORMAL_STATUS_REG	0x86 /* RO */
 #define KHADAS_MCU_WOL_INIT_START_REG		0x87 /* WO */
 #define KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG	0x88 /* WO */
+#define KHADAS_MCU_LED_ON_RAM_REG		0x89 /* WO */
+#define KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2	0x8A /* WO */
+#define KHADAS_MCU_WDT_EN_REG			0x8B /* WO */
+#define KHADAS_MCU_SYS_RST_REG			0x91 /* WO */
+
 
 enum {
 	KHADAS_BOARD_VIM1 = 0x1,
@@ -76,6 +100,7 @@ enum {
 	KHADAS_BOARD_VIM3,
 	KHADAS_BOARD_EDGE = 0x11,
 	KHADAS_BOARD_EDGE_V,
+	KHADAS_BOARD_EDGE2,
 };
 
 /**
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2
  2025-06-26 14:04 [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2 muhammed.efecetin.67
                   ` (2 preceding siblings ...)
  2025-06-26 14:04 ` [PATCH 3/5] mfd: khadas-mcu: add Khadas Edge 2 registers muhammed.efecetin.67
@ 2025-06-26 14:04 ` muhammed.efecetin.67
  2025-06-26 14:11   ` neil.armstrong
  2025-06-26 14:04 ` [PATCH 5/5] arm64: dts: rockchip: add Khadas MCU and fan control nodes muhammed.efecetin.67
  4 siblings, 1 reply; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:04 UTC (permalink / raw)
  To: linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, neil.armstrong, lee, rafael,
	efectn, daniel.lezcano

From: Muhammed Efe Cetin <efectn@protonmail.com>

Fan control on the Khadas Edge 2 is controlled with the 0x8A register,
using percentage values from 0 to 100, whereas there are only 3 constant steps in previous Khadas boards.
Therefore, i added a new cooling-levels property, similar to the one used in the pwm-fan driver.
The original behavior can still be used when the cooling-levels property is not specified,
ensuring that the new functionality does not break old boards.

Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
---
 drivers/thermal/khadas_mcu_fan.c | 76 ++++++++++++++++++++++++++++++--
 1 file changed, 72 insertions(+), 4 deletions(-)

diff --git a/drivers/thermal/khadas_mcu_fan.c b/drivers/thermal/khadas_mcu_fan.c
index d35e5313b..504e7d254 100644
--- a/drivers/thermal/khadas_mcu_fan.c
+++ b/drivers/thermal/khadas_mcu_fan.c
@@ -15,10 +15,16 @@
 #include <linux/thermal.h>
 
 #define MAX_LEVEL 3
+#define MAX_SPEED 0x64
 
 struct khadas_mcu_fan_ctx {
 	struct khadas_mcu *mcu;
 	unsigned int level;
+
+	unsigned int fan_max_level;
+	unsigned int fan_register;
+	unsigned int *fan_cooling_levels;
+
 	struct thermal_cooling_device *cdev;
 };
 
@@ -26,9 +32,21 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
 				    unsigned int level)
 {
 	int ret;
+	unsigned int write_level = level;
+
+	if (level > ctx->fan_max_level)
+		return -EINVAL;
+
+	if (ctx->fan_cooling_levels != NULL) {
+		write_level = ctx->fan_cooling_levels[level];
+
+		if (write_level > MAX_SPEED)
+			return -EINVAL;
+	}
+
+	ret = regmap_write(ctx->mcu->regmap, ctx->fan_register,
+			   write_level);
 
-	ret = regmap_write(ctx->mcu->regmap, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
-			   level);
 	if (ret)
 		return ret;
 
@@ -40,7 +58,9 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
 static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
 					unsigned long *state)
 {
-	*state = MAX_LEVEL;
+	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
+
+	*state = ctx->fan_max_level;
 
 	return 0;
 }
@@ -61,7 +81,7 @@ khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
 {
 	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
 
-	if (state > MAX_LEVEL)
+	if (state > ctx->fan_max_level)
 		return -EINVAL;
 
 	if (state == ctx->level)
@@ -76,6 +96,47 @@ static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
 	.set_cur_state = khadas_mcu_fan_set_cur_state,
 };
 
+static int khadas_mcu_fan_get_cooling_data_edge2(struct khadas_mcu_fan_ctx *ctx, struct device *dev)
+{
+	struct device_node *np = ctx->mcu->dev->of_node;
+	int num, i, ret;
+
+	if (!of_property_present(np, "cooling-levels"))
+		return 0;
+
+	ret = of_property_count_u32_elems(np, "cooling-levels");
+	if (ret <= 0) {
+		dev_err(dev, "Wrong data!\n");
+		return ret ? : -EINVAL;
+	}
+
+	num = ret;
+	ctx->fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
+						   GFP_KERNEL);
+	if (!ctx->fan_cooling_levels)
+		return -ENOMEM;
+
+	ret = of_property_read_u32_array(np, "cooling-levels",
+					 ctx->fan_cooling_levels, num);
+	if (ret) {
+		dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
+		return ret;
+	}
+
+	for (i = 0; i < num; i++) {
+		if (ctx->fan_cooling_levels[i] > MAX_SPEED) {
+			dev_err(dev, "MCU fan state[%d]:%d > %d\n", i,
+				ctx->fan_cooling_levels[i], MAX_SPEED);
+			return -EINVAL;
+		}
+	}
+
+	ctx->fan_max_level = num - 1;
+	ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2;
+
+	return 0;
+}
+
 static int khadas_mcu_fan_probe(struct platform_device *pdev)
 {
 	struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
@@ -90,6 +151,13 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev)
 	ctx->mcu = mcu;
 	platform_set_drvdata(pdev, ctx);
 
+	ctx->fan_max_level = MAX_LEVEL;
+	ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG;
+
+	ret = khadas_mcu_fan_get_cooling_data_edge2(ctx, dev);
+	if (ret)
+		return ret;
+
 	cdev = devm_thermal_of_cooling_device_register(dev->parent,
 			dev->parent->of_node, "khadas-mcu-fan", ctx,
 			&khadas_mcu_fan_cooling_ops);
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 5/5] arm64: dts: rockchip: add Khadas MCU and fan control nodes
  2025-06-26 14:04 [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2 muhammed.efecetin.67
                   ` (3 preceding siblings ...)
  2025-06-26 14:04 ` [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2 muhammed.efecetin.67
@ 2025-06-26 14:04 ` muhammed.efecetin.67
  4 siblings, 0 replies; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:04 UTC (permalink / raw)
  To: linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, neil.armstrong, lee, rafael,
	efectn, daniel.lezcano

From: Muhammed Efe Cetin <efectn@protonmail.com>

Add Khadas MCU fan control to Khadas Edge 2 with 4 fan control levels.

Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
---
 .../dts/rockchip/rk3588s-khadas-edge2.dts     | 59 +++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/arch/arm64/boot/dts/rockchip/rk3588s-khadas-edge2.dts b/arch/arm64/boot/dts/rockchip/rk3588s-khadas-edge2.dts
index 2c22abaf4..60766c896 100644
--- a/arch/arm64/boot/dts/rockchip/rk3588s-khadas-edge2.dts
+++ b/arch/arm64/boot/dts/rockchip/rk3588s-khadas-edge2.dts
@@ -271,12 +271,71 @@ hym8563: rtc@51 {
 		clock-output-names = "hym8563";
 		wakeup-source;
 	};
+
+	khadas_mcu: system-controller@18 {
+		compatible = "khadas,mcu";
+		reg = <0x18>;
+		cooling-levels = <0 50 72 100>;
+		#cooling-cells = <2>;
+	};
 };
 
 &i2s5_8ch {
 	status = "okay";
 };
 
+&package_thermal {
+	polling-delay = <2000>;
+
+	trips {
+		package_fan0: package-fan0 {
+			temperature = <50000>;
+			hysteresis = <5000>;
+			type = "active";
+		};
+
+		package_fan1: package-fan1 {
+			temperature = <60000>;
+			hysteresis = <5000>;
+			type = "active";
+		};
+
+		package_fan2: package-fan2 {
+			temperature = <65000>;
+			hysteresis = <5000>;
+			type = "active";
+		};
+
+		package_fan3: package-fan3 {
+			temperature = <75000>;
+			hysteresis = <5000>;
+			type = "active";
+		};
+	};
+
+	cooling-maps {
+		map0 {
+			trip = <&package_fan0>;
+			cooling-device = <&khadas_mcu THERMAL_NO_LIMIT 1>;
+		};
+
+		map1 {
+			trip = <&package_fan1>;
+			cooling-device = <&khadas_mcu 1 2>;
+		};
+
+		map2 {
+			trip = <&package_fan2>;
+			cooling-device = <&khadas_mcu 2 3>;
+		};
+
+		map3 {
+			trip = <&package_fan3>;
+			cooling-device = <&khadas_mcu 3 THERMAL_NO_LIMIT>;
+		};
+	};
+};
+
 &pd_gpu {
 	domain-supply = <&vdd_gpu_s0>;
 };
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 2/5] mfd: khadas-mcu: drop unused nvmem code
  2025-06-26 14:04 ` [PATCH 2/5] mfd: khadas-mcu: drop unused nvmem code muhammed.efecetin.67
@ 2025-06-26 14:07   ` neil.armstrong
  0 siblings, 0 replies; 14+ messages in thread
From: neil.armstrong @ 2025-06-26 14:07 UTC (permalink / raw)
  To: muhammed.efecetin.67, linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, lee, rafael, efectn,
	daniel.lezcano

On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
> From: Muhammed Efe Cetin <efectn@protonmail.com>
> 
> Drop "khadas-mcu-user-mem" code since it is not used anywhere.
> 
> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
> ---
>   drivers/mfd/khadas-mcu.c | 11 -----------
>   1 file changed, 11 deletions(-)
> 
> diff --git a/drivers/mfd/khadas-mcu.c b/drivers/mfd/khadas-mcu.c
> index ba981a788..ca4bd6cf5 100644
> --- a/drivers/mfd/khadas-mcu.c
> +++ b/drivers/mfd/khadas-mcu.c
> @@ -80,10 +80,6 @@ static struct mfd_cell khadas_mcu_fan_cells[] = {
>   	{ .name = "khadas-mcu-fan-ctrl", },
>   };
>   
> -static struct mfd_cell khadas_mcu_cells[] = {
> -	{ .name = "khadas-mcu-user-mem", },
> -};
> -
>   static int khadas_mcu_probe(struct i2c_client *client)
>   {
>   	struct device *dev = &client->dev;
> @@ -105,13 +101,6 @@ static int khadas_mcu_probe(struct i2c_client *client)
>   		return ret;
>   	}
>   
> -	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
> -				   khadas_mcu_cells,
> -				   ARRAY_SIZE(khadas_mcu_cells),
> -				   NULL, 0, NULL);
> -	if (ret)
> -		return ret;
> -
>   	if (of_property_present(dev->of_node, "#cooling-cells"))
>   		return devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
>   					    khadas_mcu_fan_cells,

Reviewed-by: Neil Armstrong <neil.armstrong@linaro.org>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2
  2025-06-26 14:04 ` [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2 muhammed.efecetin.67
@ 2025-06-26 14:11   ` neil.armstrong
  2025-06-26 14:36     ` muhammed.efecetin.67
  0 siblings, 1 reply; 14+ messages in thread
From: neil.armstrong @ 2025-06-26 14:11 UTC (permalink / raw)
  To: muhammed.efecetin.67, linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, lee, rafael, efectn,
	daniel.lezcano

On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
> From: Muhammed Efe Cetin <efectn@protonmail.com>
> 
> Fan control on the Khadas Edge 2 is controlled with the 0x8A register,
> using percentage values from 0 to 100, whereas there are only 3 constant steps in previous Khadas boards.
> Therefore, i added a new cooling-levels property, similar to the one used in the pwm-fan driver.
> The original behavior can still be used when the cooling-levels property is not specified,
> ensuring that the new functionality does not break old boards.

Thanks for the explanation, but would would you like to change that ? The MCU can accept
any value between 0 and 99, so why change the levels from DT ?

Neil

> 
> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
> ---
>   drivers/thermal/khadas_mcu_fan.c | 76 ++++++++++++++++++++++++++++++--
>   1 file changed, 72 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/thermal/khadas_mcu_fan.c b/drivers/thermal/khadas_mcu_fan.c
> index d35e5313b..504e7d254 100644
> --- a/drivers/thermal/khadas_mcu_fan.c
> +++ b/drivers/thermal/khadas_mcu_fan.c
> @@ -15,10 +15,16 @@
>   #include <linux/thermal.h>
>   
>   #define MAX_LEVEL 3
> +#define MAX_SPEED 0x64
>   
>   struct khadas_mcu_fan_ctx {
>   	struct khadas_mcu *mcu;
>   	unsigned int level;
> +
> +	unsigned int fan_max_level;
> +	unsigned int fan_register;
> +	unsigned int *fan_cooling_levels;
> +
>   	struct thermal_cooling_device *cdev;
>   };
>   
> @@ -26,9 +32,21 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>   				    unsigned int level)
>   {
>   	int ret;
> +	unsigned int write_level = level;
> +
> +	if (level > ctx->fan_max_level)
> +		return -EINVAL;
> +
> +	if (ctx->fan_cooling_levels != NULL) {
> +		write_level = ctx->fan_cooling_levels[level];
> +
> +		if (write_level > MAX_SPEED)
> +			return -EINVAL;
> +	}
> +
> +	ret = regmap_write(ctx->mcu->regmap, ctx->fan_register,
> +			   write_level);
>   
> -	ret = regmap_write(ctx->mcu->regmap, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
> -			   level);
>   	if (ret)
>   		return ret;
>   
> @@ -40,7 +58,9 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>   static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
>   					unsigned long *state)
>   {
> -	*state = MAX_LEVEL;
> +	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
> +
> +	*state = ctx->fan_max_level;
>   
>   	return 0;
>   }
> @@ -61,7 +81,7 @@ khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
>   {
>   	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>   
> -	if (state > MAX_LEVEL)
> +	if (state > ctx->fan_max_level)
>   		return -EINVAL;
>   
>   	if (state == ctx->level)
> @@ -76,6 +96,47 @@ static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
>   	.set_cur_state = khadas_mcu_fan_set_cur_state,
>   };
>   
> +static int khadas_mcu_fan_get_cooling_data_edge2(struct khadas_mcu_fan_ctx *ctx, struct device *dev)
> +{
> +	struct device_node *np = ctx->mcu->dev->of_node;
> +	int num, i, ret;
> +
> +	if (!of_property_present(np, "cooling-levels"))
> +		return 0;
> +
> +	ret = of_property_count_u32_elems(np, "cooling-levels");
> +	if (ret <= 0) {
> +		dev_err(dev, "Wrong data!\n");
> +		return ret ? : -EINVAL;
> +	}
> +
> +	num = ret;
> +	ctx->fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
> +						   GFP_KERNEL);
> +	if (!ctx->fan_cooling_levels)
> +		return -ENOMEM;
> +
> +	ret = of_property_read_u32_array(np, "cooling-levels",
> +					 ctx->fan_cooling_levels, num);
> +	if (ret) {
> +		dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
> +		return ret;
> +	}
> +
> +	for (i = 0; i < num; i++) {
> +		if (ctx->fan_cooling_levels[i] > MAX_SPEED) {
> +			dev_err(dev, "MCU fan state[%d]:%d > %d\n", i,
> +				ctx->fan_cooling_levels[i], MAX_SPEED);
> +			return -EINVAL;
> +		}
> +	}
> +
> +	ctx->fan_max_level = num - 1;
> +	ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2;
> +
> +	return 0;
> +}
> +
>   static int khadas_mcu_fan_probe(struct platform_device *pdev)
>   {
>   	struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
> @@ -90,6 +151,13 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev)
>   	ctx->mcu = mcu;
>   	platform_set_drvdata(pdev, ctx);
>   
> +	ctx->fan_max_level = MAX_LEVEL;
> +	ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG;
> +
> +	ret = khadas_mcu_fan_get_cooling_data_edge2(ctx, dev);
> +	if (ret)
> +		return ret;
> +
>   	cdev = devm_thermal_of_cooling_device_register(dev->parent,
>   			dev->parent->of_node, "khadas-mcu-fan", ctx,
>   			&khadas_mcu_fan_cooling_ops);


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property
  2025-06-26 14:04 ` [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property muhammed.efecetin.67
@ 2025-06-26 14:12   ` neil.armstrong
  0 siblings, 0 replies; 14+ messages in thread
From: neil.armstrong @ 2025-06-26 14:12 UTC (permalink / raw)
  To: muhammed.efecetin.67, linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, lee, rafael, efectn,
	daniel.lezcano

On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
> From: Muhammed Efe Cetin <efectn@protonmail.com>
> 
> Add fan controlling step property which is used by Khadas Edge 2.
> 
> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
> ---
>   Documentation/devicetree/bindings/mfd/khadas,mcu.yaml | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml b/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
> index 084960fd5..f69643aae 100644
> --- a/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
> +++ b/Documentation/devicetree/bindings/mfd/khadas,mcu.yaml
> @@ -11,7 +11,7 @@ maintainers:
>   
>   description: |
>     Khadas embeds a microcontroller on their VIM and Edge boards adding some
> -  system feature as PWM Fan control (for VIM2 rev14 or VIM3), User memory
> +  system feature as PWM Fan control (for VIM2 rev14, VIM3, Edge2), User memory
>     storage, IR/Key resume control, system power LED control and more.
>   
>   properties:
> @@ -22,6 +22,13 @@ properties:
>     "#cooling-cells": # Only needed for boards having FAN control feature
>       const: 2
>   
> +  cooling-levels:
> +    description:
> +      Max speed of PWM fan, from 0 to 100. This property is necessary for Khadas Edge 2.
> +    $ref: /schemas/types.yaml#/definitions/uint32-array
> +    items:
> +      maximum: 100

The cooling level can be detected dynamically by reading the MCU register, so
just set the max_level to 100 when edge2 is detected, and drop this property.

Neil

> +
>     reg:
>       maxItems: 1
>   


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 3/5] mfd: khadas-mcu: add Khadas Edge 2 registers
  2025-06-26 14:04 ` [PATCH 3/5] mfd: khadas-mcu: add Khadas Edge 2 registers muhammed.efecetin.67
@ 2025-06-26 14:13   ` neil.armstrong
  0 siblings, 0 replies; 14+ messages in thread
From: neil.armstrong @ 2025-06-26 14:13 UTC (permalink / raw)
  To: muhammed.efecetin.67, linux-rockchip
  Cc: devicetree, linux-arm-kernel, linux-kernel, robh+dt,
	krzysztof.kozlowski+dt, heiko, lee, rafael, efectn,
	daniel.lezcano

On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
> From: Muhammed Efe Cetin <efectn@protonmail.com>
> 
> Add Khadas Edge 2 MCU registers to khadas-mcu.h and adjust some properties.
> Also, i've added some registers with V2 suffix,
> as same addresses were already exist for previous boards.
> 
> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
> ---
>   drivers/mfd/khadas-mcu.c       |  8 ++++++--
>   include/linux/mfd/khadas-mcu.h | 25 +++++++++++++++++++++++++
>   2 files changed, 31 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mfd/khadas-mcu.c b/drivers/mfd/khadas-mcu.c
> index ca4bd6cf5..199ed4925 100644
> --- a/drivers/mfd/khadas-mcu.c
> +++ b/drivers/mfd/khadas-mcu.c
> @@ -26,6 +26,10 @@ static bool khadas_mcu_reg_volatile(struct device *dev, unsigned int reg)
>   	case KHADAS_MCU_CHECK_USER_PASSWD_REG:
>   	case KHADAS_MCU_WOL_INIT_START_REG:
>   	case KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG:
> +	case KHADAS_MCU_LED_ON_RAM_REG:
> +	case KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2:
> +	case KHADAS_MCU_WDT_EN_REG:
> +	case KHADAS_MCU_SYS_RST_REG:
>   		return true;
>   	default:
>   		return false;
> @@ -69,14 +73,14 @@ static const struct regmap_config khadas_mcu_regmap_config = {
>   	.reg_bits	= 8,
>   	.reg_stride	= 1,
>   	.val_bits	= 8,
> -	.max_register	= KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
> +	.max_register	= KHADAS_MCU_SYS_RST_REG,
>   	.volatile_reg	= khadas_mcu_reg_volatile,
>   	.writeable_reg	= khadas_mcu_reg_writeable,
>   	.cache_type	= REGCACHE_MAPLE,
>   };
>   
>   static struct mfd_cell khadas_mcu_fan_cells[] = {
> -	/* VIM1/2 Rev13+ and VIM3 only */
> +	/* VIM1/2 Rev13+, VIM3 and Edge2 only */
>   	{ .name = "khadas-mcu-fan-ctrl", },
>   };
>   
> diff --git a/include/linux/mfd/khadas-mcu.h b/include/linux/mfd/khadas-mcu.h
> index a99ba2ed0..de570e159 100644
> --- a/include/linux/mfd/khadas-mcu.h
> +++ b/include/linux/mfd/khadas-mcu.h
> @@ -35,26 +35,45 @@
>   #define KHADAS_MCU_FACTORY_TEST_REG		0x16 /* R */
>   #define KHADAS_MCU_BOOT_MODE_REG		0x20 /* RW */
>   #define KHADAS_MCU_BOOT_EN_WOL_REG		0x21 /* RW */
> +#define KHADAS_MCU_BOOT_EN_DCIN_REG_V2		0x21 /* RW */
>   #define KHADAS_MCU_BOOT_EN_RTC_REG		0x22 /* RW */
>   #define KHADAS_MCU_BOOT_EN_EXP_REG		0x23 /* RW */
> +#define KHADAS_MCU_LED_MODE_ON_REG_V2		0x23 /* RW */
> +#define KHADAS_MCU_LED_MODE_OFF_REG_V2		0x24 /* RW */
>   #define KHADAS_MCU_BOOT_EN_IR_REG		0x24 /* RW */
>   #define KHADAS_MCU_BOOT_EN_DCIN_REG		0x25 /* RW */
> +#define KHADAS_MCU_RGB_ON_R_REG			0x25 /* RW */
> +#define KHADAS_MCU_RGB_ON_G_REG			0x26 /* RW */
>   #define KHADAS_MCU_BOOT_EN_KEY_REG		0x26 /* RW */
> +#define KHADAS_MCU_RGB_ON_B_REG			0x27 /* RW */
>   #define KHADAS_MCU_KEY_MODE_REG			0x27 /* RW */
> +#define KHADAS_MCU_RGB_OFF_R_REG		0x28 /* RW */
>   #define KHADAS_MCU_LED_MODE_ON_REG		0x28 /* RW */
> +#define KHADAS_MCU_RGB_OFF_G_REG		0x29 /* RW */
>   #define KHADAS_MCU_LED_MODE_OFF_REG		0x29 /* RW */
> +#define KHADAS_MCU_RGB_OFF_B_REG		0x2a /* RW */
> +#define KHADAS_MCU_REST_CONF_REG		0x2c /* WO */
>   #define KHADAS_MCU_SHUTDOWN_NORMAL_REG		0x2c /* RW */
>   #define KHADAS_MCU_MAC_SWITCH_REG		0x2d /* RW */
>   #define KHADAS_MCU_MCU_SLEEP_MODE_REG		0x2e /* RW */
> +#define KHADAS_MCU_BOOT_EN_IR_REG_V2		0x2f /* RW */
>   #define KHADAS_MCU_IR_CODE1_0_REG		0x2f /* RW */
>   #define KHADAS_MCU_IR_CODE1_1_REG		0x30 /* RW */
> +#define KHADAS_MCU_IR1_CUST1_REG		0x30 /* RW */
>   #define KHADAS_MCU_IR_CODE1_2_REG		0x31 /* RW */
> +#define KHADAS_MCU_IR1_CUST2_REG		0x31 /* RW */
>   #define KHADAS_MCU_IR_CODE1_3_REG		0x32 /* RW */
> +#define KHADAS_MCU_IR1_ORDER1_REG		0x32 /* RW */
>   #define KHADAS_MCU_USB_PCIE_SWITCH_REG		0x33 /* RW */
> +#define KHADAS_MCU_IR1_ORDER2_REG		0x33 /* RW */
> +#define KHADAS_MCU_IR2_CUST1_REG		0x34 /* RW */
>   #define KHADAS_MCU_IR_CODE2_0_REG		0x34 /* RW */
>   #define KHADAS_MCU_IR_CODE2_1_REG		0x35 /* RW */
> +#define KHADAS_MCU_IR2_CUST2_REG		0x35 /* RW */
>   #define KHADAS_MCU_IR_CODE2_2_REG		0x36 /* RW */
> +#define KHADAS_MCU_IR2_ORDER1_REG		0x36 /* RW */
>   #define KHADAS_MCU_IR_CODE2_3_REG		0x37 /* RW */
> +#define KHADAS_MCU_IR2_ORDER2_REG		0x36 /* RW */
>   #define KHADAS_MCU_PASSWD_USER_0_REG		0x40 /* RW */
>   #define KHADAS_MCU_PASSWD_USER_1_REG		0x41 /* RW */
>   #define KHADAS_MCU_PASSWD_USER_2_REG		0x42 /* RW */
> @@ -69,6 +88,11 @@
>   #define KHADAS_MCU_SHUTDOWN_NORMAL_STATUS_REG	0x86 /* RO */
>   #define KHADAS_MCU_WOL_INIT_START_REG		0x87 /* WO */
>   #define KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG	0x88 /* WO */
> +#define KHADAS_MCU_LED_ON_RAM_REG		0x89 /* WO */
> +#define KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2	0x8A /* WO */
> +#define KHADAS_MCU_WDT_EN_REG			0x8B /* WO */
> +#define KHADAS_MCU_SYS_RST_REG			0x91 /* WO */
> +
>   
>   enum {
>   	KHADAS_BOARD_VIM1 = 0x1,
> @@ -76,6 +100,7 @@ enum {
>   	KHADAS_BOARD_VIM3,
>   	KHADAS_BOARD_EDGE = 0x11,
>   	KHADAS_BOARD_EDGE_V,
> +	KHADAS_BOARD_EDGE2,
>   };
>   
>   /**

Reviewed-by: Neil Armstrong <neil.armstrong@linaro.org>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2
  2025-06-26 14:11   ` neil.armstrong
@ 2025-06-26 14:36     ` muhammed.efecetin.67
  2025-06-26 14:46       ` neil.armstrong
  0 siblings, 1 reply; 14+ messages in thread
From: muhammed.efecetin.67 @ 2025-06-26 14:36 UTC (permalink / raw)
  To: neil.armstrong
  Cc: daniel.lezcano, devicetree, efectn, heiko, krzysztof.kozlowski+dt,
	lee, linux-arm-kernel, linux-kernel, linux-rockchip,
	muhammed.efecetin.67, rafael, robh+dt

On 6/26/25 17:11, neil.armstrong@linaro.org wrote:
> On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
>> From: Muhammed Efe Cetin <efectn@protonmail.com>
>>
>> Fan control on the Khadas Edge 2 is controlled with the 0x8A register,
>> using percentage values from 0 to 100, whereas there are only 3 constant steps in previous Khadas boards.
>> Therefore, i added a new cooling-levels property, similar to the one used in the pwm-fan driver.
>> The original behavior can still be used when the cooling-levels property is not specified,
>> ensuring that the new functionality does not break old boards. 
>
> Thanks for the explanation, but would would you like to change that ? The MCU can accept
> any value between 0 and 99, so why change the levels from DT ?
>
> Neil

Thanks for the review. Therefore, you say just add values between 0-100 to cooling-device instead of remapping them using cooling-levels property?

What would be the best practise of detecting whether the board is Khadas Edge 2? Adding new bool property, reading model propety from devicetree etc.

Best regards.

>
>>
>> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
>> ---
>>   drivers/thermal/khadas_mcu_fan.c | 76 ++++++++++++++++++++++++++++++--
>>   1 file changed, 72 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/thermal/khadas_mcu_fan.c b/drivers/thermal/khadas_mcu_fan.c
>> index d35e5313b..504e7d254 100644
>> --- a/drivers/thermal/khadas_mcu_fan.c
>> +++ b/drivers/thermal/khadas_mcu_fan.c
>> @@ -15,10 +15,16 @@
>>   #include <linux/thermal.h>
>>     #define MAX_LEVEL 3
>> +#define MAX_SPEED 0x64
>>     struct khadas_mcu_fan_ctx {
>>       struct khadas_mcu *mcu;
>>       unsigned int level;
>> +
>> +    unsigned int fan_max_level;
>> +    unsigned int fan_register;
>> +    unsigned int *fan_cooling_levels;
>> +
>>       struct thermal_cooling_device *cdev;
>>   };
>>   @@ -26,9 +32,21 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>                       unsigned int level)
>>   {
>>       int ret;
>> +    unsigned int write_level = level;
>> +
>> +    if (level > ctx->fan_max_level)
>> +        return -EINVAL;
>> +
>> +    if (ctx->fan_cooling_levels != NULL) {
>> +        write_level = ctx->fan_cooling_levels[level];
>> +
>> +        if (write_level > MAX_SPEED)
>> +            return -EINVAL;
>> +    }
>> +
>> +    ret = regmap_write(ctx->mcu->regmap, ctx->fan_register,
>> +               write_level);
>>   -    ret = regmap_write(ctx->mcu->regmap, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
>> -               level);
>>       if (ret)
>>           return ret;
>>   @@ -40,7 +58,9 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>   static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
>>                       unsigned long *state)
>>   {
>> -    *state = MAX_LEVEL;
>> +    struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>> +
>> +    *state = ctx->fan_max_level;
>>         return 0;
>>   }
>> @@ -61,7 +81,7 @@ khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
>>   {
>>       struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>   -    if (state > MAX_LEVEL)
>> +    if (state > ctx->fan_max_level)
>>           return -EINVAL;
>>         if (state == ctx->level)
>> @@ -76,6 +96,47 @@ static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
>>       .set_cur_state = khadas_mcu_fan_set_cur_state,
>>   };
>>   +static int khadas_mcu_fan_get_cooling_data_edge2(struct khadas_mcu_fan_ctx *ctx, struct device *dev)
>> +{
>> +    struct device_node *np = ctx->mcu->dev->of_node;
>> +    int num, i, ret;
>> +
>> +    if (!of_property_present(np, "cooling-levels"))
>> +        return 0;
>> +
>> +    ret = of_property_count_u32_elems(np, "cooling-levels");
>> +    if (ret <= 0) {
>> +        dev_err(dev, "Wrong data!\n");
>> +        return ret ? : -EINVAL;
>> +    }
>> +
>> +    num = ret;
>> +    ctx->fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
>> +                           GFP_KERNEL);
>> +    if (!ctx->fan_cooling_levels)
>> +        return -ENOMEM;
>> +
>> +    ret = of_property_read_u32_array(np, "cooling-levels",
>> +                     ctx->fan_cooling_levels, num);
>> +    if (ret) {
>> +        dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
>> +        return ret;
>> +    }
>> +
>> +    for (i = 0; i < num; i++) {
>> +        if (ctx->fan_cooling_levels[i] > MAX_SPEED) {
>> +            dev_err(dev, "MCU fan state[%d]:%d > %d\n", i,
>> +                ctx->fan_cooling_levels[i], MAX_SPEED);
>> +            return -EINVAL;
>> +        }
>> +    }
>> +
>> +    ctx->fan_max_level = num - 1;
>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2;
>> +
>> +    return 0;
>> +}
>> +
>>   static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>   {
>>       struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
>> @@ -90,6 +151,13 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>       ctx->mcu = mcu;
>>       platform_set_drvdata(pdev, ctx);
>>   +    ctx->fan_max_level = MAX_LEVEL;
>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG;
>> +
>> +    ret = khadas_mcu_fan_get_cooling_data_edge2(ctx, dev);
>> +    if (ret)
>> +        return ret;
>> +
>>       cdev = devm_thermal_of_cooling_device_register(dev->parent,
>>               dev->parent->of_node, "khadas-mcu-fan", ctx,
>>               &khadas_mcu_fan_cooling_ops); 
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2
  2025-06-26 14:36     ` muhammed.efecetin.67
@ 2025-06-26 14:46       ` neil.armstrong
  2025-06-26 15:16         ` Muhammed Efe Cetin
  0 siblings, 1 reply; 14+ messages in thread
From: neil.armstrong @ 2025-06-26 14:46 UTC (permalink / raw)
  To: muhammed.efecetin.67
  Cc: daniel.lezcano, devicetree, efectn, heiko, krzysztof.kozlowski+dt,
	lee, linux-arm-kernel, linux-kernel, linux-rockchip, rafael,
	robh+dt

On 26/06/2025 16:36, muhammed.efecetin.67@gmail.com wrote:
> On 6/26/25 17:11, neil.armstrong@linaro.org wrote:
>> On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
>>> From: Muhammed Efe Cetin <efectn@protonmail.com>
>>>
>>> Fan control on the Khadas Edge 2 is controlled with the 0x8A register,
>>> using percentage values from 0 to 100, whereas there are only 3 constant steps in previous Khadas boards.
>>> Therefore, i added a new cooling-levels property, similar to the one used in the pwm-fan driver.
>>> The original behavior can still be used when the cooling-levels property is not specified,
>>> ensuring that the new functionality does not break old boards.
>>
>> Thanks for the explanation, but would would you like to change that ? The MCU can accept
>> any value between 0 and 99, so why change the levels from DT ?
>>
>> Neil
> 
> Thanks for the review. Therefore, you say just add values between 0-100 to cooling-device instead of remapping them using cooling-levels property?
> 
> What would be the best practise of detecting whether the board is Khadas Edge 2? Adding new bool property, reading model propety from devicetree etc.

The register DEVICE_NO should be set at 0x12 for Edge V, 0x11 for Edge 1. I don't have the number for Edge 2, perhaps you can read it ?

Neil

> 
> Best regards.
> 
>>
>>>
>>> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
>>> ---
>>>    drivers/thermal/khadas_mcu_fan.c | 76 ++++++++++++++++++++++++++++++--
>>>    1 file changed, 72 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/drivers/thermal/khadas_mcu_fan.c b/drivers/thermal/khadas_mcu_fan.c
>>> index d35e5313b..504e7d254 100644
>>> --- a/drivers/thermal/khadas_mcu_fan.c
>>> +++ b/drivers/thermal/khadas_mcu_fan.c
>>> @@ -15,10 +15,16 @@
>>>    #include <linux/thermal.h>
>>>      #define MAX_LEVEL 3
>>> +#define MAX_SPEED 0x64
>>>      struct khadas_mcu_fan_ctx {
>>>        struct khadas_mcu *mcu;
>>>        unsigned int level;
>>> +
>>> +    unsigned int fan_max_level;
>>> +    unsigned int fan_register;
>>> +    unsigned int *fan_cooling_levels;
>>> +
>>>        struct thermal_cooling_device *cdev;
>>>    };
>>>    @@ -26,9 +32,21 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>>                        unsigned int level)
>>>    {
>>>        int ret;
>>> +    unsigned int write_level = level;
>>> +
>>> +    if (level > ctx->fan_max_level)
>>> +        return -EINVAL;
>>> +
>>> +    if (ctx->fan_cooling_levels != NULL) {
>>> +        write_level = ctx->fan_cooling_levels[level];
>>> +
>>> +        if (write_level > MAX_SPEED)
>>> +            return -EINVAL;
>>> +    }
>>> +
>>> +    ret = regmap_write(ctx->mcu->regmap, ctx->fan_register,
>>> +               write_level);
>>>    -    ret = regmap_write(ctx->mcu->regmap, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
>>> -               level);
>>>        if (ret)
>>>            return ret;
>>>    @@ -40,7 +58,9 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>>    static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
>>>                        unsigned long *state)
>>>    {
>>> -    *state = MAX_LEVEL;
>>> +    struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>> +
>>> +    *state = ctx->fan_max_level;
>>>          return 0;
>>>    }
>>> @@ -61,7 +81,7 @@ khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
>>>    {
>>>        struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>>    -    if (state > MAX_LEVEL)
>>> +    if (state > ctx->fan_max_level)
>>>            return -EINVAL;
>>>          if (state == ctx->level)
>>> @@ -76,6 +96,47 @@ static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
>>>        .set_cur_state = khadas_mcu_fan_set_cur_state,
>>>    };
>>>    +static int khadas_mcu_fan_get_cooling_data_edge2(struct khadas_mcu_fan_ctx *ctx, struct device *dev)
>>> +{
>>> +    struct device_node *np = ctx->mcu->dev->of_node;
>>> +    int num, i, ret;
>>> +
>>> +    if (!of_property_present(np, "cooling-levels"))
>>> +        return 0;
>>> +
>>> +    ret = of_property_count_u32_elems(np, "cooling-levels");
>>> +    if (ret <= 0) {
>>> +        dev_err(dev, "Wrong data!\n");
>>> +        return ret ? : -EINVAL;
>>> +    }
>>> +
>>> +    num = ret;
>>> +    ctx->fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
>>> +                           GFP_KERNEL);
>>> +    if (!ctx->fan_cooling_levels)
>>> +        return -ENOMEM;
>>> +
>>> +    ret = of_property_read_u32_array(np, "cooling-levels",
>>> +                     ctx->fan_cooling_levels, num);
>>> +    if (ret) {
>>> +        dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
>>> +        return ret;
>>> +    }
>>> +
>>> +    for (i = 0; i < num; i++) {
>>> +        if (ctx->fan_cooling_levels[i] > MAX_SPEED) {
>>> +            dev_err(dev, "MCU fan state[%d]:%d > %d\n", i,
>>> +                ctx->fan_cooling_levels[i], MAX_SPEED);
>>> +            return -EINVAL;
>>> +        }
>>> +    }
>>> +
>>> +    ctx->fan_max_level = num - 1;
>>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2;
>>> +
>>> +    return 0;
>>> +}
>>> +
>>>    static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>>    {
>>>        struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
>>> @@ -90,6 +151,13 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>>        ctx->mcu = mcu;
>>>        platform_set_drvdata(pdev, ctx);
>>>    +    ctx->fan_max_level = MAX_LEVEL;
>>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG;
>>> +
>>> +    ret = khadas_mcu_fan_get_cooling_data_edge2(ctx, dev);
>>> +    if (ret)
>>> +        return ret;
>>> +
>>>        cdev = devm_thermal_of_cooling_device_register(dev->parent,
>>>                dev->parent->of_node, "khadas-mcu-fan", ctx,
>>>                &khadas_mcu_fan_cooling_ops);
>>
> 


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2
  2025-06-26 14:46       ` neil.armstrong
@ 2025-06-26 15:16         ` Muhammed Efe Cetin
  2025-06-26 15:29           ` neil.armstrong
  0 siblings, 1 reply; 14+ messages in thread
From: Muhammed Efe Cetin @ 2025-06-26 15:16 UTC (permalink / raw)
  To: neil.armstrong
  Cc: daniel.lezcano, devicetree, efectn, heiko, krzysztof.kozlowski+dt,
	lee, linux-arm-kernel, linux-kernel, linux-rockchip,
	muhammed.efecetin.67, rafael, robh+dt

On 6/26/25 17:46, neil.armstrong@linaro.org wrote:
> On 26/06/2025 16:36, muhammed.efecetin.67@gmail.com wrote:
>> On 6/26/25 17:11, neil.armstrong@linaro.org wrote:
>>> On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
>>>> From: Muhammed Efe Cetin <efectn@protonmail.com>
>>>>
>>>> Fan control on the Khadas Edge 2 is controlled with the 0x8A register,
>>>> using percentage values from 0 to 100, whereas there are only 3 constant steps in previous Khadas boards.
>>>> Therefore, i added a new cooling-levels property, similar to the one used in the pwm-fan driver.
>>>> The original behavior can still be used when the cooling-levels property is not specified,
>>>> ensuring that the new functionality does not break old boards. 
>>>
>>> Thanks for the explanation, but would would you like to change that ? The MCU can accept
>>> any value between 0 and 99, so why change the levels from DT ?
>>>
>>> Neil 
>>
>> Thanks for the review. Therefore, you say just add values between 0-100 to cooling-device instead of remapping them using cooling-levels property?
>>
>> What would be the best practise of detecting whether the board is Khadas Edge 2? Adding new bool property, reading model propety from devicetree etc. 
>
> The register DEVICE_NO should be set at 0x12 for Edge V, 0x11 for Edge 1. I don't have the number for Edge 2, perhaps you can read it ?
>
> Neil

The DEVICE_NO register is not available for Khadas Edge 2. Here are the registers [0].

[0] https://dl.khadas.com/products/edge2/tools/mcu/edge2-mcu-register-description-en-v1.0-230520.pdf

>
>>
>> Best regards.
>>
>>>
>>>>
>>>> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
>>>> ---
>>>>    drivers/thermal/khadas_mcu_fan.c | 76 ++++++++++++++++++++++++++++++--
>>>>    1 file changed, 72 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/drivers/thermal/khadas_mcu_fan.c b/drivers/thermal/khadas_mcu_fan.c
>>>> index d35e5313b..504e7d254 100644
>>>> --- a/drivers/thermal/khadas_mcu_fan.c
>>>> +++ b/drivers/thermal/khadas_mcu_fan.c
>>>> @@ -15,10 +15,16 @@
>>>>    #include <linux/thermal.h>
>>>>      #define MAX_LEVEL 3
>>>> +#define MAX_SPEED 0x64
>>>>      struct khadas_mcu_fan_ctx {
>>>>        struct khadas_mcu *mcu;
>>>>        unsigned int level;
>>>> +
>>>> +    unsigned int fan_max_level;
>>>> +    unsigned int fan_register;
>>>> +    unsigned int *fan_cooling_levels;
>>>> +
>>>>        struct thermal_cooling_device *cdev;
>>>>    };
>>>>    @@ -26,9 +32,21 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>>>                        unsigned int level)
>>>>    {
>>>>        int ret;
>>>> +    unsigned int write_level = level;
>>>> +
>>>> +    if (level > ctx->fan_max_level)
>>>> +        return -EINVAL;
>>>> +
>>>> +    if (ctx->fan_cooling_levels != NULL) {
>>>> +        write_level = ctx->fan_cooling_levels[level];
>>>> +
>>>> +        if (write_level > MAX_SPEED)
>>>> +            return -EINVAL;
>>>> +    }
>>>> +
>>>> +    ret = regmap_write(ctx->mcu->regmap, ctx->fan_register,
>>>> +               write_level);
>>>>    -    ret = regmap_write(ctx->mcu->regmap, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
>>>> -               level);
>>>>        if (ret)
>>>>            return ret;
>>>>    @@ -40,7 +58,9 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>>>    static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
>>>>                        unsigned long *state)
>>>>    {
>>>> -    *state = MAX_LEVEL;
>>>> +    struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>>> +
>>>> +    *state = ctx->fan_max_level;
>>>>          return 0;
>>>>    }
>>>> @@ -61,7 +81,7 @@ khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
>>>>    {
>>>>        struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>>>    -    if (state > MAX_LEVEL)
>>>> +    if (state > ctx->fan_max_level)
>>>>            return -EINVAL;
>>>>          if (state == ctx->level)
>>>> @@ -76,6 +96,47 @@ static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
>>>>        .set_cur_state = khadas_mcu_fan_set_cur_state,
>>>>    };
>>>>    +static int khadas_mcu_fan_get_cooling_data_edge2(struct khadas_mcu_fan_ctx *ctx, struct device *dev)
>>>> +{
>>>> +    struct device_node *np = ctx->mcu->dev->of_node;
>>>> +    int num, i, ret;
>>>> +
>>>> +    if (!of_property_present(np, "cooling-levels"))
>>>> +        return 0;
>>>> +
>>>> +    ret = of_property_count_u32_elems(np, "cooling-levels");
>>>> +    if (ret <= 0) {
>>>> +        dev_err(dev, "Wrong data!\n");
>>>> +        return ret ? : -EINVAL;
>>>> +    }
>>>> +
>>>> +    num = ret;
>>>> +    ctx->fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
>>>> +                           GFP_KERNEL);
>>>> +    if (!ctx->fan_cooling_levels)
>>>> +        return -ENOMEM;
>>>> +
>>>> +    ret = of_property_read_u32_array(np, "cooling-levels",
>>>> +                     ctx->fan_cooling_levels, num);
>>>> +    if (ret) {
>>>> +        dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
>>>> +        return ret;
>>>> +    }
>>>> +
>>>> +    for (i = 0; i < num; i++) {
>>>> +        if (ctx->fan_cooling_levels[i] > MAX_SPEED) {
>>>> +            dev_err(dev, "MCU fan state[%d]:%d > %d\n", i,
>>>> +                ctx->fan_cooling_levels[i], MAX_SPEED);
>>>> +            return -EINVAL;
>>>> +        }
>>>> +    }
>>>> +
>>>> +    ctx->fan_max_level = num - 1;
>>>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2;
>>>> +
>>>> +    return 0;
>>>> +}
>>>> +
>>>>    static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>>>    {
>>>>        struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
>>>> @@ -90,6 +151,13 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>>>        ctx->mcu = mcu;
>>>>        platform_set_drvdata(pdev, ctx);
>>>>    +    ctx->fan_max_level = MAX_LEVEL;
>>>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG;
>>>> +
>>>> +    ret = khadas_mcu_fan_get_cooling_data_edge2(ctx, dev);
>>>> +    if (ret)
>>>> +        return ret;
>>>> +
>>>>        cdev = devm_thermal_of_cooling_device_register(dev->parent,
>>>>                dev->parent->of_node, "khadas-mcu-fan", ctx,
>>>>                &khadas_mcu_fan_cooling_ops); 
>>>
>>
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2
  2025-06-26 15:16         ` Muhammed Efe Cetin
@ 2025-06-26 15:29           ` neil.armstrong
  0 siblings, 0 replies; 14+ messages in thread
From: neil.armstrong @ 2025-06-26 15:29 UTC (permalink / raw)
  To: Muhammed Efe Cetin
  Cc: daniel.lezcano, devicetree, efectn, heiko, krzysztof.kozlowski+dt,
	lee, linux-arm-kernel, linux-kernel, linux-rockchip, rafael,
	robh+dt

On 26/06/2025 17:16, Muhammed Efe Cetin wrote:
> On 6/26/25 17:46, neil.armstrong@linaro.org wrote:
>> On 26/06/2025 16:36, muhammed.efecetin.67@gmail.com wrote:
>>> On 6/26/25 17:11, neil.armstrong@linaro.org wrote:
>>>> On 26/06/2025 16:04, muhammed.efecetin.67@gmail.com wrote:
>>>>> From: Muhammed Efe Cetin <efectn@protonmail.com>
>>>>>
>>>>> Fan control on the Khadas Edge 2 is controlled with the 0x8A register,
>>>>> using percentage values from 0 to 100, whereas there are only 3 constant steps in previous Khadas boards.
>>>>> Therefore, i added a new cooling-levels property, similar to the one used in the pwm-fan driver.
>>>>> The original behavior can still be used when the cooling-levels property is not specified,
>>>>> ensuring that the new functionality does not break old boards.
>>>>
>>>> Thanks for the explanation, but would would you like to change that ? The MCU can accept
>>>> any value between 0 and 99, so why change the levels from DT ?
>>>>
>>>> Neil
>>>
>>> Thanks for the review. Therefore, you say just add values between 0-100 to cooling-device instead of remapping them using cooling-levels property?
>>>
>>> What would be the best practise of detecting whether the board is Khadas Edge 2? Adding new bool property, reading model propety from devicetree etc.
>>
>> The register DEVICE_NO should be set at 0x12 for Edge V, 0x11 for Edge 1. I don't have the number for Edge 2, perhaps you can read it ?
>>
>> Neil
> 
> The DEVICE_NO register is not available for Khadas Edge 2. Here are the registers [0].
> 
> [0] https://dl.khadas.com/products/edge2/tools/mcu/edge2-mcu-register-description-en-v1.0-230520.pdf

OK so it's basically completely different.

In this case introduce a separate compatible since we can't detect it dynamically,
and just add the registers with EDGE2 prefix.
But keep the khadas-mcu-fan-ctrl, it will work if you pass the device type detected
via the new compatible.

Neil

> 
>>
>>>
>>> Best regards.
>>>
>>>>
>>>>>
>>>>> Signed-off-by: Muhammed Efe Cetin <efectn@protonmail.com>
>>>>> ---
>>>>>     drivers/thermal/khadas_mcu_fan.c | 76 ++++++++++++++++++++++++++++++--
>>>>>     1 file changed, 72 insertions(+), 4 deletions(-)
>>>>>
>>>>> diff --git a/drivers/thermal/khadas_mcu_fan.c b/drivers/thermal/khadas_mcu_fan.c
>>>>> index d35e5313b..504e7d254 100644
>>>>> --- a/drivers/thermal/khadas_mcu_fan.c
>>>>> +++ b/drivers/thermal/khadas_mcu_fan.c
>>>>> @@ -15,10 +15,16 @@
>>>>>     #include <linux/thermal.h>
>>>>>       #define MAX_LEVEL 3
>>>>> +#define MAX_SPEED 0x64
>>>>>       struct khadas_mcu_fan_ctx {
>>>>>         struct khadas_mcu *mcu;
>>>>>         unsigned int level;
>>>>> +
>>>>> +    unsigned int fan_max_level;
>>>>> +    unsigned int fan_register;
>>>>> +    unsigned int *fan_cooling_levels;
>>>>> +
>>>>>         struct thermal_cooling_device *cdev;
>>>>>     };
>>>>>     @@ -26,9 +32,21 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>>>>                         unsigned int level)
>>>>>     {
>>>>>         int ret;
>>>>> +    unsigned int write_level = level;
>>>>> +
>>>>> +    if (level > ctx->fan_max_level)
>>>>> +        return -EINVAL;
>>>>> +
>>>>> +    if (ctx->fan_cooling_levels != NULL) {
>>>>> +        write_level = ctx->fan_cooling_levels[level];
>>>>> +
>>>>> +        if (write_level > MAX_SPEED)
>>>>> +            return -EINVAL;
>>>>> +    }
>>>>> +
>>>>> +    ret = regmap_write(ctx->mcu->regmap, ctx->fan_register,
>>>>> +               write_level);
>>>>>     -    ret = regmap_write(ctx->mcu->regmap, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
>>>>> -               level);
>>>>>         if (ret)
>>>>>             return ret;
>>>>>     @@ -40,7 +58,9 @@ static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
>>>>>     static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
>>>>>                         unsigned long *state)
>>>>>     {
>>>>> -    *state = MAX_LEVEL;
>>>>> +    struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>>>> +
>>>>> +    *state = ctx->fan_max_level;
>>>>>           return 0;
>>>>>     }
>>>>> @@ -61,7 +81,7 @@ khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
>>>>>     {
>>>>>         struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
>>>>>     -    if (state > MAX_LEVEL)
>>>>> +    if (state > ctx->fan_max_level)
>>>>>             return -EINVAL;
>>>>>           if (state == ctx->level)
>>>>> @@ -76,6 +96,47 @@ static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
>>>>>         .set_cur_state = khadas_mcu_fan_set_cur_state,
>>>>>     };
>>>>>     +static int khadas_mcu_fan_get_cooling_data_edge2(struct khadas_mcu_fan_ctx *ctx, struct device *dev)
>>>>> +{
>>>>> +    struct device_node *np = ctx->mcu->dev->of_node;
>>>>> +    int num, i, ret;
>>>>> +
>>>>> +    if (!of_property_present(np, "cooling-levels"))
>>>>> +        return 0;
>>>>> +
>>>>> +    ret = of_property_count_u32_elems(np, "cooling-levels");
>>>>> +    if (ret <= 0) {
>>>>> +        dev_err(dev, "Wrong data!\n");
>>>>> +        return ret ? : -EINVAL;
>>>>> +    }
>>>>> +
>>>>> +    num = ret;
>>>>> +    ctx->fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
>>>>> +                           GFP_KERNEL);
>>>>> +    if (!ctx->fan_cooling_levels)
>>>>> +        return -ENOMEM;
>>>>> +
>>>>> +    ret = of_property_read_u32_array(np, "cooling-levels",
>>>>> +                     ctx->fan_cooling_levels, num);
>>>>> +    if (ret) {
>>>>> +        dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
>>>>> +        return ret;
>>>>> +    }
>>>>> +
>>>>> +    for (i = 0; i < num; i++) {
>>>>> +        if (ctx->fan_cooling_levels[i] > MAX_SPEED) {
>>>>> +            dev_err(dev, "MCU fan state[%d]:%d > %d\n", i,
>>>>> +                ctx->fan_cooling_levels[i], MAX_SPEED);
>>>>> +            return -EINVAL;
>>>>> +        }
>>>>> +    }
>>>>> +
>>>>> +    ctx->fan_max_level = num - 1;
>>>>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG_V2;
>>>>> +
>>>>> +    return 0;
>>>>> +}
>>>>> +
>>>>>     static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>>>>     {
>>>>>         struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
>>>>> @@ -90,6 +151,13 @@ static int khadas_mcu_fan_probe(struct platform_device *pdev)
>>>>>         ctx->mcu = mcu;
>>>>>         platform_set_drvdata(pdev, ctx);
>>>>>     +    ctx->fan_max_level = MAX_LEVEL;
>>>>> +    ctx->fan_register = KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG;
>>>>> +
>>>>> +    ret = khadas_mcu_fan_get_cooling_data_edge2(ctx, dev);
>>>>> +    if (ret)
>>>>> +        return ret;
>>>>> +
>>>>>         cdev = devm_thermal_of_cooling_device_register(dev->parent,
>>>>>                 dev->parent->of_node, "khadas-mcu-fan", ctx,
>>>>>                 &khadas_mcu_fan_cooling_ops);
>>>>
>>>
>>
> 


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2025-06-26 15:29 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-26 14:04 [PATCH 0/5] Add Khadas MCU and fan control support for Khadas Edge 2 muhammed.efecetin.67
2025-06-26 14:04 ` [PATCH 1/5] dt-bindings: mfd: khadas-mcu: add cooling-levels property muhammed.efecetin.67
2025-06-26 14:12   ` neil.armstrong
2025-06-26 14:04 ` [PATCH 2/5] mfd: khadas-mcu: drop unused nvmem code muhammed.efecetin.67
2025-06-26 14:07   ` neil.armstrong
2025-06-26 14:04 ` [PATCH 3/5] mfd: khadas-mcu: add Khadas Edge 2 registers muhammed.efecetin.67
2025-06-26 14:13   ` neil.armstrong
2025-06-26 14:04 ` [PATCH 4/5] thermal: khadas_mcu_fan: add support for Khadas Edge 2 muhammed.efecetin.67
2025-06-26 14:11   ` neil.armstrong
2025-06-26 14:36     ` muhammed.efecetin.67
2025-06-26 14:46       ` neil.armstrong
2025-06-26 15:16         ` Muhammed Efe Cetin
2025-06-26 15:29           ` neil.armstrong
2025-06-26 14:04 ` [PATCH 5/5] arm64: dts: rockchip: add Khadas MCU and fan control nodes muhammed.efecetin.67

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).