Browse Source

fix century for ds1337

Helmut Pozimski 5 years ago
parent
commit
b14424a20c
1 changed files with 9 additions and 5 deletions
  1. 9 5
      rtc-ds13307.c

+ 9 - 5
rtc-ds13307.c

@@ -104,8 +104,7 @@ static int ds13307_start_oscillator(struct i2c_client *client) {
 
 static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
 	struct i2c_client *client;
-	int r;
-	int h12 = 0;
+	int r, century = 1, h12 = 0;
 	u8 buf[7], stopbit;
 	u8 addr = COMMON_SEC;
 	client = to_i2c_client(dev);
@@ -132,7 +131,8 @@ static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
 		}
 	}
 	if (model_detected == DEVICE_DS1337) {
-		buf[COMMON_MONTH] = buf[COMMON_MONTH] ^ COMMON_HIGH_BIT;
+		century = buf[COMMON_MONTH] & COMMON_HIGH_BIT;
+		buf[COMMON_MONTH] = buf[COMMON_MONTH] & 0x7F;
 	}
 
 	time->tm_sec = bcd2bin(buf[COMMON_SEC]);
@@ -144,7 +144,11 @@ static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
 	time->tm_mday = bcd2bin(buf[COMMON_DATE]);
 	time->tm_wday = bcd2bin(buf[COMMON_DAY]);
 	time->tm_mon = bcd2bin(buf[COMMON_MONTH]) - 1 ;
-	time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
+	if (century) {
+		time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
+	} else {
+		time->tm_year = bcd2bin(buf[COMMON_YEAR]);
+	}
 	return 0;
 }
 
@@ -161,7 +165,7 @@ static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
 	buf[1] = bin2bcd(time->tm_min);
 	buf[2] = bin2bcd(time->tm_hour);
 	buf[3] = bin2bcd(time->tm_mday);
-	if (model_detected == DEVICE_DS1337) {
+	if ((model_detected == DEVICE_DS1337) && (time->tm_year >= 100)) {
 		buf[4] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
 	} else {
 		buf[4] = bin2bcd(time->tm_mon + 1);