All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rtc: rtc-stmp3xxx: detect power failure on backup power domain
@ 2017-05-18 14:45 Michael Thalmeier
  2017-05-18 15:56 ` Alexandre Belloni
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Thalmeier @ 2017-05-18 14:45 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni; +Cc: linux-rtc, linux-kernel, michael

To detect when the backup power domain has lost power a software defined bit
is set in one of the general purpose persistent registers when writing a new
time into the rtc.
When reading the time this bit is checked to determine if a power fail has
happened since the last time the rtc time was set.

When we detect a power fail we return -ENODATA.

Signed-off-by: Michael Thalmeier <michael.thalmeier@hale.at>
---
 drivers/rtc/rtc-stmp3xxx.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/rtc/rtc-stmp3xxx.c b/drivers/rtc/rtc-stmp3xxx.c
index d578e40..51330ec 100644
--- a/drivers/rtc/rtc-stmp3xxx.c
+++ b/drivers/rtc/rtc-stmp3xxx.c
@@ -62,6 +62,9 @@
 /* missing bitmask in headers */
 #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER	0x80000000
 
+#define STMP3XXX_RTC_PERSISTENT2		0x80
+#define STMP3XXX_RTC_PERSISTENT2_VALID_TIME	0x01
+
 struct stmp3xxx_rtc_data {
 	struct rtc_device *rtc;
 	void __iomem *io;
@@ -160,6 +163,10 @@ static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
 	if (ret)
 		return ret;
 
+	if (!(readl(rtc_data->io + STMP3XXX_RTC_PERSISTENT2) &
+				STMP3XXX_RTC_PERSISTENT2_VALID_TIME))
+		return -ENODATA;
+
 	rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
 	return 0;
 }
@@ -169,6 +176,8 @@ static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
 
 	writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
+	writel(STMP3XXX_RTC_PERSISTENT2_VALID_TIME,
+		rtc_data->io + STMP3XXX_RTC_PERSISTENT2 + STMP_OFFSET_REG_SET);
 	return stmp3xxx_wait_time(rtc_data);
 }
 
-- 
2.9.2

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

* Re: [PATCH] rtc: rtc-stmp3xxx: detect power failure on backup power domain
  2017-05-18 14:45 [PATCH] rtc: rtc-stmp3xxx: detect power failure on backup power domain Michael Thalmeier
@ 2017-05-18 15:56 ` Alexandre Belloni
  2017-06-07 13:05   ` Michael Thalmeier
  0 siblings, 1 reply; 4+ messages in thread
From: Alexandre Belloni @ 2017-05-18 15:56 UTC (permalink / raw)
  To: Michael Thalmeier; +Cc: Alessandro Zummo, linux-rtc, linux-kernel, michael

Hi,

On 18/05/2017 at 16:45:21 +0200, Michael Thalmeier wrote:
> To detect when the backup power domain has lost power a software defined bit
> is set in one of the general purpose persistent registers when writing a new
> time into the rtc.
> When reading the time this bit is checked to determine if a power fail has
> happened since the last time the rtc time was set.
> 

I'm kind of concerned that other people may want to use those register
for something else but I don't currently have anything better to
suggest.

The other concern is that when updating the kernel, this will make amm
the rtc report that the time is invalid until the next update. So this
should be disabled by default.

> When we detect a power fail we return -ENODATA.
> 

All the other drivers return -EINVAL in that case.

> Signed-off-by: Michael Thalmeier <michael.thalmeier@hale.at>
> ---
>  drivers/rtc/rtc-stmp3xxx.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/drivers/rtc/rtc-stmp3xxx.c b/drivers/rtc/rtc-stmp3xxx.c
> index d578e40..51330ec 100644
> --- a/drivers/rtc/rtc-stmp3xxx.c
> +++ b/drivers/rtc/rtc-stmp3xxx.c
> @@ -62,6 +62,9 @@
>  /* missing bitmask in headers */
>  #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER	0x80000000
>  
> +#define STMP3XXX_RTC_PERSISTENT2		0x80
> +#define STMP3XXX_RTC_PERSISTENT2_VALID_TIME	0x01
> +
>  struct stmp3xxx_rtc_data {
>  	struct rtc_device *rtc;
>  	void __iomem *io;
> @@ -160,6 +163,10 @@ static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
>  	if (ret)
>  		return ret;
>  
> +	if (!(readl(rtc_data->io + STMP3XXX_RTC_PERSISTENT2) &
> +				STMP3XXX_RTC_PERSISTENT2_VALID_TIME))
> +		return -ENODATA;
> +
>  	rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
>  	return 0;
>  }
> @@ -169,6 +176,8 @@ static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
>  	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
>  
>  	writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
> +	writel(STMP3XXX_RTC_PERSISTENT2_VALID_TIME,
> +		rtc_data->io + STMP3XXX_RTC_PERSISTENT2 + STMP_OFFSET_REG_SET);
>  	return stmp3xxx_wait_time(rtc_data);
>  }
>  
> -- 
> 2.9.2
> 

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

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

* Re: [PATCH] rtc: rtc-stmp3xxx: detect power failure on backup power domain
  2017-05-18 15:56 ` Alexandre Belloni
