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

This commit is contained in:
jmcneill 2015-01-11 18:58:09 +00:00
parent 198c829970
commit ffda385d59

View File

@ -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_softc *sc, struct clock_ymdhms *dt)
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_softc *sc, struct clock_ymdhms *dt)
{
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_softc *sc, struct clock_ymdhms *dt)
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;
}