From 15ade055295d13f95d49e3d99b09f3bbfb4a43e7 Mon Sep 17 00:00:00 2001
From: hc <hc@nodka.com>
Date: Mon, 06 Nov 2023 07:25:24 +0000
Subject: [PATCH] add at24 driver

---
 kernel/arch/arm64/configs/rockchip_linux_defconfig |    6 +-
 kernel/drivers/misc/Makefile                       |    1 
 kernel/drivers/misc/eeprom/at24.c                  |  148 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 151 insertions(+), 4 deletions(-)

diff --git a/kernel/arch/arm64/configs/rockchip_linux_defconfig b/kernel/arch/arm64/configs/rockchip_linux_defconfig
index 2efcace..ff8d54f 100644
--- a/kernel/arch/arm64/configs/rockchip_linux_defconfig
+++ b/kernel/arch/arm64/configs/rockchip_linux_defconfig
@@ -4,10 +4,10 @@
 #
 
 #
-# Compiler: aarch64-none-linux-gnu-gcc (GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)) 10.3.1 20210621
+# Compiler: aarch64-linux-gnu-gcc (Linaro GCC 6.3-2017.05) 6.3.1 20170404
 #
 CONFIG_CC_IS_GCC=y
-CONFIG_GCC_VERSION=100301
+CONFIG_GCC_VERSION=60301
 CONFIG_CLANG_VERSION=0
 CONFIG_LLD_VERSION=0
 CONFIG_CC_HAS_ASM_GOTO=y
@@ -1311,7 +1311,7 @@
 #
 # EEPROM support
 #
-# CONFIG_EEPROM_AT24 is not set
+CONFIG_EEPROM_AT24=y
 # CONFIG_EEPROM_AT25 is not set
 # CONFIG_EEPROM_LEGACY is not set
 # CONFIG_EEPROM_MAX6875 is not set
diff --git a/kernel/drivers/misc/Makefile b/kernel/drivers/misc/Makefile
index 6f30411..313d46c 100644
--- a/kernel/drivers/misc/Makefile
+++ b/kernel/drivers/misc/Makefile
@@ -63,3 +63,4 @@
 obj-$(CONFIG_PIR_ASCHIP)	+= pir-aschip.o
 obj-$(CONFIG_RK803)		+= rk803.o
 obj-y				+= nkio/
+obj-y				+= nkmcu/
diff --git a/kernel/drivers/misc/eeprom/at24.c b/kernel/drivers/misc/eeprom/at24.c
index dc35376..dde0b45 100644
--- a/kernel/drivers/misc/eeprom/at24.c
+++ b/kernel/drivers/misc/eeprom/at24.c
@@ -6,6 +6,7 @@
  * Copyright (C) 2008 Wolfram Sang, Pengutronix
  */
 
+#define DEBUG
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -106,6 +107,8 @@
 module_param_named(write_timeout, at24_write_timeout, uint, 0);
 MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)");
 
