[U-Boot] [PATCH v5] rtc: pcf2127: Fixed bug with rtc settings and getting error time

The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.
This patch sets the register address using dm_i2c_write and reads the register data using the original dm_i2c_xfer in order to generate a stop signal after the register address is set, and fixes the bug of the original read and write time.
Signed-off-by: Biwen Li biwen.li@nxp.com Signed-off-by: Chuanhua Han chuanhua.han@nxp.com Reviewed-by: Lukasz Majewski lukma@denx.de --- Changes in v5: - The modification made by i2c core layer is removed, and the RTC time bug is fixed in pcf2127 driver using dm_i2c_xfer. Changes in v4: - Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with DM_I2C_CHIP_ADDR_STOP. Changes in v3: - When the rtc time is obtained, the address of the set register is separated from the read data. Changes in v2: - Split the original patch into 3 patches - Add detailed description information for each patch
drivers/rtc/pcf2127.c | 33 ++++++++++++++++++++++++--------- 1 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/drivers/rtc/pcf2127.c b/drivers/rtc/pcf2127.c index dcf0340..f695350 100644 --- a/drivers/rtc/pcf2127.c +++ b/drivers/rtc/pcf2127.c @@ -22,14 +22,32 @@ #define PCF2127_REG_MO 0x08 #define PCF2127_REG_YR 0x09
+static int pcf2127_read_reg(struct udevice *dev, uint offset, + u8 *buffer, int len) +{ + struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); + struct i2c_msg msg; + int ret; + + /* Set the address of the start register to be read */ + ret = dm_i2c_write(dev, offset, NULL, 0); + if (ret < 0) + return ret; + + /* Read register's data */ + msg.addr = chip->chip_addr; + msg.flags |= I2C_M_RD; + msg.len = len; + msg.buf = buffer; + + return dm_i2c_xfer(dev, &msg, 1); +} + static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) { - uchar buf[8]; + uchar buf[7] = {0}; int i = 0, ret;
- /* start register address */ - buf[i++] = PCF2127_REG_SC; - /* hours, minutes and seconds */ buf[i++] = bin2bcd(tm->tm_sec); buf[i++] = bin2bcd(tm->tm_min); @@ -44,7 +62,7 @@ static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
/* write register's data */ - ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); + ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
return ret; } @@ -54,10 +72,7 @@ static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm) int ret = 0; uchar buf[10] = { PCF2127_REG_CTRL1 };
- ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1); - if (ret < 0) - return ret; - ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); + ret = pcf2127_read_reg(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); if (ret < 0) return ret;

Hello Chuanhua Han,
Am 08.07.2019 um 05:45 schrieb Chuanhua Han:
The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.
This patch sets the register address using dm_i2c_write and reads the register data using the original dm_i2c_xfer in order to generate a stop signal after the register address is set, and fixes the bug of the original read and write time.
Signed-off-by: Biwen Li biwen.li@nxp.com Signed-off-by: Chuanhua Han chuanhua.han@nxp.com Reviewed-by: Lukasz Majewski lukma@denx.de
Changes in v5:
- The modification made by i2c core layer is removed,
and the RTC time bug is fixed in pcf2127 driver using dm_i2c_xfer. Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP. Changes in v3:
- When the rtc time is obtained, the address of the set register
is separated from the read data. Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch
drivers/rtc/pcf2127.c | 33 ++++++++++++++++++++++++--------- 1 files changed, 24 insertions(+), 9 deletions(-)
Looks good to me now, many thanks!
Reviewed-by: Heiko Schocher hs@denx.de
bye, Heiko

-----Original Message----- From: Heiko Schocher hs@denx.de Sent: 2019年7月9日 16:50 To: Chuanhua Han chuanhua.han@nxp.com Cc: lukma@denx.de; Prabhakar Kushwaha prabhakar.kushwaha@nxp.com; Biwen Li biwen.li@nxp.com; sjg@chromium.org; u-boot@lists.denx.de Subject: [EXT] Re: [PATCH v5] rtc: pcf2127: Fixed bug with rtc settings and getting error time
Caution: EXT Email
Hello Chuanhua Han,
Am 08.07.2019 um 05:45 schrieb Chuanhua Han:
The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.
This patch sets the register address using dm_i2c_write and reads the register data using the original dm_i2c_xfer in order to generate a stop signal after the register address is set, and fixes the bug of the original read and write time.
Signed-off-by: Biwen Li biwen.li@nxp.com Signed-off-by: Chuanhua Han chuanhua.han@nxp.com Reviewed-by: Lukasz Majewski lukma@denx.de
Changes in v5: - The modification made by i2c core layer is removed, and the RTC time bug is fixed in pcf2127 driver using dm_i2c_xfer. Changes in v4: - Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with DM_I2C_CHIP_ADDR_STOP. Changes in v3: - When the rtc time is obtained, the address of the set register is separated from the read data. Changes in v2: - Split the original patch into 3 patches - Add detailed description information for each patch
drivers/rtc/pcf2127.c | 33 ++++++++++++++++++++++++--------- 1 files changed, 24 insertions(+), 9 deletions(-)
Looks good to me now, many thanks!
Thank you very much for your advice!
Reviewed-by: Heiko Schocher hs@denx.de
bye, Heiko
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-52 Fax: +49-8142-66989-80 Email: hs@denx.de
participants (2)
-
Chuanhua Han
-
Heiko Schocher