@ 2017-06-07 13:05   ` Michael Thalmeier
  2017-07-29 22:38     ` Alexandre Belloni
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Thalmeier @ 2017-06-07 13:05 UTC (permalink / raw)
  To: Alexandre Belloni; +Cc: Alessandro Zummo, linux-rtc, linux-kernel

On Thu, May 18, 2017 at 05:56:31PM +0200, Alexandre Belloni wrote:
> Hi,
> 
> On 18/05/2017 at 16:45:21 +0200, Michael Thalmeier wrote:
> > To detect when the backup power domain has lost power a software defined bit
> > is set in one of the general purpose persistent registers when writing a new
> > time into the rtc.
> > When reading the time this bit is checked to determine if a power fail has
> > happened since the last time the rtc time was set.
> > 
> 
> I'm kind of concerned that other people may want to use those register
> for something else but I don't currently have anything better to
> suggest.
> 
> The other concern is that when updating the kernel, this will make amm
> the rtc report that the time is invalid until the next update. So this
> should be disabled by default.

How would you think this is best to disable by default? With a config
option or with a module parameter?
Either way would also allow to make the bit mask in the PERSISTENT2
register configurable, so one can use whichever bit is still unused by
some other applications.

> 
> > When we detect a power fail we return -ENODATA.
> > 
> 
> All the other drivers return -EINVAL in that case.
> 
> > Signed-off-by: Michael Thalmeier <michael.thalmeier@hale.at>
> > ---
> >  drivers/rtc/rtc-stmp3xxx.c | 9 +++++++++
> >  1 file changed, 9 insertions(+)
> > 
> > diff --git a/drivers/rtc/rtc-stmp3xxx.c b/drivers/rtc/rtc-stmp3xxx.c
> > index d578e40..51330ec 100644
> > --- a/drivers/rtc/rtc-stmp3xxx.c
> > +++ b/drivers/rtc/rtc-stmp3xxx.c
> > @@ -62,6 +62,9 @@
> >  /* missing bitmask in headers */
> >  #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER	0x80000000
> >  
> > +#define STMP3XXX_RTC_PERSISTENT2		0x80
> > +#define STMP3XXX_RTC_PERSISTENT2_VALID_TIME	0x01
> > +
> >  struct stmp3xxx_rtc_data {
> >  	struct rtc_device *rtc;
> >  	void __iomem *io;
> > @@ -160,6 +163,10 @@ static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
> >  	if (ret)
> >  		return ret;
> >  
> > +	if (!(readl(rtc_data->io + STMP3XXX_RTC_PERSISTENT2) &
> > +				STMP3XXX_RTC_PERSISTENT2_VALID_TIME))
> > +		return -ENODATA;
> > +
> >  	rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
> >  	return 0;
> >  }
> > @@ -169,6 +176,8 @@ static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
> >  	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
> >  
> >  	writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
> > +	writel(STMP3XXX_RTC_PERSISTENT2_VALID_TIME,
> > +		rtc_data->io + STMP3XXX_RTC_PERSISTENT2 + STMP_OFFSET_REG_SET);
> >  	return stmp3xxx_wait_time(rtc_data);
> >  }
> >  
> > -- 
> > 2.9.2
> > 
> 
> -- 
> Alexandre Belloni, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

-- 

Michael Thalmeier
(Entwicklung)
HALE electronic GmbH
Eugen-Müller-Straße 18, 5020 Salzburg, Austria
Tel: +43 (662) 439011 0
Fax: +43 (662) 439011 9
michael.thalmeier@hale.at
Firmenbuchnummer: FN 66801m HG Salzburg

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