+//Ben
+struct at24_data *at24_private=NULL;
 struct at24_chip_data {
 	/*
 	 * these fields mirror their equivalents in
@@ -421,6 +424,146 @@
 	return 0;
 }
 
+//add ben
+static ssize_t at24_read_private(struct at24_data *at24,
+		char *buf, loff_t off, size_t count)
+{
+	ssize_t retval = 0;
+
+	if (unlikely(!count))
+		return count;
+
+	if (off + count > at24->byte_len)
+		return -EINVAL;
+	
+	/*
+	 * Read data from chip, protecting against concurrent updates
+	 * from this host, but not from other I2C masters.
+	 */
+	mutex_lock(&at24->lock);
+
+	while (count) {
+		ssize_t	status;
+
+		//status = at24_eeprom_read_i2c(at24, buf, off, count);
+		status = at24_regmap_read(at24, buf, off, count);
+		if (status <= 0) {
+			if (retval == 0)
+				retval = status;
+			break;
+		}
+		buf += status;
+		off += status;
+		count -= status;
+		retval += status;
+	}
+
+	mutex_unlock(&at24->lock);
+
+	return retval;
+}
+
+#if 0
+static unsigned char AscToHex(unsigned char aChar)
+{
+    if((aChar>=0x30)&&(aChar<=0x39))
+        aChar -= 0x30;
+    else if((aChar>=0x41)&&(aChar<=0x46))
+        aChar -= 0x37;
+    else if((aChar>=0x61)&&(aChar<=0x66))
+        aChar -= 0x57;
+    else aChar = 0xff;
+
+    return aChar; 
+}
+#endif
+
+#if 0
+ssize_t at24_mac_read(unsigned char* addr)
+{
+	char buf[20];
+	char buf_tmp[12];
+	int i;
+	ssize_t ret;	
+	if (at24_private == NULL)
+	{
+		printk("ben %s: at24_private==null error\n", __func__);
+		return 0;
+	}
+	memset(buf, 0x00, 20);
+	memset(buf_tmp, 0x00, 12);
+	ret = at24_read(at24_private, 0, buf, 12);
+	if (ret > 0)
+	{	
+		for(i=0; i<12; i++)
+		{
+			buf_tmp[i] = AscToHex(buf[i]);
+		}
+		addr[0] = (buf_tmp[0] << 4) | buf_tmp[1];
+		addr[1] = (buf_tmp[2] << 4) | buf_tmp[3];
+		addr[2] = (buf_tmp[4] << 4) | buf_tmp[5];
+		addr[3] = (buf_tmp[6] << 4) | buf_tmp[7];
+		addr[4] = (buf_tmp[8] << 4) | buf_tmp[9];
+		addr[5] = (buf_tmp[10] << 4) | buf_tmp[11]; 	
+	}
+	return ret;
+}
+#endif
+
+ssize_t at24_mac_read(unsigned char* addr)
+{
+	char buf[20];
+	char buf_tmp[12];
+	ssize_t ret;	
+	if (at24_private == NULL)
+	{
+		printk("ben: at24_mac_read at24_private==null error");
+		return 0;
+	}
+	memset(buf, 0x00, 20);
+	memset(buf_tmp, 0x00, 12);
+	ret = at24_read_private(at24_private, buf, 0, 6);
+	if (ret > 0)
+	{	
+		addr[0] = buf[0];
+		addr[1] = buf[1];
+		addr[2] = buf[2];
+		addr[3] = buf[3];
+		addr[4] = buf[4];
+		addr[5] = buf[5];	
+	}
+	printk("at24_mac_read ...............\n");
+	return ret;
+}
+EXPORT_SYMBOL(at24_mac_read);
+
+ssize_t at24_mac1_read(unsigned char* mac)
+{
+	char buf[20];
+	char buf_tmp[12];
+	ssize_t ret;
+	if (at24_private == NULL)
+	{
+		printk("zcl: at24_mac_read at24_private==null error");
+		return 0;
+	}
+	memset(buf, 0x00, 20);
+	memset(buf_tmp, 0x00, 12);
+	ret = at24_read_private(at24_private, buf, 0x10, 6);
+	if (ret > 0)
+	{
+		*mac = buf[0];
+		*(mac + 1) = buf[1];
+		*(mac + 2) = buf[2];
+		*(mac + 3) = buf[3];
+		*(mac + 4) = buf[4];
+		*(mac + 5) = buf[5];
+	}
+	printk("at24_mac1_read ...............\n");
+	return ret;
+}
+EXPORT_SYMBOL(at24_mac1_read);
+
 static int at24_write(void *priv, unsigned int off, void *val, size_t count)
 {
 	struct at24_data *at24;
@@ -630,6 +773,7 @@
 	u8 test_byte;
 	int err;
 
+	printk("ben %s ...\n", __func__);
 	i2c_fn_i2c = i2c_check_functionality(client->adapter, I2C_FUNC_I2C);
 	i2c_fn_block = i2c_check_functionality(client->adapter,
 					       I2C_FUNC_SMBUS_WRITE_I2C_BLOCK);
@@ -674,6 +818,7 @@
 	if (!at24)
 		return -ENOMEM;
 
+	at24_private = at24;
 	mutex_init(&at24->lock);
 	at24->byte_len = pdata.byte_len;
 	at24->page_size = pdata.page_size;
@@ -792,7 +937,8 @@
 	at24_io_limit = rounddown_pow_of_two(at24_io_limit);
 	return i2c_add_driver(&at24_driver);
 }
-module_init(at24_init);
+//module_init(at24_init);
+postcore_initcall_sync(at24_init);
 
 static void __exit at24_exit(void)
 {

--
Gitblit v1.6.2