linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: boris.brezillon@free-electrons.com (Boris Brezillon)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2 2/6] rtc: at91sam9: rework wakeup and interrupt handling
Date: Mon,  2 Mar 2015 10:18:14 +0100	[thread overview]
Message-ID: <1425287898-15093-3-git-send-email-boris.brezillon@free-electrons.com> (raw)
In-Reply-To: <1425287898-15093-1-git-send-email-boris.brezillon@free-electrons.com>

The IRQ line used by the RTC device is usually shared with the system timer
(PIT) on at91 platforms.
Since timers are registering their handlers with IRQF_NO_SUSPEND, we should
expect being called in suspended state, and properly wake the system up
when this is the case.

Set IRQF_COND_SUSPEND flag when registering the IRQ handler to inform
irq core that it can safely be called while the system is suspended.

Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
---
 drivers/rtc/rtc-at91sam9.c | 73 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 12 deletions(-)

diff --git a/drivers/rtc/rtc-at91sam9.c b/drivers/rtc/rtc-at91sam9.c
index 2183fd2..5ccaee3 100644
--- a/drivers/rtc/rtc-at91sam9.c
+++ b/drivers/rtc/rtc-at91sam9.c
@@ -23,6 +23,7 @@
 #include <linux/io.h>
 #include <linux/mfd/syscon.h>
 #include <linux/regmap.h>
+#include <linux/suspend.h>
 #include <linux/clk.h>
 
 /*
@@ -77,6 +78,9 @@ struct sam9_rtc {
 	unsigned int		gpbr_offset;
 	int 			irq;
 	struct clk		*sclk;
+	bool			suspended;
+	unsigned long		events;
+	spinlock_t		lock;
 };
 
 #define rtt_readl(rtc, field) \
@@ -271,14 +275,9 @@ static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
 	return 0;
 }
 
-/*
- * IRQ handler for the RTC
- */
-static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
+static irqreturn_t at91_rtc_cache_events(struct sam9_rtc *rtc)
 {
-	struct sam9_rtc *rtc = _rtc;
 	u32 sr, mr;
-	unsigned long events = 0;
 
 	/* Shared interrupt may be for another device.  Note: reading
 	 * SR clears it, so we must only read it in this irq handler!
@@ -290,18 +289,54 @@ static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
 
 	/* alarm status */
 	if (sr & AT91_RTT_ALMS)
-		events |= (RTC_AF | RTC_IRQF);
+		rtc->events |= (RTC_AF | RTC_IRQF);
 
 	/* timer update/increment */
 	if (sr & AT91_RTT_RTTINC)
-		events |= (RTC_UF | RTC_IRQF);
+		rtc->events |= (RTC_UF | RTC_IRQF);
+
+	return IRQ_HANDLED;
+}
+
+static void at91_rtc_flush_events(struct sam9_rtc *rtc)
+{
+	if (!rtc->events)
+		return;
 
-	rtc_update_irq(rtc->rtcdev, 1, events);
+	rtc_update_irq(rtc->rtcdev, 1, rtc->events);
+	rtc->events = 0;
 
 	pr_debug("%s: num=%ld, events=0x%02lx\n", __func__,
-		events >> 8, events & 0x000000FF);
+		rtc->events >> 8, rtc->events & 0x000000FF);
+}
 
-	return IRQ_HANDLED;
+/*
+ * IRQ handler for the RTC
+ */
+static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
+{
+	struct sam9_rtc *rtc = _rtc;
+	int ret;
+
+	spin_lock(&rtc->lock);
+
+	ret = at91_rtc_cache_events(rtc);
+
+	/* We're called in suspended state */
+	if (rtc->suspended) {
+		/* Mask irqs coming from this peripheral */
+		rtt_writel(rtc, MR,
+			   rtt_readl(rtc, MR) &
+			   ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
+		/* Trigger a system wakeup */
+		pm_system_wakeup();
+	} else {
+		at91_rtc_flush_events(rtc);
+	}
+
+	spin_unlock(&rtc->lock);
+
+	return ret;
 }
 
 static const struct rtc_class_ops at91_rtc_ops = {
@@ -421,7 +456,8 @@ static int at91_rtc_probe(struct platform_device *pdev)
 
 	/* register irq handler after we know what name we'll use */
 	ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt,
-				IRQF_SHARED, dev_name(&rtc->rtcdev->dev), rtc);
+			       IRQF_SHARED | IRQF_COND_SUSPEND,
+			       dev_name(&rtc->rtcdev->dev), rtc);
 	if (ret) {
 		dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq);
 		return ret;
