[PATCH 1/2] rtc: ds1307: support register offsets

Alexander Feilke <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <[email protected]>
From: Alexander Feilke <[email protected]>

Convert the scalar type data to a data struct that also
contains a register offset for otherwise compatible RTCs.

Signed-off-by: Alexander Feilke <[email protected]>
---
 drivers/rtc/ds1307.c | 87 +++++++++++++++++++++++++++++++-------------
 1 file changed, 61 insertions(+), 26 deletions(-)

diff --git a/drivers/rtc/ds1307.c b/drivers/rtc/ds1307.c
index 4176ab3701e..db9bf43214c 100644
--- a/drivers/rtc/ds1307.c
+++ b/drivers/rtc/ds1307.c
@@ -29,6 +29,11 @@ enum ds_type {
 	mcp794xx,
 };
 
+struct rtc_ds1370_data {
+	enum ds_type type;
+	int offset;
+};
+
 /*
  * RTC register addresses
  */
@@ -72,7 +77,7 @@ static int ds1307_rtc_set(struct udevice *dev, const struct rtc_time *tm)
 {
 	int ret;
 	uchar buf[7];
-	enum ds_type type = dev_get_driver_data(dev);
+	struct rtc_ds1370_data *data = (void*)dev_get_driver_data(dev);
 
 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
 	      tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
@@ -89,18 +94,18 @@ static int ds1307_rtc_set(struct udevice *dev, const struct rtc_time *tm)
 	buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
 	buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
 
-	if (type == mcp794xx) {
+	if (data->type == mcp794xx) {
 		buf[RTC_DAY_REG_ADDR] |= MCP7941X_BIT_VBATEN;
 		buf[RTC_SEC_REG_ADDR] |= MCP7941X_BIT_ST;
 	}
 
-	ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, data->offset, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
 
-	if (type == ds_1337) {
+	if (data->type == ds_1337) {
 		/* Ensure oscillator is enabled */
-		dm_i2c_reg_write(dev, DS1337_CTL_REG_ADDR, 0);
+		dm_i2c_reg_write(dev, plat->offset + DS1337_CTL_REG_ADDR, 0);
 	}
 
 	return 0;
@@ -110,21 +115,21 @@ static int ds1307_rtc_get(struct udevice *dev, struct rtc_time *tm)
 {
 	int ret;
 	uchar buf[7];
-	enum ds_type type = dev_get_driver_data(dev);
+	struct rtc_ds1370_data *data = (void*)dev_get_driver_data(dev);
 
-	ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
+	ret = dm_i2c_read(dev, data->offset, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
 
-	if (type == ds_1337 || type == ds_1339 || type == ds_1340) {
-		uint reg = (type == ds_1340) ? DS1340_STAT_REG_ADDR :
+	if (data->type == ds_1337 || data->type == ds_1339 || data->type == ds_1340) {
+		uint reg = (data->type == ds_1340) ? DS1340_STAT_REG_ADDR :
 					       DS1337_STAT_REG_ADDR;
-		int status = dm_i2c_reg_read(dev, reg);
+		int status = dm_i2c_reg_read(dev, data->offset + reg);
 
 		if (status >= 0 && (status & RTC_STAT_BIT_OSF)) {
 			printf("### Warning: RTC oscillator has stopped\n");
 			/* clear the OSF flag */
-			dm_i2c_reg_write(dev, reg, status & ~RTC_STAT_BIT_OSF);
+			dm_i2c_reg_write(dev, data->offset + reg, status & ~RTC_STAT_BIT_OSF);
 		}
 	}
 
@@ -150,7 +155,7 @@ static int ds1307_rtc_get(struct udevice *dev, struct rtc_time *tm)
 static int ds1307_rtc_reset(struct udevice *dev)
 {
 	int ret;
-	enum ds_type type = dev_get_driver_data(dev);
+	struct rtc_ds1370_data *data = (void*)dev_get_driver_data(dev);
 
 	/*
 	 * reset clock/oscillator in the seconds register:
@@ -158,26 +163,26 @@ static int ds1307_rtc_reset(struct udevice *dev)
 	 * on DS1340 bit 7 disables the oscillator (not EOSC)
 	 * on MCP794xx bit 7 enables Start Oscillator (ST)
 	 */
-	ret = dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR, 0x00);
+	ret = dm_i2c_reg_write(dev, data->offset + RTC_SEC_REG_ADDR, 0x00);
 	if (ret < 0)
 		return ret;
 
-	if (type == ds_1307) {
+	if (data->type == ds_1307) {
 		/* Write control register in order to enable square-wave
 		 * output (SQWE) and set a default rate of 32.768kHz (RS1|RS0).
 		 */
-		ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR,
+		ret = dm_i2c_reg_write(dev, data->offset + RTC_CTL_REG_ADDR,
 				       RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 |
 				       RTC_CTL_BIT_RS0);
-	} else if (type == ds_1337) {
+	} else if (data->type == ds_1337) {
 		/* Write control register in order to enable oscillator output
 		 * (not EOSC) and set a default rate of 32.768kHz (RS2|RS1).
 		 */
-		ret = dm_i2c_reg_write(dev, DS1337_CTL_REG_ADDR,
+		ret = dm_i2c_reg_write(dev, data->offset + DS1337_CTL_REG_ADDR,
 				       DS1337_CTL_BIT_RS2 | DS1337_CTL_BIT_RS1);
-	} else if (type == ds_1340 || type == mcp794xx || type == m41t11) {
+	} else if (data->type == ds_1340 || data->type == mcp794xx || data->type == m41t11) {
 		/* Reset clock calibration, frequency test and output level. */
-		ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0x00);
+		ret = dm_i2c_reg_write(dev, data->offset + RTC_CTL_REG_ADDR, 0x00);
 	}
 
 	return ret;
@@ -197,14 +202,44 @@ static const struct rtc_ops ds1307_rtc_ops = {
 	.reset = ds1307_rtc_reset,
 };
 
+static const struct rtc_ds1370_data ds_1307_data = {
+	.type   = ds_1307,
+	.offset = 0,
+};
+
+static const struct rtc_ds1370_data ds_1337_data = {
+	.type   = ds_1337,
+	.offset = 0,
+};
+
+static const struct rtc_ds1370_data ds_1339_data = {
+	.type   = ds_1339,
+	.offset = 0,
+};
+
+static const struct rtc_ds1370_data ds_1340_data = {
+	.type   = ds_1340,
+	.offset = 0,
+};
+
+static const struct rtc_ds1370_data mcp794xx_data = {
+	.type   = mcp794xx,
+	.offset = 0,
+};
+
+static const struct rtc_ds1370_data m41t11_data = {
+	.type   = m41t11,
+	.offset = 0,
+};
+
 static const struct udevice_id ds1307_rtc_ids[] = {
-	{ .compatible = "dallas,ds1307", .data = ds_1307 },
-	{ .compatible = "dallas,ds1337", .data = ds_1337 },
-	{ .compatible = "dallas,ds1339", .data = ds_1339 },
-	{ .compatible = "dallas,ds1340", .data = ds_1340 },
-	{ .compatible = "microchip,mcp7940x", .data = mcp794xx },
-	{ .compatible = "microchip,mcp7941x", .data = mcp794xx },
-	{ .compatible = "st,m41t11", .data = m41t11 },
+	{ .compatible = "dallas,ds1307", .data = (ulong)&ds_1307_data },
+	{ .compatible = "dallas,ds1337", .data = (ulong)&ds_1337_data },
+	{ .compatible = "dallas,ds1339", .data = (ulong)&ds_1339_data },
+	{ .compatible = "dallas,ds1340", .data = (ulong)&ds_1340_data },
+	{ .compatible = "microchip,mcp7940x", .data = (ulong)&mcp794xx_data },
+	{ .compatible = "microchip,mcp7941x", .data = (ulong)&mcp794xx_data },
+	{ .compatible = "st,m41t11", .data = (ulong)&m41t11_data },
 	{ }
 };
 
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.