public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>,
	linux-rtc@vger.kernel.org,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] rtc: pcf-8563: Report previously detected low-voltage via RTC_VL_BACKUP_LOW
Date: Sat, 10 Jun 2023 10:31:35 +0200	[thread overview]
Message-ID: <20230610083135e40dd2f6@mail.local> (raw)
In-Reply-To: <da84b6b1-a9d8-ce46-16a9-e1a2d495240c@siemens.com>

Hello Jan,

On 09/06/2023 23:04:12+0200, Jan Kiszka wrote:
> From: Jan Kiszka <jan.kiszka@siemens.com>
> 
> The VL bit in the seconds register remains set only until seconds are
> written under main power. As this often happens during boot-up after
> picking up a network time, make sure to preserve the low battery state
> across this, caching it and returning it via the RTC_VL_BACKUP_LOW bit.
> 
> To permit userspace clearing this state during runtime, also implement
> RTC_VL_CLR that works against the cached state.
> 
> This is emulating RTCs which have a battery voltage check that works
> under main power as well.
> 

Emulating doesn't work well and I deliberately chose to not implement
it. For example, in your scenario, if you boot twice without using
VL_READ, you anyway have lost the information. This makes emulating
unreliabl. The fix you need is in userspace where you have to ensure you
read the status before setting the time.

> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
>  drivers/rtc/rtc-pcf8563.c | 17 ++++++++++++++---
>  1 file changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c
> index 7e720472213c..f8c6cdb9a39d 100644
> --- a/drivers/rtc/rtc-pcf8563.c
> +++ b/drivers/rtc/rtc-pcf8563.c
> @@ -81,6 +81,7 @@ struct pcf8563 {
>  #ifdef CONFIG_COMMON_CLK
>  	struct clk_hw		clkout_hw;
>  #endif
> +	bool low_bat;
>  };
>  
>  static int pcf8563_read_block_data(struct i2c_client *client, unsigned char reg,
> @@ -207,6 +208,7 @@ static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
>  		return err;
>  
>  	if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) {
> +		pcf8563->low_bat = true;
>  		dev_err(&client->dev,
>  			"low voltage detected, date/time is not reliable.\n");
>  		return -EINVAL;
> @@ -277,6 +279,8 @@ static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
>  static int pcf8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
>  {
>  	struct i2c_client *client = to_i2c_client(dev);
> +	struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
> +	unsigned int state = 0;
>  	int ret;
>  
>  	switch (cmd) {
> @@ -284,9 +288,16 @@ static int pcf8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long
>  		ret = i2c_smbus_read_byte_data(client, PCF8563_REG_SC);
>  		if (ret < 0)
>  			return ret;
> -
> -		return put_user(ret & PCF8563_SC_LV ? RTC_VL_DATA_INVALID : 0,
> -				(unsigned int __user *)arg);
> +		if (ret & PCF8563_SC_LV) {
> +			state |= RTC_VL_DATA_INVALID;
> +			pcf8563->low_bat = true;
> +		}
> +		if (pcf8563->low_bat)
> +			state |= RTC_VL_BACKUP_LOW;
> +		return put_user(state, (unsigned int __user *)arg);
> +	case RTC_VL_CLR:
> +		pcf8563->low_bat = false;
> +		return 0;
>  	default:
>  		return -ENOIOCTLCMD;
>  	}
> -- 
> 2.35.3

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

  reply	other threads:[~2023-06-10  8:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09 21:04 [PATCH] rtc: pcf-8563: Report previously detected low-voltage via RTC_VL_BACKUP_LOW Jan Kiszka
2023-06-10  8:31 ` Alexandre Belloni [this message]
2023-06-11 13:38   ` Jan Kiszka
2023-06-11 15:11     ` Alexandre Belloni
2023-06-11 16:28       ` Jan Kiszka
2023-06-11 22:16         ` Alexandre Belloni
2023-06-12  6:49           ` Jan Kiszka
2023-07-11  7:27             ` Jan Kiszka
2023-07-11 20:54               ` Alexandre Belloni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230610083135e40dd2f6@mail.local \
    --to=alexandre.belloni@bootlin.com \
    --cc=a.zummo@towertech.it \
    --cc=jan.kiszka@siemens.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox