linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [rtc-linux] [PATCH] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user
       [not found] <1432628260-24652-1-git-send-email-rnd4@dave-tech.it>
@ 2015-06-08 15:42 ` Alexandre Belloni
  2015-06-10 15:21   ` Andrea Scian
  0 siblings, 1 reply; 6+ messages in thread
From: Alexandre Belloni @ 2015-06-08 15:42 UTC (permalink / raw)
  To: rnd4; +Cc: r.cerrato, a.zummo, rtc-linux, linux-kernel, Andrea Scian

Hi,

This seems ok, a few comments below:

On 26/05/2015 at 10:17:40 +0200, rnd4@dave-tech.it wrote :
> From: Andrea Scian <andrea.scian@dave.eu>
> 
> I'm using PCF2127 in a custom ARM-based board and, by looking into PCF2127 datasheet 
> I've found that, in my understanding, it's wrong to say that the date in unreliable
> if BLF (battery low flag) is set but you should use OSF flag (seconds register) to
> check if oscillator, for any reason, stopped.
> Battery may be low (usually below 2V5 threshold) but the date may be anyway correct
> (tipically date is unreliable when input voltage is below 1V2).

typically -^

> diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
> index 1ee514a..2365788 100644
> --- a/drivers/rtc/rtc-pcf2127.c
> +++ b/drivers/rtc/rtc-pcf2127.c
> @@ -59,7 +59,16 @@ static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm)
>  	if (buf[PCF2127_REG_CTRL3] & 0x04) {
>  		pcf2127->voltage_low = 1;
>  		dev_info(&client->dev,
> -			"low voltage detected, date/time is not reliable.\n");
> +			"low voltage detected, check/replace RTC battery.\n");
> +	}
> +
> +	if (buf[PCF2127_REG_SC] & 0x80) {

Maybe use a define instead of 0x80, remember to use the BIT() macro.

> +		dev_warn(&client->dev,
> +			"oscillator stop detected, date/time is not reliable.\n");

I would return -EINVAL here because the result might still pass
rtc_valid_tm() but be outdated.

> +		/*
> +		 * no need clear the flag here,
> +		 * it will be cleared once the new date is saved
> +		 */
>  	}
>  
>  	dev_dbg(&client->dev,
> @@ -112,7 +121,7 @@ static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm)
>  	buf[i++] = PCF2127_REG_SC;
>  
>  	/* hours, minutes and seconds */
> -	buf[i++] = bin2bcd(tm->tm_sec);
> +	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OFS flag */
>  	buf[i++] = bin2bcd(tm->tm_min);
>  	buf[i++] = bin2bcd(tm->tm_hour);
>  	buf[i++] = bin2bcd(tm->tm_mday);
> @@ -144,7 +153,7 @@ static int pcf2127_rtc_ioctl(struct device *dev,
>  	switch (cmd) {
>  	case RTC_VL_READ:
>  		if (pcf2127->voltage_low)
> -			dev_info(dev, "low voltage detected, date/time is not reliable.\n");
> +			dev_info(dev, "low voltage detected, check/replace battery\n");

I would also print a warning about OFS here.

-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rtc-linux] [PATCH] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user
  2015-06-08 15:42 ` [rtc-linux] [PATCH] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user Alexandre Belloni
@ 2015-06-10 15:21   ` Andrea Scian
  2015-06-12  7:42     ` Alexandre Belloni
  0 siblings, 1 reply; 6+ messages in thread
From: Andrea Scian @ 2015-06-10 15:21 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: r.cerrato, a.zummo, rtc-linux, linux-kernel, Andrea Scian


Dear Alexandre,

On 08/06/2015 17:42, Alexandre Belloni wrote:
> Hi,
>
> This seems ok, a few comments below:
>
> On 26/05/2015 at 10:17:40 +0200, rnd4@dave-tech.it wrote :
>> From: Andrea Scian <andrea.scian@dave.eu>
>>
>> I'm using PCF2127 in a custom ARM-based board and, by looking into PCF2127 datasheet
>> I've found that, in my understanding, it's wrong to say that the date in unreliable
>> if BLF (battery low flag) is set but you should use OSF flag (seconds register) to
>> check if oscillator, for any reason, stopped.
>> Battery may be low (usually below 2V5 threshold) but the date may be anyway correct
>> (tipically date is unreliable when input voltage is below 1V2).
>
> typically -^


wooops.. thanks

>
>> diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
>> index 1ee514a..2365788 100644
>> --- a/drivers/rtc/rtc-pcf2127.c
>> +++ b/drivers/rtc/rtc-pcf2127.c
>> @@ -59,7 +59,16 @@ static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm)
>>   	if (buf[PCF2127_REG_CTRL3] & 0x04) {
>>   		pcf2127->voltage_low = 1;
>>   		dev_info(&client->dev,
>> -			"low voltage detected, date/time is not reliable.\n");
>> +			"low voltage detected, check/replace RTC battery.\n");
>> +	}
>> +
>> +	if (buf[PCF2127_REG_SC] & 0x80) {
>
> Maybe use a define instead of 0x80, remember to use the BIT() macro.

I'll fix it

I'll also use BIT() for the 0x04 above (in a different commit)

>
>> +		dev_warn(&client->dev,
>> +			"oscillator stop detected, date/time is not reliable.\n");
>
> I would return -EINVAL here because the result might still pass
> rtc_valid_tm() but be outdated.

At first look I agree with you, but a bit later they say:

/* the clock can give out invalid datetime, but we cannot return
  * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  */

http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/drivers/rtc/rtc-pcf2127.c#n91

so they don't actually return -EINVAL even if rtc_valid_tm() fails.
WDYT? I'm not an RTC subsystem expert, so maybe I'm missing something..

If the comment above is correct, so we can't return -EINVAL, I will 
reset the time to epoch, with something like

rtc_time64_to_tm((time64_t)0, tm);

and issue the warning.
Later userspace script usually reset the time to /etc/timestamp to avoid 
"back to future" problems ;-)

>
>> +		/*
>> +		 * no need clear the flag here,
>> +		 * it will be cleared once the new date is saved
>> +		 */
>>   	}
>>
>>   	dev_dbg(&client->dev,
>> @@ -112,7 +121,7 @@ static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm)
>>   	buf[i++] = PCF2127_REG_SC;
>>
>>   	/* hours, minutes and seconds */
>> -	buf[i++] = bin2bcd(tm->tm_sec);
>> +	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OFS flag */
>>   	buf[i++] = bin2bcd(tm->tm_min);
>>   	buf[i++] = bin2bcd(tm->tm_hour);
>>   	buf[i++] = bin2bcd(tm->tm_mday);
>> @@ -144,7 +153,7 @@ static int pcf2127_rtc_ioctl(struct device *dev,
>>   	switch (cmd) {
>>   	case RTC_VL_READ:
>>   		if (pcf2127->voltage_low)
>> -			dev_info(dev, "low voltage detected, date/time is not reliable.\n");
>> +			dev_info(dev, "low voltage detected, check/replace battery\n");
>
> I would also print a warning about OFS here.
>

I'll do.
Do you think is better to add another variable inside struct pcf2127 or 
is better to re-read the RTC registers?
(for the former I have also to clear the variable inside 
pcf2127_set_datetime(), for the latter I have to issue another read in a 
function that, at the moment, does not read anything..)

Thanks for you feedback and kind regards,

-- 

Andrea SCIAN

DAVE Embedded Systems

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rtc-linux] [PATCH] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user
  2015-06-10 15:21   ` Andrea Scian
@ 2015-06-12  7:42     ` Alexandre Belloni
  2015-06-15 15:57       ` Andrea Scian
  0 siblings, 1 reply; 6+ messages in thread
From: Alexandre Belloni @ 2015-06-12  7:42 UTC (permalink / raw)
  To: Andrea Scian; +Cc: r.cerrato, a.zummo, rtc-linux, linux-kernel, Andrea Scian

On 10/06/2015 at 17:21:57 +0200, Andrea Scian wrote :
> >I would return -EINVAL here because the result might still pass
> >rtc_valid_tm() but be outdated.
> 
> At first look I agree with you, but a bit later they say:
> 
> /* the clock can give out invalid datetime, but we cannot return
>  * -EINVAL otherwise hwclock will refuse to set the time on bootup.
>  */
> 
> http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/drivers/rtc/rtc-pcf2127.c#n91
> 
> so they don't actually return -EINVAL even if rtc_valid_tm() fails.
> WDYT? I'm not an RTC subsystem expert, so maybe I'm missing something..
> 

This has been copy pasted from other drivers and this is simply not
true.

> If the comment above is correct, so we can't return -EINVAL, I will reset
> the time to epoch, with something like
> 
> rtc_time64_to_tm((time64_t)0, tm);
> 

Doing that is worse. You really want userspace to know that the time is
invalid instead of giving an incorrect value. This allow userspace to
actually choose its policy when the time is invalid. For example, use
epoch or any other later date that probabyl makes more sense for the
product.

> >>@@ -144,7 +153,7 @@ static int pcf2127_rtc_ioctl(struct device *dev,
> >>  	switch (cmd) {
> >>  	case RTC_VL_READ:
> >>  		if (pcf2127->voltage_low)
> >>-			dev_info(dev, "low voltage detected, date/time is not reliable.\n");
> >>+			dev_info(dev, "low voltage detected, check/replace battery\n");
> >
> >I would also print a warning about OFS here.
> >
> 
> I'll do.
> Do you think is better to add another variable inside struct pcf2127 or is
> better to re-read the RTC registers?
> (for the former I have also to clear the variable inside
> pcf2127_set_datetime(), for the latter I have to issue another read in a
> function that, at the moment, does not read anything..)
> 

I don't really care. But since one of them is already cached, it is
probably better to cache OFS. Maybe you could also use voltage_low as a
bit field which would allow userspace to make the difference between a
simple low voltage and the time loss condition.


-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rtc-linux] [PATCH] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user
  2015-06-12  7:42     ` Alexandre Belloni
@ 2015-06-15 15:57       ` Andrea Scian
       [not found]         ` <1434447319-23007-1-git-send-email-rnd4@dave-tech.it>
       [not found]         ` <1434447587-23992-1-git-send-email-rnd4@dave-tech.it>
  0 siblings, 2 replies; 6+ messages in thread
From: Andrea Scian @ 2015-06-15 15:57 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: r.cerrato, a.zummo, rtc-linux, linux-kernel, Andrea Scian


Hi Alexandre,

On 12/06/2015 09:42, Alexandre Belloni wrote:
> On 10/06/2015 at 17:21:57 +0200, Andrea Scian wrote :
>>> I would return -EINVAL here because the result might still pass
>>> rtc_valid_tm() but be outdated.
>> At first look I agree with you, but a bit later they say:
>>
>> /* the clock can give out invalid datetime, but we cannot return
>>   * -EINVAL otherwise hwclock will refuse to set the time on bootup.
>>   */
>>
>> http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/drivers/rtc/rtc-pcf2127.c#n91
>>
>> so they don't actually return -EINVAL even if rtc_valid_tm() fails.
>> WDYT? I'm not an RTC subsystem expert, so maybe I'm missing something..
>>
> This has been copy pasted from other drivers and this is simply not
> true.

Thanks for point this out.
I'll split the patch to fix this for all PCF drivers (which have nearly 
all the same structure) and later add the OFS flag

>> If the comment above is correct, so we can't return -EINVAL, I will reset
>> the time to epoch, with something like
>>
>> rtc_time64_to_tm((time64_t)0, tm);
>>
> Doing that is worse. You really want userspace to know that the time is
> invalid instead of giving an incorrect value. This allow userspace to
> actually choose its policy when the time is invalid. For example, use
> epoch or any other later date that probabyl makes more sense for the
> product.

Most of minimal RFS I saw reset the date to what's inside /etc/timestamp 
(which is updated in runlevel 6). However, this is OT here.

>>>> @@ -144,7 +153,7 @@ static int pcf2127_rtc_ioctl(struct device *dev,
>>>>   	switch (cmd) {
>>>>   	case RTC_VL_READ:
>>>>   		if (pcf2127->voltage_low)
>>>> -			dev_info(dev, "low voltage detected, date/time is not reliable.\n");
>>>> +			dev_info(dev, "low voltage detected, check/replace battery\n");
>>> I would also print a warning about OFS here.
>>>
>> I'll do.
>> Do you think is better to add another variable inside struct pcf2127 or is
>> better to re-read the RTC registers?
>> (for the former I have also to clear the variable inside
>> pcf2127_set_datetime(), for the latter I have to issue another read in a
>> function that, at the moment, does not read anything..)
>>
> I don't really care. But since one of them is already cached, it is
> probably better to cache OFS. Maybe you could also use voltage_low as a
> bit field which would allow userspace to make the difference between a
> simple low voltage and the time loss condition.
>
>

I'll cache OFS too, in a different variable. Returning different values 
depending on OFS when querying about voltage low may mislead some 
application. Moreover I think that there's may be some cases when OFS is 
set and voltage low is not (e.g. when replacing battery with a brand new 
one).

I'll send the updated patch soon

Thanks for your comments/help,

-- 

Andrea SCIAN

DAVE Embedded Systems


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rtc-linux] [PATCH] driver: rtc: use rtc_valid_tm() error code when reading date/time
       [not found]         ` <1434447319-23007-1-git-send-email-rnd4@dave-tech.it>
@ 2015-07-19 22:00           ` Alexandre Belloni
  0 siblings, 0 replies; 6+ messages in thread
From: Alexandre Belloni @ 2015-07-19 22:00 UTC (permalink / raw)
  To: rnd4; +Cc: a.zummo, rtc-linux, linux-kernel, Andrea Scian

On 16/06/2015 at 11:35:19 +0200, rnd4@dave-tech.it wrote :
> From: Andrea Scian <andrea.scian@dave.eu>
> 
> There's a wrong commend in some RTC driver that say it's better to ignore
> rtc_valid_tm() when reading RTC timestamp. However this is wrong and is
> better to return to the userspace the an error if timestamp is not valid.
> 
> Signed-off-by: Andrea Scian <andrea.scian@dave.eu>
> ---
>  drivers/rtc/rtc-isl12022.c |    7 +------
>  drivers/rtc/rtc-pcf2123.c  |    8 +-------
>  drivers/rtc/rtc-pcf2127.c  |    8 +-------
>  drivers/rtc/rtc-pcf8563.c  |    8 +-------
>  4 files changed, 4 insertions(+), 27 deletions(-)
> 

Applied, after dropping the pcf8563 part as it was part of another
patch.


-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [rtc-linux] [PATCH v2] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user
       [not found]         ` <1434447587-23992-1-git-send-email-rnd4@dave-tech.it>
@ 2015-07-19 22:03           ` Alexandre Belloni
  0 siblings, 0 replies; 6+ messages in thread
From: Alexandre Belloni @ 2015-07-19 22:03 UTC (permalink / raw)
  To: rnd4; +Cc: a.zummo, rtc-linux, linux-kernel, Andrea Scian

On 16/06/2015 at 11:39:47 +0200, rnd4@dave-tech.it wrote :
> From: Andrea Scian <andrea.scian@dave.eu>
> 
> Signed-off-by: Andrea Scian <andrea.scian@dave.eu>
> ---
> 
> Changes since v1:
> * use BIT() to select the right bitfield instead of hardcode value
> * return -EINVAL if OFS is detected
> * cache OSF into driver structure to warn the user even later
> 
>  drivers/rtc/rtc-pcf2127.c |   26 +++++++++++++++++++++++---
>  1 file changed, 23 insertions(+), 3 deletions(-)
> 

Applied, after adding back the commit message. Thanks!


-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-07-19 22:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1432628260-24652-1-git-send-email-rnd4@dave-tech.it>
2015-06-08 15:42 ` [rtc-linux] [PATCH] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user Alexandre Belloni
2015-06-10 15:21   ` Andrea Scian
2015-06-12  7:42     ` Alexandre Belloni
2015-06-15 15:57       ` Andrea Scian
     [not found]         ` <1434447319-23007-1-git-send-email-rnd4@dave-tech.it>
2015-07-19 22:00           ` [rtc-linux] [PATCH] driver: rtc: use rtc_valid_tm() error code when reading date/time Alexandre Belloni
     [not found]         ` <1434447587-23992-1-git-send-email-rnd4@dave-tech.it>
2015-07-19 22:03           ` [rtc-linux] [PATCH v2] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).