rtc: pcf85063: simplify code to read the current time

By using i2c_smbus_read_i2c_block_data() the code is now much simpler.

While at it: when reading the RTC's seconds register, all time/date registers
are frozen until the RTC's year register is read. So it is important to read
all time/date registers in one turn to not lose a second event. Make it more
clear why the read must happen in this way.

Signed-off-by: Juergen Borleis <jbe@pengutronix.de>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
This commit is contained in:
Juergen Borleis 2016-02-09 11:57:25 +01:00 committed by Alexandre Belloni
parent bae2f64702
commit 7b5768486a
1 changed files with 20 additions and 25 deletions

View File

@ -45,39 +45,34 @@ struct pcf85063 {
*/
static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
{
int rc;
struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
unsigned char buf[13] = { PCF85063_REG_CTRL1 };
struct i2c_msg msgs[] = {
{/* setup read ptr */
.addr = client->addr,
.len = 1,
.buf = buf
},
{/* read status + date */
.addr = client->addr,
.flags = I2C_M_RD,
.len = 13,
.buf = buf
},
};
u8 regs[7];
/* read registers */
if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
dev_err(&client->dev, "%s: read error\n", __func__);
/*
* while reading, the time/date registers are blocked and not updated
* anymore until the access is finished. To not lose a second
* event, the access must be finished within one second. So, read all
* time/date registers in one turn.
*/
rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
sizeof(regs), regs);
if (rc != sizeof(regs)) {
dev_err(&client->dev, "date/time register read error\n");
return -EIO;
}
tm->tm_sec = bcd2bin(buf[PCF85063_REG_SC] & 0x7F);
tm->tm_min = bcd2bin(buf[PCF85063_REG_MN] & 0x7F);
tm->tm_hour = bcd2bin(buf[PCF85063_REG_HR] & 0x3F); /* rtc hr 0-23 */
tm->tm_mday = bcd2bin(buf[PCF85063_REG_DM] & 0x3F);
tm->tm_wday = buf[PCF85063_REG_DW] & 0x07;
tm->tm_mon = bcd2bin(buf[PCF85063_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
tm->tm_year = bcd2bin(buf[PCF85063_REG_YR]);
tm->tm_sec = bcd2bin(regs[0] & 0x7F);
tm->tm_min = bcd2bin(regs[1] & 0x7F);
tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
tm->tm_mday = bcd2bin(regs[3] & 0x3F);
tm->tm_wday = regs[4] & 0x07;
tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
tm->tm_year = bcd2bin(regs[6]);
if (tm->tm_year < 70)
tm->tm_year += 100; /* assume we are in 1970...2069 */
/* detect the polarity heuristically. see note above. */
pcf85063->c_polarity = (buf[PCF85063_REG_MO] & PCF85063_MO_C) ?
pcf85063->c_polarity = (regs[5] & PCF85063_MO_C) ?
(tm->tm_year >= 100) : (tm->tm_year < 100);
return rtc_valid_tm(tm);