@@ -482,7 +518,12 @@ static int at91_rtc_suspend(struct device *dev)
 	rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
 	if (rtc->imr) {
 		if (device_may_wakeup(dev) && (mr & AT91_RTT_ALMIEN)) {
+			unsigned long flags;
+
 			enable_irq_wake(rtc->irq);
+			spin_lock_irqsave(&rtc->lock, flags);
+			rtc->suspended = true;
+			spin_unlock_irqrestore(&rtc->lock, flags);
 			/* don't let RTTINC cause wakeups */
 			if (mr & AT91_RTT_RTTINCIEN)
 				rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
@@ -499,10 +540,18 @@ static int at91_rtc_resume(struct device *dev)
 	u32		mr;
 
 	if (rtc->imr) {
+		unsigned long flags;
+
 		if (device_may_wakeup(dev))
 			disable_irq_wake(rtc->irq);
 		mr = rtt_readl(rtc, MR);
 		rtt_writel(rtc, MR, mr | rtc->imr);
+
+		spin_lock_irqsave(&rtc->lock, flags);
+		rtc->suspended = false;
+		at91_rtc_cache_events(rtc);
+		at91_rtc_flush_events(rtc);
+		spin_unlock_irqrestore(&rtc->lock, flags);
 	}
 
 	return 0;
-- 
1.9.1

  parent reply	other threads:[~2015-03-02  9:18 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-02  9:18 [PATCH v2 0/6] ARM: at91: fix irq_pm_install_action WARNING Boris Brezillon
2015-03-02  9:18 ` [PATCH v2 1/6] PM / wakeup: export pm_system_wakeup symbol Boris Brezillon
2015-03-02  9:18 ` Boris Brezillon [this message]
2015-03-04 18:23   ` [PATCH v2 2/6] rtc: at91sam9: rework wakeup and interrupt handling Mark Rutland
2015-03-02  9:18 ` [PATCH v2 3/6] rtc: at91rm9200: " Boris Brezillon
2015-03-02  9:18 ` [PATCH v2 4/6] clk: at91: implement suspend/resume for the PMC irqchip Boris Brezillon
2015-03-09 22:34   ` Mike Turquette
2015-03-02  9:18 ` [PATCH v2 5/6] watchdog: at91sam9: request the irq with IRQF_NO_SUSPEND Boris Brezillon
2015-03-02 14:10   ` Guenter Roeck
2015-03-04 18:38   ` Mark Rutland
2015-03-04 21:41     ` Rafael J. Wysocki
2015-03-05 10:57       ` Mark Rutland
2015-03-05 15:10         ` Rafael J. Wysocki
2015-03-05 16:32           ` Mark Rutland
2015-03-06  0:29             ` Rafael J. Wysocki
2015-03-06 11:06               ` Mark Rutland
2015-03-06 12:39                 ` Rafael J. Wysocki
2015-03-06 13:10                   ` Mark Rutland
2015-03-07  9:12                 ` Peter Zijlstra
2015-03-07  9:06           ` Peter Zijlstra
2015-03-05  8:53     ` Boris Brezillon
2015-03-05 10:53       ` Mark Rutland
2015-03-05 11:17         ` Boris Brezillon
2015-03-05 11:31           ` Boris Brezillon
2015-03-05 11:53           ` Mark Rutland
2015-03-07  9:18             ` Peter Zijlstra
2015-03-07 10:20               ` Sylvain Rochet
2015-03-07 10:39                 ` Pavel Machek
2015-03-07 10:59                   ` Sylvain Rochet
2015-03-07 11:06                   ` Alexandre Belloni
2015-03-07 11:29                     ` Pavel Machek
2015-03-07 11:46                       ` Sylvain Rochet
2015-03-08  1:12                       ` Rafael J. Wysocki
2015-03-09  7:55                         ` Alexandre Belloni
2015-03-09 14:30                           ` Rafael J. Wysocki
2015-03-10 21:33                             ` Alexandre Belloni
2015-03-10 22:31                               ` Rafael J. Wysocki
2015-03-10 22:33                                 ` Alexandre Belloni
2015-03-11  1:03                                   ` Rafael J. Wysocki
2015-03-11  7:33                                     ` Boris Brezillon
2015-03-08  1:11                     ` Rafael J. Wysocki
2015-03-11  8:38                       ` Boris Brezillon
2015-03-11 11:17                         ` Nicolas Ferre
2015-03-02  9:18 ` [PATCH v2 6/6] tty: serial: atmel: rework interrupt and wakeup handling Boris Brezillon
2015-03-03  8:56 ` [PATCH v2 0/6] ARM: at91: fix irq_pm_install_action WARNING Alexandre Belloni
2015-03-03 15:35 ` Nicolas Ferre
2015-03-04  1:43   ` Rafael J. Wysocki
2015-03-04 18:43 ` Mark Rutland

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=1425287898-15093-3-git-send-email-boris.brezillon@free-electrons.com \
    --to=boris.brezillon@free-electrons.com \
    --cc=linux-arm-kernel@lists.infradead.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).