[PATCH 1/2] rtc: Add flag for dynamic hctosys control

From: Akashdeep Kaur

Date: Wed Oct 21 2026 - 00:25:09 EST


Add flag to indicate whether RTC has valid absolute time on current
boot. This allows drivers to dynamically communicate RTC validity
per-boot to prevent hctosys from setting system time from invalid
RTC values.
The flag defaults to true for backward compatibility with existing
drivers.

Signed-off-by: Akashdeep Kaur <a-kaur@xxxxxx>
---
drivers/rtc/class.c | 15 +++++++++++++++
include/linux/rtc.h | 7 +++++++
2 files changed, 22 insertions(+)

diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 01ba04028f1f..cec27c476545 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -63,6 +63,18 @@ static void rtc_hctosys(struct rtc_device *rtc)
.tv_nsec = NSEC_PER_SEC >> 1,
};

+ /*
+ * If the RTC does not have valid time, do not set system time at boot.
+ * This can happen when RTC loses power or has never been initialized.
+ * PM resume compensation will still work via rtc_resume().
+ */
+ if (!rtc->time_is_valid) {
+ dev_info(rtc->dev.parent,
+ "hctosys: RTC time not valid, skipping\n");
+ err = -EINVAL;
+ goto err_read;
+ }
+
err = rtc_read_time(rtc, &tm);
if (err) {
dev_err(rtc->dev.parent,
@@ -243,6 +255,9 @@ static struct rtc_device *rtc_allocate_device(void)
hrtimer_setup(&rtc->pie_timer, rtc_pie_update_irq, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
rtc->pie_enabled = 0;

+ /* Assume RTC has valid time by default. Drivers can override. */
+ rtc->time_is_valid = true;
+
set_bit(RTC_FEATURE_ALARM, rtc->features);
set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);

diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index 0f43b5027235..f6f982610938 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -152,6 +152,13 @@ struct rtc_device {
time64_t offset_secs;
bool set_start_time;

+ /*
+ * Set by drivers to indicate RTC has valid absolute time.
+ * Used to prevent hctosys from using RTC when time is invalid
+ * (e.g., lost power, never initialized). PM compensation is unaffected.
+ */
+ bool time_is_valid;
+
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
struct work_struct uie_task;
struct timer_list uie_timer;
--
2.34.1