linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: jianbinz <quic_jianbinz@quicinc.com>
To: <alexandre.belloni@bootlin.com>, <a.zummo@towertech.it>
Cc: jianbinz <quic_jianbinz@quicinc.com>,
	<linux-kernel@vger.kernel.org>, <linux-rtc@vger.kernel.org>
Subject: [PATCH] rtc: Disable alarm irq if alarm time is less than rtc time and irq is enabled
Date: Thu, 23 Sep 2021 15:17:34 +0800	[thread overview]
Message-ID: <20210923071734.25545-1-quic_jianbinz@quicinc.com> (raw)

If device is boot up by rtc alarm, the alarm irq will still be enabled and the alarm time is smaller than current rtc time before any alarm is set or canceled.
If the device is shutdown this time, the device will reboot automatically.

So disable irq if alarm time is less than rtc time and irq is enabled.

Signed-off-by: jianbin zhang <quic_jianbinz@quicinc.com>
---
 drivers/rtc/rtc-pm8xxx.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/drivers/rtc/rtc-pm8xxx.c b/drivers/rtc/rtc-pm8xxx.c
index 29a1c65661e9..c8a75d3e9c43 100644
--- a/drivers/rtc/rtc-pm8xxx.c
+++ b/drivers/rtc/rtc-pm8xxx.c
@@ -267,16 +267,19 @@ static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
 {
 	int rc;
 	unsigned int ctrl_reg;
+	unsigned long irq_flags;
 	u8 value[NUM_8_BIT_RTC_REGS];
 	unsigned long secs;
 	struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
 	const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
 
+	spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
+
 	rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
 			      sizeof(value));
 	if (rc) {
 		dev_err(dev, "RTC alarm time read failed\n");
-		return rc;
+		goto rtc_rw_fail;
 	}
 
 	secs = value[0] | (value[1] << 8) | (value[2] << 16) |
@@ -287,14 +290,30 @@ static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
 	rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
 	if (rc) {
 		dev_err(dev, "Read from RTC alarm control register failed\n");
-		return rc;
+		goto rtc_rw_fail;
+	}
+
+	if (ctrl_reg && (rtc_tm_to_ktime(alarm->time) >=
+				rtc_dd->rtc->aie_timer.node.expires)) {
+		ctrl_reg &= ~regs->alarm_en;
+		rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
+		if (rc) {
+			dev_err(dev, "Update RTC control register failed \n");
+			goto rtc_rw_fail;
+		}
 	}
 	alarm->enabled = !!(ctrl_reg & PM8xxx_RTC_ALARM_ENABLE);
 
+	spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
+
 	dev_dbg(dev, "Alarm set for - h:m:s=%ptRt, y-m-d=%ptRdr\n",
 		&alarm->time, &alarm->time);
 
 	return 0;
+
+rtc_rw_fail:
+	spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
+	return rc;
 }
 
 static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
-- 
2.17.1


                 reply	other threads:[~2021-09-23  7:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210923071734.25545-1-quic_jianbinz@quicinc.com \
    --to=quic_jianbinz@quicinc.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.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;
as well as URLs for NNTP newsgroup(s).