From patchwork Mon Sep 26 19:13:53 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 4354 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 7FA0E23F57 for ; Mon, 26 Sep 2011 19:14:55 +0000 (UTC) Received: from mail-gy0-f180.google.com (mail-gy0-f180.google.com [209.85.160.180]) by fiordland.canonical.com (Postfix) with ESMTP id 38D72A18ABC for ; Mon, 26 Sep 2011 19:14:55 +0000 (UTC) Received: by gyd8 with SMTP id 8so6908745gyd.11 for ; Mon, 26 Sep 2011 12:14:54 -0700 (PDT) Received: by 10.223.57.17 with SMTP id a17mr6102739fah.65.1317064494187; Mon, 26 Sep 2011 12:14:54 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.3.234 with SMTP id f10cs48623laf; Mon, 26 Sep 2011 12:14:54 -0700 (PDT) Received: by 10.150.62.17 with SMTP id k17mr6890048yba.259.1317064493046; Mon, 26 Sep 2011 12:14:53 -0700 (PDT) Received: from e8.ny.us.ibm.com (e8.ny.us.ibm.com. [32.97.182.138]) by mx.google.com with ESMTPS id f11si10515263ybf.84.2011.09.26.12.14.52 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 26 Sep 2011 12:14:53 -0700 (PDT) Received-SPF: pass (google.com: domain of jstultz@us.ibm.com designates 32.97.182.138 as permitted sender) client-ip=32.97.182.138; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jstultz@us.ibm.com designates 32.97.182.138 as permitted sender) smtp.mail=jstultz@us.ibm.com Received: from d01relay03.pok.ibm.com (d01relay03.pok.ibm.com [9.56.227.235]) by e8.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p8QJ006h000716; Mon, 26 Sep 2011 15:00:00 -0400 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d01relay03.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p8QJEpCU103428; Mon, 26 Sep 2011 15:14:51 -0400 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p8QJE4pX012071; Mon, 26 Sep 2011 13:14:04 -0600 Received: from kernel.beaverton.ibm.com ([9.47.67.96]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p8QJE4mI012024; Mon, 26 Sep 2011 13:14:04 -0600 Received: by kernel.beaverton.ibm.com (Postfix, from userid 1056) id 025C01E7514; Mon, 26 Sep 2011 12:14:02 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , "Rafael J. Wysocki" , arve@android.com, markgross@thegnar.org, Alan Stern , amit.kucheria@linaro.org, farrowg@sg.ibm.com, "Dmitry Fink (Palm GBU)" , linux-pm@lists.linux-foundation.org, khilman@ti.com, Magnus Damm , mjg@redhat.com, peterz@infradead.org Subject: [PATCH 5/6] [RFC] alarmtimer: Add pm_stay_awake /pm_relax calls Date: Mon, 26 Sep 2011 12:13:53 -0700 Message-Id: <1317064434-1829-6-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.7.3.2.146.gca209 In-Reply-To: <1317064434-1829-1-git-send-email-john.stultz@linaro.org> References: <1317064434-1829-1-git-send-email-john.stultz@linaro.org> This provides wakelock like chaining to protects the RTC wakeup path to the hrtimer firing of the alarmtimer. CC: Rafael J. Wysocki CC: arve@android.com CC: markgross@thegnar.org CC: Alan Stern CC: amit.kucheria@linaro.org CC: farrowg@sg.ibm.com CC: Dmitry Fink (Palm GBU) CC: linux-pm@lists.linux-foundation.org CC: khilman@ti.com CC: Magnus Damm CC: mjg@redhat.com CC: peterz@infradead.org Signed-off-by: John Stultz --- kernel/time/alarmtimer.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 42 insertions(+), 2 deletions(-) diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index ea5e1a9..00ee80f 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -46,12 +46,17 @@ static struct alarm_base { static ktime_t freezer_delta; static DEFINE_SPINLOCK(freezer_delta_lock); + #ifdef CONFIG_RTC_CLASS /* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer; static struct rtc_device *rtcdev; static DEFINE_SPINLOCK(rtcdev_lock); +static struct wakeup_source *alarmtimer_wakelock; +static int stay_awake; +static DEFINE_SPINLOCK(stay_awake_lock); + /** * has_wakealarm - check rtc device has wakealarm ability * @dev: current device @@ -73,6 +78,19 @@ static int has_wakealarm(struct device *dev, void *name_ptr) return 1; } +/* rtctimer function called by first rtc interrupt after suspend */ +void alarmtimer_resume_call(void* p) +{ + unsigned long flags; + + spin_lock_irqsave(&stay_awake_lock, flags); + if (stay_awake == 1) { + stay_awake = 2; + __pm_stay_awake(alarmtimer_wakelock); + } + spin_unlock_irqrestore(&stay_awake_lock, flags); +} + /** * alarmtimer_get_rtcdev - Return selected rtcdevice * @@ -99,7 +117,7 @@ static struct rtc_device *alarmtimer_get_rtcdev(void) * rtc_open takes its own. */ put_device(dev); - rtc_timer_init(&rtctimer, NULL, NULL); + rtc_timer_init(&rtctimer, alarmtimer_resume_call, NULL); } } ret = rtcdev; @@ -158,6 +176,8 @@ static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm) } + + /** * alarmtimer_fired - Handles alarm hrtimer being fired. * @timer: pointer to hrtimer being run @@ -175,6 +195,8 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) ktime_t now; int ret = HRTIMER_NORESTART; + + __pm_stay_awake(alarmtimer_wakelock); spin_lock_irqsave(&base->lock, flags); now = base->gettime(); while ((next = timerqueue_getnext(&base->timerqueue))) { @@ -206,6 +228,13 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) } spin_unlock_irqrestore(&base->lock, flags); + spin_lock_irqsave(&stay_awake_lock, flags); + if (stay_awake == 2) + __pm_relax(alarmtimer_wakelock); + stay_awake = 0; + spin_unlock_irqrestore(&stay_awake_lock, flags); + __pm_relax(alarmtimer_wakelock); + return ret; } @@ -225,12 +254,14 @@ static int alarmtimer_suspend(struct device *dev) { struct rtc_time tm; ktime_t min, now; + int min_base; unsigned long flags; struct rtc_device *rtc; int i; spin_lock_irqsave(&freezer_delta_lock, flags); min = freezer_delta; + min_base = 0; freezer_delta = ktime_set(0, 0); spin_unlock_irqrestore(&freezer_delta_lock, flags); @@ -251,8 +282,10 @@ static int alarmtimer_suspend(struct device *dev) if (!next) continue; delta = ktime_sub(next->expires, base->gettime()); - if (!min.tv64 || (delta.tv64 < min.tv64)) + if (!min.tv64 || (delta.tv64 < min.tv64)) { min = delta; + min_base = i; + } } if (min.tv64 == 0) return 0; @@ -266,6 +299,10 @@ static int alarmtimer_suspend(struct device *dev) now = rtc_tm_to_ktime(tm); now = ktime_add(now, min); + spin_lock_irqsave(&stay_awake_lock, flags); + stay_awake=1; + spin_unlock_irqrestore(&stay_awake_lock, flags); + rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0)); return 0; @@ -703,6 +740,9 @@ static int __init alarmtimer_init(void) .nsleep = alarm_timer_nsleep, }; + alarmtimer_wakelock = wakeup_source_register("alarmtimer"); + stay_awake=0; + posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock); posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);