Re: [PATCH RESEND] rtc: Add NXP PCF8523 support

From: Andrew Morton
Date: Tue Nov 27 2012 - 16:36:03 EST


On Fri, 23 Nov 2012 16:17:37 +0100
Thierry Reding <thierry.reding@xxxxxxxxxxxxxxxxx> wrote:

> This commit adds an RTC driver for PCF8523 chips by NXP Semiconductors.
> No support is currently provided for the alarm and interrupt functions.
> Only the time and date functionality is implemented.
>
> ...
>
> +static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
> +{
> + struct i2c_client *client = to_i2c_client(dev);
> + u8 start = REG_SECONDS, regs[7];
> + struct i2c_msg msgs[2];
> + int err;
> +
> + msgs[0].addr = client->addr;
> + msgs[0].flags = 0;
> + msgs[0].len = 1;
> + msgs[0].buf = &start;
> +
> + msgs[1].addr = client->addr;
> + msgs[1].flags = I2C_M_RD;
> + msgs[1].len = sizeof(regs);
> + msgs[1].buf = regs;
> +
> + err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
> + if (err < 0)
> + return err;
> +
> + if (regs[0] & REG_SECONDS_OS) {
> + dev_dbg(dev, "clock integrity is not guaranteed\n");
> +
> + /* try to clear the flag */

The comment explains the "what", which was pretty obvious from the code.

But readers want to know "why".

> + regs[0] &= ~REG_SECONDS_OS;
> +
> + err = pcf8523_write(client, REG_SECONDS, regs[0]);
> + if (err < 0)
> + return err;
> + }
> +
> + tm->tm_sec = bcd2bin(regs[0] & 0x7f);
> + tm->tm_min = bcd2bin(regs[1] & 0x7f);
> + tm->tm_hour = bcd2bin(regs[2] & 0x3f);
> + tm->tm_mday = bcd2bin(regs[3] & 0x3f);
> + tm->tm_wday = regs[4] & 0x7;
> + tm->tm_mon = bcd2bin(regs[5] & 0x1f);
> + tm->tm_year = bcd2bin(regs[6]) + 100;
> +
> + return rtc_valid_tm(tm);
> +}
> +
> +static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
> +{
> + struct i2c_client *client = to_i2c_client(dev);
> + struct i2c_msg msg;
> + u8 regs[8];
> + int err;
> +
> + err = pcf8523_stop_rtc(client);
> + if (err < 0)
> + return err;
> +
> + regs[0] = REG_SECONDS;
> + regs[1] = bin2bcd(tm->tm_sec);
> + regs[2] = bin2bcd(tm->tm_min);
> + regs[3] = bin2bcd(tm->tm_hour);
> + regs[4] = bin2bcd(tm->tm_mday);
> + regs[5] = tm->tm_wday;
> + regs[6] = bin2bcd(tm->tm_mon);
> + regs[7] = bin2bcd(tm->tm_year - 100);
> +
> + msg.addr = client->addr;
> + msg.flags = 0;
> + msg.len = sizeof(regs);
> + msg.buf = regs;
> +
> + err = i2c_transfer(client->adapter, &msg, 1);
> + if (err < 0)
> + return err;

Should we restart the rtc if the transfer failed?

> + return pcf8523_start_rtc(client);
> +}
>
> ...
>

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/