Module Name:    src
Committed By:   jmcneill
Date:           Sun Jan 11 18:58:09 UTC 2015

Modified Files:
        src/sys/dev/i2c: pcf8563.c

Log Message:
dont read cs1/cs2 regs when reading clock, only use polling mode when cold, 
invert logic for century flag (1 means 1900, 0 means 2000), set dt_wday when 
reading clock


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/i2c/pcf8563.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/i2c/pcf8563.c
diff -u src/sys/dev/i2c/pcf8563.c:1.5 src/sys/dev/i2c/pcf8563.c:1.6
--- src/sys/dev/i2c/pcf8563.c:1.5	Sun Jan 11 18:05:36 2015
+++ src/sys/dev/i2c/pcf8563.c	Sun Jan 11 18:58:09 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcf8563.c,v 1.5 2015/01/11 18:05:36 jmcneill Exp $	*/
+/*	$NetBSD: pcf8563.c,v 1.6 2015/01/11 18:58:09 jmcneill Exp $	*/
 
 /*
  * Copyright (c) 2011 Jonathan A. Kollasch
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.5 2015/01/11 18:05:36 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.6 2015/01/11 18:58:09 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -127,21 +127,22 @@ static int
 pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
 {
 	uint8_t bcd[PCF8563_NREGS];
-	uint8_t reg = PCF8563_R_CS1;
+	uint8_t reg = PCF8563_R_SECOND;
+	const int flags = cold ? I2C_F_POLL : 0;
 
-	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
+	if (iic_acquire_bus(sc->sc_tag, flags)) {
 		device_printf(sc->sc_dev, "acquire bus for read failed\n");
 		return 0;
 	}
 
 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
-		     &bcd[reg], PCF8563_R_YEAR - reg + 1, I2C_F_POLL)) {
-		iic_release_bus(sc->sc_tag, I2C_F_POLL);
+		     &bcd[reg], PCF8563_R_YEAR - reg + 1, flags)) {
+		iic_release_bus(sc->sc_tag, flags);
 		device_printf(sc->sc_dev, "read failed\n");
 		return 0;
 	}
 
-	iic_release_bus(sc->sc_tag, I2C_F_POLL);
+	iic_release_bus(sc->sc_tag, flags);
 
 	if (bcd[PCF8563_R_SECOND] & PCF8563_M_VL)
 		return 0;
@@ -150,10 +151,11 @@ pcf8563rtc_clock_read(struct pcf8563rtc_
 	dt->dt_min = bcdtobin(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
 	dt->dt_hour = bcdtobin(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
 	dt->dt_day = bcdtobin(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
+	dt->dt_wday = bcdtobin(bcd[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY);
 	dt->dt_mon = bcdtobin(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
 	dt->dt_year = 1900 +
 	    (bcdtobin(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR) % 100);
-	if (bcd[PCF8563_R_MONTH] & PCF8563_M_CENTURY)
+	if ((bcd[PCF8563_R_MONTH] & PCF8563_M_CENTURY) == 0)
 		dt->dt_year += 100;
 
 	return 1;
@@ -164,6 +166,7 @@ pcf8563rtc_clock_write(struct pcf8563rtc
 {
 	uint8_t bcd[PCF8563_NREGS];
 	uint8_t reg = PCF8563_R_SECOND;
+	const int flags = cold ? I2C_F_POLL : 0;
 
 	bcd[PCF8563_R_SECOND] = bintobcd(dt->dt_sec);
 	bcd[PCF8563_R_MINUTE] = bintobcd(dt->dt_min);
@@ -172,22 +175,22 @@ pcf8563rtc_clock_write(struct pcf8563rtc
 	bcd[PCF8563_R_WEEKDAY] = bintobcd(dt->dt_wday);
 	bcd[PCF8563_R_MONTH] = bintobcd(dt->dt_mon);
 	bcd[PCF8563_R_YEAR] = bintobcd(dt->dt_year % 100);
-	if (dt->dt_year >= 2000)
+	if (dt->dt_year < 2000)
 		bcd[PCF8563_R_MONTH] |= PCF8563_M_CENTURY;
 
-	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
+	if (iic_acquire_bus(sc->sc_tag, flags)) {
 		device_printf(sc->sc_dev, "acquire bus for write failed\n");
 		return 0;
 	}
 
 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, &reg, 1,
-		     &bcd[reg], PCF8563_R_YEAR - reg + 1, I2C_F_POLL)) {
-		iic_release_bus(sc->sc_tag, I2C_F_POLL);
+		     &bcd[reg], PCF8563_R_YEAR - reg + 1, flags)) {
+		iic_release_bus(sc->sc_tag, flags);
 		device_printf(sc->sc_dev, "write failed\n");
 		return 0;
 	}
 
-	iic_release_bus(sc->sc_tag, I2C_F_POLL);
+	iic_release_bus(sc->sc_tag, flags);
 
 	return 1;
 }

Reply via email to