From: Igor Velkov via B4 Relay <devnull+iav.iav.lv@kernel.org>
To: John Stultz <jstultz@google.com>,
Thomas Gleixner <tglx@kernel.org>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Neil Armstrong <neil.armstrong@linaro.org>,
Kevin Hilman <khilman@baylibre.com>
Cc: Stephen Boyd <sboyd@kernel.org>,
Miroslav Lichvar <mlichvar@redhat.com>,
Jerome Brunet <jbrunet@baylibre.com>,
Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-amlogic@lists.infradead.org, linux-kernel@vger.kernel.org,
Igor Velkov <iav@iav.lv>
Subject: [PATCH v2 4/8] alarmtimer: Register the platform device without a parent
Date: Mon, 16 Nov 2026 00:25:03 +0000 [thread overview]
Message-ID: <20261116-b4-alarmtimer-rtc-priority-v2-4-b2c69721e489@iav.lv> (raw)
In-Reply-To: <20261116-b4-alarmtimer-rtc-priority-v2-0-b2c69721e489@iav.lv>
From: Igor Velkov <iav@iav.lv>
alarmtimer registers its platform device as a child of the wakeup RTC, so
the driver core suspends it before the RTC and alarmtimer_suspend() can
program the RTC. A device with one parent cannot be ordered against a
second RTC.
Register the device without a parent once the RTC is taken and add a
device link to the RTC, which keeps the device after the RTC in dpm_list
also when the RTC is moved there later, so it still suspends first.
Add links named after the suspend states, which point to the RTC that
wakes the system from that state. The names are those of
/sys/power/mem_sleep: in /sys/power/state "mem" stands for whichever
state mem_sleep selects, which can be s2idle.
The device moves from under the RTC to /sys/devices/platform, its name
stays alarmtimer.N.auto.
Signed-off-by: Igor Velkov <iav@iav.lv>
---
kernel/time/alarmtimer.c | 45 ++++++++++++++++++++++++++++++++++++++-------
1 file changed, 38 insertions(+), 7 deletions(-)
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 066d980fa6d5..4f87df95edcd 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -26,6 +26,7 @@
#include <linux/freezer.h>
#include <linux/compat.h>
#include <linux/module.h>
+#include <linux/suspend.h>
#include <linux/time_namespace.h>
#include "posix-timers.h"
@@ -53,12 +54,14 @@ static struct alarm_base {
/**
* struct rtc_wakeup - State for waking the system from suspend on alarms
* @rtc: RTC programmed on suspend, NULL if none
+ * @pdev: Device whose suspend and resume callbacks program the RTC
* @timer: RTC timer started on suspend
* @freezer_expires: Earliest expiry per alarm type of the clock_nanosleep()
* calls which the freezer interrupted, KTIME_MAX if none
*/
struct rtc_wakeup {
struct rtc_device *rtc;
+ struct platform_device *pdev;
struct rtc_timer timer;
ktime_t freezer_expires[ALARM_NUMTYPE];
};
@@ -71,6 +74,16 @@ static DEFINE_SPINLOCK(freezer_lock);
#endif
#ifdef CONFIG_RTC_CLASS
+/* Suspend state names as in /sys/power/mem_sleep */
+static const char * const alarmtimer_state_names[PM_SUSPEND_MAX] = {
+ [PM_SUSPEND_TO_IDLE] = "s2idle",
+ [PM_SUSPEND_STANDBY] = "shallow",
+ [PM_SUSPEND_MEM] = "deep",
+};
+
+/* Bounds of suspend_state_t as integers for loops, bit masks and indices */
+#define ALARMTIMER_STATE_MIN ((__force int)PM_SUSPEND_MIN)
+#define ALARMTIMER_STATE_MAX ((__force int)PM_SUSPEND_MAX)
/**
* alarmtimer_has_rtcdev - Check whether an RTC can wake the system for alarms
@@ -83,10 +96,20 @@ bool alarmtimer_has_rtcdev(void)
}
EXPORT_SYMBOL_GPL(alarmtimer_has_rtcdev);
+/* Show in sysfs which RTC wakes the system from @state */
+static void alarmtimer_link_state(int state, struct device *dev)
+{
+ const char *name = alarmtimer_state_names[state];
+
+ if (sysfs_create_link(&wakeup_ctl.pdev->dev.kobj, &dev->kobj, name))
+ dev_warn(&wakeup_ctl.pdev->dev, "Cannot link %s to %s\n", name, dev_name(dev));
+}
+
static int alarmtimer_rtc_add_device(struct device *dev)
{
struct rtc_device *rtc = to_rtc_device(dev);
struct platform_device *pdev;
+ int state;
if (wakeup_ctl.rtc)
return -EBUSY;
@@ -96,22 +119,30 @@ static int alarmtimer_rtc_add_device(struct device *dev)
if (!device_may_wakeup(rtc->dev.parent))
return -1;
- pdev = platform_device_register_data(dev, "alarmtimer",
+ if (!try_module_get(rtc->owner))
+ return -1;
+
+ pdev = platform_device_register_data(NULL, "alarmtimer",
PLATFORM_DEVID_AUTO, NULL, 0);
- if (IS_ERR(pdev))
+ if (IS_ERR(pdev)) {
+ module_put(rtc->owner);
return PTR_ERR(pdev);
-
- device_init_wakeup(&pdev->dev, true);
-
- if (!try_module_get(rtc->owner)) {
+ }
+ /* Keep the device after the RTC in dpm_list, so it suspends first */
+ if (!device_link_add(&pdev->dev, dev, DL_FLAG_STATELESS)) {
platform_device_unregister(pdev);
- return -1;
+ module_put(rtc->owner);
+ return -ENODEV;
}
+ device_init_wakeup(&pdev->dev, true);
+ wakeup_ctl.pdev = pdev;
WRITE_ONCE(wakeup_ctl.rtc, rtc);
/* Hold a reference so it doesn't go away */
get_device(dev);
+ for (state = ALARMTIMER_STATE_MIN; state < ALARMTIMER_STATE_MAX; state++)
+ alarmtimer_link_state(state, dev);
return 0;
}
--
2.43.0
next prev parent reply other threads:[~2026-11-16 0:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 0:24 [PATCH v2 0/8] alarmtimer: Pick the wakeup RTC per suspend state Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 1/8] alarmtimer: Replace alarmtimer_get_rtcdev() with alarmtimer_has_rtcdev() Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 2/8] alarmtimer: Move the wakeup state into struct rtc_wakeup Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 3/8] alarmtimer: Replace the freezer delta with per type expiry times Igor Velkov via B4 Relay
2026-11-16 0:25 ` Igor Velkov via B4 Relay [this message]
2026-11-16 0:25 ` [PATCH v2 5/8] alarmtimer: Keep an RTC per suspend state Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 6/8] dt-bindings: rtc: Add linux,wakeup-states Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 7/8] alarmtimer: Read the wakeup states of an RTC from linux,wakeup-states Igor Velkov via B4 Relay
2026-11-16 0:25 ` [PATCH v2 8/8] arm64: dts: amlogic: meson-sm1-odroid-hc4: Describe which RTC wakes the board Igor Velkov via B4 Relay
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=20261116-b4-alarmtimer-rtc-priority-v2-4-b2c69721e489@iav.lv \
--to=devnull+iav.iav.lv@kernel.org \
--cc=alexandre.belloni@bootlin.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=iav@iav.lv \
--cc=jbrunet@baylibre.com \
--cc=jstultz@google.com \
--cc=khilman@baylibre.com \
--cc=krzk+dt@kernel.org \
--cc=linux-amlogic@lists.infradead.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=martin.blumenstingl@googlemail.com \
--cc=mlichvar@redhat.com \
--cc=neil.armstrong@linaro.org \
--cc=robh@kernel.org \
--cc=sboyd@kernel.org \
--cc=tglx@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
all inboxes | Powered by JetHome®