From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: JohnTheCoolingFan <ivan8215145640@gmail.com>
Date: Fri, 9 Aug 2024 07:14:37 +0000
Subject: Allwinner H616: driver: I2C3

Signed-off-by: JohnTheCoolingFan <ivan8215145640@gmail.com>
---
 arch/arm/include/asm/arch-sunxi/i2c.h |  3 +
 arch/arm/mach-sunxi/Kconfig           |  9 +++
 board/sunxi/board.c                   | 12 +++++
 3 files changed, 24 insertions(+)

diff --git a/arch/arm/include/asm/arch-sunxi/i2c.h b/arch/arm/include/asm/arch-sunxi/i2c.h
index f0da46d863c..98c26d7408e 100644
--- a/arch/arm/include/asm/arch-sunxi/i2c.h
+++ b/arch/arm/include/asm/arch-sunxi/i2c.h
@@ -11,10 +11,13 @@
 #define CFG_I2C_MVTWSI_BASE0	SUNXI_TWI0_BASE
 #endif
 #ifdef CONFIG_I2C1_ENABLE
 #define CFG_I2C_MVTWSI_BASE1	SUNXI_TWI1_BASE
 #endif
+#ifdef CONFIG_I2C3_ENABLE
+#define CONFIG_I2C_MVTWSI_BASE3 SUNXI_TWI3_BASE
+#endif
 #ifdef CONFIG_R_I2C_ENABLE
 #define CFG_I2C_MVTWSI_BASE2 SUNXI_R_TWI_BASE
 #endif
 
 /* This is abp0-clk on sun4i/5i/7i / abp1-clk on sun6i/sun8i which is 24MHz */
diff --git a/arch/arm/mach-sunxi/Kconfig b/arch/arm/mach-sunxi/Kconfig
index d91faf9ab13..59cd48653c3 100644
--- a/arch/arm/mach-sunxi/Kconfig
+++ b/arch/arm/mach-sunxi/Kconfig
@@ -807,10 +807,19 @@ config I2C1_ENABLE
 	bool "Enable I2C/TWI controller 1"
 	select CMD_I2C
 	---help---
 	See I2C0_ENABLE help text.
 
+if MACH_SUN50I_H616
+config I2C3_ENABLE
+	bool "Enable I2C/TWI controller 3"
+	default n
+	select CMD_I2C
+	---help---
+	See I2C0_ENABLE help text.
+endif
+
 if SUNXI_GEN_SUN6I || SUN50I_GEN_H6
 config R_I2C_ENABLE
 	bool "Enable the PRCM I2C/TWI controller"
 	# This is used for the pmic on H3
 	default y if SY8106A_POWER
diff --git a/board/sunxi/board.c b/board/sunxi/board.c
index 2163fac5e06..302a927f524 100644
--- a/board/sunxi/board.c
+++ b/board/sunxi/board.c
@@ -13,10 +13,11 @@
 #include <common.h>
 #include <clock_legacy.h>
 #include <dm.h>
 #include <env.h>
 #include <hang.h>
+#include <i2c.h>
 #include <image.h>
 #include <init.h>
 #include <log.h>
 #include <mmc.h>
 #include <axp_pmic.h>
@@ -107,10 +108,21 @@ void i2c_init_board(void)
 	sunxi_gpio_set_cfgpin(SUNXI_GPH(3), SUN50I_GPH_TWI1);
 	clock_twi_onoff(1, 1);
 #endif
 #endif
 
+#ifdef CONFIG_I2C3_ENABLE
+#if defined(CONFIG_MACH_SUN50I_H616)
+	sunxi_gpio_set_cfgpin(SUNXI_GPA(10), 2);
+	sunxi_gpio_set_cfgpin(SUNXI_GPA(11), 2);
+	sunxi_gpio_set_cfgpin(SUNXI_GPA(12), 2);
+	sunxi_gpio_set_pull(SUNXI_GPA(10), SUNXI_GPIO_PULL_UP);
+	sunxi_gpio_set_pull(SUNXI_GPA(11), SUNXI_GPIO_PULL_UP);
+	clock_twi_onoff(3, 1);
+#endif
+#endif
+
 #ifdef CONFIG_R_I2C_ENABLE
 #ifdef CONFIG_MACH_SUN50I
 	clock_twi_onoff(5, 1);
 	sunxi_gpio_set_cfgpin(SUNXI_GPL(8), SUN50I_GPL_R_TWI);
 	sunxi_gpio_set_cfgpin(SUNXI_GPL(9), SUN50I_GPL_R_TWI);
