From 03469473733ccb461781e091233bb337f70417f1 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@openmoko.com>
Date: Fri, 25 Jul 2008 23:06:12 +0100
Subject: [PATCH] fix-pcf50633-rtc-i2c-bulk-autoincrement-simplify.patch

More pcf50633 major time saving by using i2c bulk autoincrement.  Code
reduction too by using array for time elements.

Signed-off-by: Andy Green <andy@openmoko.com>
---
 drivers/i2c/chips/pcf50633.c |  134 ++++++++++++++++++++++++------------------
 1 files changed, 77 insertions(+), 57 deletions(-)

diff --git a/drivers/i2c/chips/pcf50633.c b/drivers/i2c/chips/pcf50633.c
index 5c9d209..0406767 100644
--- a/drivers/i2c/chips/pcf50633.c
+++ b/drivers/i2c/chips/pcf50633.c
@@ -1504,37 +1504,42 @@ static void pcf50633_get_power_status(struct apm_power_info *info)
 /***********************************************************************
  * RTC
  ***********************************************************************/
+enum pcf50633_time_indexes {
+	PCF50633_TI_SEC = 0,
+	PCF50633_TI_MIN,
+	PCF50633_TI_HOUR,
+	PCF50633_TI_WKDAY,
+	PCF50633_TI_DAY,
+	PCF50633_TI_MONTH,
+	PCF50633_TI_YEAR,
+	PCF50633_TI_EXTENT /* always last */
+};
+
 
 struct pcf50633_time {
-	u_int8_t sec;
-	u_int8_t min;
-	u_int8_t hour;
-	u_int8_t wkday;
-	u_int8_t day;
-	u_int8_t month;
-	u_int8_t year;
+	u_int8_t time[PCF50633_TI_EXTENT];
 };
 
 static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
 {
-	rtc->tm_sec = BCD2BIN(pcf->sec);
-	rtc->tm_min = BCD2BIN(pcf->min);
-	rtc->tm_hour = BCD2BIN(pcf->hour);
-	rtc->tm_wday = BCD2BIN(pcf->wkday);
-	rtc->tm_mday = BCD2BIN(pcf->day);
-	rtc->tm_mon = BCD2BIN(pcf->month);
-	rtc->tm_year = BCD2BIN(pcf->year) + 100;
+	rtc->tm_sec = BCD2BIN(pcf->time[PCF50633_TI_SEC]);
+	rtc->tm_min = BCD2BIN(pcf->time[PCF50633_TI_MIN]);
+	rtc->tm_hour = BCD2BIN(pcf->time[PCF50633_TI_HOUR]);
+	rtc->tm_wday = BCD2BIN(pcf->time[PCF50633_TI_WKDAY]);
+	rtc->tm_mday = BCD2BIN(pcf->time[PCF50633_TI_DAY]);
+	rtc->tm_mon = BCD2BIN(pcf->time[PCF50633_TI_MONTH]);
+	rtc->tm_year = BCD2BIN(pcf->time[PCF50633_TI_YEAR]) + 100;
 }
 
 static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
 {
-	pcf->sec = BIN2BCD(rtc->tm_sec);
-	pcf->min = BIN2BCD(rtc->tm_min);
-	pcf->hour = BIN2BCD(rtc->tm_hour);
-	pcf->wkday = BIN2BCD(rtc->tm_wday);
-	pcf->day = BIN2BCD(rtc->tm_mday);
-	pcf->month = BIN2BCD(rtc->tm_mon);
-	pcf->year = BIN2BCD(rtc->tm_year - 100);
+	pcf->time[PCF50633_TI_SEC] = BIN2BCD(rtc->tm_sec);
+	pcf->time[PCF50633_TI_MIN] = BIN2BCD(rtc->tm_min);
+	pcf->time[PCF50633_TI_HOUR] = BIN2BCD(rtc->tm_hour);
+	pcf->time[PCF50633_TI_WKDAY] = BIN2BCD(rtc->tm_wday);
+	pcf->time[PCF50633_TI_DAY] = BIN2BCD(rtc->tm_mday);
+	pcf->time[PCF50633_TI_MONTH] = BIN2BCD(rtc->tm_mon);
+	pcf->time[PCF50633_TI_YEAR] = BIN2BCD(rtc->tm_year - 100);
 }
 
 static int pcf50633_rtc_ioctl(struct device *dev, unsigned int cmd,
@@ -1573,24 +1578,30 @@ static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf50633_data *pcf = i2c_get_clientdata(client);
 	struct pcf50633_time pcf_tm;
+	int ret;
 
 	mutex_lock(&pcf->lock);
-	pcf_tm.sec = __reg_read(pcf, PCF50633_REG_RTCSC);
-	pcf_tm.min = __reg_read(pcf, PCF50633_REG_RTCMN);
-	pcf_tm.hour = __reg_read(pcf, PCF50633_REG_RTCHR);
-	pcf_tm.wkday = __reg_read(pcf, PCF50633_REG_RTCWD);
-	pcf_tm.day = __reg_read(pcf, PCF50633_REG_RTCDT);
-	pcf_tm.month = __reg_read(pcf, PCF50633_REG_RTCMT);
-	pcf_tm.year = __reg_read(pcf, PCF50633_REG_RTCYR);
+
+	ret = i2c_smbus_read_i2c_block_data(&pcf->client,
+					    PCF50633_REG_RTCSC,
+					    PCF50633_TI_EXTENT,
+					    &pcf_tm.time[0]);
+	if (ret != PCF50633_TI_EXTENT)
+		dev_err(dev, "Failed to read time :-(\n");
+
 	mutex_unlock(&pcf->lock);
 
-	DEBUGP("PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
-		pcf_tm.day, pcf_tm.month, pcf_tm.year,
-		pcf_tm.hour, pcf_tm.min, pcf_tm.sec);
+	dev_info(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
+		pcf_tm.time[PCF50633_TI_DAY],
+		pcf_tm.time[PCF50633_TI_MONTH],
+		pcf_tm.time[PCF50633_TI_YEAR],
+		pcf_tm.time[PCF50633_TI_HOUR],
+		pcf_tm.time[PCF50633_TI_MIN],
+		pcf_tm.time[PCF50633_TI_SEC]);
 
 	pcf2rtc_time(tm, &pcf_tm);
 
-	DEBUGP("RTC_TIME: %u.%u.%u %u:%u:%u\n",
+	dev_info(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
 		tm->tm_mday, tm->tm_mon, tm->tm_year,
 		tm->tm_hour, tm->tm_min, tm->tm_sec);
 
@@ -1602,24 +1613,30 @@ static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf50633_data *pcf = i2c_get_clientdata(client);
 	struct pcf50633_time pcf_tm;
+	int ret;
 
-	DEBUGP("RTC_TIME: %u.%u.%u %u:%u:%u\n",
+	dev_info(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
 		tm->tm_mday, tm->tm_mon, tm->tm_year,
 		tm->tm_hour, tm->tm_min, tm->tm_sec);
 	rtc2pcf_time(&pcf_tm, tm);
-	DEBUGP("PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
-		pcf_tm.day, pcf_tm.month, pcf_tm.year,
-		pcf_tm.hour, pcf_tm.min, pcf_tm.sec);
+	dev_info(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
+		pcf_tm.time[PCF50633_TI_DAY],
+		pcf_tm.time[PCF50633_TI_MONTH],
+		pcf_tm.time[PCF50633_TI_YEAR],
+		pcf_tm.time[PCF50633_TI_HOUR],
+		pcf_tm.time[PCF50633_TI_MIN],
+		pcf_tm.time[PCF50633_TI_SEC]);
 
 	mutex_lock(&pcf->lock);
 	/* FIXME: disable second interrupt */
-	__reg_write(pcf, PCF50633_REG_RTCSC, pcf_tm.sec);
-	__reg_write(pcf, PCF50633_REG_RTCMN, pcf_tm.min);
-	__reg_write(pcf, PCF50633_REG_RTCHR, pcf_tm.hour);
-	__reg_write(pcf, PCF50633_REG_RTCWD, pcf_tm.wkday);
-	__reg_write(pcf, PCF50633_REG_RTCDT, pcf_tm.day);
-	__reg_write(pcf, PCF50633_REG_RTCMT, pcf_tm.month);
-	__reg_write(pcf, PCF50633_REG_RTCYR, pcf_tm.year);
+
+	ret = i2c_smbus_write_i2c_block_data(&pcf->client,
+					     PCF50633_REG_RTCSC,
+					     PCF50633_TI_EXTENT,
+					     &pcf_tm.time[0]);
+	if (ret)
+		dev_err(dev, "Failed to set time %d\n", ret);
+
 	/* FIXME: re-enable second interrupt */
 	mutex_unlock(&pcf->lock);
 
@@ -1631,17 +1648,20 @@ static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf50633_data *pcf = i2c_get_clientdata(client);
 	struct pcf50633_time pcf_tm;
+	int ret;
 
 	mutex_lock(&pcf->lock);
+
 	alrm->enabled =
 	     __reg_read(pcf, PCF50633_REG_INT1M) & PCF50633_INT1_ALARM ? 0 : 1;
-	pcf_tm.sec = __reg_read(pcf, PCF50633_REG_RTCSCA);
-	pcf_tm.min = __reg_read(pcf, PCF50633_REG_RTCMNA);
-	pcf_tm.hour = __reg_read(pcf, PCF50633_REG_RTCHRA);
-	pcf_tm.wkday = __reg_read(pcf, PCF50633_REG_RTCWDA);
-	pcf_tm.day = __reg_read(pcf, PCF50633_REG_RTCDTA);
-	pcf_tm.month = __reg_read(pcf, PCF50633_REG_RTCMTA);
-	pcf_tm.year = __reg_read(pcf, PCF50633_REG_RTCYRA);
+
+	ret = i2c_smbus_read_i2c_block_data(&pcf->client,
+					    PCF50633_REG_RTCSCA,
+					    PCF50633_TI_EXTENT,
+					    &pcf_tm.time[0]);
+	if (ret != PCF50633_TI_EXTENT)
+		dev_err(dev, "Failed to read Alarm time :-(\n");
+
 	mutex_unlock(&pcf->lock);
 
 	pcf2rtc_time(&alrm->time, &pcf_tm);
@@ -1655,6 +1675,7 @@ static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	struct pcf50633_data *pcf = i2c_get_clientdata(client);
 	struct pcf50633_time pcf_tm;
 	u_int8_t irqmask;
+	int ret;
 
 	rtc2pcf_time(&pcf_tm, &alrm->time);
 
@@ -1665,13 +1686,12 @@ static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	irqmask |= PCF50633_INT1_ALARM;
 	__reg_write(pcf, PCF50633_REG_INT1M, irqmask);
 
-	__reg_write(pcf, PCF50633_REG_RTCSCA, pcf_tm.sec);
-	__reg_write(pcf, PCF50633_REG_RTCMNA, pcf_tm.min);
-	__reg_write(pcf, PCF50633_REG_RTCHRA, pcf_tm.hour);
-	__reg_write(pcf, PCF50633_REG_RTCWDA, pcf_tm.wkday);
-	__reg_write(pcf, PCF50633_REG_RTCDTA, pcf_tm.day);
-	__reg_write(pcf, PCF50633_REG_RTCMTA, pcf_tm.month);
-	__reg_write(pcf, PCF50633_REG_RTCYRA, pcf_tm.year);
+	ret = i2c_smbus_write_i2c_block_data(&pcf->client,
+					     PCF50633_REG_RTCSCA,
+					     PCF50633_TI_EXTENT,
+					     &pcf_tm.time[0]);
+	if (ret)
+		dev_err(dev, "Failed to write alarm time :-( %d\n", ret);
 
 	if (alrm->enabled) {
 		/* (re-)enaable alarm interrupt */
-- 
1.5.6.3