* Re: [PATCH] rtc: rtc-stmp3xxx: detect power failure on backup power domain
  2017-06-07 13:05   ` Michael Thalmeier
@ 2017-07-29 22:38     ` Alexandre Belloni
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Belloni @ 2017-07-29 22:38 UTC (permalink / raw)
  To: Michael Thalmeier; +Cc: Alessandro Zummo, linux-rtc, linux-kernel

On 07/06/2017 at 15:05:03 +0200, Michael Thalmeier wrote:
> On Thu, May 18, 2017 at 05:56:31PM +0200, Alexandre Belloni wrote:
> > Hi,
> > 
> > On 18/05/2017 at 16:45:21 +0200, Michael Thalmeier wrote:
> > > To detect when the backup power domain has lost power a software defined bit
> > > is set in one of the general purpose persistent registers when writing a new
> > > time into the rtc.
> > > When reading the time this bit is checked to determine if a power fail has
> > > happened since the last time the rtc time was set.
> > > 
> > 
> > I'm kind of concerned that other people may want to use those register
> > for something else but I don't currently have anything better to
> > suggest.
> > 
> > The other concern is that when updating the kernel, this will make amm
> > the rtc report that the time is invalid until the next update. So this
> > should be disabled by default.
> 
> How would you think this is best to disable by default? With a config
> option or with a module parameter?
> Either way would also allow to make the bit mask in the PERSISTENT2
> register configurable, so one can use whichever bit is still unused by
> some other applications.
> 

What you could do is export the persistent registers with nvmem. Then
you can define a cell in DT using a bit in persistent2 and use it from
the rtc driver itself.

If the cell is defined, then the driver know it has been configured to
detect power failure.


> > 
> > > When we detect a power fail we return -ENODATA.
> > > 
> > 
> > All the other drivers return -EINVAL in that case.
> > 
> > > Signed-off-by: Michael Thalmeier <michael.thalmeier@hale.at>
> > > ---
> > >  drivers/rtc/rtc-stmp3xxx.c | 9 +++++++++
> > >  1 file changed, 9 insertions(+)
> > > 
> > > diff --git a/drivers/rtc/rtc-stmp3xxx.c b/drivers/rtc/rtc-stmp3xxx.c
> > > index d578e40..51330ec 100644
> > > --- a/drivers/rtc/rtc-stmp3xxx.c
> > > +++ b/drivers/rtc/rtc-stmp3xxx.c
> > > @@ -62,6 +62,9 @@
> > >  /* missing bitmask in headers */
> > >  #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER	0x80000000
> > >  
> > > +#define STMP3XXX_RTC_PERSISTENT2		0x80
> > > +#define STMP3XXX_RTC_PERSISTENT2_VALID_TIME	0x01
> > > +
> > >  struct stmp3xxx_rtc_data {
> > >  	struct rtc_device *rtc;
> > >  	void __iomem *io;
> > > @@ -160,6 +163,10 @@ static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
> > >  	if (ret)
> > >  		return ret;
> > >  
> > > +	if (!(readl(rtc_data->io + STMP3XXX_RTC_PERSISTENT2) &
> > > +				STMP3XXX_RTC_PERSISTENT2_VALID_TIME))
> > > +		return -ENODATA;
> > > +
> > >  	rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
> > >  	return 0;
> > >  }
> > > @@ -169,6 +176,8 @@ static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
> > >  	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
> > >  
> > >  	writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
> > > +	writel(STMP3XXX_RTC_PERSISTENT2_VALID_TIME,
> > > +		rtc_data->io + STMP3XXX_RTC_PERSISTENT2 + STMP_OFFSET_REG_SET);
> > >  	return stmp3xxx_wait_time(rtc_data);
> > >  }
> > >  
> > > -- 
> > > 2.9.2
> > > 
> > 
> > -- 
> > Alexandre Belloni, Free Electrons
> > Embedded Linux and Kernel engineering
> > http://free-electrons.com
> 
> -- 
> 
> Michael Thalmeier
> (Entwicklung)
> HALE electronic GmbH
> Eugen-Müller-Straße 18, 5020 Salzburg, Austria
> Tel: +43 (662) 439011 0
> Fax: +43 (662) 439011 9
> michael.thalmeier@hale.at
> Firmenbuchnummer: FN 66801m HG Salzburg

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

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

end of thread, other threads:[~2017-07-29 22:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-18 14:45 [PATCH] rtc: rtc-stmp3xxx: detect power failure on backup power domain Michael Thalmeier
2017-05-18 15:56 ` Alexandre Belloni
2017-06-07 13:05   ` Michael Thalmeier
2017-07-29 22:38     ` Alexandre Belloni

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.